Skip to content

Commit

Permalink
fix: Make Command template class and use contex_node in commands to a…
Browse files Browse the repository at this point in the history
…void relying on selected_map in plugin
  • Loading branch information
piiertho committed Jan 28, 2024
1 parent a36580c commit bbc244c
Show file tree
Hide file tree
Showing 42 changed files with 227 additions and 218 deletions.
6 changes: 3 additions & 3 deletions src/editor/commands/add_layer_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
using namespace editor::commands;

void AddLayerCommand::redo() {
IsometricEditorPlugin::get_instance()->get_selected_map()->add_layer(layer_name, layer_id);
context_node->add_layer(layer_name, layer_id);
}

void AddLayerCommand::undo() {
if (layer_id == node::IsometricMap::NO_LAYER_ID) {
IsometricEditorPlugin::get_instance()->get_selected_map()->remove_layer(layer_name);
context_node->remove_layer(layer_name);
return;
}
IsometricEditorPlugin::get_instance()->get_selected_map()->remove_layer(layer_id);
context_node->remove_layer(layer_id);
}

void AddLayerCommand::set_layer_id(uint32_t p_layer_id) {
Expand Down
3 changes: 2 additions & 1 deletion src/editor/commands/add_layer_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
#ifdef TOOLS_ENABLED

#include "command.h"
#include "node/isometric_map.h"

namespace editor {
namespace commands {
class AddLayerCommand : public Command {
class AddLayerCommand : public Command<node::IsometricMap> {
public:
void redo() override;
void undo() override;
Expand Down
4 changes: 2 additions & 2 deletions src/editor/commands/add_positionable_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
using namespace editor::commands;

void AddPositionableCommand::redo() {
IsometricEditorPlugin::get_instance()->get_selected_map()->add_positionable_if_nothing_present(
context_node->add_positionable_if_nothing_present(
aabb,
positionable_id,
layer_id
);
}

void AddPositionableCommand::undo() {
IsometricEditorPlugin::get_instance()->get_selected_map()->remove_positionable(aabb);
context_node->remove_positionable(aabb);
}

void AddPositionableCommand::set_aabb(const AABB& p_aabb) {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/commands/add_positionable_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace editor {
namespace commands {
class AddPositionableCommand : public Command {
class AddPositionableCommand : public Command<node::IsometricMap> {
public:
void redo() override;
void undo() override;
Expand Down
19 changes: 0 additions & 19 deletions src/editor/commands/command.cpp

This file was deleted.

29 changes: 27 additions & 2 deletions src/editor/commands/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,50 @@

#include "core/object/ref_counted.h"
#include "editor/editor_undo_redo_manager.h"
#include "scene/main/node.h"

namespace editor {
namespace commands {

template<class TContextNode>
class Command : public RefCounted {
GDCLASS(Command, RefCounted)
GDCLASS(Command<TContextNode>, RefCounted)

protected:
TContextNode* context_node;

static void _bind_methods();

public:
virtual void redo() = 0;
virtual void undo() = 0;
void set_context_node(TContextNode* p_context_node);

void append_to_undoredo();

Command() = default;
Command();
~Command() override = default;
};

template<class TContextNode>
void Command<TContextNode>::append_to_undoredo() {
EditorUndoRedoManager::get_singleton()->add_do_method(this, "redo");
EditorUndoRedoManager::get_singleton()->add_undo_method(this, "undo");
}

template<class TContextNode>
void Command<TContextNode>::set_context_node(TContextNode* p_context_node) {
context_node = p_context_node;
}

template<class TContextNode>
Command<TContextNode>::Command() : context_node(nullptr) {}

template<class TContextNode>
void Command<TContextNode>::_bind_methods() {
ClassDB::bind_method(D_METHOD("redo"), &Command<TContextNode>::redo);
ClassDB::bind_method(D_METHOD("undo"), &Command<TContextNode>::undo);
}
}// namespace commands
}// namespace editor

Expand Down
23 changes: 0 additions & 23 deletions src/editor/commands/composite_command.cpp

This file was deleted.

26 changes: 23 additions & 3 deletions src/editor/commands/composite_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,39 @@

namespace editor {
namespace commands {
class CompositeCommand : public Command {
template<class TContextNode>
class CompositeCommand : public Command<TContextNode> {
public:
void redo() override;
void undo() override;

void append_command(Ref<Command> command);
void append_command(Ref<Command<TContextNode>> command);

CompositeCommand() = default;
~CompositeCommand() override = default;

private:
Vector<Ref<Command>> commands;
Vector<Ref<Command<TContextNode>>> commands;
};

template<class TContextNode>
void editor::commands::CompositeCommand<TContextNode>::redo() {
for (int i = 0; i < commands.size(); ++i) {
commands.get(i)->redo();
}
}

template<class TContextNode>
void CompositeCommand<TContextNode>::undo() {
for (int i = commands.size() - 1; i >= 0; --i) {
commands.get(i)->undo();
}
}

template<class TContextNode>
void CompositeCommand<TContextNode>::append_command(Ref<Command<TContextNode>> command) {// NOLINT(performance-unnecessary-value-param)
commands.push_back(command);
}
}// namespace commands
}// namespace editor

Expand Down
20 changes: 10 additions & 10 deletions src/editor/commands/emitters/command_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ namespace editor {
namespace commands {
namespace emitters {

template<class Derived, class Evt, const char* action_title, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE>
template<class Derived, class Evt, class TContextNode, const char* action_title, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE>
class CommandEmitter {
public:
void on_gui_input(const Ref<InputEvent>& p_event, Node* p_context = nullptr);
void on_gui_input(const Ref<InputEvent>& p_event, TContextNode* p_context = nullptr);

CommandEmitter() = default;
~CommandEmitter() = default;

private:
Vector<Ref<Command>> from_gui_input_to_command(Ref<Evt> p_event);
Vector<Ref<Command<TContextNode>>> from_gui_input_to_command(Ref<Evt> p_event);
};

template<class Derived, class Evt, const char* action_title, UndoRedo::MergeMode merge_mode>
void CommandEmitter<Derived, Evt, action_title, merge_mode>::on_gui_input(const Ref<InputEvent>& p_event, Node* p_context) {
template<class Derived, class Evt, class TContextNode, const char* action_title, UndoRedo::MergeMode merge_mode>
void CommandEmitter<Derived, Evt, TContextNode, action_title, merge_mode>::on_gui_input(const Ref<InputEvent>& p_event, TContextNode* p_context) {
Ref<Evt> event {p_event};

if (event.is_null()) {
return;
}

CommandToActionTransformer action_transformer;
action_transformer.transform<action_title, merge_mode>(
from_gui_input_to_command(event),
p_context
action_transformer.transform<TContextNode, action_title, merge_mode>(
from_gui_input_to_command(event),
p_context
);
}

template<class Derived, class Evt, const char* action_title, UndoRedo::MergeMode merge_mode>
Vector<Ref<Command>> CommandEmitter<Derived, Evt, action_title, merge_mode>::from_gui_input_to_command(Ref<Evt> p_event) {
template<class Derived, class Evt, class TContextNode, const char* action_title, UndoRedo::MergeMode merge_mode>
Vector<Ref<Command<TContextNode>>> CommandEmitter<Derived, Evt, TContextNode, action_title, merge_mode>::from_gui_input_to_command(Ref<Evt> p_event) {
return reinterpret_cast<Derived*>(this)->from_gui_input_to_command_impl(p_event);
}
}// namespace emitters
Expand Down
11 changes: 6 additions & 5 deletions src/editor/commands/emitters/command_to_action_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ namespace editor {
namespace emitters {
class CommandToActionTransformer {
public:
template<const char* action_title, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE>
void transform(const Vector<Ref<Command>> commands, Node* p_context);
template<class TContextNode, const char* action_title, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE>
void transform(const Vector<Ref<Command<TContextNode>>> commands, TContextNode* p_context);

CommandToActionTransformer() = default;
~CommandToActionTransformer() = default;
};

template<const char* action_title, UndoRedo::MergeMode merge_mode>
void CommandToActionTransformer::transform(const Vector<Ref<Command>> commands, Node* p_context) {
template<class TContextNode, const char* action_title, UndoRedo::MergeMode merge_mode>
void CommandToActionTransformer::transform(const Vector<Ref<Command<TContextNode>>> commands, TContextNode* p_context) {
bool has_valid_command {false};
for (int i = 0; i < commands.size(); ++i) {
Ref<Command> command {commands[i]};
Ref<Command<TContextNode>> command {commands[i]};
if (command.is_null()) { continue; }
if (!has_valid_command) {
EditorUndoRedoManager::get_singleton()->create_action(action_title, merge_mode, p_context);
has_valid_command = true;
}
command->set_context_node(p_context);
command->append_to_undoredo();
}

Expand Down
10 changes: 5 additions & 5 deletions src/editor/commands/emitters/delete_command_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

using namespace editor::commands::emitters;

Vector<Ref<editor::commands::Command>> DeleteCommandEmitter::from_gui_input_to_command_impl(Ref<InputEventKey> p_event) {
Vector<Ref<editor::commands::Command>> commands;
Vector<Ref<editor::commands::Command<node::IsometricMap>>> DeleteCommandEmitter::from_gui_input_to_command_impl(Ref<InputEventKey> p_event) {
Vector<Ref<editor::commands::Command<node::IsometricMap>>> commands;

if (!p_event->is_pressed()) { return commands; }

Expand All @@ -33,7 +33,7 @@ Vector<Ref<editor::commands::Command>> DeleteCommandEmitter::from_gui_input_to_c
select_command->set_should_deselect_first(false);
select_command->set_position(position);

Ref<RevertCommand> deselect_command;
Ref<RevertCommand<node::IsometricMap>> deselect_command;
deselect_command.instantiate();
deselect_command->set_reverse_command(select_command);

Expand All @@ -43,11 +43,11 @@ Vector<Ref<editor::commands::Command>> DeleteCommandEmitter::from_gui_input_to_c
add_command->set_aabb({local_position, current->get_size()});
add_command->set_positionable_id(map->get_positionable_id_for_position(local_position));

Ref<RevertCommand> delete_command;
Ref<RevertCommand<node::IsometricMap>> delete_command;
delete_command.instantiate();
delete_command->set_reverse_command(add_command);

Ref<CompositeCommand> composite_command;
Ref<CompositeCommand<node::IsometricMap>> composite_command;
composite_command.instantiate();
composite_command->append_command(deselect_command);
composite_command->append_command(delete_command);
Expand Down
6 changes: 3 additions & 3 deletions src/editor/commands/emitters/delete_command_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace editor {
namespace emitters {
static constexpr const char delete_positionable_action_name[] {"Delete positionable"};

class DeleteCommandEmitter : public CommandEmitter<DeleteCommandEmitter, InputEventKey, delete_positionable_action_name> {
friend class CommandEmitter<DeleteCommandEmitter, InputEventKey, delete_positionable_action_name>;
class DeleteCommandEmitter : public CommandEmitter<DeleteCommandEmitter, InputEventKey, node::IsometricMap, delete_positionable_action_name> {
friend class CommandEmitter<DeleteCommandEmitter, InputEventKey, node::IsometricMap, delete_positionable_action_name>;

public:
DeleteCommandEmitter() = default;
~DeleteCommandEmitter() = default;

private:
Vector<Ref<Command>> from_gui_input_to_command_impl(Ref<InputEventKey> p_event);
Vector<Ref<Command<node::IsometricMap>>> from_gui_input_to_command_impl(Ref<InputEventKey> p_event);
};
}// namespace emitters
}// namespace commands
Expand Down
11 changes: 7 additions & 4 deletions src/editor/commands/emitters/drag_and_drop_command_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

using namespace editor::commands::emitters;

Vector<Ref<editor::commands::Command>> DragAndDropCommandEmitter::from_gui_input_to_command_impl([[maybe_unused]] Ref<InputEventMouse> p_event
Vector<Ref<editor::commands::Command<node::IsometricMap>>> DragAndDropCommandEmitter::from_gui_input_to_command_impl([[maybe_unused]] Ref<InputEventMouse> p_event
) {// NOLINT(performance-unnecessary-value-param)
Vector<Ref<editor::commands::Command>> commands;
Vector<Ref<editor::commands::Command<node::IsometricMap>>> commands;
bool is_activated {initial_position != Vector3(-1, -1, -1)};

int selected_tile_id {editor::IsometricEditorPlugin::get_instance()->get_selection_pane()->get_selected_positionable_id()};
Expand All @@ -22,7 +22,7 @@ Vector<Ref<editor::commands::Command>> DragAndDropCommandEmitter::from_gui_input

const data::IsometricParameters* parameters {IsometricServer::get_instance()->space_get_configuration(map->get_space_RID())};

EditorPlane& editor_plane = isometric_editor_plugin->get_editor_plane_for_selected_map(EditorPlane::PlaneType::EDITOR_DRAWER);
EditorPlane& editor_plane = isometric_editor_plugin->get_editor_plane_for_map(map, EditorPlane::PlaneType::EDITOR_DRAWER);
Vector3::Axis editor_plane_axis = editor_plane.get_axis();
const Vector3& mouse_position {utils::from_screen_to_3D(
*parameters,
Expand Down Expand Up @@ -156,7 +156,10 @@ AABB DragAndDropCommandEmitter::_calculate_real_aabb(const Vector3& initial_posi
}

switch (
IsometricEditorPlugin::get_instance()->get_editor_plane_for_selected_map(EditorPlane::PlaneType::EDITOR_DRAWER).get_axis()
IsometricEditorPlugin::get_instance()->get_editor_plane_for_map(
IsometricEditorPlugin::get_instance()->get_selected_map(),
EditorPlane::PlaneType::EDITOR_DRAWER
).get_axis()
) {
case Vector3::AXIS_X:
return {initial_position, {positionable_x_size, y_size, z_size}};
Expand Down
6 changes: 3 additions & 3 deletions src/editor/commands/emitters/drag_and_drop_command_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace editor {
namespace emitters {
static constexpr const char drag_and_drop_action_name[]{"Add positionables with drag and drop"};

class DragAndDropCommandEmitter : public CommandEmitter<DragAndDropCommandEmitter, InputEventMouse, drag_and_drop_action_name> {
friend class CommandEmitter<DragAndDropCommandEmitter, InputEventMouse, drag_and_drop_action_name>;
class DragAndDropCommandEmitter : public CommandEmitter<DragAndDropCommandEmitter, InputEventMouse, node::IsometricMap, drag_and_drop_action_name> {
friend class CommandEmitter<DragAndDropCommandEmitter, InputEventMouse, node::IsometricMap, drag_and_drop_action_name>;

public:
DragAndDropCommandEmitter();
Expand All @@ -24,7 +24,7 @@ namespace editor {
Vector3 initial_position;
Vector3 limit_position;

Vector<Ref<Command>> from_gui_input_to_command_impl([[maybe_unused]] Ref<InputEventMouse> p_event);
Vector<Ref<Command<node::IsometricMap>>> from_gui_input_to_command_impl([[maybe_unused]] Ref<InputEventMouse> p_event);

void _clear_current_preview_nodes(int new_size);

Expand Down
Loading

0 comments on commit bbc244c

Please sign in to comment.