Explorar el Código

Merge pull request #24 from huderlem/coords

Display hovered map tile and block info
yenatch hace 6 años
padre
commit
6bb945d798
No account linked to committer's email address
Se han modificado 7 ficheros con 137 adiciones y 8 borrados
  1. 64
    0
      editor.cpp
  2. 15
    0
      editor.h
  3. 10
    0
      mainwindow.cpp
  4. 3
    0
      mainwindow.h
  5. 0
    8
      mainwindow.ui
  6. 36
    0
      map.cpp
  7. 9
    0
      map.h

+ 64
- 0
editor.cpp Ver fichero

@@ -257,6 +257,25 @@ void MetatilesPixmapItem::pick(uint tile) {
257 257
     emit map->paintTileChanged(map);
258 258
 }
259 259
 
260
+void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
261
+    int x = ((int)pos.x()) / 16;
262
+    int y = ((int)pos.y()) / 16;
263
+    int width = pixmap().width() / 16;
264
+    int height = pixmap().height() / 16;
265
+    if (x < 0 || x >= width || y < 0 || y >= height) {
266
+        map->clearHoveredMetatile();
267
+    } else {
268
+        int block = y * width + x;
269
+        map->hoveredMetatileChanged(block);
270
+    }
271
+}
272
+
273
+void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
274
+    updateCurHoveredMetatile(event->pos());
275
+}
276
+void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
277
+    map->clearHoveredMetatile();
278
+}
260 279
 void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
261 280
     QPointF pos = event->pos();
262 281
     int x = ((int)pos.x()) / 16;
@@ -269,12 +288,38 @@ void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
269 288
     }
270 289
 }
271 290
 void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
291
+    updateCurHoveredMetatile(event->pos());
272 292
     mousePressEvent(event);
273 293
 }
274 294
 void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
275 295
     mousePressEvent(event);
276 296
 }
277 297
 
298
+void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
299
+    int x = ((int)pos.x()) / 16;
300
+    int y = ((int)pos.y()) / 16;
301
+    int width = pixmap().width() / 16;
302
+    int height = pixmap().height() / 16;
303
+    if (x < 0 || x >= width || y < 0 || y >= height) {
304
+        map->clearHoveredCollisionTile();
305
+    } else {
306
+        int collision = y * width + x;
307
+        map->hoveredCollisionTileChanged(collision);
308
+    }
309
+}
310
+
311
+void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
312
+    int x = ((int)pos.x()) / 16;
313
+    int y = ((int)pos.y()) / 16;
314
+    int width = pixmap().width() / 16;
315
+    int height = pixmap().height() / 16;
316
+    if (x < 0 || x >= width || y < 0 || y >= height) {
317
+        map->clearHoveredElevationTile();
318
+    } else {
319
+        int elevation = y * width + x;
320
+        map->hoveredElevationTileChanged(elevation);
321
+    }
322
+}
278 323
 
279 324
 void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
280 325
     if (map) {
@@ -368,10 +413,29 @@ void MapPixmapItem::redo() {
368 413
     }
369 414
 }
370 415
 
416
+void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
417
+    int x = ((int)pos.x()) / 16;
418
+    int y = ((int)pos.y()) / 16;
419
+    int blockIndex = y * map->getWidth() + x;
420
+    if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
421
+        map->clearHoveredTile();
422
+    } else {
423
+        int tile = map->blockdata->blocks->at(blockIndex).tile;
424
+        map->hoveredTileChanged(x, y, tile);
425
+    }
426
+}
427
+
428
+void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
429
+    updateCurHoveredTile(event->pos());
430
+}
431
+void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
432
+    map->clearHoveredTile();
433
+}
371 434
 void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
372 435
     emit mouseEvent(event, this);
373 436
 }
374 437
 void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
438
+    updateCurHoveredTile(event->pos());
375 439
     emit mouseEvent(event, this);
376 440
 }
