Bladeren bron

Add ability to change map border

Marcus Huderle 5 jaren geleden
bovenliggende
commit
5bd88e6fef
12 gewijzigde bestanden met toevoegingen van 294 en 143 verwijderingen
  1. 51
    0
      editor.cpp
  2. 20
    0
      editor.h
  3. 3
    0
      mainwindow.cpp
  4. 91
    7
      mainwindow.ui
  5. 4
    103
      map.cpp
  6. 0
    7
      map.h
  7. 0
    6
      metatile.cpp
  8. 0
    16
      metatile.h
  9. 0
    2
      pretmap.pro
  10. 0
    1
      project.cpp
  11. 106
    0
      tileset.cpp
  12. 19
    1
      tileset.h

+ 51
- 0
editor.cpp Bestand weergeven

301
     ui->comboBox_ConnectionDirection->blockSignals(false);
301
     ui->comboBox_ConnectionDirection->blockSignals(false);
302
 }
302
 }
303
 
303
 
304
+void Editor::onBorderMetatilesChanged() {
305
+    displayMapBorder();
306
+}
307
+
304
 void Editor::setConnectionsVisibility(bool visible) {
308
 void Editor::setConnectionsVisibility(bool visible) {
305
     for (QGraphicsPixmapItem* item : map->connection_items) {
309
     for (QGraphicsPixmapItem* item : map->connection_items) {
306
         item->setVisible(visible);
310
         item->setVisible(visible);
383
     );
387
     );
384
 
388
 
385
     displayMetatiles();
389
     displayMetatiles();
390
+    displayBorderMetatiles();
386
     displayCollisionMetatiles();
391
     displayCollisionMetatiles();
387
     displayElevationMetatiles();
392
     displayElevationMetatiles();
388
     displayMapEvents();
393
     displayMapEvents();
398
     scene_metatiles->addItem(metatiles_item);
403
     scene_metatiles->addItem(metatiles_item);
399
 }
404
 }
400
 
405
 
406
+void Editor::displayBorderMetatiles() {
407
+    scene_selected_border_metatiles = new QGraphicsScene;
408
+    selected_border_metatiles_item = new BorderMetatilesPixmapItem(map);
409
+    selected_border_metatiles_item->draw();
410
+    scene_selected_border_metatiles->addItem(selected_border_metatiles_item);
411
+
412
+    connect(selected_border_metatiles_item, SIGNAL(borderMetatilesChanged()), this, SLOT(onBorderMetatilesChanged()));
413
+}
414
+
401
 void Editor::displayCollisionMetatiles() {
415
 void Editor::displayCollisionMetatiles() {
402
     scene_collision_metatiles = new QGraphicsScene;
416
     scene_collision_metatiles = new QGraphicsScene;
403
     collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
417
     collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
792
     }
806
     }
793
 }
807
 }
794
 
808
 
809
+void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
810
+    QPointF pos = event->pos();
811
+    int x = ((int)pos.x()) / 16;
812
+    int y = ((int)pos.y()) / 16;
813
+
814
+    for (int i = 0; i < map->paint_tile_width && (i + x) < 2; i++) {
815
+        for (int j = 0; j < map->paint_tile_height && (j + y) < 2; j++) {
816
+            int blockIndex = (j + y) * 2 + (i + x);
817
+            int tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
818
+            (*map->layout->border->blocks)[blockIndex].tile = tile;
819
+        }
820
+    }
821
+
822
+    draw();
823
+    emit borderMetatilesChanged();
824
+}
825
+
826
+void BorderMetatilesPixmapItem::draw() {
827
+    QImage image(32, 32, QImage::Format_RGBA8888);
828
+    QPainter painter(&image);
829
+    QList<Block> *blocks = map->layout->border->blocks;
830
+
831
+    for (int i = 0; i < 2; i++)
832
+    for (int j = 0; j < 2; j++)
833
+    {
834
+        int x = i * 16;
835
+        int y = j * 16;
836
+        int index = j * 2 + i;
837
+        QImage metatile_image = Metatile::getMetatileImage(blocks->value(index).tile, map->layout->tileset_primary, map->layout->tileset_secondary);
838
+        QPoint metatile_origin = QPoint(x, y);
839
+        painter.drawImage(metatile_origin, metatile_image);
840
+    }
841
+
842
+    painter.end();
843
+    setPixmap(QPixmap::fromImage(image));
844
+}
845
+
795
 void MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
