| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Fabian 'x3n' Landau |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | * Inspiration: MultiType by Benjamin Grauer |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include "MultiTypeMath.h" |
|---|
| 31 | #include "Convert.h" |
|---|
| 32 | |
|---|
| 33 | MultiTypeMath::MultiTypeMath(MultiType type) : MultiTypeString(type) |
|---|
| 34 | { |
|---|
| 35 | if (type == MT_vector2) |
|---|
| 36 | this->vector2_ = orxonox::Vector2(0, 0); |
|---|
| 37 | else if (type == MT_vector3) |
|---|
| 38 | this->vector3_ = orxonox::Vector3(0, 0, 0); |
|---|
| 39 | else if (type == MT_colourvalue) |
|---|
| 40 | this->colourvalue_ = orxonox::ColourValue(0, 0, 0, 0); |
|---|
| 41 | else if (type == MT_quaternion) |
|---|
| 42 | this->quaternion_ = orxonox::Quaternion(1, 0, 0, 0); |
|---|
| 43 | else if (type == MT_radian) |
|---|
| 44 | this->radian_ = orxonox::Radian(0); |
|---|
| 45 | else if (type == MT_degree) |
|---|
| 46 | this->degree_ = orxonox::Degree(0); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | bool MultiTypeMath::operator==(const MultiTypeMath& mtm) const |
|---|
| 50 | { |
|---|
| 51 | if (!MultiTypeString::operator==(mtm) && this->type_ == mtm.type_) |
|---|
| 52 | { |
|---|
| 53 | if (this->type_ == MT_vector2) |
|---|
| 54 | return (this->vector2_ == mtm.vector2_); |
|---|
| 55 | else if (this->type_ == MT_vector3) |
|---|
| 56 | return (this->vector3_ == mtm.vector3_); |
|---|
| 57 | else if (this->type_ == MT_colourvalue) |
|---|
| 58 | return (this->colourvalue_ == mtm.colourvalue_); |
|---|
| 59 | else if (this->type_ == MT_quaternion) |
|---|
| 60 | return (this->quaternion_ == mtm.quaternion_); |
|---|
| 61 | else if (this->type_ == MT_radian) |
|---|
| 62 | return (this->radian_ == mtm.radian_); |
|---|
| 63 | else if (this->type_ == MT_degree) |
|---|
| 64 | return (this->degree_ == mtm.degree_); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | return false; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | bool MultiTypeMath::operator!=(const MultiTypeMath& mtm) const |
|---|
| 71 | { |
|---|
| 72 | if (MultiTypeString::operator==(mtm) && this->type_ == mtm.type_) |
|---|
| 73 | { |
|---|
| 74 | if (this->type_ == MT_vector2) |
|---|
| 75 | return (this->vector2_ != mtm.vector2_); |
|---|
| 76 | else if (this->type_ == MT_vector3) |
|---|
| 77 | return (this->vector3_ != mtm.vector3_); |
|---|
| 78 | else if (this->type_ == MT_colourvalue) |
|---|
| 79 | return (this->colourvalue_ != mtm.colourvalue_); |
|---|
| 80 | else if (this->type_ == MT_quaternion) |
|---|
| 81 | return (this->quaternion_ != mtm.quaternion_); |
|---|
| 82 | else if (this->type_ == MT_radian) |
|---|
| 83 | return (this->radian_ != mtm.radian_); |
|---|
| 84 | else if (this->type_ == MT_degree) |
|---|
| 85 | return (this->degree_ != mtm.degree_); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | MultiTypeMath::operator void*() const |
|---|
| 92 | { return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeMath, void*>(*this, 0); } |
|---|
| 93 | MultiTypeMath::operator int() const |
|---|
| 94 | { return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeMath, int>(*this, 0); } |
|---|
| 95 | MultiTypeMath::operator unsigned int() const |
|---|
| 96 | { return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeMath, unsigned int>(*this, 0); } |
|---|
| 97 | MultiTypeMath::operator char() const |
|---|
| 98 | { return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeMath, char>(*this, 0); } |
|---|
| 99 | MultiTypeMath::operator unsigned char() const |
|---|
| 100 | { return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeMath, unsigned char>(*this, 0); } |
|---|
| 101 | MultiTypeMath::operator short() const |
|---|
| 102 | { return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeMath, short>(*this, 0); } |
|---|
| 103 | MultiTypeMath::operator unsigned short() const |
|---|
| 104 | { return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeMath, unsigned short>(*this, 0); } |
|---|
| 105 | MultiTypeMath::operator long() const |
|---|
| 106 | { return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeMath, long>(*this, 0); } |
|---|
| 107 | MultiTypeMath::operator unsigned long() const |
|---|
| 108 | { return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeMath, unsigned long>(*this, 0); } |
|---|
| 109 | MultiTypeMath::operator float() const |
|---|
| 110 | { return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeMath, float>(*this, 0); } |
|---|
| 111 | MultiTypeMath::operator double() const |
|---|
| 112 | { return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeMath, double>(*this, 0); } |
|---|
| 113 | MultiTypeMath::operator long double() const |
|---|
| 114 | { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeMath, long double>(*this, 0); } |
|---|
| 115 | MultiTypeMath::operator bool() const |
|---|
| 116 | { return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeMath, bool>(*this, 0); } |
|---|
| 117 | MultiTypeMath::operator std::string() const |
|---|
| 118 | { return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this); } |
|---|
| 119 | MultiTypeMath::operator const char*() const |
|---|
| 120 | { return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this)).c_str(); } |
|---|
| 121 | MultiTypeMath::operator orxonox::Vector2() const |
|---|
| 122 | { return (this->type_ == MT_vector2) ? this->vector2_ : getConvertedValue<MultiTypeMath, orxonox::Vector2>(*this); } |
|---|
| 123 | MultiTypeMath::operator orxonox::Vector3() const |
|---|
| 124 | { return (this->type_ == MT_vector3) ? this->vector3_ : getConvertedValue<MultiTypeMath, orxonox::Vector3>(*this); } |
|---|
| 125 | MultiTypeMath::operator orxonox::Quaternion() const |
|---|
| 126 | { return (this->type_ == MT_quaternion) ? this->quaternion_ : getConvertedValue<MultiTypeMath, orxonox::Quaternion>(*this); } |
|---|
| 127 | MultiTypeMath::operator orxonox::ColourValue() const |
|---|
| 128 | { return (this->type_ == MT_colourvalue) ? this->colourvalue_ : getConvertedValue<MultiTypeMath, orxonox::ColourValue>(*this); } |
|---|
| 129 | MultiTypeMath::operator orxonox::Radian() const |
|---|
| 130 | { return (this->type_ == MT_radian) ? this->radian_ : getConvertedValue<MultiTypeMath, orxonox::Radian>(*this); } |
|---|
| 131 | MultiTypeMath::operator orxonox::Degree() const |
|---|
| 132 | { return (this->type_ == MT_degree) ? this->degree_ : getConvertedValue<MultiTypeMath, orxonox::Degree>(*this); } |
|---|
| 133 | |
|---|
| 134 | void MultiTypeMath::setValue(const MultiTypeMath& mtm) |
|---|
| 135 | { |
|---|
| 136 | MultiTypeString::setValue(mtm); |
|---|
| 137 | this->vector2_ = mtm.vector2_; |
|---|
| 138 | this->vector3_ = mtm.vector3_; |
|---|
| 139 | this->quaternion_ = mtm.quaternion_; |
|---|
| 140 | this->colourvalue_ = mtm.colourvalue_; |
|---|
| 141 | this->radian_ = mtm.radian_; |
|---|
| 142 | this->degree_ = mtm.degree_; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | std::string MultiTypeMath::getTypename() const |
|---|
| 146 | { |
|---|
| 147 | if (this->type_ == MT_vector2) |
|---|
| 148 | return "Vector2"; |
|---|
| 149 | else if (this->type_ == MT_vector3) |
|---|
| 150 | return "Vector3"; |
|---|
| 151 | else if (this->type_ == MT_colourvalue) |
|---|
| 152 | return "ColourValue"; |
|---|
| 153 | else if (this->type_ == MT_quaternion) |
|---|
| 154 | return "Quaternion"; |
|---|
| 155 | else if (this->type_ == MT_radian) |
|---|
| 156 | return "Radian"; |
|---|
| 157 | else if (this->type_ == MT_degree) |
|---|
| 158 | return "Degree"; |
|---|
| 159 | else |
|---|
| 160 | return MultiTypeString::getTypename(); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | std::string MultiTypeMath::toString() const |
|---|
| 164 | { |
|---|
| 165 | std::string output; |
|---|
| 166 | |
|---|
| 167 | if (this->type_ == MT_vector2) |
|---|
| 168 | ConvertValue(&output, this->vector2_); |
|---|
| 169 | else if (this->type_ == MT_vector3) |
|---|
| 170 | ConvertValue(&output, this->vector3_); |
|---|
| 171 | else if (this->type_ == MT_colourvalue) |
|---|
| 172 | ConvertValue(&output, this->colourvalue_); |
|---|
| 173 | else if (this->type_ == MT_quaternion) |
|---|
| 174 | ConvertValue(&output, this->quaternion_); |
|---|
| 175 | else if (this->type_ == MT_radian) |
|---|
| 176 | ConvertValue(&output, this->radian_); |
|---|
| 177 | else if (this->type_ == MT_degree) |
|---|
| 178 | ConvertValue(&output, this->degree_); |
|---|
| 179 | else |
|---|
| 180 | return MultiTypeString::toString(); |
|---|
| 181 | |
|---|
| 182 | return output; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | bool MultiTypeMath::fromString(const std::string value) |
|---|
| 186 | { |
|---|
| 187 | if (this->type_ == MT_vector2) |
|---|
| 188 | return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0)); |
|---|
| 189 | else if (this->type_ == MT_vector3) |
|---|
| 190 | return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0)); |
|---|
| 191 | else if (this->type_ == MT_colourvalue) |
|---|
| 192 | return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0)); |
|---|
| 193 | else if (this->type_ == MT_quaternion) |
|---|
| 194 | return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0)); |
|---|
| 195 | else if (this->type_ == MT_radian) |
|---|
| 196 | return ConvertValue(&this->radian_, value, orxonox::Radian(0)); |
|---|
| 197 | else if (this->type_ == MT_degree) |
|---|
| 198 | return ConvertValue(&this->degree_, value, orxonox::Degree(0)); |
|---|
| 199 | else |
|---|
| 200 | return MultiTypeString::fromString(value); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | std::ostream& operator<<(std::ostream& out, MultiTypeMath& mtm) |
|---|
| 204 | { |
|---|
| 205 | out << mtm.toString(); |
|---|
| 206 | return out; |
|---|
| 207 | } |
|---|