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 "windowBase.hpp"
16#include "UI.hpp"
17
18namespace KolibriLib
19{ //=============================================================================================================================================================
20
21
22
24 namespace window
25 {
26
28 {
30 bool use;
31 };
32
34 {
36 bool use;
37 };
38
40 {
41 UI::Form frm;
42 bool use;
43 };
44
46 class Window
47 {
48 private:
50 std::string _title;
51
53 point _size;
54
56 ksys_colors_table_t _colors;
57
59 unsigned _MARGIN;
60
62 bool _Redraw = false;
63
65 int _style;
66
67 // Я тут теряюсь, использовать std::vector или std::list ?
68
70 std::vector<ButtonListElement> _Buttons;
71
73 std::vector<TextListElement> _Texts;
74
76 std::vector<FormListElement> _Forms;
79 unsigned ActiveForm = 0;
80
84 unsigned AddNewButton(UI::buttons::Button btn);
85
89 unsigned AddNewTextLabel(UI::text::TextLabel text);
90
94 unsigned AddNewForm(UI::Form form);
95
96 public:
103 Window(std::string Title = "Window", point size = DefaultWindowSize, int style = 0x14, ksys_colors_table_t colors = Color::DefaultColorTable, unsigned Margin = 0);
104 ~Window();
105
107 void Render();
108
111 unsigned GetMargin();
112
115 point GetSize();
116
119 void SetWindowColors(ksys_colors_table_t colorTable);
120
124 void StartRedraw();
125
128 void EndRedraw();
129
137
141 void ChangeWindow(point coord, point size);
142
145 void ChangeTilte(std::string newTitle);
146
151
161 unsigned CreateButton(point coord = {0, 0}, point size = {16, 16}, std::string Text = " ", unsigned margin = UI::DefaultMargin, bool UseWindowColors = true, ksys_color_t color = OS::sys_color_table.work_button, ksys_color_t TextColor = OS::sys_color_table.work_button_text);
162
166 unsigned CreateButton(UI::buttons::Button btn);
167
170 void DeleteButton(unsigned id);
171
180 unsigned CreateText(point coord = {0,0}, point size = {16, 16}, std::string text = "Text", unsigned FontSize = 9, bool UseWindowColors = true, ksys_color_t color = OS::sys_color_table.work_text);
181
185 unsigned CreateText(UI::text::TextLabel text);
186
189 void DeleteText(unsigned id);
190
195 unsigned CreateForm(point coord = {0, 0}, 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);
196
200 unsigned CreateForm(UI::Form form);
201
205 std::string GetTextFromForm(unsigned form);
206
209 void DeleteForm(unsigned id);
210
212 void FormHandler();
213
216 unsigned Handler();
217
220 unsigned GetPressedButton();
221 };
222
223 unsigned Window::AddNewButton(UI::buttons::Button btn)
224 {
225 for(unsigned i = 0; i < _Buttons.size(); i++)
226 {
227 if(!_Buttons[i].use)
228 {
229 _Buttons[i].btn = btn;
230 _Buttons[i].btn.Activate();
231 _Buttons[i].use = true;
232 return i;
233 }
234 }
235 ButtonListElement a;
236 a.btn = btn;
237 a.use = true;
238 _Buttons.push_back(a);
239 return _Buttons.size();
240 }
241
242 unsigned Window::AddNewTextLabel(UI::text::TextLabel text)
243 {
244 for (unsigned i = 0; i < _Texts.size(); i++)
245 {
246 if (!_Texts[i].use)
247 {
248 _Texts[i].txt = text;
249 _Texts[i].use = true;
250 return i;
251 }
252 }
253 TextListElement a;
254 a.txt = text;
255 a.use = true;
256 _Texts.push_back(a);
257 }
258
259 unsigned Window::AddNewForm(UI::Form form)
260 {
261 for (unsigned i = 0; i < _Forms.size(); i++)
262 {
263 if (!_Forms[i].use)
264 {
265 _Forms[i].frm = form;
266 _Forms[i].use = true;
267 return i;
268 }
269 }
270 FormListElement a;
271 a.frm = form;
272 a.use = true;
273 _Forms.push_back(a);
274 return _Forms.size();
275 }
276
277 Window::Window(std::string Title, point size, int style, ksys_colors_table_t colors, unsigned Margin)
278 {
279 _title = Title;
280 _size = size;
281 _style = style;
283 {
284 _colors = OS::GetSystemColors();
285 }
286 else
287 {
288 _colors = colors;
289 }
290 _MARGIN = Margin;
291 _Texts.reserve(8 * sizeof(TextListElement));
292 _Buttons.reserve(8 * sizeof(ButtonListElement));
293 _Forms.reserve(8 * sizeof(FormListElement));
295 Render();
296 }
297
298 Window::~Window()
299 {
300 }
301
302 void Window::SetWindowColors(ksys_colors_table_t colorTable)
303 {
304 _colors = colorTable;
305 }
306
308 {
309 _ksys_start_draw();
310 _Redraw = true;
311 }
312
314 {
315 if (_Redraw)
316 {
317 _ksys_end_draw();
318 _Redraw = false;
319 }
320 }
321
323 {
324 _ksys_create_window(coord.x, coord.y, _size.x, _size.y, _title.c_str(), _colors.work_area, _style);
325 }
326
328 {
329 _size = size;
330 _ksys_change_window(coord.x, coord.y, size.x, size.y);
331 }
332
333 void Window::ChangeTilte(std::string newTitle)
334 {
335 _ksys_set_window_title(newTitle.c_str());
336 }
337
339 {
340 return _size;
341 }
343 {
344 StartRedraw();
345 DrawWindow();
346
347 for(unsigned i = 0; i < _Buttons.size(); i++)
348 {
349 if(_Buttons[i].use)
350 {
351 _Buttons[i].btn.render();
352 }
353 }
354 for(unsigned i = 0; i < _Texts.size(); i++)
355 {
356 if(_Texts[i].use)
357 {
358 _Texts[i].txt.render();
359 }
360 }
361 for(unsigned i = 0; i < _Forms.size(); i++)
362 {
363 if(_Forms[i].use)
364 {
365 _Forms[i].frm.render();
366 }
367 }
368
369 EndRedraw();
370 }
371
373 {
374 return _MARGIN;
375 }
376
378 {
379 return _size;
380 }
381
382 unsigned Window::CreateButton(point coord, point size, std::string Text, unsigned margin, bool UseWindowColors, ksys_color_t color, ksys_color_t TextColor)
383 {
384 if(UseWindowColors)
385 {
386 color = _colors.work_button;
387 TextColor = _colors.work_button_text;
388 }
389 UI::buttons::Button btn(coord, size, Text, margin, color, TextColor);
390 unsigned a = AddNewButton(btn);
391 btn.Deactivate();
392 return a;
393 }
394
396 {
397 return AddNewButton(btn);
398 }
399
400 void Window::DeleteButton(unsigned id)
401 {
402 _Buttons[id].btn.Deactivate();
403 _Buttons[id].use = false;
404 }
405 unsigned Window::CreateText(point coord, point size, std::string text, unsigned FontSize, bool UseWindowColors, ksys_color_t color)
406 {
407 if(UseWindowColors)
408 {
409 color = _colors.work_text;
410 }
411
412 UI::text::TextLabel t(coord, size, text, FontSize);
413
414 return AddNewTextLabel(t);
415
416 }
418 {
419 return AddNewTextLabel(text);
420 }
421
422 void Window::DeleteText(unsigned id)
423 {
424 _Texts[id].use = false;
425 }
426
427 unsigned Window::CreateForm (point coord, point size, std::string BackgroundText, ksys_color_t FormColor, ksys_color_t BackgroundTextColor)
428 {
429 UI::Form form (coord, size, BackgroundText, FormColor, BackgroundTextColor, _MARGIN);
430 return AddNewForm (form);
431 }
432
434 {
435 return AddNewForm(form);
436 }
437
438 std::string Window::GetTextFromForm(unsigned form)
439 {
440 return _Forms[form].frm.GetInput();
441 }
442
443 void Window::DeleteForm(unsigned id)
444 {
445 _Forms[id].use = false;
446 }
447
449 {
450 for (unsigned i = 0; i < _Forms.size(); i++)
451 {
452 if (_Forms[i].use) // Если форма используется
453 {
454 if (_Forms[i].frm.ButtonHandler())
455 {
456 ActiveForm = i;
457 }
458 }
459 }
460 _Forms[ActiveForm].frm.Handler();
461 }
462
464 {
465 unsigned Event = OS::WaitEvent(); // Ждём пока не появится какой либо ивент
466
467 switch (Event)
468 {
469 case KSYS_EVENT_REDRAW:
470 Render();
471 break;
472 case KSYS_EVENT_BUTTON:
473 bool flag = false;
474 for (unsigned i = 0; i < _Buttons.size(); i++)
475 {
476 if (_Buttons[i].use) // Если кнопка используется
477 {
478 _Buttons[i].btn.Handler();
479 flag = true;
480 }
481 }
482 FormHandler();
483 if(flag)
484 {
485
486 unsigned button = KolibriLib::UI::buttons::GetPressedButton();
487 if (button == 1)
488 {
489 EXIT = true;
490 EXITCODE = 0;
491 }
492 }
493 break;
494 }
495 return Event;
496 }
498 {
499 for(unsigned i = 0; i < _Buttons.size(); i++)
500 {
501 if(_Buttons[i].btn.GetStatus())
502 {
503 return i;
504 }
505 }
506 return _Buttons.size();
507 }
508 }
509
510 //=============================================================================================================================================================
511
512}
513
514
515
516
517//=============================================================================================================================================================
518
519
520#endif
Форма для ввода текста
Definition UI.hpp:503
Класс для работы с кнопками
Definition UI.hpp:344
void Deactivate()
Деактивировать кнопку
Definition UI.hpp:414
bool GetStatus()
Получить сосояние кнопки на момент последней обработки
Definition UI.hpp:465
Текстовая метка
Definition UI.hpp:108
Класс для работы с окном
Definition window.hpp:47
Window(std::string Title="Window", point size=DefaultWindowSize, int style=0x14, ksys_colors_table_t colors=Color::DefaultColorTable, unsigned Margin=0)
Конструктор
Definition window.hpp:277
void EndRedraw()
Закончить перересовку окна
Definition window.hpp:313
void DeleteText(unsigned id)
Удалить текст
Definition window.hpp:422
void DeleteForm(unsigned id)
Удалить форму
Definition window.hpp:443
void ChangeWindow(point coord, point size)
Изменить окно
Definition window.hpp:327
unsigned CreateText(point coord={0, 0}, point size={16, 16}, std::string text="Text", unsigned FontSize=9, bool UseWindowColors=true, ksys_color_t color=OS::sys_color_table.work_text)
Создать текст в окне
Definition window.hpp:405
void StartRedraw()
Начать перересовку окна
Definition window.hpp:307
void SetWindowColors(ksys_colors_table_t colorTable)
Задать стандартные цвета окна
Definition window.hpp:302
std::string GetTextFromForm(unsigned form)
Получить текст из формы
Definition window.hpp:438
void ChangeTilte(std::string newTitle)
Изменить заголовок окна
Definition window.hpp:333
point GetSize()
Получить размер окна
Definition window.hpp:377
unsigned CreateButton(point coord={0, 0}, point size={16, 16}, std::string Text=" ", unsigned margin=UI::DefaultMargin, bool UseWindowColors=true, ksys_color_t color=OS::sys_color_table.work_button, ksys_color_t TextColor=OS::sys_color_table.work_button_text)
Создать кнопку
Definition window.hpp:382
unsigned CreateForm(point coord={0, 0}, 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)
Создать форму
Definition window.hpp:427
void FormHandler()
Обработчик форм
Definition window.hpp:448
unsigned GetMargin()
Получить рамер отступов в этом окне
Definition window.hpp:372
void DeleteButton(unsigned id)
Удалить кнопку
Definition window.hpp:400
void Render()
Отрисовать окно
Definition window.hpp:342
point GetWindowSize()
Получить размер окна
Definition window.hpp:338
unsigned Handler()
Обработчик элементов
Definition window.hpp:463
unsigned GetPressedButton()
Получить нажатую кнопку
Definition window.hpp:497
void DrawWindow(point coord=mouse::GetMousePositionOnSreen())
Отрисовать окно
Definition window.hpp:322
const ksys_colors_table_t DefaultColorTable
Таблица цветов по умолчанию
Definition windowBase.hpp:14
bool ComparisonColorsTables(ksys_colors_table_t a, ksys_colors_table_t b)
Сравнить две таблица цветов
Definition windowBase.hpp:20
ksys_colors_table_t GetSystemColors()
Получить системные цвета
Definition base.hpp:41
unsigned int WaitEvent()
Ждать ивента
Definition base.hpp:49
ksys_colors_table_t sys_color_table
Таблица стандартных(системных) цветов
Definition base.hpp:36
const unsigned DefaultMargin
Отступы поумолчанию
Definition UI.hpp:20
point GetMousePositionOnSreen()
Получить позицияю курсора на экране
Definition mouse.hpp:15
const point DefaultWindowCoord
Соординаты окна по умолчанию
Definition windowBase.hpp:39
const point DefaultWindowSize
Размер окна поумолчанию
Definition windowBase.hpp:36
Основное пространство имён
Definition base.hpp:17
bool EXIT
Сообщение всем функциям что нужно завершать работу
Definition base.hpp:19
int EXITCODE
Код ошибки
Definition base.hpp:22
Просто точка
Definition base.hpp:26
Definition window.hpp:40
Definition window.hpp:34