implement window minimizing

pull/3471/head
kallaballa 2 years ago
parent ee2647cfdc
commit 15271a85b5
  1. 45
      src/common/viz2d.cpp
  2. 15
      src/common/viz2d.hpp

@ -30,12 +30,12 @@ cv::Scalar color_convert(const cv::Scalar& src, cv::ColorConversionCodes code) {
cv::Scalar dst(vdst[0],vdst[1],vdst[2], src[3]); cv::Scalar dst(vdst[0],vdst[1],vdst[2], src[3]);
return dst; return dst;
} }
std::function<bool(Viz2DWindow*, Viz2DWindow*)> Viz2DWindow::viz2DWin_Xcomparator([](Viz2DWindow* lhs, Viz2DWindow* rhs){ return lhs->position()[0] < rhs->position()[0]; });
std::set<Viz2DWindow*> Viz2DWindow::all_windows_; std::set<Viz2DWindow*, decltype(Viz2DWindow::viz2DWin_Xcomparator)> Viz2DWindow::all_windows_xsorted_(viz2DWin_Xcomparator);
Viz2DWindow::Viz2DWindow(nanogui::Screen *screen, int x, int y, const string &title) : Viz2DWindow::Viz2DWindow(nanogui::Screen *screen, int x, int y, const string &title) :
Window(screen, title), screen_(screen), lastPos_(x, y) { Window(screen, title), screen_(screen), lastDragPos_(x, y) {
all_windows_.insert(this); all_windows_xsorted_.insert(this);
oldLayout_ = new nanogui::AdvancedGridLayout( { 10, 0, 10, 0 }, { }); oldLayout_ = new nanogui::AdvancedGridLayout( { 10, 0, 10, 0 }, { });
oldLayout_->set_margin(10); oldLayout_->set_margin(10);
oldLayout_->set_col_stretch(2, 1); oldLayout_->set_col_stretch(2, 1);
@ -58,7 +58,9 @@ Viz2DWindow::Viz2DWindow(nanogui::Screen *screen, int x, int y, const string &ti
} }
this->set_layout(oldLayout_); this->set_layout(oldLayout_);
this->set_position(maximizedPos_);
this->screen_->perform_layout(); this->screen_->perform_layout();
this->minimized_ = false;
}); });
minBtn_->set_callback([&, this]() { minBtn_->set_callback([&, this]() {
@ -71,16 +73,42 @@ Viz2DWindow::Viz2DWindow(nanogui::Screen *screen, int x, int y, const string &ti
this->set_size( { 0, 0 }); this->set_size( { 0, 0 });
this->set_layout(newLayout_); this->set_layout(newLayout_);
this->screen_->perform_layout(); this->screen_->perform_layout();
int gap = 0;
int x = 0;
int w = width();
int lastX = 0;
this->maximizedPos_ = this->position();
for (Viz2DWindow* win : all_windows_xsorted_) {
if(win != this && win->isMinimized()) {
x = win->position()[0];
gap = lastX + x;
if(gap >= w) {
this->set_position({lastX, screen_->height() - this->height()});
break;
}
lastX = x + win->width();
}
}
if(gap < w) {
this->set_position({lastX, screen_->height() - this->height()});
}
this->minimized_ = true;
}); });
} }
Viz2DWindow::~Viz2DWindow() { Viz2DWindow::~Viz2DWindow() {
all_windows_.erase(this); all_windows_xsorted_.erase(this);
}
bool Viz2DWindow::isMinimized() {
return minimized_;
} }
bool Viz2DWindow::mouse_drag_event(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int mods) { bool Viz2DWindow::mouse_drag_event(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int mods) {
if (m_drag && (button & (1 << GLFW_MOUSE_BUTTON_1)) != 0) { if (m_drag && (button & (1 << GLFW_MOUSE_BUTTON_1)) != 0) {
for (auto *win : all_windows_) { if(maxBtn_->visible()) {
for (auto *win : all_windows_xsorted_) {
if (win != this) { if (win != this) {
if (win->contains(this->position()) if (win->contains(this->position())
|| win->contains( { this->position()[0] + this->size()[0], this->position()[1] + this->size()[1] }) || win->contains( { this->position()[0] + this->size()[0], this->position()[1] + this->size()[1] })
@ -90,12 +118,13 @@ bool Viz2DWindow::mouse_drag_event(const nanogui::Vector2i &p, const nanogui::Ve
|| this->contains( { win->position()[0] + win->size()[0], win->position()[1] + win->size()[1] }) || this->contains( { win->position()[0] + win->size()[0], win->position()[1] + win->size()[1] })
|| this->contains( { win->position()[0], win->position()[1] + win->size()[1] }) || this->contains( { win->position()[0], win->position()[1] + win->size()[1] })
|| this->contains( { win->position()[0] + win->size()[0], win->position()[1] })) { || this->contains( { win->position()[0] + win->size()[0], win->position()[1] })) {
this->set_position(lastPos_); this->set_position(lastDragPos_);
return true; return true;
} }
} }
} }
lastPos_ = m_pos; }
lastDragPos_ = m_pos;
bool result = nanogui::Window::mouse_drag_event(p, rel, button, mods); bool result = nanogui::Window::mouse_drag_event(p, rel, button, mods);
return result; return result;

@ -19,6 +19,7 @@ namespace detail {
class CLGLContext; class CLGLContext;
class CLVAContext; class CLVAContext;
class NanoVGContext; class NanoVGContext;
void gl_check_error(const std::filesystem::path &file, unsigned int line, const char *expression); void gl_check_error(const std::filesystem::path &file, unsigned int line, const char *expression);
#define GL_CHECK(expr) \ #define GL_CHECK(expr) \
@ -32,26 +33,29 @@ cv::Scalar color_convert(const cv::Scalar& src, cv::ColorConversionCodes code);
using namespace kb::viz2d::detail; using namespace kb::viz2d::detail;
class Viz2DWindow : public nanogui::Window { class Viz2DWindow : public nanogui::Window {
private: private:
static std::set<Viz2DWindow*> all_windows_; static std::function<bool(Viz2DWindow*, Viz2DWindow*)> viz2DWin_Xcomparator;
static std::set<Viz2DWindow*, decltype(viz2DWin_Xcomparator)> all_windows_xsorted_;
nanogui::Screen* screen_; nanogui::Screen* screen_;
nanogui::Vector2i lastPos_; nanogui::Vector2i lastDragPos_;
nanogui::Vector2i maximizedPos_;
nanogui::Button* minBtn_; nanogui::Button* minBtn_;
nanogui::Button* maxBtn_; nanogui::Button* maxBtn_;
nanogui::ref<nanogui::AdvancedGridLayout> oldLayout_; nanogui::ref<nanogui::AdvancedGridLayout> oldLayout_;
nanogui::ref<nanogui::AdvancedGridLayout> newLayout_; nanogui::ref<nanogui::AdvancedGridLayout> newLayout_;
bool minimized_ = false;
public: public:
Viz2DWindow(nanogui::Screen* screen, int x, int y, const string& title); Viz2DWindow(nanogui::Screen* screen, int x, int y, const string& title);
virtual ~Viz2DWindow(); virtual ~Viz2DWindow();
bool isMinimized();
bool mouse_drag_event(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int mods) override; bool mouse_drag_event(const nanogui::Vector2i &p, const nanogui::Vector2i &rel, int button, int mods) override;
}; };
class NVG; class NVG;
class Viz2D: public nanogui::Screen { class Viz2D: public nanogui::Screen {
friend class NanoVGContext;
const cv::Size initialSize_; const cv::Size initialSize_;
cv::Size frameBufferSize_; cv::Size frameBufferSize_;
bool offscreen_; bool offscreen_;
@ -130,8 +134,8 @@ public:
} }
void setAccelerated(bool u); void setAccelerated(bool u);
NVGcontext* getNVGcontext();
private: private:
virtual bool keyboard_event(int key, int scancode, int action, int modifiers); virtual bool keyboard_event(int key, int scancode, int action, int modifiers);
CLGLContext& clgl(); CLGLContext& clgl();
@ -140,6 +144,7 @@ private:
nanogui::Screen& screen(); nanogui::Screen& screen();
void makeGLFWContextCurrent(); void makeGLFWContextCurrent();
GLFWwindow* getGLFWWindow(); GLFWwindow* getGLFWWindow();
NVGcontext* getNVGcontext();
}; };
} }
} /* namespace kb */ } /* namespace kb */

Loading…
Cancel
Save