Skip to content

Commit

Permalink
app: update grid/graph simulation layer
Browse files Browse the repository at this point in the history
  • Loading branch information
quesnel committed Jun 3, 2024
1 parent 6faa0ad commit ed6e4c9
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 266 deletions.
18 changes: 13 additions & 5 deletions app/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,19 @@ static void application_show_windows(application& app) noexcept
}

if (app.component_ed.is_open)
app.component_ed.show();
//if (std::unique_lock l(app.component_ed.mutex, std::try_to_lock);
// l.owns_lock())
app.component_ed.show();

if (app.simulation_ed.is_open)
// if (std::unique_lock l(app.simulation_ed.mutex, std::try_to_lock);
// l.owns_lock())
app.simulation_ed.show();

if (app.output_ed.is_open)
app.output_ed.show();
//if (std::unique_lock l(app.output_ed.mutex, std::try_to_lock);
// l.owns_lock())
app.output_ed.show();

if (app.data_ed.is_open)
app.data_ed.show();
Expand All @@ -482,7 +488,9 @@ static void application_show_windows(application& app) noexcept
app.log_wnd.show();

if (app.library_wnd.is_open)
app.library_wnd.show();
//if (std::unique_lock l(app.library_wnd.mutex, std::try_to_lock);
// l.owns_lock())
app.library_wnd.show();
}

void application::show() noexcept
Expand Down Expand Up @@ -626,8 +634,8 @@ static void show_select_model_box_recursive(application& app,
show_select_model_box_recursive(app, *sibling, access);
}

auto build_unique_component_vector(application& app,
tree_node& tn) -> vector<component_id>
auto build_unique_component_vector(application& app, tree_node& tn)
-> vector<component_id>
{
vector<component_id> ret;
vector<tree_node*> stack;
Expand Down
36 changes: 25 additions & 11 deletions app/gui/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ constexpr T& container_of(M* ptr, const M T::* member)
}

struct application;
struct component_editor;
class component_editor;

enum class notification_id : u32;
enum class plot_copy_id : u32;
Expand Down Expand Up @@ -128,9 +128,12 @@ class notification_manager
void show() noexcept;

private:
data_array<notification, notification_id> data;
ring_buffer<notification_id> r_buffer;
spin_mutex mutex;
data_array<notification, notification_id> m_data;
ring_buffer<notification_id> m_enabled_ids;

spin_mutex m_mutex; /** @c alloc() and @c enable() functions lock the mutex
to fill @c data and @c r_buffer. The show function
only try to lock the mutex to display data. */
};

//! @brief Show notification into a classical window in botton.
Expand Down Expand Up @@ -241,7 +244,9 @@ struct plot_copy_widget {
void show_plot_line(const plot_copy_id id) noexcept;
};

