Browse Source

Merge remote-tracking branch 'origin/master' into connect

yenatch 6 years ago
parent
commit
4280735c1e
4 changed files with 86 additions and 31 deletions
  1. 38
    14
      editor.cpp
  2. 18
    6
      editor.h
  3. 26
    9
      map.cpp
  4. 4
    2
      map.h

+ 38
- 14
editor.cpp View File

780
     if ((x >= 0 && x < width) && (y >=0 && y < height)) {
780
     if ((x >= 0 && x < width) && (y >=0 && y < height)) {
781
         int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
781
         int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
782
         int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
782
         int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
783
-        map->paint_tile = baseTileY * 8 + baseTileX;
783
+        map->paint_tile_index = baseTileY * 8 + baseTileX;
784
         map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
784
         map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
785
         map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
785
         map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
786
         map->smart_paths_enabled = button == Qt::RightButton
786
         map->smart_paths_enabled = button == Qt::RightButton
790
     }
790
     }
791
 }
791
 }
792
 
792
 
793
+void MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
794
+    QPointF pos = event->pos();
795
+    int x = ((int)pos.x()) / 16;
796
+    int y = ((int)pos.y()) / 16;
797
+    int width = pixmap().width() / 16;
798
+    int height = pixmap().height() / 16;
799
+    if ((x >= 0 && x < width) && (y >=0 && y < height)) {
800
+        pick(y * width + x);
801
+    }
802
+}
803
+void MovementPermissionsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
804
+    updateCurHoveredMetatile(event->pos());
805
+    mousePressEvent(event);
806
+}
807
+void MovementPermissionsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
808
+    mousePressEvent(event);
809
+}
810
+
793
 void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
811
 void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
794
     int x = ((int)pos.x()) / 16;
812
     int x = ((int)pos.x()) / 16;
795
     int y = ((int)pos.y()) / 16;
813
     int y = ((int)pos.y()) / 16;
907
         int actualY = j + y;
925
         int actualY = j + y;
908
         Block *block = map->getBlock(actualX, actualY);
926
         Block *block = map->getBlock(actualX, actualY);
909
         if (block) {
927
         if (block) {
910
-            block->tile = map->paint_tile + i + (j * 8);
928
+            block->tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
911
             map->_setBlock(actualX, actualY, *block);
929
             map->_setBlock(actualX, actualY, *block);
912
         }
930
         }
913
     }
931
     }
935
     8 + 1, // 1111
953
     8 + 1, // 1111
936
 });
954
 });
937
 
955
 
938
-#define IS_SMART_PATH_TILE(block) ((block->tile >= map->paint_tile && block->tile < map->paint_tile + 3) \
939
-                                || (block->tile >= map->paint_tile + 8 && block->tile < map->paint_tile + 11) \
940
-                                || (block->tile >= map->paint_tile + 16 && block->tile < map->paint_tile + 19))
956
+#define IS_SMART_PATH_TILE(block) ((map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 3) \
957
+                                || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 8 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 11) \
958
+                                || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 16 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 19))
941
 
959
 
