Skip to content

Commit

Permalink
Support custom fields for all event types
Browse files Browse the repository at this point in the history
  • Loading branch information
huderlem committed Feb 5, 2019
1 parent a894bea commit c4ad097
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 15 deletions.
5 changes: 5 additions & 0 deletions include/core/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Event
{
public:
Event();
Event(QJsonObject, QString);
public:
int x() {
return getInt("x");
Expand Down Expand Up @@ -75,8 +76,12 @@ class Event
void setPixmapFromSpritesheet(QImage, int, int);
int getPixelX();
int getPixelY();
QMap<QString, bool> getExpectedFields();
void readCustomValues(QJsonObject values);
void addCustomValuesTo(QJsonObject *obj);

QMap<QString, QString> values;
QMap<QString, QString> customValues;
QPixmap pixmap;
int spriteWidth;
int spriteHeight;
Expand Down
22 changes: 22 additions & 0 deletions include/ui/customattributestable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef CUSTOMATTRIBUTESTABLE_H
#define CUSTOMATTRIBUTESTABLE_H

#include "event.h"
#include <QObject>
#include <QFrame>
#include <QTableWidget>

class CustomAttributesTable : public QFrame
{
public:
explicit CustomAttributesTable(Event *event, QWidget *parent = nullptr);
~CustomAttributesTable();

private:
Event *event;
QTableWidget *table;
void resizeVertically();
QMap<QString, QString> getTableFields();
};

#endif // CUSTOMATTRIBUTESTABLE_H
2 changes: 2 additions & 0 deletions porymap.pro
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOURCES += src/core/block.cpp \
src/ui/connectionpixmapitem.cpp \
src/ui/currentselectedmetatilespixmapitem.cpp \
src/ui/cursortilerect.cpp \
src/ui/customattributestable.cpp \
src/ui/eventpropertiesframe.cpp \
src/ui/filterchildrenproxymodel.cpp \
src/ui/graphicsview.cpp \
Expand Down Expand Up @@ -83,6 +84,7 @@ HEADERS += include/core/block.h \
include/ui/connectionpixmapitem.h \
include/ui/currentselectedmetatilespixmapitem.h \
include/ui/cursortilerect.h \
include/ui/customattributestable.h \
include/ui/eventpropertiesframe.h \
include/ui/filterchildrenproxymodel.h \
include/ui/graphicsview.h \
Expand Down
108 changes: 108 additions & 0 deletions src/core/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ Event::Event()
this->usingSprite = false;
}

Event::Event(QJsonObject obj, QString type)
{
Event();
this->put("event_type", type);
this->readCustomValues(obj);
}

Event* Event::createNewEvent(QString event_type, QString map_name)
{
Event *event = new Event;
Expand Down Expand Up @@ -139,6 +146,100 @@ int Event::getPixelY()
return (this->y() * 16) - qMax(0, this->spriteHeight - 16);
}

QMap<QString, bool> Event::getExpectedFields()
{
QString type = this->get("event_type");
if (type == EventType::Object) {
return QMap<QString, bool> {
{"graphics_id", true},
{"x", true},
{"y", true},
{"elevation", true},
{"movement_type", true},
{"movement_range_x", true},
{"movement_range_y", true},
{"trainer_type", true},
{"trainer_sight_or_berry_tree_id", true},
{"script", true},
{"flag", true},
};
} else if (type == EventType::Warp) {
return QMap<QString, bool> {
{"x", true},
{"y", true},
{"elevation", true},
{"dest_map", true},
{"dest_warp_id", true},
};
} else if (type == EventType::Trigger) {
return QMap<QString, bool> {
{"type", true},
{"x", true},
{"y", true},
{"elevation", true},
{"var", true},
{"var_value", true},
{"script", true},
};
} else if (type == EventType::WeatherTrigger) {
return QMap<QString, bool> {
{"type", true},
{"x", true},
{"y", true},
{"elevation", true},
{"weather", true},
};
} else if (type == EventType::Sign) {
return QMap<QString, bool> {
{"type", true},
{"x", true},
{"y", true},
{"elevation", true},
{"player_facing_dir", true},
{"script", true},
};
} else if (type == EventType::HiddenItem) {
return QMap<QString, bool> {
{"type", true},
{"x", true},
{"y", true},
{"elevation", true},
{"item", true},
{"flag", true},
};
} else if (type == EventType::SecretBase) {
return QMap<QString, bool> {
{"type", true},
{"x", true},
{"y", true},
{"elevation", true},
{"secret_base_id", true},
};
} else {
return QMap<QString, bool>();
}
};

void Event::readCustomValues(QJsonObject values)
{
this->customValues.clear();
QMap<QString, bool> expectedValues = this->getExpectedFields();
for (QString key : values.keys()) {
if (!expectedValues.contains(key)) {
this->customValues[key] = values[key].toString();
}
}
}

void Event::addCustomValuesTo(QJsonObject *obj)
{
for (QString key : this->customValues.keys()) {
if (!obj->contains(key)) {
(*obj)[key] = this->customValues[key];
}
}
}

QJsonObject Event::buildObjectEventJSON()
{
QJsonObject eventObj;
Expand All @@ -153,6 +254,7 @@ QJsonObject Event::buildObjectEventJSON()
eventObj["trainer_sight_or_berry_tree_id"] = this->get("sight_radius_tree_id");
eventObj["script"] = this->get("script_label");
eventObj["flag"] = this->get("event_flag");
this->addCustomValuesTo(&eventObj);

return eventObj;
}
Expand All @@ -165,6 +267,7 @@ QJsonObject Event::buildWarpEventJSON(QMap<QString, QString> *mapNamesToMapConst
warpObj["elevation"] = this->getInt("elevation");
warpObj["dest_map"] = mapNamesToMapConstants->value(this->get("destination_map_name"));
warpObj["dest_warp_id"] = this->getInt("destination_warp");
this->addCustomValuesTo(&warpObj);

return warpObj;
}
Expand All @@ -179,6 +282,7 @@ QJsonObject Event::buildTriggerEventJSON()
triggerObj["var"] = this->get("script_var");
triggerObj["var_value"] = this->get("script_var_value");
triggerObj["script"] = this->get("script_label");
this->addCustomValuesTo(&triggerObj);

return triggerObj;
}
Expand All @@ -191,6 +295,7 @@ QJsonObject Event::buildWeatherTriggerEventJSON()
weatherObj["y"] = this->getU16("y");
weatherObj["elevation"] = this->getInt("elevation");
weatherObj["weather"] = this->get("weather");
this->addCustomValuesTo(&weatherObj);

