Skip to content

Commit

Permalink
nicer default view
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstink committed Jan 12, 2025
1 parent b48e9b6 commit 12b53eb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 49 deletions.
3 changes: 2 additions & 1 deletion symmetri/gui/draw_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ void draw_graph(const model::ViewModel& vm) {
(vm.selected_node_idx.has_value() || vm.selected_arc_idxs.has_value())) {
resetSelection();
}
offset = ImGui::GetCursorScreenPos() + ImVec2(vm.scrolling.x, vm.scrolling.y);

offset = ImVec2(vm.scrolling.x, vm.scrolling.y);

static char view_name[256] = "";
strcpy(view_name, vm.active_file.c_str());
Expand Down
60 changes: 16 additions & 44 deletions symmetri/gui/load_file.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
#include "load_file.h"

#include <algorithm>
#include <map>
#include <numeric>
#include <ranges>
#include <vector>

#include "petri.h"
#include "position_parsers.h"
#include "reducers.h"
#include "rxdispatch.h"
#include "symmetri/parsers.h"
#include "symmetri/symmetri.h"

template <typename T>
inline void append(std::vector<T> source, std::vector<T>& destination) {
if (destination.empty())
destination = std::move(source);
else
destination.insert(std::end(destination),
std::make_move_iterator(std::begin(source)),
std::make_move_iterator(std::end(source)));
}

void loadPetriNet(const std::filesystem::path& file) {
rxdispatch::push([=](model::Model&& model) {
auto& m = *model.data;
Expand Down Expand Up @@ -50,9 +43,12 @@ void loadPetriNet(const std::filesystem::path& file) {

new_net.priority = symmetri::createPriorityLookup(new_net.transition, pt);

m.t_positions.reserve(m.t_positions.size() + new_net.transition.size());
for (const auto& transition : new_net.transition) {
m.t_positions.push_back(positions.at(transition));
}

m.p_positions.reserve(m.p_positions.size() + new_net.place.size());
for (const auto& place : new_net.place) {
m.p_positions.push_back(positions.at(place));
}
Expand All @@ -79,14 +75,14 @@ void loadPetriNet(const std::filesystem::path& file) {
// For GCC, we copy the priority table because of some weird GCC13/14 bug...
// should eventually just become: append(std::move(new_net.priority),
// m.net.priority); ...
m.net.priority.insert(m.net.priority.end(), new_net.priority.begin(),
new_net.priority.end());

append(std::move(new_net.transition), m.net.transition);
append(std::move(new_net.place), m.net.place);
append(std::move(new_net.output_n), m.net.output_n);
append(std::move(new_net.input_n), m.net.input_n);
append(std::move(new_net.store), m.net.store);
// m.net.priority.insert(m.net.priority.end(), new_net.priority.begin(),
// new_net.priority.end());
std::ranges::move(new_net.priority, std::back_inserter(m.net.priority));
std::ranges::move(new_net.transition, std::back_inserter(m.net.transition));
std::ranges::move(new_net.place, std::back_inserter(m.net.place));
std::ranges::move(new_net.output_n, std::back_inserter(m.net.output_n));
std::ranges::move(new_net.input_n, std::back_inserter(m.net.input_n));
std::ranges::move(new_net.store, std::back_inserter(m.net.store));

m.net.p_to_ts_n = createReversePlaceToTransitionLookup(
m.net.place.size(), m.net.transition.size(), m.net.input_n);
Expand All @@ -97,40 +93,16 @@ void loadPetriNet(const std::filesystem::path& file) {
{symmetri::toIndex(new_net.place, p) + old_place_count, c});
}

m.t_view.clear();
m.p_view.clear();
m.t_view.resize(new_transition_count);
m.p_view.resize(new_place_count);

std::iota(m.t_view.begin(), m.t_view.end(), old_transition_count);
std::iota(m.p_view.begin(), m.p_view.end(), old_place_count);

float min_x = std::numeric_limits<float>::max();
float min_y = std::numeric_limits<float>::max();

for (auto&& idx : m.t_view) {
if (min_x > m.t_positions[idx].x) {
min_x = m.t_positions[idx].x;
}
if (min_y > m.t_positions[idx].y) {
min_y = m.t_positions[idx].y;
}
}

for (auto&& idx : m.p_view) {
if (min_x > m.p_positions[idx].x) {
min_x = m.p_positions[idx].x;
}
if (min_y > m.p_positions[idx].y) {
min_y = m.p_positions[idx].y;
}
}

// maybe this can be improved to have a better initialization :-)
m.scrolling = {min_x, min_y + 50.f};

m.active_file = file;

resetNetView(); // convenient to view new graph

return model;
});
}
8 changes: 7 additions & 1 deletion symmetri/gui/menu_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ void draw_menu_bar(const model::ViewModel &vm) {
//...
ImGui::EndMenu();
}

if (ImGui::BeginMenu("View")) {
//...
if (ImGui::MenuItem("Reset view")) {
resetNetView();
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Window")) {
//...
ImGui::EndMenu();
Expand Down
26 changes: 26 additions & 0 deletions symmetri/gui/reducers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#include "reducers.h"

#include <algorithm>
#include <filesystem>
#include <ranges>
#include <tuple>
Expand Down Expand Up @@ -332,6 +333,31 @@ void resetSelectedTargetNode() {
});
}

void resetNetView() {
rxdispatch::push([=](model::Model&& model) {
auto& m = *model.data;
const auto getMinX = [&](const auto& in_view, const auto& pos) {
return std::ranges::min(in_view | std::views::transform([&](auto idx) {
return pos[idx].x;
}));
};
const auto getMinY = [&](const auto& in_view, const auto& pos) {
return std::ranges::min(in_view | std::views::transform([&](auto idx) {
return pos[idx].y;
}));
};

float min_x = std::min(getMinX(m.t_view, m.t_positions),
getMinX(m.p_view, m.p_positions));
float min_y = std::min(getMinY(m.t_view, m.t_positions),
getMinY(m.p_view, m.p_positions));

m.scrolling = {400 - min_x, 120 - min_y};

return model;
});
}

void tryFire(size_t transition_idx) {
rxdispatch::push([=](model::Model&& m) {
if (canFire(m.data->net.input_n[transition_idx], m.data->tokens)) {
Expand Down
2 changes: 2 additions & 0 deletions symmetri/gui/reducers.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void resetSelectedTargetNode();

void resetSelection();

void resetNetView();

void setSelectedArc(bool is_input, size_t idx, size_t sub_idx);

void renderNodeEntry(bool is_place, const std::string& name, size_t idx,
Expand Down
3 changes: 0 additions & 3 deletions symmetri/gui/rxdispatch.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <functional>
#include <iostream>
#include <variant>

#include "model.h"
Expand Down Expand Up @@ -33,9 +32,7 @@ struct VisitPackage {
inline auto get_events_observable() {
return rpp::source::create<Update>([](auto&& observer) {
Update f;
auto mainthreadid = std::this_thread::get_id();
while (not observer.is_disposed()) {
std::cout << "dispatch " << mainthreadid << std::endl;
getQueue().wait_dequeue(f);
observer.on_next(std::forward<Update>(f));
}
Expand Down

0 comments on commit 12b53eb

Please sign in to comment.