AWind
WindowsManager.h
1 /*
2  AWind.h - Arduino window library support for Color TFT LCD Boards
3  Copyright (C)2015 Andrei Degtiarev. All right reserved
4 
5 
6  You can always find the latest version of the library at
7  https://github.com/AndreiDegtiarev/AWind
8 
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the CC BY-NC-SA 3.0 license.
12  Please see the included documents for further information.
13 
14  Commercial use of this library requires you to buy a license that
15  will allow commercial use. This includes using the library,
16  modified or not, as a tool to sell products.
17 
18  The license applies to all part of the library including the
19  examples and tools supplied with the library.
20 */
21 #pragma once
22 
23 #include "Window.h"
24 #include "MainWindow.h"
25 #include "ICriticalProcess.h"
26 
27 UTFT *globalLcd;
29 template <class T=MainWindow> class WindowsManager : public ICriticalProcess, public ILoopProcess
30 {
31  DC _dc;
32  T *_mainWindow;
33  UTouch *_touch;
34 public:
36 
40  WindowsManager(UTFT *lcd,UTouch *touch=NULL):_dc(lcd),_touch(touch)
41  {
42  globalLcd=lcd;
43  }
45  void Initialize()
46  {
47  _mainWindow=new T(_dc.DeviceWidth(),_dc.DeviceHeight());
48  _mainWindow->Invalidate();
49  _mainWindow->SetLoopProcess(this);
50  _mainWindow->SetDecorators(Environment::Get()->FindDecorators(F("Window")));
51  }
53  Window *HitTest(int x,int y)
54  {
55  if(MainWnd()->ModalWnd()!=NULL)
56  return HitTest(MainWnd()->ModalWnd(),x,y);
57  else
58  return HitTest(MainWnd(),x,y);
59  }
61  Window *HitTest(Window *window,int x,int y)
62  {
63  if(window->IsVisible()
64  &&x>=window->Left() && x<=window->Left()+window->Width()
65  &&y>=window->Top() && y<=window->Top()+window->Height())
66  {
67  for(int i=0;i<window->Children().Count();i++)
68  {
69  Window * retWnd=HitTest(window->Children()[i],x-window->Left(),y-window->Top());
70  if(retWnd!=NULL)
71  return retWnd;
72  }
73  return window;
74  }
75  return NULL;
76  }
78  void loop()
79  {
80  for (int i = 0;i < MainWnd()->Timers().Count();i++)
81  MainWnd()->Timers()[i]->loop();
82  if(MainWnd()->ModalWnd() == NULL)
83  redraw(MainWnd(),false);
84  if(MainWnd()->ModalWnd() != NULL)
85  redraw(MainWnd()->ModalWnd(),MainWnd()->ModalWnd()->IsDirty());
86  }
87  T *MainWnd()
88  {
89  return _mainWindow;
90  }
92  DC *GetDC()
93  {
94  return &_dc;
95  }
97  void Idle()
98  {
99  if(_touch!=NULL)
100  loopTouch();
101  }
102 protected:
104  void redraw(Window *window,bool isForceRedraw)
105  {
106  Idle();
107  if(window == MainWnd()->ModalWnd() && window->IsDirty() && isForceRedraw == false) // Modal window is updated as last one by use of isForceRedraw flag
108  return;
109  bool needRedraw=isForceRedraw || window->IsDirty();
110  if(needRedraw)
111  window->Redraw(&_dc);
112  for(int i=0;i<window->Children().Count();i++)
113  {
114  Window *child=window->Children()[i];
115  if(child->IsVisible())
116  redraw(child,needRedraw);
117  }
118 
119  }
121  void loopTouch()
122  {
123  //out<<F("Check touch")<<endln;
124  if (_touch->dataAvailable())
125  {
126  _touch->read();
127  int x=_touch->getX();
128  int y=_touch->getY();
129  //out<<F("Touch begins x:")<<x<<F(" y:")<<y<<endln;
130  if(x>0 && y>0)
131  {
132  Window *window=_dc.ScreenOrientation()==DC::Landscape?HitTest(x,y): HitTest(y, _dc.DeviceHeight()-x);
133  Window *touchWnd=NULL;
134  if(window!=NULL)
135  {
136  Window *crWindow=window;
137  //out<<F("Searching touch window: ")<<endln;
138  while(crWindow!=NULL && (!crWindow->IsAwaitTouch()))
139  {
140  //out<<crWindow->Name()<<endln;
141  crWindow=crWindow->Parent();
142  }
143  if(crWindow != NULL)
144  {
145  //out<<F("Touch found")<<endln;
146  touchWnd=crWindow;
147  touchWnd->OnTouching(GetDC());
148  }
149  }
150  if(touchWnd!=NULL)
151  {
152  while (_touch->dataAvailable())
153  {
154  _touch->read();
155  }
156  //out<<F("Touch: ")<<touchWnd->Name()<<endln;
157  {
158  Window *crWindow=touchWnd;
159  while(crWindow!=NULL && ((crWindow->IsAwaitTouch()) && !(crWindow)->OnTouch(x,y))||!crWindow->IsAwaitTouch())
160  {
161  crWindow=crWindow->Parent();
162  //out<<F("Touch while")<<endln;
163  }
164  //out<<F("Touch invalidate")<<endln;
165  touchWnd->Invalidate();
166  }
167  }
168  //out<<F("Touch finish")<<endln;
169  }
170  }
171  }
172 };
bool IsDirty()
Returns true if window has to be updated.
Definition: Window.h:162
Base class for all window objects. Provides basic window functionality.
Definition: Window.h:34
Window * HitTest(int x, int y)
Returns topmost window that lais under x,y screen coordinate.
Definition: WindowsManager.h:53
virtual bool IsAwaitTouch()
Returns true if window await touch action (like button) or false if touch manager should ignore this ...
Definition: Window.h:107
void Initialize()
Initialization procedure. Has to be call once.
Definition: WindowsManager.h:45
int Left()
Returns window left coordinate relative to the parent window.
Definition: Window.h:174
void Redraw(DC *dc)
Performs full window redraw.
Definition: Window.h:231
Window * Parent()
Returns Parent window.
Definition: Window.h:200
WindowsManager(UTFT *lcd, UTouch *touch=NULL)
Constructor.
Definition: WindowsManager.h:40
static Environment * Get()
Returns singltone instance of environment.
Definition: Environment.h:44
DC * GetDC()
Returns device context (interface class to UTFT library)
Definition: WindowsManager.h:92
LinkedList< Window > & Children()
Returns list of children window.
Definition: Window.h:223
Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing functio...
Definition: DC.h:29
int Height()
Returns window height.
Definition: Window.h:189
void Invalidate()
If function is called than the window manager updates the window.
Definition: Window.h:157
Definition: MainWindow.h:25
void redraw(Window *window, bool isForceRedraw)
Internal draw code.
Definition: WindowsManager.h:104
int DeviceWidth()
Returns screen width.
Definition: DC.h:62
bool IsVisible()
Returns true if window visible and false is hidden.
Definition: Window.h:211
void Idle()
If nothing happens, the touch event is checked.
Definition: WindowsManager.h:97
int Top()
Returns window top coordinate relative to the parent window.
Definition: Window.h:179
Window * HitTest(Window *window, int x, int y)
For defined window return topmost child window that lais under x,y screen coordinate.
Definition: WindowsManager.h:61
Main window manager. Implement core funcctionality of AWind library. It is tempate class that has to ...
Definition: WindowsManager.h:29
void loop()
Main loop where drawing code only for "dirty" window is called.
Definition: WindowsManager.h:78
virtual void OnTouching(DC *dc)
Touch manager calls this function in the loop as long as touch action proceeds.
Definition: Window.h:112
int DeviceHeight()
Returns screen height.
Definition: DC.h:67
void loopTouch()
Checks touch event.
Definition: WindowsManager.h:121
int Width()
Returns window width.
Definition: Window.h:184