942
 void MapPixmapItem::paintSmartPath(int x, int y) {
960
 void MapPixmapItem::paintSmartPath(int x, int y) {
943
     // Smart path should never be enabled without a 3x3 block selection.
961
     // Smart path should never be enabled without a 3x3 block selection.
944
     if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
962
     if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
945
 
963
 
946
     // Shift to the middle tile of the smart path selection.
964
     // Shift to the middle tile of the smart path selection.
947
-    int openTile = map->paint_tile + 8 + 1;
965
+    int openTile = map->paint_tile_index + 8 + 1;
948
 
966
 
949
     // Fill the region with the open tile.
967
     // Fill the region with the open tile.
950
-    for (int i = -1; i <= 1 && i + x < map->getWidth() && i + x >= 0; i++)
951
-    for (int j = -1; j <= 1 && j + y < map->getHeight() && j + y >= 0; j++) {
968
+    for (int i = -1; i <= 1; i++)
969
+    for (int j = -1; j <= 1; j++) {
970
+        // Check if in map bounds.
971
+        if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
972
+            continue;
952
         int actualX = i + x;
973
         int actualX = i + x;
953
         int actualY = j + y;
974
         int actualY = j + y;
954
         Block *block = map->getBlock(actualX, actualY);
975
         Block *block = map->getBlock(actualX, actualY);
955
         if (block) {
976
         if (block) {
956
-            block->tile = openTile;
977
+            block->tile = map->getSelectedBlockIndex(openTile);
957
             map->_setBlock(actualX, actualY, *block);
978
             map->_setBlock(actualX, actualY, *block);
958
         }
979
         }
959
     }
980
     }
960
 
981
 
961
     // Go back and resolve the edge tiles
982
     // Go back and resolve the edge tiles
962
-    for (int i = -2; i <= 2 && i + x < map->getWidth() && i + x >= 0; i++)
963
-    for (int j = -2; j <= 2 && j + y < map->getHeight() && j + y >= 0; j++) {
983
+    for (int i = -2; i <= 2; i++)
984
+    for (int j = -2; j <= 2; j++) {
985
+        // Check if in map bounds.
986
+        if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
987
+            continue;
964
         // Ignore the corners, which can't possible be affected by the smart path.
988
         // Ignore the corners, which can't possible be affected by the smart path.
965
         if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
989
         if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
966
             (i == -2 && j ==  2) || (i == 2 && j ==  2))
990
             (i == -2 && j ==  2) || (i == 2 && j ==  2))
990
         if (left && IS_SMART_PATH_TILE(left))
1014
         if (left && IS_SMART_PATH_TILE(left))
991
             id += 8;
1015
             id += 8;
992
 
1016
 
993
-        block->tile = map->paint_tile + smartPathTable[id];;
1017
+        block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]);
994
         map->_setBlock(actualX, actualY, *block);
1018
         map->_setBlock(actualX, actualY, *block);
995
     }
1019
     }
996
 }
1020
 }
1000
         QPointF pos = event->pos();
1024
         QPointF pos = event->pos();
1001
         int x = (int)(pos.x()) / 16;
1025
         int x = (int)(pos.x()) / 16;
1002
         int y = (int)(pos.y()) / 16;
1026
         int y = (int)(pos.y()) / 16;
1003
-        map->floodFill(x, y, map->paint_tile);
1027
+        map->floodFill(x, y, map->getSelectedBlockIndex(map->paint_tile_index));
1004
         draw();
1028
         draw();
1005
     }
1029
     }
1006
 }
1030
 }
1011
     int y = (int)(pos.y()) / 16;
1035
     int y = (int)(pos.y()) / 16;
1012
     Block *block = map->getBlock(x, y);
1036
     Block *block = map->getBlock(x, y);
1013
     if (block) {
1037
     if (block) {
1014
-        map->paint_tile = block->tile;
1038
+        map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
1015
         map->paint_tile_width = 1;
1039
         map->paint_tile_width = 1;
1016
         map->paint_tile_height = 1;
1040
         map->paint_tile_height = 1;
1017
         emit map->paintTileChanged(map);
1041
         emit map->paintTileChanged(map);

+ 18
- 6
editor.h View File

323
 class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
323
 class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
324
     Q_OBJECT
324
     Q_OBJECT
325
 public:
325
 public:
326
-    MetatilesPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
327
-    }
328
     MetatilesPixmapItem(Map *map_) {
326
     MetatilesPixmapItem(Map *map_) {
329
         map = map_;
327
         map = map_;
330
         setAcceptHoverEvents(true);
328
         setAcceptHoverEvents(true);
346
     void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
344
     void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
347
 };
345
 };
348
 
346
 
349
-class CollisionMetatilesPixmapItem : public MetatilesPixmapItem {
347
+class MovementPermissionsPixmapItem : public MetatilesPixmapItem {
348
+    Q_OBJECT
349
+public:
350
+    MovementPermissionsPixmapItem(Map *map_): MetatilesPixmapItem(map_) {}
351
+    virtual void pick(uint collision) {
352
+        map->paint_collision = collision;
353
+        draw();
354
+    }
355
+protected:
356
+    void mousePressEvent(QGraphicsSceneMouseEvent*);
357
+    void mouseMoveEvent(QGraphicsSceneMouseEvent*);
358
+    void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
359
+};
360
+
361
+class CollisionMetatilesPixmapItem : public MovementPermissionsPixmapItem {
350
     Q_OBJECT
362
     Q_OBJECT
351
 public:
363
 public:
352
-    CollisionMetatilesPixmapItem(Map *map_): MetatilesPixmapItem(map_) {
364
+    CollisionMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
353
         connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
365
         connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
354
     }
366
     }
355
     virtual void pick(uint collision) {
367
     virtual void pick(uint collision) {
367
     }
379
     }
368
 };
380
 };