return weatherObj;
}
Expand All @@ -204,6 +309,7 @@ QJsonObject Event::buildSignEventJSON()
signObj["elevation"] = this->getInt("elevation");
signObj["player_facing_dir"] = this->get("player_facing_direction");
signObj["script"] = this->get("script_label");
this->addCustomValuesTo(&signObj);

return signObj;
}
Expand All @@ -217,6 +323,7 @@ QJsonObject Event::buildHiddenItemEventJSON()
hiddenItemObj["elevation"] = this->getInt("elevation");
hiddenItemObj["item"] = this->get("item");
hiddenItemObj["flag"] = this->get("flag");
this->addCustomValuesTo(&hiddenItemObj);

return hiddenItemObj;
}
Expand All @@ -229,6 +336,7 @@ QJsonObject Event::buildSecretBaseEventJSON()
secretBaseObj["y"] = this->getU16("y");
secretBaseObj["elevation"] = this->getInt("elevation");
secretBaseObj["secret_base_id"] = this->get("secret_base_id");
this->addCustomValuesTo(&secretBaseObj);

return secretBaseObj;
}
Expand Down
6 changes: 5 additions & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ui_eventpropertiesframe.h"
#include "bordermetatilespixmapitem.h"
#include "currentselectedmetatilespixmapitem.h"
#include "customattributestable.h"

#include <QFileDialog>
#include <QStandardItemModel>
Expand Down Expand Up @@ -1376,8 +1377,11 @@ void MainWindow::updateSelectedObjects() {
item->bind(combo, key);
}

frames.append(frame);
// Custom fields table.
CustomAttributesTable *customAttributes = new CustomAttributesTable(item->event, frame);
frame->layout()->addWidget(customAttributes);

frames.append(frame);
}