846
 void MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
796
     QPointF pos = event->pos();
847
     QPointF pos = event->pos();
797
     int x = ((int)pos.x()) / 16;
848
     int x = ((int)pos.x()) / 16;

+ 20
- 0
editor.h Bestand weergeven

16
 class CollisionPixmapItem;
16
 class CollisionPixmapItem;
17
 class ConnectionPixmapItem;
17
 class ConnectionPixmapItem;
18
 class MetatilesPixmapItem;
18
 class MetatilesPixmapItem;
19
+class BorderMetatilesPixmapItem;
19
 class CollisionMetatilesPixmapItem;
20
 class CollisionMetatilesPixmapItem;
20
 class ElevationMetatilesPixmapItem;
21
 class ElevationMetatilesPixmapItem;
21
 
22
 
36
     void setMap(QString map_name);
37
     void setMap(QString map_name);
37
     void displayMap();
38
     void displayMap();
38
     void displayMetatiles();
39
     void displayMetatiles();
40
+    void displayBorderMetatiles();
39
     void displayCollisionMetatiles();
41
     void displayCollisionMetatiles();
40
     void displayElevationMetatiles();
42
     void displayElevationMetatiles();
41
     void displayMapEvents();
43
     void displayMapEvents();
78
     QList<QGraphicsPixmapItem*> borderItems;
80
     QList<QGraphicsPixmapItem*> borderItems;
79
 
81
 
80
     QGraphicsScene *scene_metatiles = NULL;
82
     QGraphicsScene *scene_metatiles = NULL;
83
+    QGraphicsScene *scene_selected_border_metatiles = NULL;
81
     QGraphicsScene *scene_collision_metatiles = NULL;
84
     QGraphicsScene *scene_collision_metatiles = NULL;
82
     QGraphicsScene *scene_elevation_metatiles = NULL;
85
     QGraphicsScene *scene_elevation_metatiles = NULL;
83
     MetatilesPixmapItem *metatiles_item = NULL;
86
     MetatilesPixmapItem *metatiles_item = NULL;
87
+    BorderMetatilesPixmapItem *selected_border_metatiles_item = NULL;
84
     CollisionMetatilesPixmapItem *collision_metatiles_item = NULL;
88
     CollisionMetatilesPixmapItem *collision_metatiles_item = NULL;
85
     ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL;
89
     ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL;
86
 
90
 
123
     void onConnectionItemSelected(ConnectionPixmapItem* connectionItem);
127
     void onConnectionItemSelected(ConnectionPixmapItem* connectionItem);
124
     void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
128
     void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem);
125
     void onConnectionDirectionChanged(QString newDirection);
129
     void onConnectionDirectionChanged(QString newDirection);
130
+    void onBorderMetatilesChanged();
126
 
131
 
127
 signals:
132
 signals:
128
     void objectsChanged();
133
     void objectsChanged();
351
     void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
356
     void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
352
 };
357
 };
353
 
358
 
