From 76649ea867d167f8a12378a5f10d04caabe9db70 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Fri, 6 Jul 2018 11:08:20 -0500 Subject: [PATCH 1/7] Add menu to 'add event' button --- editor.cpp | 5 +- editor.h | 1 - event.cpp | 8 ++++ event.h | 13 +++++- mainwindow.cpp | 22 +++++---- mainwindow.h | 3 +- mainwindow.ui | 17 ++++--- neweventtoolbutton.cpp | 103 +++++++++++++++++++++++++++++++++++++++++ neweventtoolbutton.h | 35 ++++++++++++++ pretmap.pro | 6 ++- project.cpp | 26 +++++------ 11 files changed, 197 insertions(+), 42 deletions(-) create mode 100644 neweventtoolbutton.cpp create mode 100644 neweventtoolbutton.h diff --git a/editor.cpp b/editor.cpp index f32e405f..a68b618e 100755 --- a/editor.cpp +++ b/editor.cpp @@ -1,4 +1,5 @@ #include "editor.h" +#include "event.h" #include #include #include @@ -1297,10 +1298,6 @@ void Editor::selectMapEvent(DraggablePixmapItem *object, bool toggle) { } } -DraggablePixmapItem* Editor::addNewEvent() { - return addNewEvent("object"); -} - DraggablePixmapItem* Editor::addNewEvent(QString event_type) { if (project && map) { Event *event = new Event; diff --git a/editor.h b/editor.h index 2da5521b..5065aac0 100755 --- a/editor.h +++ b/editor.h @@ -61,7 +61,6 @@ public: DraggablePixmapItem *addMapEvent(Event *event); void selectMapEvent(DraggablePixmapItem *object); void selectMapEvent(DraggablePixmapItem *object, bool toggle); - DraggablePixmapItem *addNewEvent(); DraggablePixmapItem *addNewEvent(QString event_type); void deleteEvent(Event *); void updateSelectedEvents(); diff --git a/event.cpp b/event.cpp index efee19ef..dcb0eaf7 100755 --- a/event.cpp +++ b/event.cpp @@ -1,5 +1,13 @@ #include "event.h" +QString EventType::Object = "event_object"; +QString EventType::Warp = "event_warp"; +QString EventType::CoordScript = "event_trap"; +QString EventType::CoordWeather = "event_trap_weather"; +QString EventType::Sign = "event_sign"; +QString EventType::HiddenItem = "event_hidden_item"; +QString EventType::SecretBase = "event_secret_base"; + Event::Event() { } diff --git a/event.h b/event.h index fa67b876..34e52829 100755 --- a/event.h +++ b/event.h @@ -5,11 +5,22 @@ #include #include +class EventType +{ +public: + static QString Object; + static QString Warp; + static QString CoordScript; + static QString CoordWeather; + static QString Sign; + static QString HiddenItem; + static QString SecretBase; +}; + class Event { public: Event(); - public: int x() { return getInt("x"); diff --git a/mainwindow.cpp b/mainwindow.cpp index 20582b67..3c728532 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -24,6 +24,10 @@ MainWindow::MainWindow(QWidget *parent) : QCoreApplication::setApplicationName("pretmap"); ui->setupUi(this); + + ui->newEventToolButton->initButton(); + connect(ui->newEventToolButton, SIGNAL(newEventAdded(QString)), this, SLOT(addNewEvent(QString))); + new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo())); editor = new Editor(ui); @@ -503,10 +507,10 @@ void MainWindow::on_actionRedo_triggered() redo(); } -void MainWindow::on_toolButton_newObject_clicked() +void MainWindow::addNewEvent(QString event_type) { if (editor) { - DraggablePixmapItem *object = editor->addNewEvent(); + DraggablePixmapItem *object = editor->addNewEvent(event_type); if (object) { //if (editor->selected_events->length()) { editor->selectMapEvent(object, true); @@ -590,7 +594,7 @@ void MainWindow::updateSelectedObjects() { QStringList fields; - if (event_type == "object") { + if (event_type == EventType::Object) { frame->ui->sprite->setVisible(true); frame->ui->comboBox_sprite->addItems(event_obj_gfx_constants.keys()); @@ -616,27 +620,27 @@ void MainWindow::updateSelectedObjects() { fields << "property"; fields << "sight_radius"; } - else if (event_type == "warp") { + else if (event_type == EventType::Warp) { fields << "destination_warp"; fields << "destination_map_name"; } - else if (event_type == "trap") { + else if (event_type == EventType::CoordScript) { fields << "script_label"; fields << "script_var"; fields << "script_var_value"; } - else if (event_type == "trap_weather") { + else if (event_type == EventType::CoordWeather) { fields << "weather"; } - else if (event_type == "sign") { + else if (event_type == EventType::Sign) { fields << "type"; fields << "script_label"; } - else if (event_type == "event_hidden_item") { + else if (event_type == EventType::HiddenItem) { fields << "item"; fields << "flag"; } - else if (event_type == "event_secret_base") { + else if (event_type == EventType::SecretBase) { fields << "secret_base_map"; } diff --git a/mainwindow.h b/mainwindow.h index 5c01c2da..5f1b7a2b 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -56,10 +56,9 @@ private slots: void on_actionRedo_triggered(); - void on_toolButton_newObject_clicked(); - void on_toolButton_deleteObject_clicked(); + void addNewEvent(QString); void updateSelectedObjects(); void on_toolButton_Paint_clicked(); diff --git a/mainwindow.ui b/mainwindow.ui index e6fa825c..9fc8cb5a 100755 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -60,7 +60,7 @@ - 0 + 1 false @@ -904,13 +904,7 @@ 0 - - - - 0 - 0 - - + 40 @@ -918,7 +912,7 @@ - New + New Object @@ -1794,6 +1788,11 @@ QGraphicsView
graphicsview.h
+ + NewEventToolButton + QToolButton +
neweventtoolbutton.h
+
diff --git a/neweventtoolbutton.cpp b/neweventtoolbutton.cpp new file mode 100644 index 00000000..51d4ee2b --- /dev/null +++ b/neweventtoolbutton.cpp @@ -0,0 +1,103 @@ +#include "neweventtoolbutton.h" +#include +#include + +// Custom QToolButton which has a context menu that expands to allow +// selection of different types of map events. +NewEventToolButton::NewEventToolButton(QWidget *parent) : + QToolButton(parent) +{ + setPopupMode(QToolButton::MenuButtonPopup); + QObject::connect(this, SIGNAL(triggered(QAction*)), + this, SLOT(setDefaultAction(QAction*))); +} + +void NewEventToolButton::initButton() +{ + // Add a context menu to select different types of map events. + this->newObjectAction = new QAction("New Object", this); + this->newObjectAction->setIcon(QIcon(":/icons/add.ico")); + connect(this->newObjectAction, SIGNAL(triggered(bool)), this, SLOT(newObject())); + + this->newWarpAction = new QAction("New Warp", this); + this->newWarpAction->setIcon(QIcon(":/icons/add.ico")); + connect(this->newWarpAction, SIGNAL(triggered(bool)), this, SLOT(newWarp())); + + this->newCoordScriptAction = new QAction("New Coord Script", this); + this->newCoordScriptAction->setIcon(QIcon(":/icons/add.ico")); + connect(this->newCoordScriptAction, SIGNAL(triggered(bool)), this, SLOT(newCoordScript())); + + this->newCoordWeatherAction = new QAction("New Coord Weather", this); + this->newCoordWeatherAction->setIcon(QIcon(":/icons/add.ico")); + connect(this->newCoordWeatherAction, SIGNAL(triggered(bool)), this, SLOT(newCoordWeather())); + + this->newSignAction = new QAction("New Sign", this); + this->newSignAction->setIcon(QIcon(":/icons/add.ico")); + connect(this->newSignAction, SIGNAL(triggered(bool)), this, SLOT(newSign())); + + this->newHiddenItemAction = new QAction("New Hidden Item", this); + this->newHiddenItemAction->setIcon(QIcon(":/icons/add.ico")); + connect(this->newHiddenItemAction, SIGNAL(triggered(bool)), this, SLOT(newHiddenItem())); + + this->newSecretBaseAction = new QAction("New Secret Base", this); + this->newSecretBaseAction->setIcon(QIcon(":/icons/add.ico")); + connect(this->newSecretBaseAction, SIGNAL(triggered(bool)), this, SLOT(newSecretBase())); + + QMenu *alignMenu = new QMenu(); + alignMenu->addAction(this->newObjectAction); + alignMenu->addAction(this->newWarpAction); + alignMenu->addAction(this->newCoordScriptAction); + alignMenu->addAction(this->newCoordWeatherAction); + alignMenu->addAction(this->newSignAction); + alignMenu->addAction(this->newHiddenItemAction); + alignMenu->addAction(this->newSecretBaseAction); + this->setMenu(alignMenu); + this->setDefaultAction(this->newObjectAction); +} + +QString NewEventToolButton::getSelectedEventType() +{ + return this->selectedEventType; +} + +void NewEventToolButton::newObject() +{ + this->selectedEventType = EventType::Object; + emit newEventAdded(this->selectedEventType); +} + +void NewEventToolButton::newWarp() +{ + this->selectedEventType = EventType::Warp; + emit newEventAdded(this->selectedEventType); +} + +void NewEventToolButton::newCoordScript() +{ + this->selectedEventType = EventType::CoordScript; + emit newEventAdded(this->selectedEventType); +} + +void NewEventToolButton::newCoordWeather() +{ + this->selectedEventType = EventType::CoordWeather; + emit newEventAdded(this->selectedEventType); +} + +void NewEventToolButton::newSign() +{ + this->selectedEventType = EventType::Sign; + emit newEventAdded(this->selectedEventType); +} + +void NewEventToolButton::newHiddenItem() +{ + this->selectedEventType = EventType::HiddenItem; + emit newEventAdded(this->selectedEventType); +} + +void NewEventToolButton::newSecretBase() +{ + this->selectedEventType = EventType::SecretBase; + emit newEventAdded(this->selectedEventType); +} diff --git a/neweventtoolbutton.h b/neweventtoolbutton.h new file mode 100644 index 00000000..f4bb7f55 --- /dev/null +++ b/neweventtoolbutton.h @@ -0,0 +1,35 @@ +#ifndef NEWEVENTTOOLBUTTON_H +#define NEWEVENTTOOLBUTTON_H + +#include "event.h" +#include + +class NewEventToolButton : public QToolButton +{ + Q_OBJECT +public: + explicit NewEventToolButton(QWidget *parent = 0); + void initButton(); + QString getSelectedEventType(); +public slots: + void newObject(); + void newWarp(); + void newCoordScript(); + void newCoordWeather(); + void newSign(); + void newHiddenItem(); + void newSecretBase(); +signals: + void newEventAdded(QString); +private: + QString selectedEventType; + QAction *newObjectAction; + QAction *newWarpAction; + QAction *newCoordScriptAction; + QAction *newCoordWeatherAction; + QAction *newSignAction; + QAction *newHiddenItemAction; + QAction *newSecretBaseAction; +}; + +#endif // NEWEVENTTOOLBUTTON_H diff --git a/pretmap.pro b/pretmap.pro index d0221dce..387e2627 100755 --- a/pretmap.pro +++ b/pretmap.pro @@ -25,7 +25,8 @@ SOURCES += main.cpp\ editor.cpp \ objectpropertiesframe.cpp \ graphicsview.cpp \ - parseutil.cpp + parseutil.cpp \ + neweventtoolbutton.cpp HEADERS += mainwindow.h \ project.h \ @@ -39,7 +40,8 @@ HEADERS += mainwindow.h \ editor.h \ objectpropertiesframe.h \ graphicsview.h \ - parseutil.h + parseutil.h \ + neweventtoolbutton.h FORMS += mainwindow.ui \ objectpropertiesframe.ui diff --git a/project.cpp b/project.cpp index ca64c3dc..a0255578 100755 --- a/project.cpp +++ b/project.cpp @@ -1152,17 +1152,17 @@ void Project::loadEventPixmaps(QList objects) { continue; } QString event_type = object->get("event_type"); - if (event_type == "object") { + if (event_type == EventType::Object) { object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(0, 0, 16, 16); - } else if (event_type == "warp") { + } else if (event_type == EventType::Warp) { object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(16, 0, 16, 16); - } else if (event_type == "trap" || event_type == "trap_weather") { + } else if (event_type == EventType::CoordScript || event_type == EventType::CoordWeather) { object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(32, 0, 16, 16); - } else if (event_type == "sign" || event_type == "event_hidden_item" || event_type == "event_secret_base") { + } else if (event_type == EventType::Sign || event_type == EventType::HiddenItem || event_type == EventType::SecretBase) { object->pixmap = QPixmap(":/images/Entities_16x16.png").copy(48, 0, 16, 16); } - if (event_type == "object") { + if (event_type == EventType::Object) { int sprite_id = constants.value(object->get("sprite")); QString info_label = pointers.value(sprite_id).replace("&", ""); @@ -1178,10 +1178,8 @@ void Project::loadEventPixmaps(QList objects) { object->pixmap = pixmap; } } - } } - } void Project::saveMapEvents(Map *map) { @@ -1331,7 +1329,7 @@ void Project::readMapEvents(Map *map) { object->put("script_label", command.value(i++)); object->put("event_flag", command.value(i++)); - object->put("event_type", "object"); + object->put("event_type", EventType::Object); map->events["object"].append(object); } } @@ -1352,7 +1350,7 @@ void Project::readMapEvents(Map *map) { QString mapConstant = command.value(i++); if (mapConstantsToMapNames->contains(mapConstant)) { warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant)); - warp->put("event_type", "warp"); + warp->put("event_type", EventType::Warp); map->events["warp"].append(warp); } else { qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant); @@ -1383,7 +1381,7 @@ void Project::readMapEvents(Map *map) { //coord_unknown3 //coord_unknown4 - coord->put("event_type", "trap"); + coord->put("event_type", EventType::CoordScript); map->events["trap"].append(coord); } else if (command.value(0) == "coord_weather_event") { Event *coord = new Event; @@ -1393,7 +1391,7 @@ void Project::readMapEvents(Map *map) { coord->put("y", command.value(i++)); coord->put("elevation", command.value(i++)); coord->put("weather", command.value(i++)); - coord->put("event_type", "trap_weather"); + coord->put("event_type", EventType::CoordWeather); map->events["trap_weather"].append(coord); } } @@ -1414,7 +1412,7 @@ void Project::readMapEvents(Map *map) { i++; bg->put("script_label", command.value(i++)); //sign_unknown7 - bg->put("event_type", "sign"); + bg->put("event_type", EventType::Sign); map->events["sign"].append(bg); } else if (command.value(0) == "bg_hidden_item_event") { Event *bg = new Event; @@ -1425,7 +1423,7 @@ void Project::readMapEvents(Map *map) { bg->put("elevation", command.value(i++)); bg->put("item", command.value(i++)); bg->put("flag", command.value(i++)); - bg->put("event_type", "event_hidden_item"); + bg->put("event_type", EventType::HiddenItem); map->events["event_hidden_item"].append(bg); } else if (command.value(0) == "bg_secret_base_event") { Event *bg = new Event; @@ -1435,7 +1433,7 @@ void Project::readMapEvents(Map *map) { bg->put("y", command.value(i++)); bg->put("elevation", command.value(i++)); bg->put("secret_base_map", command.value(i++)); - bg->put("event_type", "event_secret_base"); + bg->put("event_type", EventType::SecretBase); map->events["event_secret_base"].append(bg); } } From ecce7d26f244dad572455732de713a77d26a9b54 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 7 Jul 2018 10:58:25 -0500 Subject: [PATCH 2/7] Add ability to add all map event types --- editor.cpp | 4 +- editor.h | 8 ++ event.cpp | 200 +++++++++++++++++++++++++++++++++++++++++++++++++ event.h | 18 +++++ mainwindow.cpp | 21 +++--- map.cpp | 7 +- map.h | 1 - project.cpp | 149 +++++++++++++----------------------- 8 files changed, 288 insertions(+), 120 deletions(-) diff --git a/editor.cpp b/editor.cpp index a68b618e..cd8f9d37 100755 --- a/editor.cpp +++ b/editor.cpp @@ -1300,9 +1300,8 @@ void Editor::selectMapEvent(DraggablePixmapItem *object, bool toggle) { DraggablePixmapItem* Editor::addNewEvent(QString event_type) { if (project && map) { - Event *event = new Event; + Event *event = Event::createNewEvent(event_type, map->name); event->put("map_name", map->name); - event->put("event_type", event_type); map->addEvent(event); project->loadEventPixmaps(map->getAllEvents()); DraggablePixmapItem *object = addMapEvent(event); @@ -1321,7 +1320,6 @@ void Editor::deleteEvent(Event *event) { //updateSelectedObjects(); } - // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true // check if selected_events changed instead. this has the side effect of deselecting // when you click on a selected event, since selected_events doesn't change. diff --git a/editor.h b/editor.h index 5065aac0..bc21584a 100755 --- a/editor.h +++ b/editor.h @@ -62,6 +62,7 @@ public: void selectMapEvent(DraggablePixmapItem *object); void selectMapEvent(DraggablePixmapItem *object, bool toggle); DraggablePixmapItem *addNewEvent(QString event_type); + Event* createNewEvent(QString event_type); void deleteEvent(Event *); void updateSelectedEvents(); void redrawObject(DraggablePixmapItem *item); @@ -107,6 +108,13 @@ private: void updateMirroredConnectionDirection(Connection*, QString); void updateMirroredConnectionMap(Connection*, QString); void updateMirroredConnection(Connection*, QString, QString, bool isDelete = false); + Event* createNewObjectEvent(); + Event* createNewWarpEvent(); + Event* createNewCoordScriptEvent(); + Event* createNewCoordWeatherEvent(); + Event* createNewSignEvent(); + Event* createNewHiddenItemEvent(); + Event* createNewSecretBaseEvent(); private slots: void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item); diff --git a/event.cpp b/event.cpp index dcb0eaf7..6b47972e 100755 --- a/event.cpp +++ b/event.cpp @@ -11,3 +11,203 @@ QString EventType::SecretBase = "event_secret_base"; Event::Event() { } + +Event* Event::createNewEvent(QString event_type, QString map_name) +{ + Event *event; + if (event_type == EventType::Object) { + event = createNewObjectEvent(); + } else if (event_type == EventType::Warp) { + event = createNewWarpEvent(map_name); + } else if (event_type == EventType::CoordScript) { + event = createNewCoordScriptEvent(); + } else if (event_type == EventType::CoordWeather) { + event = createNewCoordWeatherEvent(); + } else if (event_type == EventType::Sign) { + event = createNewSignEvent(); + } else if (event_type == EventType::HiddenItem) { + event = createNewHiddenItemEvent(); + } else if (event_type == EventType::SecretBase) { + event = createNewSecretBaseEvent(); + } + + event->setX(0); + event->setY(0); + event->put("elevation", 3); + return event; +} + +Event* Event::createNewObjectEvent() +{ + Event *event = new Event; + event->put("event_group_type", "object_event_group"); + event->put("event_type", EventType::Object); + event->put("sprite", "EVENT_OBJ_GFX_BOY_1"); + event->put("behavior", "1"); + event->put("radius_x", 0); + event->put("radius_y", 0); + event->put("script_label", "NULL"); + event->put("event_flag", "0"); + event->put("replacement", "0"); + event->put("trainer_see_type", "0"); + event->put("sight_radius_tree_id", 0); + return event; +} + +Event* Event::createNewWarpEvent(QString map_name) +{ + Event *event = new Event; + event->put("event_group_type", "warp_event_group"); + event->put("event_type", EventType::Warp); + event->put("destination_warp", 0); + event->put("destination_map_name", map_name); + return event; +} + +Event* Event::createNewCoordScriptEvent() +{ + Event *event = new Event; + event->put("event_group_type", "coord_event_group"); + event->put("event_type", EventType::CoordScript); + event->put("script_label", "NULL"); + event->put("script_var", "VAR_TEMP_0"); + event->put("script_var_value", "0"); + return event; +} + +Event* Event::createNewCoordWeatherEvent() +{ + Event *event = new Event; + event->put("event_group_type", "coord_event_group"); + event->put("event_type", EventType::CoordWeather); + event->put("weather", "COORD_EVENT_WEATHER_SUNNY"); + return event; +} + +Event* Event::createNewSignEvent() +{ + Event *event = new Event; + event->put("event_group_type", "bg_event_group"); + event->put("event_type", EventType::Sign); + event->put("player_facing_direction", "0"); + event->put("script_label", "NULL"); + return event; +} + +Event* Event::createNewHiddenItemEvent() +{ + Event *event = new Event; + event->put("event_group_type", "bg_event_group"); + event->put("event_type", EventType::HiddenItem); + event->put("item", "ITEM_POTION"); + event->put("flag", "FLAG_HIDDEN_ITEM_0"); + return event; +} + +Event* Event::createNewSecretBaseEvent() +{ + Event *event = new Event; + event->put("event_group_type", "bg_event_group"); + event->put("event_type", EventType::SecretBase); + event->put("secret_base_map", "SECRET_BASE_RED_CAVE2_1"); + return event; +} + +QString Event::buildObjectEventMacro(int item_index) +{ + int radius_x = this->getInt("radius_x"); + int radius_y = this->getInt("radius_y"); + uint16_t x = this->getInt("x"); + uint16_t y = this->getInt("y"); + + QString text = ""; + text += QString("\tobject_event %1").arg(item_index + 1); + text += QString(", %1").arg(this->get("sprite")); + text += QString(", %1").arg(this->get("replacement")); + text += QString(", %1").arg(x); + text += QString(", %1").arg(y); + text += QString(", %1").arg(this->get("elevation")); + text += QString(", %1").arg(this->get("behavior")); + text += QString(", %1").arg(radius_x); + text += QString(", %1").arg(radius_y); + text += QString(", %1").arg(this->get("trainer_see_type")); + text += QString(", %1").arg(this->get("sight_radius_tree_id")); + text += QString(", %1").arg(this->get("script_label")); + text += QString(", %1").arg(this->get("event_flag")); + text += "\n"; + return text; +} + +QString Event::buildWarpEventMacro(QMap *mapNamesToMapConstants) +{ + QString text = ""; + text += QString("\twarp_def %1").arg(this->get("x")); + text += QString(", %1").arg(this->get("y")); + text += QString(", %1").arg(this->get("elevation")); + text += QString(", %1").arg(this->get("destination_warp")); + text += QString(", %1").arg(mapNamesToMapConstants->value(this->get("destination_map_name"))); + text += "\n"; + return text; +} + +QString Event::buildCoordScriptEventMacro() +{ + QString text = ""; + text += QString("\tcoord_event %1").arg(this->get("x")); + text += QString(", %1").arg(this->get("y")); + text += QString(", %1").arg(this->get("elevation")); + text += QString(", 0"); + text += QString(", %1").arg(this->get("script_var")); + text += QString(", %1").arg(this->get("script_var_value")); + text += QString(", 0"); + text += QString(", %1").arg(this->get("script_label")); + text += "\n"; + return text; +} + +QString Event::buildCoordWeatherEventMacro() +{ + QString text = ""; + text += QString("\tcoord_weather_event %1").arg(this->get("x")); + text += QString(", %1").arg(this->get("y")); + text += QString(", %1").arg(this->get("elevation")); + text += QString(", %1").arg(this->get("weather")); + text += "\n"; + return text; +} + +QString Event::buildSignEventMacro() +{ + QString text = ""; + text += QString("\tbg_event %1").arg(this->get("x")); + text += QString(", %1").arg(this->get("y")); + text += QString(", %1").arg(this->get("elevation")); + text += QString(", %1").arg(this->get("player_facing_direction")); + text += QString(", 0"); + text += QString(", %1").arg(this->get("script_label")); + text += "\n"; + return text; +} + +QString Event::buildHiddenItemEventMacro() +{ + QString text = ""; + text += QString("\tbg_hidden_item_event %1").arg(this->get("x")); + text += QString(", %1").arg(this->get("y")); + text += QString(", %1").arg(this->get("elevation")); + text += QString(", %1").arg(this->get("item")); + text += QString(", %1").arg(this->get("flag")); + text += "\n"; + return text; +} + +QString Event::buildSecretBaseEventMacro() +{ + QString text = ""; + text += QString("\tbg_secret_base_event %1").arg(this->get("x")); + text += QString(", %1").arg(this->get("y")); + text += QString(", %1").arg(this->get("elevation")); + text += QString(", %1").arg(this->get("secret_base_map")); + text += "\n"; + return text; +} diff --git a/event.h b/event.h index 34e52829..03313d27 100755 --- a/event.h +++ b/event.h @@ -4,6 +4,7 @@ #include #include #include +#include class EventType { @@ -50,6 +51,23 @@ public: values.insert(key, value); } + static Event* createNewEvent(QString, QString); + static Event* createNewObjectEvent(); + static Event* createNewWarpEvent(QString); + static Event* createNewCoordScriptEvent(); + static Event* createNewCoordWeatherEvent(); + static Event* createNewSignEvent(); + static Event* createNewHiddenItemEvent(); + static Event* createNewSecretBaseEvent(); + + QString buildObjectEventMacro(int); + QString buildWarpEventMacro(QMap*); + QString buildCoordScriptEventMacro(); + QString buildCoordWeatherEventMacro(); + QString buildSignEventMacro(); + QString buildHiddenItemEventMacro(); + QString buildSecretBaseEventMacro(); + QMap values; QPixmap pixmap; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 3c728532..58f0e684 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -512,9 +512,7 @@ void MainWindow::addNewEvent(QString event_type) if (editor) { DraggablePixmapItem *object = editor->addNewEvent(event_type); if (object) { - //if (editor->selected_events->length()) { - editor->selectMapEvent(object, true); - //} + editor->selectMapEvent(object, false); } updateSelectedObjects(); } @@ -558,12 +556,13 @@ void MainWindow::updateSelectedObjects() { font.setCapitalization(QFont::Capitalize); frame->ui->label_name->setFont(font); QString event_type = item->event->get("event_type"); + QString event_group_type = item->event->get("event_group_type"); QString map_name = item->event->get("map_name"); frame->ui->label_name->setText( - QString("%1 %2 %3") + QString("%1: %2 %3") + .arg(editor->project->getMap(map_name)->events.value(event_group_type).indexOf(item->event) + 1) .arg(map_name) .arg(event_type) - .arg(editor->project->getMap(map_name)->events.value(event_type).indexOf(item->event) + 1) ); frame->ui->label_spritePixmap->setPixmap(item->event->pixmap); @@ -578,13 +577,13 @@ void MainWindow::updateSelectedObjects() { field_labels["behavior"] = "Behavior"; field_labels["radius_x"] = "Movement Radius X"; field_labels["radius_y"] = "Movement Radius Y"; - field_labels["property"] = "Property"; - field_labels["sight_radius"] = "Sight Radius"; + field_labels["trainer_see_type"] = "Trainer See Type"; + field_labels["sight_radius_tree_id"] = "Sight Radius / Berry Tree ID"; field_labels["destination_warp"] = "Destination Warp"; field_labels["destination_map_name"] = "Destination Map"; field_labels["script_var"] = "Var"; field_labels["script_var_value"] = "Var Value"; - field_labels["type"] = "Type"; + field_labels["player_facing_direction"] = "Player Facing Direction"; field_labels["item"] = "Item"; field_labels["item_unknown5"] = "Unknown 5"; field_labels["item_unknown6"] = "Unknown 6"; @@ -617,8 +616,8 @@ void MainWindow::updateSelectedObjects() { fields << "script_label"; fields << "event_flag"; fields << "replacement"; - fields << "property"; - fields << "sight_radius"; + fields << "trainer_see_type"; + fields << "sight_radius_tree_id"; } else if (event_type == EventType::Warp) { fields << "destination_warp"; @@ -633,7 +632,7 @@ void MainWindow::updateSelectedObjects() { fields << "weather"; } else if (event_type == EventType::Sign) { - fields << "type"; + fields << "player_facing_direction"; fields << "script_label"; } else if (event_type == EventType::HiddenItem) { diff --git a/map.cpp b/map.cpp index 60867302..a9499b97 100755 --- a/map.cpp +++ b/map.cpp @@ -733,11 +733,6 @@ QList Map::getAllEvents() { return all; } -QList Map::getEventsByType(QString type) -{ - return events.value(type); -} - void Map::removeEvent(Event *event) { for (QString key : events.keys()) { events[key].removeAll(event); @@ -745,7 +740,7 @@ void Map::removeEvent(Event *event) { } void Map::addEvent(Event *event) { - events[event->get("event_type")].append(event); + events[event->get("event_group_type")].append(event); } bool Map::hasUnsavedChanges() { diff --git a/map.h b/map.h index 2c6bafd8..88fa834f 100755 --- a/map.h +++ b/map.h @@ -194,7 +194,6 @@ public: QString bg_events_label; QList getAllEvents(); - QList getEventsByType(QString type); void removeEvent(Event *event); void addEvent(Event *event); QMap> events; diff --git a/project.cpp b/project.cpp index a0255578..5802198d 100755 --- a/project.cpp +++ b/project.cpp @@ -1186,97 +1186,48 @@ void Project::saveMapEvents(Map *map) { QString path = root + QString("/data/maps/%1/events.inc").arg(map->name); QString text = ""; - if (map->events["object"].length() > 0) { + if (map->events["object_event_group"].length() > 0) { text += QString("%1::\n").arg(map->object_events_label); - for (int i = 0; i < map->events["object"].length(); i++) { - Event *object_event = map->events["object"].value(i); - int radius_x = object_event->getInt("radius_x"); - int radius_y = object_event->getInt("radius_y"); - uint16_t x = object_event->getInt("x"); - uint16_t y = object_event->getInt("y"); - - text += QString("\tobject_event %1").arg(i + 1); - text += QString(", %1").arg(object_event->get("sprite")); - text += QString(", %1").arg(object_event->get("replacement")); - text += QString(", %1").arg(x); - text += QString(", %1").arg(y); - text += QString(", %1").arg(object_event->get("elevation")); - text += QString(", %1").arg(object_event->get("behavior")); - text += QString(", %1").arg(radius_x); - text += QString(", %1").arg(radius_y); - text += QString(", %1").arg(object_event->get("property")); - text += QString(", %1").arg(object_event->get("sight_radius")); - text += QString(", %1").arg(object_event->get("script_label")); - text += QString(", %1").arg(object_event->get("event_flag")); - text += "\n"; + for (int i = 0; i < map->events["object_event_group"].length(); i++) { + Event *object_event = map->events["object_event_group"].value(i); + text += object_event->buildObjectEventMacro(i); } text += "\n"; } - if (map->events["warp"].length() > 0) { + if (map->events["warp_event_group"].length() > 0) { text += QString("%1::\n").arg(map->warps_label); - for (Event *warp : map->events["warp"]) { - text += QString("\twarp_def %1").arg(warp->get("x")); - text += QString(", %1").arg(warp->get("y")); - text += QString(", %1").arg(warp->get("elevation")); - text += QString(", %1").arg(warp->get("destination_warp")); - text += QString(", %1").arg(mapNamesToMapConstants->value(warp->get("destination_map_name"))); - text += "\n"; + for (Event *warp : map->events["warp_event_group"]) { + text += warp->buildWarpEventMacro(mapNamesToMapConstants); } text += "\n"; } - if (map->events["trap"].length() + map->events["trap_weather"].length() > 0) { + if (map->events["coord_event_group"].length() > 0) { text += QString("%1::\n").arg(map->coord_events_label); - for (Event *coords : map->events["trap"]) { - text += QString("\tcoord_event %1").arg(coords->get("x")); - text += QString(", %1").arg(coords->get("y")); - text += QString(", %1").arg(coords->get("elevation")); - text += QString(", 0"); - text += QString(", %1").arg(coords->get("script_var")); - text += QString(", %1").arg(coords->get("script_var_value")); - text += QString(", 0"); - text += QString(", %1").arg(coords->get("script_label")); - text += "\n"; - } - for (Event *coords : map->events["trap_weather"]) { - text += QString("\tcoord_weather_event %1").arg(coords->get("x")); - text += QString(", %1").arg(coords->get("y")); - text += QString(", %1").arg(coords->get("elevation")); - text += QString(", %1").arg(coords->get("weather")); - text += "\n"; + for (Event *event : map->events["coord_event_group"]) { + QString event_type = event->get("event_type"); + if (event_type == EventType::CoordScript) { + text += event->buildCoordScriptEventMacro(); + } else if (event_type == EventType::CoordWeather) { + text += event->buildCoordWeatherEventMacro(); + } } text += "\n"; } - if (map->events["sign"].length() + - map->events["event_hidden_item"].length() + - map->events["event_secret_base"].length() > 0) + if (map->events["bg_event_group"].length() > 0) { text += QString("%1::\n").arg(map->bg_events_label); - for (Event *sign : map->events["sign"]) { - text += QString("\tbg_event %1").arg(sign->get("x")); - text += QString(", %1").arg(sign->get("y")); - text += QString(", %1").arg(sign->get("elevation")); - text += QString(", %1").arg(sign->get("type")); - text += QString(", 0"); - text += QString(", %1").arg(sign->get("script_label")); - text += "\n"; - } - for (Event *item : map->events["event_hidden_item"]) { - text += QString("\tbg_hidden_item_event %1").arg(item->get("x")); - text += QString(", %1").arg(item->get("y")); - text += QString(", %1").arg(item->get("elevation")); - text += QString(", %1").arg(item->get("item")); - text += QString(", %1").arg(item->get("flag")); - text += "\n"; - } - for (Event *item : map->events["event_secret_base"]) { - text += QString("\tbg_secret_base_event %1").arg(item->get("x")); - text += QString(", %1").arg(item->get("y")); - text += QString(", %1").arg(item->get("elevation")); - text += QString(", %1").arg(item->get("secret_base_map")); - text += "\n"; + for (Event *event : map->events["bg_event_group"]) { + QString event_type = event->get("event_type"); + if (event_type == EventType::Sign) { + text += event->buildSignEventMacro(); + } else if (event_type == EventType::HiddenItem) { + text += event->buildHiddenItemEventMacro(); + } else if (event_type == EventType::SecretBase) { + text += event->buildSecretBaseEventMacro(); + } } text += "\n"; } @@ -1310,7 +1261,7 @@ void Project::readMapEvents(Map *map) { map->bg_events_label = labels->value(3); QList *object_events = getLabelMacros(parseAsm(text), map->object_events_label); - map->events["object"].clear(); + map->events["object_event_group"].clear(); for (QStringList command : *object_events) { if (command.value(0) == "object_event") { Event *object = new Event; @@ -1324,18 +1275,18 @@ void Project::readMapEvents(Map *map) { object->put("behavior", command.value(i++)); object->put("radius_x", command.value(i++).toInt(nullptr, 0)); object->put("radius_y", command.value(i++).toInt(nullptr, 0)); - object->put("property", command.value(i++)); - object->put("sight_radius", command.value(i++)); + object->put("trainer_see_type", command.value(i++)); + object->put("sight_radius_tree_id", command.value(i++)); object->put("script_label", command.value(i++)); object->put("event_flag", command.value(i++)); - + object->put("event_group_type", "object_event_group"); object->put("event_type", EventType::Object); - map->events["object"].append(object); + map->events["object_event_group"].append(object); } } QList *warps = getLabelMacros(parseAsm(text), map->warps_label); - map->events["warp"].clear(); + map->events["warp_event_group"].clear(); for (QStringList command : *warps) { if (command.value(0) == "warp_def") { Event *warp = new Event; @@ -1350,8 +1301,9 @@ void Project::readMapEvents(Map *map) { QString mapConstant = command.value(i++); if (mapConstantsToMapNames->contains(mapConstant)) { warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant)); + warp->put("event_group_type", "warp_event_group"); warp->put("event_type", EventType::Warp); - map->events["warp"].append(warp); + map->events["warp_event_group"].append(warp); } else { qDebug() << QString("Destination map constant '%1' is invalid for warp").arg(mapConstant); } @@ -1359,8 +1311,7 @@ void Project::readMapEvents(Map *map) { } QList *coords = getLabelMacros(parseAsm(text), map->coord_events_label); - map->events["trap"].clear(); - map->events["trap_weather"].clear(); + map->events["coord_event_group"].clear(); for (QStringList command : *coords) { if (command.value(0) == "coord_event") { Event *coord = new Event; @@ -1381,8 +1332,9 @@ void Project::readMapEvents(Map *map) { //coord_unknown3 //coord_unknown4 + coord->put("event_group_type", "coord_event_group"); coord->put("event_type", EventType::CoordScript); - map->events["trap"].append(coord); + map->events["coord_event_group"].append(coord); } else if (command.value(0) == "coord_weather_event") { Event *coord = new Event; coord->put("map_name", map->name); @@ -1391,15 +1343,14 @@ void Project::readMapEvents(Map *map) { coord->put("y", command.value(i++)); coord->put("elevation", command.value(i++)); coord->put("weather", command.value(i++)); + coord->put("event_group_type", "coord_event_group"); coord->put("event_type", EventType::CoordWeather); - map->events["trap_weather"].append(coord); + map->events["coord_event_group"].append(coord); } } QList *bgs = getLabelMacros(parseAsm(text), map->bg_events_label); - map->events["sign"].clear(); - map->events["event_hidden_item"].clear(); - map->events["event_secret_base"].clear(); + map->events["bg_event_group"].clear(); for (QStringList command : *bgs) { if (command.value(0) == "bg_event") { Event *bg = new Event; @@ -1408,12 +1359,13 @@ void Project::readMapEvents(Map *map) { bg->put("x", command.value(i++)); bg->put("y", command.value(i++)); bg->put("elevation", command.value(i++)); - bg->put("type", command.value(i++)); + bg->put("player_facing_direction", command.value(i++)); i++; bg->put("script_label", command.value(i++)); //sign_unknown7 + bg->put("event_group_type", "bg_event_group"); bg->put("event_type", EventType::Sign); - map->events["sign"].append(bg); + map->events["bg_event_group"].append(bg); } else if (command.value(0) == "bg_hidden_item_event") { Event *bg = new Event; bg->put("map_name", map->name); @@ -1423,8 +1375,9 @@ void Project::readMapEvents(Map *map) { bg->put("elevation", command.value(i++)); bg->put("item", command.value(i++)); bg->put("flag", command.value(i++)); + bg->put("event_group_type", "bg_event_group"); bg->put("event_type", EventType::HiddenItem); - map->events["event_hidden_item"].append(bg); + map->events["bg_event_group"].append(bg); } else if (command.value(0) == "bg_secret_base_event") { Event *bg = new Event; bg->put("map_name", map->name); @@ -1433,8 +1386,9 @@ void Project::readMapEvents(Map *map) { bg->put("y", command.value(i++)); bg->put("elevation", command.value(i++)); bg->put("secret_base_map", command.value(i++)); + bg->put("event_group_type", "bg_event_group"); bg->put("event_type", EventType::SecretBase); - map->events["event_secret_base"].append(bg); + map->events["bg_event_group"].append(bg); } } } @@ -1444,13 +1398,10 @@ void Project::setNewMapEvents(Map *map) { map->warps_label = "0x0"; map->coord_events_label = "0x0"; map->bg_events_label = "0x0"; - map->events["object"].clear(); - map->events["warp"].clear(); - map->events["trap"].clear(); - map->events["trap_weather"].clear(); - map->events["sign"].clear(); - map->events["event_hidden_item"].clear(); - map->events["event_secret_base"].clear(); + map->events["object_event_group"].clear(); + map->events["warp_event_group"].clear(); + map->events["coord_event_group"].clear(); + map->events["bg_event_group"].clear(); } QStringList Project::readCArray(QString text, QString label) { From fc0cf133b220728536d9053407522b4a55760c8f Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 7 Jul 2018 11:21:19 -0500 Subject: [PATCH 3/7] Bind to all event combobox value changes --- editor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor.h b/editor.h index bc21584a..7564e196 100755 --- a/editor.h +++ b/editor.h @@ -173,7 +173,7 @@ public: emit spriteChanged(event->pixmap); } void bind(QComboBox *combo, QString key) { - connect(combo, static_cast(&QComboBox::activated), + connect(combo, static_cast(&QComboBox::currentTextChanged), this, [this, key](QString value){ this->event->put(key, value); }); From a9325fc79054e5f08ab3114571eb8e95fbc0b229 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 7 Jul 2018 11:57:44 -0500 Subject: [PATCH 4/7] Dynamically set event labels --- map.cpp | 20 ++++++++++++++++++++ map.h | 15 +++++++-------- project.cpp | 44 ++++++++++++++++++++++++-------------------- 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/map.cpp b/map.cpp index a9499b97..6cacb134 100755 --- a/map.cpp +++ b/map.cpp @@ -31,6 +31,26 @@ QString Map::mapConstantFromName(QString mapName) { return constantName; } +QString Map::objectEventsLabelFromName(QString mapName) +{ + return QString("%1_EventObjects").arg(mapName); +} + +QString Map::warpEventsLabelFromName(QString mapName) +{ + return QString("%1_MapWarps").arg(mapName); +} + +QString Map::coordEventsLabelFromName(QString mapName) +{ + return QString("%1_MapCoordEvents").arg(mapName); +} + +QString Map::bgEventsLabelFromName(QString mapName) +{ + return QString("%1_MapBGEvents").arg(mapName); +} + int Map::getWidth() { return layout->width.toInt(nullptr, 0); } diff --git a/map.h b/map.h index 88fa834f..ad935ade 100755 --- a/map.h +++ b/map.h @@ -129,12 +129,16 @@ public: public: void setName(QString mapName); static QString mapConstantFromName(QString mapName); + static QString objectEventsLabelFromName(QString mapName); + static QString warpEventsLabelFromName(QString mapName); + static QString coordEventsLabelFromName(QString mapName); + static QString bgEventsLabelFromName(QString mapName); int getWidth(); int getHeight(); Tileset* getBlockTileset(int); - int getBlockIndex(int layout_id); - int getSelectedBlockIndex(int layout_id); - int getDisplayedBlockIndex(int layout_id); + int getBlockIndex(int); + int getSelectedBlockIndex(int); + int getDisplayedBlockIndex(int); Metatile* getMetatile(int); QImage getMetatileImage(int); QImage getMetatileTile(int); @@ -188,11 +192,6 @@ public: void redo(); void commit(); - QString object_events_label; - QString warps_label; - QString coord_events_label; - QString bg_events_label; - QList getAllEvents(); void removeEvent(Event *event); void addEvent(Event *event); diff --git a/project.cpp b/project.cpp index 5802198d..5d8843c3 100755 --- a/project.cpp +++ b/project.cpp @@ -1185,9 +1185,14 @@ void Project::loadEventPixmaps(QList objects) { void Project::saveMapEvents(Map *map) { QString path = root + QString("/data/maps/%1/events.inc").arg(map->name); QString text = ""; + QString objectEventsLabel = "0x0"; + QString warpEventsLabel = "0x0"; + QString coordEventsLabel = "0x0"; + QString bgEventsLabel = "0x0"; if (map->events["object_event_group"].length() > 0) { - text += QString("%1::\n").arg(map->object_events_label); + objectEventsLabel = Map::objectEventsLabelFromName(map->name); + text += QString("%1::\n").arg(objectEventsLabel); for (int i = 0; i < map->events["object_event_group"].length(); i++) { Event *object_event = map->events["object_event_group"].value(i); text += object_event->buildObjectEventMacro(i); @@ -1196,7 +1201,8 @@ void Project::saveMapEvents(Map *map) { } if (map->events["warp_event_group"].length() > 0) { - text += QString("%1::\n").arg(map->warps_label); + warpEventsLabel = Map::warpEventsLabelFromName(map->name); + text += QString("%1::\n").arg(warpEventsLabel); for (Event *warp : map->events["warp_event_group"]) { text += warp->buildWarpEventMacro(mapNamesToMapConstants); } @@ -1204,7 +1210,8 @@ void Project::saveMapEvents(Map *map) { } if (map->events["coord_event_group"].length() > 0) { - text += QString("%1::\n").arg(map->coord_events_label); + coordEventsLabel = Map::coordEventsLabelFromName(map->name); + text += QString("%1::\n").arg(coordEventsLabel); for (Event *event : map->events["coord_event_group"]) { QString event_type = event->get("event_type"); if (event_type == EventType::CoordScript) { @@ -1218,7 +1225,8 @@ void Project::saveMapEvents(Map *map) { if (map->events["bg_event_group"].length() > 0) { - text += QString("%1::\n").arg(map->bg_events_label); + bgEventsLabel = Map::bgEventsLabelFromName(map->name); + text += QString("%1::\n").arg(bgEventsLabel); for (Event *event : map->events["bg_event_group"]) { QString event_type = event->get("event_type"); if (event_type == EventType::Sign) { @@ -1234,10 +1242,10 @@ void Project::saveMapEvents(Map *map) { text += QString("%1::\n").arg(map->events_label); text += QString("\tmap_events %1, %2, %3, %4\n") - .arg(map->object_events_label) - .arg(map->warps_label) - .arg(map->coord_events_label) - .arg(map->bg_events_label); + .arg(objectEventsLabel) + .arg(warpEventsLabel) + .arg(coordEventsLabel) + .arg(bgEventsLabel); saveTextFile(path, text); } @@ -1255,12 +1263,12 @@ void Project::readMapEvents(Map *map) { } QStringList *labels = getLabelValues(parseAsm(text), map->events_label); - map->object_events_label = labels->value(0); - map->warps_label = labels->value(1); - map->coord_events_label = labels->value(2); - map->bg_events_label = labels->value(3); + QString objectEventsLabel = labels->value(0); + QString warpEventsLabel = labels->value(1); + QString coordEventsLabel = labels->value(2); + QString bgEventsLabel = labels->value(3); - QList *object_events = getLabelMacros(parseAsm(text), map->object_events_label); + QList *object_events = getLabelMacros(parseAsm(text), objectEventsLabel); map->events["object_event_group"].clear(); for (QStringList command : *object_events) { if (command.value(0) == "object_event") { @@ -1285,7 +1293,7 @@ void Project::readMapEvents(Map *map) { } } - QList *warps = getLabelMacros(parseAsm(text), map->warps_label); + QList *warps = getLabelMacros(parseAsm(text), warpEventsLabel); map->events["warp_event_group"].clear(); for (QStringList command : *warps) { if (command.value(0) == "warp_def") { @@ -1310,7 +1318,7 @@ void Project::readMapEvents(Map *map) { } } - QList *coords = getLabelMacros(parseAsm(text), map->coord_events_label); + QList *coords = getLabelMacros(parseAsm(text), coordEventsLabel); map->events["coord_event_group"].clear(); for (QStringList command : *coords) { if (command.value(0) == "coord_event") { @@ -1349,7 +1357,7 @@ void Project::readMapEvents(Map *map) { } } - QList *bgs = getLabelMacros(parseAsm(text), map->bg_events_label); + QList *bgs = getLabelMacros(parseAsm(text), bgEventsLabel); map->events["bg_event_group"].clear(); for (QStringList command : *bgs) { if (command.value(0) == "bg_event") { @@ -1394,10 +1402,6 @@ void Project::readMapEvents(Map *map) { } void Project::setNewMapEvents(Map *map) { - map->object_events_label = "0x0"; - map->warps_label = "0x0"; - map->coord_events_label = "0x0"; - map->bg_events_label = "0x0"; map->events["object_event_group"].clear(); map->events["warp_event_group"].clear(); map->events["coord_event_group"].clear(); From 5bd88e6fef9c8ba784601744f8ea0c319d1ef5a0 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sat, 7 Jul 2018 17:32:54 -0500 Subject: [PATCH 5/7] Add ability to change map border --- editor.cpp | 51 +++++++++++++++++++++++ editor.h | 20 +++++++++ mainwindow.cpp | 3 ++ mainwindow.ui | 98 ++++++++++++++++++++++++++++++++++++++++---- map.cpp | 107 ++----------------------------------------------- map.h | 7 ---- metatile.cpp | 6 --- metatile.h | 16 -------- pretmap.pro | 2 - project.cpp | 1 - tileset.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++++ tileset.h | 20 ++++++++- 12 files changed, 294 insertions(+), 143 deletions(-) delete mode 100755 metatile.cpp delete mode 100755 metatile.h diff --git a/editor.cpp b/editor.cpp index cd8f9d37..e29bb667 100755 --- a/editor.cpp +++ b/editor.cpp @@ -301,6 +301,10 @@ void Editor::onConnectionDirectionChanged(QString newDirection) { ui->comboBox_ConnectionDirection->blockSignals(false); } +void Editor::onBorderMetatilesChanged() { + displayMapBorder(); +} + void Editor::setConnectionsVisibility(bool visible) { for (QGraphicsPixmapItem* item : map->connection_items) { item->setVisible(visible); @@ -383,6 +387,7 @@ void Editor::displayMap() { ); displayMetatiles(); + displayBorderMetatiles(); displayCollisionMetatiles(); displayElevationMetatiles(); displayMapEvents(); @@ -398,6 +403,15 @@ void Editor::displayMetatiles() { scene_metatiles->addItem(metatiles_item); } +void Editor::displayBorderMetatiles() { + scene_selected_border_metatiles = new QGraphicsScene; + selected_border_metatiles_item = new BorderMetatilesPixmapItem(map); + selected_border_metatiles_item->draw(); + scene_selected_border_metatiles->addItem(selected_border_metatiles_item); + + connect(selected_border_metatiles_item, SIGNAL(borderMetatilesChanged()), this, SLOT(onBorderMetatilesChanged())); +} + void Editor::displayCollisionMetatiles() { scene_collision_metatiles = new QGraphicsScene; collision_metatiles_item = new CollisionMetatilesPixmapItem(map); @@ -792,6 +806,43 @@ void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) { } } +void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { + QPointF pos = event->pos(); + int x = ((int)pos.x()) / 16; + int y = ((int)pos.y()) / 16; + + for (int i = 0; i < map->paint_tile_width && (i + x) < 2; i++) { + for (int j = 0; j < map->paint_tile_height && (j + y) < 2; j++) { + int blockIndex = (j + y) * 2 + (i + x); + int tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8)); + (*map->layout->border->blocks)[blockIndex].tile = tile; + } + } + + draw(); + emit borderMetatilesChanged(); +} + +void BorderMetatilesPixmapItem::draw() { + QImage image(32, 32, QImage::Format_RGBA8888); + QPainter painter(&image); + QList *blocks = map->layout->border->blocks; + + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + { + int x = i * 16; + int y = j * 16; + int index = j * 2 + i; + QImage metatile_image = Metatile::getMetatileImage(blocks->value(index).tile, map->layout->tileset_primary, map->layout->tileset_secondary); + QPoint metatile_origin = QPoint(x, y); + painter.drawImage(metatile_origin, metatile_image); + } + + painter.end(); + setPixmap(QPixmap::fromImage(image)); +} + void MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) { QPointF pos = event->pos(); int x = ((int)pos.x()) / 16; diff --git a/editor.h b/editor.h index 7564e196..d2ec601b 100755 --- a/editor.h +++ b/editor.h @@ -16,6 +16,7 @@ class MapPixmapItem; class CollisionPixmapItem; class ConnectionPixmapItem; class MetatilesPixmapItem; +class BorderMetatilesPixmapItem; class CollisionMetatilesPixmapItem; class ElevationMetatilesPixmapItem; @@ -36,6 +37,7 @@ public: void setMap(QString map_name); void displayMap(); void displayMetatiles(); + void displayBorderMetatiles(); void displayCollisionMetatiles(); void displayElevationMetatiles(); void displayMapEvents(); @@ -78,9 +80,11 @@ public: QList borderItems; QGraphicsScene *scene_metatiles = NULL; + QGraphicsScene *scene_selected_border_metatiles = NULL; QGraphicsScene *scene_collision_metatiles = NULL; QGraphicsScene *scene_elevation_metatiles = NULL; MetatilesPixmapItem *metatiles_item = NULL; + BorderMetatilesPixmapItem *selected_border_metatiles_item = NULL; CollisionMetatilesPixmapItem *collision_metatiles_item = NULL; ElevationMetatilesPixmapItem *elevation_metatiles_item = NULL; @@ -123,6 +127,7 @@ private slots: void onConnectionItemSelected(ConnectionPixmapItem* connectionItem); void onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem); void onConnectionDirectionChanged(QString newDirection); + void onBorderMetatilesChanged(); signals: void objectsChanged(); @@ -351,6 +356,21 @@ protected: void mouseReleaseEvent(QGraphicsSceneMouseEvent*); }; +class BorderMetatilesPixmapItem : public QObject, public QGraphicsPixmapItem { + Q_OBJECT +public: + BorderMetatilesPixmapItem(Map *map_) { + map = map_; + setAcceptHoverEvents(true); + } + Map* map = NULL; + virtual void draw(); +signals: + void borderMetatilesChanged(); +protected: + void mousePressEvent(QGraphicsSceneMouseEvent*); +}; + class MovementPermissionsPixmapItem : public MetatilesPixmapItem { Q_OBJECT public: diff --git a/mainwindow.cpp b/mainwindow.cpp index 58f0e684..ea3a571c 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -166,6 +166,9 @@ void MainWindow::setMap(QString map_name) { //ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect()); ui->graphicsView_Metatiles->setFixedSize(editor->metatiles_item->pixmap().width() + 2, editor->metatiles_item->pixmap().height() + 2); + ui->graphicsView_BorderMetatile->setScene(editor->scene_selected_border_metatiles); + ui->graphicsView_BorderMetatile->setFixedSize(editor->selected_border_metatiles_item->pixmap().width() + 2, editor->selected_border_metatiles_item->pixmap().height() + 2); + ui->graphicsView_Collision->setScene(editor->scene_collision_metatiles); //ui->graphicsView_Collision->setSceneRect(editor->scene_collision_metatiles->sceneRect()); ui->graphicsView_Collision->setFixedSize(editor->collision_metatiles_item->pixmap().width() + 2, editor->collision_metatiles_item->pixmap().height() + 2); diff --git a/mainwindow.ui b/mainwindow.ui index 9fc8cb5a..8714f10d 100755 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -60,7 +60,7 @@
- 1 + 0 false @@ -290,7 +290,7 @@ 0 0 - 614 + 475 621 @@ -478,8 +478,8 @@ 0 0 - 180 - 629 + 358 + 612 @@ -488,7 +488,10 @@ 0 - + + + QLayout::SetDefaultConstraint + 0 @@ -501,16 +504,97 @@ 0 - + 0 + + 8 + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Raised + + + + 6 + + + QLayout::SetDefaultConstraint + + + + + + 0 + 0 + + + + Border + + + + + + + + 0 + 0 + + + + + 16777215 + 48 + + + + QFrame::StyledPanel + + + QFrame::Sunken + + + Qt::ScrollBarAsNeeded + + + + + + + Qt::Horizontal + + + QSizePolicy::Maximum + + + + 40 + 20 + + + + + + + + true - + 0 0 diff --git a/map.cpp b/map.cpp index 6cacb134..2c1189c3 100755 --- a/map.cpp +++ b/map.cpp @@ -59,35 +59,6 @@ int Map::getHeight() { return layout->height.toInt(nullptr, 0); } -Tileset* Map::getBlockTileset(int metatile_index) { - int primary_size = 0x200; - if (metatile_index < primary_size) { - return layout->tileset_primary; - } else { - return layout->tileset_secondary; - } -} - -QList> Map::getBlockPalettes(int metatile_index) { - QList> palettes; - for (int i = 0; i < 6; i++) { - palettes.append(layout->tileset_primary->palettes->at(i)); - } - for (int i = 6; i < layout->tileset_secondary->palettes->length(); i++) { - palettes.append(layout->tileset_secondary->palettes->at(i)); - } - return palettes; -} - -int Map::getBlockIndex(int index) { - int primary_size = 0x200; - if (index < primary_size) { - return index; - } else { - return index - primary_size; - } -} - int Map::getSelectedBlockIndex(int index) { if (index < layout->tileset_primary->metatiles->length()) { return index; @@ -104,25 +75,6 @@ int Map::getDisplayedBlockIndex(int index) { } } -QImage Map::getMetatileTile(int tile) { - Tileset *tileset = getBlockTileset(tile); - int local_index = getBlockIndex(tile); - if (!tileset || !tileset->tiles) { - return QImage(); - } - return tileset->tiles->value(local_index, QImage()); -} - -Metatile* Map::getMetatile(int index) { - Tileset *tileset = getBlockTileset(index); - int local_index = getBlockIndex(index); - if (!tileset || !tileset->metatiles) { - return NULL; - } - Metatile *metatile = tileset->metatiles->value(local_index, NULL); - return metatile; -} - QImage Map::getCollisionMetatileImage(Block block) { return getCollisionMetatileImage(block.collision); } @@ -166,57 +118,6 @@ QImage Map::getElevationMetatileImage(int elevation) { return metatile_image; } -QImage Map::getMetatileImage(int tile) { - - QImage metatile_image(16, 16, QImage::Format_RGBA8888); - - Metatile* metatile = getMetatile(tile); - if (!metatile || !metatile->tiles) { - metatile_image.fill(0xffffffff); - return metatile_image; - } - - Tileset* blockTileset = getBlockTileset(tile); - if (!blockTileset) { - metatile_image.fill(0xffffffff); - return metatile_image; - } - QList> palettes = getBlockPalettes(tile); - - QPainter metatile_painter(&metatile_image); - for (int layer = 0; layer < 2; layer++) - for (int y = 0; y < 2; y++) - for (int x = 0; x < 2; x++) { - Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4)); - QImage tile_image = getMetatileTile(tile_.tile); - if (tile_image.isNull()) { - // Some metatiles specify tiles that are outside the valid range. - // These are treated as completely transparent, so they can be skipped without - // being drawn. - continue; - } - - // Colorize the metatile tiles with its palette. - QList palette = palettes.value(tile_.palette); - for (int j = 0; j < palette.length(); j++) { - tile_image.setColor(j, palette.value(j)); - } - - // The top layer of the metatile has its last color displayed at transparent. - if (layer > 0) { - QColor color(tile_image.color(15)); - color.setAlpha(0); - tile_image.setColor(15, color.rgba()); - } - - QPoint origin = QPoint(x*8, y*8); - metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1)); - } - metatile_painter.end(); - - return metatile_image; -} - bool Map::blockChanged(int i, Blockdata *cache) { if (cache == NULL || cache == nullptr) { return true; @@ -295,7 +196,7 @@ QPixmap Map::renderCollision(bool ignoreCache) { } changed_any = true; Block block = layout->blockdata->blocks->value(i); - QImage metatile_image = getMetatileImage(block.tile); + QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary); QImage collision_metatile_image = getCollisionMetatileImage(block); QImage elevation_metatile_image = getElevationMetatileImage(block); int map_y = width_ ? i / width_ : 0; @@ -364,7 +265,7 @@ QPixmap Map::render(bool ignoreCache = false) { } changed_any = true; Block block = layout->blockdata->blocks->value(i); - QImage metatile_image = getMetatileImage(block.tile); + QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary); int map_y = width_ ? i / width_ : 0; int map_x = width_ ? i % width_ : 0; QPoint metatile_origin = QPoint(map_x * 16, map_y * 16); @@ -397,7 +298,7 @@ QPixmap Map::renderBorder() { } changed_any = true; Block block = layout->border->blocks->value(i); - QImage metatile_image = getMetatileImage(block.tile); + QImage metatile_image = Metatile::getMetatileImage(block.tile, layout->tileset_primary, layout->tileset_secondary); int map_y = i / width_; int map_x = i % width_; painter.drawImage(QPoint(map_x * 16, map_y * 16), metatile_image); @@ -513,7 +414,7 @@ QPixmap Map::renderMetatiles() { if (i >= primary_length) { tile += 0x200 - primary_length; } - QImage metatile_image = getMetatileImage(tile); + QImage metatile_image = Metatile::getMetatileImage(tile, layout->tileset_primary, layout->tileset_secondary); int map_y = i / width_; int map_x = i % width_; QPoint metatile_origin = QPoint(map_x * 16, map_y * 16); diff --git a/map.h b/map.h index ad935ade..43f57e06 100755 --- a/map.h +++ b/map.h @@ -135,13 +135,8 @@ public: static QString bgEventsLabelFromName(QString mapName); int getWidth(); int getHeight(); - Tileset* getBlockTileset(int); - int getBlockIndex(int); int getSelectedBlockIndex(int); int getDisplayedBlockIndex(int); - Metatile* getMetatile(int); - QImage getMetatileImage(int); - QImage getMetatileTile(int); QPixmap render(bool ignoreCache); QPixmap renderMetatiles(); @@ -214,8 +209,6 @@ public: void hoveredElevationTileChanged(int elevation); void clearHoveredElevationTile(); - QList > getBlockPalettes(int metatile_index); - signals: void paintTileChanged(Map *map); void paintCollisionChanged(Map *map); diff --git a/metatile.cpp b/metatile.cpp deleted file mode 100755 index 93d435fa..00000000 --- a/metatile.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "metatile.h" - -Metatile::Metatile() -{ - tiles = new QList; -} diff --git a/metatile.h b/metatile.h deleted file mode 100755 index 2712602d..00000000 --- a/metatile.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef METATILE_H -#define METATILE_H - -#include "tile.h" -#include - -class Metatile -{ -public: - Metatile(); -public: - QList *tiles = NULL; - int attr; -}; - -#endif // METATILE_H diff --git a/pretmap.pro b/pretmap.pro index 387e2627..5e722c11 100755 --- a/pretmap.pro +++ b/pretmap.pro @@ -19,7 +19,6 @@ SOURCES += main.cpp\ blockdata.cpp \ block.cpp \ tileset.cpp \ - metatile.cpp \ tile.cpp \ event.cpp \ editor.cpp \ @@ -34,7 +33,6 @@ HEADERS += mainwindow.h \ blockdata.h \ block.h \ tileset.h \ - metatile.h \ tile.h \ event.h \ editor.h \ diff --git a/project.cpp b/project.cpp index 5d8843c3..5422e0a2 100755 --- a/project.cpp +++ b/project.cpp @@ -2,7 +2,6 @@ #include "project.h" #include "tile.h" #include "tileset.h" -#include "metatile.h" #include "event.h" #include diff --git a/tileset.cpp b/tileset.cpp index cb781cc6..4fac3f4f 100755 --- a/tileset.cpp +++ b/tileset.cpp @@ -1,6 +1,112 @@ #include "tileset.h" +#include +#include + Tileset::Tileset() { } + +Metatile::Metatile() +{ + tiles = new QList; +} + +QImage Metatile::getMetatileImage(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) { + QImage metatile_image(16, 16, QImage::Format_RGBA8888); + + Metatile* metatile = Metatile::getMetatile(tile, primaryTileset, secondaryTileset); + if (!metatile || !metatile->tiles) { + metatile_image.fill(0xffffffff); + return metatile_image; + } + + Tileset* blockTileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset); + if (!blockTileset) { + metatile_image.fill(0xffffffff); + return metatile_image; + } + QList> palettes = Metatile::getBlockPalettes(primaryTileset, secondaryTileset); + + QPainter metatile_painter(&metatile_image); + for (int layer = 0; layer < 2; layer++) + for (int y = 0; y < 2; y++) + for (int x = 0; x < 2; x++) { + Tile tile_ = metatile->tiles->value((y * 2) + x + (layer * 4)); + QImage tile_image = Metatile::getMetatileTile(tile_.tile, primaryTileset, secondaryTileset); + if (tile_image.isNull()) { + // Some metatiles specify tiles that are outside the valid range. + // These are treated as completely transparent, so they can be skipped without + // being drawn. + continue; + } + + // Colorize the metatile tiles with its palette. + QList palette = palettes.value(tile_.palette); + for (int j = 0; j < palette.length(); j++) { + tile_image.setColor(j, palette.value(j)); + } + + // The top layer of the metatile has its last color displayed at transparent. + if (layer > 0) { + QColor color(tile_image.color(15)); + color.setAlpha(0); + tile_image.setColor(15, color.rgba()); + } + + QPoint origin = QPoint(x*8, y*8); + metatile_painter.drawImage(origin, tile_image.mirrored(tile_.xflip == 1, tile_.yflip == 1)); + } + metatile_painter.end(); + + return metatile_image; +} + +Metatile* Metatile::getMetatile(int index, Tileset *primaryTileset, Tileset *secondaryTileset) { + Tileset *tileset = Metatile::getBlockTileset(index, primaryTileset, secondaryTileset); + int local_index = Metatile::getBlockIndex(index); + if (!tileset || !tileset->metatiles) { + return NULL; + } + Metatile *metatile = tileset->metatiles->value(local_index, NULL); + return metatile; +} + +QImage Metatile::getMetatileTile(int tile, Tileset *primaryTileset, Tileset *secondaryTileset) { + Tileset *tileset = Metatile::getBlockTileset(tile, primaryTileset, secondaryTileset); + int local_index = Metatile::getBlockIndex(tile); + if (!tileset || !tileset->tiles) { + return QImage(); + } + return tileset->tiles->value(local_index, QImage()); +} + +Tileset* Metatile::getBlockTileset(int metatile_index, Tileset *primaryTileset, Tileset *secondaryTileset) { + int primary_size = 0x200; + if (metatile_index < primary_size) { + return primaryTileset; + } else { + return secondaryTileset; + } +} + +int Metatile::getBlockIndex(int index) { + int primary_size = 0x200; + if (index < primary_size) { + return index; + } else { + return index - primary_size; + } +} + +QList> Metatile::getBlockPalettes(Tileset *primaryTileset, Tileset *secondaryTileset) { + QList> palettes; + for (int i = 0; i < 6; i++) { + palettes.append(primaryTileset->palettes->at(i)); + } + for (int i = 6; i < secondaryTileset->palettes->length(); i++) { + palettes.append(secondaryTileset->palettes->at(i)); + } + return palettes; +} diff --git a/tileset.h b/tileset.h index 78d7b856..f831857b 100755 --- a/tileset.h +++ b/tileset.h @@ -1,9 +1,11 @@ #ifndef TILESET_H #define TILESET_H -#include "metatile.h" +#include "tile.h" #include +class Metatile; + class Tileset { public: @@ -24,4 +26,20 @@ public: QList> *palettes = NULL; }; +class Metatile +{ +public: + Metatile(); +public: + QList *tiles = NULL; + int attr; + + static QImage getMetatileImage(int, Tileset*, Tileset*); + static Metatile* getMetatile(int, Tileset*, Tileset*); + static QImage getMetatileTile(int, Tileset*, Tileset*); + static Tileset* getBlockTileset(int, Tileset*, Tileset*); + static int getBlockIndex(int); + static QList> getBlockPalettes(Tileset*, Tileset*); +}; + #endif // TILESET_H From 37529d37ec13b861e1262da9de20b86e1e26b375 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sun, 8 Jul 2018 10:29:03 -0500 Subject: [PATCH 6/7] Add tileset comboboxes, and make metatile area scrollable --- mainwindow.cpp | 6 ++ mainwindow.ui | 269 +++++++++++++++++++++++++++++++++---------------- project.cpp | 47 ++++++++- project.h | 3 +- 4 files changed, 234 insertions(+), 91 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index ea3a571c..f74240f4 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -222,6 +222,12 @@ void MainWindow::displayMapProperties() { ui->comboBox_Location->addItems(project->getLocations()); ui->comboBox_Location->setCurrentText(map->location); + QMap tilesets = project->getTilesets(); + ui->comboBox_PrimaryTileset->addItems(tilesets.value("primary")); + ui->comboBox_PrimaryTileset->setCurrentText(map->layout->tileset_primary_label); + ui->comboBox_SecondaryTileset->addItems(tilesets.value("secondary")); + ui->comboBox_SecondaryTileset->setCurrentText(map->layout->tileset_secondary_label); + ui->comboBox_Visibility->addItems(project->getVisibilities()); ui->comboBox_Visibility->setCurrentText(map->visibility); diff --git a/mainwindow.ui b/mainwindow.ui index 8714f10d..77405325 100755 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -290,7 +290,7 @@ 0 0 - 475 + 436 621 @@ -451,9 +451,146 @@ 0 + + + + 0 + 0 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + Primary Tileset + + + + + + + true + + + + + + + Secondary Tileset + + + + + + + true + + + + + + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Raised + + + + 6 + + + QLayout::SetDefaultConstraint + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + Border + + + + + + + + 0 + 0 + + + + + 16777215 + 48 + + + + QFrame::StyledPanel + + + QFrame::Sunken + + + Qt::ScrollBarAsNeeded + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 40 + 20 + + + + + + + + - + 0 0 @@ -470,6 +607,9 @@ true + + Qt::AlignHCenter|Qt::AlignTop + true @@ -479,7 +619,7 @@ 0 0 358 - 612 + 497 @@ -488,7 +628,7 @@ 0 - + QLayout::SetDefaultConstraint @@ -504,97 +644,26 @@ 0 - - 0 - - - 8 - - - - - - 0 - 0 - + + + + Qt::Vertical - - QFrame::NoFrame + + + 20 + 40 + - - QFrame::Raised - - - - 6 - - - QLayout::SetDefaultConstraint - - - - - - 0 - 0 - - - - Border - - - - - - - - 0 - 0 - - - - - 16777215 - 48 - - - - QFrame::StyledPanel - - - QFrame::Sunken - - - Qt::ScrollBarAsNeeded - - - - - - - Qt::Horizontal - - - QSizePolicy::Maximum - - - - 40 - 20 - - - - - - + - + true - + 0 0 @@ -610,6 +679,32 @@ + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + +
diff --git a/project.cpp b/project.cpp index 5422e0a2..98c2680b 100755 --- a/project.cpp +++ b/project.cpp @@ -354,7 +354,7 @@ void Project::readMapLayout(Map* map) { map->layout = mapLayouts[map->layout_label]; } - getTilesets(map); + getMapTilesets(map); loadBlockdata(map); loadMapBorder(map); } @@ -494,7 +494,7 @@ void Project::saveMapConstantsHeader() { saveTextFile(root + "/include/constants/maps.h", text); } -void Project::getTilesets(Map* map) { +void Project::getMapTilesets(Map* map) { if (map->layout->has_unsaved_changes) { return; } @@ -955,7 +955,7 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum) { mapNamesToMapConstants->insert(map->name, map->constantName); setNewMapHeader(map, mapLayoutsTable.size() + 1); setNewMapLayout(map); - getTilesets(map); + getMapTilesets(map); setNewMapBlockdata(map); setNewMapBorder(map); setNewMapEvents(map); @@ -1001,6 +1001,47 @@ QStringList Project::getVisibilities() { return names; } +QMap Project::getTilesets() { + QMap allTilesets; + QStringList primaryTilesets; + QStringList secondaryTilesets; + allTilesets.insert("primary", primaryTilesets); + allTilesets.insert("secondary", secondaryTilesets); + QString headers_text = readTextFile(root + "/data/tilesets/headers.inc"); + QList* commands = parseAsm(headers_text); + int i = 0; + while (i < commands->length()) { + if (commands->at(i).length() != 2) + continue; + + if (commands->at(i).at(0) == ".label") { + QString tilesetLabel = commands->at(i).at(1); + // Advance to command specifying whether or not it is a secondary tileset + i += 2; + if (commands->at(i).at(0) != ".byte") { + qDebug() << "Unexpected command found for secondary tileset flag. Expected '.byte', but found: " << commands->at(i).at(0); + continue; + } + + QString secondaryTilesetValue = commands->at(i).at(1); + if (secondaryTilesetValue != "TRUE" && secondaryTilesetValue != "FALSE" && secondaryTilesetValue != "0" && secondaryTilesetValue != "1") { + qDebug() << "Unexpected secondary tileset flag found. Expected \"TRUE\", \"FALSE\", \"0\", or \"1\", but found: " << secondaryTilesetValue; + continue; + } + + bool isSecondaryTileset = (secondaryTilesetValue == "TRUE" || secondaryTilesetValue == "1"); + if (isSecondaryTileset) + allTilesets["secondary"].append(tilesetLabel); + else + allTilesets["primary"].append(tilesetLabel); + } + + i++; + } + + return allTilesets; +} + QStringList Project::getWeathers() { // TODO QStringList names; diff --git a/project.h b/project.h index ca5a29d2..61996ac5 100755 --- a/project.h +++ b/project.h @@ -57,7 +57,7 @@ public: QStringList* readLayoutValues(QString layoutName); void readMapLayout(Map*); void readMapsWithConnections(); - void getTilesets(Map*); + void getMapTilesets(Map*); void loadTilesetAssets(Tileset*); void saveBlockdata(Map*); @@ -74,6 +74,7 @@ public: QStringList getSongNames(); QStringList getLocations(); QStringList getVisibilities(); + QMap getTilesets(); QStringList getWeathers(); QStringList getMapTypes(); QStringList getBattleScenes(); From 48a562d1eb232c43972621360907315d08a88b6b Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Sun, 8 Jul 2018 11:17:43 -0500 Subject: [PATCH 7/7] Update map view when tilesets change --- editor.cpp | 124 ++++++++++++++++++++++++++++++++++++++++--------- editor.h | 5 ++ mainwindow.cpp | 18 +++++++ mainwindow.h | 5 ++ map.h | 1 - project.cpp | 7 ++- project.h | 2 +- tileset.cpp | 13 ++++-- 8 files changed, 143 insertions(+), 32 deletions(-) diff --git a/editor.cpp b/editor.cpp index e29bb667..ac470c02 100755 --- a/editor.cpp +++ b/editor.cpp @@ -306,7 +306,7 @@ void Editor::onBorderMetatilesChanged() { } void Editor::setConnectionsVisibility(bool visible) { - for (QGraphicsPixmapItem* item : map->connection_items) { + for (QGraphicsPixmapItem* item : connection_items) { item->setVisible(visible); item->setActive(visible); } @@ -348,8 +348,13 @@ void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixm } void Editor::displayMap() { - scene = new QGraphicsScene; + if (!scene) + scene = new QGraphicsScene; + if (map_item && scene) { + scene->removeItem(map_item); + delete map_item; + } map_item = new MapPixmapItem(map); connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)), this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*))); @@ -357,6 +362,10 @@ void Editor::displayMap() { map_item->draw(true); scene->addItem(map_item); + if (collision_item && scene) { + scene->removeItem(collision_item); + delete collision_item; + } collision_item = new CollisionPixmapItem(map); connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)), this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*))); @@ -364,19 +373,6 @@ void Editor::displayMap() { collision_item->draw(true); scene->addItem(collision_item); - events_group = new EventGroup; - scene->addItem(events_group); - - if (map_item) { - map_item->setVisible(false); - } - if (collision_item) { - collision_item->setVisible(false); - } - if (events_group) { - events_group->setVisible(false); - } - int tw = 16; int th = 16; scene->setSceneRect( @@ -394,9 +390,24 @@ void Editor::displayMap() { displayMapConnections(); displayMapBorder(); displayMapGrid(); + + if (map_item) { + map_item->setVisible(false); + } + if (collision_item) { + collision_item->setVisible(false); + } + if (events_group) { + events_group->setVisible(false); + } } void Editor::displayMetatiles() { + if (metatiles_item && metatiles_item->scene()) { + metatiles_item->scene()->removeItem(metatiles_item); + delete metatiles_item; + } + scene_metatiles = new QGraphicsScene; metatiles_item = new MetatilesPixmapItem(map); metatiles_item->draw(); @@ -404,6 +415,11 @@ void Editor::displayMetatiles() { } void Editor::displayBorderMetatiles() { + if (selected_border_metatiles_item && selected_border_metatiles_item->scene()) { + selected_border_metatiles_item->scene()->removeItem(selected_border_metatiles_item); + delete selected_border_metatiles_item; + } + scene_selected_border_metatiles = new QGraphicsScene; selected_border_metatiles_item = new BorderMetatilesPixmapItem(map); selected_border_metatiles_item->draw(); @@ -413,6 +429,11 @@ void Editor::displayBorderMetatiles() { } void Editor::displayCollisionMetatiles() { + if (collision_metatiles_item && collision_metatiles_item->scene()) { + collision_metatiles_item->scene()->removeItem(collision_metatiles_item); + delete collision_metatiles_item; + } + scene_collision_metatiles = new QGraphicsScene; collision_metatiles_item = new CollisionMetatilesPixmapItem(map); collision_metatiles_item->draw(); @@ -420,6 +441,11 @@ void Editor::displayCollisionMetatiles() { } void Editor::displayElevationMetatiles() { + if (elevation_metatiles_item && elevation_metatiles_item->scene()) { + elevation_metatiles_item->scene()->removeItem(elevation_metatiles_item); + delete elevation_metatiles_item; + } + scene_elevation_metatiles = new QGraphicsScene; elevation_metatiles_item = new ElevationMetatilesPixmapItem(map); elevation_metatiles_item->draw(); @@ -427,10 +453,22 @@ void Editor::displayElevationMetatiles() { } void Editor::displayMapEvents() { - for (QGraphicsItem *child : events_group->childItems()) { - events_group->removeFromGroup(child); + if (events_group) { + for (QGraphicsItem *child : events_group->childItems()) { + events_group->removeFromGroup(child); + delete child; + } + + if (events_group->scene()) { + events_group->scene()->removeItem(events_group); + } + + delete events_group; } + events_group = new EventGroup; + scene->addItem(events_group); + QList events = map->getAllEvents(); project->loadEventPixmaps(events); for (Event *event : events) { @@ -450,12 +488,18 @@ DraggablePixmapItem *Editor::addMapEvent(Event *event) { } void Editor::displayMapConnections() { - for (QGraphicsPixmapItem* item : map->connection_items) { + for (QGraphicsPixmapItem* item : connection_items) { + if (item->scene()) { + item->scene()->removeItem(item); + } delete item; } - map->connection_items.clear(); + connection_items.clear(); for (ConnectionPixmapItem* item : connection_edit_items) { + if (item->scene()) { + item->scene()->removeItem(item); + } delete item; } selected_connection_item = NULL; @@ -497,7 +541,7 @@ void Editor::createConnectionItem(Connection* connection, bool hide) { item->setX(x); item->setY(y); scene->addItem(item); - map->connection_items.append(item); + connection_items.append(item); item->setVisible(!hide); ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y, map->getWidth(), map->getHeight()); @@ -512,6 +556,14 @@ void Editor::createConnectionItem(Connection* connection, bool hide) { } void Editor::displayMapBorder() { + for (QGraphicsPixmapItem* item : borderItems) { + if (item->scene()) { + item->scene()->removeItem(item); + } + delete item; + } + borderItems.clear(); + QPixmap pixmap = map->renderBorder(); for (int y = -6; y < map->getHeight() + 6; y += 2) for (int x = -6; x < map->getWidth() + 6; x += 2) { @@ -525,18 +577,29 @@ void Editor::displayMapBorder() { } void Editor::displayMapGrid() { + for (QGraphicsLineItem* item : gridLines) { + if (item && item->scene()) { + item->scene()->removeItem(item); + } + delete item; + } + gridLines.clear(); + ui->checkBox_ToggleGrid->disconnect(); + int pixelWidth = map->getWidth() * 16; int pixelHeight = map->getHeight() * 16; for (int i = 0; i <= map->getWidth(); i++) { int x = i * 16; QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight); line->setVisible(ui->checkBox_ToggleGrid->isChecked()); + gridLines.append(line); connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);}); } for (int j = 0; j <= map->getHeight(); j++) { int y = j * 16; QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y); line->setVisible(ui->checkBox_ToggleGrid->isChecked()); + gridLines.append(line); connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);}); } } @@ -679,8 +742,11 @@ void Editor::removeCurrentConnection() { connection_edit_items.removeOne(selected_connection_item); removeMirroredConnection(selected_connection_item->connection); - scene->removeItem(selected_connection_item); - delete selected_connection_item; + if (selected_connection_item && selected_connection_item->scene()) { + selected_connection_item->scene()->removeItem(selected_connection_item); + delete selected_connection_item; + } + selected_connection_item = NULL; setConnectionEditControlsEnabled(false); ui->spinBox_ConnectionOffset->setValue(0); @@ -737,6 +803,20 @@ void Editor::updateDiveEmergeMap(QString mapName, QString direction) { ui->label_NumConnections->setText(QString::number(map->connections.length())); } +void Editor::updatePrimaryTileset(QString tilesetLabel) +{ + map->layout->tileset_primary_label = tilesetLabel; + map->layout->tileset_primary = project->getTileset(tilesetLabel); + emit tilesetChanged(map->name); +} + +void Editor::updateSecondaryTileset(QString tilesetLabel) +{ + map->layout->tileset_secondary_label = tilesetLabel; + map->layout->tileset_secondary = project->getTileset(tilesetLabel); + emit tilesetChanged(map->name); +} + void MetatilesPixmapItem::paintTileChanged(Map *map) { draw(); } diff --git a/editor.h b/editor.h index d2ec601b..b93f9bd5 100755 --- a/editor.h +++ b/editor.h @@ -59,6 +59,8 @@ public: void updateDiveMap(QString mapName); void updateEmergeMap(QString mapName); void setSelectedConnectionFromMap(QString mapName); + void updatePrimaryTileset(QString tilesetLabel); + void updateSecondaryTileset(QString tilesetLabel); DraggablePixmapItem *addMapEvent(Event *event); void selectMapEvent(DraggablePixmapItem *object); @@ -74,10 +76,12 @@ public: QGraphicsPixmapItem *current_view = NULL; MapPixmapItem *map_item = NULL; ConnectionPixmapItem* selected_connection_item = NULL; + QList connection_items; QList connection_edit_items; CollisionPixmapItem *collision_item = NULL; QGraphicsItemGroup *events_group = NULL; QList borderItems; + QList gridLines; QGraphicsScene *scene_metatiles = NULL; QGraphicsScene *scene_selected_border_metatiles = NULL; @@ -133,6 +137,7 @@ signals: void objectsChanged(); void selectedObjectsChanged(); void loadMapRequested(QString, QString); + void tilesetChanged(QString); }; diff --git a/mainwindow.cpp b/mainwindow.cpp index f74240f4..697963cd 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -34,6 +34,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects())); connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects())); connect(editor, SIGNAL(loadMapRequested(QString, QString)), this, SLOT(onLoadMapRequested(QString, QString))); + connect(editor, SIGNAL(tilesetChanged(QString)), this, SLOT(onTilesetChanged(QString))); on_toolButton_Paint_clicked(); @@ -206,6 +207,8 @@ void MainWindow::displayMapProperties() { ui->comboBox_Weather->clear(); ui->comboBox_Type->clear(); ui->comboBox_BattleScene->clear(); + ui->comboBox_PrimaryTileset->clear(); + ui->comboBox_SecondaryTileset->clear(); ui->checkBox_ShowLocation->setChecked(false); if (!editor || !editor->map || !editor->project) { ui->frame_3->setEnabled(false); @@ -418,6 +421,11 @@ void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) setMap(newMapName); } +void MainWindow::onTilesetChanged(QString mapName) +{ + setMap(mapName); +} + void MainWindow::on_mapList_activated(const QModelIndex &index) { QVariant data = index.data(Qt::UserRole); @@ -820,3 +828,13 @@ void MainWindow::on_comboBox_EmergeMap_currentTextChanged(const QString &mapName { editor->updateEmergeMap(mapName); } + +void MainWindow::on_comboBox_PrimaryTileset_activated(const QString &tilesetLabel) +{ + editor->updatePrimaryTileset(tilesetLabel); +} + +void MainWindow::on_comboBox_SecondaryTileset_activated(const QString &tilesetLabel) +{ + editor->updateSecondaryTileset(tilesetLabel); +} diff --git a/mainwindow.h b/mainwindow.h index 5f1b7a2b..88f08661 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -71,6 +71,7 @@ private slots: void onOpenMapListContextMenu(const QPoint &point); void onAddNewMapToGroupClick(QAction* triggeredAction); + void onTilesetChanged(QString); void on_action_Export_Map_Image_triggered(); @@ -88,6 +89,10 @@ private slots: void on_comboBox_EmergeMap_currentTextChanged(const QString &mapName); + void on_comboBox_PrimaryTileset_activated(const QString &arg1); + + void on_comboBox_SecondaryTileset_activated(const QString &arg1); + private: Ui::MainWindow *ui; QStandardItemModel *mapListModel; diff --git a/map.h b/map.h index 43f57e06..a98359a1 100755 --- a/map.h +++ b/map.h @@ -193,7 +193,6 @@ public: QMap> events; QList connections; - QList connection_items; QPixmap renderConnection(Connection); QPixmap renderBorder(); diff --git a/project.cpp b/project.cpp index 98c2680b..68b7a742 100755 --- a/project.cpp +++ b/project.cpp @@ -64,7 +64,6 @@ void Project::loadMapConnections(Map *map) { } map->connections.clear(); - map->connection_items.clear(); if (!map->connections_label.isNull()) { QString path = root + QString("/data/maps/%1/connections.inc").arg(map->name); QString text = readTextFile(path); @@ -354,7 +353,7 @@ void Project::readMapLayout(Map* map) { map->layout = mapLayouts[map->layout_label]; } - getMapTilesets(map); + loadMapTilesets(map); loadBlockdata(map); loadMapBorder(map); } @@ -494,7 +493,7 @@ void Project::saveMapConstantsHeader() { saveTextFile(root + "/include/constants/maps.h", text); } -void Project::getMapTilesets(Map* map) { +void Project::loadMapTilesets(Map* map) { if (map->layout->has_unsaved_changes) { return; } @@ -955,7 +954,7 @@ Map* Project::addNewMapToGroup(QString mapName, int groupNum) { mapNamesToMapConstants->insert(map->name, map->constantName); setNewMapHeader(map, mapLayoutsTable.size() + 1); setNewMapLayout(map); - getMapTilesets(map); + loadMapTilesets(map); setNewMapBlockdata(map); setNewMapBorder(map); setNewMapEvents(map); diff --git a/project.h b/project.h index 61996ac5..18ab4282 100755 --- a/project.h +++ b/project.h @@ -57,7 +57,7 @@ public: QStringList* readLayoutValues(QString layoutName); void readMapLayout(Map*); void readMapsWithConnections(); - void getMapTilesets(Map*); + void loadMapTilesets(Map*); void loadTilesetAssets(Tileset*); void saveBlockdata(Map*); diff --git a/tileset.cpp b/tileset.cpp index 4fac3f4f..1e5dde68 100755 --- a/tileset.cpp +++ b/tileset.cpp @@ -2,6 +2,7 @@ #include #include +#include Tileset::Tileset() { @@ -43,9 +44,13 @@ QImage Metatile::getMetatileImage(int tile, Tileset *primaryTileset, Tileset *se } // Colorize the metatile tiles with its palette. - QList palette = palettes.value(tile_.palette); - for (int j = 0; j < palette.length(); j++) { - tile_image.setColor(j, palette.value(j)); + if (tile_.palette < palettes.length()) { + QList palette = palettes.value(tile_.palette); + for (int j = 0; j < palette.length(); j++) { + tile_image.setColor(j, palette.value(j)); + } + } else { + qDebug() << "Tile is referring to invalid palette number: " << tile_.palette; } // The top layer of the metatile has its last color displayed at transparent. @@ -105,7 +110,7 @@ QList> Metatile::getBlockPalettes(Tileset *primaryTileset, Tileset * for (int i = 0; i < 6; i++) { palettes.append(primaryTileset->palettes->at(i)); } - for (int i = 6; i < secondaryTileset->palettes->length(); i++) { + for (int i = 6; i < 12; i++) { palettes.append(secondaryTileset->palettes->at(i)); } return palettes;