Repository navigation
Expand file tree
/
Copy pathglWidget.h
More file actions
362 lines (293 loc) · 8.78 KB
/
Copy pathglWidget.h
File metadata and controls
362 lines (293 loc) · 8.78 KB
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
/*
* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __GL_WIDGET_H__
#define __GL_WIDGET_H__
#include <stdint.h>
#include <stdio.h>
#include <vector>
// forward declarations
class glDisplay;
/**
* OpenGL graphics widget for rendering moveable/resizable shapes.
* @ingroup OpenGL
*/
class glWidget
{
public:
/**
* Shape enum
*/
enum Shape
{
Rect,
Line,
Ellipse
};
/**
* Constructor
*/
glWidget( Shape shape=Rect );
/**
* Constructor
*/
glWidget( float x, float y, float width, float height, Shape shape=Rect );
/**
* Destructor
*/
virtual ~glWidget();
/**
* Test if the point is inside the widget
*/
bool Contains( float x, float y ) const;
/**
* Move the widget's position by the specified offset
*/
inline void Move( float x, float y ) { mX += x; mY += y; }
/**
* Get the widget's X coordinate
*/
inline float X() const { return mX; }
/**
* Get the widget's Y coordinate
*/
inline float Y() const { return mY; }
/**
* Set the widget's X coordinate
*/
inline void SetX( float x ) { mX = x; }
/**
* Set the widget's Y coordinate
*/
inline void SetY( float y ) { mY = y; }
/**
* Get the widget's width
*/
inline float Width() const { return mWidth; }
/**
* Get the widget's height
*/
inline float Height() const { return mHeight; }
/**
* Set the widget's width
*/
inline void SetWidth( float width ) { mWidth = width; }
/**
* Set the widget's height
*/
inline void SetHeight( float height ) { mHeight = height; }
/**
* Get position of widget in global window coordinates
*/
inline void GetPosition( float* x, float* y ) const { if(x) *x=mX; if(y) *y=mY; }
/**
* Set position of widget in global window coordinates
*/
inline void SetPosition( float x, float y ) { mX = x; mY = y; }
/**
* Get the bounding coordinates of the widget
*/
inline void GetCoords( float* x1, float* y1, float* x2, float* y2 ) const { if(x1) *x1=mX; if(y1) *y1=mY; if(x2) *x2=mX+mWidth; if(y2) *y2=mY+mHeight; }
/**
* Set the bounding coordinates of the widget
*/
inline void SetCoords( float x1, float y1, float x2, float y2 ) { mX = x1; mY = y1; mWidth=x2-x1; mHeight=y2-y1; }
/**
* Get the widget's size
*/
inline void GetSize( float* width, float* height ) const { if(width) *width=mWidth; if(height) *height=mHeight; }
/**
* Set the widget's size
*/
inline void SetSize( float width, float height ) { mWidth = width; mHeight = height; }
/**
* Get the shape
*/
inline Shape GetShape() const { return mShape; }
/**
* Set the shape
*/
inline void SetShape( Shape shape ) { mShape = shape; }
/**
* Set fill alpha
*/
inline void SetFillAlpha( float a ) { mFillColor[3] = a; }
/**
* Set outline alpha
*/
inline void SetLineAlpha( float a ) { mLineColor[3] = a; }
/**
* Set outline width
*/
inline void SetLineWidth( float width ) { mLineWidth = width; }
/**
* Set fill color
*/
inline void SetFillColor( float r, float g, float b, float a=1.0f ) { mFillColor[0] = r; mFillColor[1] = g; mFillColor[2] = b; mFillColor[3] = a; }
/**
* Set outline color
*/
inline void SetLineColor( float r, float g, float b, float a=1.0f ) { mLineColor[0] = r; mLineColor[1] = g; mLineColor[2] = b; mLineColor[3] = a; }
/**
* Set selected fill color
*/
inline void SetSelectedFillColor( float r, float g, float b, float a=1.0f ) { mSelectedFillColor[0] = r; mSelectedFillColor[1] = g; mSelectedFillColor[2] = b; mSelectedFillColor[3] = a; }
/**
* Set selected outline color
*/
inline void SetSelectedLineColor( float r, float g, float b, float a=1.0f ) { mSelectedLineColor[0] = r; mSelectedLineColor[1] = g; mSelectedLineColor[2] = b; mSelectedLineColor[3] = a; }
/**
* Is the widget moveable/draggable by the user?
*/
inline bool IsMoveable() const { return mMoveable; }
/**
* Toggle if the user can move/drag the widget
*/
inline void SetMoveable( bool moveable ) { mMoveable = moveable; }
/**
* Is the widget resizeable by the user?
*/
inline bool IsResizeable() const { return mResizeable; }
/**
* Toggle if the user can resize the widget
*/
inline void SetResizeable( bool resizeable ) { mResizeable = resizeable; }
/**
* Is the widget selected?
*/
inline bool IsSelected() const { return mSelected; }
/**
* Select/de-select the widget
*/
inline void SetSelected( bool selected ) { mSelected = true; }
/**
* Is the widget visible
*/
inline bool IsVisible() const { return mVisible; }
/**
* Show/hide the widget
*/
inline void SetVisible( bool visible ) { mVisible = visible; }
/**
* Retrieve user data
*/
inline void* GetUserData() const { return mUserData; }
/**
* Set user-defined data
*/
inline void SetUserData( void* user ) { mUserData = user; }
/**
* Get the root window of the widget
*/
inline glDisplay* GetDisplay() const { return mDisplay; }
/**
* Get the index of the widget in the window (or -1 if none)
*/
int GetIndex() const;
/**
* Render (automatically called by parent)
*/
void Render();
/**
* Convert from global window coordinates to local widget offset
*/
void GlobalToLocal( float x, float y, float* x_out, float* y_out ) const;
/**
* Convert from local widget offset to global window coordinates
*/
void LocalToGlobal( float x, float y, float* x_out, float* y_out ) const;
/**
* Event message handler callback for recieving UI messages from widgets.
*
* Recieves 4 parameters - the widget that sent the event, the event type,
* a & b message values (@see glEventType from glEvents.h),
* and a user-specified pointer from registration.
*
* Event message handlers should return `true` if the message was
* handled, or `false` if the message was skipped or not handled.
*
* @see AddEventHandler
* @see RemoveEventHandler
*/
typedef bool (*glWidgetEventHandler)(glWidget* widget, uint16_t event, int a, int b, void* user);
/**
* Register an event message handler the widget will send events to.
* @param callback function pointer to the event message handler callback
* @param user optional user-specified pointer that will be passed to all
* invocations of this event handler (typically an object)
*/
void AddEventHandler( glWidgetEventHandler callback, void* user=NULL );
/**
* Remove an event message handler from being called by the widget.
* RemoveEventHandler() will search for previously registered event
* handlers that have the same function pointer and/or user pointer,
* and remove them for being called again in the future.
*/
void RemoveEventHandler( glWidgetEventHandler callback, void* user=NULL );
/**
* @internal Event handler (automatically called by parent window)
*/
bool OnEvent( uint16_t event, int a, int b, void* user );
protected:
friend class glDisplay;
enum DragState
{
DragNone,
DragMove,
DragResizeN,
DragResizeNW,
DragResizeNE,
DragResizeS,
DragResizeSW,
DragResizeSE,
DragResizeW,
DragResizeE
};
struct eventHandler
{
glWidgetEventHandler callback;
void* user;
};
void initDefaults();
void setCursor( DragState cursor );
void setDisplay( glDisplay* display );
void dispatchEvent( uint16_t msg, int a, int b );
DragState coordToBorder( float x, float y, float max_distance=10.0f );
float mX;
float mY;
float mWidth;
float mHeight;
float mSelectedFillColor[4];
float mSelectedLineColor[4];
float mFillColor[4];
float mLineColor[4];
float mLineWidth;
bool mMoveable;
bool mResizeable;
bool mSelected;
bool mVisible;
Shape mShape;
void* mUserData;
glDisplay* mDisplay;
DragState mDragState;
std::vector<eventHandler> mEventHandlers;
};
#endif