Gorgon Game Engine
Window.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Utils/Assert.h"
4 #include "../Event.h"
5 #include "../Main.h"
6 #include "../Layer.h"
7 #include "../Input/Layer.h"
8 #include "../Input/Keyboard.h"
9 #include "../Graphics/Layer.h"
10 #include "../Window.h"
11 #include "LayerAdapter.h"
12 
13 #include "WidgetContainer.h"
14 
15 #include <stdexcept>
16 
17 
18 namespace Gorgon { namespace UI {
19 
24  class Window : public Gorgon::Window, public Runner, public WidgetContainer {
25  public:
27 
28  Window() : Gorgon::Window() {
29  }
30 
31  ~Window() {
32  KeyEvent.Unregister(inputtoken);
33  CharacterEvent.Unregister(chartoken);
34  delete extenderlayer;
35  }
36 
37  Window(Window &&other) : Gorgon::Window(std::move(other)), WidgetContainer(std::move(other)) {
38  KeyEvent.Unregister(inputtoken);
39  CharacterEvent.Unregister(chartoken);
40 
41  inputtoken = keyinit();
42  chartoken = charinit();
43 
44  delete extenderlayer;
45  extenderlayer = other.extenderlayer;
46  Add(extenderlayer);
47  other.extenderlayer = other.layerinit();
48  }
49 
50  Window &operator=(Window &&other) {
51  other.KeyEvent.Unregister(other.inputtoken);
52  other.CharacterEvent.Unregister(other.chartoken);
53 
54  KeyEvent.Unregister(inputtoken);
55  CharacterEvent.Unregister(chartoken);
56 
57  Gorgon::Window::operator =(std::move(other));
58  WidgetContainer::operator =(std::move(other));
59 
60 
61  inputtoken = keyinit();
62  chartoken = charinit();
63 
64  delete extenderlayer;
65  extenderlayer = other.extenderlayer;
66  Add(extenderlayer);
67  adapter.SetLayer(*extenderlayer);
68  other.extenderlayer = other.layerinit();
69  other.Add(other.extenderlayer);
70  other.adapter.SetLayer(*other.extenderlayer);
71 
72  return *this;
73  }
74 
75  virtual Geometry::Size GetInteriorSize() const override {
76  return Gorgon::Window::GetSize();
77  }
78 
79  virtual bool IsVisible() const override {
81  }
82 
86  virtual void Quit() override {
87  quiting = true;
88  }
89 
90  virtual void Run() override {
91  while(!quiting) {
93  }
94 
96  }
97 
98  virtual void Step() override {
100  }
101 
103  bool EnsureVisible(const UI::Widget &) override {
104  return true;
105  }
106 
107  virtual ExtenderRequestResponse RequestExtender(const Gorgon::Layer &self) override {
108  return {true, &adapter, self.TranslateToTopLevel(), GetSize()};
109  }
110 
111  using WidgetContainer::Add;
112  using Gorgon::Window::Add;
115 
116  protected:
117  virtual Gorgon::Layer &getlayer() override {
118  return *this;
119  }
120 
121  virtual bool ResizeInterior(Geometry::Size size) override {
123 
124  return GetSize() == size;
125  }
126 
127  decltype(KeyEvent)::Token keyinit() {
128  inputtoken = KeyEvent.Register([this](Input::Key key, float amount) {
129  return WidgetContainer::KeyEvent(key, amount);
130  });
131 
132  KeyEvent.NewHandler = [this]{
133  RegisterOnce([this] {
134  this->KeyEvent.MoveToTop(this->inputtoken);
135  });
136  };
137 
138  return inputtoken;
139  }
140 
141  decltype(CharacterEvent)::Token charinit() {
142  chartoken = CharacterEvent.Register([this](Char c) {
144  });
145 
146  CharacterEvent.NewHandler = [this]{
147  RegisterOnce([this] {
148  this->CharacterEvent.MoveToTop(this->chartoken);
149  });
150  };
151 
152  return chartoken;
153  }
154 
156  auto l = new Graphics::Layer;
157  Add(l);
158  adapter.SetLayer(*l);
159  return l;
160  }
161 
162  void added(Layer &l) override {
163  long ind = children.FindLocation(extenderlayer);
164  if(ind != -1) {
165  children.MoveBefore(ind, children.GetSize());
166  }
167 
169  }
170 
171  private:
172  bool quiting = false;
173  LayerAdapter adapter;
174  Graphics::Layer *extenderlayer = layerinit();
175 
176  decltype(KeyEvent)::Token inputtoken = keyinit(); //to initialize token after window got constructed
177  decltype(CharacterEvent)::Token chartoken = charinit(); //to initialize token after window got constructed
178  };
179 
180 } }
181 
Gorgon::UI::Window::charinit
decltype(CharacterEvent) ::Token charinit()
Definition: Window.h:141
Gorgon::UI::Window::~Window
~Window()
Definition: Window.h:31
Gorgon::UI::WidgetContainer::Add
bool Add(Widget &widget)
Adds the given widget to this container.
Definition: WidgetContainer.cpp:5
Gorgon::Window
This class represents a window.
Definition: Window.h:31
Gorgon::UI::Window::Run
virtual void Run() override
Takes the control of the execution until Quit is called.
Definition: Window.h:90
Gorgon::Window::IsVisible
virtual bool IsVisible() const override
Returns whether this layer is effectively visible.
Definition: Window.h:229
Gorgon::Layer::Add
void Add(Layer &layer)
Adds the given layer as a child.
Definition: Layer.cpp:23
Gorgon::UI::WidgetContainer::CharacterEvent
virtual bool CharacterEvent(Char c)
This function should be called whenever a character is received from operating system.
Definition: WidgetContainer.h:388
Gorgon::Window::Window
Window()
Empty constructor creates a non-initialized window.
Definition: Window.h:45
Gorgon::UI::Window::EnsureVisible
bool EnsureVisible(const UI::Widget &) override
Window does not do any scrolling, thus cannot ensure visibility.
Definition: Window.h:103
Gorgon::Window::CharacterEvent
ConsumableEvent< Window, Input::Keyboard::Char > CharacterEvent
Called when a character is received.
Definition: Window.h:397
Gorgon::UI::Window::operator=
Window & operator=(Window &&other)
Definition: Window.h:50
Gorgon::UI::Window::IsVisible
virtual bool IsVisible() const override
Returns whether this layer is effectively visible.
Definition: Window.h:79
Gorgon::NextFrame
void NextFrame()
This function marks the end of current frame and starts the next one.
Definition: Main.cpp:115
Gorgon::UI::Window::Add
bool Add(Widget &widget)
Adds the given widget to this container.
Definition: WidgetContainer.cpp:5
Gorgon::UI::WidgetContainer::KeyEvent
virtual bool KeyEvent(Input::Key key, float state)
This function should be called whenever a key is pressed or released.
Definition: WidgetContainer.h:384
Gorgon::Window::operator=
Window & operator=(Window &&other)
Moves another window into this one.
Definition: Window.h:141
Gorgon::UI::Window::GetInteriorSize
virtual Geometry::Size GetInteriorSize() const override
Should return the interior (usable) size of the container.
Definition: Window.h:75
Gorgon::Graphics::Layer
This layer allows drawing texture images on.
Definition: Layer.h:169
Gorgon::Runner
Defines the abstract class of Runner.
Definition: Main.h:21
Gorgon::UI::LayerAdapter::SetLayer
void SetLayer(Gorgon::Layer &value)
Definition: LayerAdapter.h:47
Gorgon::Input::Key
int Key
A type to represent an input key.
Definition: Input.h:14
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Char
uint32_t Char
Definition: Types.h:46
Gorgon::UI::LayerAdapter
This class turns a layer into a widget container.
Definition: LayerAdapter.h:12
Gorgon::UI::Window::layerinit
Graphics::Layer * layerinit()
Definition: Window.h:155
Gorgon::UI::Window::Window
Window(Window &&other)
Definition: Window.h:37
Gorgon::Window::Resize
virtual void Resize(const Geometry::Size &size) override
Resizes the window to the given size.
Definition: Window.cpp:323
Gorgon::UI::Window::getlayer
virtual Gorgon::Layer & getlayer() override
Returns the layer that will be used to place the contained widgets.
Definition: Window.h:117
Gorgon::UI::Window::keyinit
decltype(KeyEvent) ::Token keyinit()
Definition: Window.h:127
Gorgon::UI::ExtenderRequestResponse
This structure is used to transfer extender request response.
Definition: WidgetContainer.h:15
Gorgon::UI::Window::Quit
virtual void Quit() override
Closes the window, returning the execution to the point where Run function is called.
Definition: Window.h:86
Gorgon::UI::Window::ResizeInterior
virtual bool ResizeInterior(Geometry::Size size) override
Should resize the interior (usable) size of the container.
Definition: Window.h:121
Gorgon::Layer
This class is the base class for all layer types.
Definition: Layer.h:79
LayerAdapter.h
Gorgon::UI::Window::added
void added(Layer &l) override
Will be called when a layer is added.
Definition: Window.h:162
Gorgon::UI::Window::Step
virtual void Step() override
Runs a single frame.
Definition: Window.h:98
Gorgon::Window::added
virtual void added(Layer &layer) override
Will be called when a layer is added.
Definition: Window.cpp:216
Gorgon::UI::WidgetContainer
This class is the base class for all widget containers.
Definition: WidgetContainer.h:37
WidgetContainer.h
Gorgon::UI::Window
UI window allows programmers to create an OS window that will accept widgets and has the ability to r...
Definition: Window.h:24
Gorgon::RegisterOnce
void RegisterOnce(std::function< void()> fn)
Registers a function to be run at the start of the next frame.
Definition: Main.cpp:142
Gorgon::UI::Window::RequestExtender
virtual ExtenderRequestResponse RequestExtender(const Gorgon::Layer &self) override
This function will return a container that will act as an extender.
Definition: Window.h:107
Gorgon::UI::Window::Window
Window()
Definition: Window.h:28
Gorgon::Layer::children
Containers::Collection< Layer > children
Child layers that this layer holds, all child layers are considered to be above current layer.
Definition: Layer.h:501
Gorgon::Window::Close
void Close()
Closes the window. After this function, any use of this object might fail.
Definition: Window.cpp:340
Gorgon::UI::WidgetContainer::operator=
WidgetContainer & operator=(WidgetContainer &&)=default
Gorgon::Window::KeyEvent
ConsumableEvent< Window, Input::Key, float > KeyEvent
Called when a key is pressed or released.
Definition: Window.h:392
Gorgon::Layer::GetSize
Geometry::Size GetSize() const
Returns the size of the layer.
Definition: Layer.h:362
Gorgon::UI::Widget
This class is the base for all widgets.
Definition: Widget.h:16