Skip to content

Commit

Permalink
feat: Add layer rename in LayerEditor (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
piiertho authored May 25, 2024
1 parent 652487c commit c8cd77b
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 10 deletions.
31 changes: 31 additions & 0 deletions src/editor/commands/change_layer_name_command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifdef TOOLS_ENABLED

#include "change_layer_name_command.h"

using namespace editor::commands;

void ChangeLayerNameCommand::redo() {
context_node->set_layer_name(layer_id, new_layer_name);

Command::redo();
}

void ChangeLayerNameCommand::undo() {
context_node->set_layer_name(layer_id, former_layer_name);

Command::undo();
}

void ChangeLayerNameCommand::set_layer_id(uint32_t p_layer_id) {
layer_id = p_layer_id;
}

void ChangeLayerNameCommand::set_new_layer_name(const String& p_new_layer_name) {
new_layer_name = p_new_layer_name;
}

void ChangeLayerNameCommand::set_former_layer_name(const String& p_former_layer_name) {
former_layer_name = p_former_layer_name;
}

#endif
32 changes: 32 additions & 0 deletions src/editor/commands/change_layer_name_command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef ISOMETRIC_MAPS_CHANGE_LAYER_NAME_COMMAND_H
#define ISOMETRIC_MAPS_CHANGE_LAYER_NAME_COMMAND_H

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

namespace editor {
namespace commands {
class ChangeLayerNameCommand : public Command<node::IsometricMap> {
public:
void redo() override;
void undo() override;

void set_layer_id(uint32_t p_layer_id);
void set_new_layer_name(const String& p_new_layer_name);
void set_former_layer_name(const String& p_former_layer_name);

ChangeLayerNameCommand() = default;
~ChangeLayerNameCommand() override = default;

private:
String new_layer_name;
String former_layer_name;
uint32_t layer_id;
};
}
}

#endif

#endif// ISOMETRIC_MAPS_CHANGE_LAYER_NAME_COMMAND_H
9 changes: 8 additions & 1 deletion src/editor/commands/emitters/command_to_action_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ namespace editor {
public:
template<class TContextNode, const char* action_title, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE, bool backward_undo_ops = false>
void transform(const Vector<Ref<Command<TContextNode>>>& commands, TContextNode* p_context);
template<class TContextNode, UndoRedo::MergeMode merge_mode = UndoRedo::MERGE_DISABLE, bool backward_undo_ops = false>
void transform(const Vector<Ref<Command<TContextNode>>>& commands, TContextNode* p_context, const String& p_action_name);

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

template<class TContextNode, const char* action_title, UndoRedo::MergeMode merge_mode, bool backward_undo_ops>
void CommandToActionTransformer::transform(const Vector<Ref<Command<TContextNode>>>& commands, TContextNode* p_context) {
transform<TContextNode, merge_mode, backward_undo_ops>(commands, p_context, action_title);
}

template<class TContextNode, UndoRedo::MergeMode merge_mode, bool backward_undo_ops>
void CommandToActionTransformer::transform(const Vector<Ref<Command<TContextNode>>>& commands, TContextNode* p_context, const String& p_action_name) {
bool has_valid_command {false};
for (int i = 0; i < commands.size(); ++i) {
Ref<Command<TContextNode>> command {commands[i]};
if (command.is_null()) { continue; }
if (!has_valid_command) {
EditorUndoRedoManager::get_singleton()->create_action(
action_title,
p_action_name,
merge_mode,
p_context,
backward_undo_ops
Expand Down
52 changes: 45 additions & 7 deletions src/editor/inspector/layers_editor.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#ifdef TOOLS_ENABLED

#include "layers_editor.h"
#include "editor/isometric_editor_plugin.h"

#include "editor/commands/add_layer_command.h"
#include "editor/commands/change_layer_name_command.h"
#include "editor/commands/revert_command.h"
#include "editor/commands/set_layer_visibility_command.h"
#include "editor/isometric_editor_plugin.h"

using namespace editor::inspector;

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 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 change_layer_name_action_name_format[] = "Change layer name (id: %s)";

LayersEditor::LayersEditor() {
HBoxContainer* top_bar {memnew(HBoxContainer)};
Expand Down Expand Up @@ -87,9 +90,21 @@ void LayersEditor::refresh() {
);
current_layer_check_box->set_button_group(current_layer_button_group);
layer_controls_container->add_child(current_layer_check_box);
Label* current_layer_name_label {memnew(Label)};
current_layer_name_label->set_text(layer_name);
layer_controls_container->add_child(current_layer_name_label);

if (layer_name == node::IsometricMap::DEFAULT_LAYER_NAME) {
Label* current_layer_name_label {memnew(Label)};
current_layer_name_label->set_text(layer_name);
layer_controls_container->add_child(current_layer_name_label);
} else {
LineEdit* current_layer_name_line_edit {memnew(LineEdit)};
current_layer_name_line_edit->set_text(layer_name);
current_layer_name_line_edit->connect(
SNAME("text_changed"),
callable_mp(this, &LayersEditor::_on_layer_name_changed).bind(layer_id)
);
layer_controls_container->add_child(current_layer_name_line_edit);
}

ColorPickerButton* color_picker_button {memnew(ColorPickerButton)};
color_picker_button->connect(
SNAME("color_changed"),
Expand Down Expand Up @@ -133,6 +148,8 @@ void LayersEditor::_add_layer() {

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

layer_line_edit->clear();
}

void LayersEditor::_on_remove_layer_button(const uint32_t p_layer_id, const String& p_layer_name) {
Expand All @@ -152,6 +169,27 @@ void LayersEditor::_on_remove_layer_button(const uint32_t p_layer_id, const Stri
remove_layer_popup->popup_centered();
}

void LayersEditor::_on_layer_name_changed(const String& p_layer_name, uint32_t p_layer_id) { // NOLINT(*-convert-member-functions-to-static)
if (node::IsometricMap* current_map {IsometricEditorPlugin::get_instance()->get_selected_map()}) {
Vector<Ref<commands::Command<node::IsometricMap>>> commands;

Ref<commands::ChangeLayerNameCommand> change_layer_name_command;
change_layer_name_command.instantiate();
change_layer_name_command->set_layer_id(p_layer_id);
change_layer_name_command->set_new_layer_name(p_layer_name);
change_layer_name_command->set_former_layer_name(current_map->get_layer_name(p_layer_id));

commands.push_back(change_layer_name_command);

commands::emitters::CommandToActionTransformer action_transformer;
action_transformer.transform<node::IsometricMap, UndoRedo::MERGE_ALL>(
commands,
current_map,
vformat(change_layer_name_action_name_format, p_layer_id)
);
}
}

void LayersEditor::_set_layer_visible(const uint32_t p_layer_id, CheckBox* p_check_box) { // NOLINT(*-convert-member-functions-to-static)
Vector<Ref<commands::Command<node::IsometricMap>>> commands;

Expand Down
1 change: 1 addition & 0 deletions src/editor/inspector/layers_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace editor {

void _add_layer();
void _on_remove_layer_button(uint32_t p_layer_id, const String& p_layer_name);
void _on_layer_name_changed(const String& p_layer_name, uint32_t p_layer_id);
void _set_layer_visible(uint32_t p_layer_id, CheckBox* p_check_box);
void _set_current_layer(uint32_t p_layer_id);
void _layer_color_changed(const Color& p_color, uint32_t p_layer_id);
Expand Down
5 changes: 5 additions & 0 deletions src/editor/isometric_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ void IsometricEditorPlugin::_notification(int p_notification) {
// Add layers editor to dock
layers_editor = memnew(editor::inspector::LayersEditor);

EditorUndoRedoManager::get_singleton()->connect(
SNAME("version_changed"),
callable_mp(layers_editor, &inspector::LayersEditor::refresh)
);

RenderingServer::get_singleton()->connect("frame_post_draw", Callable(this, "_on_frame_post_draw"));
}

Expand Down
10 changes: 8 additions & 2 deletions src/node/isometric_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

using namespace node;

static const char* DEFAULT_LAYER_NAME = "Default";

IsometricMap::IsometricMap() : child_positionable_initialized(false)
#ifdef TOOLS_ENABLED
, last_layer_id(0)
Expand Down Expand Up @@ -135,6 +133,14 @@ void IsometricMap::remove_layer(const String& p_layer_name) {
remove_layer(layer_id);
}

void IsometricMap::set_layer_name(uint32_t p_layer_id, const String& p_layer_name) {
layers[p_layer_id] = p_layer_name;
}

String IsometricMap::get_layer_name(uint32_t p_layer_id) {
return layers[p_layer_id];
}

uint32_t IsometricMap::get_layer_id_at(const Vector3& p_position) {
return layers_grid_3d.get_data(p_position);
}
Expand Down
3 changes: 3 additions & 0 deletions src/node/isometric_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace node {
public:
static constexpr uint32_t DEFAULT_LAYER_ID = 0;
static constexpr uint32_t NO_LAYER_ID = 0xffffffff;
static constexpr const char* DEFAULT_LAYER_NAME = "Default";

#ifdef TOOLS_ENABLED
static constexpr const char* LAST_EDITED_LAYER_META_NAME = "_LAST_EDITED_LAYER";
Expand Down Expand Up @@ -59,6 +60,8 @@ namespace node {
uint32_t add_layer(const String& p_layer, uint32_t p_layer_id = NO_LAYER_ID);
void remove_layer(uint32_t p_layer_id);
void remove_layer(const String& p_layer_name);
void set_layer_name(uint32_t p_layer_id, const String& p_layer_name);
String get_layer_name(uint32_t p_layer_id);
uint32_t get_layer_id_at(const Vector3& p_position);
uint32_t get_last_layer_id() const;
void set_last_layer_id(uint32_t p_last_layer_id);
Expand Down

0 comments on commit c8cd77b

Please # to comment.