359
+class BorderMetatilesPixmapItem : public QObject, public QGraphicsPixmapItem {
360
+    Q_OBJECT
361
+public:
362
+    BorderMetatilesPixmapItem(Map *map_) {
363
+        map = map_;
364
+        setAcceptHoverEvents(true);
365
+    }
366
+    Map* map = NULL;
367
+    virtual void draw();
368
+signals:
369
+    void borderMetatilesChanged();
370
+protected:
371
+    void mousePressEvent(QGraphicsSceneMouseEvent*);
372
+};
373
+
354
 class MovementPermissionsPixmapItem : public MetatilesPixmapItem {
374
 class MovementPermissionsPixmapItem : public MetatilesPixmapItem {
355
     Q_OBJECT
375
     Q_OBJECT
356
 public:
376
 public:

+ 3
- 0
mainwindow.cpp Bestand weergeven

166
     //ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect());
166
     //ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect());
167
     ui->graphicsView_Metatiles->setFixedSize(editor->metatiles_item->pixmap().width() + 2, editor->metatiles_item->pixmap().height() + 2);
167
     ui->graphicsView_Metatiles->setFixedSize(editor->metatiles_item->pixmap().width() + 2, editor->metatiles_item->pixmap().height() + 2);
168
 
168
 
169
+    ui->graphicsView_BorderMetatile->setScene(editor->scene_selected_border_metatiles);
170
+    ui->graphicsView_BorderMetatile->setFixedSize(editor->selected_border_metatiles_item->pixmap().width() + 2, editor->selected_border_metatiles_item->pixmap().height() + 2);
171
+
169
     ui->graphicsView_Collision->setScene(editor->scene_collision_metatiles);
172
     ui->graphicsView_Collision->setScene(editor->scene_collision_metatiles);
170
     //ui->graphicsView_Collision->setSceneRect(editor->scene_collision_metatiles->sceneRect());
173
     //ui->graphicsView_Collision->setSceneRect(editor->scene_collision_metatiles->sceneRect());
171
     ui->graphicsView_Collision->setFixedSize(editor->collision_metatiles_item->pixmap().width() + 2, editor->collision_metatiles_item->pixmap().height() + 2);
174
     ui->graphicsView_Collision->setFixedSize(editor->collision_metatiles_item->pixmap().width() + 2, editor->collision_metatiles_item->pixmap().height() + 2);

+ 91
- 7
mainwindow.ui Bestand weergeven

60
         </sizepolicy>
60
         </sizepolicy>
61
        </property>
61
        </property>
62
        <property name="currentIndex">
62
        <property name="currentIndex">
63
-        <number>1</number>
63
+        <number>0</number>
64
        </property>
64
        </property>
65
        <property name="tabsClosable">
65
        <property name="tabsClosable">
66
         <bool>false</bool>
66
         <bool>false</bool>
290
                     <rect>
290
                     <rect>
291
                      <x>0</x>
291
                      <x>0</x>
292
                      <y>0</y>
292
                      <y>0</y>
293
-                     <width>614</width>
293
+                     <width>475</width>
294
                      <height>621</height>
294
                      <height>621</height>
295
                     </rect>
295
                     </rect>
296
                    </property>
296
                    </property>
478
                      <rect>
478
                      <rect>
479
                       <x>0</x>
479
                       <x>0</x>
480
                       <y>0</y>
480
                       <y>0</y>
481
-                      <width>180</width>
482
-                      <height>629</height>
481
+                      <width>358</width>
482
+                      <height>612</height>
483
                      </rect>
483
                      </rect>
484
                     </property>
484
                     </property>
485
                     <property name="sizePolicy">
485
                     <property name="sizePolicy">
488
                       <verstretch>0</verstretch>
488
                       <verstretch>0</verstretch>
489
                      </sizepolicy>
489
                      </sizepolicy>
490
                     </property>
490
                     </property>
491
-                    <layout class="QGridLayout" name="gridLayout_5" rowstretch="0" columnstretch="0">
491
+                    <layout class="QGridLayout" name="gridLayout_5" rowstretch="0,0" columnstretch="0">
492
+                     <property name="sizeConstraint">
493
+                      <enum>QLayout::SetDefaultConstraint</enum>
494
+                     </property>
492
                      <property name="leftMargin">
495
                      <property name="leftMargin">
493
                       <number>0</number>
496
                       <number>0</number>
494
                      </property>
497
                      </property>
501
                      <property name="bottomMargin">
504
                      <property name="bottomMargin">
502
                       <number>0</number>
505
                       <number>0</number>
503
                      </property>
506
                      </property>
504
-                     <property name="spacing">
507
+                     <property name="horizontalSpacing">
505
                       <number>0</number>
508
                       <number>0</number>
506
                      </property>
509
                      </property>
510
+                     <property name="verticalSpacing">
511
+                      <number>8</number>
512
+                     </property>
507
                      <item row="0" column="0">
513
                      <item row="0" column="0">
514
+                      <widget class="QFrame" name="frame_10">
515
+                       <property name="sizePolicy">
516
+                        <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
517
+                         <horstretch>0</horstretch>
518
+                         <verstretch>0</verstretch>
519
+                        </sizepolicy>
520
+                       </property>
521
+                       <property name="frameShape">
522
+                        <enum>QFrame::NoFrame</enum>
523
+                       </property>
524
+                       <property name="frameShadow">
525
+                        <enum>QFrame::Raised</enum>
526
+                       </property>
527
+                       <layout class="QHBoxLayout" name="horizontalLayout_5">
528
+                        <property name="spacing">
529
+                         <number>6</number>
530
+                        </property>
531
+                        <property name="sizeConstraint">
532
+                         <enum>QLayout::SetDefaultConstraint</enum>
533
+                        </property>
534
+                        <item>
535
+                         <widget class="QLabel" name="label_16">
536
+                          <property name="sizePolicy">
537
+                           <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
538
+                            <horstretch>0</horstretch>
539
+                            <verstretch>0</verstretch>
540
+                           </sizepolicy>
541
+                          </property>
542
+                          <property name="text">
543
+                           <string>Border</string>
544
+                          </property>
545
+                         </widget>
546
+                        </item>
547
+                        <item>
548
+                         <widget class="QGraphicsView" name="graphicsView_BorderMetatile">
549
+                          <property name="sizePolicy">
550
+                           <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
551
+                            <horstretch>0</horstretch>
552
+                            <verstretch>0</verstretch>
553
+                           </sizepolicy>
554
+                          </property>
555
+                          <property name="maximumSize">
556
+                           <size>
557
+                            <width>16777215</width>
558
+                            <height>48</height>
559
+                           </size>
560
+                          </property>
561
+                          <property name="frameShape">
562
+                           <enum>QFrame::StyledPanel</enum>
563
+                          </property>
564
+                          <property name="frameShadow">
565
+                           <enum>QFrame::Sunken</enum>
566
+                          </property>
567
+                          <property name="verticalScrollBarPolicy">
568
+                           <enum>Qt::ScrollBarAsNeeded</enum>
569
+                          </property>
570
+                         </widget>
571
+                        </item>
572
+                        <item>
573
+                         <spacer name="horizontalSpacer_12">
574
+                          <property name="orientation">
575
+                           <enum>Qt::Horizontal</enum>
576
+                          </property>
577
+                          <property name="sizeType">
578
+                           <enum>QSizePolicy::Maximum</enum>
579
+                          </property>
580
+                          <property name="sizeHint" stdset="0">
581
+                           <size>
582
+                            <width>40</width>
583
+                            <height>20</height>
584
+                           </size>
585
+                          </property>
586
+                         </spacer>
587
+                        </item>
588
+                       </layout>
589
+                      </widget>
590
+                     </item>
591
+                     <item row="1" column="0">
508
                       <widget class="QGraphicsView" name="graphicsView_Metatiles">
592
                       <widget class="QGraphicsView" name="graphicsView_Metatiles">
509
                        <property name="enabled">
593
                        <property name="enabled">
510
                         <bool>true</bool>
594
                         <bool>true</bool>
511
                        </property>
595
                        </property>
512
                        <property name="sizePolicy">
596
                        <property name="sizePolicy">
513
-                        <sizepolicy hsizetype="Ignored" vsizetype="Ignored">
597
+                        <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
514
                          <horstretch>0</horstretch>
598
                          <horstretch>0</horstretch>
515
                          <verstretch>0</verstretch>
599
                          <verstretch>0</verstretch>
516
                         </sizepolicy>
600
                         </sizepolicy>

+ 4
- 103
map.cpp Bestand weergeven

59
     return layout->height.toInt(nullptr, 0);
59
     return layout->height.toInt(nullptr, 0);
60
 }
60
 }