377 441
 void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {

+ 15
- 0
editor.h Ver fichero

@@ -185,6 +185,7 @@ public:
185 185
     Map *map = NULL;
186 186
     MapPixmapItem(Map *map_) {
187 187
         map = map_;
188
+        setAcceptHoverEvents(true);
188 189
     }
189 190
     bool active;
190 191
     bool right_click;
@@ -198,10 +199,15 @@ public:
198 199
     virtual void redo();
199 200
     virtual void draw();
200 201
 
202
+private:
203
+    void updateCurHoveredTile(QPointF pos);
204
+
201 205
 signals:
202 206
     void mouseEvent(QGraphicsSceneMouseEvent *, MapPixmapItem *);
203 207
 
204 208
 protected:
209
+    void hoverMoveEvent(QGraphicsSceneHoverEvent*);
210
+    void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
205 211
     void mousePressEvent(QGraphicsSceneMouseEvent*);
206 212
     void mouseMoveEvent(QGraphicsSceneMouseEvent*);
207 213
     void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
@@ -235,14 +241,19 @@ public:
235 241
     }
236 242
     MetatilesPixmapItem(Map *map_) {
237 243
         map = map_;
244
+        setAcceptHoverEvents(true);
238 245
         connect(map, SIGNAL(paintTileChanged(Map*)), this, SLOT(paintTileChanged(Map *)));
239 246
     }
240 247
     Map* map = NULL;
241 248
     virtual void pick(uint);
242 249
     virtual void draw();
250
+protected:
251
+    virtual void updateCurHoveredMetatile(QPointF pos);
243 252
 private slots:
244 253
     void paintTileChanged(Map *map);
245 254
 protected:
255
+    void hoverMoveEvent(QGraphicsSceneHoverEvent*);
256
+    void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
246 257
     void mousePressEvent(QGraphicsSceneMouseEvent*);
247 258
     void mouseMoveEvent(QGraphicsSceneMouseEvent*);
248 259
     void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
@@ -261,6 +272,8 @@ public:
261 272
     virtual void draw() {
262 273
         setPixmap(map->renderCollisionMetatiles());
263 274
     }
275
+protected:
276
+    virtual void updateCurHoveredMetatile(QPointF pos);
264 277
 private slots:
