| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "igui.h" |
| 4 |
#include <memory> |
| 5 |
#include "string/convert.h" |
| 6 |
#include <parser/DefTokeniser.h> |
| 7 |
|
| 8 |
namespace gui |
| 9 |
{ |
| 10 |
|
| 11 |
class GuiExpression; |
| 12 |
typedef std::shared_ptr<GuiExpression> GuiExpressionPtr; |
| 13 |
|
| 14 |
// Represents a right-hand expression found in the GUI code, which can be |
| 15 |
// a simple float, a string, a gui variable or a complex formula. |
| 16 |
class GuiExpression |
| 17 |
{ |
| 18 |
protected: |
| 19 |
sigc::signal<void> _sigValueChanged; |
| 20 |
|
| 21 |
public: |
| 22 |
GuiExpression(); |
| 23 |
|
| 24 |
virtual float getFloatValue() = 0; |
| 25 |
virtual std::string getStringValue() = 0; |
| 26 |
|
| 27 |
// Sub-expressions or clients can subscribe to get notified about changes |
| 28 |
sigc::signal<void>& signal_valueChanged() |
| 29 |
{ |
| 30 |
return _sigValueChanged; |
| 31 |
} |
| 32 |
|
| 33 |
static GuiExpressionPtr CreateFromString(IGui& gui, const std::string& exprStr); |
| 34 |
|
| 35 |
static GuiExpressionPtr CreateFromTokens(IGui& gui, parser::DefTokeniser& tokeniser); |
| 36 |
}; |
| 37 |
|
| 38 |
// An expression representing a constant floating point value |
| 39 |
class FloatExpression : |
| 40 |
public GuiExpression, |
| 41 |
public ConstantExpression<float> |
| 42 |
{ |
| 43 |
public: |
| 44 |
FloatExpression(float value) : |
| 45 |
ConstantExpression(value) |
| 46 |
{} |
| 47 |
|
| 48 |
virtual float getFloatValue() override |
| 49 |
{ |
| 50 |
return this->evaluate(); |
| 51 |
} |
| 52 |
|
| 53 |
virtual std::string getStringValue() override |
| 54 |
{ |
| 55 |
return string::to_string(getFloatValue()); |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
// An expression representing a constant string value |
| 60 |
class StringExpression : |
| 61 |
public GuiExpression, |
| 62 |
public ConstantExpression<std::string> |
| 63 |
{ |
| 64 |
public: |
| 65 |
StringExpression(const std::string& value) : |
| 66 |
ConstantExpression(value) |
| 67 |
{} |
| 68 |
|
| 69 |
virtual float getFloatValue() override |
| 70 |
{ |
| 71 |
return string::convert<float>(getStringValue()); |
| 72 |
} |
| 73 |
|
| 74 |
virtual std::string getStringValue() override |
| 75 |
{ |
| 76 |
return this->evaluate(); |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
// Adapter class converting a generic GuiExpression to a IGuiExpression of a given type |
| 81 |
// making use of the string conversion templates in the string/conv.h header. |
| 82 |
template<typename ValueType> |
| 83 |
class TypedExpression : |
| 84 |
public IGuiExpression<ValueType> |
| 85 |
{ |
| 86 |
private: |
| 87 |
GuiExpressionPtr _contained; |
| 88 |
|
| 89 |
sigc::signal<void> _sigValueChanged; |
| 90 |
|
| 91 |
public: |
| 92 |
TypedExpression(const GuiExpressionPtr& contained) : |
| 93 |
_contained(contained) |
| 94 |
{ |
| 95 |
if (_contained) |
| 96 |
{ |
| 97 |
// Connect the changed signal of the contained expression |
| 98 |
// to fire our own signal |
| 99 |
_contained->signal_valueChanged().connect([this]() |
| 100 |
{ |
| 101 |
signal_valueChanged().emit(); |
| 102 |
}); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// The generic evaluate() implementation will try to cast the string |
| 107 |
// value of the GuiExpression to the desired ValueType |
| 108 |
virtual ValueType evaluate() override |
| 109 |
{ |
| 110 |
return string::convert<ValueType>(_contained->getStringValue()); |
| 111 |
} |
| 112 |
|
| 113 |
sigc::signal<void>& signal_valueChanged() override |
| 114 |
{ |
| 115 |
return _sigValueChanged; |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
// Boolean specialisation, making use of the contained getFloatValue() |
| 120 |
// casting it to a bool (which is true if the float value is not 0.0f). |
| 121 |
template<> |
| 122 |
class TypedExpression<bool> : |
| 123 |
public IGuiExpression<bool> |
| 124 |
{ |
| 125 |
private: |
| 126 |
GuiExpressionPtr _contained; |
| 127 |
|
| 128 |
sigc::signal<void> _sigValueChanged; |
| 129 |
|
| 130 |
public: |
| 131 |
TypedExpression(const GuiExpressionPtr& contained) : |
| 132 |
_contained(contained) |
| 133 |
{ |
| 134 |
if (_contained) |
| 135 |
{ |
| 136 |
// Connect the changed signal of the contained expression |
| 137 |
// to fire our own signal |
| 138 |
_contained->signal_valueChanged().connect([this]() |
| 139 |
{ |
| 140 |
signal_valueChanged().emit(); |
| 141 |
}); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
virtual bool evaluate() override |
| 146 |
{ |
| 147 |
return _contained->getFloatValue() != 0.0f; |
| 148 |
} |
| 149 |
|
| 150 |
sigc::signal<void>& signal_valueChanged() override |
| 151 |
{ |
| 152 |
return _sigValueChanged; |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
// An expression representing a Vector4 value |
| 157 |
// Translating between a quadruple of script expressions and IGuiExpression<Vector4> |
| 158 |
class Vector4Expression : |
| 159 |
public IGuiExpression<Vector4> |
| 160 |
{ |
| 161 |
private: |
| 162 |
std::vector<GuiExpressionPtr> _vec; |
| 163 |
|
| 164 |
sigc::signal<void> _sigValueChanged; |
| 165 |
|
| 166 |
public: |
| 167 |
Vector4Expression(const GuiExpressionPtr& x, const GuiExpressionPtr& y, |
| 168 |
const GuiExpressionPtr& z, const GuiExpressionPtr& w) : |
| 169 |
_vec(4) // initialise to size 4 |
| 170 |
{ |
| 171 |
_vec[0] = x; |
| 172 |
_vec[1] = y; |
| 173 |
_vec[2] = z; |
| 174 |
_vec[3] = w; |
| 175 |
|
| 176 |
for (const GuiExpressionPtr& comp : _vec) |
| 177 |
{ |
| 178 |
if (!comp) continue; |
| 179 |
|
| 180 |
comp->signal_valueChanged().connect([this]() |
| 181 |
{ |
| 182 |
signal_valueChanged().emit(); |
| 183 |
}); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
virtual Vector4 evaluate() override |
| 188 |
{ |
| 189 |
return Vector4(_vec[0]->getFloatValue(), _vec[1]->getFloatValue(), |
| 190 |
_vec[2]->getFloatValue(), _vec[3]->getFloatValue()); |
| 191 |
} |
| 192 |
|
| 193 |
sigc::signal<void>& signal_valueChanged() override |
| 194 |
{ |
| 195 |
return _sigValueChanged; |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
class GuiStateVariableExpression : |
| 200 |
public GuiExpression |
| 201 |
{ |
| 202 |
private: |
| 203 |
IGui& _gui; |
| 204 |
std::string _variableName; |
| 205 |
|
| 206 |
public: |
| 207 |
GuiStateVariableExpression(IGui& gui, const std::string& variableName); |
| 208 |
|
| 209 |
virtual float getFloatValue() override; |
| 210 |
virtual std::string getStringValue() override; |
| 211 |
}; |
| 212 |
|
| 213 |
} |
| 214 |
|