1 #include "MainToolBar.h"
2 #include "ui/FontEngine.h"
3 #include "ui/NanoVGHelpers.h"
4 #include "plugin/ColorPalette.h"
5 #include "Window.hpp"
6
MainToolBar(Widget * group,ColorPalette & palette)7 MainToolBar::MainToolBar(Widget *group, ColorPalette &palette)
8 : NanoWidget(group),
9 fPalette(palette)
10 {
11 }
12
addButton(int id,const char * label,const char * icon)13 void MainToolBar::addButton(int id, const char *label, const char *icon)
14 {
15 Item item;
16 item.id = id;
17 item.type = kTypeIcon;
18 item.label = label;
19 item.icon = icon;
20 fLayout.push_back(std::move(item));
21 updatePositionsAndSizes();
22 repaint();
23 }
24
setSelected(int id,bool sel)25 void MainToolBar::setSelected(int id, bool sel)
26 {
27 Item *item = nullptr;
28
29 for (size_t i = 0, n = fLayout.size(); i < n && !item; ++i) {
30 if (fLayout[i].id == id)
31 item = &fLayout[i];
32 }
33
34 if (item && item->selected != sel) {
35 item->selected = sel;
36 repaint();
37 }
38 }
39
getIdealWidth() const40 float MainToolBar::getIdealWidth() const
41 {
42 if (fLayout.empty())
43 return 0.0;
44
45 const RectF &rect = fItemRects.back();
46 return rect.x + rect.w + 2.0;
47 }
48
onNanoDisplay()49 void MainToolBar::onNanoDisplay()
50 {
51 const ColorPalette &cp = fPalette;
52 FontEngine fe(*this, cp);
53
54 const double w = getWidth();
55 const double h = getHeight();
56
57 beginPath();
58 roundedRectWithCorners(*this, RectF{0, 0, w, h}, 10.0, RectangleSE);
59 fillColor(Colors::fromRGBA8(cp[Colors::tool_bar_back]));
60 fill();
61
62 Font fontLabel;
63 fontLabel.name = "regular";
64 fontLabel.size = 12.0;
65 fontLabel.color = cp[Colors::text_normal];
66 Font fontIcons;
67 fontIcons.name = "awesome";
68 fontIcons.size = h - 1.5 * fontLabel.size;
69 fontIcons.color = cp[Colors::text_normal];
70
71 Font fontLabelSelected = fontLabel;
72 Font fontIconsSelected = fontIcons;
73 const ColorRGBA8 selectionColor = cp[Colors::text_active];
74 fontLabelSelected.color = selectionColor;
75 fontIconsSelected.color = selectionColor;
76
77 for (size_t i = 0, n = fLayout.size(); i < n; ++i) {
78 const Item &item = fLayout[i];
79 RectF rect = fItemRects[i];
80
81 switch (item.type) {
82 case kTypeIcon:
83 fe.drawInBox(item.icon.c_str(), !item.selected ? fontIcons : fontIconsSelected, rect, kAlignCenter|kAlignTop|kAlignInside);
84 fe.drawInBox(item.label.c_str(), !item.selected ? fontLabel : fontLabelSelected, rect, kAlignCenter|kAlignBottom|kAlignInside);
85 break;
86 }
87 }
88 }
89
onResize(const ResizeEvent & ev)90 void MainToolBar::onResize(const ResizeEvent &ev)
91 {
92 (void)ev;
93 updatePositionsAndSizes();
94 repaint();
95 }
96
onMouse(const MouseEvent & ev)97 bool MainToolBar::onMouse(const MouseEvent &ev)
98 {
99 if (ev.press && ev.button == 1) {
100 for (size_t i = 0, n = fLayout.size(); i < n; ++i) {
101 const Item &item = fLayout[i];
102 const RectF rect = fItemRects[i];
103
104 const int evX = ev.pos.getX();
105 const int evY = ev.pos.getY();
106
107 const bool inside = evX >= rect.x && evX < rect.x + rect.w &&
108 evY >= rect.y && evY < rect.y + rect.h;
109
110 switch (item.type) {
111 case kTypeIcon:
112 if (inside) {
113 if (fListener)
114 fListener->onToolBarItemClicked(item.id);
115 return true;
116 }
117 break;
118 }
119 }
120 }
121
122 return false;
123 }
124
updatePositionsAndSizes()125 void MainToolBar::updatePositionsAndSizes()
126 {
127 //const double w = getWidth();
128 const double h = getHeight();
129
130 fItemRects.clear();
131
132 double x = 0.0;
133 for (const Item &item : fLayout) {
134 RectF rect;
135 rect.x = x;
136 rect.y = 0.0;
137
138 switch (item.type) {
139 default:
140 case kTypeIcon:
141 rect.w = h;
142 rect.h = h;
143 break;
144 }
145
146 rect = rect.reduced(2.0);
147
148 x += rect.w;
149 fItemRects.push_back(rect);
150 }
151 }
152