OpenWalnut  1.5.0dev
WMTemplate.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WMTEMPLATE_H
26 #define WMTEMPLATE_H
27 
28 #include <memory>
29 #include <string>
30 
31 #include <osg/Geode>
32 
33 #include "core/common/WItemSelection.h"
34 #include "core/common/WItemSelectionItemTyped.h"
35 #include "core/common/WItemSelector.h"
36 #include "core/graphicsEngine/WGEManagedGroupNode.h"
37 #include "core/graphicsEngine/shaders/WGEShader.h"
38 #include "core/kernel/WModule.h"
39 #include "core/kernel/WModuleInputData.h"
40 #include "core/kernel/WModuleOutputData.h"
41 
42 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43 // This is your starting point. Switch to WMTemplae.cpp. For other examples, refer to the README file.
44 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45 
46 /**
47  * This module is intended to be a simple template and example module. It can be used for fast creation of new modules by copying and refactoring
48  * the files. It shows the basic usage of properties, update callbacks and how to wait for data.
49  *
50  * \ingroup modules
51  */
52 class WMTemplate: public WModule
53 {
54 public:
55  /**
56  * Default constructor.
57  */
58  WMTemplate();
59 
60  /**
61  * Destructor.
62  */
63  virtual ~WMTemplate();
64 
65  /**
66  * Gives back the name of this module.
67  * \return the module's name.
68  */
69  virtual const std::string getName() const;
70 
71  /**
72  * Gives back a description of this module.
73  * \return description to module.
74  */
75  virtual const std::string getDescription() const;
76 
77  /**
78  * Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it
79  * should never be initialized or modified in some other way. A simple new instance is required.
80  *
81  * \return the prototype used to create every module in OpenWalnut.
82  */
83  virtual std::shared_ptr< WModule > factory() const;
84 
85  /**
86  * Get the icon for this module in XPM format.
87  * \return The icon.
88  */
89  virtual const char** getXPMIcon() const;
90 
91 protected:
92  /**
93  * Entry point after loading the module. Runs in separate thread.
94  */
95  virtual void moduleMain();
96 
97  /**
98  * Initialize the connectors this module is using.
99  */
100  virtual void connectors();
101 
102  /**
103  * Initialize the properties for this module.
104  */
105  virtual void properties();
106 
107  /**
108  * Initialize requirements for this module.
109  */
110  virtual void requirements();
111 
112  /**
113  * The root node used for this modules graphics. For OSG nodes, always use osg::ref_ptr to ensure proper resource management.
114  */
115  osg::ref_ptr< WGEManagedGroupNode > m_rootNode;
116 
117  /**
118  * The geometry rendered by this module.
119  */
120  osg::ref_ptr< osg::Geode > m_geode;
121 
122  /**
123  * Callback for m_active. Overwrite this in your modules to handle m_active changes separately.
124  */
125  virtual void activate();
126 
127 private:
128  /**
129  * An input connector used to get datasets from other modules. The connection management between connectors must not be handled by the module.
130  */
131  std::shared_ptr< WModuleInputData< WDataSetSingle > > m_input;
132 
133  /**
134  * The output connector used to provide the calculated data to other modules.
135  */
136  std::shared_ptr< WModuleOutputData< WDataSetSingle > > m_output;
137 
138  /**
139  * A condition used to notify about changes in several properties.
140  */
141  std::shared_ptr< WCondition > m_propCondition;
142 
143  /**
144  * To group properties, use WPropGroup.
145  */
146  WPropGroup m_group1;
147 
148  /**
149  * To group properties, use WPropGroup.
150  */
151  WPropGroup m_group1a;
152 
153  /**
154  * To group properties, use WPropGroup.
155  */
156  WPropGroup m_group2;
157 
158  /**
159  * En/Disables an feature.
160  */
161  WPropBool m_enableFeature;
162 
163  /**
164  * A nice feature trigger inside m_group1
165  */
166  WPropBool m_group1Bool;
167 
168  /**
169  * An integer value.
170  */
171  WPropInt m_anInteger;
172 
173  /**
174  * An integer value. It is synchronized with m_anInteger.
175  */
177 
178  /**
179  * A double value.
180  */
181  WPropDouble m_aDouble;
182 
183  /**
184  * A string.
185  */
186  WPropString m_aString;
187 
188  /**
189  * A filename.
190  */
191  WPropFilename m_aFile;
192 
193  /**
194  * A color.
195  */
196  WPropColor m_aColor;
197 
198  /**
199  * A int property used to show hide/un-hide feature.
200  */
201  WPropInt m_aHiddenInt;
202 
203  /**
204  * A group used to show that even hiding whole groups is possible.
205  */
206  WPropGroup m_aHiddenGroup;
207 
208  /**
209  * A trigger which can be used to trigger some kind of operation.
210  */
211  WPropTrigger m_aTrigger;
212 
213  /**
214  * A property allowing the user to select ONE item of some list
215  */
216  WPropSelection m_aSingleSelection;
217 
218  /**
219  * A property allowing the user to select ONE item. This additionally demonstrates how to use your own types/classes in selections.
220  */
222 
223  /**
224  * A property allowing the user to select multiple elements of a list.
225  */
226  WPropSelection m_aMultiSelection;
227 
228  // Outputs -> demonstrate the use of information properties:
229 
230  /**
231  * A property simply providing a integer value to the outside world.
232  */
234 
235  /**
236  * A property simply providing a double value to the outside world.
237  */
238  WPropDouble m_aDoubleOutput;
239 
240  /**
241  * A property simply providing a int value to the outside world.
242  */
243  WPropInt m_aIntOutput;
244 
245  /**
246  * A property simply providing some text to the outside world.
247  */
248  WPropString m_aStringOutput;
249 
250  /**
251  * A Property used to show some color to the user.
252  */
253  WPropColor m_aColorOutput;
254 
255  /**
256  * A Property used to store some position.
257  */
258  WPropPosition m_aPosition;
259 
260  /**
261  * A Property used to show some filename to the user.
262  */
263  WPropFilename m_aFilenameOutput;
264 
265  /**
266  * A Property used to show some trigger to the user.
267  */
268  WPropTrigger m_aTriggerOutput;
269 
270  /**
271  * A Property used to show some selection to the user.
272  */
273  WPropSelection m_aSelectionOutput;
274 
275  /**
276  * A list of items that can be selected using m_aSingleSelection or m_aMultiSelection.
277  */
278  std::shared_ptr< WItemSelection > m_possibleSelections;
279 
280  /**
281  * You should typedef the item type you use. This shortens some code later. We encapsulate a string into an item.
282  */
284 
285  /**
286  * A list of items that can be selected using m_aSingleSelectionUsingTypes property.
287  */
288  std::shared_ptr< WItemSelection > m_possibleSelectionsUsingTypes;
289 
290  /**
291  * A Property used to show the callback mechanism avoiding the thread wake up on change.
292  */
293  WPropTrigger m_hideButton;
294 
295  /**
296  * This causes an exception to be thrown on press to demonstrate how the GUI handles crashing modules.
297  */
298  WPropTrigger m_exceptionTrigger;
299 
300  /**
301  * A property allowing you to define whole intervals.
302  */
303  WPropInterval m_anInterval;
304 
305  /**
306  * The callback triggering the hide flag of m_aColor for demonstration.
307  */
308  void hideButtonPressed();
309 
310  /**
311  * We want some nice shading effects, so we need a nice shader.
312  */
314 
315  /**
316  * Node callback to change the color of the shapes inside the root node. For more details on this class, refer to the documentation in
317  * moduleMain().
318  */
319  class SafeUpdateCallback : public osg::NodeCallback
320  {
321  public: // NOLINT
322  /**
323  * Constructor.
324  *
325  * \param module just set the creating module as pointer for later reference.
326  */
327  explicit SafeUpdateCallback( WMTemplate* module ): m_module( module ), m_initialUpdate( true )
328  {
329  };
330 
331  /**
332  * operator () - called during the update traversal.
333  *
334  * \param node the osg node
335  * \param nv the node visitor
336  */
337  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
338 
339  /**
340  * Pointer used to access members of the module to modify the node.
341  * Please do not use shared_ptr here as this would prevent deletion of the module as the callback contains
342  * a reference to it. It is safe to use a simple pointer here as callback get deleted before the module.
343  */
345 
346  /**
347  * Denotes whether the update callback is called the first time. It is especially useful
348  * to set some initial value even if the property has not yet changed.
349  */
351  };
352 
353 
354  /**
355  * Node callback to change the position of the shapes in the coordinate system of the scene.
356  * For more details on this class, refer to the documentation in moduleMain().
357  */
358  class TranslateCallback : public osg::NodeCallback
359  {
360  public: // NOLINT
361  /**
362  * Constructor.
363  *
364  * \param module just set the creating module as pointer for later reference.
365  */
366  explicit TranslateCallback( WMTemplate* module ): m_module( module ), m_initialUpdate( true )
367  {
368  };
369 
370  /**
371  * operator () - called during the update traversal.
372  *
373  * \param node the osg node
374  * \param nv the node visitor
375  */
376  virtual void operator()( osg::Node* node, osg::NodeVisitor* nv );
377 
378  /**
379  * Pointer used to access members of the module to modify the node.
380  * Please do not use shared_ptr here as this would prevent deletion of the module as the callback contains
381  * a reference to it. It is safe to use a simple pointer here as callback get deleted before the module.
382  */
384 
385  /**
386  * Denotes whether the update callback is called the first time. It is especially useful
387  * to set some initial value even if the property has not yet changed.
388  */
390  };
391 
392  /**
393  * This shows how to write custom constraints for your modules. Please refer to the documentation in properties() for more details.
394  *
395  * \note: always use WPVBaseTypes to specialize the PropertyVariable template.
396  */
397  class StringLength: public WPropertyVariable< WPVBaseTypes::PV_STRING >::PropertyConstraint
398  {
399  /**
400  * You need to overwrite this method. It decides whether the specified new value should be accepted or not.
401  *
402  * \param property the property thats going to be changed.
403  * \param value the new value
404  *
405  * \return true if the new value is OK.
406  */
407  virtual bool accept( std::shared_ptr< WPropertyVariable< WPVBaseTypes::PV_STRING > > property, const WPVBaseTypes::PV_STRING& value );
408 
409  /**
410  * Method to clone the constraint and create a new one with the correct dynamic type.
411  *
412  * \return the constraint.
413  */
414  virtual std::shared_ptr< WPropertyVariable< WPVBaseTypes::PV_STRING >::PropertyConstraint > clone();
415  };
416 };
417 
418 #endif // WMTEMPLATE_H
osg::ref_ptr< WGEShader > RefPtr
Convenience typedef for an osg::ref_ptr.
Definition: WGEShader.h:53
A derivation of WItemSelection which can store a value of any type.
Node callback to change the color of the shapes inside the root node.
Definition: WMTemplate.h:320
SafeUpdateCallback(WMTemplate *module)
Constructor.
Definition: WMTemplate.h:327
bool m_initialUpdate
Denotes whether the update callback is called the first time.
Definition: WMTemplate.h:350
WMTemplate * m_module
Pointer used to access members of the module to modify the node.
Definition: WMTemplate.h:344
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
operator () - called during the update traversal.
Definition: WMTemplate.cpp:773
This shows how to write custom constraints for your modules.
Definition: WMTemplate.h:398
virtual bool accept(std::shared_ptr< WPropertyVariable< WPVBaseTypes::PV_STRING > > property, const WPVBaseTypes::PV_STRING &value)
You need to overwrite this method.
Definition: WMTemplate.cpp:808
virtual std::shared_ptr< WPropertyVariable< WPVBaseTypes::PV_STRING >::PropertyConstraint > clone()
Method to clone the constraint and create a new one with the correct dynamic type.
Definition: WMTemplate.cpp:820
Node callback to change the position of the shapes in the coordinate system of the scene.
Definition: WMTemplate.h:359
WMTemplate * m_module
Pointer used to access members of the module to modify the node.
Definition: WMTemplate.h:383
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
operator () - called during the update traversal.
Definition: WMTemplate.cpp:788
TranslateCallback(WMTemplate *module)
Constructor.
Definition: WMTemplate.h:366
bool m_initialUpdate
Denotes whether the update callback is called the first time.
Definition: WMTemplate.h:389
This module is intended to be a simple template and example module.
Definition: WMTemplate.h:53
WPropColor m_aColor
A color.
Definition: WMTemplate.h:196
osg::ref_ptr< WGEManagedGroupNode > m_rootNode
The root node used for this modules graphics.
Definition: WMTemplate.h:115
WPropPosition m_aPosition
A Property used to store some position.
Definition: WMTemplate.h:258
WPropDouble m_aDouble
A double value.
Definition: WMTemplate.h:181
WPropSelection m_aSingleSelection
A property allowing the user to select ONE item of some list.
Definition: WMTemplate.h:216
WPropSelection m_aSelectionOutput
A Property used to show some selection to the user.
Definition: WMTemplate.h:273
WPropTrigger m_aTriggerOutput
A Property used to show some trigger to the user.
Definition: WMTemplate.h:268
WPropColor m_aColorOutput
A Property used to show some color to the user.
Definition: WMTemplate.h:253
WPropFilename m_aFilenameOutput
A Property used to show some filename to the user.
Definition: WMTemplate.h:263
osg::ref_ptr< osg::Geode > m_geode
The geometry rendered by this module.
Definition: WMTemplate.h:120
WPropInterval m_anInterval
A property allowing you to define whole intervals.
Definition: WMTemplate.h:303
virtual const std::string getDescription() const
Gives back a description of this module.
Definition: WMTemplate.cpp:140
WPropTrigger m_exceptionTrigger
This causes an exception to be thrown on press to demonstrate how the GUI handles crashing modules.
Definition: WMTemplate.h:298
virtual void requirements()
Initialize requirements for this module.
Definition: WMTemplate.cpp:379
WPropInt m_aIntOutput
A property simply providing a int value to the outside world.
Definition: WMTemplate.h:243
std::shared_ptr< WItemSelection > m_possibleSelectionsUsingTypes
A list of items that can be selected using m_aSingleSelectionUsingTypes property.
Definition: WMTemplate.h:288
WPropInt m_anInteger
An integer value.
Definition: WMTemplate.h:171
WPropInt m_anIntegerClone
An integer value.
Definition: WMTemplate.h:176
WMTemplate()
Default constructor.
Definition: WMTemplate.cpp:79
std::shared_ptr< WModuleOutputData< WDataSetSingle > > m_output
The output connector used to provide the calculated data to other modules.
Definition: WMTemplate.h:136
virtual void properties()
Initialize the properties for this module.
Definition: WMTemplate.cpp:177
WPropTrigger m_hideButton
A Property used to show the callback mechanism avoiding the thread wake up on change.
Definition: WMTemplate.h:293
void hideButtonPressed()
The callback triggering the hide flag of m_aColor for demonstration.
Definition: WMTemplate.cpp:846
virtual const char ** getXPMIcon() const
Get the icon for this module in XPM format.
Definition: WMTemplate.cpp:125
WPropSelection m_aSingleSelectionUsingTypes
A property allowing the user to select ONE item.
Definition: WMTemplate.h:221
std::shared_ptr< WItemSelection > m_possibleSelections
A list of items that can be selected using m_aSingleSelection or m_aMultiSelection.
Definition: WMTemplate.h:278
WPropDouble m_aDoubleOutput
A property simply providing a double value to the outside world.
Definition: WMTemplate.h:238
WPropBool m_group1Bool
A nice feature trigger inside m_group1.
Definition: WMTemplate.h:166
WPropSelection m_aMultiSelection
A property allowing the user to select multiple elements of a list.
Definition: WMTemplate.h:226
virtual void moduleMain()
Entry point after loading the module.
Definition: WMTemplate.cpp:389
virtual void activate()
Callback for m_active.
Definition: WMTemplate.cpp:827
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
Definition: WMTemplate.cpp:93
WPropGroup m_group1a
To group properties, use WPropGroup.
Definition: WMTemplate.h:151
WPropInt m_aHiddenInt
A int property used to show hide/un-hide feature.
Definition: WMTemplate.h:201
virtual ~WMTemplate()
Destructor.
Definition: WMTemplate.cpp:88
WPropString m_aStringOutput
A property simply providing some text to the outside world.
Definition: WMTemplate.h:248
virtual const std::string getName() const
Gives back the name of this module.
Definition: WMTemplate.cpp:134
WPropGroup m_group1
To group properties, use WPropGroup.
Definition: WMTemplate.h:146
WGEShader::RefPtr m_shader
We want some nice shading effects, so we need a nice shader.
Definition: WMTemplate.h:313
WPropGroup m_group2
To group properties, use WPropGroup.
Definition: WMTemplate.h:156
std::shared_ptr< WModuleInputData< WDataSetSingle > > m_input
An input connector used to get datasets from other modules.
Definition: WMTemplate.h:131
WPropInt m_aIntegerOutput
A property simply providing a integer value to the outside world.
Definition: WMTemplate.h:233
WPropFilename m_aFile
A filename.
Definition: WMTemplate.h:191
WPropString m_aString
A string.
Definition: WMTemplate.h:186
WPropTrigger m_aTrigger
A trigger which can be used to trigger some kind of operation.
Definition: WMTemplate.h:211
virtual void connectors()
Initialize the connectors this module is using.
Definition: WMTemplate.cpp:146
WPropBool m_enableFeature
En/Disables an feature.
Definition: WMTemplate.h:161
std::shared_ptr< WCondition > m_propCondition
A condition used to notify about changes in several properties.
Definition: WMTemplate.h:141
WPropGroup m_aHiddenGroup
A group used to show that even hiding whole groups is possible.
Definition: WMTemplate.h:206
WItemSelectionItemTyped< std::string > MyItemType
You should typedef the item type you use.
Definition: WMTemplate.h:283
Class representing a single module of OpenWalnut.
Definition: WModule.h:72
A named property class with a concrete type.
std::string PV_STRING
base type used for every WPVString