61
 
61
 
62
-Tileset* Map::getBlockTileset(int metatile_index) {
63
-    int primary_size = 0x200;
64
-    if (metatile_index < primary_size) {
65
-        return layout->tileset_primary;
66
-    } else {
67
-        return layout->tileset_secondary;
68
-    }
69
-}
70
-
71
-QList<QList<QRgb>> Map::getBlockPalettes(int metatile_index) {
72
-    QList<QList<QRgb>> palettes;
73
-    for (int i = 0; i < 6; i++) {
74
-        palettes.append(layout->tileset_primary->palettes->at(i));
75
-    }
76
-    for (int i = 6; i < layout->tileset_secondary->palettes->length(); i++) {
77
-        palettes.append(layout->tileset_secondary->palettes->at(i));
78
-    }
79
-    return palettes;
80
-}
81
-
82
-int Map::getBlockIndex(int index) {
83
-    int primary_size = 0x200;
84
-    if (index < primary_size) {
85
-        return index;
86
-    } else {
87
-        return index - primary_size;
88
-    }
89
-}
90
-
91
 int Map::getSelectedBlockIndex(int index) {
62
 int Map::getSelectedBlockIndex(int index) {
92
     if (index < layout->tileset_primary->metatiles->length()) {
63
     if (index < layout->tileset_primary->metatiles->length()) {
93
         return index;
64
         return index;
104
     }
75
     }
105
 }
76
 }
