Fix issue in Rocket.lua script.
[Cafu-Engine.git] / Libs / MaterialSystem / Expression.hpp
bloba2bf96024ab163482bf7dd62c6f6a0c39550e978
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
7 #ifndef CAFU_MATSYS_EXPRESSION_HPP_INCLUDED
8 #define CAFU_MATSYS_EXPRESSION_HPP_INCLUDED
10 #include <string>
11 #include "Templates/Array.hpp"
14 class TextParserT;
17 class TableT
19 public:
21 struct RowT
23 bool RowSnap;
24 bool RowClamp;
25 ArrayT<float> Data;
28 std::string Name;
29 bool ColumnSnap;
30 bool ColumnClamp;
31 ArrayT<RowT> Rows;
34 /// Constructor.
35 TableT() : ColumnSnap(false), ColumnClamp(false) { }
37 /// Looks up the table value at x.
38 float Lookup(float x) const;
42 /// A typed expression class.
43 class ExpressionT
45 public:
47 /// The type of the expression.
48 /// If bit 0x10000 is set, the type is the generic float variable whose index is obtained from the 16 lower (0x0FFFF) bits.
49 /// If bit 0x20000 is set, the type is the generic int variable whose index is obtained from the 16 lower (0x0FFFF) bits.
50 enum TypeT { FloatNumber, IntNumber, SymbolTime, SymbolALRed, SymbolALGreen, SymbolALBlue, TableLookup,
51 CastInt, Add, Sub, Mul, Div, GENERIC_FLOAT_START=0x10000, GENERIC_INT_START=0x20000 };
53 /// A helper structure for returning both the type and the value of an expression.
54 struct ResultT
56 TypeT Type; ///< The type of the result, can only be FloatNumber or IntNumber.
57 float ValueFloat; ///< If Type==FloatNumber, this is the value of the result. Otherwise it is undefined.
58 int ValueInt; ///< If Type==IntNumber, this is the value of the result. Otherwise it is undefined.
60 ResultT(float Value) : Type(FloatNumber), ValueFloat(Value), ValueInt(0 ) { } ///< Constructor.
61 ResultT(int Value) : Type(IntNumber ), ValueFloat(0.0 ), ValueInt(Value) { } ///< Constructor.
63 float GetAsFloat() const { return Type==FloatNumber ? ValueFloat : ValueInt; }
64 int GetAsInt () const { return Type==FloatNumber ? int(ValueFloat) : ValueInt; }
67 /// This structure contains the values for the "variables" of an expression.
68 /// An instance of it must be passed to the Evaluate() method.
69 struct SymbolsT
71 SymbolsT();
73 float Time;
74 float AmbientLightColor[3];
75 ArrayT<float> GenFloat; ///< Generic / general purpose float variables/symbols.
76 ArrayT<int> GenInt; ///< Generic / general purpose int variables/symbols.
80 /// Constructor.
81 ExpressionT(float FloatNumberValue_=0.0);
83 /// Constructor.
84 ExpressionT(int IntNumberValue_);
86 /// This constructor creates an ExpressionT of a given type.
87 /// Note that only those types are meaningful for which no other parameter is required,
88 /// such as \c SymbolTime, \c SymbolALRed, \c SymbolALGreen and \c SymbolALBlue.
89 ExpressionT(TypeT Type_);
91 /// Constructor.
92 ExpressionT(TextParserT& TP, const ArrayT<TableT*>& ListOfTables) /*throw (TextParserT::ParseError)*/;
94 /// Copy Constructor (Law of the Big Three).
95 ExpressionT(const ExpressionT& Source);
97 /// Destructor (Law of the Big Three).
98 ~ExpressionT();
100 /// Assignment Operator (Law of the Big Three).
101 ExpressionT& operator = (const ExpressionT& Source);
103 /// Returns a string description of this ExpressionT.
104 std::string GetString() const;
106 /// Evaluates this expression and returns the result.
107 ResultT Evaluate(const SymbolsT& Symbols) const;
110 private:
112 TypeT Type;
113 float FloatNumberValue;
114 int IntNumberValue;
115 TableT* Table;
116 ExpressionT* Child1;
117 ExpressionT* Child2;
119 /// The equal operator is not defined.
120 bool operator == (const ExpressionT& rhs) const;
123 #endif