Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add Callables when executing command and use it to refresh layer editor when add or remove layer #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 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,17 @@
using namespace editor::commands;

void AddLayerCommand::redo() {
context_node->add_layer(layer_name, layer_id);
layer_id = context_node->add_layer(layer_name, layer_id);
Command<node::IsometricMap>::redo();
}

void AddLayerCommand::undo() {
if (layer_id == node::IsometricMap::NO_LAYER_ID) {
context_node->remove_layer(layer_name);
return;
} else {
context_node->remove_layer(layer_id);
}
context_node->remove_layer(layer_id);
Command<node::IsometricMap>::undo();
}

void AddLayerCommand::set_layer_id(uint32_t p_layer_id) {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/commands/add_layer_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace editor {
~AddLayerCommand() override = default;

private:
uint32_t layer_id;
String layer_name;
uint32_t layer_id;
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/editor/commands/add_positionable_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ void AddPositionableCommand::redo() {
positionable_id,
layer_id
);
Command<node::IsometricMap>::redo();
}

void AddPositionableCommand::undo() {
context_node->remove_positionable(aabb);
Command<node::IsometricMap>::undo();
}

void AddPositionableCommand::set_aabb(const AABB& p_aabb) {
Expand Down
34 changes: 32 additions & 2 deletions src/editor/commands/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "editor/editor_undo_redo_manager.h"
#include "scene/main/node.h"

constexpr const char* undo_or_redo_triggered { "undo_or_redo_triggered" };

namespace editor {
namespace commands {

Expand All @@ -21,16 +23,32 @@ namespace editor {
static void _bind_methods();

public:
virtual void redo() = 0;
virtual void undo() = 0;
virtual void redo();
virtual void undo();
virtual void set_context_node(TContextNode* p_context_node);
void add_hook(const Callable& p_hook);

void append_to_undoredo();

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

private:
Vector<Callable> hooks;

void _execute_hooks() const;
};

template<class TContextNode>
void Command<TContextNode>::redo() {
_execute_hooks();
}

template<class TContextNode>
void Command<TContextNode>::undo() {
_execute_hooks();
}

template<class TContextNode>
void Command<TContextNode>::append_to_undoredo() {
EditorUndoRedoManager::get_singleton()->add_do_method(this, "redo");
Expand All @@ -42,6 +60,18 @@ namespace editor {
context_node = p_context_node;
}

template<class TContextNode>
void Command<TContextNode>::add_hook(const Callable& p_hook) {
hooks.append(p_hook);
}

template<class TContextNode>
void Command<TContextNode>::_execute_hooks() const {
for (const Callable& hook : hooks) {
hook.call();
}
}

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

Expand Down
2 changes: 2 additions & 0 deletions src/editor/commands/composite_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ namespace editor {
for (int i = 0; i < commands.size(); ++i) {
commands.get(i)->redo();
}
Command<TContextNode>::redo();
}

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

template<class TContextNode>
Expand Down
2 changes: 2 additions & 0 deletions src/editor/commands/move_editor_plane_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ void MoveEditorPlaneCommand::redo() {
IsometricEditorPlugin* isometric_editor_plugin = IsometricEditorPlugin::get_instance();
isometric_editor_plugin->get_editor_plane_for_map(context_node, plane_type).set_position(new_position);
isometric_editor_plugin->refresh(plane_type);
Command<node::IsometricMap>::redo();
}

void MoveEditorPlaneCommand::undo() {
IsometricEditorPlugin* isometric_editor_plugin = IsometricEditorPlugin::get_instance();
isometric_editor_plugin->get_editor_plane_for_map(context_node, plane_type).set_position(old_position);
isometric_editor_plugin->refresh(plane_type);
Command<node::IsometricMap>::undo();
}

void MoveEditorPlaneCommand::set_new_position(int p_new_position) {
Expand Down
2 changes: 2 additions & 0 deletions src/editor/commands/revert_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ namespace editor {
template<class TContextNode>
void RevertCommand<TContextNode>::redo() {
reverse_command->undo();
Command<TContextNode>::redo();
}

template<class TContextNode>
void RevertCommand<TContextNode>::undo() {
reverse_command->redo();
Command<TContextNode>::undo();
}

template<class TContextNode>
Expand Down
2 changes: 2 additions & 0 deletions src/editor/commands/rotate_editor_plane_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ using namespace editor::commands;

void RotateEditorPlaneCommand::redo() {
set_axis_and_position(new_axis, 0);
Command<node::IsometricMap>::redo();
}

void RotateEditorPlaneCommand::undo() {
set_axis_and_position(former_axis, former_position);
Command<node::IsometricMap>::undo();
}

void RotateEditorPlaneCommand::set_new_axis(Vector3::Axis p_axis) {
Expand Down
4 changes: 4 additions & 0 deletions src/editor/commands/select_positionable_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ void SelectPositionableCommand::redo() {
} else {
editor::PositionableSelectorManager::get_instance().select_positionable_at(context_node, positionable);
}

Command<node::IsometricMap>::redo();
}

void SelectPositionableCommand::undo() {
Expand All @@ -30,6 +32,8 @@ void SelectPositionableCommand::undo() {
if (should_deselect_first) {
editor::PositionableSelectorManager::get_instance().set_selected_for_map(context_node, selected_cache);
}

Command<node::IsometricMap>::undo();
}

void SelectPositionableCommand::set_position(const Vector3& p_position) {
Expand Down
4 changes: 4 additions & 0 deletions src/editor/commands/set_layer_visibility_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ using namespace editor::commands;

void SetLayerVisibilityCommand::redo() {
context_node->set_layer_visible(layer_id, is_visible);

Command<node::IsometricMap>::redo();
}

void SetLayerVisibilityCommand::undo() {
context_node->set_layer_visible(layer_id, !is_visible);

Command<node::IsometricMap>::undo();
}

void SetLayerVisibilityCommand::set_layer_id(uint32_t p_layer_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void SetPositionableVisibilityCommand::redo() {
if (node::IsometricPositionable * positionable {context_node->get_positionable_at(position)}) {
positionable->set_visible(!positionable->is_visible());
}
Command<node::IsometricMap>::redo();
}

void SetPositionableVisibilityCommand::undo() {
Expand Down
23 changes: 5 additions & 18 deletions src/editor/inspector/layers_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ constexpr const char add_layer_action_name[] = "add_layer";
constexpr const char remove_layer_action_name[] = "remove_layer";
constexpr const char set_layer_visible_action_name[] = "set_layer_visible";

constexpr const char layer_removed_signal[] = "layer_removed";

LayersEditor::LayersEditor() {
HBoxContainer* top_bar {memnew(HBoxContainer)};
layer_line_edit->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL);
Expand Down Expand Up @@ -128,12 +126,13 @@ void LayersEditor::_add_layer() {
Ref<commands::AddLayerCommand> add_layer_command;
add_layer_command.instantiate();
add_layer_command->set_layer_name(layer_line_edit->get_text());

add_layer_command->add_hook(callable_mp(this, &LayersEditor::refresh));

commands.push_back(add_layer_command);

commands::emitters::CommandToActionTransformer action_transformer;
action_transformer.transform<node::IsometricMap, add_layer_action_name>(commands, IsometricEditorPlugin::get_instance()->get_selected_map());

refresh();
}

void LayersEditor::_on_remove_layer_button(const uint32_t p_layer_id, const String& p_layer_name) {
Expand Down Expand Up @@ -225,25 +224,13 @@ void LayersEditor::_remove_layer(uint32_t p_layer_id, const String& p_layer_name
remove_layer_command.instantiate();
remove_layer_command->set_reverse_command(add_layer_command);

remove_layer_command->add_hook(callable_mp(this, &LayersEditor::refresh));

commands.push_back(remove_layer_command);

commands::emitters::CommandToActionTransformer action_transformer;
action_transformer.transform<node::IsometricMap, remove_layer_action_name>(commands, current_map);

emit_signal(layer_removed_signal);
}
}

void LayersEditor::_notification(int notif) {
if (notif != NOTIFICATION_POSTINITIALIZE) {
return;
}

connect(layer_removed_signal, callable_mp(this, &LayersEditor::refresh));
}

void LayersEditor::_bind_methods() {
ADD_SIGNAL(MethodInfo(layer_removed_signal));
}

#endif
4 changes: 0 additions & 4 deletions src/editor/inspector/layers_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ namespace editor {
void _set_current_layer(uint32_t p_layer_id);
void _layer_color_changed(const Color& p_color, uint32_t p_layer_id);
void _remove_layer(uint32_t p_layer_id, const String& p_layer_name);

protected:
void _notification(int notif);
static void _bind_methods();
};
}// namespace inspector
}// namespace editor
Expand Down
Loading