106
 
77
 
107
-QImage Map::getMetatileTile(int tile) {
108
-    Tileset *tileset = getBlockTileset(tile);
109
-    int local_index = getBlockIndex(tile);
110
-    if (!tileset || !tileset->tiles) {
111
-        return QImage();
112
-    }
113
-    return tileset->tiles->value(local_index, QImage());
114
-}
115
-
116
-Metatile* Map::getMetatile(int index) {
117
-    Tileset *tileset = getBlockTileset(index);
118
-    int local_index = getBlockIndex(index);
119
-    if (!tileset || !tileset->metatiles) {
120
-        return NULL;
121
-    }
122
-    Metatile *metatile = tileset->metatiles->value(local_index, NULL);
123
-    return metatile;
124
-}
125
-
126
 QImage Map::getCollisionMetatileImage(Block block) {
78
 QImage Map::getCollisionMetatileImage(Block block) {
127
     return getCollisionMetatileImage(block.collision);
79
     return getCollisionMetatileImage(block.collision);
128
 }
80
 }
166
     return metatile_image;
118
     return metatile_image;
167
 }
119
 }
168
 
120
 
169
-QImage Map::getMetatileImage(int tile) {
170
-
171
-    QImage metatile_image(16, 16, QImage::Format_RGBA8888);
172
-
173
-    Metatile* metatile = getMetatile(tile);
174
-    if (!metatile || !metatile->tiles) {
175
-        metatile_image.fill(0xffffffff);
176
-        return metatile_image;
177
-    }
178
-
179
-    Tileset* blockTileset = getBlockTileset(tile);
180
-    if (!blockTileset) {
181
-        metatile_image.fill(0xffffffff);
182
-        return metatile_image;
183
-    }
184
-    QList<QList<QRgb>> palettes = getBlockPalettes(tile);
185
-
186
-    QPainter metatile_painter(&metatile_image);
187
-    for (int layer = 0; layer < 2; layer++)
188
-    for (int y = 0; y < 2; y++)
189
-    for (int x = 0; x < 2; x++) {
190
-        Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4));
191
-        QImage tile_image = getMetatileTile(tile_.tile);
192
-        if (tile_image.isNull()) {
193
-            // Some metatiles specify tiles that are outside the valid range.
194
-            // These are treated as completely transparent, so they can be skipped without
195
-            // being drawn.
196
-            continue;
197
-        }
198
-
199
-        // Colorize the metatile tiles with its palette.
200
-        QList<QRgb> palette = palettes.value(tile_.palette);
201
-        for (int j = 0; j < palette.length(); j++) {
202
-            tile_image.setColor(j, palette.value(j));
203
-        }
204
-
205
-        // The top layer of the metatile has its last color displayed at transparent.
206
-        if (layer > 0) {
207
-            QColor color(tile_image.color(15));
208
-            color.setAlpha(0);
209
-            tile_image.setColor(15, color.rgba());
210
-        }
211
-
212
-        QPoint origin = QPoint(x*8, y*8);
213
-        metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
214
-    }
215
-    metatile_painter.end();
216
-
217
-    return metatile_image;
218
-}
219
-
220
 bool Map::blockChanged(int i, Blockdata *cache) {
121
 bool Map::blockChanged(int i, Blockdata *cache) {
221
     if (cache == NULL || cache == nullptr) {
122
     if (cache == NULL || cache == nullptr) {
222
         return true;
123
         return true;
295
         }
196
         }
296
         changed_any = true;
197
         changed_any = true;
297
         Block block = layout->blockdata->blocks->value(i);
198
         Block block = layout->blockdata->blocks->value(i);
298
-        QImage metatile_image = getMetatileImage(block.tile);
199
+        QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
299
         QImage collision_metatile_image = getCollisionMetatileImage(block);
200
         QImage collision_metatile_image = getCollisionMetatileImage(block);
300
         QImage elevation_metatile_image = getElevationMetatileImage(block);
201
         QImage elevation_metatile_image = getElevationMetatileImage(block);
301
         int map_y = width_ ? i / width_ : 0;
202
         int map_y = width_ ? i / width_ : 0;
364
         }
265
         }
