Browse Source

Merge pull request #41 from huderlem/fixtilesets

Properly support painting tiles from secondary tileset
yenatch 6 years ago
parent
commit
8c80e44438
No account linked to committer's email address
4 changed files with 50 additions and 31 deletions
  1. 20
    18
      editor.cpp
  2. 0
    2
      editor.h
  3. 26
    9
      map.cpp
  4. 4
    2
      map.h

+ 20
- 18
editor.cpp View File

@@ -322,7 +322,7 @@ void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
322 322
     if ((x >= 0 && x < width) && (y >=0 && y < height)) {
323 323
         int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
324 324
         int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
325
-        map->paint_tile = baseTileY * 8 + baseTileX;
325
+        map->paint_tile_index = baseTileY * 8 + baseTileX;
326 326
         map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
327 327
         map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
328 328
         map->smart_paths_enabled = button == Qt::RightButton
@@ -411,7 +411,7 @@ void MapPixmapItem::paintNormal(int x, int y) {
411 411
         int actualY = j + y;
412 412
         Block *block = map->getBlock(actualX, actualY);
413 413
         if (block) {
414
-            block->tile = map->paint_tile + i + (j * 8);
414
+            block->tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
415 415
             map->_setBlock(actualX, actualY, *block);
416 416
         }
417 417
     }
@@ -439,32 +439,38 @@ QList<int> MapPixmapItem::smartPathTable = QList<int>({
439 439
     8 + 1, // 1111
440 440
 });
441 441
 
442
-#define IS_SMART_PATH_TILE(block) ((block->tile >= map->paint_tile && block->tile < map->paint_tile + 3) \
443
-                                || (block->tile >= map->paint_tile + 8 && block->tile < map->paint_tile + 11) \
444
-                                || (block->tile >= map->paint_tile + 16 && block->tile < map->paint_tile + 19))
442
+#define IS_SMART_PATH_TILE(block) ((map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 3) \
443
+                                || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 8 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 11) \
444
+                                || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 16 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 19))
445 445
 
446 446
 void MapPixmapItem::paintSmartPath(int x, int y) {
447 447
     // Smart path should never be enabled without a 3x3 block selection.
448 448
     if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
449 449
 
450 450
     // Shift to the middle tile of the smart path selection.
451
-    int openTile = map->paint_tile + 8 + 1;
451
+    int openTile = map->paint_tile_index + 8 + 1;
452 452
 
453 453
     // Fill the region with the open tile.
454
-    for (int i = -1; i <= 1 && i + x < map->getWidth() && i + x >= 0; i++)
455
-    for (int j = -1; j <= 1 && j + y < map->getHeight() && j + y >= 0; j++) {
454
+    for (int i = -1; i <= 1; i++)
455
+    for (int j = -1; j <= 1; j++) {
456
+        // Check if in map bounds.
457
+        if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
458
+            continue;
456 459
         int actualX = i + x;
457 460
         int actualY = j + y;
458 461
         Block *block = map->getBlock(actualX, actualY);
459 462
         if (block) {
460
-            block->tile = openTile;
463
+            block->tile = map->getSelectedBlockIndex(openTile);
461 464
             map->_setBlock(actualX, actualY, *block);
462 465
         }
463 466
     }
464 467
 
465 468
     // Go back and resolve the edge tiles
466
-    for (int i = -2; i <= 2 && i + x < map->getWidth() && i + x >= 0; i++)
467
-    for (int j = -2; j <= 2 && j + y < map->getHeight() && j + y >= 0; j++) {
469
+    for (int i = -2; i <= 2; i++)
470
+    for (int j = -2; j <= 2; j++) {
471
+        // Check if in map bounds.
472
+        if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
473
+            continue;
468 474
         // Ignore the corners, which can't possible be affected by the smart path.
469 475
         if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
470 476
             (i == -2 && j ==  2) || (i == 2 && j ==  2))
@@ -494,11 +500,7 @@ void MapPixmapItem::paintSmartPath(int x, int y) {
494 500
         if (left && IS_SMART_PATH_TILE(left))
495 501
             id += 8;
496 502
 
497
-        if (block) {
498
-            qDebug() << "tile: " << block->tile << "base: " << map->paint_tile << "id: " << id;
499
-        }
500
-
501
-        block->tile = map->paint_tile + smartPathTable[id];;
503
+        block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]);
502 504
         map->_setBlock(actualX, actualY, *block);
503 505
     }
504 506
 }
@@ -508,7 +510,7 @@ void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
508 510
         QPointF pos = event->pos();
509 511
         int x = (int)(pos.x()) / 16;
510 512
         int y = (int)(pos.y()) / 16;
511
-        map->floodFill(x, y, map->paint_tile);
513
+        map->floodFill(x, y, map->getSelectedBlockIndex(map->paint_tile_index));
512 514
         draw();
513 515
     }
514 516
 }
@@ -519,7 +521,7 @@ void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
519 521
     int y = (int)(pos.y()) / 16;
520 522
     Block *block = map->getBlock(x, y);
521 523
     if (block) {
522
-        map->paint_tile = block->tile;
524
+        map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
523 525
         map->paint_tile_width = 1;
524 526
         map->paint_tile_height = 1;
525 527
         emit map->paintTileChanged(map);

+ 0
- 2
editor.h View File

@@ -243,8 +243,6 @@ protected:
243 243
 class MetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
244 244
     Q_OBJECT
245 245
 public:
246
-    MetatilesPixmapItem(QPixmap pixmap): QGraphicsPixmapItem(pixmap) {
247
-    }
248 246
     MetatilesPixmapItem(Map *map_) {
249 247
         map = map_;
250 248
         setAcceptHoverEvents(true);

+ 26
- 9
map.cpp View File

@@ -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 View File

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