Neo  0.5.0
Developer Documentation
Widget.h
Go to the documentation of this file.
1 /*
2  * Copyright 2014 (C) Yannick Pflanzer <neo-engine.de>
3  *
4  * This file is part of Neo2D.
5  *
6  * Neo2D is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Neo2D is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with Neo2D. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Diese Datei ist Teil von Neo2D.
20  *
21  * Neo2D ist Freie Software: Sie können es unter den Bedingungen
22  * der GNU Lesser General Public License, wie von der Free Software Foundation,
23  * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
24  * veröffentlichten Version, weiterverbreiten und/oder modifizieren.
25  *
26  * Neo2D wird in der Hoffnung, dass es nützlich sein wird, aber
27  * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
28  * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
29  * Siehe die GNU Lesser General Public License für weitere Details.
30  *
31  * Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit diesem
32  * Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
33  */
34 
35 #ifndef __WIDGET_H__
36 #define __WIDGET_H__
37 
38 #include <NeoEngine.h>
39 #include <Neo2D.h>
40 #include <string>
41 #include <functional>
42 
43 namespace Neo2D
44 {
45 
46 using namespace Neo;
47 
48 // Predefinition
49 #ifndef SWIG
50 class Widget;
51 #endif
52 
53 //#if !defined(WIN32)
57  //typedef void (*CALLBACK_FUNCTION)(Widget*, long int);
58 //#elif !defined(SWIG)
59 //#define CALLBACK_FUNCTION void *
60 //#endif
61 
62 typedef std::function<void (Widget*, long int)> NEO_CALLBACK_FUNCTION;
63 
72 class NEO2D_EXPORT Widget
73 {
74 protected:
76  float m_x, m_y;
77 
79  float m_rotation;
80 
82  unsigned int m_width, m_height;
83 
85  std::string m_label;
86 
89 
90 #ifndef SWIG
91  NEO_CALLBACK_FUNCTION m_callback;
93 #endif
94 
96 
98  long int m_userData;
99 
101  bool m_visible;
102 
105 
108  float m_fontSize;
109 
111 
112 private:
119  static void doNothing(Widget* w, long data) {}
120 
121  static void callScript(Widget* w, long data)
122  {
125  }
126 
127 public:
128  Widget(unsigned int x, unsigned int y, unsigned int width,
129  unsigned int height, const char* label);
130  Widget();
131 
132  void setParent(Widget* w) { m_parent = w; }
133  Widget* getParent() { return m_parent; }
134 
135  float getFontSize() { return m_fontSize; }
136  void setFontSize(float s) { m_fontSize = s; }
137 
138  void setOffset(Vector2 offset) { m_offset = offset; }
139  Vector2 getOffset() { return m_offset; }
140 
141  void setSize(unsigned int w, unsigned int h) { m_width = w; m_height = h; }
142  Vector2 getSize() { return Vector2(m_width, m_height); }
143 
149  virtual void draw() { draw(Vector2(0, 0)); }
150 
158  virtual void draw(Vector2 offset) { m_offset = offset; }
159 
163  virtual void update() = 0;
164 
178 #ifndef SWIG
179  void setCallback(NEO_CALLBACK_FUNCTION func) { m_callback = func; }
180 #endif
181 
182  void setScriptCallback(const char* name)
183  {
184  m_callbackScript = name;
185  m_callback = Widget::callScript;
186  }
187 
199 #ifndef SWIG
200  void setCallback(NEO_CALLBACK_FUNCTION func, long int data)
201  {
202  m_callback = func;
203  m_userData = data;
204  }
205 #endif
206 
210  long int getUserData() { return m_userData; }
211 
216  void setUserData(long int data) { m_userData = data; }
217 
222  const char* getLabel() { return m_label.c_str(); }
223 
228  void setLabel(const char* l) { m_label = l; }
229 
235  {
236  m_x = pos.x;
237  m_y = pos.y;
238  }
239 
244  Vector2 getPosition() { return Vector2(m_x, m_y); }
245 
250  void setRotation(float rot) { m_rotation = rot; }
251 
256  float getRotation() { return m_rotation; }
257 
262  void translate(Vector2 vec)
263  {
264  m_x += vec.x;
265  m_y += vec.y;
266  }
267 
272  void rotate(float value) { m_rotation += value; }
273 
277  void doCallback();
278 
283  bool isVisible() { return m_visible; }
284 
289  void setVisible(bool v) { m_visible = v; }
294  void setScale(Vector2 scale) { m_scale = scale; }
295  Vector2 getScale() { return m_scale; }
299  void setFlip(Vector2 flip) { m_flip = flip; }
300  Vector2 getFlip() { return m_flip; }
301 
302  // For runtime identification
303  const char* getStaticName() { return "Widget"; }
304 
305  virtual bool isMouseOver();
306 };
307 }
308 #endif
void setCallback(NEO_CALLBACK_FUNCTION func)
Sets the callback.
Definition: Widget.h:179
long int m_userData
Some userdata that is given to the callback.
Definition: Widget.h:98
Vector2 getFlip()
Definition: Widget.h:300
void setPosition(Vector2 pos)
Changes the widget position.
Definition: Widget.h:234
std::function< void(Widget *, long int)> NEO_CALLBACK_FUNCTION
Definition: Widget.h:50
void setRotation(float rot)
Changes the widget rotation.
Definition: Widget.h:250
static NeoEngine * getInstance(void)
std::string m_label
The label of the widget.
Definition: Widget.h:85
Vector2 m_flip
The flip vector.
Definition: Widget.h:107
void setFontSize(float s)
Definition: Widget.h:136
String m_callbackScript
Definition: Widget.h:95
float y
Definition: Vector2.h:36
Widget * m_parent
Definition: Widget.h:110
Implements a framework for string manipulation that operates directly on C strings.
Definition: NeoString.h:35
long int getUserData()
Retrieves the user data that will be given to every callback call.
Definition: Widget.h:210
float x
Definition: Vector2.h:35
float getRotation()
Retrieves the current rotation.
Definition: Widget.h:256
void setUserData(long int data)
Changes the user data that will be given to every callback call.
Definition: Widget.h:216
The Widget class contains all information that is common to all GUI widgets.
Definition: Widget.h:72
Vector2 getPosition()
Retrieves the current position.
Definition: Widget.h:244
void setLabel(const char *l)
Changes the current label of the widget.
Definition: Widget.h:228
bool m_visible
Is the widget visible?
Definition: Widget.h:101
float getFontSize()
Definition: Widget.h:135
void setFlip(Vector2 flip)
setFlip Flip this Widget
Definition: Widget.h:299
void translate(Vector2 vec)
Translates the object.
Definition: Widget.h:262
float m_fontSize
Definition: Widget.h:108
void setScriptCallback(const char *name)
Definition: Widget.h:182
bool isVisible()
Returns if the widget is turned visible.
Definition: Widget.h:283
const char * getStaticName()
Definition: Widget.h:303
Definition: Vector2.h:31
Vector2 getScale()
Definition: Widget.h:295
Definition: Button.h:42
const char * getSafeString(void)
Returns the C string. This method is ensured to return a valid C string and never NULL...
Widget * getParent()
Definition: Widget.h:133
Vector2 m_offset
The offset that was used for rendering in the last frame.
Definition: Widget.h:88
virtual void draw()
Draws the widget to the canvas it belongs to.
Definition: Widget.h:149
virtual void draw(Vector2 offset)
Draws the widget to the canvas it belongs to.
Definition: Widget.h:158
virtual void callFunction(const char *name)=0
void setScale(Vector2 scale)
setScale Scale this Widget
Definition: Widget.h:294
float m_rotation
The rotation of the widget.
Definition: Widget.h:79
float m_y
Definition: Widget.h:76
Vector2 m_scale
The scale vector.
Definition: Widget.h:104
void setVisible(bool v)
Sets the visibility status.
Definition: Widget.h:289
const char * getLabel()
Gets the currently displayed label a C string.
Definition: Widget.h:222
void setCallback(NEO_CALLBACK_FUNCTION func, long int data)
Sets the callback and appends user data to it.
Definition: Widget.h:200
Definition: Color.h:29
unsigned int m_width
The width and height of the widget.
Definition: Widget.h:82
void setSize(unsigned int w, unsigned int h)
Definition: Widget.h:141
Class used to manage script functions virtually.
Definition: ScriptContext.h:32
Vector2 getOffset()
Definition: Widget.h:139
void setParent(Widget *w)
Definition: Widget.h:132
void setOffset(Vector2 offset)
Definition: Widget.h:138
Vector2 getSize()
Definition: Widget.h:142
void rotate(float value)
Rotates the object.
Definition: Widget.h:272
ScriptContext * getScriptContext(void)
Definition: NeoEngine.h:227