appkit  1.5.1
RoadNarrows Robotics Application Kit
StateWin.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_win
8 //
9 // File: StateWin.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-07-13 13:54:59 -0600 (Sat, 13 Jul 2013) $
14  * $Rev: 3122 $
15  *
16  * \brief GUI window StateWin derived state class interface.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-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 #ifndef _RNR_STATE_WIN_H
58 #define _RNR_STATE_WIN_H
59 
60 #include <stdio.h>
61 
62 #include <string>
63 #include <map>
64 
65 #include "rnr/rnrconfig.h"
66 
67 #include "rnr/appkit/Win.h"
68 #include "rnr/appkit/WinMenu.h"
69 #include "rnr/appkit/SessionWin.h"
70 #include "rnr/appkit/State.h"
71 
72 /*!
73  * \brief RoadNarrows Robotics
74  */
75 namespace rnr
76 {
77  //----------------------------------------------------------------------------
78  // StateWin Class
79  //----------------------------------------------------------------------------
80 
81  /*!
82  * State base class.
83  */
84  class StateWin : public State
85  {
86  public:
87  /*!
88  * \brief Initialization constructor.
89  *
90  * \param nStateId State id. Must be state machine unique.
91  * \param session State's embedding in (derived) window session.
92  * \param strStateName State name.
93  * \param strRefTag State reference id.
94  */
95  StateWin(int nStateId,
97  const std::string &strStateName="",
98  const std::string &strRefTag="") :
99  State(nStateId, strStateName, strRefTag),
100  m_session(session)
101  {
102  m_bOneTimeInit = false;
103  m_bMouseEvents = true;
104  m_bKbEvents = false;
105  m_pButtons = NULL;
106  }
107 
108  /*!
109  * \brief List constructor.
110  *
111  * \param nStateId State id. Must be state machine unique.
112  * \param session State's embedding in (derived) window session.
113  * \param strStateName State name.
114  * \param strRefTag State reference id.
115  * \param listStateEvents Declaration list of allocated state events.
116  * NULL terminated.
117  */
118  StateWin(int nStateId,
120  const std::string &strStateName,
121  const std::string &strRefTag,
122  StateEvent *listStateEvents[]) :
123  State(nStateId, strStateName, strRefTag, listStateEvents),
124  m_session(session)
125  {
126  m_bOneTimeInit = false;
127  m_bMouseEvents = true;
128  m_bKbEvents = false;
129  m_pButtons = NULL;
130  }
131 
132  /*!
133  * \brief Destructor.
134  */
135  virtual ~StateWin()
136  {
137  if( m_pButtons != NULL )
138  {
139  delete m_pButtons;
140  }
141  }
142 
143  /*!
144  * \brief Receive next window event.
145  *
146  * The receive event may set state internal variables used specifically by
147  * the state.
148  *
149  * \return Returns event id.
150  */
151  virtual int receiveEvent();
152 
153  /*!
154  * \brief Execute "enter state" action.
155  *
156  * \param nPrevStateId Previous state id.
157  * \param nEventId Received event id.
158  */
159  virtual void actionEnterState(int nPrevStateId, int nEventId);
160 
161  /*!
162  * \brief Execute "exit state" action.
163  *
164  * \param nNextStateId Next state id.
165  * \param nEventId Received event id.
166  */
167  virtual void actionExitState(int nNextStateId, int nEventId);
168 
169  /*!
170  * \brief Set button states.
171  *
172  * State and/or session data determine the state of the buttons.
173  */
174  virtual void setButtonStates()
175  {
176  }
177 
178  /*!
179  * \brief Get the embedding window session
180  *
181  * \return SessionWin (derived) object reference.
182  */
184  {
185  return m_session;
186  }
187 
188  /*!
189  * \brief Enable/disable mouse events.
190  *
191  * \param bEnable Enable (true) or disable (false) state.
192  */
193  void enableMouseEvents(bool bEnable)
194  {
195  m_bMouseEvents = bEnable;
196  }
197 
198  /*!
199  * \brief Enable/disable keyboard events.
200  *
201  * \param bEnable Enable (true) or disable (false) state.
202  */
203  void enableKbEvents(bool bEnable)
204  {
205  m_bKbEvents = bEnable;
206  }
207 
208  protected:
209  SessionWin &m_session; ///< state id
210  bool m_bOneTimeInit; ///< state's one-time initializaion
211  bool m_bMouseEvents; ///< enable/disable mouse events
212  bool m_bKbEvents; ///< enable/disable keyboard events
213  WinButtonMenu *m_pButtons; ///< button menu
214  WinCvMouse m_mouse; ///< mouse
215 
216  friend class StateEvent; ///< friend
217 
218  /*!
219  * \brief One-time initialization of the window gui interface.
220  *
221  * This function is called on the "enter state" action. Another option, is
222  * to do these one-time initialization during object construction.
223  * However, derived class objects would call the base classes one-time
224  * initialization unnecessarily. So, lazy initialization is used to make
225  * use of C++ inheritence.
226  */
227  virtual void initOnceGuiInterface()
228  {
229  if( !m_bOneTimeInit )
230  {
231  initOnceButtons();
232  m_bOneTimeInit = true;
233  }
234  }
235 
236  /*!
237  * \brief One-time button menu initialization.
238  *
239  * This function is called on the "enter state" action.
240  */
241  virtual void initOnceButtons()
242  {
243  // initialize button here
244  }
245 
246  /*!
247  * \brief Build the window gui interface.
248  *
249  * This function is called on the "enter state" action.
250  */
251  virtual void buildGuiInterface()
252  {
253  // create new widgets here
254  }
255 
256  /*!
257  * \brief Show the window gui interface.
258  *
259  * This function is called on the "enter state" action.
260  */
261  virtual void showGuiInterface()
262  {
263  // show all gui widgets in workspace.
264  m_session.win().showWorkspace();
265  }
266 
267  /*!
268  * \brief Destroy the window gui interface.
269  *
270  * This function is called on the "exit state" action.
271  */
272  virtual void destroyGuiInterface()
273  {
274  // remove all widgets from workspace
275  m_session.win().eraseWorkspace();
276  }
277  };
278 
279 } // namespace rnr
280 
281 
282 #endif // _RNR_STATE_WIN_H
virtual ~StateWin()
Destructor.
Definition: StateWin.h:135
Win & win()
Get the session window object.
Definition: SessionWin.h:167
Window button menu base class.
Definition: WinMenu.h:232
virtual void initOnceGuiInterface()
One-time initialization of the window gui interface.
Definition: StateWin.h:227
StateWin(int nStateId, SessionWin &session, const std::string &strStateName, const std::string &strRefTag, StateEvent *listStateEvents[])
List constructor.
Definition: StateWin.h:118
virtual void initOnceButtons()
One-time button menu initialization.
Definition: StateWin.h:241
virtual int receiveEvent()
Receive next window event.
Definition: StateWin.cxx:78
WinButtonMenu * m_pButtons
button menu
Definition: StateWin.h:213
bool m_bMouseEvents
enable/disable mouse events
Definition: StateWin.h:211
void enableMouseEvents(bool bEnable)
Enable/disable mouse events.
Definition: StateWin.h:193
RoadNarrows Robotics Win abstract base class interface.
virtual void actionEnterState(int nPrevStateId, int nEventId)
Execute "enter state" action.
Definition: StateWin.cxx:115
SessionWin & m_session
state id
Definition: StateWin.h:209
RoadNarrows Robotics base window button menu interface.
virtual void destroyGuiInterface()
Destroy the window gui interface.
Definition: StateWin.h:272
SessionWin derived class.
virtual void setButtonStates()
Set button states.
Definition: StateWin.h:174
bool m_bOneTimeInit
state&#39;s one-time initializaion
Definition: StateWin.h:210
bool m_bKbEvents
enable/disable keyboard events
Definition: StateWin.h:212
virtual void buildGuiInterface()
Build the window gui interface.
Definition: StateWin.h:251
void enableKbEvents(bool bEnable)
Enable/disable keyboard events.
Definition: StateWin.h:203
State base class interface.
StateWin(int nStateId, SessionWin &session, const std::string &strStateName="", const std::string &strRefTag="")
Initialization constructor.
Definition: StateWin.h:95
SessionWin & session()
Get the embedding window session.
Definition: StateWin.h:183
virtual void showWorkspace()=0
Show/refresh all gui widgets in workspace.
virtual void eraseWorkspace()=0
Remove all widgets contained in the window&#39;s workspace container.
Window OpenCV Mouse base class.
Definition: WinCvMouse.h:81
virtual void showGuiInterface()
Show the window gui interface.
Definition: StateWin.h:261
RoadNarrows Robotics.
Definition: Camera.h:74
WinCvMouse m_mouse
mouse
Definition: StateWin.h:214
virtual void actionExitState(int nNextStateId, int nEventId)
Execute "exit state" action.
Definition: StateWin.cxx:141