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::GetIndex
int GetIndex() const
Get the index of the widget in the window (or -1 if none)
glWidget::SetFillAlpha
void SetFillAlpha(float a)
Set fill alpha.
Definition: glWidget.h:161
glWidget::mEventHandlers
std::vector< eventHandler > mEventHandlers
Definition: glWidget.h:358
glWidget::SetVisible
void SetVisible(bool visible)
Show/hide the widget.
Definition: glWidget.h:231
glWidget::eventHandler::user
void * user
Definition: glWidget.h:325
glWidget::SetSelected
void SetSelected(bool selected)
Select/de-select the widget.
Definition: glWidget.h:221
glWidget::setCursor
void setCursor(DragState cursor)
glWidget::OnEvent
bool OnEvent(uint16_t event, int a, int b, void *user)
glWidget::mResizeable
bool mResizeable
Definition: glWidget.h:348
glWidget::SetFillColor
void SetFillColor(float r, float g, float b, float a=1.0f)
Set fill color.
Definition: glWidget.h:176
glWidget::SetHeight
void SetHeight(float height)
Set the widget's height.
Definition: glWidget.h:116
glWidget
OpenGL graphics widget for rendering moveable/resizable shapes.
Definition: glWidget.h:40
glWidget::DragResizeS
@ DragResizeS
Definition: glWidget.h:315
glWidget::GetSize
void GetSize(float *width, float *height) const
Get the widget's size.
Definition: glWidget.h:141
glWidget::SetMoveable
void SetMoveable(bool moveable)
Toggle if the user can move/drag the widget.
Definition: glWidget.h:201
glWidget::Width
float Width() const
Get the widget's width.
Definition: glWidget.h:101
glWidget::DragResizeE
@ DragResizeE
Definition: glWidget.h:319
glWidget::mDragState
DragState mDragState
Definition: glWidget.h:356
glWidget::eventHandler::callback
glWidgetEventHandler callback
Definition: glWidget.h:324
glWidget::eventHandler
Definition: glWidget.h:322
glDisplay
OpenGL display window and image/video renderer with CUDA interoperability.
Definition: glDisplay.h:47
glWidget::GetPosition
void GetPosition(float *x, float *y) const
Get position of widget in global window coordinates.
Definition: glWidget.h:121
glWidget::SetShape
void SetShape(Shape shape)
Set the shape.
Definition: glWidget.h:156
glWidget::IsVisible
bool IsVisible() const
Is the widget visible.
Definition: glWidget.h:226
glWidget::mY
float mY
Definition: glWidget.h:336
glWidget::GetUserData
void * GetUserData() const
Retrieve user data.
Definition: glWidget.h:236
glWidget::mLineWidth
float mLineWidth
Definition: glWidget.h:345
glWidget::DragResizeNW
@ DragResizeNW
Definition: glWidget.h:313
glWidget::SetLineWidth
void SetLineWidth(float width)
Set outline width.
Definition: glWidget.h:171
glWidget::mX
float mX
Definition: glWidget.h:335
glWidget::mDisplay
glDisplay * mDisplay
Definition: glWidget.h:355
glWidget::RemoveEventHandler
void RemoveEventHandler(glWidgetEventHandler callback, void *user=NULL)
Remove an event message handler from being called by the widget.
glWidget::dispatchEvent
void dispatchEvent(uint16_t msg, int a, int b)
glWidget::DragResizeNE
@ DragResizeNE
Definition: glWidget.h:314
glWidget::SetResizeable
void SetResizeable(bool resizeable)
Toggle if the user can resize the widget.
Definition: glWidget.h:211
glWidget::SetCoords
void SetCoords(float x1, float y1, float x2, float y2)
Set the bounding coordinates of the widget.
Definition: glWidget.h:136
glWidget::mSelected
bool mSelected
Definition: glWidget.h:349
glWidget::SetSize
void SetSize(float width, float height)
Set the widget's size.
Definition: glWidget.h:146
glWidget::IsSelected
bool IsSelected() const
Is the widget selected?
Definition: glWidget.h:216
glWidget::DragNone
@ DragNone
Definition: glWidget.h:310
glWidget::mUserData
void * mUserData
Definition: glWidget.h:353
glWidget::SetX
void SetX(float x)
Set the widget's X coordinate.
Definition: glWidget.h:91
glWidget::glWidget
glWidget(Shape shape=Rect)
Constructor.
glWidget::mSelectedFillColor
float mSelectedFillColor[4]
Definition: glWidget.h:340
glWidget::DragMove
@ DragMove
Definition: glWidget.h:311
glWidget::mSelectedLineColor
float mSelectedLineColor[4]
Definition: glWidget.h:341
glWidget::DragResizeW
@ DragResizeW
Definition: glWidget.h:318
glWidget::Shape
Shape
Shape enum.
Definition: glWidget.h:46
glWidget::GetCoords
void GetCoords(float *x1, float *y1, float *x2, float *y2) const
Get the bounding coordinates of the widget.
Definition: glWidget.h:131
glWidget::SetSelectedFillColor
void SetSelectedFillColor(float r, float g, float b, float a=1.0f)
Set selected fill color.
Definition: glWidget.h:186
glWidget::coordToBorder
DragState coordToBorder(float x, float y, float max_distance=10.0f)
glWidget::SetSelectedLineColor
void SetSelectedLineColor(float r, float g, float b, float a=1.0f)
Set selected outline color.
Definition: glWidget.h:191
glWidget::Y
float Y() const
Get the widget's Y coordinate.
Definition: glWidget.h:86
glWidget::mLineColor
float mLineColor[4]
Definition: glWidget.h:344
glWidget::mMoveable
bool mMoveable
Definition: glWidget.h:347
glWidget::Move
void Move(float x, float y)
Move the widget's position by the specified offset.
Definition: glWidget.h:76
glWidget::LocalToGlobal
void LocalToGlobal(float x, float y, float *x_out, float *y_out) const
Convert from local widget offset to global window coordinates.
glWidget::glWidgetEventHandler
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
glWidget::IsResizeable
bool IsResizeable() const
Is the widget resizeable by the user?
Definition: glWidget.h:206
glWidget::DragResizeSE
@ DragResizeSE
Definition: glWidget.h:317
glWidget::GlobalToLocal
void GlobalToLocal(float x, float y, float *x_out, float *y_out) const
Convert from global window coordinates to local widget offset.
glWidget::Line
@ Line
Definition: glWidget.h:49
glWidget::mShape
Shape mShape
Definition: glWidget.h:352
glWidget::Rect
@ Rect
Definition: glWidget.h:48
glWidget::Ellipse
@ Ellipse
Definition: glWidget.h:50
glWidget::mVisible
bool mVisible
Definition: glWidget.h:350
glWidget::DragState
DragState
Definition: glWidget.h:308
glWidget::GetShape
Shape GetShape() const
Get the shape.
Definition: glWidget.h:151
glWidget::mHeight
float mHeight
Definition: glWidget.h:338
glWidget::Contains
bool Contains(float x, float y) const
Test if the point is inside the widget.
glWidget::SetLineAlpha
void SetLineAlpha(float a)
Set outline alpha.
Definition: glWidget.h:166
glWidget::SetLineColor
void SetLineColor(float r, float g, float b, float a=1.0f)
Set outline color.
Definition: glWidget.h:181
glWidget::X
float X() const
Get the widget's X coordinate.
Definition: glWidget.h:81
glWidget::mWidth
float mWidth
Definition: glWidget.h:337
glWidget::DragResizeN
@ DragResizeN
Definition: glWidget.h:312
glWidget::setDisplay
void setDisplay(glDisplay *display)
glWidget::AddEventHandler
void AddEventHandler(glWidgetEventHandler callback, void *user=NULL)
Register an event message handler the widget will send events to.
glWidget::SetWidth
void SetWidth(float width)
Set the widget's width.
Definition: glWidget.h:111
glWidget::~glWidget
virtual ~glWidget()
Destructor.
glWidget::initDefaults
void initDefaults()
glWidget::SetPosition
void SetPosition(float x, float y)
Set position of widget in global window coordinates.
Definition: glWidget.h:126
glWidget::DragResizeSW
@ DragResizeSW
Definition: glWidget.h:316
glWidget::SetY
void SetY(float y)
Set the widget's Y coordinate.
Definition: glWidget.h:96
glWidget::GetDisplay
glDisplay * GetDisplay() const
Get the root window of the widget.
Definition: glWidget.h:246
glWidget::Height
float Height() const
Get the widget's height.
Definition: glWidget.h:106
glWidget::IsMoveable
bool IsMoveable() const
Is the widget moveable/draggable by the user?
Definition: glWidget.h:196
glWidget::mFillColor
float mFillColor[4]
Definition: glWidget.h:343
glWidget::SetUserData
void SetUserData(void *user)
Set user-defined data.
Definition: glWidget.h:241
glWidget::Render
void Render()
Render (automatically called by parent)