5 #include<spdlog/spdlog.h>
6 #include<source_location>
7 #include<opencv2/highgui.hpp>
8 #include<opencv2/freetype.hpp>
9 #include<opencv2/imgproc.hpp>
12 #define EVENT_ENTER 8000
13 #define EVENT_LEAVE 8001
14 #define EVENT_KEYBOARD 8002
18 std::string source_loc(
const std::source_location &location = std::source_location::current());
28 virtual void focus(
bool);
29 virtual void event_callback(
int event,
int x,
int y);
31 virtual std::string type()
const {
return "Widget"; }
44 virtual bool is_window()
const {
return false;}
46 void zIndex(
int v) {
zIndex_ = v; }
47 void hidden(
bool v) {
hidden_ = v;}
49 bool hidden()
const {
return hidden_;}
52 float alpha()
const {
return alpha_;}
53 void alpha(
float f) {
alpha_ = f;}
54 void shade_rect(cv::Rect2i r,
int shade = 3, cv::Vec3b color = widget_color_,
55 cv::Vec3b upper_left = highlight_color_, cv::Vec3b lower_right = click_color_);
61 static const cv::Vec3b background_color_, widget_color_, highlight_color_, click_color_;
66 static cv::Ptr<cv::freetype::FreeType2>
ft2_;
74 void text(std::string s);
75 std::string
text()
const;
84 void click(std::function<
void()> f);
85 void text(std::string s);
86 void draw(cv::Vec3b color = widget_color_,
bool repaint =
true);
97 void on_change(std::function<
void(
bool)> f);
99 bool checked_ =
false;
101 void click(
int,
int);
109 std::string
value()
const;
110 void value(std::string s);
111 void enter(std::function<
void(std::string)> f);
113 std::string type()
const {
return "TextInput";}
115 std::string fore_, back_, editting_;
119 static HangulInputContext* hic_;
120 static HanjaTable* table_;
121 static bool hangul_mode_;
122 virtual bool on_overflow(std::string, std::string, std::string) {
return true;}
125 void flush(), del(), hanja(), backspace(), hangul();
126 void popup(std::vector<std::string> v);
127 const cv::Vec3b white = cv::Vec3b{255, 255, 255};
137 template<
class T>
class TwoD :
public std::vector<std::vector<T>>
140 void push_back(T a) {
144 std::vector<std::vector<T>>::push_back(std::move(tmp_));
150 template<
class T>
auto to_number(std::string n) {
152 if constexpr(std::is_same_v<int, T>)
return stoi(n);
153 else if constexpr(std::is_same_v<float, T>)
return stof(n);
154 else if constexpr(std::is_same_v<double, T>)
return stod(n);
167 void popup(
Window &w, std::function<
void(
int)> f = [](
int){});
169 std::string type()
const {
return "Window"; }
170 int open(
int flag = cv::WINDOW_AUTOSIZE,
int x = -1,
int y = -1);
178 std::vector<Widget*>::iterator begin(), end();
180 void start(
int flag = cv::WINDOW_AUTOSIZE | cv::WINDOW_KEEPRATIO);
181 void keyboard_callback(
int key,
int level = 0);
183 std::string
title()
const;
185 void tie(
TextInput &t,
Button &b,
const std::vector<std::string> &v,
int font_size);
190 b1.
text(
"\u25b2"); b2.
text(
"\u25bc");
192 *
this << b1; *
this << b2; *
this << t;
193 b1.
click([&, step](){t.
value(std::to_string(to_number<T>(t.
value()) + step)); *
this << t;
show();});
194 b2.
click([&, step](){t.
value(std::to_string(to_number<T>(t.
value()) - step)); *
this << t;
show();});
195 return [&t]() {
return to_number<T>(t.
value()); };
197 template<
class... T>
void tabs(
int xpos,
int ypos, T&... wins)
201 static TwoD<z::Window*> v;
202 static TwoD<std::shared_ptr<z::Button>> bts;
203 static std::vector<std::shared_ptr<z::Widget>> panels;
204 const int button_width = 100;
205 panels.push_back(std::make_shared<z::Widget>(cv::Rect2i{0,0,1,1}));
206 static std::vector<int> max_zindex;
207 int k =
sizeof...(wins);
208 (v.push_back(&wins), ...);
210 v.back().front()->zIndex(2);
211 panels.back()->zIndex(1);
212 max_zindex.push_back(2);
213 int sz = v.size(), shift = 0, max_w = 0, max_h = 0;
214 for(
auto *pw : v.back()) {
215 pw->scrolled_rect_ = {0,0,0,0};
216 pw->x = xpos; pw->y = ypos + 40;
217 if(pw->width > max_w) max_w = pw->width;
218 if(pw->height > max_h) max_h = pw->height;
219 auto p = std::make_shared<z::Button>(pw->title(),
220 cv::Rect2i{xpos + shift++ * button_width, ypos, button_width, 30});
223 p->click([
this, sz, pw]() {
224 int z = max_zindex[sz-1];
225 panels[sz-1]->zIndex(z+1);
227 max_zindex[sz - 1] = z + 2;
233 panels.back()->resize(cv::Rect2i{xpos, ypos + 40, max_w, max_h});
234 *
this + *panels.back();
240 bool is_window()
const {
return true;}
241 template<
class... T>
auto tie(T&... checks)
244 static std::vector<z::CheckBox*> v;
245 int k =
sizeof...(checks);
247 (v.push_back(&checks), ...);
248 for(
int i=sz; i < sz + k; i++) v[i]->on_change([i, k, sz,
this](
bool) {
249 for(
int j=sz; j < sz + k; j++) {
250 if(i != j) v[j]->checked(
false);
251 else v[j]->checked(
true);
255 return [sz, k, &v]() {
256 for(
int i=sz; i<sz+k; i++)
if(v[i]->checked())
return i - sz;
259 template<
class... T>
void wrap(
const char*
title,
int font,
int N,
const T&... widgets)
262 std::vector<int> xs, ys;
263 (xs.push_back(widgets.x), ...);
264 (xs.push_back(widgets.br().x), ...);
265 (ys.push_back(widgets.y), ...);
266 (ys.push_back(widgets.br().y), ...);
267 auto p = std::minmax_element(xs.begin(), xs.end());
268 auto q = std::minmax_element(ys.begin(), ys.end());
269 cv::Point2i ul = {*p.first -N, *q.first -N};
270 wrapped_.push_back({cv::Rect2i{ul, cv::Point{*p.second + N, *q.second + N}}, font,
title});
271 draw_wrapped(wrapped_.back());
273 cv::Rect2i scrolled_rect_ = cv::Rect2i{0,0,0,0};
274 std::vector<Widget*> widgets_, backup_;
277 void set_ul(cv::Point2i p);
278 void set_br(cv::Point2i p);
281 void draw_wrapped(
const Wrapped &wr);
282 bool closed_ =
false;
286 std::function<void(
int)> popup_exit_func_;
287 std::vector<Wrapped> wrapped_;
288 void copy_widget_to_mat(
const Widget &r);
293 class VHandle :
public Widget
297 const static int widget_width_ = 15;
303 bool mouse_down_ =
false;
306 void scroll_window(
int ypos);
307 int scroll_delta(
int yd);
311 class HHandle :
public Widget
315 const static int widget_height_ = 15;
321 bool mouse_down_ =
false;
324 int scroll_delta(
int xd);
325 void scroll_window(
int xpos);
334 static const int widget_size_ = 30;
337 void position_widgets();
340 bool mouse_down_ =
false;
342 cv::Rect2i scroll_backup_ = {0,0,0,0};
344 void routine(
int,
int);
348 std::string fore, editting, back;
349 bool new_line =
false;
350 std::string value()
const {
return fore + editting + back; }
353 class TextInput2 :
public TextInput
356 using iter = std::list<Line>::iterator;
357 TextInput2(cv::Rect2i r);
363 void set_iter(iter it);
364 TextInput2 *prev_ =
nullptr, *next_ =
nullptr;
365 std::list<Line> *contents_ptr_ =
nullptr;
366 bool end_new_line_ =
false;
370 void down_stream(iter it,
int level = 0);
371 void up_stream(iter it);
372 bool on_overflow(std::string fore, std::string editting, std::string back);
373 void up(), down(), new_line();
374 bool del(), backsp();
375 void keyboard_callback(
int,
int);
376 bool is_end()
const {
return contents_ptr_->end() == it_;}
377 friend class TextBox;
383 TextBox(cv::Rect2i r,
int lines);
384 void set_max_character(
int max);
385 std::string type()
const {
return "TextBox";}
386 std::string value()
const;
387 void value(std::string s);
389 int top_line_index_ = 0, focus_line_ = 0;
390 std::list<Line> contents_;
391 std::vector<std::shared_ptr<TextInput2>> inputs_;
409 Slider(cv::Rect2i r,
int start,
int stop,
int step);
412 void on_change(std::function<
void(
int)> f);
415 int value_, start_, end_, step_, logical_length_, physical_length_;
418 std::function<void(
int)> on_change_;
419 void key_event(
int key,
int);
420 void move(
int x,
int y);
421 void ldown(
int x,
int y);
422 void lup(
int x,
int y);
423 int to_pos(
int val), to_val(
int pos);
424 bool user_hold_ =
false;
440 AsciiWindow(
const char *asciiart,
int unit_width = 10,
int unit_height = 15,
442 void title(std::string t) { title_ = t; }
444 std::vector<std::shared_ptr<Slider>> S;
445 std::vector<std::shared_ptr<Button>> B;
446 std::vector<std::shared_ptr<TextInput>> T;
447 std::vector<std::shared_ptr<CheckBox>> C;
448 std::vector<std::shared_ptr<Label>> L;
449 std::vector<std::shared_ptr<Image>> I;
450 std::vector<std::shared_ptr<Progress>> P;
451 std::vector<std::shared_ptr<Widget>> Z;
452 std::vector<std::shared_ptr<TextBox>> E;
454 int get_size(
char c);
455 bool parse_widget_area(
int y,
int x);
457 void cjk_correction();
458 int uw_, uh_, margin_;
459 std::vector<std::string> art_, parsed_;
462 class PopupInterface {
464 PopupInterface(
Window *p);
465 int open(
int flag = cv::WINDOW_AUTOSIZE,
int x = -1,
int y = -1);
468 bool closed_ =
false;
471 Window *window_ptr_ =
nullptr;
474 class WButton :
public Widget
477 WButton(cv::Rect2i r);
AsciiWindow(const char *asciiart, int unit_width=10, int unit_height=15, int margin=1)
Definition: asciiwindow.cc:26
void on_change(std::function< void(bool)> f)
Definition: checkbox.cc:35
bool checked() const
Definition: checkbox.cc:23
CheckBox(cv::Rect2i r)
Definition: checkbox.cc:6
add Handle for Scrolled Window. If you add Handle widget to a window, it will become a scrolled Windo...
Definition: zgui.h:331
cv::Mat & operator=(const cv::Mat &r)
Definition: image.cc:8
Label(std::string text, cv::Rect2i r)
Definition: label.cc:5
std::string text() const
Definition: label.cc:10
void value(int val)
Definition: progress.cc:10
void draw()
Definition: slider.cc:22
int value()
Definition: slider.cc:58
Slider(cv::Rect2i r, int start, int stop, int step)
Definition: slider.cc:6
CJK font, libhangul needed.
Definition: zgui.h:106
bool draw()
Definition: textinput.cc:274
std::string value() const
Definition: textinput.cc:243
TextInput(cv::Rect2i r)
Definition: textinput.cc:91
void enter(std::function< void(std::string)> f)
Definition: textinput.cc:238
void key_event(int key, int)
Definition: textinput.cc:142
void show_cursor()
Definition: textinput.cc:176
void move_cursor(bool right)
Definition: textinput.cc:184
auto tie(T &... checks)
Definition: zgui.h:241
int open(int flag=cv::WINDOW_AUTOSIZE, int x=-1, int y=-1)
Definition: window.cc:258
Window(std::string title, cv::Rect_< int > r)
Definition: window.cc:38
void wrap(const char *title, int font, int N, const T &... widgets)
Definition: zgui.h:259
Window & operator>>(Widget &r)
Definition: window.cc:133
Window & operator+(Widget &w)
Definition: window.cc:57
int loop()
Definition: window.cc:236
std::string title() const
Definition: window.cc:92
void move_widget(Widget &w, cv::Point2i p)
Definition: window.cc:76
void tabs(int xpos, int ypos, T &... wins)
Definition: zgui.h:197
auto tie(TextInput &t, Button &b1, Button &b2, T start, T step)
Definition: zgui.h:186
void scroll_to(cv::Rect2i r)
Definition: scrolledwindow.cc:5
void draw_all_wrapped()
Definition: window.cc:152
void close()
Definition: window.cc:166
Window & operator<<(Widget &r)
Definition: window.cc:119
void on_register()
will be called when widget is added to a window
Definition: window.cc:279
void organize_accordingto_zindex()
Definition: window.cc:111
void start(int flag=cv::WINDOW_AUTOSIZE|cv::WINDOW_KEEPRATIO)
Definition: window.cc:228
void popdown(int value)
Definition: window.cc:204
Window & operator-(Widget &w)
Definition: window.cc:65
void show()
draw mat_
Definition: window.cc:157
void popup(Window &w, std::function< void(int)> f=[](int){})
Definition: window.cc:171