Просмотр исходного кода

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

yenatch 6 лет назад
Родитель
Сommit
4280735c1e
4 измененных файлов: 86 добавлений и 31 удалений
  1. 38
    14
      editor.cpp
  2. 18
    6
      editor.h
  3. 26
    9
      map.cpp
  4. 4
    2
      map.h

+ 38
- 14
editor.cpp Просмотреть файл

@@ -780,7 +780,7 @@ void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
780 780
     if ((x >= 0 && x < width) && (y >=0 && y < height)) {
781 781
         int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
782 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 784
         map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
785 785
         map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
786 786
         map->smart_paths_enabled = button == Qt::RightButton
@@ -790,6 +790,24 @@ void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
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 811
 void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
794 812
     int x = ((int)pos.x()) / 16;
795 813
     int y = ((int)pos.y()) / 16;
@@ -907,7 +925,7 @@ void MapPixmapItem::paintNormal(int x, int y) {
907 925
         int actualY = j + y;
908 926
         Block *block = map->getBlock(actualX, actualY);
909 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 929
             map->_setBlock(actualX, actualY, *block);
912 930
         }
913 931
     }
@@ -935,32 +953,38 @@ QList<int> MapPixmapItem::smartPathTable = QList<int>({
935 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 960
 void MapPixmapItem::paintSmartPath(int x, int y) {
943 961
     // Smart path should never be enabled without a 3x3 block selection.
944 962
     if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
945 963
 
946 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 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 973
         int actualX = i + x;
953 974
         int actualY = j + y;
954 975
         Block *block = map->getBlock(actualX, actualY);
955 976
         if (block) {
956
-            block->tile = openTile;
977
+            block->tile = map->getSelectedBlockIndex(openTile);
957 978
             map->_setBlock(actualX, actualY, *block);
958 979
         }
959 980
     }
960 981
 
961 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 988
         // Ignore the corners, which can't possible be affected by the smart path.
965 989
         if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
966 990
             (i == -2 && j ==  2) || (i == 2 && j ==  2))
@@ -990,7 +1014,7 @@ void MapPixmapItem::paintSmartPath(int x, int y) {
990 1014
         if (left && IS_SMART_PATH_TILE(left))
991 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 1018
         map->_setBlock(actualX, actualY, *block);
995 1019
     }
996 1020
 }
@@ -1000,7 +1024,7 @@ void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
1000 1024
         QPointF pos = event->pos();
1001 1025
         int x = (int)(pos.x()) / 16;
1002 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 1028
         draw();
1005 1029
     }
1006 1030
 }
@@ -1011,7 +1035,7 @@ void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
1011 1035
     int y = (int)(pos.y()) / 16;
1012 1036
     Block *block = map->getBlock(x, y);
1013 1037
     if (block) {
1014
-        map->paint_tile = block->tile;
1038
+        map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
1015 1039
         map->paint_tile_width = 1;
1016 1040
         map->paint_tile_height = 1;
1017 1041
         emit map->paintTileChanged(map);

+ 18
- 6
editor.h Просмотреть файл

@@ -323,8 +323,6 @@ signals:
323 323
 class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
324 324
     Q_OBJECT
325 325
 public:
326
-    MetatilesPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
327
-    }
328 326
     MetatilesPixmapItem(Map *map_) {
329 327
         map = map_;
330 328
         setAcceptHoverEvents(true);
@@ -346,10 +344,24 @@ protected:
346 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 362
     Q_OBJECT
351 363
 public:
352
-    CollisionMetatilesPixmapItem(Map *map_): MetatilesPixmapItem(map_) {
364
+    CollisionMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
353 365
         connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
354 366
     }
355 367
     virtual void pick(uint collision) {
@@ -367,10 +379,10 @@ private slots:
367 379
     }
368 380
 };
369 381
 