//int scroll = ui->scrollArea_4->verticalScrollBar()->value();
Expand Down
21 changes: 7 additions & 14 deletions src/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ bool Project::loadMapData(Map* map) {
QJsonArray objectEventsArr = mapObj["object_events"].toArray();
for (int i = 0; i < objectEventsArr.size(); i++) {
QJsonObject event = objectEventsArr[i].toObject();
Event *object = new Event;
Event *object = new Event(event, EventType::Object);
object->put("map_name", map->name);
object->put("sprite", event["graphics_id"].toString());
object->put("x", QString::number(event["x"].toInt()));
Expand All @@ -232,15 +232,14 @@ bool Project::loadMapData(Map* map) {
object->put("script_label", event["script"].toString());
object->put("event_flag", event["flag"].toString());
object->put("event_group_type", "object_event_group");
object->put("event_type", EventType::Object);
map->events["object_event_group"].append(object);
}

map->events["warp_event_group"].clear();
QJsonArray warpEventsArr = mapObj["warp_events"].toArray();
for (int i = 0; i < warpEventsArr.size(); i++) {
QJsonObject event = warpEventsArr[i].toObject();
Event *warp = new Event;
Event *warp = new Event(event, EventType::Warp);
warp->put("map_name", map->name);
warp->put("x", QString::number(event["x"].toInt()));
warp->put("y", QString::number(event["y"].toInt()));
Expand All @@ -252,12 +251,10 @@ bool Project::loadMapData(Map* map) {
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_event_group"].append(warp);
} else if (mapConstant == NONE_MAP_CONSTANT) {
warp->put("destination_map_name", NONE_MAP_NAME);
warp->put("event_group_type", "warp_event_group");
warp->put("event_type", EventType::Warp);
map->events["warp_event_group"].append(warp);
} else {
logError(QString("Destination map constant '%1' is invalid for warp").arg(mapConstant));
Expand Down Expand Up @@ -292,7 +289,7 @@ bool Project::loadMapData(Map* map) {
QJsonObject event = coordEventsArr[i].toObject();
QString type = event["type"].toString();
if (type == "trigger") {
Event *coord = new Event;
Event *coord = new Event(event, EventType::Trigger);
coord->put("map_name", map->name);
coord->put("x", QString::number(event["x"].toInt()));
coord->put("y", QString::number(event["y"].toInt()));
Expand All @@ -301,10 +298,9 @@ bool Project::loadMapData(Map* map) {
coord->put("script_var_value", QString::number(event["var_value"].toInt()));
coord->put("script_label", event["script"].toString());
coord->put("event_group_type", "coord_event_group");
coord->put("event_type", EventType::Trigger);
map->events["coord_event_group"].append(coord);
} else if (type == "weather") {
Event *coord = new Event;
Event *coord = new Event(event, EventType::WeatherTrigger);
coord->put("map_name", map->name);
coord->put("x", QString::number(event["x"].toInt()));
coord->put("y", QString::number(event["y"].toInt()));
Expand All @@ -322,36 +318,33 @@ bool Project::loadMapData(Map* map) {
QJsonObject event = bgEventsArr[i].toObject();
QString type = event["type"].toString();
if (type == "sign") {
Event *bg = new Event;
Event *bg = new Event(event, EventType::Sign);
bg->put("map_name", map->name);
bg->put("x", QString::number(event["x"].toInt()));
bg->put("y", QString::number(event["y"].toInt()));
bg->put("elevation", QString::number(event["elevation"].toInt()));
bg->put("player_facing_direction", event["player_facing_dir"].toString());
bg->put("script_label", event["script"].toString());
bg->put("event_group_type", "bg_event_group");
bg->put("event_type", EventType::Sign);
map->events["bg_event_group"].append(bg);
} else if (type == "hidden_item") {
Event *bg = new Event;
Event *bg = new Event(event, EventType::HiddenItem);
bg->put("map_name", map->name);
bg->put("x", QString::number(event["x"].toInt()));
bg->put("y", QString::number(event["y"].toInt()));
bg->put("elevation", QString::number(event["elevation"].toInt()));
bg->put("item", event["item"].toString());
bg->put("flag", event["flag"].toString());
bg->put("event_group_type", "bg_event_group");
bg->put("event_type", EventType::HiddenItem);
map->events["bg_event_group"].append(bg);
} else if (type == "secret_base") {
Event *bg = new Event;
Event *bg = new Event(event, EventType::SecretBase);
bg->put("map_name", map->name);
bg->put("x", QString::number(event["x"].toInt()));
bg->put("y", QString::number(event["y"].toInt()));
bg->put("elevation", QString::number(event["elevation"].toInt()));
bg->put("secret_base_id", event["secret_base_id"].toString());
bg->put("event_group_type", "bg_event_group");
bg->put("event_type", EventType::SecretBase);
map->events["bg_event_group"].append(bg);
}
}
Expand Down
Loading

0 comments on commit c4ad097

Please sign in to comment.