365
         changed_any = true;
266
         changed_any = true;
366
         Block block = layout->blockdata->blocks->value(i);
267
         Block block = layout->blockdata->blocks->value(i);
367
-        QImage metatile_image = getMetatileImage(block.tile);
268
+        QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
368
         int map_y = width_ ? i / width_ : 0;
269
         int map_y = width_ ? i / width_ : 0;
369
         int map_x = width_ ? i % width_ : 0;
270
         int map_x = width_ ? i % width_ : 0;
370
         QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
271
         QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
397
         }
298
         }
398
         changed_any = true;
299
         changed_any = true;
399
         Block block = layout->border->blocks->value(i);
300
         Block block = layout->border->blocks->value(i);
400
-        QImage metatile_image = getMetatileImage(block.tile);
301
+        QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary);
401
         int map_y = i / width_;
302
         int map_y = i / width_;
402
         int map_x = i % width_;
303
         int map_x = i % width_;
403
         painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);
304
         painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image);
513
         if (i >= primary_length) {
414
         if (i >= primary_length) {
514
             tile += 0x200 - primary_length;
415
             tile += 0x200 - primary_length;
515
         }
416
         }
516
-        QImage metatile_image = getMetatileImage(tile);
417
+        QImage metatile_image = Metatile::getMetatileImage(tile, layout->tileset_primary, layout->tileset_secondary);
517
         int map_y = i / width_;
418
         int map_y = i / width_;
518
         int map_x = i % width_;
419
         int map_x = i % width_;
519
         QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);
420
         QPoint metatile_origin = QPoint(map_x * 16, map_y * 16);

+ 0
- 7
map.h Bestand weergeven

135
     static QString bgEventsLabelFromName(QString mapName);
135
     static QString bgEventsLabelFromName(QString mapName);
136
     int getWidth();
136
     int getWidth();
137
     int getHeight();
137
     int getHeight();
138
-    Tileset* getBlockTileset(int);
139
-    int getBlockIndex(int);
140
     int getSelectedBlockIndex(int);
138
     int getSelectedBlockIndex(int);
141
     int getDisplayedBlockIndex(int);
139
     int getDisplayedBlockIndex(int);
142
-    Metatile* getMetatile(int);
143
-    QImage getMetatileImage(int);
144
-    QImage getMetatileTile(int);
145
     QPixmap render(bool ignoreCache);
140
     QPixmap render(bool ignoreCache);
146
     QPixmap renderMetatiles();
141
     QPixmap renderMetatiles();
147
 
142
 