370
-class ElevationMetatilesPixmapItem : public MetatilesPixmapItem {
382
+class ElevationMetatilesPixmapItem : public MovementPermissionsPixmapItem {
371 383
     Q_OBJECT
372 384
 public:
373
-    ElevationMetatilesPixmapItem(Map *map_): MetatilesPixmapItem(map_) {
385
+    ElevationMetatilesPixmapItem(Map *map_): MovementPermissionsPixmapItem(map_) {
374 386
         connect(map, SIGNAL(paintCollisionChanged(Map*)), this, SLOT(paintCollisionChanged(Map *)));
375 387
     }
376 388
     virtual void pick(uint elevation) {

+ 26
- 9
map.cpp Просмотреть файл

@@ -12,7 +12,7 @@ Map::Map(QObject *parent) : QObject(parent)
12 12
     cached_blockdata = new Blockdata;
13 13
     cached_collision = new Blockdata;
14 14
     cached_border = new Blockdata;
15
-    paint_tile = 1;
15
+    paint_tile_index = 1;
16 16
     paint_collision = 0;
17 17
     paint_elevation = 3;
18 18
 }
@@ -72,6 +72,22 @@ int Map::getBlockIndex(int index) {
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 91
 QImage Map::getMetatileTile(int tile) {
76 92
     Tileset *tileset = getBlockTileset(tile);
77 93
     int local_index = getBlockIndex(tile);
@@ -426,7 +442,7 @@ QPixmap Map::renderCollisionMetatiles() {
426 442
         QImage metatile_image = getCollisionMetatileImage(i);
427 443
         painter.drawImage(origin, metatile_image);
428 444
     }
429
-    drawSelection(paint_collision, width_, &painter);
445
+    drawSelection(paint_collision, width_, 1, 1, &painter);
430 446
     painter.end();
431 447
     return QPixmap::fromImage(image);
432 448
 }
@@ -444,20 +460,20 @@ QPixmap Map::renderElevationMetatiles() {
444 460
         QImage metatile_image = getElevationMetatileImage(i);
445 461
         painter.drawImage(origin, metatile_image);
446 462
     }
447
-    drawSelection(paint_elevation, width_, &painter);
463
+    drawSelection(paint_elevation, width_, 1, 1, &painter);
448 464
     painter.end();
449 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 469
     int x = i % w;
454 470
     int y = i / w;
455 471
     painter->save();
456 472
 
457 473
     QColor penColor = smart_paths_enabled ? QColor(0xff, 0x0, 0xff) : QColor(0xff, 0xff, 0xff);
458 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 477
     painter->drawRect(x * 16, y * 16, rectWidth - 1, rectHeight -1);
462 478
     painter->setPen(QColor(0, 0, 0));
463 479
     painter->drawRect(x * 16 - 1, y * 16 - 1, rectWidth + 1, rectHeight + 1);
@@ -487,7 +503,7 @@ QPixmap Map::renderMetatiles() {
487 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 508
     painter.end();
493 509
     return QPixmap::fromImage(image);
@@ -750,9 +766,10 @@ void Map::clearHoveredTile() {
750 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 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 775
 void Map::clearHoveredMetatile() {

+ 4
- 2
map.h Просмотреть файл

@@ -113,6 +113,8 @@ public:
113 113
     int getHeight();
114 114
     Tileset* getBlockTileset(int);
115 115
     int getBlockIndex(int index);
116
+    int getSelectedBlockIndex(int index);
117
+    int getDisplayedBlockIndex(int index);
116 118
     Metatile* getMetatile(int);
117 119
     QImage getMetatileImage(int);
118 120
     QImage getMetatileTile(int);
@@ -129,7 +131,7 @@ public:
129 131
 
130 132
     QPixmap renderCollisionMetatiles();
131 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 136
     bool blockChanged(int, Blockdata*);
135 137
     Blockdata* cached_blockdata = NULL;
@@ -142,7 +144,7 @@ public:
142 144
     bool smart_paths_enabled = false;
143 145
     int paint_metatile_initial_x;
144 146
     int paint_metatile_initial_y;
145
-    int paint_tile;
147
+    int paint_tile_index;
146 148
     int paint_tile_width = 1;
147 149
     int paint_tile_height = 1;
148 150
     int paint_tile_initial_x;