-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
EnhancedWindow.h
146 lines (119 loc) · 3.43 KB
/
EnhancedWindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef _ENHANCED_WINDOW_H_
#define _ENHANCED_WINDOW_H_
/*
This class enhances the window component of cvui by making it movable and minimizable.
Authors:
ShengYu - https://github.com/shengyu7697
Amaury Bréhéret - https://github.com/abreheret
Contributions:
Fernando Bevilacqua <[email protected]>
Code licensed under the MIT license.
*/
class EnhancedWindow
{
private:
int mX;
int mY;
int mWidth;
int mHeight;
int mHeightNotMinimized;
cv::String mTitle;
int mDeltaY;
int mDeltaX;
bool mIsMoving;
bool mMinimized;
bool mMinimizable;
double mFontScale;
public:
EnhancedWindow(int x, int y, int width, int height, const cv::String& title, bool minimizable = true, double theFontScale = cvui::DEFAULT_FONT_SCALE):
mX(x),
mY(y),
mWidth(width),
mHeight(height),
mHeightNotMinimized(height),
mTitle(title),
mDeltaY(0),
mDeltaX(0),
mIsMoving(false),
mMinimized(false),
mMinimizable(minimizable),
mFontScale(theFontScale) {
}
void begin(cv::Mat &frame) {
int scaledTitleHeight = std::lround(20*mFontScale/cvui::DEFAULT_FONT_SCALE);
bool mouseInsideTitleArea = cvui::mouse().inside(cv::Rect(mX, mY, mWidth, scaledTitleHeight));
mHeight = mMinimized ? scaledTitleHeight : mHeightNotMinimized;
if (mIsMoving == false && cvui::mouse(cvui::DOWN) && mouseInsideTitleArea) {
mDeltaX = cvui::mouse().x - mX;
mDeltaY = cvui::mouse().y - mY;
mIsMoving = true;
} else if (mIsMoving && cvui::mouse(cvui::IS_DOWN)) {
mX = cvui::mouse().x - mDeltaX;
mY = cvui::mouse().y - mDeltaY;
} else {
mIsMoving = false;
mX = std::max(0, mX);
mY = std::max(0, mY);
mX = std::min(frame.cols - mWidth, mX);
mY = std::min(frame.rows - scaledTitleHeight, mY);
}
cvui::window(frame, mX, mY, mWidth, mHeight, mTitle, mFontScale);
if (mMinimizable && cvui::button(frame, mX + mWidth - scaledTitleHeight, mY + 1, scaledTitleHeight-1, scaledTitleHeight-1, mMinimized ? "+" : "-", mFontScale)) {
mMinimized = !mMinimized;
}
cvui::beginRow(frame, mX + std::lround(10*mFontScale/cvui::DEFAULT_FONT_SCALE), mY + std::lround(30*mFontScale/cvui::DEFAULT_FONT_SCALE), mWidth - scaledTitleHeight, mHeight - scaledTitleHeight);
cvui::beginColumn(mWidth - std::lround(10*mFontScale/cvui::DEFAULT_FONT_SCALE), mHeight - scaledTitleHeight);
}
/**
Use this function to get the maximum width of a child widget of EnhancedWindow.
\return the width of the EnhancedWindow without the inner borders that are automatically added
*/
int widthWithoutBorders() {
return mWidth - std::lround(20*mFontScale/cvui::DEFAULT_FONT_SCALE);
}
/**
Use this function to get the maximum height of a child widget of EnhancedWindow.
\return the height of the EnhancedWindow without title and inner borders that are automatically added
*/
int heightWithoutBorders() {
return mHeight - std::lround(40*mFontScale/cvui::DEFAULT_FONT_SCALE);
}
void end() {
cvui::endColumn();
cvui::endRow();
}
int posX() const {
return mX;
}
int posY() const {
return mY;
}
void setPosX(int posX) {
mX = posX;
}
void setPosY(int posY) {
mY = posY;
}
int width() const {
return mWidth;
}
int height() const {
return mHeight;
}
void setWidth(int w) {
mWidth = w;
}
void setHeight(int h) {
mHeight = mHeightNotMinimized = h;
}
double fontScale() const {
return mFontScale;
}
void setFontScale(double fontScale) {
mFontScale = fontScale;
}
bool isMinimized() const {
return mMinimized;
}
};
#endif // _ENHANCED_WINDOW_H_