1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 /*!
34   \class SoOnOff SoOnOff.h Inventor/engines/SoOnOff.h
35   \brief The SoOnOff engine is a simple on/off switch.
36 
37   \ingroup engines
38 */
39 
40 #include <Inventor/engines/SoOnOff.h>
41 #include <Inventor/lists/SoEngineOutputList.h>
42 #include <Inventor/fields/SoSFBool.h>
43 
44 #include "engines/SoSubEngineP.h"
45 
46 /*!
47   \var SoSFTrigger SoOnOff::on
48 
49   An input signal on this trigger makes the SoOnOff::isOn output
50   become \c TRUE.
51 */
52 /*!
53   \var SoSFTrigger SoOnOff::off
54 
55   An input signal on this trigger makes the SoOnOff::isOn output
56   become \c FALSE.
57 */
58 /*!
59   \var SoSFTrigger SoOnOff::toggle
60 
61   An input signal on this trigger toggles the \c TRUE or \c FALSE
62   value of the SoOnOff::isOn output.
63 */
64 
65 /*!
66   \var SoEngineOutput SoOnOff::isOn
67   (SoSFBool) Whether the engine is "on" or not.
68 */
69 /*!
70   \var SoEngineOutput SoOnOff::isOff
71   (SoSFBool) Always the opposite value of SoOnOff::isOn.
72 */
73 
74 SO_ENGINE_SOURCE(SoOnOff);
75 
76 /*!
77   Default constructor.
78 */
SoOnOff(void)79 SoOnOff::SoOnOff(void)
80 {
81   SO_ENGINE_INTERNAL_CONSTRUCTOR(SoOnOff);
82 
83   SO_ENGINE_ADD_INPUT(on, ());
84   SO_ENGINE_ADD_INPUT(off, ());
85   SO_ENGINE_ADD_INPUT(toggle, ());
86 
87   SO_ENGINE_ADD_OUTPUT(isOn, SoSFBool);
88   SO_ENGINE_ADD_OUTPUT(isOff, SoSFBool);
89 
90   this->state = FALSE;
91 }
92 
93 // Documented in superclass.
94 void
initClass(void)95 SoOnOff::initClass(void)
96 {
97   SO_ENGINE_INTERNAL_INIT_CLASS(SoOnOff);
98 }
99 
100 /*!
101   Destructor is protected because engines are reference counted.
102 */
~SoOnOff()103 SoOnOff::~SoOnOff()
104 {
105 }
106 
107 // Documented in superclass.
108 void
evaluate(void)109 SoOnOff::evaluate(void)
110 {
111   SO_ENGINE_OUTPUT(isOn, SoSFBool, setValue(this->state));
112   SO_ENGINE_OUTPUT(isOff, SoSFBool, setValue(!this->state));
113 }
114 
115 // Documented in superclass.
116 void
inputChanged(SoField * which)117 SoOnOff::inputChanged(SoField * which)
118 {
119   if (which == &on) this->state = TRUE;
120   else if (which == &off) this->state = FALSE;
121   if (which == &toggle) this->state = this->state ? FALSE : TRUE;
122 }
123