2501
|
1 #pragma once
|
|
2 #include <list>
|
|
3 #include <algorithm>
|
|
4
|
|
5 class UIControl
|
|
6 {
|
|
7 public:
|
|
8 virtual void Show() = 0;
|
|
9
|
|
10 virtual bool Focused() = 0;
|
|
11
|
|
12 // Events
|
|
13 virtual bool OnKey(int key) {return DefaultOnKey(key);}
|
|
14 virtual bool OnMouseLeftClick(int x, int y) {return DefaultOnMouseLeftClick(x, y);}
|
|
15 virtual bool OnMouseRightClick(int x, int y) {return DefaultOnMouseRightClick(x, y);}
|
|
16 virtual bool OnMouseEnter() {return DefaultOnMouseEnter();}
|
|
17 virtual bool OnMouseLeave() {return DefaultOnMouseLeave();}
|
|
18
|
|
19 // Container
|
|
20 virtual bool AddControl(UIControl *ctrl)
|
|
21 {
|
|
22 if (std::find(children.begin(), children.end(), ctrl) == children.end())
|
|
23 {
|
|
24 children.push_back(ctrl);
|
|
25 ctrl->parent = this;
|
|
26 return true;
|
|
27 }
|
|
28 return false;
|
|
29 }
|
|
30
|
|
31 virtual bool RemoveControl(UIControl *ctrl)
|
|
32 {
|
|
33 auto i = std::find(children.begin(), children.end(), ctrl);
|
|
34
|
|
35 children.remove(ctrl);
|
|
36 if (i != children.end())
|
|
37 {
|
|
38 ctrl->parent = nullptr;
|
|
39 return true;
|
|
40 }
|
|
41 return false;
|
|
42 }
|
|
43
|
|
44 protected:
|
|
45 UIControl *parent;
|
|
46 std::list<UIControl *> children;
|
|
47
|
|
48
|
|
49 bool DefaultOnKey(int key)
|
|
50 {
|
|
51 for (auto i = children.begin(); i != children.end(); ++i)
|
|
52 if ((*i)->OnKey(key))
|
|
53 return true;
|
|
54 return false;
|
|
55 }
|
|
56
|
|
57 bool DefaultOnMouseLeftClick(int x, int y)
|
|
58 {
|
|
59 for (auto i = children.begin(); i != children.end(); ++i)
|
|
60 if ((*i)->OnMouseLeftClick(x, y))
|
|
61 return true;
|
|
62 return false;
|
|
63 }
|
|
64
|
|
65 bool DefaultOnMouseRightClick(int x, int y)
|
|
66 {
|
|
67 for (auto i = children.begin(); i != children.end(); ++i)
|
|
68 if ((*i)->OnMouseRightClick(x, y))
|
|
69 return true;
|
|
70 return false;
|
|
71 }
|
|
72
|
|
73 bool DefaultOnMouseEnter()
|
|
74 {
|
|
75 for (auto i = children.begin(); i != children.end(); ++i)
|
|
76 if ((*i)->OnMouseEnter())
|
|
77 return true;
|
|
78 return false;
|
|
79 }
|
|
80
|
|
81 bool DefaultOnMouseLeave()
|
|
82 {
|
|
83 for (auto i = children.begin(); i != children.end(); ++i)
|
|
84 if ((*i)->OnMouseLeave())
|
|
85 return true;
|
|
86 return false;
|
|
87 }
|
|
88 }; |