28 |
#include "Interface/UiWidget.h" |
#include "Interface/UiWidget.h" |
29 |
|
|
30 |
/** |
/** |
31 |
* UiWidget implementation |
* UiWidget is an abstract class, defining widgets wich are elements |
32 |
|
* of the graphical user interface. |
33 |
*/ |
*/ |
34 |
@implementation UiWidget |
@implementation UiWidget |
35 |
|
|
36 |
/** |
/** |
37 |
* UiWidget initialisation |
* Initializes the widget with the given position and size. |
38 |
* @param (NSPoint)position the position of the widget |
* The position is relative to its container. |
39 |
* @param (NSSize)size the size of the widget |
* The widget must be completly contained in the screen space. |
40 |
*/ |
*/ |
41 |
- (id) initWithPosition: (NSPoint)position |
- (id) initWithPosition: (NSPoint)position |
42 |
size: (NSSize)size; |
size: (NSSize)size; |
43 |
{ |
{ |
44 |
NSParameterAssert(position.x >= 0.0f); |
NSParameterAssert (position.x >= 0.0f); |
45 |
NSParameterAssert(position.y >= 0.0f); |
NSParameterAssert (position.y >= 0.0f); |
46 |
NSParameterAssert(position.x < 1024.0f); |
NSParameterAssert (position.x < 1024.0f); |
47 |
NSParameterAssert(position.y < 768.0f); |
NSParameterAssert (position.y < 768.0f); |
48 |
NSParameterAssert(size.width >= 0.0f); |
NSParameterAssert (size.width >= 0.0f); |
49 |
NSParameterAssert(size.height >= 0.0F); |
NSParameterAssert (size.height >= 0.0F); |
50 |
NSParameterAssert((size.width + position.x) <= 1024.0f); |
NSParameterAssert (size.width + position.x <= 1024.0f); |
51 |
NSParameterAssert((size.height + position.y) <= 768.0f); |
NSParameterAssert (size.height + position.y <= 768.0f); |
52 |
|
|
53 |
self = [super init]; |
self = [super init]; |
54 |
if (self != nil) |
if (self != nil) |
55 |
{ |
{ |
56 |
_position = position; |
[self setPosition: position]; |
57 |
_size = size; |
[self setDepth: 0.0f]; |
58 |
_visible = YES; |
[self setSize: size]; |
59 |
_enabled = NO; |
[self setAlpha: 1.0f]; |
60 |
_focus = NO; |
[self setVisible: YES]; |
61 |
_layer = 1; |
[self setEnabled: YES]; |
62 |
|
[self setFocus: NO]; |
63 |
} |
} |
64 |
|
|
65 |
return self; |
return self; |
66 |
} |
} |
67 |
|
|
68 |
/** |
/** |
69 |
* Returns if a widget is visible or not |
* Set the widget delegate. |
70 |
|
* The delegate will receive all the actions emited by the widget |
71 |
|
* and serves as a controller. |
72 |
*/ |
*/ |
73 |
- (BOOL) isVisible |
- (void) setDelegate: (id)delegate |
74 |
{ |
{ |
75 |
return _visible; |
NSParameterAssert (delegate); |
76 |
|
|
77 |
|
ASSIGN(_delegate, delegate); |
78 |
} |
} |
79 |
|
|
80 |
/** |
/** |
81 |
* Sets if a widget is enabled or not |
* Returns the current widget delegate. |
|
* @param (BOOL)enabled the state of the widget if it is enabled or not |
|
82 |
*/ |
*/ |
83 |
- (void) setEnabled: (BOOL)enabled |
- (id) delegate |
84 |
{ |
{ |
85 |
_enabled = enabled; |
return _delegate; |
86 |
} |
} |
87 |
|
|
88 |
/** |
/** |
89 |
* Returns if a widget is enabled or not |
* Returns the frame of the widget. |
90 |
|
* The frame rectangle is defined by the current widget position and size. |
91 |
*/ |
*/ |
92 |
- (BOOL) isEnabled |
- (NSRect) frame |
93 |
{ |
{ |
94 |
return (_enabled); |
return _frame; |
95 |
} |
} |
96 |
|
|
97 |
/** |
/** |
98 |
* Sets if a widget has the focus or not |
* Sets the widget position. |
99 |
* @param (BOOL)focus |
* The position is relative to its container. |
100 |
|
* The widget must be completly contained in the screen space. |
101 |
*/ |
*/ |
102 |
- (void) setFocus: (BOOL)focus |
- (void) setPosition: (NSPoint)position; |
103 |
{ |
{ |
104 |
_focus = focus; |
NSParameterAssert (position.x >= 0.0f); |
105 |
|
NSParameterAssert (position.y >= 0.0f); |
106 |
|
NSParameterAssert (position.x + _frame.size.width <= 1024.0f); |
107 |
|
NSParameterAssert (position.y + _frame.size.height <= 768.0f); |
108 |
|
|
109 |
|
_frame.origin = position; |
110 |
} |
} |
111 |
|
|
112 |
/** |
/** |
113 |
* Returns if a widget has the focus or not |
* Returns the widget position. |
114 |
|
* The position is relative to its container. |
115 |
*/ |
*/ |
116 |
- (BOOL) hasFocus |
- (NSPoint) position |
117 |
{ |
{ |
118 |
return _focus; |
return _frame.origin; |
119 |
} |
} |
120 |
|
|
121 |
/** |
/** |
122 |
* Sets if the mouse is over the widget or not |
* Sets the widget size. |
123 |
* @param (BOOL)focus |
* The widget must be completly contained in the screen space. |
124 |
*/ |
*/ |
125 |
- (void) setMouseOver: (BOOL)mouseOver |
- (void) setSize: (NSSize)size |
126 |
{ |
{ |
127 |
_mouseOver = mouseOver; |
NSParameterAssert (size.width >= 0.0f); |
128 |
|
NSParameterAssert (size.height >= 0.0f); |
129 |
|
NSParameterAssert (size.width + _frame.origin.x <= 1024.0f); |
130 |
|
NSParameterAssert (size.height + _frame.origin.y <= 768.0f); |
131 |
|
|
132 |
|
_frame.size = size; |
133 |
} |
} |
134 |
|
|
135 |
/** |
/** |
136 |
* Returns the mouse is over the widget or not |
* Sets the size of the widget according to its content. |
137 |
|
* This method is abstract and must be redefined by subclasses. |
138 |
*/ |
*/ |
139 |
- (BOOL) mouseOver |
- (void) sizeToFit |
140 |
{ |
{ |
141 |
return _mouseOver; |
[self notImplemented: _cmd]; |
142 |
} |
} |
143 |
|
|
144 |
|
|
145 |
/** |
/** |
146 |
* Returns the horizontal position |
* Returns the widget size. |
147 |
*/ |
*/ |
148 |
- (float) x |
- (NSSize) size |
149 |
{ |
{ |
150 |
return _position.x; |
return _frame.size; |
151 |
} |
} |
152 |
|
|
153 |
/** |
/** |
154 |
* Returns the vertical position |
* Sets the widget depth. |
155 |
|
* The default depth is 0.0f, with negative values the widget will be behind |
156 |
|
* the default layer, and with positive value it will be in front of it. |
157 |
*/ |
*/ |
158 |
- (float) y |
- (void) setDepth: (float)depth |
159 |
{ |
{ |
160 |
return _position.y; |
_depth = depth; |
161 |
} |
} |
162 |
|
|
163 |
/** |
/** |
164 |
* Returns the position |
* Returns the widget depth. |
165 |
*/ |
*/ |
166 |
- (NSPoint) position |
- (float) depth |
167 |
{ |
{ |
168 |
return _position; |
return _depth; |
169 |
} |
} |
170 |
|
|
171 |
/** |
/** |
172 |
* Returns the height of the widget |
* Sets the widget alpha. |
173 |
|
* 1.0f is opaque, and 0.0f is transparent. |
174 |
|
* Default is opaque. |
175 |
*/ |
*/ |
176 |
- (float) height |
- (void) setAlpha: (float)alpha |
177 |
{ |
{ |
178 |
return _size.height; |
NSParameterAssert (alpha >= 0.0f); |
179 |
|
NSParameterAssert (alpha <= 1.0f); |
180 |
|
|
181 |
|
_alpha = alpha; |
182 |
} |
} |
183 |
|
|
184 |
/** |
/** |
185 |
* Returns the width of the widget |
* Returns the widget alpha. |
186 |
*/ |
*/ |
187 |
- (float) width |
- (float) alpha |
188 |
{ |
{ |
189 |
return _size.width; |
return _alpha; |
190 |
} |
} |
191 |
|
|
192 |
/** |
/** |
193 |
* Returns the size |
* Sets the widget visibility. |
194 |
|
* A widget is visible by default. |
195 |
*/ |
*/ |
196 |
- (NSSize) size |
- (void) setVisible: (BOOL)visible |
197 |
{ |
{ |
198 |
return _size; |
_visible = visible; |
199 |
} |
} |
200 |
|
|
201 |
/** |
/** |
202 |
* Returns the layer |
* Returns if a widget is visible or not. |
203 |
*/ |
*/ |
204 |
- (int) layer |
- (BOOL) isVisible |
205 |
{ |
{ |
206 |
return _layer; |
return _visible; |
207 |
} |
} |
208 |
|
|
209 |
/** |
/** |
210 |
* Sets the layer, layer shall be compared to a z buffer, we defined |
* Enable/Disable the widget. |
211 |
* that the first layer is 1 |
* A disabled widget is inactive and does not recieve events. |
|
* @param (int)layer |
|
212 |
*/ |
*/ |
213 |
- (void) setLayer: (int)layer |
- (void) setEnabled: (BOOL)enabled |
214 |
{ |
{ |
215 |
NSParameterAssert(layer > 0); |
_enabled = enabled; |
|
|
|
|
_layer = layer; |
|
216 |
} |
} |
217 |
|
|
218 |
/** |
/** |
219 |
* Sets the size of the widget |
* Returns if a widget is enabled or not |
|
* @param (NSSize)size |
|
220 |
*/ |
*/ |
221 |
- (void) setSize: (NSSize)size |
- (BOOL) isEnabled |
222 |
{ |
{ |
223 |
NSParameterAssert(size.width >= 0); |
return (_enabled); |
|
NSParameterAssert(size.height >= 0); |
|
|
NSParameterAssert((size.width + _position.x) <= 1024.0f); |
|
|
NSParameterAssert((size.height + _position.y) <= 768.0f); |
|
|
|
|
|
_size = size; |
|
224 |
} |
} |
225 |
|
|
226 |
/** |
/** |
227 |
* Display the widget on the screen and sets the boolean _visible to YES |
* Sets the widget focus. |
228 |
*/ |
*/ |
229 |
- (void) show |
- (void) setFocus: (BOOL)focus |
230 |
{ |
{ |
231 |
_visible = YES; |
_focus = focus; |
|
|
|
|
/* FIXME - call the display method */ |
|
232 |
} |
} |
233 |
|
|
234 |
/** |
/** |
235 |
* Hide the widget on the screen and sets the boolean _visible to NO |
* Returns if a widget has the focus or not. |
236 |
*/ |
*/ |
237 |
- (void) hide |
- (BOOL) hasFocus |
238 |
{ |
{ |
239 |
_visible = NO; |
return _focus; |
|
|
|
|
/* FIXME - call the display method to hide the widget */ |
|
240 |
} |
} |
241 |
|
|
242 |
/** |
/** |
243 |
* method who sets the size of the widget, it does nothing and must be |
* Abstract method with does the real rendering. |
244 |
* redefined in the widgets. |
* This method must be redefined by the subclasses. |
245 |
*/ |
*/ |
|
- (void) sizeToFit |
|
|
{ |
|
|
[self notImplemented: _cmd]; |
|
|
} |
|
|
|
|
246 |
- (void) _display |
- (void) _display |
247 |
{ |
{ |
248 |
[self notImplemented: _cmd]; |
[self notImplemented: _cmd]; |
249 |
} |
} |
250 |
|
|
251 |
|
/** |
252 |
|
* Display the widget using OpenGL. |
253 |
|
*/ |
254 |
- (void) display |
- (void) display |
255 |
{ |
{ |
256 |
if (_visible) |
if (_visible) |
257 |
{ |
{ |
258 |
glPushMatrix(); |
glPushMatrix(); |
259 |
glTranslatef (_position.x, _position.y, 0.0f); |
glTranslatef (_frame.origin.x, _frame.origin.y, 0.0f); |
260 |
|
|
261 |
[self _display]; |
[self _display]; |
262 |
|
|
264 |
} |
} |
265 |
} |
} |
266 |
|
|
|
/** |
|
|
* Set the delegate with an object |
|
|
*/ |
|
|
- (void) setDelegate: (id)delegate |
|
|
{ |
|
|
NSParameterAssert (delegate); |
|
|
|
|
|
ASSIGN(_delegate, delegate); |
|
|
} |
|
|
|
|
|
- (id) delegate |
|
|
{ |
|
|
return _delegate; |
|
|
} |
|
|
|
|
267 |
@end |
@end |