Browse code

Added name labels for objects on isometric maps

For rectangles, polygons and tile objects.

Andrew Motrenko authored on 25/05/2012 14:56:28 • Thorbjørn Lindeijer committed on 09/06/2012 20:43:05
Showing 1 changed files
... ...
@@ -66,23 +66,26 @@ QRect IsometricRenderer::boundingRect(const QRect &rect) const
66 66
 
67 67
 QRectF IsometricRenderer::boundingRect(const MapObject *object) const
68 68
 {
69
+    const int nameHeight = object->name().isEmpty() ? 0 : 15;
70
+
69 71
     if (object->tile()) {
70 72
         const QPointF bottomCenter = tileToPixelCoords(object->position());
71 73
         const QPixmap &img = object->tile()->image();
72 74
         return QRectF(bottomCenter.x() - img.width() / 2,
73 75
                       bottomCenter.y() - img.height(),
74 76
                       img.width(),
75
-                      img.height()).adjusted(-1, -1, 1, 1);
77
+                      img.height()).adjusted(-1, -1 - nameHeight, 1, 1);
76 78
     } else if (!object->polygon().isEmpty()) {
77 79
         const QPointF &pos = object->position();
78 80
         const QPolygonF polygon = object->polygon().translated(pos);
79 81
         const QPolygonF screenPolygon = tileToPixelCoords(polygon);
80
-        return screenPolygon.boundingRect().adjusted(-2, -2, 3, 3);
82
+        return screenPolygon.boundingRect().adjusted(-2, -2 - nameHeight, 3, 3);
81 83
     } else {
82 84
         // Take the bounding rect of the projected object, and then add a few
83 85
         // pixels on all sides to correct for the line width.
84 86
         const QRectF base = tileRectToPolygon(object->bounds()).boundingRect();
85
-        return base.adjusted(-2, -3, 2, 2);
87
+
88
+        return base.adjusted(-2, -3 - nameHeight, 2, 2);
86 89
     }
87 90
 }
88 91
 
... ...
@@ -307,6 +310,12 @@ void IsometricRenderer::drawMapObject(QPainter *painter,
307 310
         paintOrigin += tileToPixelCoords(object->position()).toPoint();
308 311
         painter->drawPixmap(paintOrigin, img);
309 312
 
313
+        const QFontMetrics fm = painter->fontMetrics();
314
+        QString name = fm.elidedText(object->name(), Qt::ElideRight,
315
+                                     img.width() + 2);
316
+        if (!name.isEmpty())
317
+            painter->drawText(QPoint(paintOrigin.x(), paintOrigin.y() - 5 + 1), name);
318
+
310 319
         pen.setStyle(Qt::SolidLine);
311 320
         painter->setPen(pen);
312 321
         painter->drawRect(QRectF(paintOrigin, img.size()));
... ...
@@ -314,6 +323,10 @@ void IsometricRenderer::drawMapObject(QPainter *painter,
314 323
         pen.setColor(color);
315 324
         painter->setPen(pen);
316 325
         painter->drawRect(QRectF(paintOrigin, img.size()));
326
+
327
+        if (!name.isEmpty())
328
+            painter->drawText(QPoint(paintOrigin.x(), paintOrigin.y() - 5), name);
329
+
317 330
     } else {
318 331
         QColor brushColor = color;
319 332
         brushColor.setAlpha(50);
... ...
@@ -331,8 +344,24 @@ void IsometricRenderer::drawMapObject(QPainter *painter,
331 344
 
332 345
         switch (object->shape()) {
333 346
         case MapObject::Rectangle: {
347
+
348
+            QPointF topLeft(tileToPixelCoords(object->bounds().topLeft()));
349
+            QPointF bottomLeft(tileToPixelCoords(object->bounds().bottomLeft()));
350
+            QPointF topRight(tileToPixelCoords(object->bounds().topRight()));
351
+
352
+            const qreal headerX = bottomLeft.x();
353
+            const qreal headerY = topLeft.y();
354
+
355
+            QRectF rect(bottomLeft, topRight);
356
+
357
+            const QFontMetrics fm = painter->fontMetrics();
358
+            QString name = fm.elidedText(object->name(), Qt::ElideRight,
359
+                                         rect.width() + 2);
360
+
334 361
             QPolygonF polygon = tileRectToPolygon(object->bounds());
335 362
             painter->drawPolygon(polygon);
363
+            if (!name.isEmpty())
364
+                painter->drawText(QPoint(headerX, headerY - 5 + 1), name);
336 365
 
337 366
             pen.setColor(color);
338 367
             painter->setPen(pen);
... ...
@@ -340,6 +369,8 @@ void IsometricRenderer::drawMapObject(QPainter *painter,
340 369
             polygon.translate(0, -1);
341 370
 
342 371
             painter->drawPolygon(polygon);
372
+            if (!name.isEmpty())
373
+                painter->drawText(QPoint(headerX, headerY - 5), name);
343 374
             break;
344 375
         }
345 376
         case MapObject::Polygon: {
... ...
@@ -347,6 +378,15 @@ void IsometricRenderer::drawMapObject(QPainter *painter,
347 378
             const QPolygonF polygon = object->polygon().translated(pos);
348 379
             QPolygonF screenPolygon = tileToPixelCoords(polygon);
349 380
 
381
+            const QRectF polygonBoundingRect = screenPolygon.boundingRect();
382
+
383
+            const QFontMetrics fm = painter->fontMetrics();
384
+            QString name = fm.elidedText(object->name(), Qt::ElideRight,
385
+                                         polygonBoundingRect.width() + 2);
386
+
387
+            if (!name.isEmpty())
388
+                painter->drawText(QPoint(polygonBoundingRect.left(), polygonBoundingRect.top() - 5 + 1), name);
389
+
350 390
             painter->drawPolygon(screenPolygon);
351 391
 
352 392
             pen.setColor(color);
... ...
@@ -355,6 +395,10 @@ void IsometricRenderer::drawMapObject(QPainter *painter,
355 395
             screenPolygon.translate(0, -1);
356 396
 
357 397
             painter->drawPolygon(screenPolygon);
398
+
399
+            if (!name.isEmpty())
400
+                painter->drawText(QPoint(polygonBoundingRect.left(), polygonBoundingRect.top() - 5), name);
401
+
358 402
             break;
359 403
         }
360 404
         case MapObject::Polyline: {