Jetson Inference
DNN Vision Library
glWidget.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __GL_WIDGET_H__
24 #define __GL_WIDGET_H__
25 
26 
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <vector>
30 
31 
32 // forward declarations
33 class glDisplay;
34 
35 
40 class glWidget
41 {
42 public:
46  enum Shape
47  {
51  };
52 
56  glWidget( Shape shape=Rect );
57 
61  glWidget( float x, float y, float width, float height, Shape shape=Rect );
62 
66  virtual ~glWidget();
67 
71  bool Contains( float x, float y ) const;
72 
76  inline void Move( float x, float y ) { mX += x; mY += y; }
77 
81  inline float X() const { return mX; }
82 
86  inline float Y() const { return mY; }
87 
91  inline void SetX( float x ) { mX = x; }
92 
96  inline void SetY( float y ) { mY = y; }
97 
101  inline float Width() const { return mWidth; }
102 
106  inline float Height() const { return mHeight; }
107 
111  inline void SetWidth( float width ) { mWidth = width; }
112 
116  inline void SetHeight( float height ) { mHeight = height; }
117 
121  inline void GetPosition( float* x, float* y ) const { if(x) *x=mX; if(y) *y=mY; }
122 
126  inline void SetPosition( float x, float y ) { mX = x; mY = y; }
127 
131  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; }
132 
136  inline void SetCoords( float x1, float y1, float x2, float y2 ) { mX = x1; mY = y1; mWidth=x2-x1; mHeight=y2-y1; }
137 
141  inline void GetSize( float* width, float* height ) const { if(width) *width=mWidth; if(height) *height=mHeight; }
142 
146  inline void SetSize( float width, float height ) { mWidth = width; mHeight = height; }
147 
151  inline Shape GetShape() const { return mShape; }
152 
156  inline void SetShape( Shape shape ) { mShape = shape; }
157 
161  inline void SetFillAlpha( float a ) { mFillColor[3] = a; }
162 
166  inline void SetLineAlpha( float a ) { mLineColor[3] = a; }
167 
171  inline void SetLineWidth( float width ) { mLineWidth = width; }
172 
176  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; }
177 
181  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; }
182 
186  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; }
187 
191  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; }
192 
196  inline bool IsMoveable() const { return mMoveable; }
197 
201  inline void SetMoveable( bool moveable ) { mMoveable = moveable; }
202 
206  inline bool IsResizeable() const { return mResizeable; }
207 
211  inline void SetResizeable( bool resizeable ) { mResizeable = resizeable; }
212 
216  inline bool IsSelected() const { return mSelected; }
217 
221  inline void SetSelected( bool selected ) { mSelected = true; }
222 
226  inline bool IsVisible() const { return mVisible; }
227 
231  inline void SetVisible( bool visible ) { mVisible = visible; }
232 
236  inline void* GetUserData() const { return mUserData; }
237 
241  inline void SetUserData( void* user ) { mUserData = user; }
242 
246  inline glDisplay* GetDisplay() const { return mDisplay; }
247 
251  int GetIndex() const;
252 
256  void Render();
257 
261  void GlobalToLocal( float x, float y, float* x_out, float* y_out ) const;
262 
266  void LocalToGlobal( float x, float y, float* x_out, float* y_out ) const;
267 
281  typedef bool (*glWidgetEventHandler)(glWidget* widget, uint16_t event, int a, int b, void* user);
282 
289  void AddEventHandler( glWidgetEventHandler callback, void* user=NULL );
290 
297  void RemoveEventHandler( glWidgetEventHandler callback, void* user=NULL );
298 
302  bool OnEvent( uint16_t event, int a, int b, void* user );
303 
304 
305 protected:
306  friend class glDisplay;
307 
309  {
320  };
321 
323  {
325  void* user;
326  };
327 
328  void initDefaults();
329  void setCursor( DragState cursor );
330  void setDisplay( glDisplay* display );
331  void dispatchEvent( uint16_t msg, int a, int b );
332 
333  DragState coordToBorder( float x, float y, float max_distance=10.0f );
334 
335  float mX;
336  float mY;
337  float mWidth;
338  float mHeight;
339 
342 
343  float mFillColor[4];
344  float mLineColor[4];
345  float mLineWidth;
346 
347  bool mMoveable;
349  bool mSelected;
350  bool mVisible;
351 
353  void* mUserData;
354 
357 
358  std::vector<eventHandler> mEventHandlers;
359 };
360 
361 #endif
362 
glWidget(Shape shape=Rect)
Constructor.
void RemoveEventHandler(glWidgetEventHandler callback, void *user=NULL)
Remove an event message handler from being called by the widget.
void dispatchEvent(uint16_t msg, int a, int b)
void SetResizeable(bool resizeable)
Toggle if the user can resize the widget.
Definition: glWidget.h:211
void GetPosition(float *x, float *y) const
Get position of widget in global window coordinates.
Definition: glWidget.h:121
Definition: glWidget.h:314
void SetShape(Shape shape)
Set the shape.
Definition: glWidget.h:156
float X() const
Get the widget&#39;s X coordinate.
Definition: glWidget.h:81
void SetUserData(void *user)
Set user-defined data.
Definition: glWidget.h:241
bool IsVisible() const
Is the widget visible.
Definition: glWidget.h:226
bool IsSelected() const
Is the widget selected?
Definition: glWidget.h:216
Definition: glWidget.h:50
void SetHeight(float height)
Set the widget&#39;s height.
Definition: glWidget.h:116
void AddEventHandler(glWidgetEventHandler callback, void *user=NULL)
Register an event message handler the widget will send events to.
void SetPosition(float x, float y)
Set position of widget in global window coordinates.
Definition: glWidget.h:126
void SetMoveable(bool moveable)
Toggle if the user can move/drag the widget.
Definition: glWidget.h:201
Definition: glWidget.h:317
Definition: glWidget.h:315
bool Contains(float x, float y) const
Test if the point is inside the widget.
DragState coordToBorder(float x, float y, float max_distance=10.0f)
void * GetUserData() const
Retrieve user data.
Definition: glWidget.h:236
bool mResizeable
Definition: glWidget.h:348
float mSelectedLineColor[4]
Definition: glWidget.h:341
glDisplay * GetDisplay() const
Get the root window of the widget.
Definition: glWidget.h:246
void GetSize(float *width, float *height) const
Get the widget&#39;s size.
Definition: glWidget.h:141
void SetLineColor(float r, float g, float b, float a=1.0f)
Set outline color.
Definition: glWidget.h:181
void GlobalToLocal(float x, float y, float *x_out, float *y_out) const
Convert from global window coordinates to local widget offset.
DragState
Definition: glWidget.h:308
void SetSize(float width, float height)
Set the widget&#39;s size.
Definition: glWidget.h:146
void setCursor(DragState cursor)
void initDefaults()
Definition: glWidget.h:49
DragState mDragState
Definition: glWidget.h:356
glDisplay * mDisplay
Definition: glWidget.h:355
void Render()
Render (automatically called by parent)
Definition: glWidget.h:313
void Move(float x, float y)
Move the widget&#39;s position by the specified offset.
Definition: glWidget.h:76
Definition: glWidget.h:312
float mX
Definition: glWidget.h:335
glWidgetEventHandler callback
Definition: glWidget.h:324
bool IsResizeable() const
Is the widget resizeable by the user?
Definition: glWidget.h:206
Shape mShape
Definition: glWidget.h:352
virtual ~glWidget()
Destructor.
void SetCoords(float x1, float y1, float x2, float y2)
Set the bounding coordinates of the widget.
Definition: glWidget.h:136
Definition: glWidget.h:310
OpenGL graphics widget for rendering moveable/resizable shapes.
Definition: glWidget.h:40
Shape
Shape enum.
Definition: glWidget.h:46
void SetX(float x)
Set the widget&#39;s X coordinate.
Definition: glWidget.h:91
Definition: glWidget.h:319
void * user
Definition: glWidget.h:325
void SetSelectedFillColor(float r, float g, float b, float a=1.0f)
Set selected fill color.
Definition: glWidget.h:186
Definition: glWidget.h:318
float mLineWidth
Definition: glWidget.h:345
OpenGL display window and image/video renderer with CUDA interoperability.
Definition: glDisplay.h:47
std::vector< eventHandler > mEventHandlers
Definition: glWidget.h:358
float Width() const
Get the widget&#39;s width.
Definition: glWidget.h:101
void * mUserData
Definition: glWidget.h:353
void SetVisible(bool visible)
Show/hide the widget.
Definition: glWidget.h:231
float mFillColor[4]
Definition: glWidget.h:343
Shape GetShape() const
Get the shape.
Definition: glWidget.h:151
void SetWidth(float width)
Set the widget&#39;s width.
Definition: glWidget.h:111
void GetCoords(float *x1, float *y1, float *x2, float *y2) const
Get the bounding coordinates of the widget.
Definition: glWidget.h:131
Definition: glWidget.h:322
void SetLineWidth(float width)
Set outline width.
Definition: glWidget.h:171
bool mSelected
Definition: glWidget.h:349
float mY
Definition: glWidget.h:336
bool IsMoveable() const
Is the widget moveable/draggable by the user?
Definition: glWidget.h:196
void SetFillAlpha(float a)
Set fill alpha.
Definition: glWidget.h:161
Definition: glWidget.h:48
bool OnEvent(uint16_t event, int a, int b, void *user)
float mSelectedFillColor[4]
Definition: glWidget.h:340
bool mMoveable
Definition: glWidget.h:347
float Height() const
Get the widget&#39;s height.
Definition: glWidget.h:106
float Y() const
Get the widget&#39;s Y coordinate.
Definition: glWidget.h:86
bool mVisible
Definition: glWidget.h:350
Definition: glWidget.h:316
float mWidth
Definition: glWidget.h:337
bool(* glWidgetEventHandler)(glWidget *widget, uint16_t event, int a, int b, void *user)
Event message handler callback for recieving UI messages from widgets.
Definition: glWidget.h:281
void SetFillColor(float r, float g, float b, float a=1.0f)
Set fill color.
Definition: glWidget.h:176
void setDisplay(glDisplay *display)
float mLineColor[4]
Definition: glWidget.h:344
void SetSelectedLineColor(float r, float g, float b, float a=1.0f)
Set selected outline color.
Definition: glWidget.h:191
void SetY(float y)
Set the widget&#39;s Y coordinate.
Definition: glWidget.h:96
float mHeight
Definition: glWidget.h:338
int GetIndex() const
Get the index of the widget in the window (or -1 if none)
void LocalToGlobal(float x, float y, float *x_out, float *y_out) const
Convert from local widget offset to global window coordinates.
Definition: glWidget.h:311
void SetSelected(bool selected)
Select/de-select the widget.
Definition: glWidget.h:221
void SetLineAlpha(float a)
Set outline alpha.
Definition: glWidget.h:166