From e91abb536f208dae271a34ca10dd18dd68af9483 Mon Sep 17 00:00:00 2001 From: Tristan Walter Date: Wed, 4 Oct 2023 01:39:54 +0200 Subject: [PATCH] * dynamicGUI adaptations --- Application/src/commons | 2 +- Application/src/tracker/Alterface.cpp | 21 +-- Application/src/tracker/Alterface.h | 4 +- Application/src/tracker/VideoOpener.cpp | 14 +- Application/src/tracker/VideoOpenerScene.cpp | 14 +- Application/src/tracker/gui/ConvertScene.cpp | 6 +- Application/src/tracker/gui/DrawBlobView.cpp | 6 +- .../src/tracker/gui/DrawPreviewImage.cpp | 8 +- Application/src/tracker/gui/LoadingScene.h | 4 +- .../src/tracker/gui/SettingsDropdown.h | 12 +- Application/src/tracker/gui/SettingsScene.cpp | 164 +++++++++--------- Application/src/tracker/gui/SettingsScene.h | 4 +- Application/src/tracker/gui/StartingScene.cpp | 48 ++--- Application/src/tracker/gui/StartingScene.h | 5 +- Application/src/tracker/gui/gui.cpp | 8 +- Application/src/tracker/misc/RecentItems.cpp | 3 + Application/src/tracker/settings_layout.json | 8 +- 17 files changed, 167 insertions(+), 164 deletions(-) diff --git a/Application/src/commons b/Application/src/commons index 980a3de2d..920603c41 160000 --- a/Application/src/commons +++ b/Application/src/commons @@ -1 +1 @@ -Subproject commit 980a3de2d2d0a840724d3e991ad1ae7e10931d6c +Subproject commit 920603c41850f37cbfaabc6a37f04baf28a25297 diff --git a/Application/src/tracker/Alterface.cpp b/Application/src/tracker/Alterface.cpp index 2c2867fd5..258d7ec02 100644 --- a/Application/src/tracker/Alterface.cpp +++ b/Application/src/tracker/Alterface.cpp @@ -23,27 +23,28 @@ inline static sprite::Map _video_info = [](){ }(); Alterface::Alterface(dyn::Context&& context, std::function&& settings_update) - : context(std::move(context)), + : dynGUI{ + .path = "alter_layout.json", + .graph = nullptr, + .context = std::move(context) + }, settings(std::move(settings_update)) { } Alterface::~Alterface() { - context = {}; - state = {}; - - objects.clear(); + dynGUI.clear(); } void Alterface::draw(IMGUIBase& base, DrawStructure& g) { - dyn::update_layout("alter_layout.json", context, state, objects); + if(not dynGUI) { + dynGUI.graph = &g; + dynGUI.base = (Base*)&base; + } g.section("buttons", [&](auto&, Section* section) { section->set_scale(g.scale().reciprocal()); - for(auto &obj : objects) { - dyn::update_objects(g, obj, context, state); - g.wrap_object(*obj); - } + dynGUI.update(nullptr); }); settings.draw(base, g); diff --git a/Application/src/tracker/Alterface.h b/Application/src/tracker/Alterface.h index d09bf72f2..69c6cfb94 100644 --- a/Application/src/tracker/Alterface.h +++ b/Application/src/tracker/Alterface.h @@ -10,9 +10,7 @@ namespace gui { struct Alterface { Image::Ptr next; - dyn::Context context; - dyn::State state; - std::vector objects; + dyn::DynamicGUI dynGUI; SettingsDropdown settings; Alterface() = delete; diff --git a/Application/src/tracker/VideoOpener.cpp b/Application/src/tracker/VideoOpener.cpp index 012376b16..8d294aa09 100644 --- a/Application/src/tracker/VideoOpener.cpp +++ b/Application/src/tracker/VideoOpener.cpp @@ -109,15 +109,15 @@ VideoOpener::LabeledDropDown::LabeledDropDown(const std::string& name) items.push_back(Dropdown::TextItem(name, index++)); } _dropdown->set_items(items); - _dropdown->select_item(narrow_cast(_ref.get().enum_index()())); + _dropdown->select_item(Dropdown::RawIndex{narrow_cast(_ref.get().enum_index()())}); _dropdown->textfield()->set_text(_ref.get().valueString()); _dropdown->on_select([this](auto index, auto) { - if(index < 0) + if(not index.valid()) return; try { - _ref.get().set_value_from_string(_ref.get().enum_values()().at((size_t)index)); + _ref.get().set_value_from_string(_ref.get().enum_values()().at((size_t)index.value)); } catch(...) {} _dropdown->set_opened(false); @@ -125,7 +125,7 @@ VideoOpener::LabeledDropDown::LabeledDropDown(const std::string& name) } void VideoOpener::LabeledDropDown::update() { - _dropdown->select_item(narrow_cast(_ref.get().enum_index()())); + _dropdown->select_item(Dropdown::RawIndex{narrow_cast(_ref.get().enum_index()())}); } VideoOpener::VideoOpener() @@ -979,7 +979,7 @@ void VideoOpener::select_file(const file::Path &p) { } if(name == "output_prefix") { - ((Dropdown*)children.back().get())->on_select([dropdown = ((Dropdown*)children.back().get()), this](long_t, const Dropdown::TextItem & item) + ((Dropdown*)children.back().get())->on_select([dropdown = ((Dropdown*)children.back().get()), this](auto, const Dropdown::TextItem & item) { _output_prefix = item.search_name(); dropdown->set_opened(false); @@ -1001,13 +1001,13 @@ void VideoOpener::select_file(const file::Path &p) { }); if(_output_prefix.empty()) - ((Dropdown*)children.back().get())->select_item(-1); + ((Dropdown*)children.back().get())->select_item(Dropdown::RawIndex()); else { auto items = ((Dropdown*)children.back().get())->items(); auto N = items.size(); for(size_t i=0; iselect_item(narrow_cast(i)); + ((Dropdown*)children.back().get())->select_item(Dropdown::RawIndex{narrow_cast(i)}); break; } } diff --git a/Application/src/tracker/VideoOpenerScene.cpp b/Application/src/tracker/VideoOpenerScene.cpp index 7f54e4b17..271afdd2c 100644 --- a/Application/src/tracker/VideoOpenerScene.cpp +++ b/Application/src/tracker/VideoOpenerScene.cpp @@ -109,15 +109,15 @@ VideoOpener::LabeledDropDown::LabeledDropDown(const std::string& name) items.push_back(Dropdown::TextItem(name, index++)); } _dropdown->set_items(items); - _dropdown->select_item(narrow_cast(_ref.get().enum_index()())); + _dropdown->select_item(Dropdown::RawIndex{narrow_cast(_ref.get().enum_index()())}); _dropdown->textfield()->set_text(_ref.get().valueString()); _dropdown->on_select([this](auto index, auto) { - if(index < 0) + if(not index.valid()) return; try { - _ref.get().set_value_from_string(_ref.get().enum_values()().at((size_t)index)); + _ref.get().set_value_from_string(_ref.get().enum_values()().at((size_t)index.value)); } catch(...) {} _dropdown->set_opened(false); @@ -125,7 +125,7 @@ VideoOpener::LabeledDropDown::LabeledDropDown(const std::string& name) } void VideoOpener::LabeledDropDown::update() { - _dropdown->select_item(narrow_cast(_ref.get().enum_index()())); + _dropdown->select_item(Dropdown::RawIndex{narrow_cast(_ref.get().enum_index()())}); } VideoOpener::VideoOpener() @@ -979,7 +979,7 @@ void VideoOpener::select_file(const file::Path &p) { } if(name == "output_prefix") { - ((Dropdown*)children.back().get())->on_select([dropdown = ((Dropdown*)children.back().get()), this](long_t, const Dropdown::TextItem & item) + ((Dropdown*)children.back().get())->on_select([dropdown = ((Dropdown*)children.back().get()), this](auto, const Dropdown::TextItem & item) { _output_prefix = item.search_name(); dropdown->set_opened(false); @@ -1001,13 +1001,13 @@ void VideoOpener::select_file(const file::Path &p) { }); if(_output_prefix.empty()) - ((Dropdown*)children.back().get())->select_item(-1); + ((Dropdown*)children.back().get())->select_item(Dropdown::RawIndex{}); else { auto items = ((Dropdown*)children.back().get())->items(); auto N = items.size(); for(size_t i=0; iselect_item(narrow_cast(i)); + ((Dropdown*)children.back().get())->select_item(Dropdown::RawIndex{narrow_cast(i)}); break; } } diff --git a/Application/src/tracker/gui/ConvertScene.cpp b/Application/src/tracker/gui/ConvertScene.cpp index 0d58d42d0..c633598c5 100644 --- a/Application/src/tracker/gui/ConvertScene.cpp +++ b/Application/src/tracker/gui/ConvertScene.cpp @@ -152,7 +152,7 @@ _on_deactivate(on_deactivate) _video_info.set_do_print(false); fish.set_do_print(false); - menu.context.variables.emplace("fishes", new Variable([this](std::string) -> std::vector>&{ + menu.dynGUI.context.variables.emplace("fishes", new Variable([this](std::string) -> std::vector>&{ return _gui_objects; })); } @@ -204,9 +204,7 @@ void ConvertScene::deactivate() { _segmenter = nullptr; _object_blobs.clear(); _current_data = {}; - - menu.state = dyn::State{}; - menu.objects.clear(); + menu.dynGUI.clear(); if(_on_deactivate) _on_deactivate(*this); diff --git a/Application/src/tracker/gui/DrawBlobView.cpp b/Application/src/tracker/gui/DrawBlobView.cpp index 6d48f8061..f15961f2c 100644 --- a/Application/src/tracker/gui/DrawBlobView.cpp +++ b/Application/src/tracker/gui/DrawBlobView.cpp @@ -500,7 +500,7 @@ void draw_blob_view(const DisplayParameters& parm) GUI::set_redraw(); } }); - list->on_select([parm](long_t, auto& item) { + list->on_select([parm](auto, auto& item) { pv::bid clicked_blob_id { (uint32_t)int64_t(item.custom()) }; if(item.ID() == 0) /* SPLIT */ { auto copy = FAST_SETTING(manual_splits); @@ -640,7 +640,7 @@ void draw_blob_view(const DisplayParameters& parm) } void clicked_background(DrawStructure& base, GUICache& cache, const Vec2& pos, bool v, std::string key, Dropdown& settings_dropdown, Textfield& value_input) { - const std::string chosen = settings_dropdown.selected_id() > -1 ? settings_dropdown.items().at(settings_dropdown.selected_id()).name() : ""; + const std::string chosen = settings_dropdown.has_selection() ? settings_dropdown.selected_item().name() : ""; if (key.empty()) key = chosen; @@ -923,7 +923,7 @@ void draw_boundary_selection(DrawStructure& base, Base* window, GUICache& cache, "track_include", "recognition_shapes" }); - dropdown->on_select([&](long_t, const Dropdown::TextItem & item){ + dropdown->on_select([&](auto, const Dropdown::TextItem & item){ clicked_background(base, cache, Vec2(), true, item.name(), settings_dropdown, value_input); }); dropdown->textfield()->set_placeholder("append to..."); diff --git a/Application/src/tracker/gui/DrawPreviewImage.cpp b/Application/src/tracker/gui/DrawPreviewImage.cpp index 01ad86dfa..feb245c08 100644 --- a/Application/src/tracker/gui/DrawPreviewImage.cpp +++ b/Application/src/tracker/gui/DrawPreviewImage.cpp @@ -133,15 +133,15 @@ LabeledDropDown::LabeledDropDown(const std::string& name) items.push_back(Dropdown::TextItem(name, index++)); } _dropdown->set_items(items); - _dropdown->select_item(narrow_cast(_ref.get().enum_index()())); + _dropdown->select_item(Dropdown::RawIndex{narrow_cast(_ref.get().enum_index()())}); _dropdown->textfield()->set_text(_ref.get().valueString()); _dropdown->on_select([this](auto index, auto) { - if(index < 0) + if(not index.valid()) return; try { - _ref.get().set_value_from_string(_ref.get().enum_values()().at((size_t)index)); + _ref.get().set_value_from_string(_ref.get().enum_values()().at((size_t)index.value)); } catch(...) {} _dropdown->set_opened(false); @@ -149,7 +149,7 @@ LabeledDropDown::LabeledDropDown(const std::string& name) } void LabeledDropDown::update() { - _dropdown->select_item(narrow_cast(_ref.get().enum_index()())); + _dropdown->select_item(Dropdown::RawIndex{narrow_cast(_ref.get().enum_index()())}); } } diff --git a/Application/src/tracker/gui/LoadingScene.h b/Application/src/tracker/gui/LoadingScene.h index b5c14980e..50560243b 100644 --- a/Application/src/tracker/gui/LoadingScene.h +++ b/Application/src/tracker/gui/LoadingScene.h @@ -165,7 +165,7 @@ class LoadingScene : public Scene { _textfield = std::make_shared(Bounds(0, 0, _list->width(), 30)); //_textfield = std::make_shared - _textfield->on_select([this](long_t, const Dropdown::TextItem& item) { + _textfield->on_select([this](auto, const Dropdown::TextItem& item) { file::Path path; if (((std::string)item).empty()) { @@ -320,4 +320,4 @@ class LoadingScene : public Scene { void change_folder(const file::Path&); }; -} \ No newline at end of file +} diff --git a/Application/src/tracker/gui/SettingsDropdown.h b/Application/src/tracker/gui/SettingsDropdown.h index 5b07524b7..2961b96ad 100644 --- a/Application/src/tracker/gui/SettingsDropdown.h +++ b/Application/src/tracker/gui/SettingsDropdown.h @@ -17,16 +17,16 @@ struct SettingsDropdown { _settings_dropdown.set_origin(Vec2(0, 1)); _value_input.set_origin(Vec2(0, 1)); - _settings_dropdown.on_select([&](long_t index, const std::string& name) { - this->selected_setting(index, name, _value_input); + _settings_dropdown.on_select([&](auto index, const std::string& name) { + this->selected_setting(index.value, name, _value_input); }); _value_input.on_enter([this, on_enter = std::move(on_enter)](){ try { - auto key = _settings_dropdown.items().at(_settings_dropdown.selected_id()).name(); + auto key = _settings_dropdown.selected_item().name(); if(GlobalSettings::access_level(key) == AccessLevelType::PUBLIC) { GlobalSettings::get(key).get().set_value_from_string(_value_input.text()); if(GlobalSettings::get(key).is_type()) - this->selected_setting(_settings_dropdown.selected_id(), key, _value_input); + this->selected_setting(_settings_dropdown.selected_item().ID(), key, _value_input); if((std::string)key == "auto_apply" || (std::string)key == "auto_train") { SETTING(auto_train_on_startup) = false; @@ -41,11 +41,11 @@ struct SettingsDropdown { FormatError("User cannot write setting ", key," (",GlobalSettings::access_level(key).name(),")."); } catch(const std::logic_error&) { #ifndef NDEBUG - FormatExcept("Cannot set ",_settings_dropdown.items().at(_settings_dropdown.selected_id())," to value ",_value_input.text()," (invalid)."); + FormatExcept("Cannot set ",_settings_dropdown.selected_item()," to value ",_value_input.text()," (invalid)."); #endif } catch(const UtilsException&) { #ifndef NDEBUG - FormatExcept("Cannot set ",_settings_dropdown.items().at(_settings_dropdown.selected_id())," to value ",_value_input.text()," (invalid)."); + FormatExcept("Cannot set ",_settings_dropdown.selected_item()," to value ",_value_input.text()," (invalid)."); #endif } }); diff --git a/Application/src/tracker/gui/SettingsScene.cpp b/Application/src/tracker/gui/SettingsScene.cpp index 8a3f887e7..456c293ac 100644 --- a/Application/src/tracker/gui/SettingsScene.cpp +++ b/Application/src/tracker/gui/SettingsScene.cpp @@ -11,71 +11,7 @@ namespace gui { SettingsScene::SettingsScene(Base& window) : Scene(window, "settings-scene", [this](auto&, DrawStructure& graph){ _draw(graph); }), -_preview_image(std::make_shared()), -context ({ - .actions = { - { - "go-back", - [](Event){ - auto prev = SceneManager::getInstance().last_active(); - if(prev) - SceneManager::getInstance().set_active(prev); - print("Going back"); - } - }, - { - "convert", - [](Event){ - SceneManager::getInstance().set_active("convert-scene"); - } - }, - { "choose-source", - [](Event){ - print("choose-source"); - - } - }, - { "choose-target", - [](Event){ - print("choose-target"); - } - }, - { "choose-model", - [](Event){ - print("choose-detection"); - } - }, - { "choose-region", - [](Event){ - print("choose-region"); - } - }, - { "choose-settings", - [](Event){ - print("choose-settings"); - } - }, - { "toggle-background-subtraction", - [](Event){ - SETTING(track_background_subtraction) = not SETTING(track_background_subtraction).value(); - } - } - }, - .variables = { - { - "global", - std::unique_ptr(new dyn::Variable([](std::string) -> sprite::Map& { - return GlobalSettings::map(); - })) - }, - { - "settings_summary", - std::unique_ptr(new dyn::Variable([](std::string) -> std::string { - return std::string(GlobalSettings::map().toStr()); - })) - } - } -}) +_preview_image(std::make_shared()) { auto dpi = ((const IMGUIBase*)&window)->dpi_scale(); print(window.window_dimensions().mul(dpi), " and logo ", _preview_image->size()); @@ -112,17 +48,95 @@ void SettingsScene::activate() { //SceneManager::getInstance().set_active("convert-scene"); SceneManager::getInstance().set_active("settings-menu"); });*/ + + dyn::Modules::add(dyn::Modules::Module{ + ._name = "follow", + ._apply = [](size_t index, dyn::State& state, const Layout::Ptr& o) { + state.display_fns[index] = [o = o.get()](DrawStructure& g){ + o->set_pos(g.mouse_position() + Vec2(5)); + }; + } + }); } void SettingsScene::deactivate() { // Logic to clear or save state if needed //RecentItems::set_select_callback(nullptr); - state = dyn::State{}; - objects.clear(); + dynGUI.clear(); + dyn::Modules::remove("follow"); } void SettingsScene::_draw(DrawStructure& graph) { - dyn::update_layout("settings_layout.json", context, state, objects); + if(not dynGUI) + dynGUI = dyn::DynamicGUI{ + .path = "settings_layout.json", + .graph = &graph, + .base = nullptr, + .context = { + .actions = { + { + "go-back", + [](Event){ + auto prev = SceneManager::getInstance().last_active(); + if(prev) + SceneManager::getInstance().set_active(prev); + print("Going back"); + } + }, + { + "convert", + [](Event){ + SceneManager::getInstance().set_active("convert-scene"); + } + }, + { "choose-source", + [](Event){ + print("choose-source"); + + } + }, + { "choose-target", + [](Event){ + print("choose-target"); + } + }, + { "choose-model", + [](Event){ + print("choose-detection"); + } + }, + { "choose-region", + [](Event){ + print("choose-region"); + } + }, + { "choose-settings", + [](Event){ + print("choose-settings"); + } + }, + { "toggle-background-subtraction", + [](Event){ + SETTING(track_background_subtraction) = not SETTING(track_background_subtraction).value(); + } + } + }, + .variables = { + { + "global", + std::unique_ptr(new dyn::Variable([](std::string) -> sprite::Map& { + return GlobalSettings::map(); + })) + }, + { + "settings_summary", + std::unique_ptr(new dyn::Variable([](std::string) -> std::string { + return std::string(GlobalSettings::map().toStr()); + })) + } + } + } + }; //auto dpi = ((const IMGUIBase*)window())->dpi_scale(); auto max_w = window()->window_dimensions().width * 0.65; @@ -132,17 +146,9 @@ void SettingsScene::_draw(DrawStructure& graph) { graph.wrap_object(_main_layout); - std::vector _objs{objects.begin(), objects.end()}; - _objs.push_back(Layout::Ptr(_preview_image)); - _logo_title_layout->set_children(_objs); - //_logo_title_layout->set_policy(VerticalLayout::Policy::CENTER); - - for(auto &obj : objects) { - dyn::update_objects(graph, obj, context, state); - //graph.wrap_object(*obj); - } - - dyn::update_tooltips(graph, state); + dynGUI.update(_logo_title_layout.get(), [this](auto &objs){ + objs.push_back(Layout::Ptr(_preview_image)); + }); _buttons_and_items->auto_size(Margin{0,0}); _logo_title_layout->auto_size(Margin{0,0}); diff --git a/Application/src/tracker/gui/SettingsScene.h b/Application/src/tracker/gui/SettingsScene.h index 480d8e4cb..fa467f2bc 100644 --- a/Application/src/tracker/gui/SettingsScene.h +++ b/Application/src/tracker/gui/SettingsScene.h @@ -20,10 +20,8 @@ class SettingsScene : public Scene { // The HorizontalLayout for the two buttons and the image HorizontalLayout _main_layout; + dyn::DynamicGUI dynGUI; - dyn::Context context; - dyn::State state; - std::vector objects; public: SettingsScene(Base& window); diff --git a/Application/src/tracker/gui/StartingScene.cpp b/Application/src/tracker/gui/StartingScene.cpp index fdeeb6e86..00847875a 100644 --- a/Application/src/tracker/gui/StartingScene.cpp +++ b/Application/src/tracker/gui/StartingScene.cpp @@ -16,21 +16,14 @@ StartingScene::StartingScene(Base& window) _logo_image(std::make_shared(Image::Make(cv::imread(_image_path.str(), cv::IMREAD_UNCHANGED)))), _recent_items(std::make_shared>(Bounds(0, 10, 310, 500))), _video_file_button(std::make_shared