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
12//=============================================================================================================================================================
13
14namespace KolibriLib
15{
17 namespace UI
18 {
20 const unsigned DefaultMargin = 4;
21
25 {
26 protected:
27
30
33
35 unsigned _Margin;
36
37 public:
38 UIElement(point coord = {0,0}, KolibriLib::point size = {16, 16}, unsigned Margin = DefaultMargin)
39 {
40 _coord = coord;
41 _size = size;
42 _Margin = Margin;
43 }
44 };
45
46 //=============================================================================================================================================================
47
52 inline void DrawBar(point coord, KolibriLib::point size, ksys_color_t color = OS::sys_color_table.work_graph)
53 {
54 _ksys_draw_bar(coord.x, coord.y, size.x, size.y, color);
55 }
56
58 namespace text
59 {
62 unsigned int GetTextSize()
63 {
64 unsigned Size;
65 asm_inline(
66 "int $0x40"
67 : "=c"(Size)
68 : "a"(48), "b"(11)
69 );
70 return Size;
71 }
72
75 void SetTextSize(unsigned int newSize)
76 {
77 asm_inline(
78 "int $0x40"
79 ::"a"(48), "b"(12), "c"(newSize)
80 );
81 }
82
83
88 inline void DrawText(const std::string& text, const point& coord, const unsigned& size = 9, ksys_color_t color = OS::sys_color_table.work_text)
89 {
90 SetTextSize(size);
91 _ksys_draw_text(text.c_str(), coord.x, coord.y, text.length(), color);
92 }
93
98 inline void DrawText(const char *text, const point& coord, const unsigned& size = 9, ksys_color_t color = OS::sys_color_table.work_text)
99 {
100 SetTextSize(size);
101 _ksys_draw_text(text, coord.x, coord.y, strlen(text), color);
102 }
103
104 //=============================================================================================================================================================
105
107 class TextLabel: public UIElement
108 {
109 private:
111 std::string _text;
112
114 ksys_color_t _TextColor;
115
117 unsigned _FontSize;
118
120 bool _TextScale;
121
122 public:
130 TextLabel(point coord = {0, 0}, point size = {0, 0}, std::string text = "Text", unsigned FontSize = 9, bool TextScale = true, ksys_color_t TextColor = OS::sys_color_table.work_text, unsigned Margin = 0);
131
132 ~TextLabel();
133
135 void render();
136
139 std::string GetText();
140
143 unsigned GetFontSize();
144
147 ksys_color_t GetTextColor();
148
151 void SetText(const std::string& NewText);
152
155 void SetFontSize(const unsigned& NewTextSize);
156
159 void SetSize(const point& NewSize);
160
163 void SetCoord(const point& NewCoords);
164
167 void SetTextColor(const ksys_color_t& NewTextColor);
168
171 void SetScale(bool scale);
172
173 };
174
175 TextLabel::TextLabel(point coord, point size, std::string text, unsigned FontSize, bool TextScale, ksys_color_t TextColor, unsigned Margin) : UIElement(coord, size, Margin)
176 {
177 _text = text;
178 _FontSize = FontSize;
179 _TextScale = TextScale;
180 _TextColor = TextColor;
181 }
182
183 TextLabel::~TextLabel()
184 {
185
186 }
187
189 {
190 if(_TextScale) //Если текст нужно подстраивать размер, то
191 { //Постраиваем
192 _FontSize = _size.x / _text.length();
193 }
194 SetTextSize(_FontSize);
195
196 unsigned a = 0;
197
198 if((_text.length() * _FontSize) > _size.x) //Центрирование текста
199 {
200 a = (_size.x / 2) - (_text.length() * _FontSize);
201 }
202
203 DrawText(_text, {_coord.x + a, _coord.y + (_size.y / 2)}, _FontSize, _TextColor);
204 }
205
206 std::string TextLabel::GetText()
207 {
208 return _text;
209 }
211 {
212 return _FontSize;
213 }
214
215 void TextLabel::SetText(const std::string& NewText)
216 {
217 _text = NewText;
218 }
219 void TextLabel::SetFontSize(const unsigned& NewTextSize)
220 {
221 _FontSize = NewTextSize;
222 }
223
224 void TextLabel::SetSize(const point& NewSize)
225 {
226 _size = NewSize;
227 }
228
229 void TextLabel::SetCoord(const point& NewCoords)
230 {
231 _coord = NewCoords;
232 }
233
234 void TextLabel::SetTextColor(const ksys_color_t& NewTextColor)
235 {
236 _TextColor = NewTextColor;
237 }
238
239 void TextLabel::SetScale(bool scale)
240 {
241 _TextScale = scale;
242 }
243
245 {
246 return _TextColor;
247 }
248 }
249
250 //=============================================================================================================================================================
251
252 // Работа с кнопками
253 namespace buttons
254 {
255 // Коды кнопок начинаются с этого числа
256 const unsigned StartButtonId = 100;
257
260 {
261 bool use = false;
262 };
263
265 std::vector<ButtonsIdData> ButtonsIdList;
266
270 unsigned GetFreeButtonId()
271 {
272 for (unsigned i = 0; i < ButtonsIdList.size(); i++) // Проходим по всему массиву
273 { // Если встречается свободный элемент,
274 if (!ButtonsIdList[i].use) // То используем его
275 { // Иначе создаём новый и использем тоже новый
276 ButtonsIdList[i].use = true;
277 return i;
278
279 }
280 }
281 ButtonsIdData a;
282 a.use = true;
283 ButtonsIdList.push_back(a);
284 return ButtonsIdList.size() - 1; //-1 потому что обращение к элементам массива идёт с 0
285 }
286
289 void FreeButtonId(unsigned id)
290 {
291 ButtonsIdList[id].use = false; // Этот элемент теперь не используется
292 }
293
298 unsigned GetButtonId(unsigned id)
299 {
300 return StartButtonId + id;
301 }
302
308 inline unsigned autoDefineButton(const point& coords, const point& size, ksys_color_t color = OS::sys_color_table.work_button)
309 {
310 unsigned id = GetButtonId(GetFreeButtonId()); // Автоматически получаем id для кнопки
311 _ksys_define_button(coords.x, coords.y, size.x, size.y, id, color);
312 return id;
313 }
314
320 inline void DefineButton(const point& coord, point& size, const unsigned& id, ksys_color_t color = OS::sys_color_table.work_button)
321 {
322 _ksys_define_button(coord.x, coord.y, size.x, size.y, id, color);
323 }
324
327 inline void DeleteButton(unsigned id)
328 {
329 _ksys_delete_button(id);
330 FreeButtonId(id); // Кнопка удалена, теперь этот id не использется
331 }
332
335 inline unsigned GetPressedButton()
336 {
337 return _ksys_get_button();
338 }
339
340 //=============================================================================================================================================================
341
343 class Button : public UIElement
344 {
345 private:
347 text::TextLabel _text;
348
350 ksys_color_t _ButtonColor;
351
353 unsigned _id;
354
356 bool _status;
357
360 bool _active;
361
362 public:
370 Button(point coord = {0, 0}, point size = {20, 20}, std::string text = "Button", unsigned Margin = DefaultMargin, ksys_color_t ButtonColor = OS::sys_color_table.work_button, ksys_color_t TextColor = OS::sys_color_table.work_text);
371
379 void init(point coord = {0, 0}, point size = {0, 0}, std::string text = "Button", unsigned Margin = DefaultMargin, ksys_color_t ButtonColor = OS::sys_color_table.work_button, ksys_color_t TextColor = OS::sys_color_table.work_text);
380
382 void render();
383
387 bool Handler();
388
391 bool GetStatus();
392
395 unsigned GetId();
396
400 void Deactivate();
401
404 void Activate();
405
406 ~Button();
407 };
408
409 Button::Button(point coord, point size, std::string text, unsigned Margin, ksys_color_t ButtonColor, ksys_color_t TextColor) : UIElement(coord, size, Margin)
410 {
411 init(coord, size, text, Margin, ButtonColor, TextColor);
412 }
413
415 {
416 if(_active)
417 {
418 DeleteButton(_id);
419 _active = false;
420 }
421 }
422
423 inline void Button::Activate()
424 {
425 if(!_active)
426 {
427 _id = GetFreeButtonId();
428 _active = true;
429 }
430 }
431
432 Button::~Button()
433 {
434 DeleteButton(_id);
435 }
436
438 {
439 if(_active)
440 {
441 DefineButton(_coord, _size, GetButtonId(_id), _ButtonColor);
442 }
443 else
444 {
445 graphic::DrawRectangleLines(_coord, {_coord.x + _size.x, _coord.y + _size.y}, _ButtonColor);
446 }
447
448 _text.render();
449
450 }
451
453 {
454 if (GetPressedButton() == _id)
455 {
456 _status = true;
457 }
458 else
459 {
460 _status = false;
461 }
462 return _status;
463 }
464
466 {
467 return _status;
468 }
469
470 void UI::buttons::Button::init(point coord, point size, std::string text, unsigned Margin, ksys_color_t ButtonColor, ksys_color_t TextColor)
471 {
472 _coord = coord;
473 _size = size;
474 _Margin = Margin;
475 _text.SetText(text);
476 _text.SetCoord(coord);
477 _text.SetSize(size);
478 _text.SetTextColor(TextColor);
479 _ButtonColor = ButtonColor;
480
481 if (!_active) //Если кнопка была неактивна, то нужно её активировать
482 {
483 Activate();
484 }
485 else
486 {
487 _id = GetButtonId(GetFreeButtonId());
488 }
489
490 }
491
492 unsigned Button::GetId()
493 {
494 return _id;
495 }
496
497 }
498
499 //=============================================================================================================================================================
500
502 class Form : public UIElement
503 {
504 private:
507
509 std::string _inputText;
510
512 ksys_color_t _FormColor;
513
514 public:
522 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 ButtonTextColor = OS::sys_color_table.work_area, unsigned Margin = DefaultMargin);
523
525 void render();
526
528 void Handler();
529
530 bool ButtonHandler();
531
534 std::string GetInput();
535
536 ~Form();
537 };
538
539 Form::Form(KolibriLib::point coord, KolibriLib::point size, std::string BackgroundText, ksys_color_t FormColor, ksys_color_t ButtonTextColor, unsigned Margin) : UIElement(coord, size, Margin)
540 {
541
542 _FormColor = FormColor;
543 _butt.init(coord, size, BackgroundText, Margin, ButtonTextColor);
544 }
545
546 Form::~Form()
547 {
548 _butt.~Button();
549 }
550
552 {
553 KolibriLib::graphic::DrawRectangleLines(_coord, {_coord.x + _size.x, _coord.y + _size.y}, _FormColor);
554
555 _butt.render();
556 }
557
558 std::string Form::GetInput()
559 {
560 return _inputText;
561 }
562
564 {
565 char input = CheckKeyboard();
566 if (input > 33 && input != 127) // Если введённый символ не является спецсимволом, и это не Delete
567 {
568 _inputText.push_back(input);
569 }
570 if (input == 127) // input = Delete
571 {
572 _inputText.erase(_inputText.end() - 1);
573 }
574 }
575 bool Form::ButtonHandler()
576 {
577 if(_butt.Handler())
578 {
579 return true;
580 }
581 else
582 {
583 return false;
584 }
585 }
586 //=============================================================================================================================================================
587
588
589 }
590}
591
592#endif // __UI_H__
Форма для ввода текста
Definition UI.hpp:503
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 ButtonTextColor=OS::sys_color_table.work_area, unsigned Margin=DefaultMargin)
конструктор
Definition UI.hpp:539
void render()
Отрисовать форму
Definition UI.hpp:551
void Handler()
Обработчик
Definition UI.hpp:563
std::string GetInput()
Получить введённый текст
Definition UI.hpp:558
Элемент интерфейса
Definition UI.hpp:25
unsigned _Margin
Отступы
Definition UI.hpp:35
KolibriLib::point _coord
Координаты
Definition UI.hpp:29
KolibriLib::point _size
Размер
Definition UI.hpp:32
Класс для работы с кнопками
Definition UI.hpp:344
bool Handler()
Обработчик кнопки
Definition UI.hpp:452
void Deactivate()
Деактивировать кнопку
Definition UI.hpp:414
bool GetStatus()
Получить сосояние кнопки на момент последней обработки
Definition UI.hpp:465
void Activate()
Активировать кнопку
Definition UI.hpp:423
Button(point coord={0, 0}, point size={20, 20}, std::string text="Button", unsigned Margin=DefaultMargin, ksys_color_t ButtonColor=OS::sys_color_table.work_button, ksys_color_t TextColor=OS::sys_color_table.work_text)
Это конструктор
Definition UI.hpp:409
void init(point coord={0, 0}, point size={0, 0}, std::string text="Button", unsigned Margin=DefaultMargin, ksys_color_t ButtonColor=OS::sys_color_table.work_button, ksys_color_t TextColor=OS::sys_color_table.work_text)
инициализировать параметры
Definition UI.hpp:470
void render()
Отрисовать кнопку
Definition UI.hpp:437
unsigned GetId()
Получить номер кнопки
Definition UI.hpp:492
Текстовая метка
Definition UI.hpp:108
void SetTextColor(const ksys_color_t &NewTextColor)
Изменить цвет текста
Definition UI.hpp:234
void SetScale(bool scale)
Изменить значение переменной _TextScale.
Definition UI.hpp:239
void SetFontSize(const unsigned &NewTextSize)
Изменить рамер текста
Definition UI.hpp:219
ksys_color_t GetTextColor()
Получить цвет текста
Definition UI.hpp:244
void SetSize(const point &NewSize)
Изменить размер текстовой метки
Definition UI.hpp:224
unsigned GetFontSize()
Получить Размер шрифта
Definition UI.hpp:210
TextLabel(point coord={0, 0}, point size={0, 0}, std::string text="Text", unsigned FontSize=9, bool TextScale=true, ksys_color_t TextColor=OS::sys_color_table.work_text, unsigned Margin=0)
Конструктор
Definition UI.hpp:175
void SetCoord(const point &NewCoords)
Изменить координаты текстовой метки
Definition UI.hpp:229
void SetText(const std::string &NewText)
Изменить текст
Definition UI.hpp:215
void render()
Отрисовать текстовую метку
Definition UI.hpp:188
std::string GetText()
Получить текст
Definition UI.hpp:206
ksys_colors_table_t sys_color_table
Таблица стандартных(системных) цветов
Definition base.hpp:36
void SetTextSize(unsigned int newSize)
Изменить размер текста
Definition UI.hpp:75
void DrawText(const std::string &text, const point &coord, const unsigned &size=9, ksys_color_t color=OS::sys_color_table.work_text)
Вывести текст
Definition UI.hpp:88
unsigned int GetTextSize()
Получить размер текста
Definition UI.hpp:62
void DrawBar(point coord, KolibriLib::point size, ksys_color_t color=OS::sys_color_table.work_graph)
Definition UI.hpp:52
const unsigned DefaultMargin
Отступы поумолчанию
Definition UI.hpp:20
Основное пространство имён
Definition base.hpp:17
char CheckKeyboard()
Проверить какая клавиша клавиатуры нажата
Definition base.hpp:114
void init()
Инициализация
Definition base.hpp:106
Служебная структура, нигде не использется кроме ButtonsIdList.
Definition UI.hpp:260
Просто точка
Definition base.hpp:26