265 278
     void paintCollisionChanged(Map *map) {
266 279
         draw();
@@ -280,6 +293,8 @@ public:
280 293
     virtual void draw() {
281 294
         setPixmap(map->renderElevationMetatiles());
282 295
     }
296
+protected:
297
+    virtual void updateCurHoveredMetatile(QPointF pos);
283 298
 private slots:
284 299
     void paintCollisionChanged(Map *map) {
285 300
         draw();

+ 10
- 0
mainwindow.cpp Ver fichero

@@ -48,10 +48,17 @@ MainWindow::~MainWindow()
48 48
     delete ui;
49 49
 }
50 50
 
51
+void MainWindow::setStatusBarMessage(QString message, int timeout/* = 0*/) {
52
+    statusBar()->showMessage(message, timeout);
53
+}
54
+
51 55
 void MainWindow::openProject(QString dir) {
52 56
     if (dir.isNull()) {
53 57
         return;
54 58
     }
59
+
60
+    setStatusBarMessage(QString("Opening project %1").arg(dir));
61
+
55 62
     bool already_open = (
56 63
         (editor != NULL && editor != nullptr)
57 64
         && (editor->project != NULL && editor->project != nullptr)
@@ -69,6 +76,8 @@ void MainWindow::openProject(QString dir) {
69 76
         loadDataStructures();
70 77
         populateMapList();
71 78
     }
79
+
80
+    setStatusBarMessage(QString("Opened project %1").arg(dir));
72 81
 }
73 82
 
74 83
 QString MainWindow::getDefaultMap() {
@@ -169,6 +178,7 @@ void MainWindow::setMap(QString map_name) {
169 178
     setWindowTitle(map_name + " - " + editor->project->getProjectTitle() + " - pretmap");
170 179
 
171 180
     connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *)));
181
+    connect(editor->map, SIGNAL(statusBarMessage(QString)), this, SLOT(setStatusBarMessage(QString)));
172 182
 
173 183
     setRecentMap(map_name);
174 184
     updateMapList();

+ 3
- 0
mainwindow.h Ver fichero

@@ -25,6 +25,9 @@ public:
25 25
     explicit MainWindow(QWidget *parent = 0);
26 26
     ~MainWindow();
27 27
 
28
+public slots:
29
+    void setStatusBarMessage(QString message, int timeout = 0);
30
+
28 31
 private slots:
29 32
     void on_action_Open_Project_triggered();
30 33
     void on_mapList_activated(const QModelIndex &index);

+ 0
- 8
mainwindow.ui Ver fichero

@@ -1217,14 +1217,6 @@
1217 1217
    <addaction name="menuFile"/>
1218 1218
    <addaction name="menuEdit"/>
1219 1219
   </widget>
1220
-  <widget class="QToolBar" name="mainToolBar">
1221
-   <attribute name="toolBarArea">
1222
-    <enum>TopToolBarArea</enum>
1223
-   </attribute>
1224
-   <attribute name="toolBarBreak">
1225
-    <bool>false</bool>
1226
-   </attribute>
1227
-  </widget>
1228 1220
   <widget class="QStatusBar" name="statusBar"/>
1229 1221
   <action name="action_Save_Project">
1230 1222
    <property name="text">

+ 36
- 0
map.cpp Ver fichero

@@ -734,3 +734,39 @@ void Map::addEvent(Event *event) {
734 734
 bool Map::hasUnsavedChanges() {
735 735
     return !history.isSaved() || !isPersistedToFile;
736 736
 }
737
+
738
+void Map::hoveredTileChanged(int x, int y, int block) {
739
+    emit statusBarMessage(QString("X: %1, Y: %2, Block: 0x%3")
740
+                          .arg(x)
741
+                          .arg(y)
742
+                          .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
743
+}
744
+
745
+void Map::clearHoveredTile() {
746
+    emit statusBarMessage(QString(""));
747
+}
748
+
749
+void Map::hoveredMetatileChanged(int block) {
750
+    emit statusBarMessage(QString("Block: 0x%1")
751
+                          .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
752
+}
753
+
754
+void Map::clearHoveredMetatile() {
755
+    emit statusBarMessage(QString(""));
756
+}
757
+
758
+void Map::hoveredCollisionTileChanged(int collision) {
759
+    emit statusBarMessage(QString("Collision: %1").arg(collision));
760
+}
761
+
762
+void Map::clearHoveredCollisionTile() {
763
+    emit statusBarMessage(QString(""));
764
+}
765
+
766
+void Map::hoveredElevationTileChanged(int elevation) {
767
+    emit statusBarMessage(QString("Elevation: %1").arg(elevation));
768
+}
769
+
770
+void Map::clearHoveredElevationTile() {
771
+    emit statusBarMessage(QString(""));
772
+}

+ 9
- 0
map.h Ver fichero

@@ -182,6 +182,14 @@ public:
182 182
     void cacheBorder();
183 183
 
184 184
     bool hasUnsavedChanges();
185
+    void hoveredTileChanged(int x, int y, int block);
186
+    void clearHoveredTile();
187
+    void hoveredMetatileChanged(int block);
188
+    void clearHoveredMetatile();
189
+    void hoveredCollisionTileChanged(int collision);
190
+    void clearHoveredCollisionTile();
191
+    void hoveredElevationTileChanged(int elevation);
192
+    void clearHoveredElevationTile();
185 193
 
186 194
     QList<QList<QRgb> > getBlockPalettes(int metatile_index);
187 195
 
@@ -189,6 +197,7 @@ signals:
189 197
     void paintTileChanged(Map *map);
190 198
     void paintCollisionChanged(Map *map);
191 199
     void mapChanged(Map *map);
200
+    void statusBarMessage(QString);
192 201
 
193 202
 public slots:
194 203
 };