diff --git a/src/common/viz2d.cpp b/src/common/viz2d.cpp index 1d15e3ba2..8dfce74a6 100644 --- a/src/common/viz2d.cpp +++ b/src/common/viz2d.cpp @@ -612,22 +612,41 @@ bool Viz2D::isStretching() { return stretch_; } -nanogui::Window* Viz2D::makeWindow(int x, int y, const string &title) { - return form()->add_window(nanogui::Vector2i(x, y), title); +Viz2DWindow* Viz2D::makeWindow(int x, int y, const string &title) { + this->makeCurrent(); + auto* win = new kb::viz2d::Viz2DWindow(this, x, y, title); + this->form()->set_window(win); + return win; } nanogui::Label* Viz2D::makeGroup(const string &label) { return form()->add_group(label); } -nanogui::detail::FormWidget* Viz2D::makeFormVariable(const string &name, bool &v, const string &tooltip, bool visible) { +nanogui::detail::FormWidget* Viz2D::makeFormVariable(const string &name, bool &v, const string &tooltip, bool visible, bool enabled) { auto var = form()->add_variable(name, v); + var->set_enabled(enabled); var->set_visible(visible); if (!tooltip.empty()) var->set_tooltip(tooltip); return var; } +nanogui::ColorPicker* Viz2D::makeColorPicker(const string& label, nanogui::Color& color, const string& tooltip, std::function fn, bool visible, bool enabled) { + auto* colorPicker = form()->add_variable(label, color); + colorPicker->set_enabled(enabled); + colorPicker->set_visible(visible); + if (!tooltip.empty()) + colorPicker->set_tooltip(tooltip); + if(fn) + colorPicker->set_final_callback(fn); + + return colorPicker; +} + +nanogui::Button* Viz2D::makeButton(const string& caption, std::function fn) { + return this->form()->add_button(caption, fn); +} bool Viz2D::isAccelerated() { return cv::ocl::useOpenCL(); } diff --git a/src/common/viz2d.hpp b/src/common/viz2d.hpp index 61cf16ed6..20e455f66 100644 --- a/src/common/viz2d.hpp +++ b/src/common/viz2d.hpp @@ -134,11 +134,12 @@ public: bool display(); nanogui::FormHelper* form(); - nanogui::Window* makeWindow(int x, int y, const string& title); + Viz2DWindow* makeWindow(int x, int y, const string& title); nanogui::Label* makeGroup(const string& label); - nanogui::detail::FormWidget* makeFormVariable(const string &name, bool &v, const string &tooltip = "", bool visible = true); - template nanogui::detail::FormWidget* makeFormVariable(const string &name, T &v, const T &min, const T &max, bool spinnable, const string &unit, const string tooltip, bool visible = true) { + nanogui::detail::FormWidget* makeFormVariable(const string &name, bool &v, const string &tooltip = "", bool visible = true, bool enabled = true); + template nanogui::detail::FormWidget* makeFormVariable(const string &name, T &v, const T &min, const T &max, bool spinnable, const string &unit, const string tooltip, bool visible = true, bool enabled = true) { auto var = form()->add_variable(name, v); + var->set_enabled(enabled); var->set_visible(visible); var->set_spinnable(spinnable); var->set_min_value(min); @@ -149,6 +150,16 @@ public: var->set_tooltip(tooltip); return var; } + + nanogui::ColorPicker* makeColorPicker(const string& label, nanogui::Color& color, const string& tooltip = "", std::function fn = nullptr, bool visible = true, bool enabled = true); + template nanogui::ComboBox* makeComboBox(const string &label, T& e, const std::vector& items) { + auto* var = form()->add_variable("Mode", e, true); + var->set_items(items); + return var; + } + + + nanogui::Button* makeButton(const string& caption, std::function fn); private: virtual bool keyboard_event(int key, int scancode, int action, int modifiers) override; void setMousePosition(int x, int y); diff --git a/src/optflow/optflow-demo.cpp b/src/optflow/optflow-demo.cpp index ec5df82c3..7b8ee4eb8 100644 --- a/src/optflow/optflow-demo.cpp +++ b/src/optflow/optflow-demo.cpp @@ -260,17 +260,13 @@ kb::viz2d::Viz2DWindow* menuWindow; void setup_gui(cv::Ptr v2d, cv::Ptr v2dMenu) { - v2d->makeCurrent(); - effectWindow = new kb::viz2d::Viz2DWindow(v2d, 5, 30, "Effects"); - v2d->form()->set_window(effectWindow); - + effectWindow = v2d->makeWindow(5, 30, "Effects"); v2d->makeGroup("Foreground"); v2d->makeFormVariable("Scale", fg_scale, 0.1f, 4.0f, true, "", "Generate the foreground at this scale"); v2d->makeFormVariable("Loss", fg_loss, 0.1f, 99.9f, true, "%", "On every frame the foreground loses on brightness"); v2d->makeGroup("Background"); - v2d->form()->add_variable("Mode", background_mode, true)->set_items({"Grey", "Color", "Value", "None"}); - + v2d->makeComboBox("Mode",background_mode, {"Grey", "Color", "Value", "None"}); v2d->makeGroup("Points"); v2d->makeFormVariable("Max. Points", max_points, 10, 1000000, true, "", "The theoretical maximum number of points to track which is scaled by the density of detected points and therefor is usually much smaller"); v2d->makeFormVariable("Point Loss", point_loss, 0.0f, 100.0f, true, "%", "How many of the tracked points to lose intentionally"); @@ -278,9 +274,7 @@ void setup_gui(cv::Ptr v2d, cv::Ptr v2dMenu) v2d->makeGroup("Optical flow"); v2d->makeFormVariable("Max. Stroke Size", max_stroke, 1, 100, true, "px", "The theoretical maximum size of the drawing stroke which is scaled by the area of the convex hull of tracked points and therefor is usually much smaller"); - auto* color = v2d->form()->add_variable("Color", effect_color); - color->set_tooltip("The primary effect color"); - color->set_final_callback([](const nanogui::Color &c) { + v2d->makeColorPicker("Color", effect_color, "The primary effect color",[](const nanogui::Color &c) { effect_color[0] = c[0]; effect_color[1] = c[1]; effect_color[2] = c[2]; @@ -293,10 +287,8 @@ void setup_gui(cv::Ptr v2d, cv::Ptr v2dMenu) kernel_size = std::max(int(k % 2 == 0 ? k + 1 : k), 1); }); - auto* thresh = v2d->makeFormVariable("Threshold", bloom_thresh, 1, 255, true, "", "The lightness selection threshold"); - auto* gain = v2d->makeFormVariable("Gain", bloom_gain, 0.1f, 20.0f, true, "", "Intensity of the effect defined by gain"); - thresh->set_enabled(false); - gain->set_enabled(false); + auto* thresh = v2d->makeFormVariable("Threshold", bloom_thresh, 1, 255, true, "", "The lightness selection threshold", true, false); + auto* gain = v2d->makeFormVariable("Gain", bloom_gain, 0.1f, 20.0f, true, "", "Intensity of the effect defined by gain", true, false); enableBloom->set_callback([&,thresh, gain](const bool& b) { if(b) { thresh->set_enabled(true); @@ -307,9 +299,8 @@ void setup_gui(cv::Ptr v2d, cv::Ptr v2dMenu) } use_bloom = b; }); - settingsWindow = new kb::viz2d::Viz2DWindow(v2d, 220, 320, "Settings"); - v2d->form()->set_window(settingsWindow); + settingsWindow = v2d->makeWindow(220, 320, "Settings"); v2d->makeGroup("Hardware Acceleration"); v2d->makeFormVariable("Enable", use_acceleration, "Enable or disable libva and OpenCL acceleration"); @@ -317,22 +308,19 @@ void setup_gui(cv::Ptr v2d, cv::Ptr v2dMenu) v2d->makeFormVariable("Threshold", scene_change_thresh, 0.1f, 1.0f, true, "", "Peak threshold. Lowering it makes detection more sensitive"); v2d->makeFormVariable("Threshold Diff", scene_change_thresh_diff, 0.1f, 1.0f, true, "", "Difference of peak thresholds. Lowering it makes detection more sensitive"); - v2dMenu->makeCurrent(); - menuWindow = new kb::viz2d::Viz2DWindow(v2dMenu, 0, 0, "Display"); - v2dMenu->form()->set_window(menuWindow); - + menuWindow = v2dMenu->makeWindow(0, 0, "Display"); v2dMenu->makeGroup("Display"); v2dMenu->makeFormVariable("Show FPS", show_fps, "Enable or disable the On-screen FPS display"); - v2dMenu->makeFormVariable("Stetch", stretch, "Stretch the frame buffer to the window size")->set_callback([=](const bool& s) { + v2dMenu->makeFormVariable("Stetch", stretch, "Stretch the frame buffer to the window size")->set_callback([=](const bool &s) { v2d->setStretching(s); }); - v2dMenu->form()->add_button("Fullscreen", [=]() { + v2dMenu->makeButton("Fullscreen", [=]() { v2d->setFullscreen(!v2d->isFullscreen()); }); - v2dMenu->form()->add_button("Offscreen", [=]() { - v2d->setOffscreen(!v2d->isOffscreen()); + v2dMenu->makeButton("Offscreen", [=]() { + v2d->setOffscreen(!v2d->isOffscreen()); }); menuWindow->center();