Browse Source

Merge pull request #24 from huderlem/coords

Display hovered map tile and block info
yenatch 6 years ago
parent
commit
6bb945d798
No account linked to committer's email address
7 changed files with 137 additions and 8 deletions
  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 View File

257
     emit map->paintTileChanged(map);
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
 void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
279
 void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
261
     QPointF pos = event->pos();
280
     QPointF pos = event->pos();
262
     int x = ((int)pos.x()) / 16;
281
     int x = ((int)pos.x()) / 16;
269
     }
288
     }
270
 }
289
 }
271
 void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
290
 void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
291
+    updateCurHoveredMetatile(event->pos());
272
     mousePressEvent(event);
292
     mousePressEvent(event);
273
 }
293
 }
274
 void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
294
 void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
275
     mousePressEvent(event);
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
 void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
324
 void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
280
     if (map) {
325
     if (map) {
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
 void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
434
 void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
372
     emit mouseEvent(event, this);
435
     emit mouseEvent(event, this);
373
 }
436
 }
374
 void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
437
 void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
438
+    updateCurHoveredTile(event->pos());
375
     emit mouseEvent(event, this);
439
     emit mouseEvent(event, this);
376
 }
440
 }
377
 void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
441
 void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {

+ 15
- 0
editor.h View File

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

+ 10
- 0
mainwindow.cpp View File

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

+ 3
- 0
mainwindow.h View File

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

+ 0
- 8
mainwindow.ui View File

1217
    <addaction name="menuFile"/>
1217
    <addaction name="menuFile"/>
1218
    <addaction name="menuEdit"/>
1218
    <addaction name="menuEdit"/>
1219
   </widget>
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
   <widget class="QStatusBar" name="statusBar"/>
1220
   <widget class="QStatusBar" name="statusBar"/>
1229
   <action name="action_Save_Project">
1221
   <action name="action_Save_Project">
1230
    <property name="text">
1222
    <property name="text">

+ 36
- 0
map.cpp View File

734
 bool Map::hasUnsavedChanges() {
734
 bool Map::hasUnsavedChanges() {
735
     return !history.isSaved() || !isPersistedToFile;
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 View File

182
     void cacheBorder();
182
     void cacheBorder();
183
 
183
 
184
     bool hasUnsavedChanges();
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
     QList<QList<QRgb> > getBlockPalettes(int metatile_index);
194
     QList<QList<QRgb> > getBlockPalettes(int metatile_index);
187
 
195
 
189
     void paintTileChanged(Map *map);
197
     void paintTileChanged(Map *map);
190
     void paintCollisionChanged(Map *map);
198
     void paintCollisionChanged(Map *map);
191
     void mapChanged(Map *map);
199
     void mapChanged(Map *map);
200
+    void statusBarMessage(QString);
192
 
201
 
193
 public slots:
202
 public slots:
194
 };
203
 };