Skip to content

Commit

Permalink
Consolidate event constructor calls
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Aug 31, 2024
1 parent 0b4f027 commit 9ae3360
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 93 deletions.
2 changes: 2 additions & 0 deletions include/core/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class Event {
}
}

static Event* create(Event::Type type);

static QMap<Event::Group, const QPixmap*> icons;

// standard public methods
Expand Down
15 changes: 15 additions & 0 deletions src/core/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

QMap<Event::Group, const QPixmap*> Event::icons;

Event* Event::create(Event::Type type) {
switch (type) {
case Event::Type::Object: return new ObjectEvent();
case Event::Type::CloneObject: return new CloneObjectEvent();
case Event::Type::Warp: return new WarpEvent();
case Event::Type::Trigger: return new TriggerEvent();
case Event::Type::WeatherTrigger: return new WeatherTriggerEvent();
case Event::Type::Sign: return new SignEvent();
case Event::Type::HiddenItem: return new HiddenItemEvent();
case Event::Type::SecretBase: return new SecretBaseEvent();
case Event::Type::HealLocation: return new HealLocationEvent();
default: return nullptr;
}
}

Event::~Event() {
if (this->eventFrame)
this->eventFrame->deleteLater();
Expand Down
62 changes: 15 additions & 47 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,10 +2003,10 @@ void Editor::selectMapEvent(DraggablePixmapItem *object, bool toggle) {
selected_events->clear();
selected_events->append(object);
} else if (!selected_events->contains(object)) {
// Adding event to selection
// Adding event to group selection
selected_events->append(object);
} else if (selected_events->length() > 1) {
// Removing from group selection
// Removing event from group selection
selected_events->removeOne(object);
} else {
// Attempting to toggle the only currently-selected event.
Expand Down Expand Up @@ -2068,56 +2068,24 @@ void Editor::duplicateSelectedEvents() {
}

DraggablePixmapItem *Editor::addNewEvent(Event::Type type) {
Event *event = nullptr;
if (!project || !map || eventLimitReached(type))
return nullptr;

if (project && map && !eventLimitReached(type)) {
switch (type) {
case Event::Type::Object:
event = new ObjectEvent();
break;
case Event::Type::CloneObject:
event = new CloneObjectEvent();
break;
case Event::Type::Warp:
event = new WarpEvent();
break;
case Event::Type::Trigger:
event = new TriggerEvent();
break;
case Event::Type::WeatherTrigger:
event = new WeatherTriggerEvent();
break;
case Event::Type::Sign:
event = new SignEvent();
break;
case Event::Type::HiddenItem:
event = new HiddenItemEvent();
break;
case Event::Type::SecretBase:
event = new SecretBaseEvent();
break;
case Event::Type::HealLocation: {
event = new HealLocationEvent();
event->setMap(this->map);
event->setDefaultValues(this->project);
HealLocation healLocation = HealLocation::fromEvent(event);
project->healLocations.append(healLocation);
((HealLocationEvent *)event)->setIndex(project->healLocations.length());
break;
}
default:
break;
}
if (!event) return nullptr;
Event *event = Event::create(type);
if (!event)
return nullptr;

event->setMap(this->map);
event->setDefaultValues(this->project);
event->setMap(this->map);
event->setDefaultValues(this->project);

map->editHistory.push(new EventCreate(this, map, event));
return event->getPixmapItem();
if (type == Event::Type::HealLocation) {
HealLocation healLocation = HealLocation::fromEvent(event);
project->healLocations.append(healLocation);
((HealLocationEvent *)event)->setIndex(project->healLocations.length());
}

return nullptr;
map->editHistory.push(new EventCreate(this, map, event));
return event->getPixmapItem();
}

// Currently only object events have an explicit limit
Expand Down
61 changes: 15 additions & 46 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ void MainWindow::paste() {
editor->metatile_selector_item->setExternalSelection(width, height, metatiles, collisions);
break;
}
case 1:
case MainTab::Events:
{
// can only paste events to this tab
if (pasteObject["object"].toString() != "events") {
Expand All @@ -1773,56 +1773,25 @@ void MainWindow::paste() {
QJsonArray events = pasteObject["events"].toArray();
for (QJsonValue event : events) {
// paste the event to the map
Event *pasteEvent = nullptr;

Event::Type type = Event::eventTypeFromString(event["event_type"].toString());
const QString typeString = event["event_type"].toString();
Event::Type type = Event::eventTypeFromString(typeString);

if (this->editor->eventLimitReached(type)) {
logWarn(QString("Cannot paste event, the limit for type '%1' has been reached.").arg(event["event_type"].toString()));
break;
logWarn(QString("Cannot paste event, the limit for type '%1' has been reached.").arg(typeString));
continue;
}

switch (type) {
case Event::Type::Object:
pasteEvent = new ObjectEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
case Event::Type::CloneObject:
pasteEvent = new CloneObjectEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
case Event::Type::Warp:
pasteEvent = new WarpEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
case Event::Type::Trigger:
pasteEvent = new TriggerEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
case Event::Type::WeatherTrigger:
pasteEvent = new WeatherTriggerEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
case Event::Type::Sign:
pasteEvent = new SignEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
case Event::Type::HiddenItem:
pasteEvent = new HiddenItemEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
case Event::Type::SecretBase:
pasteEvent = new SecretBaseEvent();
pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
break;
default:
break;
if (type == Event::Type::HealLocation) {
logWarn(QString("Cannot paste events of type '%1'").arg(typeString));
continue;
}

if (pasteEvent) {
pasteEvent->setMap(this->editor->map);
newEvents.append(pasteEvent);
}
Event *pasteEvent = Event::create(type);
if (!pasteEvent)
continue;

pasteEvent->loadFromJson(event["event"].toObject(), this->editor->project);
pasteEvent->setMap(this->editor->map);
newEvents.append(pasteEvent);
}

if (!newEvents.empty()) {
Expand Down

0 comments on commit 9ae3360

Please sign in to comment.