214
     void hoveredElevationTileChanged(int elevation);
209
     void hoveredElevationTileChanged(int elevation);
215
     void clearHoveredElevationTile();
210
     void clearHoveredElevationTile();
216
 
211
 
217
-    QList<QList<QRgb> > getBlockPalettes(int metatile_index);
218
-
219
 signals:
212
 signals:
220
     void paintTileChanged(Map *map);
213
     void paintTileChanged(Map *map);
221
     void paintCollisionChanged(Map *map);
214
     void paintCollisionChanged(Map *map);

+ 0
- 6
metatile.cpp Bestand weergeven

1
-#include "metatile.h"
2
-
3
-Metatile::Metatile()
4
-{
5
-    tiles = new QList<Tile>;
6
-}

+ 0
- 16
metatile.h Bestand weergeven

1
-#ifndef METATILE_H
2
-#define METATILE_H
3
-
4
-#include "tile.h"
5
-#include <QList>
6
-
7
-class Metatile
8
-{
9
-public:
10
-    Metatile();
11
-public:
12
-    QList<Tile> *tiles = NULL;
13
-    int attr;
14
-};
15
-
16
-#endif // METATILE_H

+ 0
- 2
pretmap.pro Bestand weergeven

19
     blockdata.cpp \
19
     blockdata.cpp \
20
     block.cpp \
20
     block.cpp \
21
     tileset.cpp \
21
     tileset.cpp \
22
-    metatile.cpp \
23
     tile.cpp \
22
     tile.cpp \
24
     event.cpp \
23
     event.cpp \
25
     editor.cpp \
24
     editor.cpp \
34
     blockdata.h \
33
     blockdata.h \
35
     block.h \
34
     block.h \
36
     tileset.h \
35
     tileset.h \
37
-    metatile.h \
38
     tile.h \
36
     tile.h \
39
     event.h \
37
     event.h \
40
     editor.h \
38
     editor.h \

+ 0
- 1
project.cpp Bestand weergeven

2
 #include "project.h"
2
 #include "project.h"
3
 #include "tile.h"
3
 #include "tile.h"
4
 #include "tileset.h"
4
 #include "tileset.h"
5
-#include "metatile.h"
6
 #include "event.h"
5
 #include "event.h"
7
 
6
 
8
 #include <QDebug>
7
 #include <QDebug>

+ 106
- 0
tileset.cpp Bestand weergeven

1
 #include "tileset.h"
1
 #include "tileset.h"
2
 
2
 
3
+#include <QPainter>
4
+#include <QImage>
5
+
3
 Tileset::Tileset()
6
 Tileset::Tileset()
4
 {
7
 {
5
 
8
 
6
 }
9
 }
