appkit  1.5.1
RoadNarrows Robotics Application Kit
WinCvMouse.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Library: librnr_win
6 //
7 // File: WinCvMouse.cxx
8 //
9 // Package: RoadNarrows Robotics Windowing Package
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-05-08 08:22:06 -0600 (Wed, 08 May 2013) $
14  * $Rev: 2920 $
15  *
16  * \brief RoadNarrows Robotics base OpenCV Mouse class implementation.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2011-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #include <sys/types.h>
58 #include <limits.h>
59 #include <libgen.h>
60 #include <unistd.h>
61 #include <stdarg.h>
62 
63 #include <cstring>
64 #include <iostream>
65 #include <fstream>
66 #include <cmath>
67 
68 #include "rnr/rnrconfig.h"
69 #include "rnr/log.h"
70 
71 #include "opencv2/core/core.hpp"
72 #include "opencv2/highgui/highgui.hpp"
73 
74 #include "rnr/appkit/Win.h"
75 #include "rnr/appkit/WinCvMouse.h"
76 #include "rnr/appkit/WinOpenCv.h"
77 
78 using namespace std;
79 using namespace cv;
80 using namespace rnr;
81 
82 //.............................................................................
83 // Class WinCvMouse
84 //.............................................................................
85 
86 WinCvMouse::WinCvMouse()
87 {
88  m_pWin = NULL;
89  m_nCurrentEvent = UIEventNone;
90  m_ptMouse = nopoint;
91  m_bDrag = false;
92  m_bDragState = false;
93 }
94 
95 WinCvMouse::~WinCvMouse()
96 {
97  if( m_pWin != NULL )
98  {
99  unbind();
100  }
101 }
102 
103 void WinCvMouse::bind(Win *pWin)
104 {
105  if( m_pWin != NULL )
106  {
107  unbind();
108  }
109 
110  m_pWin = pWin;
111  m_nCurrentEvent = UIEventNone;
112  m_ptMouse = nopoint;
113  m_bDrag = false;
114  m_bDragState = false;
115 
116  m_pWin->registerCvImageMouseCallback(WinCvMouse::onMouse, this);
117 }
118 
119 void WinCvMouse::unbind()
120 {
121  if( m_pWin != NULL )
122  {
123  m_pWin->unregisterCvImageMouseCallback();
124  m_pWin = NULL;
125  }
126 }
127 
128 
129 void WinCvMouse::onMouse(int event, int x, int y, int flags, void* param)
130 {
131  WinCvMouse *pMouse = (WinCvMouse *)param;
132 
133  pMouse->m_nCurrentEvent = UIEventNone;
134 
135  // drag the mouse events enabled
136  switch( event )
137  {
138  case CV_EVENT_MOUSEMOVE:
139  if( pMouse->m_bDrag && pMouse->m_bDragState )
140  {
141  pMouse->m_ptMouse.x = x;
142  pMouse->m_ptMouse.y = y;
144  }
145  break;
146 
147  case CV_EVENT_LBUTTONUP:
148  if( pMouse->m_bDrag )
149  {
150  pMouse->m_ptMouse.x = x;
151  pMouse->m_ptMouse.y = y;
152  pMouse->m_bDragState = false;
154  }
155  break;
156 
157  case CV_EVENT_LBUTTONDOWN:
158  pMouse->m_ptMouse.x = x;
159  pMouse->m_ptMouse.y = y;
160 
161  if( pMouse->m_bDrag )
162  {
163  pMouse->m_bDragState = true;
165  }
166  else
167  {
168  pMouse->m_nCurrentEvent = UIEventClick;
169  }
170  break;
171 
172  default:
173  break;
174  }
175 }
dragging mouse
Definition: Win.h:118
bool m_bDrag
enable mouse drag operation
Definition: WinCvMouse.h:161
start of mouse drag
Definition: Win.h:117
RoadNarrows Robotics Win abstract base class interface.
const cv::Point nopoint(-1,-1)
integer 2D "No Point"
no action
Definition: Win.h:115
RoadNarrows Robotics base OpenCV Mouse class interface.
mouse click
Definition: Win.h:116
CvPoint m_ptMouse
mouse point
Definition: WinCvMouse.h:160
int m_nCurrentEvent
current (user) menu event
Definition: WinCvMouse.h:159
RNR Win window abstract base class.
Definition: Win.h:211
bool m_bDragState
[not] within drag operation
Definition: WinCvMouse.h:162
RoadNarrows Robotics OpenCV Utilities.
Window OpenCV Mouse base class.
Definition: WinCvMouse.h:81
RoadNarrows Robotics.
Definition: Camera.h:74
end of mouse drag
Definition: Win.h:119