Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
InputVariables.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_INPUT_VARIABLES_H_
34#define DVD_INPUT_VARIABLES_H_
35
36namespace Divide {
37namespace Input {
40
41class Variable {
42 protected:
45
46 public:
47 Variable(const D64 dInitValue)
48 : _dInitValue(dInitValue),
49 _dValue(dInitValue)
50 {
51 }
52
53 virtual ~Variable() = default;
54
55 [[nodiscard]] D64 getValue() const { return _dValue; }
56
58
59 virtual void setValue(const D64 dValue) { _dValue = dValue; }
60
61 [[nodiscard]] virtual string toString() const {
63 }
64
65 virtual void update(){};
66};
67
68class Constant final : public Variable {
69 public:
70 Constant(const D64 dInitValue) : Variable(dInitValue) {}
71
72 void setValue([[maybe_unused]] const D64 dValue) override {
73 }
74};
75
76class LimitedVariable : public Variable {
77 protected:
80
81 public:
82 LimitedVariable(const D64 dInitValue, const D64 dMinValue, const D64 dMaxValue)
83 : Variable(dInitValue),
84 _dMinValue(dMinValue),
85 _dMaxValue(dMaxValue)
86 {
87
88 }
89
90 void setValue(const D64 dValue) override {
91 _dValue = dValue;
92 if (_dValue > _dMaxValue)
94 else if (_dValue < _dMinValue)
96 }
97};
98
99class TriangleVariable final : public LimitedVariable {
100 protected:
102
103 public:
104 TriangleVariable(const D64 dInitValue, const D64 dDeltaValue, const D64 dMinValue, const D64 dMaxValue)
105 : LimitedVariable(dInitValue, dMinValue, dMaxValue),
106 _dDeltaValue(dDeltaValue){};
107
108 void update() override {
109 D64 dValue = getValue() + _dDeltaValue;
110 if (dValue > _dMaxValue) {
111 dValue = _dMaxValue;
113 // cout << "Decreasing variable towards " << _dMinValue << endl;
114 } else if (dValue < _dMinValue) {
115 dValue = _dMinValue;
117 // cout << "Increasing variable towards " << _dMaxValue << endl;
118 }
119 setValue(dValue);
120 // cout << "TriangleVariable::update : delta=" << _dDeltaValue << ",
121 // value=" << dValue << endl;
122 }
123};
124
127
129typedef void (*EffectVariablesApplier)(MapVariables& mapVars, OIS::Effect* pEffect);
130
132 protected:
133 // Effect description
134 const char* _pszDesc;
135
136 // The associate OIS effect
137 OIS::Effect* _pEffect{};
138
139 // The effect variables.
141
142 // The effect variables applier function.
144
145 // True if the effect is currently being played.
146 bool _bActive = false;
147
148 public:
149 VariableEffect(const char* pszDesc, OIS::Effect* pEffect,
150 const MapVariables& mapVars,
151 const EffectVariablesApplier pfApplyVars)
152 : _pszDesc(pszDesc),
153 _pEffect(pEffect),
154 _mapVariables(mapVars),
155 _pfApplyVariables(pfApplyVars)
156 {
157 }
158
160 {
161 delete _pEffect;
162 for (MapVariables::iterator iterVars = std::begin(_mapVariables); iterVars != std::end(_mapVariables); ++iterVars)
163 {
164 delete iterVars->second;
165 }
166 }
167
168 void setActive(const bool bActive = true) {
169 reset();
170 _bActive = bActive;
171 }
172
173 [[nodiscard]] bool isActive() const noexcept { return _bActive; }
174 OIS::Effect* getFFEffect() { return _pEffect; }
175
176 [[nodiscard]] const char* getDescription() const { return _pszDesc; }
177
178 void update() {
179 if (isActive()) {
180 for (MapVariables::iterator iterVars = std::begin(_mapVariables);
181 iterVars != std::end(_mapVariables); ++iterVars) {
182 iterVars->second->update();
183 }
184
185 // Apply the updated variable values to the effect.
187 }
188 }
189
190 void reset() {
191 for (MapVariables::iterator iterVars = std::begin(_mapVariables);
192 iterVars != std::end(_mapVariables); ++iterVars) {
193 iterVars->second->reset();
194 }
196 }
197
198 [[nodiscard]] string toString() const {
199 string str;
200 for (MapVariables::const_iterator iterVars = std::begin(_mapVariables);
201 iterVars != std::end(_mapVariables); ++iterVars) {
202 str += iterVars->first + ":" + iterVars->second->toString() + " ";
203 }
204 return str;
205 }
206};
207
208}; // namespace Input
209}; // namespace Divide
210
211#endif //DVD_INPUT_VARIABLES_H_
void setValue(const D64 dValue) override
Constant(const D64 dInitValue)
void setValue(const D64 dValue) override
LimitedVariable(const D64 dInitValue, const D64 dMinValue, const D64 dMaxValue)
TriangleVariable(const D64 dInitValue, const D64 dDeltaValue, const D64 dMinValue, const D64 dMaxValue)
EffectVariablesApplier _pfApplyVariables
const char * getDescription() const
bool isActive() const noexcept
VariableEffect(const char *pszDesc, OIS::Effect *pEffect, const MapVariables &mapVars, const EffectVariablesApplier pfApplyVars)
void setActive(const bool bActive=true)
Variable(const D64 dInitValue)
virtual string toString() const
virtual ~Variable()=default
virtual void setValue(const D64 dValue)
hashMap< U64, Variable * > MapVariables
void(* EffectVariablesApplier)(MapVariables &mapVars, OIS::Effect *pEffect)
string to_string(GET_PASS_TYPE< T > value)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
double D64