-
Notifications
You must be signed in to change notification settings - Fork 2
/
uicore_BaseObject.h
495 lines (413 loc) · 14.8 KB
/
uicore_BaseObject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Copyright (C) 2007 Benjamin Litzelmann
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _UICORE_BASEOBJECT_H_
#define _UICORE_BASEOBJECT_H_
#include "uicore_Types.h"
namespace UICore
{
typedef enum
{
BORDER_TOP_LEFT,
BORDER_TOP,
BORDER_TOP_RIGHT,
BORDER_RIGHT,
BORDER_BOTTOM_RIGHT,
BORDER_BOTTOM,
BORDER_BOTTOM_LEFT,
BORDER_LEFT,
BORDER_MAX_BORDERS
} BorderImage;
class BaseObject
{
protected:
Rect position;
bool visible;
bool enabled;
bool draggable;
bool focusable;
bool clickable;
bool clickthrough;
unsigned int lastclick;
Color backColor;
Color borderColor;
Color disabledColor;
Image backgroundImage;
Image disabledImage;
float borderWidth;
void *userData;
float rotation;
Image borderImages[BORDER_MAX_BORDERS]; // border images starting from top left and going clockwise
int index, zIndex;
bool sort_children;
BaseObject *parent;
BaseObject *prevObject;
BaseObject *nextObject;
std::list<BaseObject*> children;
std::list<BaseObject*> sorted_children;
/** shift state is globally stored in order to properly do the backward keyboard nav (shift + tab) */
static bool shiftPressed;
virtual void BaseMouseDown( float x, float y, int button );
virtual void BaseMouseUp( float x, float y, int button );
virtual void BaseMouseMove( float x, float y, float xrel, float yrel );
virtual void BaseMouseIn( BaseObject *oldObject );
virtual void BaseMouseOut( BaseObject *newObject );
virtual void BaseLostFocus( void );
virtual void BaseKeyDown( int key, int charVal );
virtual void BaseKeyUp( int key, int charVal );
virtual void DoClick( void );
void (*AfterDraw)( BaseObject *target, unsigned int deltatime, const Rect *parentPos, const Rect *parentPosSrc, bool enabled );
void (*Click)( BaseObject *target );
void (*DoubleClick)( BaseObject *target );
void (*MouseDown)( BaseObject *target, float x, float y, int button );
void (*MouseUp)( BaseObject *target, float x, float y, int button );
void (*MouseMove)( BaseObject *target, float x, float y, float xrel, float yrel );
void (*MouseIn)( BaseObject *target, BaseObject *oldObject );
void (*MouseOut)( BaseObject *target, BaseObject *newObject );
void (*GotFocus) (BaseObject *target );
void (*LostFocus) (BaseObject *target );
void (*Dragging)( BaseObject *target, float xrel, float yrel );
void (*KeyDown)( BaseObject *target, int key, int charVal );
void (*KeyUp)( BaseObject *target, int key, int charVal );
bool RemoveChild( BaseObject *child );
virtual void AddChild( BaseObject *child );
void setFocusable( bool v );
virtual void setSortChildren( bool r );
virtual void sortChildren( void );
virtual void Initialize();
/** Set the actual position of this object according to its parent's one
@param parentPos a rectangle representing actual parent position and size in absolute screen coordinates.
This parameter can be NULL, and then posSrc is set to 0, 0, w, h and posDest to x, y, w, h
@param parentPosSrc a rectangle representing the part of the parent object, in its local coordinates,
that is going to be displayed. If parentPos is NULL, this parameter is not used. If this parameter is NULL,
it is considered that the whole parent object will be displayed
@param posSrc a rectangle instance on which will be written the part of the object that is going to be displayed,
in local object coordinates (if posSrc is 0, 0, w, h, then the whole object is going to be displayed)
@param posDest a rectangle instance on which will be written the position and size in absolute screen coordinates
on which the object will be displayed */
void FitPosition( const Rect *parentPos, const Rect *parentPosSrc, Rect &posSrc, Rect &posDest );
virtual void DrawBackground( const Rect &posSrc, const Rect &posDest, bool enabled = true );
virtual void DrawBorder( const Rect &posSrc, const Rect &posDest, bool enabled = true );
virtual void DrawChildren( unsigned int deltatime, const Rect &posSrc, const Rect &posDest, bool enabled = true );
public:
BaseObject();
BaseObject( BaseObject *parent, float x, float y, float w, float h );
virtual ~BaseObject();
void deleteChildren( void );
MEMORY_OPERATORS
virtual void Draw( unsigned int deltatime, const Rect *parentPos = NULL, const Rect *parentPosSrc = NULL, bool enabled = true );
/** Returns true if this object contains the passed object */
bool Contains( BaseObject *obj );
/** Returns an iterator set at the first child of the object. @Remark Using this function is UNSAFE */
inline std::list<BaseObject*>::iterator getFirstChildIterator( void );
/** Returns an iterator set just after the last child of the object. @Remark Using this function is UNSAFE */
inline std::list<BaseObject*>::iterator getEndChildIterator( void );
static bool compareChildren( BaseObject *c1, BaseObject *c2 );
void setParent( BaseObject *obj );
BaseObject *getParent( void );
#if 0
void PutOnTop( BaseObject *obj );
void PutOnBottom( BaseObject *obj );
#endif
virtual void setFocus( void );
inline virtual void setVisible( bool v );
inline virtual void setEnabled( bool v );
inline virtual void setDraggable( bool v );
inline virtual void setClickable( bool v );
inline virtual void setClickThrough( bool v );
inline virtual void setPosition( const Rect &pos );
inline virtual void setRotation( float angle );
inline virtual void setPosition( float x, float y );
inline virtual void setSize( float w, float h );
inline virtual void setBackColor( const Color &color );
inline virtual void setBorderColor( const Color &color );
inline virtual void setDisabledColor( const Color &color );
inline virtual void setBackgroundImage( Image image );
inline virtual void setDisabledImage( Image image );
inline virtual void setBorderWidth( const float width );
inline virtual void setBorderImages( const Image *images );
inline virtual void setZIndex( int z );
inline virtual void setUserData(void *data);
inline virtual bool isVisible( void ) const;
inline virtual bool isEnabled( void ) const;
inline virtual bool isDraggable( void ) const;
inline virtual bool isClickable( void ) const;
inline virtual Rect getPosition( void ) const;
inline virtual void getPosition( float &x, float &y ) const;
inline virtual float getPositionX( void ) const;
inline virtual float getPositionY( void ) const;
inline virtual void getSize( float &w, float &h ) const;
inline virtual float getWidth( void ) const;
inline virtual float getHeight( void ) const;
inline virtual Color getBackColor( void ) const;
inline virtual Color getBorderColor( void ) const;
inline virtual Color getDisabledColor( void ) const;
inline virtual Image getBackgroundImage( void ) const;
inline virtual Image getDisabledImage( void ) const;
inline virtual float getBorderWidth( void ) const;
inline virtual const Image *getBorderImages( void ) const;
inline virtual int getZIndex( void ) const;
inline virtual void *getUserData(void);
virtual std::string getType( void ) const = 0;
inline virtual void setAfterDrawHandler( void (*AfterDraw)( BaseObject *target, unsigned int deltatime, const Rect *parentPos, const Rect *parentPosSrc, bool enabled ) );
inline virtual void setClickHandler( void (*Click)( BaseObject *target ) );
inline virtual void setDoubleClickHandler( void (*DoubleClick)( BaseObject *target ) );
inline virtual void setMouseDownHandler( void (*MouseDown)( BaseObject *target, float x, float y, int button ) );
inline virtual void setMouseUpHandler( void (*MouseUp)( BaseObject *target, float x, float y, int button ) );
inline virtual void setMouseMoveHandler( void (*MouseMove)( BaseObject *target, float x, float y, float xrel, float yrel ) );
inline virtual void setMouseInHandler( void (*MouseIn)( BaseObject *target, BaseObject *oldObject ) );
inline virtual void setMouseOutHandler( void (*MouseOut)( BaseObject *target, BaseObject *newObject ) );
inline virtual void setGotFocusHandler( void (*GotFocus)( BaseObject *target ) );
inline virtual void setLostFocusHandler( void (*LostFocus)( BaseObject *target ) );
inline virtual void setDraggingHandler( void (*Dragging)( BaseObject *target, float xrel, float yrel ) );
inline virtual void setKeyDownHandler( void (*KeyDown)( BaseObject *target, int key, int charVal ) );
inline virtual void setKeyUpHandler( void (*KeyUp)( BaseObject *target, int key, int charVal ) );
};
extern BaseObject *draggingObject;
extern BaseObject *mouseOverObject;
extern BaseObject *focusedObject;
std::list<BaseObject*>::iterator BaseObject::getFirstChildIterator( void )
{
return children.begin();
}
std::list<BaseObject*>::iterator BaseObject::getEndChildIterator( void )
{
return children.end();
}
void BaseObject::setVisible( bool v )
{
visible = v;
if ( !v && focusedObject && Contains( focusedObject ) )
focusedObject = NULL;
}
void BaseObject::setEnabled( bool v )
{
enabled = v;
if ( !v && focusedObject == this )
focusedObject = NULL;
}
void BaseObject::setDraggable( bool v )
{
draggable = v;
}
void BaseObject::setClickable( bool v )
{
clickable = v;
}
void BaseObject::setClickThrough( bool v )
{
clickthrough = v;
}
void BaseObject::setPosition( const Rect &pos )
{
position = pos;
}
void BaseObject::setPosition( float x, float y )
{
position.x = x;
position.y = y;
}
void BaseObject::setRotation( float angle )
{
rotation = angle;
}
void BaseObject::setSize( float w, float h )
{
position.w = w;
position.h = h;
}
void BaseObject::setBackColor( const Color &color )
{
backColor = color;
}
void BaseObject::setBorderColor( const Color &color )
{
borderColor = color;
}
void BaseObject::setDisabledColor( const Color &color )
{
disabledColor = color;
}
void BaseObject::setBackgroundImage( Image image )
{
backgroundImage = image;
}
void BaseObject::setDisabledImage( Image image )
{
disabledImage = image;
}
void BaseObject::setBorderWidth( float width )
{
borderWidth = width;
}
void BaseObject::setBorderImages( const Image *images )
{
int i;
for( i = 0; i < BORDER_MAX_BORDERS; i++ )
borderImages[i] = NULL;
if( !images )
return;
for( i = 0; i < BORDER_MAX_BORDERS; i++ )
borderImages[i] = images[i];
}
void BaseObject::setZIndex( int z )
{
zIndex = z;
if ( parent )
parent->setSortChildren( true );
}
void BaseObject::setUserData(void *data)
{
userData = data;
}
void *BaseObject::getUserData(void)
{
return userData;
}
bool BaseObject::isVisible( void ) const
{
return visible;
}
bool BaseObject::isEnabled( void ) const
{
if( parent && !parent->isEnabled() )
return false;
return enabled;
}
bool BaseObject::isDraggable( void ) const
{
return draggable;
}
bool BaseObject::isClickable( void ) const
{
if( parent && !parent->isClickable() )
return false;
return clickable;
}
Rect BaseObject::getPosition( void ) const
{
return position;
}
float BaseObject::getPositionX( void ) const
{
return position.x;
}
float BaseObject::getPositionY( void ) const
{
return position.y;
}
void BaseObject::getPosition( float &x, float &y ) const
{
x = position.x;
y = position.y;
}
void BaseObject::getSize( float &w, float &h ) const
{
w = position.w;
h = position.h;
}
float BaseObject::getWidth( void ) const
{
return position.w;
}
float BaseObject::getHeight( void ) const
{
return position.h;
}
Color BaseObject::getBackColor( void ) const
{
return backColor;
}
Color BaseObject::getBorderColor( void ) const
{
return borderColor;
}
Color BaseObject::getDisabledColor( void ) const
{
return disabledColor;
}
Image BaseObject::getBackgroundImage( void ) const
{
return backgroundImage;
}
Image BaseObject::getDisabledImage( void ) const
{
return disabledImage;
}
float BaseObject::getBorderWidth( void ) const
{
return borderWidth;
}
const Image *BaseObject::getBorderImages( void ) const
{
return borderImages;
}
int BaseObject::getZIndex( void ) const
{
return zIndex;
}
void BaseObject::setAfterDrawHandler( void (*AfterDraw)( BaseObject *target, unsigned int deltatime, const Rect *parentPos, const Rect *parentPosSrc, bool enabled ) )
{
this->AfterDraw = AfterDraw;
}
void BaseObject::setClickHandler( void (*Click)( BaseObject *target ) )
{
this->Click = Click;
}
void BaseObject::setDoubleClickHandler( void (*DoubleClick)( BaseObject *target ) )
{
this->DoubleClick = DoubleClick;
}
void BaseObject::setMouseDownHandler( void (*MouseDown)( BaseObject *target, float x, float y, int button ) )
{
this->MouseDown = MouseDown;
}
void BaseObject::setMouseUpHandler( void (*MouseUp)( BaseObject *target, float x, float y, int button ) )
{
this->MouseUp = MouseUp;
}
void BaseObject::setMouseMoveHandler( void (*MouseMove)( BaseObject *target, float x, float y, float xrel, float yrel ) )
{
this->MouseMove = MouseMove;
}
void BaseObject::setMouseInHandler( void (*MouseIn)( BaseObject *target, BaseObject *oldObject ) )
{
this->MouseIn = MouseIn;
}
void BaseObject::setMouseOutHandler( void (*MouseOut)( BaseObject *target, BaseObject *newObject ) )
{
this->MouseOut = MouseOut;
}
void BaseObject::setGotFocusHandler( void (*GotFocus)( BaseObject *target ) )
{
this->GotFocus = GotFocus;
}
void BaseObject::setLostFocusHandler( void (*LostFocus)( BaseObject *target ) )
{
this->LostFocus = LostFocus;
}
void BaseObject::setDraggingHandler( void (*Dragging)( BaseObject *target, float xrel, float yrel ) )
{
this->Dragging = Dragging;
}
void BaseObject::setKeyDownHandler( void (*KeyDown)( BaseObject *target, int key, int charVal ) )
{
this->KeyDown = KeyDown;
}
void BaseObject::setKeyUpHandler( void (*KeyUp)( BaseObject *target, int key, int charVal ) )
{
this->KeyUp = KeyUp;
}
}
#endif