Skip to content

Commit

Permalink
app: fix new child position in generic component
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed Jun 6, 2024
1 parent b601e02 commit da38f22
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
11 changes: 7 additions & 4 deletions app/gui/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,10 @@ class graph_component_editor_data
component_id m_id = undefined<component_id>();
};

struct generic_component_editor_data {
generic_component_editor_data(const component_id id_) noexcept;
class generic_component_editor_data
{
public:
generic_component_editor_data(const component_id id) noexcept;
~generic_component_editor_data() noexcept;

void show(component_editor& ed) noexcept;
Expand All @@ -407,8 +409,9 @@ struct generic_component_editor_data {
bool fix_input_output = false;
bool force_update_position = false;

ImVector<int> selected_links;
ImVector<int> selected_nodes;
ImVector<int> selected_links;
ImVector<int> selected_nodes;
vector<child_id> update_position_list;

private:
component_id m_id = undefined<component_id>();
Expand Down
53 changes: 43 additions & 10 deletions app/gui/generic/modeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,44 @@ static void show_graph_node(application& app,
ImNodes::PopColorStyle();
}

static void update_position(generic_component_editor_data& data,
generic_component& generic) noexcept
static void update_editor_position(const child_id id,
const child_position& pos) noexcept
{
for_each_data(generic.children, [&](auto& child) {
ImNodes::SetNodeScreenSpacePos(pack_node_child(id), ImVec2(pos.x, pos.y));
}

static void update_editor_all_positions(generic_component& generic) noexcept
{
for_each_data(generic.children, [&](auto& child) noexcept {
const auto id = generic.children.get_id(child);
const auto idx = get_index(id);

ImNodes::SetNodeEditorSpacePos(
pack_node_child(id),
ImVec2(generic.children_positions[idx].x,
generic.children_positions[idx].y));
update_editor_position(id, generic.children_positions[idx]);
});
}

static void update_editor_list_positions(generic_component& generic,
std::span<child_id> ids) noexcept
{
for (const auto id : ids) {
if_data_exists_do(generic.children, id, [&](auto& /*child*/) noexcept {
const auto idx = get_index(id);

update_editor_position(id, generic.children_positions[idx]);
});
}
}

static void update_editor_positions(generic_component_editor_data& data,
generic_component& generic) noexcept
{
if (data.update_position_list.empty()) {
update_editor_all_positions(generic);
} else {
update_editor_list_positions(
generic, std::span<child_id>(data.update_position_list));
data.update_position_list.clear();
}

data.force_update_position = false;
}
Expand Down Expand Up @@ -507,7 +533,7 @@ static void show_graph(component_editor& ed,
const auto pos_x2 = pos.x + width - 50.f;

if (data.force_update_position)
update_position(data, s_parent);
update_editor_positions(data, s_parent);

if (data.show_input_output) {
update_input_output_draggable(parent, data.fix_input_output);
Expand Down Expand Up @@ -657,6 +683,7 @@ static void add_popup_menuitem(component_editor& ed,
s_parent.children_parameters[idx].init_from(type);

parent.state = component_status::modified;
data.update_position_list.emplace_back(id);
data.update_position();

auto& app = container_of(&ed, &application::component_ed);
Expand Down Expand Up @@ -708,6 +735,7 @@ static void compute_grid_layout(settings_window& settings,
}
});

data.update_position_list.clear();
data.update_position();
}

Expand Down Expand Up @@ -736,6 +764,8 @@ static void add_component_to_current(component_editor& ed,

parent_compo.children_positions[c_idx].x = click_pos.x;
parent_compo.children_positions[c_idx].y = click_pos.y;

data.update_position_list.emplace_back(c_id);
data.update_position();
}

Expand Down Expand Up @@ -1189,6 +1219,7 @@ static void show_component_editor(component_editor& ed,
if (app.grid_dlg.is_ok && !app.grid_dlg.is_running) {
app.grid_dlg.save();
app.grid_dlg.is_ok = false;
data.update_position_list.clear();
data.update_position();

for_each_data(s_compo.children, [&](auto& c) noexcept {
Expand All @@ -1209,6 +1240,7 @@ static void show_component_editor(component_editor& ed,
if (app.graph_dlg.is_ok && !app.graph_dlg.is_running) {
app.graph_dlg.save();
app.graph_dlg.is_ok = false;
data.update_position_list.clear();
data.update_position();

for_each_data(s_compo.children, [&](auto& c) noexcept {
Expand Down Expand Up @@ -1260,8 +1292,8 @@ static void show_component_editor(component_editor& ed,
}

generic_component_editor_data::generic_component_editor_data(
const component_id id_) noexcept
: m_id{ id_ }
const component_id id) noexcept
: m_id{ id }
{
context = ImNodes::EditorContextCreate();
ImNodes::PushAttributeFlag(
Expand All @@ -1276,6 +1308,7 @@ generic_component_editor_data::generic_component_editor_data(
ImNodesStyleFlags_GridLinesPrimary | ImNodesStyleFlags_GridSnapping;

first_show_input_output = true;
update_position_list.reserve(8);
update_position();
}

Expand Down

0 comments on commit da38f22

Please sign in to comment.