Skip to content

Commit

Permalink
enh: optimize drag and drop command emitter by adding previews to a n…
Browse files Browse the repository at this point in the history
…ode outside of tree
  • Loading branch information
piiertho committed Jun 12, 2024
1 parent 4ed7825 commit 3aac0b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/editor/commands/emitters/drag_and_drop_command_emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Vector<Ref<editor::commands::Command<node::IsometricMap>>> DragAndDropCommandEmi

int new_size = all_positions.size();

if (preview_node->get_parent() == map) { map->remove_child(preview_node); }

// Shrink, remove and delete excess nodes.
_clear_current_preview_nodes(new_size);

Expand All @@ -71,7 +73,7 @@ Vector<Ref<editor::commands::Command<node::IsometricMap>>> DragAndDropCommandEmi
node::IsometricPositionable* preview_positionable {current_preview_nodes[i]};
preview_positionable->set_modulate(Color(1, 1, 1, 0.5));
preview_positionable->set_local_position_3d(all_positions[i]);
map->add_child(preview_positionable);
preview_node->add_child(preview_positionable);
}

// New nodes are added if the size grew.
Expand All @@ -82,11 +84,14 @@ Vector<Ref<editor::commands::Command<node::IsometricMap>>> DragAndDropCommandEmi
current_preview_nodes.push_back(preview_positionable);
preview_positionable->set_modulate(Color(1, 1, 1, 0.5));
preview_positionable->set_local_position_3d(all_positions[i]);
map->add_child(preview_positionable);
preview_node->add_child(preview_positionable);
}

map->add_child(preview_node);

return commands;
} else if (is_activated) {
map->remove_child(preview_node);
_clear_current_preview_nodes(0);

Vector<Vector3> all_positions {_calculate_positionables_positions(initial_position, limit_position, positionable_size)};
Expand Down Expand Up @@ -114,13 +119,13 @@ void DragAndDropCommandEmitter::_clear_current_preview_nodes(int new_size) {
// Only remove but not delete nodes in the new range
for (int i = 0; i < MIN(new_size, current_preview_nodes.size()); ++i) {
if (node::IsometricPositionable* current_preview_node = current_preview_nodes[i]) {
map->remove_child(current_preview_node);
preview_node->remove_child(current_preview_node);
}
}
// Remove and delete excess nodes.
for (int i = new_size; i < current_preview_nodes.size(); ++i) {
if (node::IsometricPositionable* current_preview_node = current_preview_nodes[i]) {
map->remove_child(current_preview_node);
preview_node->remove_child(current_preview_node);
memdelete(current_preview_node);
}
}
Expand Down Expand Up @@ -204,10 +209,13 @@ DragAndDropCommandEmitter::DragAndDropCommandEmitter() :
CommandEmitter(),
current_preview_nodes(),
initial_position(-1, -1, -1),
limit_position(-1, -1, -1) {}
limit_position(-1, -1, -1),
preview_node(memnew(Node)) {}

DragAndDropCommandEmitter::~DragAndDropCommandEmitter() {
_clear_current_preview_nodes(0);
memdelete(preview_node);
preview_node = nullptr;
}

#endif
2 changes: 2 additions & 0 deletions src/editor/commands/emitters/drag_and_drop_command_emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace editor {
Vector3 initial_position;
Vector3 limit_position;

Node* preview_node;

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

0 comments on commit 3aac0b9

Please # to comment.