10
+
11
+Metatile::Metatile()
12
+{
13
+    tiles = new QList<Tile>;
14
+}
15
+
16
+QImage Metatile::getMetatileImage(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
17
+    QImage metatile_image(16, 16, QImage::Format_RGBA8888);
18
+
19
+    Metatile* metatile = Metatile::getMetatile(tile, primaryTileset, secondaryTileset);
20
+    if (!metatile || !metatile->tiles) {
21
+        metatile_image.fill(0xffffffff);
22
+        return metatile_image;
23
+    }
24
+
25
+    Tileset* blockTileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset);
26
+    if (!blockTileset) {
27
+        metatile_image.fill(0xffffffff);
28
+        return metatile_image;
29
+    }
30
+    QList<QList<QRgb>> palettes = Metatile::getBlockPalettes(primaryTileset, secondaryTileset);
31
+
32
+    QPainter metatile_painter(&metatile_image);
33
+    for (int layer = 0; layer < 2; layer++)
34
+    for (int y = 0; y < 2; y++)
35
+    for (int x = 0; x < 2; x++) {
36
+        Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4));
37
+        QImage tile_image = Metatile::getMetatileTile(tile_.tile, primaryTileset, secondaryTileset);
38
+        if (tile_image.isNull()) {
39
+            // Some metatiles specify tiles that are outside the valid range.
40
+            // These are treated as completely transparent, so they can be skipped without
41
+            // being drawn.
42
+            continue;
43
+        }
44
+
45
+        // Colorize the metatile tiles with its palette.
46
+        QList<QRgb> palette = palettes.value(tile_.palette);
47
+        for (int j = 0; j < palette.length(); j++) {
48
+            tile_image.setColor(j, palette.value(j));
49
+        }
50
+
51
+        // The top layer of the metatile has its last color displayed at transparent.
52
+        if (layer > 0) {
53
+            QColor color(tile_image.color(15));
54
+            color.setAlpha(0);
55
+            tile_image.setColor(15, color.rgba());
56
+        }
57
+
58
+        QPoint origin = QPoint(x*8, y*8);
59
+        metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1));
60
+    }
61
+    metatile_painter.end();
62
+
63
+    return metatile_image;
64
+}
65
+
66
+Metatile* Metatile::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) {
67
+    Tileset *tileset = Metatile::getBlockTileset(index, primaryTileset, secondaryTileset);
68
+    int local_index = Metatile::getBlockIndex(index);
69
+    if (!tileset || !tileset->metatiles) {
70
+        return NULL;
71
+    }
72
+    Metatile *metatile = tileset->metatiles->value(local_index, NULL);
73
+    return metatile;
74
+}
75
+
76
+QImage Metatile::getMetatileTile(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) {
77
+    Tileset *tileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset);
78
+    int local_index = Metatile::getBlockIndex(tile);
79
+    if (!tileset || !tileset->tiles) {
80
+        return QImage();
81
+    }
82
+    return tileset->tiles->value(local_index, QImage());
83
+}
84
+
85
+Tileset* Metatile::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) {
86
+    int primary_size = 0x200;
87
+    if (metatile_index < primary_size) {
88
+        return primaryTileset;
89
+    } else {
90
+        return secondaryTileset;
91
+    }
92
+}
93
+
94
+int Metatile::getBlockIndex(int index) {
95
+    int primary_size = 0x200;
96
+    if (index < primary_size) {
97
+        return index;
98
+    } else {
99
+        return index - primary_size;
100
+    }
101
+}
102
+
103
+QList<QList<QRgb>> Metatile::getBlockPalettes(Tileset *primaryTileset, Tileset *secondaryTileset) {
104
+    QList<QList<QRgb>> palettes;
105
+    for (int i = 0; i < 6; i++) {
106
+        palettes.append(primaryTileset->palettes->at(i));
107
+    }
108
+    for (int i = 6; i < secondaryTileset->palettes->length(); i++) {
109
+        palettes.append(secondaryTileset->palettes->at(i));
110
+    }
111
+    return palettes;
112
+}

+ 19
- 1
tileset.h Bestand weergeven

1
 #ifndef TILESET_H
1
 #ifndef TILESET_H
2
 #define TILESET_H
2
 #define TILESET_H
3
 
3
 
4
-#include "metatile.h"
4
+#include "tile.h"
5
 #include <QImage>
5
 #include <QImage>
6
 
6
 
7
+class Metatile;
8
+
7
 class Tileset
9
 class Tileset
8
 {
10
 {
9
 public:
11
 public:
24
     QList<QList<QRgb>> *palettes = NULL;
26
     QList<QList<QRgb>> *palettes = NULL;
25
 };
27
 };
26
 
28
 
29
+class Metatile
30
+{
31
+public:
32
+    Metatile();
33
+public:
34
+    QList<Tile> *tiles = NULL;
35
+    int attr;
36
+
37
+    static QImage getMetatileImage(int, Tileset*, Tileset*);
38
+    static Metatile* getMetatile(int, Tileset*, Tileset*);
39
+    static QImage getMetatileTile(int, Tileset*, Tileset*);
40
+    static Tileset* getBlockTileset(int, Tileset*, Tileset*);
41
+    static int getBlockIndex(int);
42
+    static QList<QList<QRgb>> getBlockPalettes(Tileset*, Tileset*);
43
+};
44
+
27
 #endif // TILESET_H
45
 #endif // TILESET_H