369
 
381
 
370
-class ElevationMetatilesPixmapItem : public MetatilesPixmapItem {
382
+class ElevationMetatilesPixmapItem : public MovementPermissionsPixmapItem {
371
     Q_OBJECT
383
     Q_OBJECT
372
 public:
384
 public:
373
-    ElevationMetatilesPixmapItem(Map *map_): MetatilesPixmapItem(map_) {
385
+    ElevationMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
374
         connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
386
         connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
375
     }
387
     }
376
     virtual void pick(uint elevation) {
388
     virtual void pick(uint elevation) {

+ 26
- 9
map.cpp View File

12
     cached_blockdata = new Blockdata;
12
     cached_blockdata = new Blockdata;
13
     cached_collision = new Blockdata;
13
     cached_collision = new Blockdata;
14
     cached_border = new Blockdata;
14
     cached_border = new Blockdata;
15
-    paint_tile = 1;
15
+    paint_tile_index = 1;
16
     paint_collision = 0;
16
     paint_collision = 0;
17
     paint_elevation = 3;
17
     paint_elevation = 3;
18
 }
18
 }
72
     }
72
     }
73
 }
73
 }
74
 
74
 
75
+int Map::getSelectedBlockIndex(int index) {
76
+    if (index < tileset_primary->metatiles->length()) {
77
+        return index;
78
+    } else {
79
+        return 0x200 + (index - tileset_primary->metatiles->length());
80
+    }
81
+}
82
+
83
+int Map::getDisplayedBlockIndex(int index) {
84
+    if (index < tileset_primary->metatiles->length()) {
85
+        return index;
86
+    } else {
87
+        return index - 0x200 + tileset_primary->metatiles->length();
88
+    }
89
+}
90
+
75
 QImage Map::getMetatileTile(int tile) {
91
 QImage Map::getMetatileTile(int tile) {
76
     Tileset *tileset = getBlockTileset(tile);
92
     Tileset *tileset = getBlockTileset(tile);
77
     int local_index = getBlockIndex(tile);
93
     int local_index = getBlockIndex(tile);
426
         QImage metatile_image = getCollisionMetatileImage(i);
442
         QImage metatile_image = getCollisionMetatileImage(i);
427
         painter.drawImage(origin, metatile_image);
443
         painter.drawImage(origin, metatile_image);
428
     }
444
     }
429
-    drawSelection(paint_collision, width_, &painter);
445
+    drawSelection(paint_collision, width_, 1, 1, &painter);
430
     painter.end();
446
     painter.end();
431
     return QPixmap::fromImage(image);
447
     return QPixmap::fromImage(image);
432
 }
448
 }
444
         QImage metatile_image = getElevationMetatileImage(i);
460
         QImage metatile_image = getElevationMetatileImage(i);
445
         painter.drawImage(origin, metatile_image);
461
         painter.drawImage(origin, metatile_image);
446
     }
462
     }
447
-    drawSelection(paint_elevation, width_, &painter);
463
+    drawSelection(paint_elevation, width_, 1, 1, &painter);
448
     painter.end();
464
     painter.end();
449
     return QPixmap::fromImage(image);
465
     return QPixmap::fromImage(image);
450
 }
466
 }
451
 
467
 
