From 581b63e30cf3656ed0b6b3603330d1e1481dd260 Mon Sep 17 00:00:00 2001 From: Tristan Walter <twalter@orn.mpg.de> Date: Wed, 28 Oct 2020 13:58:25 +0100 Subject: [PATCH] * search according to z_index * fixing issue #4 * adding Dropdown and Textfields to Opening page --- .../src/commons/common/gui/DrawStructure.cpp | 13 +- .../src/commons/common/gui/Section.cpp | 6 +- Application/src/commons/common/gui/Section.h | 2 +- .../src/commons/common/gui/types/Drawable.cpp | 12 +- .../src/commons/common/gui/types/Drawable.h | 2 +- .../src/commons/common/video/GenericVideo.cpp | 2 +- Application/src/tracker/VideoOpener.cpp | 115 ++++++++++-------- Application/src/tracker/VideoOpener.h | 35 +++++- 8 files changed, 115 insertions(+), 72 deletions(-) diff --git a/Application/src/commons/common/gui/DrawStructure.cpp b/Application/src/commons/common/gui/DrawStructure.cpp index 8f8b0ab..e90dbc3 100644 --- a/Application/src/commons/common/gui/DrawStructure.cpp +++ b/Application/src/commons/common/gui/DrawStructure.cpp @@ -483,7 +483,18 @@ namespace gui { Drawable* DrawStructure::find(float x, float y) { _root.update_bounds(); - return _root.find(x, y); + std::vector<Drawable*> results; + _root.find(x, y, results); + + int64_t Z = -1; + Drawable* found = nullptr; + for(auto ptr : results) { + if(!found || ptr->z_index() > Z) { + Z = ptr->z_index(); + found = ptr; + } + } + return found; } Drawable* DrawStructure::mouse_move(float x, float y) { diff --git a/Application/src/commons/common/gui/Section.cpp b/Application/src/commons/common/gui/Section.cpp index 8c9ac96..9b6b789 100644 --- a/Application/src/commons/common/gui/Section.cpp +++ b/Application/src/commons/common/gui/Section.cpp @@ -333,11 +333,11 @@ namespace gui { SectionInterface::update_bounds(); } - Drawable* Section::find(float x, float y) { + void Section::find(float x, float y, std::vector<Drawable*>& results) { if(!enabled()) - return NULL; + return; - return SectionInterface::find(x, y); + SectionInterface::find(x, y, results); } void Section::clear() { diff --git a/Application/src/commons/common/gui/Section.h b/Application/src/commons/common/gui/Section.h index 67970aa..422b535 100644 --- a/Application/src/commons/common/gui/Section.h +++ b/Application/src/commons/common/gui/Section.h @@ -151,7 +151,7 @@ namespace gui { void wrap_object(Drawable* d); void end(); - Drawable* find(float x, float y) override; + void find(float x, float y, std::vector<Drawable*>&) override; private: void reuse_current_object(); diff --git a/Application/src/commons/common/gui/types/Drawable.cpp b/Application/src/commons/common/gui/types/Drawable.cpp index 51f9f24..b381a22 100644 --- a/Application/src/commons/common/gui/types/Drawable.cpp +++ b/Application/src/commons/common/gui/types/Drawable.cpp @@ -913,7 +913,7 @@ void SectionInterface::set_z_index(int index) { } } - Drawable* SectionInterface::find(float x, float y) { + void SectionInterface::find(float x, float y, std::vector<Drawable*>& results) { for(auto it = children().rbegin(); it != children().rend(); ++it) { auto ptr = *it; @@ -924,19 +924,17 @@ void SectionInterface::set_z_index(int index) { if(ptr->clickable()) { if(ptr->type() == Type::SECTION || ptr->type() == Type::ENTANGLED) { - auto found = static_cast<SectionInterface*>(ptr)->find(x, y); - if(found) - return found; + static_cast<SectionInterface*>(ptr)->find(x, y, results); } else if(ptr->in_bounds(x, y)) - return ptr; + results.push_back(ptr); } } if(!_clickable || !global_bounds().contains(x, y)) - return NULL; + return; - return this; + results.push_back(this); } Drawable* SectionInterface::find(const std::string& search) { diff --git a/Application/src/commons/common/gui/types/Drawable.h b/Application/src/commons/common/gui/types/Drawable.h index a331b37..0c89e86 100644 --- a/Application/src/commons/common/gui/types/Drawable.h +++ b/Application/src/commons/common/gui/types/Drawable.h @@ -364,7 +364,7 @@ namespace gui { virtual std::vector<Drawable*>& children() = 0; - virtual Drawable* find(float x, float y); + virtual void find(float x, float y, std::vector<Drawable*>& results); virtual Drawable* find(const std::string& search); virtual void set_stage(DrawStructure*); diff --git a/Application/src/commons/common/video/GenericVideo.cpp b/Application/src/commons/common/video/GenericVideo.cpp index 9eff5e6..0bce830 100644 --- a/Application/src/commons/common/video/GenericVideo.cpp +++ b/Application/src/commons/common/video/GenericVideo.cpp @@ -28,7 +28,7 @@ void GenericVideo::undistort(const gpuMat& disp, gpuMat &image) const { if(map2.empty()) GlobalSettings::get("cam_undistort2").value<cv::Mat>().copyTo(map2); - if(map1.cols == disp.cols && map1.rows == disp.cols && map2.cols == disp.cols && map2.rows == disp.rows) + if(map1.cols == disp.cols && map1.rows == disp.rows && map2.cols == disp.cols && map2.rows == disp.rows) { if(!map1.empty() && !map2.empty()) { Debug("Undistorting %dx%d", disp.cols, disp.rows); diff --git a/Application/src/tracker/VideoOpener.cpp b/Application/src/tracker/VideoOpener.cpp index c7cbbd7..5f732c4 100644 --- a/Application/src/tracker/VideoOpener.cpp +++ b/Application/src/tracker/VideoOpener.cpp @@ -15,6 +15,53 @@ namespace gui { GlobalSettings::docs_map_t temp_docs; sprite::Map temp_settings; +VideoOpener::LabeledTextField::LabeledTextField(const std::string& name) + : LabeledField(name), + _text_field(std::make_shared<gui::Textfield>("", Bounds(0, 0, 300, 33))), + _ref(gui::temp_settings[name]) +{ + _text_field->set_placeholder(name); + + _text_field->set_text(_ref.get().valueString()); + _text_field->on_text_changed([this](){ + try { + _ref.get().set_value_from_string(_text_field->text()); + + } catch(...) {} + }); +} + +void VideoOpener::LabeledTextField::update() { + _text_field->set_text(_ref.get().valueString()); +} + +VideoOpener::LabeledDropDown::LabeledDropDown(const std::string& name) + : LabeledField(name), + _dropdown(std::make_shared<gui::Dropdown>(Bounds(0, 0, 300, 33))), + _ref(gui::temp_settings[name]) +{ + assert(_ref.get().is_enum()); + std::vector<Dropdown::TextItem> items; + int index = 0; + for(auto &name : _ref.get().enum_values()()) { + items.push_back(Dropdown::TextItem(name, index++)); + } + _dropdown->set_items(items); + _dropdown->select_item(_ref.get().enum_index()()); + _dropdown->textfield()->set_text(_ref.get().valueString()); + + _dropdown->on_select([this](auto index, auto) { + try { + _ref.get().set_value_from_string(_ref.get().enum_values()().at(index)); + } catch(...) {} + _dropdown->set_opened(false); + }); +} + +void VideoOpener::LabeledDropDown::update() { + _dropdown->select_item(_ref.get().enum_index()()); +} + VideoOpener::VideoOpener() { grab::default_config::get(temp_settings, temp_docs, nullptr); //::default_config::get(GlobalSettings::map(), temp_docs, nullptr); @@ -37,62 +84,26 @@ VideoOpener::VideoOpener() { _raw_info->set_policy(gui::VerticalLayout::LEFT); _screenshot = std::make_shared<gui::ExternalImage>(); _text_fields.clear(); - - _text_fields["output_name"] = LabeledField("output name"); - _text_fields["output_name"]._text_field->set_text(TEMP_SETTING(output_name).get().valueString()); - _text_fields["output_name"]._text_field->on_text_changed([this](){ - file::Path number = _text_fields["output_name"]._text_field->text(); - TEMP_SETTING(output_name) = number; - }); - _text_fields["threshold"] = LabeledField("threshold"); - _text_fields["threshold"]._text_field->set_text(TEMP_SETTING(threshold).get().valueString()); - _text_fields["threshold"]._text_field->on_text_changed([this](){ - try { - auto number = Meta::fromStr<int>(_text_fields["threshold"]._text_field->text()); - TEMP_SETTING(threshold) = number; - - if(_buffer) { - _buffer->_threshold = number; - } - - } catch(...) {} - }); - - _text_fields["average_samples"] = LabeledField("average_samples"); - _text_fields["average_samples"]._text_field->set_text(TEMP_SETTING(average_samples).get().valueString()); - _text_fields["average_samples"]._text_field->on_text_changed([this](){ - try { - auto number = Meta::fromStr<int>(_text_fields["average_samples"]._text_field->text()); - TEMP_SETTING(average_samples) = number; + gui::temp_settings.register_callback((void*)this, [this](auto&map, auto&key, auto&value){ + if(key == "threshold") { + if(_buffer) + _buffer->_threshold = value.template value<int>(); - if(_buffer) { - _buffer->restart_background(); - } - - } catch(...) {} - }); - - _text_fields["averaging_method"] = LabeledField("averaging_method"); - _text_fields["averaging_method"]._text_field->set_text(TEMP_SETTING(averaging_method).get().valueString()); - _text_fields["averaging_method"]._text_field->on_text_changed([this]() { - try { - auto number = Meta::fromStr<averaging_method_t::Class>(_text_fields["averaging_method"]._text_field->text()); - TEMP_SETTING(averaging_method) = number; - - if (_buffer) { + } else if(key == "average_samples" || key == "averaging_method") { + if(_buffer) _buffer->restart_background(); - } - } - catch (...) {} }); + + _text_fields["output_name"] = std::make_unique<LabeledTextField>("output_name"); + _text_fields["threshold"] = std::make_unique<LabeledTextField>("threshold"); + _text_fields["average_samples"] = std::make_unique<LabeledTextField>("average_samples"); + _text_fields["averaging_method"] = std::make_unique<LabeledDropDown>("averaging_method"); std::vector<Layout::Ptr> objects{}; - for(auto &[key, ptr] : _text_fields) { - objects.push_back(ptr._text); - objects.push_back(ptr._text_field); - } + for(auto &[key, ptr] : _text_fields) + ptr->add_to(objects); _raw_settings->set_children(objects); @@ -417,7 +428,7 @@ void VideoOpener::select_file(const file::Path &p) { } else Warning("Given empty filename, the program will default to using input basename '%S'.", &filename.str()); - _text_fields["output_name"]._text_field->set_text(filename.str()); + _text_fields["output_name"]->update(); } _buffer = std::make_unique<BufferedVideo>(p); @@ -426,9 +437,7 @@ void VideoOpener::select_file(const file::Path &p) { _screenshot_previous_size = 0; try { - auto number = _text_fields["threshold"]._text_field->text(); - if(!number.empty()) - _buffer->_threshold = Meta::fromStr<uint32_t>(number); + _buffer->_threshold = TEMP_SETTING(threshold).value<int>(); } catch(const std::exception &e) { Except("Converting number: '%s'", e.what()); diff --git a/Application/src/tracker/VideoOpener.h b/Application/src/tracker/VideoOpener.h index 1f6c2cb..a6360d7 100644 --- a/Application/src/tracker/VideoOpener.h +++ b/Application/src/tracker/VideoOpener.h @@ -6,6 +6,7 @@ #include <gui/types/Checkbox.h> #include <file/Path.h> #include <video/VideoSource.h> +#include <gui/types/Dropdown.h> namespace gui { @@ -76,20 +77,44 @@ public: struct LabeledField { gui::derived_ptr<gui::Text> _text; - gui::derived_ptr<gui::Textfield> _text_field; //gui::derived_ptr<gui::HorizontalLayout> _joint; LabeledField(const std::string& name = "") - : _text(std::make_shared<gui::Text>(name)), - _text_field(std::make_shared<gui::Textfield>("", Bounds(0, 0, 300, 33))) + : _text(std::make_shared<gui::Text>(name)) //_joint(std::make_shared<gui::HorizontalLayout>(std::vector<Layout::Ptr>{_text, _text_field})) { _text->set_font(Font(0.75, Style::Bold)); _text->set_color(White); - _text_field->set_placeholder(name); } + + virtual ~LabeledField() {} + + virtual void add_to(std::vector<Layout::Ptr>& v) { + v.push_back(_text); + } + virtual void update() {} + }; + struct LabeledTextField : public LabeledField { + gui::derived_ptr<gui::Textfield> _text_field; + sprite::Reference _ref; + LabeledTextField(const std::string& name = ""); + void add_to(std::vector<Layout::Ptr>& v) override { + LabeledField::add_to(v); + v.push_back(_text_field); + } + void update() override; + }; + struct LabeledDropDown : public LabeledField { + gui::derived_ptr<gui::Dropdown> _dropdown; + sprite::Reference _ref; + LabeledDropDown(const std::string& name = ""); + void add_to(std::vector<Layout::Ptr>& v) override { + LabeledField::add_to(v); + v.push_back(_dropdown); + } + void update() override; }; - std::map<std::string, LabeledField> _text_fields; + std::map<std::string, std::unique_ptr<LabeledField>> _text_fields; gui::Checkbox *_load_results_checkbox = nullptr; std::string _output_prefix; -- GitLab