KolibriLib
Loading...
Searching...
No Matches
window.hpp
1#pragma once
2
3#ifndef __WINDOW_H__
4#define __WINDOW_H__
5
6
7#include <string>
8#include <vector>
9
10
11#include <sys/ksys.h>
12
13#include "base.hpp"
14#include "mouse.hpp"
15#include "os.hpp"
16#include "windowBase.hpp"
17#include "button.hpp"
18#include "image.hpp"
19#include "checkbox.hpp"
20#include "form.hpp"
21
22namespace KolibriLib
23{ //=============================================================================================================================================================
24
25
26
28 namespace window
29 {
30
31
32 struct Element
33 {
34 enum Type
35 {
36 None = 0,
37 Button,
38 Image,
39 CheckBox,
40 Form,
41 TextLabel
42 };
43
44
47 UI::Form frm;
48 UI::CheckBox ChckBx;
50
51
52 unsigned _type;
53 unsigned DrawPrioritet;
54 bool use;
55
56
57
58
59 };
60
62 class Window
63 {
64 private:
66 std::string _title;
67
69 UI::Size _size;
70
72 Color::ColorsTable _colors;
73
75 unsigned _MARGIN;
76
78 int _style;
79
80 unsigned int activeForm;
81
83 bool _Redraw = false;
84
85
87 std::vector<Element> _Elements;
88
92 unsigned AddNewButton(UI::buttons::Button btn);
93
97 unsigned AddNewTextLabel(UI::text::TextLabel text);
98
102 unsigned AddNewForm(UI::Form form);
103
104 public:
111 Window(std::string Title = "Window", UI::Size size = DefaultWindowSize, int style = 0x14, Color::ColorsTable colors = Color::DefaultColorTable, unsigned Margin = 0);
112 ~Window();
113
115 void Render();
116
119 unsigned GetMargin();
120
124
127 void SetWindowColors(Color::ColorsTable colorTable);
128
132 void StartRedraw();
133
136 void EndRedraw();
137
145
149 void ChangeWindow(UI::Coord coord, UI::Size size);
150
153 void ChangeTilte(std::string newTitle);
154
159
169 unsigned CreateButton(UI::Coord coord = {0, 0}, UI::Size size = {16, 16}, std::string Text = " ", unsigned margin = UI::DefaultMargin, bool UseWindowColors = true, Color::Color color = OS::sys_color_table.work_button, Color::Color TextColor = OS::sys_color_table.work_button_text);
170
174 unsigned CreateButton(const UI::buttons::Button& btn);
175
179 UI::buttons::Button GetButton(unsigned id);
180
189 unsigned CreateText(UI::Coord coord = {0, 0}, UI::Size size = {16, 16}, std::string text = "Text", unsigned FontSize = 9, bool UseWindowColors = true, Color::Color color = OS::sys_color_table.work_text);
190
194 unsigned CreateText(const UI::text::TextLabel& text);
195
199 void SetTextLabel(unsigned N, const UI::text::TextLabel &text);
200
204 unsigned CreateForm(UI::Form form);
205
208 void DeleteElement(unsigned id);
209
213
215 UI::buttons::ButtonID GetPressedButton();
216
220 std::string GetInputFromFrom(unsigned form);
221
223 void HandlerThread();
224 };
225
226 unsigned Window::AddNewButton(UI::buttons::Button btn)
227 {
228 for(unsigned i = 0; i < _Elements.size(); i++)
229 {
230 if (!_Elements[i].use)
231 {
232 switch (btn.GetType())
233 {
234 case UI::buttons::Button::Type::Image:
235 _Elements[i].btn = btn;
236 break;
237 case UI::buttons::Button::Type::Text:
238 _Elements[i].btn.init(btn.GetCoord(), btn.GetSize(), btn.GetTextLabel(), btn.GetMargin(), btn.GetColor());
239 break;
240 default:
241 break;
242 }
243 _Elements[i]._type = Element::Type::Button;
244 _Elements[i].use = true;
245 return i;
246 }
247 }
248 Element a;
249 switch (btn.GetType())
250 {
251 case UI::buttons::Button::Type::Image:
252 a.btn.init(btn.GetCoord(), btn.GetSize(), btn.GetImage(), btn.GetMargin(), btn.GetColor());
253 break;
254 case UI::buttons::Button::Type::Text:
255 a.btn.init(btn.GetCoord(), btn.GetSize(), btn.GetTextLabel(), btn.GetMargin(), btn.GetColor());
256 default:
257 break;
258 }
259 a.use = true;
260 _Elements.push_back(a);
261 return _Elements.size();
262 }
263
264 unsigned Window::AddNewTextLabel(UI::text::TextLabel text)
265 {
266 _ksys_debug_puts("AddNewText:");
267 for (unsigned i = 0; i < _Elements.size(); i++)
268 {
269 char *a = "find, try №";
270 char *b = (char*)('0' + (char)i);
271 strcat(b, ".\n");
272 strcat(a, b);
273 _ksys_debug_puts(a);
274 if (!_Elements[i].use)
275 {
276 _Elements[i].txt = text;
277 _Elements[i]._type = Element::Type::TextLabel;
278 _Elements[i].use = true;
279 _ksys_debug_puts("done\n");
280 return i;
281 }
282 }
283 _ksys_debug_puts("\n\n==>Element:");
284 Element a;
285 _ksys_debug_puts("\na.use = true");
286 a.use = true;
287 _ksys_debug_puts("\ntxt.init()");
288 a.txt.init(text.GetCoord(), text.GetSize(), text.GetText(), text.GetFontSize(), text.GetColor());
289 _ksys_debug_puts("\n\nDONE!!!\n\n");
290 _Elements.push_back(a);
291 }
292
293 unsigned Window::AddNewForm(UI::Form form)
294 {
295 for (unsigned i = 0; i < _Elements.size(); i++)
296 {
297 if (!_Elements[i].use)
298 {
299 _Elements[i].frm.init(form.GetCoord(), form.GetSize(), NULL, form.GetColor(), form.GetBackgroundColor(), form.GetMargin());
300 _Elements[i]._type = Element::Type::Form;
301 _Elements[i].use = true;
302 return i;
303 }
304 }
305 Element a;
306 a.frm.init(form.GetCoord(), form.GetSize(), NULL, form.GetColor(), form.GetBackgroundColor(), form.GetMargin());
307 a.use = true;
308 a._type = Element::Type::Form;
309 _Elements.push_back(a);
310 return _Elements.size();
311 }
312
313 Window::Window(std::string Title, UI::Size size, int style, Color::ColorsTable colors, unsigned Margin)
314 {
315 _ksys_debug_puts("window:");
316 _title = Title;
317 _size = size;
318 _style = style;
319 _MARGIN = Margin;
320
321 if (Color::ComparisonColorsTables(colors, Color::DefaultColorTable)) //Если небыла в аргументах таблица цветов
322 { //Используется системная
323 _colors = OS::GetSystemColors();
324 }
325 else
326 {
327 _colors = colors;
328 }
329
330 DrawWindow(DefaultWindowCoord); //Отрисовать окно
331 }
332
333 Window::~Window()
334 {
335 for(unsigned i = 0; i < _Elements.size(); i++)
336 {
337 _Elements[i].use = false;
338 _Elements[i].btn.~Button();
339 _Elements[i].ChckBx.~CheckBox();
340 _Elements[i].txt.~TextLabel();
341 _Elements[i].frm.~Form();
342 _Elements[i].img.~image();
343 }
344 }
345
347 {
348 _colors = colorTable;
349 }
350
352 {
353 _ksys_start_draw();
354 _Redraw = true;
355 }
356
358 {
359 if (_Redraw)
360 {
361 _ksys_end_draw();
362 _Redraw = false;
363 }
364 }
365
367 {
368 _ksys_create_window(coord.x, coord.y, _size.x, _size.y, _title.c_str(), _colors.work_area, _style);
369 }
370
372 {
373 _size = size;
374 _ksys_change_window(coord.x, coord.y, size.x, size.y);
375 }
376
377 void Window::ChangeTilte(std::string newTitle)
378 {
379 _ksys_set_window_title(newTitle.c_str());
380 }
381
383 {
384 return _size;
385 }
387 {
388 StartRedraw();
389 DrawWindow();
390
391 for (unsigned i = 0; i < _Elements.size(); i++)
392 {
393 if (_Elements[i].use)
394 {
395 switch (_Elements[i]._type)
396 {
397 case Element::Type::Button:
398 _Elements[i].btn.Render();
399 break;
400 case Element::Type::TextLabel:
401 _Elements[i].txt.Render();
402 break;
403 case Element::Type::CheckBox:
404 _Elements[i].ChckBx.Render();
405 break;
406 case Element::Type::Image:
407 _Elements[i].img.Render();
408 break;
409 default:
410 break;
411 }
412 }
413 }
414
415 EndRedraw();
416 }
417
419 {
420 return _MARGIN;
421 }
422
424 {
425 return _size;
426 }
427
428 unsigned KolibriLib::window::Window::CreateButton(UI::Coord coord, UI::Size size, std::string Text, unsigned margin, bool UseWindowColors, Color::Color color, Color::Color TextColor)
429 {
430 if(UseWindowColors)
431 {
432 color = _colors.work_button;
433 TextColor = _colors.work_button_text;
434 }
435 UI::buttons::Button b(coord, size, margin, color);
436 return AddNewButton(b);
437 }
438
439
440
441 unsigned Window::CreateText(UI::Coord coord, UI::Size size, std::string text, unsigned FontSize, bool UseWindowColors, Color::Color color)
442 {
443 _ksys_debug_puts("CreateText:");
444 if(UseWindowColors)
445 {
446 color = _colors.work_text;
447 }
448
449 UI::text::TextLabel t(coord, size, text, FontSize, true, color, _MARGIN);
450 return AddNewTextLabel(t);
451
452 }
454 {
455 return AddNewTextLabel(UI::text::TextLabel(text));
456 }
457
458
459
460 void Window::DeleteElement(unsigned id)
461 {
462 _Elements[id].use = false;
463 }
465 {
466 OS::Event event = OS::WaitEvent(10);
467
468 switch (event)
469 {
470 case OS::Events::Redraw:
471 Render();
472 break;
473 case OS::Events::Button:
475 {
476 return OS::Events::Exit;
477 }
478
479 for (unsigned i = 0; i < _Elements.size(); i++)
480 {
481 if (_Elements[i].use)
482 {
483 switch(_Elements[i]._type)
484 {
485 case Element::Type::Form:
486
487 _Elements[i].frm.ButtonHandler();
488 case Element::CheckBox:
489 _Elements[i].ChckBx.Handler();
490 case Element::Button:
491 _Elements[i].btn.Handler();
492 }
493 }
494 }
495 break;
496 case OS::Events::Key:
497 for(unsigned i = 0; i < _Elements.size(); i++)
498 {
499 if(_Elements[i].use)
500 {
501 if(_Elements[i]._type == Element::Type::Form)
502 {
503 _Elements[activeForm].frm.Handler();
504 }
505 }
506 }
507 break;
508 }
509 return event;
510 }
511
512 UI::buttons::ButtonID Window::GetPressedButton()
513 {
514 for(unsigned i = 0; i < _Elements.size(); i++)
515 {
516 if(_Elements[i]._type == Element::Type::Button)
517 {
518 if(_Elements[i].btn.GetStatus())
519 {
520 return _Elements[i].btn.GetId();
521 }
522 }
523 }
524 }
525 std::string Window::GetInputFromFrom(unsigned form)
526 {
527 if(_Elements[form]._type == Element::Type::Form)
528 {
529 return _Elements[form].frm.GetInput();
530 }
531 }
532
534 {
535 return _Elements[id].btn;
536 }
537
539 {
540 while(true)
541 {
542 if(Handler() == OS::Events::Exit)
543 {
544 break;
545 }
546 }
547 }
548 }
549
550 //=============================================================================================================================================================
551
552
553
554}
555
556
557
558
559//=============================================================================================================================================================
560
561
562#endif
Класс для работы с чекбоксами
Definition checkbox.hpp:21
Форма для ввода текста
Definition form.hpp:19
Картинка как элемент интерфейса
Definition image.hpp:23
Color::Color GetColor()
Получить осносной цвет элемента
Definition UI.hpp:69
Класс для работы с кнопками
Definition button.hpp:127
unsigned GetType()
Возвращает тип данных используемых в кнопке _type.
Definition button.hpp:274
bool GetStatus()
Получить сосояние кнопки на момент последней обработки
Definition button.hpp:318
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
std::string GetTextLabel()
Возвращает текст кнопки
Definition button.hpp:279
Текстовая метка
Definition text.hpp:66
Класс для работы с окном
Definition window.hpp:63
void HandlerThread()
Обработчик окна в отдельном потоке
Definition window.hpp:538
void EndRedraw()
Закончить перересовку окна
Definition window.hpp:357
Window(std::string Title="Window", UI::Size size=DefaultWindowSize, int style=0x14, Color::ColorsTable colors=Color::DefaultColorTable, unsigned Margin=0)
Конструктор
Definition window.hpp:313
unsigned CreateButton(const UI::buttons::Button &btn)
Создать кнопку
OS::Event Handler()
Обработчик окна
Definition window.hpp:464
UI::buttons::Button GetButton(unsigned id)
Получить кнопку
Definition window.hpp:533
void StartRedraw()
Начать перересовку окна
Definition window.hpp:351
UI::Size GetWindowSize()
Получить размер окна
Definition window.hpp:382
void ChangeWindow(UI::Coord coord, UI::Size size)
Изменить окно
Definition window.hpp:371
void ChangeTilte(std::string newTitle)
Изменить заголовок окна
Definition window.hpp:377
void SetTextLabel(unsigned N, const UI::text::TextLabel &text)
Изменить текст
void DeleteElement(unsigned id)
Удалить элемент
Definition window.hpp:460
unsigned CreateText(UI::Coord coord={0, 0}, UI::Size size={16, 16}, std::string text="Text", unsigned FontSize=9, bool UseWindowColors=true, Color::Color color=OS::sys_color_table.work_text)
Создать текст в окне
Definition window.hpp:441
void SetWindowColors(Color::ColorsTable colorTable)
Задать стандартные цвета окна
Definition window.hpp:346
unsigned CreateButton(UI::Coord coord={0, 0}, UI::Size size={16, 16}, std::string Text=" ", unsigned margin=UI::DefaultMargin, bool UseWindowColors=true, Color::Color color=OS::sys_color_table.work_button, Color::Color TextColor=OS::sys_color_table.work_button_text)
Создать кнопку
Definition window.hpp:428
std::string GetInputFromFrom(unsigned form)
Получить текст введённый в форму
Definition window.hpp:525
unsigned GetMargin()
Получить рамер отступов в этом окне
Definition window.hpp:418
void DrawWindow(UI::Coord coord=mouse::GetMousePositionOnSreen())
Отрисовать окно
Definition window.hpp:366
UI::Size GetSize()
Получить размер окна
Definition window.hpp:423
void Render()
Отрисовать окно
Definition window.hpp:386
UI::buttons::ButtonID GetPressedButton()
Проверить какая нажата
Definition window.hpp:512
unsigned CreateForm(UI::Form form)
Создать форму
ksys_color_t Color
Цвет
Definition color.hpp:13
bool ComparisonColorsTables(const ColorsTable &a, const ColorsTable &b)
Сравнить две таблица цветов
Definition color.hpp:26
const ColorsTable DefaultColorTable
Таблица цветов по умолчанию
Definition color.hpp:19
ksys_colors_table_t ColorsTable
Таблица цветов
Definition color.hpp:16
Event WaitEvent()
Ждать ивента
Definition os.hpp:43
Color::ColorsTable sys_color_table
Таблица стандартных(системных) цветов
Definition os.hpp:15
unsigned int Event
Ивент
Definition os.hpp:27
Color::ColorsTable GetSystemColors()
Получить системные цвета
Definition os.hpp:20
unsigned GetPressedButton()
проверить какая кнопка нажата
Definition button.hpp:117
point< int > GetMousePositionOnSreen()
Получить позицияю курсора на экране
Definition mouse.hpp:15
const UI::Coord DefaultWindowCoord
Соординаты окна по умолчанию
Definition windowBase.hpp:19
const UI::Size DefaultWindowSize
Размер окна поумолчанию
Definition windowBase.hpp:16
Основное пространство имён
Definition base.hpp:18
Definition window.hpp:33