KolibriLib
Loading...
Searching...
No Matches
UI.hpp
1#ifndef __UI_HPP__
2#define __UI_HPP__
3
4#include <sys/ksys.h>
5
6#include <vector>
7
8#include "base.hpp"
9#include "windowBase.hpp"
10#include "graphic.hpp"
11
12namespace KolibriLib
13{
14 // Элементы UI
15 namespace UI
16 {
18 const unsigned DefaultMargin = 4;
19
23 {
24 protected:
25
28
31
33 unsigned _Margin;
34
35 public:
36 UIElement(KolibriLib::point coord = {0,0}, KolibriLib::point size = {16, 16}, unsigned Margin = DefaultMargin)
37 {
38 _coord = coord;
39 _size = size;
40 _Margin = Margin;
41 }
42 };
43
44 //=============================================================================================================================================================
45
50 inline void DrawBar(KolibriLib::point coord, KolibriLib::point size, ksys_color_t color = OS::sys_color_table.work_graph)
51 {
52 _ksys_draw_bar(coord.x * KolibriLib::AAANUMBER, coord.y * KolibriLib::AAANUMBER, size.x, size.y, color);
53 }
54
55 namespace text
56 {
59 unsigned int GetTextSize()
60 {
61 unsigned Size;
62 asm_inline(
63 "int $0x40"
64 : "=c"(Size)
65 : "a"(48), "b"(11)
66 );
67 return Size;
68 }
69
72 void SetTextSize(unsigned int newSize)
73 {
74 asm_inline(
75 "int $0x40" ::"a"(48), "b"(12), "c"(newSize)
76 );
77 }
78
79
84 inline void DrawText(std::string text, KolibriLib::point coord, unsigned size = 9, ksys_color_t color = OS::sys_color_table.work_text)
85 {
86 SetTextSize(size);
87 _ksys_draw_text(text.c_str(), coord.x, coord.y, text.length(), color);
88 }
89
94 inline void DrawText(const char *text, KolibriLib::point coord, unsigned size = 9, ksys_color_t color = OS::sys_color_table.work_text)
95 {
96 SetTextSize(size);
97 _ksys_draw_text(text, coord.x, coord.y, strlen(text), color);
98 }
99
100
101
103 class TextLabel: public UIElement
104 {
105 private:
107 std::string _text;
108
110 unsigned _FontSize;
111
113 bool _TextScale;
114 public:
122 TextLabel(point coord = {0, 0}, point size = {0, 0}, std::string text = "Text", unsigned FontSize = 9, bool TextScale = true, unsigned Margin = 0);
123 ~TextLabel();
124
126 void render();
127
130 std::string GetText();
131
134 unsigned GetFontSize();
135 };
136
137 TextLabel::TextLabel(point coord, point size, std::string text, unsigned FontSize, bool TextScale, unsigned Margin) : UIElement(coord, size, Margin)
138 {
139 _text = text;
140 _FontSize = FontSize;
141 _TextScale = TextScale;
142 }
143
144 TextLabel::~TextLabel()
145 {
146
147 }
148
150 {
151 if(_TextScale)
152 {
153 _FontSize = _size.x / _text.length();
154 }
155 SetTextSize(_FontSize);
156 DrawText(_text, _coord);
157 }
158
159 std::string TextLabel::GetText()
160 {
161 return _text;
162 }
164 {
165 return _FontSize;
166 }
167 }
168
169 //=============================================================================================================================================================
170
171 // Работа с кнопками
172 namespace buttons
173 {
174 // Коды кнопок начинаются с этого числа
175 const unsigned StartButtonId = 100;
176
179 {
180 unsigned data;
181 bool use = false;
182 };
183
185 std::vector<ButtonsIdData> ButtonsIdList;
186
190 unsigned GetFreeButtonId()
191 {
192 for (unsigned i = 0; i < ButtonsIdList.size(); i++) // Проходим по всему массиву
193 { // Если встречается свободный элемент,
194 if (!ButtonsIdList[i].use) // То используем его
195 { // Иначе создаём новый и использем тоже новый
196 ButtonsIdList[i].use = true;
197 return i;
198
199 }
200 }
201 ButtonsIdData a;
202 a.data = StartButtonId + ButtonsIdList.size();
203 a.use = true;
204 ButtonsIdList.push_back(a);
205 return ButtonsIdList.size() - 1; //-1 потому что обращение к элементам массива идёт с 0
206 }
207
210 void FreeButtonId(unsigned id)
211 {
212 ButtonsIdList[id].use = false; // Этот элемент теперь не используется
213 }
214
219 unsigned GetButtonId(unsigned id)
220 {
221 return ButtonsIdList[id].data;
222 }
223
229 inline unsigned autoDefineButton(point coords, point size, ksys_color_t color = OS::sys_color_table.work_button)
230 {
231 unsigned id = GetButtonId(GetFreeButtonId()); // Автоматически получаем id для кнопки
232 _ksys_define_button(coords.x, coords.y, size.x, size.y, id, color);
233 return id;
234 }
235
241 inline void DefineButton(point coord, point size, unsigned id, ksys_color_t color = OS::sys_color_table.work_button)
242 {
243 _ksys_define_button(coord.x, coord.y, size.x, size.y, id, color);
244 }
245
248 inline void DeleteButton(unsigned id)
249 {
250 _ksys_delete_button(id);
251 FreeButtonId(id); // Кнопка удалена, теперь этот id не использется
252 }
253
256 inline unsigned GetPressedButton()
257 {
258 return _ksys_get_button();
259 }
260
261
262
264 class Button : public UIElement
265 {
266 private:
268 std::string _text;
269
271 ksys_color_t _BackgroundColor;
272
274 ksys_color_t _TextColor;
275
277 unsigned _id;
278
280 bool _status;
281
284 bool _active;
285 public:
293 Button(point coord = {0, 0}, point size = {20, 20}, std::string text = "Button", unsigned Margin = DefaultMargin, ksys_color_t BackgroundColor = OS::sys_color_table.work_button, ksys_color_t TextColor = OS::sys_color_table.work_text);
294
302 void init(point coord, point size, std::string text, unsigned Margin = DefaultMargin, ksys_color_t BackgroundColor = OS::sys_color_table.work_button, ksys_color_t TextColor = OS::sys_color_table.work_text);
303
305 void render();
306
310 bool Handler();
311
314 bool GetStatus();
315
318 unsigned GetId();
319
323 void Deactivate();
324
327 void Activate();
328
329 ~Button();
330 };
331
332 Button::Button(point coord, point size, std::string text, unsigned Margin, ksys_color_t BackgroundColor, ksys_color_t TextColor) : UIElement(coord, size, Margin)
333 {
334 init(coord, size, text, Margin, BackgroundColor, TextColor);
335 }
336
338 {
339 if(_active)
340 {
341 DeleteButton(_id);
342 _active = false;
343 }
344 }
345
346 inline void Button::Activate()
347 {
348 if(!_active)
349 {
350 _id = GetFreeButtonId();
351 _active = true;
352 }
353 }
354
355 Button::~Button()
356 {
357 DeleteButton(_id);
358 }
359
361 {
362 if(_active)
363 {
364 DefineButton(_coord, _size, GetButtonId(_id), _BackgroundColor);
365 }
366 else
367 {
368 graphic::DrawRectangleLines(_coord, {_coord.x + _size.x, _coord.y + _size.y});
369 }
370 unsigned buff = text::GetTextSize();
371 text::SetTextSize(_size.y - 2 * _Margin);
372 text::DrawText(_text, _coord, _TextColor);
373 text::SetTextSize(buff); // Возвращение размера текста обратно
374 }
375
377 {
378 if (GetPressedButton() == _id)
379 {
380 _status = true;
381 }
382 else
383 {
384 _status = false;
385 }
386 return _status;
387 }
388
390 {
391 return _status;
392 }
393
394 void UI::buttons::Button::init(point coord, point size, std::string text, unsigned Margin, ksys_color_t BackgroundColor, ksys_color_t TextColor)
395 {
396 _coord = coord;
397 _size = size;
398 _Margin = Margin;
399 _text = text;
400 _BackgroundColor = BackgroundColor;
401 _TextColor = TextColor;
402 if (!_active) //Если кнопка была неактивна, то нужно её активировать
403 {
404 Activate();
405 }
406 else
407 {
408 _id = GetFreeButtonId();
409 }
410
411 render();
412 }
413
414 unsigned Button::GetId()
415 {
416 return _id;
417 }
418
419
420 }
421
422 //=============================================================================================================================================================
423
425 class Form : public UIElement
426 {
427 private:
428 std::string _BackgroundText;
429
431 std::string _inputText;
432
434 ksys_color_t _FormColor;
435 ksys_color_t _BackgroundTextColor;
437 bool _active;
438
439 public:
447 Form(point coord = {0,0}, KolibriLib::point size = {32, 16}, std::string BackgroundText = " ", ksys_color_t FormColor = OS::sys_color_table.work_text, ksys_color_t BackgroundTextColor = OS::sys_color_table.work_button_text, unsigned Margin = DefaultMargin);
448
450 void render();
452 void Handler();
453
456 std::string GetInput();
457 ~Form();
458 };
459
460 Form::Form(KolibriLib::point coord, KolibriLib::point size, std::string BackgroundText, ksys_color_t FormColor, ksys_color_t BackgroundTextColor, unsigned Margin) : UIElement(coord, size, Margin)
461 {
462 _BackgroundText = BackgroundText;
463 _FormColor = FormColor;
464 _BackgroundTextColor = BackgroundTextColor;
465 _active = false;
466 _butt.init(coord, size, " ", 0);
467 render();
468 }
469
470 Form::~Form()
471 {
472 }
473
475 {
476
477 KolibriLib::graphic::DrawRectangleLines(_coord, {_coord.x + _size.x, _coord.y + _size.y});
478 text::SetTextSize(_size.y - (2 * _Margin)); // Подгон размера текста под размеры формы
479 if (_inputText == "")
480 {
481 text::DrawText(
482 _BackgroundText,
484 _BackgroundTextColor);
485 }
486
487 _butt.render();
488 }
489
490 std::string Form::GetInput()
491 {
492 return _inputText;
493 }
494
496 {
497
498 if (_butt.Handler())
499 {
500 _active = true;
501 }
502 else
503 {
504 _active = false;
505 }
506 }
507 }
508}
509
510#endif // __UI_H__
Форма для ввода текста
Definition UI.hpp:426
void render()
Отрисовать форму
Definition UI.hpp:474
Form(point coord={0, 0}, KolibriLib::point size={32, 16}, std::string BackgroundText=" ", ksys_color_t FormColor=OS::sys_color_table.work_text, ksys_color_t BackgroundTextColor=OS::sys_color_table.work_button_text, unsigned Margin=DefaultMargin)
конструктор
Definition UI.hpp:460
void Handler()
Обработчик
Definition UI.hpp:495
std::string GetInput()
Получить введённый текст
Definition UI.hpp:490
Элемент интерфейса
Definition UI.hpp:23
unsigned _Margin
Отступы
Definition UI.hpp:33
KolibriLib::point _coord
Координаты
Definition UI.hpp:27
KolibriLib::point _size
Размер
Definition UI.hpp:30
Класс для работы с кнопками
Definition UI.hpp:265
bool Handler()
Обработчик кнопки
Definition UI.hpp:376
Button(point coord={0, 0}, point size={20, 20}, std::string text="Button", unsigned Margin=DefaultMargin, ksys_color_t BackgroundColor=OS::sys_color_table.work_button, ksys_color_t TextColor=OS::sys_color_table.work_text)
Это конструктор
Definition UI.hpp:332
void Deactivate()
Деактивировать кнопку
Definition UI.hpp:337
bool GetStatus()
Получить сосояние кнопки на момент последней обработки
Definition UI.hpp:389
void Activate()
Активировать кнопку
Definition UI.hpp:346
void render()
Отрисовать кнопку
Definition UI.hpp:360
unsigned GetId()
Получить номер кнопки
Definition UI.hpp:414
void init(point coord, point size, std::string text, unsigned Margin=DefaultMargin, ksys_color_t BackgroundColor=OS::sys_color_table.work_button, ksys_color_t TextColor=OS::sys_color_table.work_text)
инициализировать параметры
Definition UI.hpp:394
Класс для работы с текстом
Definition UI.hpp:104
unsigned GetFontSize()
Получить Размер шрифта
Definition UI.hpp:163
void render()
Отрисовать текст
Definition UI.hpp:149
TextLabel(point coord={0, 0}, point size={0, 0}, std::string text="Text", unsigned FontSize=9, bool TextScale=true, unsigned Margin=0)
Конструктор
Definition UI.hpp:137
std::string GetText()
Получить текст
Definition UI.hpp:159
ksys_colors_table_t sys_color_table
Таблица стандартных(системных) цветов
Definition base.hpp:37
Основное пространство имён
Definition base.hpp:17
void init()
инициализация
Definition base.hpp:112
Служебная структура, нигде не использется кроме ButtonsIdList.
Definition UI.hpp:179
Просто точка
Definition base.hpp:22