452
-void Map::drawSelection(int i, int w, QPainter *painter) {
468
+void Map::drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter) {
453
     int x = i % w;
469
     int x = i % w;
454
     int y = i / w;
470
     int y = i / w;
455
     painter->save();
471
     painter->save();
456
 
472
 
457
     QColor penColor = smart_paths_enabled ? QColor(0xff, 0x0, 0xff) : QColor(0xff, 0xff, 0xff);
473
     QColor penColor = smart_paths_enabled ? QColor(0xff, 0x0, 0xff) : QColor(0xff, 0xff, 0xff);
458
     painter->setPen(penColor);
474
     painter->setPen(penColor);
459
-    int rectWidth = paint_tile_width * 16;
460
-    int rectHeight = paint_tile_height * 16;
475
+    int rectWidth = selectionWidth * 16;
476
+    int rectHeight = selectionHeight * 16;
461
     painter->drawRect(x * 16, y * 16, rectWidth - 1, rectHeight -1);
477
     painter->drawRect(x * 16, y * 16, rectWidth - 1, rectHeight -1);
462
     painter->setPen(QColor(0, 0, 0));
478
     painter->setPen(QColor(0, 0, 0));
463
     painter->drawRect(x * 16 - 1, y * 16 - 1, rectWidth + 1, rectHeight + 1);
479
     painter->drawRect(x * 16 - 1, y * 16 - 1, rectWidth + 1, rectHeight + 1);
487
         painter.drawImage(metatile_origin, metatile_image);
503
         painter.drawImage(metatile_origin, metatile_image);
488
     }
504
     }
489
 
505
 
490
-    drawSelection(paint_tile, width_, &painter);
506
+    drawSelection(paint_tile_index, width_, paint_tile_width, paint_tile_height, &painter);
491
 
507
 
492
     painter.end();
508
     painter.end();
493
     return QPixmap::fromImage(image);
509
     return QPixmap::fromImage(image);
750
     emit statusBarMessage(QString(""));
766
     emit statusBarMessage(QString(""));
751
 }
767
 }
752
 
768
 
753
-void Map::hoveredMetatileChanged(int block) {
769
+void Map::hoveredMetatileChanged(int blockIndex) {
770
+    int tile = getSelectedBlockIndex(blockIndex);
754
     emit statusBarMessage(QString("Block: 0x%1")
771
     emit statusBarMessage(QString("Block: 0x%1")
755
-                          .arg(QString("%1").arg(block, 3, 16, QChar('0')).toUpper()));
772
+                          .arg(QString("%1").arg(tile, 3, 16, QChar('0')).toUpper()));
756
 }
773
 }
757
 
774
 
758
 void Map::clearHoveredMetatile() {
775
 void Map::clearHoveredMetatile() {

+ 4
- 2
map.h View File

113
     int getHeight();
113
     int getHeight();
114
     Tileset* getBlockTileset(int);
114
     Tileset* getBlockTileset(int);
115
     int getBlockIndex(int index);
115
     int getBlockIndex(int index);
116
+    int getSelectedBlockIndex(int index);
117
+    int getDisplayedBlockIndex(int index);
116
     Metatile* getMetatile(int);
118
     Metatile* getMetatile(int);
117
     QImage getMetatileImage(int);
119
     QImage getMetatileImage(int);
118
     QImage getMetatileTile(int);
120
     QImage getMetatileTile(int);
129
 
131
 
130
     QPixmap renderCollisionMetatiles();
132
     QPixmap renderCollisionMetatiles();
131
     QPixmap renderElevationMetatiles();
133
     QPixmap renderElevationMetatiles();
132
-    void drawSelection(int i, int w, QPainter *painter);
134
+    void drawSelection(int i, int w, int selectionWidth, int selectionHeight, QPainter *painter);
133
 
135
 
134
     bool blockChanged(int, Blockdata*);
136
     bool blockChanged(int, Blockdata*);
135
     Blockdata* cached_blockdata = NULL;
137
     Blockdata* cached_blockdata = NULL;
142
     bool smart_paths_enabled = false;
144
     bool smart_paths_enabled = false;
143
     int paint_metatile_initial_x;
145
     int paint_metatile_initial_x;
144
     int paint_metatile_initial_y;
146
     int paint_metatile_initial_y;
145
-    int paint_tile;
147
+    int paint_tile_index;
146
     int paint_tile_width = 1;
148
     int paint_tile_width = 1;
147
     int paint_tile_height = 1;
149
     int paint_tile_height = 1;
148
     int paint_tile_initial_x;
150
     int paint_tile_initial_x;