struct output_editor {
class output_editor
{
public:
constexpr static inline const char* name = "Output";

output_editor() noexcept;
Expand Down Expand Up @@ -600,9 +605,6 @@ struct simulation_editor {
small_ring_buffer<std::pair<model_id, ImVec2>, 8>
models_to_move; /**< Online simulation created models need to use ImNodes
API to move into the canvas. */

spin_mutex mutex; /**< Sharing the simulation data from gui's tasks and gui's
draw functions. */
};

struct data_window {
Expand All @@ -628,7 +630,9 @@ struct data_window {
bool is_open = true;
};

struct component_editor {
class component_editor
{
public:
constexpr static inline const char* name = "Component editor";

void show() noexcept;
Expand All @@ -647,7 +651,9 @@ struct component_editor {
component_id m_request_to_open = undefined<component_id>();
};

struct library_window {
class library_window
{
public:
constexpr static inline const char* name = "Library";

library_window() noexcept = default;
Expand Down Expand Up @@ -794,7 +800,9 @@ class component_selector
int files = 0; //! Number of component in registred directories
int unsaved = 0; //! Number of unsaved component

spin_mutex m_mutex;
spin_mutex m_mutex; /** @c update() lock the class to read modeling data and
build the @c ids and @c names vectors. Other
functions try to lock. */
};

class component_model_selector
Expand Down Expand Up @@ -853,6 +861,10 @@ struct application {
simulation sim;
project pj;

spin_mutex mod_mutex;
spin_mutex sim_mutex;
spin_mutex pj_mutex;

component_selector component_sel;
component_model_selector component_model_sel;

Expand Down Expand Up @@ -975,6 +987,8 @@ bool show_local_observers(application& app,
component& compo,
graph_component& graph) noexcept;

void alloc_grid_observer(irt::application& app, irt::tree_node& tn);

bool show_local_observers(application& app,
tree_node& tn,
component& compo,
Expand Down
106 changes: 70 additions & 36 deletions app/gui/grid/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,71 +392,105 @@ bool grid_simulation_editor::show_observations(tree_node& tn,
return grid_simulation_show_observations(app, *this, tn, grid);
}

void alloc_grid_observer(irt::application& app, irt::tree_node& tn)
{
auto& grid = app.pj.grid_observers.alloc();

grid.parent_id = app.pj.tree_nodes.get_id(tn);
grid.compo_id = undefined<component_id>();
grid.tn_id = undefined<tree_node_id>();
grid.mdl_id = undefined<model_id>();
tn.grid_observer_ids.emplace_back(app.pj.grid_observers.get_id(grid));

format(
grid.name, "rename-{}", get_index(app.pj.grid_observers.get_id(grid)));
}

bool show_local_observers(application& app,
tree_node& tn,
component& /*compo*/,
grid_component& /*grid*/) noexcept
{
if (ImGui::CollapsingHeader("Local grid observation")) {
if (app.pj.grid_observers.can_alloc() && ImGui::Button("+##grid")) {
auto& grid = app.pj.grid_observers.alloc();

grid.parent_id = app.pj.tree_nodes.get_id(tn);
grid.compo_id = undefined<component_id>();
grid.tn_id = undefined<tree_node_id>();
grid.mdl_id = undefined<model_id>();
tn.grid_observer_ids.emplace_back(
app.pj.grid_observers.get_id(grid));

format(grid.name,
"rename-{}",
get_index(app.pj.grid_observers.get_id(grid)));
}

std::optional<grid_observer_id> to_delete;
bool is_modified = false;
auto to_del = std::optional<grid_observer_id>();
auto is_modified = false;

if (ImGui::BeginTable("Grid observers", 6)) {
ImGui::TableSetupColumn("id");
ImGui::TableSetupColumn("name");
ImGui::TableSetupColumn("scale");
ImGui::TableSetupColumn("color");
ImGui::TableSetupColumn("model");
ImGui::TableSetupColumn("delete");
ImGui::TableHeadersRow();

for_specified_data(
app.pj.grid_observers,
tn.grid_observer_ids,
[&](auto& grid) noexcept {
const auto id = app.pj.grid_observers.get_id(grid);
ImGui::PushID(&grid);

if (ImGui::InputFilteredString("name", grid.name))
is_modified = true;
ImGui::TableNextRow();
ImGui::TableNextColumn();

ImGui::SameLine();
ImGui::TextFormat("{}", ordinal(id));

if (ImGui::Button("del"))
to_delete =
std::make_optional(app.pj.grid_observers.get_id(grid));
ImGui::TableNextColumn();

ImGui::PushItemWidth(-1.0f);
if (ImGui::InputFilteredString("name", grid.name))
is_modified = true;
ImGui::PopItemWidth();

ImGui::TableNextColumn();
ImGui::PushItemWidth(-1);
ImGui::DragFloatRange2(
"##scale", &grid.scale_min, &grid.scale_max, 0.01f);
ImGui::PopItemWidth();
ImGui::TableNextColumn();
if (ImPlot::ColormapButton(
ImPlot::GetColormapName(grid.color_map),
ImVec2(225, 0),
grid.color_map)) {
grid.color_map =
(grid.color_map + 1) % ImPlot::GetColormapCount();
}

ImGui::TextFormatDisabled(
"grid-id {} component_id {} tree-node-id {} model-id {}",
ordinal(grid.parent_id),
ordinal(grid.compo_id),
ordinal(grid.tn_id),
ordinal(grid.mdl_id));
ImGui::TableNextColumn();
show_select_model_box(
"Select model", "Choose model to observe", app, tn, grid);

if_data_exists_do(
app.sim.models, grid.mdl_id, [&](auto& mdl) noexcept {
ImGui::SameLine();
ImGui::TextUnformatted(
dynamics_type_names[ordinal(mdl.type)]);
});

show_select_model_box(
"Select model", "Choose model to observe", app, tn, grid);
ImGui::TableNextColumn();

if (ImGui::Button("del"))
to_del = id;

ImGui::PopID();
});

if (to_delete.has_value()) {
is_modified = true;
app.pj.grid_observers.free(*to_delete);
ImGui::TableNextRow();
ImGui::TableNextColumn();

if (app.pj.grid_observers.can_alloc() && ImGui::Button("+##grid")) {
alloc_grid_observer(app, tn);
}

ImGui::EndTable();
}

if (to_del.has_value()) {
is_modified = true;
app.pj.grid_observers.free(*to_del);
}

return false;
return is_modified;
}

} // namespace irt
25 changes: 19 additions & 6 deletions app/gui/library-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ static void add_generic_component_data(application& app) noexcept
app.generics.alloc(compo_id);
app.component_ed.request_to_open(compo_id);

app.add_gui_task([&app]() noexcept { app.component_sel.update(); });
app.add_gui_task([&app]() noexcept {
std::scoped_lock lock(app.mod_mutex);
app.component_sel.update();
});
}

static void add_grid_component_data(application& app) noexcept
Expand All @@ -27,7 +30,10 @@ static void add_grid_component_data(application& app) noexcept
auto compo_id = app.mod.components.get_id(compo);
app.grids.alloc(compo_id, compo.id.grid_id);
app.component_ed.request_to_open(compo_id);
app.add_gui_task([&app]() noexcept { app.component_sel.update(); });
app.add_gui_task([&app]() noexcept {
std::scoped_lock lock(app.mod_mutex);
app.component_sel.update();
});
}

static void add_graph_component_data(application& app) noexcept
Expand All @@ -37,7 +43,10 @@ static void add_graph_component_data(application& app) noexcept
app.graphs.alloc(compo_id, compo.id.graph_id);
app.component_ed.request_to_open(compo_id);

app.add_gui_task([&app]() noexcept { app.component_sel.update(); });
app.add_gui_task([&app]() noexcept {
std::scoped_lock lock(app.mod_mutex);
app.component_sel.update();
});
}

static void show_component_popup_menu(application& app, component& sel) noexcept
Expand Down Expand Up @@ -71,8 +80,10 @@ static void show_component_popup_menu(application& app, component& sel) noexcept
app.notifications.enable(n);
}

app.add_gui_task(
[&app]() noexcept { app.component_sel.update(); });
app.add_gui_task([&app]() noexcept {
std::scoped_lock lock(app.mod_mutex);
app.component_sel.update();
});
} else {
auto& n = app.notifications.alloc();
n.level = log_level::error;
Expand All @@ -85,7 +96,9 @@ static void show_component_popup_menu(application& app, component& sel) noexcept
const auto compo_id = app.mod.components.get_id(sel);

app.add_gui_task([&app, compo_id]() noexcept {
std::scoped_lock lock{ app.simulation_ed.mutex };
std::scoped_lock lock{ app.sim_mutex,
app.mod_mutex,
app.pj_mutex };

attempt_all(
[&]() noexcept -> status {
Expand Down
Loading

0 comments on commit ed6e4c9

Please sign in to comment.