KolibriLib
Loading...
Searching...
No Matches
button.hpp
1#ifndef __BUTTON_H__
2#define __BUTTON_H__
3
4#include <vector>
5#include <string>
6
7#include "UI.hpp"
8#include "text.hpp"
9#include "image.hpp"
10#include "color.hpp"
11
12namespace KolibriLib
13{
14 namespace UI
15 {
17 namespace buttons
18 {
19 typedef unsigned int ButtonID;
20
21 // Коды кнопок начинаются с этого числа
22 const ButtonID StartButtonId = 100;
23
26 {
27 bool use = false;
28 };
29
31 std::vector<ButtonsIdData> ButtonsIdList;
32
36 unsigned GetFreeButtonId()
37 {
38 for (unsigned i = 0; i < ButtonsIdList.size(); i++) // Проходим по всему массиву
39 { // Если встречается свободный элемент,
40 if (!ButtonsIdList[i].use) // То используем его
41 { // Иначе создаём новый и использем тоже новый
42 ButtonsIdList[i].use = true;
43 return i;
44 }
45 }
47 a.use = true;
48 ButtonsIdList.push_back(a);
49 return ButtonsIdList.size() - 1; //-1 потому что обращение к элементам массива идёт с 0
50 }
51
54 inline void FreeButtonId(unsigned id)
55 {
56 ButtonsIdList[id].use = false; // Этот элемент теперь не используется
57 }
58
63 inline ButtonID GetButtonId(unsigned id)
64 {
65 return StartButtonId + id;
66 }
67
68 void CutList()
69 {
70 unsigned a;
71 for(unsigned i = ButtonsIdList.size(); i < 0; i++ )
72 {
73 if(ButtonsIdList[i].use)
74 {
75 a = i;
76 }
77 else
78 {
79 break;
80 }
81 }
82 ButtonsIdList.erase(ButtonsIdList.begin() + a, ButtonsIdList.end());
83 }
84
90 inline unsigned autoDefineButton(const point<int> coords, const Size &size, ksys_color_t color = OS::sys_color_table.work_button)
91 {
92 unsigned id = GetButtonId(GetFreeButtonId()); // Автоматически получаем id для кнопки
93 _ksys_define_button(coords.x, coords.y, size.x, size.y, id, color);
94 return id;
95 }
96
102 inline void DefineButton(const point<int> &coord, const Size &size, const ButtonID &id, ksys_color_t color = OS::sys_color_table.work_button)
103 {
104 _ksys_define_button(coord.x, coord.y, size.x, size.y, id, color);
105 }
106
109 inline void DeleteButton(unsigned id)
110 {
111 _ksys_delete_button(id);
112 FreeButtonId(id); // Кнопка удалена, теперь этот id не использется
113 }
114
117 inline unsigned GetPressedButton()
118 {
119 return _ksys_get_button();
120 }
121
122 //=============================================================================================================================================================
123
124
126 class Button : public UIElement
127 {
128 private:
129 Images::image _img;
130 text::TextLabel _text;
131
133 ButtonID _id;
134
135 unsigned _type;
136
138 bool _status;
139
142 bool _active;
143
144 public:
145 enum Type
146 {
147 Image = 0,
148 Text
149 };
150
158 Button(const Coord &coord = {0, 0}, const Size &size = {20, 20}, unsigned Margin = UI::DefaultMargin, Color::Color ButtonColor = OS::sys_color_table.work_button);
159
167 void init(const Coord &coord = {0, 0}, const Size &size = {0, 0}, const std::string &text = "button", const unsigned &Margin = UI::DefaultMargin, const Color::Color &ButtonColor = OS::sys_color_table.work_button);
168
176 void init(const Coord &coord = {0, 0}, const Size &size = {0, 0}, const Images::image &image = Images::image(), const unsigned &Margin = DefaultMargin, const Color::Color &ButtonColor = OS::sys_color_table.work_button);
177
185 void init(const Coord &coord = {0, 0}, const Size &size = {0, 0}, std::string Path = DefaultImage);
186
188 void Render();
189
194 bool Handler();
195
198 bool GetStatus();
199
202 ButtonID GetId();
203
207 void Deactivate();
208
211 void Activate();
212
215 unsigned GetType();
216
219 std::string GetTextLabel();
220
224
227 void SetText(std::string NewText);
228
230 ~Button();
231
232 Button& operator = (const Button& butt)
233 {
234 _coord = butt._coord;
235 _size = butt._size;
236 _MainColor = butt._MainColor;
237 _Margin = butt._Margin;
238 _type = butt._type;
239 _text = butt._text;
240 _id = butt._id;
241 _active = butt._active;
242 _status = butt._status;
243 return *this;
244 }
245 };
246
247 Button::Button(const Coord &coord, const Size &size, unsigned Margin, Color::Color ButtonColor) : UIElement(coord, size, ButtonColor, Margin)
248 {
250 _img.SetCoord(coord);
251 _img.SetSize(size);
252 _text.SetCoord(coord);
253 _text.SetSize(size);
254 }
255
257 {
258 if (_active)
259 {
260 DeleteButton(_id);
261 _active = false;
262 }
263 }
264
266 {
267 if (!_active)
268 {
270 _active = true;
271 }
272 }
273
275 {
276 return _type;
277 }
278
280 {
281 if (_type == Type::Text)
282 {
283 return _text.GetText();
284 }
285 }
286
288 {
289 return _img;
290 }
291
292 void Button::SetText(std::string NewText)
293 {
294 if (_type == Type::Text)
295 {
296 return _text.SetText(NewText);
297 }
298 }
299
301 {
302 DeleteButton(_id);
303 }
304
306 {
307 if (GetPressedButton() == _id)
308 {
309 _status = true;
310 }
311 else
312 {
313 _status = false;
314 }
315 return _status;
316 }
317
319 {
320 return _status;
321 }
322
323 void UI::buttons::Button::init(const Coord &coord, const Size &size, const std::string &text, const unsigned &Margin, const Color::Color &ButtonColor)
324 {
325 _coord = coord;
326 _size = size;
327 _Margin = Margin;
328
329 _text.SetText(text);
330 _type = Type::Text;
331
332 _text.SetCoord(coord);
333 _text.SetSize(size);
334 _text.SetScale(true);
335
336 _MainColor = ButtonColor;
337
338 if (!_active) // Если кнопка была неактивна, то нужно её активировать
339 {
340 Activate();
341 }
342 }
343
344 void UI::buttons::Button::init(const Coord &coord, const Size &size, const Images::image &image, const unsigned &Margin, const Color::Color &ButtonColor)
345 {
346 _coord = coord;
347 _size = size;
348 _Margin = Margin;
349
350 _img = image;
351 _type = Type::Image;
352 _img.SetCoord(coord);
353 _img.SetSize(size);
354
355 _MainColor = ButtonColor;
356
357 if (!_active) // Если кнопка была неактивна, то нужно её активировать
358 {
359 Activate();
360 }
361 }
362
363 void UI::buttons::Button::init(const Coord &coord, const Size &size, std::string Path)
364 {
365 _coord = coord;
366 _size = size;
367
368 _img.LoadImage(Path);
369 _type = Type::Image;
370 _img.SetCoord(coord);
371 _img.SetSize(size);
372
373
374 if (!_active) // Если кнопка была неактивна, то нужно её активировать
375 {
376 Activate();
377 }
378 }
379
381 {
382 if (_active)
383 {
384 DefineButton(_coord, _size, _id, _MainColor);
385
386 switch (_type)
387 {
388 case Type::Image:
389 _img.Render();
390 break;
391 case Type::Text:
392 _text.Render();
393 break;
394 default:
395 break;
396 }
397 }
398 }
399
400 ButtonID Button::GetId()
401 {
402 return _id;
403 }
404 } // namespace buttons
405 } // namespace UI
406
407} // namespace KolibriLib
408
409
410#endif // __BUTTON_H__
Картинка как элемент интерфейса
Definition image.hpp:23
void Render(Size size={0, 0})
Вывести изображение в окно
Definition image.hpp:121
Элемент интерфейса
Definition UI.hpp:29
unsigned _Margin
Отступы
Definition UI.hpp:41
Size _size
Размер
Definition UI.hpp:36
Coord _coord
Координаты
Definition UI.hpp:33
Класс для работы с кнопками
Definition button.hpp:127
bool Handler()
Обработчик кнопки
Definition button.hpp:305
void Deactivate()
Деактивировать кнопку
Definition button.hpp:256
unsigned GetType()
Возвращает тип данных используемых в кнопке _type.
Definition button.hpp:274
bool GetStatus()
Получить сосояние кнопки на момент последней обработки
Definition button.hpp:318
void SetText(std::string NewText)
Изменить текст кнопки
Definition button.hpp:292
ButtonID GetId()
Получить номер кнопки
Definition button.hpp:400
void Render()
Отрисовать кнопку
Definition button.hpp:380
~Button()
Декструктор
Definition button.hpp:300
void Activate()
Активировать кнопку
Definition button.hpp:265
void init(const Coord &coord={0, 0}, const Size &size={0, 0}, const std::string &text="button", const unsigned &Margin=UI::DefaultMargin, const Color::Color &ButtonColor=OS::sys_color_table.work_button)
инициализировать параметры
Definition button.hpp:323
Images::image GetImage()
Получить изображение кнопки
Definition button.hpp:287
Button(const Coord &coord={0, 0}, const Size &size={20, 20}, unsigned Margin=UI::DefaultMargin, Color::Color ButtonColor=OS::sys_color_table.work_button)
Это конструктор
Definition button.hpp:247
std::string GetTextLabel()
Возвращает текст кнопки
Definition button.hpp:279
Текстовая метка
Definition text.hpp:66
void Render()
Отрисовать текстовую метку
Definition text.hpp:139
void SetText(const std::string &NewText)
Изменить текст
Definition text.hpp:166
std::string GetText()
Получить текст
Definition text.hpp:157
ksys_color_t Color
Цвет
Definition color.hpp:13
Color::ColorsTable sys_color_table
Таблица стандартных(системных) цветов
Definition os.hpp:15
unsigned GetFreeButtonId()
Получить свободный номер id кнопки из списка
Definition button.hpp:36
void FreeButtonId(unsigned id)
Освободить номер кнопки
Definition button.hpp:54
ButtonID GetButtonId(unsigned id)
Получить id кнопки
Definition button.hpp:63
unsigned autoDefineButton(const point< int > coords, const Size &size, ksys_color_t color=OS::sys_color_table.work_button)
Создать кнопку, автоматически присвоить ей id.
Definition button.hpp:90
std::vector< ButtonsIdData > ButtonsIdList
Список idшников кнопок
Definition button.hpp:31
unsigned GetPressedButton()
проверить какая кнопка нажата
Definition button.hpp:117
void DeleteButton(unsigned id)
Удалить кнопу
Definition button.hpp:109
void DefineButton(const point< int > &coord, const Size &size, const ButtonID &id, ksys_color_t color=OS::sys_color_table.work_button)
Создать кнопку, вручную
Definition button.hpp:102
Основное пространство имён
Definition base.hpp:18
void init()
инициализация
Definition base.hpp:21
Служебная структура, нигде не использется кроме ButtonsIdList.
Definition button.hpp:26
Просто точка
Definition small.hpp:23