Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/MultiTypeMath.cc @ 1047

Last change on this file since 1047 was 871, checked in by landauf, 17 years ago

merged core branch to trunk

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