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(KolibriLib::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(KolibriLib::point coord, KolibriLib::point size, ksys_color_t color = OS::sys_color_table.work_graph)
53 {
54 _ksys_draw_bar(coord.x * KolibriLib::AAANUMBER, coord.y * KolibriLib::AAANUMBER, size.x, size.y, color);
55 }
56
57 namespace text
58 {
61 unsigned int GetTextSize()
62 {
63 unsigned Size;
64 asm_inline(
65 "int $0x40"
66 : "=c"(Size)
67 : "a"(48), "b"(11)
68 );
69 return Size;
70 }
71
74 void SetTextSize(unsigned int newSize)
75 {
76 asm_inline(
77 "int $0x40" ::"a"(48), "b"(12), "c"(newSize)
78 );
79 }
80
81
86 inline void DrawText(std::string text, KolibriLib::point coord, unsigned size = 9, ksys_color_t color = OS::sys_color_table.work_text)
87 {
88 SetTextSize(size);
89 _ksys_draw_text(text.c_str(), coord.x, coord.y, text.length(), color);
90 }
91
96 inline void DrawText(const char *text, KolibriLib::point coord, unsigned size = 9, ksys_color_t color = OS::sys_color_table.work_text)
97 {
98 SetTextSize(size);
99 _ksys_draw_text(text, coord.x, coord.y, strlen(text), color);
100 }
101
102 //=============================================================================================================================================================
103
105 class TextLabel: public UIElement
106 {
107 private:
109 std::string _text;
110
112 unsigned _FontSize;
113
115 bool _TextScale;
116
117 ksys_color_t _TextColor;
118 public:
126 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);
127
128 ~TextLabel();
129
131 void render();
132
135 std::string GetText();
136
139 unsigned GetFontSize();
140
143 void SetText(std::string NewText);
144
147 void SetFontSize(unsigned NewTextSize);
148
151 void SetSize(point NewSize);
152
155 void SetCoord(point NewCoords);
156
159 void SetTextColor(ksys_color_t NewTextColor);
160 };
161
162 TextLabel::TextLabel(point coord, point size, std::string text, unsigned FontSize, bool TextScale, ksys_color_t TextColor, unsigned Margin) : UIElement(coord, size, Margin)
163 {
164 _text = text;
165 _FontSize = FontSize;
166 _TextScale = TextScale;
167 _TextColor = TextColor;
168 render();
169 }
170
171 TextLabel::~TextLabel()
172 {
173
174 }
175
177 {
178 if(_TextScale) //Если текст нужно подстраивать размер, то
179 { //Постраиваем
180 _FontSize = _size.x / _text.length();
181 }
182 SetTextSize(_FontSize);
183
184 unsigned a = 0;
185
186 if((_text.length() * _FontSize) > _size.x) //Центрирование текста
187 {
188 a = (_size.x / 2) - (_text.length() * _FontSize);
189 }
190
191 DrawText(_text, {_coord.x + (_size.x / 2), _coord.y + a}, _FontSize, _TextColor);
192 }
193
194 std::string TextLabel::GetText()
195 {
196 return _text;
197 }
199 {
200 return _FontSize;
201 }
202
203 void TextLabel::SetText(std::string NewText)
204 {
205 _text = NewText;
206 }
207 void TextLabel::SetFontSize(unsigned NewTextSize)
208 {
209 _FontSize = NewTextSize;
210 }
211
213 {
214 _size = NewSize;
215 }
216
218 {
219 _coord = NewCoords;
220 }
221
222 void TextLabel::SetTextColor(ksys_color_t NewTextColor)
223 {
224 _TextColor = NewTextColor;
225 }
226 }
227
228 //=============================================================================================================================================================
229
230 // Работа с кнопками
231 namespace buttons
232 {
233 // Коды кнопок начинаются с этого числа
234 const unsigned StartButtonId = 100;
235
238 {
239 unsigned data;
240 bool use = false;
241 };
242
244 std::vector<ButtonsIdData> ButtonsIdList;
245
249 unsigned GetFreeButtonId()
250 {
251 for (unsigned i = 0; i < ButtonsIdList.size(); i++) // Проходим по всему массиву
252 { // Если встречается свободный элемент,
253 if (!ButtonsIdList[i].use) // То используем его
254 { // Иначе создаём новый и использем тоже новый
255 ButtonsIdList[i].use = true;
256 return i;
257
258 }
259 }
260 ButtonsIdData a;
261 a.data = StartButtonId + ButtonsIdList.size();
262 a.use = true;
263 ButtonsIdList.push_back(a);
264 return ButtonsIdList.size() - 1; //-1 потому что обращение к элементам массива идёт с 0
265 }
266
269 void FreeButtonId(unsigned id)
270 {
271 ButtonsIdList[id].use = false; // Этот элемент теперь не используется
272 }
273
278 unsigned GetButtonId(unsigned id)
279 {
280 return ButtonsIdList[id].data;
281 }
282
288 inline unsigned autoDefineButton(point coords, point size, ksys_color_t color = OS::sys_color_table.work_button)
289 {
290 unsigned id = GetButtonId(GetFreeButtonId()); // Автоматически получаем id для кнопки
291 _ksys_define_button(coords.x, coords.y, size.x, size.y, id, color);
292 return id;
293 }
294
300 inline void DefineButton(point coord, point size, unsigned id, ksys_color_t color = OS::sys_color_table.work_button)
301 {
302 _ksys_define_button(coord.x, coord.y, size.x, size.y, id, color);
303 }
304
307 inline void DeleteButton(unsigned id)
308 {
309 _ksys_delete_button(id);
310 FreeButtonId(id); // Кнопка удалена, теперь этот id не использется
311 }
312
315 inline unsigned GetPressedButton()
316 {
317 return _ksys_get_button();
318 }
319
320 //=============================================================================================================================================================
321
323 class Button : public UIElement
324 {
325 private:
327 ksys_color_t _ButtonColor;
328
330 unsigned _id;
331
333 bool _status;
334
337 bool _active;
338
340 text::TextLabel _text;
341 public:
349 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);
350
358 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);
359
361 void render();
362
366 bool Handler();
367
370 bool GetStatus();
371
374 unsigned GetId();
375
379 void Deactivate();
380
383 void Activate();
384
385 ~Button();
386 };
387
388 Button::Button(point coord, point size, std::string text, unsigned Margin, ksys_color_t ButtonColor, ksys_color_t TextColor) : UIElement(coord, size, Margin)
389 {
390 init(coord, size, text, Margin, ButtonColor, TextColor);
391 }
392
394 {
395 if(_active)
396 {
397 DeleteButton(_id);
398 _active = false;
399 }
400 }
401
402 inline void Button::Activate()
403 {
404 if(!_active)
405 {
406 _id = GetFreeButtonId();
407 _active = true;
408 }
409 }
410
411 Button::~Button()
412 {
413 DeleteButton(_id);
414 }
415
417 {
418 if(_active)
419 {
420 DefineButton(_coord, _size, GetButtonId(_id), _ButtonColor);
421 }
422 else
423 {
424 graphic::DrawRectangleLines(_coord, {_coord.x + _size.x, _coord.y + _size.y}, _ButtonColor);
425 }
426
427 _text.render();
428
429 }
430
432 {
433 if (GetPressedButton() == _id)
434 {
435 _status = true;
436 }
437 else
438 {
439 _status = false;
440 }
441 return _status;
442 }
443
445 {
446 return _status;
447 }
448
449 void UI::buttons::Button::init(point coord, point size, std::string text, unsigned Margin, ksys_color_t ButtonColor, ksys_color_t TextColor)
450 {
451 _coord = coord;
452 _size = size;
453 _Margin = Margin;
454 _text.SetText(text);
455 _text.SetCoord(coord);
456 _text.SetSize(size);
457 _text.SetTextColor(TextColor);
458 _ButtonColor = ButtonColor;
459
460 if (!_active) //Если кнопка была неактивна, то нужно её активировать
461 {
462 Activate();
463 }
464 else
465 {
466 _id = GetFreeButtonId();
467 }
468
469 render();
470 }
471
472 unsigned Button::GetId()
473 {
474 return _id;
475 }
476
477 }
478
479 //=============================================================================================================================================================
480
482 class Form : public UIElement
483 {
484 private:
486 std::string _inputText;
487
489 ksys_color_t _FormColor;
490
493
494 public:
502 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);
503
505 void render();
506
508 void Handler();
509
510 bool ButtonHandler();
511
514 std::string GetInput();
515
516 ~Form();
517 };
518
519 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)
520 {
521
522 _FormColor = FormColor;
523 _butt.init(coord, size, BackgroundText, Margin, ButtonTextColor);
524 render();
525 }
526
527 Form::~Form()
528 {
529 _butt.~Button();
530 }
531
533 {
534 KolibriLib::graphic::DrawRectangleLines(_coord, {_coord.x + _size.x, _coord.y + _size.y});
535
536 _butt.render();
537 }
538
539 std::string Form::GetInput()
540 {
541 return _inputText;
542 }
543
545 {
546 char input = CheckKeyboard();
547 if (input > 33 && input != 127) // Если введённый символ не является спецсимволом, и это не Delete
548 {
549 _inputText.push_back(input);
550 }
551 if (input == 127) // input = Delete
552 {
553 _inputText.erase(_inputText.end() - 1);
554 }
555 }
556 bool Form::ButtonHandler()
557 {
558 if(_butt.Handler())
559 {
560 return true;
561 }
562 else
563 {
564 return false;
565 }
566 }
567 //=============================================================================================================================================================
568
569
570 }
571}
572
573#endif // __UI_H__
Форма для ввода текста
Definition UI.hpp:483
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:519
void render()
Отрисовать форму
Definition UI.hpp:532
void Handler()
Обработчик
Definition UI.hpp:544
std::string GetInput()
Получить введённый текст
Definition UI.hpp:539
Элемент интерфейса
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:324
bool Handler()
Обработчик кнопки
Definition UI.hpp:431
void Deactivate()
Деактивировать кнопку
Definition UI.hpp:393
bool GetStatus()
Получить сосояние кнопки на момент последней обработки
Definition UI.hpp:444
void Activate()
Активировать кнопку
Definition UI.hpp:402
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:388
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:449
void render()
Отрисовать кнопку
Definition UI.hpp:416
unsigned GetId()
Получить номер кнопки
Definition UI.hpp:472
Текстовая метка
Definition UI.hpp:106
void SetSize(point NewSize)
Изменить размер текстовой метки
Definition UI.hpp:212
void SetText(std::string NewText)
Изменить текст
Definition UI.hpp:203
unsigned GetFontSize()
Получить Размер шрифта
Definition UI.hpp:198
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:162
void render()
Отрисовать текстовую метку
Definition UI.hpp:176
void SetFontSize(unsigned NewTextSize)
Изменить рамер текста
Definition UI.hpp:207
void SetTextColor(ksys_color_t NewTextColor)
Изменить цвет текста
Definition UI.hpp:222
std::string GetText()
Получить текст
Definition UI.hpp:194
void SetCoord(point NewCoords)
Изменить координаты текстовой метки
Definition UI.hpp:217
ksys_colors_table_t sys_color_table
Таблица стандартных(системных) цветов
Definition base.hpp:37
void DrawBar(KolibriLib::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:110
void init()
Инициализация
Definition base.hpp:102
Служебная структура, нигде не использется кроме ButtonsIdList.
Definition UI.hpp:238
Просто точка
Definition base.hpp:22