Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/util/MultiTypeMath.cc @ 848

Last change on this file since 848 was 848, checked in by landauf, 16 years ago

several changes:

  • XMLPort is now theoretically able to load something (but still buggy)
  • Expanded Convert with several partial template specializations
  • Expanded all MultiTypes with new functions, mostly to convert values
  • Expanded SubString with a new functionality: chars inside parentheses aren't split

It's not yet working as it should (at least not in all cases - loading the objects name works)

File size: 6.7 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
48MultiTypeMath::MultiTypeMath(const MultiTypeMath& mtm) : MultiTypeString(mtm)
49{
50    this->type_ = mtm.type_;
51    this->value_ = mtm.value_;
52}
53
54MultiTypeMath& MultiTypeMath::operator=(const MultiTypeMath& mtm)
55{
56    this->type_ = mtm.type_;
57    this->value_ = mtm.value_;
58    return *this;
59}
60
61bool MultiTypeMath::operator==(const MultiTypeMath& mtm) const
62{
63    if (!MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
64    {
65        if (this->type_ == MT_vector2)
66            return (this->vector2_ == mtm.vector2_);
67        else if (this->type_ == MT_vector3)
68            return (this->vector3_ == mtm.vector3_);
69        else if (this->type_ == MT_colourvalue)
70            return (this->colourvalue_ == mtm.colourvalue_);
71        else if (this->type_ == MT_quaternion)
72            return (this->quaternion_ == mtm.quaternion_);
73        else if (this->type_ == MT_radian)
74            return (this->radian_ == mtm.radian_);
75        else if (this->type_ == MT_degree)
76            return (this->degree_ == mtm.degree_);
77    }
78
79    return false;
80}
81
82bool MultiTypeMath::operator!=(const MultiTypeMath& mtm) const
83{
84    if (MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
85    {
86        if (this->type_ == MT_vector2)
87            return (this->vector2_ != mtm.vector2_);
88        else if (this->type_ == MT_vector3)
89            return (this->vector3_ != mtm.vector3_);
90        else if (this->type_ == MT_colourvalue)
91            return (this->colourvalue_ != mtm.colourvalue_);
92        else if (this->type_ == MT_quaternion)
93            return (this->quaternion_ != mtm.quaternion_);
94        else if (this->type_ == MT_radian)
95            return (this->radian_ != mtm.radian_);
96        else if (this->type_ == MT_degree)
97            return (this->degree_ != mtm.degree_);
98    }
99
100    return true;
101}
102
103MultiTypeMath::operator orxonox::Vector2() const
104{
105    return (this->type_ == MT_vector2) ? this->vector2_ : ConvertValueAndReturn<MultiTypePrimitive, orxonox::Vector2>(*this);
106}
107
108MultiTypeMath::operator orxonox::Vector3() const
109{
110    return (this->type_ == MT_vector3) ? this->vector3_ : ConvertValueAndReturn<MultiTypePrimitive, orxonox::Vector3>(*this);
111}
112
113MultiTypeMath::operator orxonox::Quaternion() const
114{
115    return (this->type_ == MT_quaternion) ? this->quaternion_ : ConvertValueAndReturn<MultiTypePrimitive, orxonox::Quaternion>(*this);
116}
117
118MultiTypeMath::operator orxonox::ColourValue() const
119{
120    return (this->type_ == MT_colourvalue) ? this->colourvalue_ : ConvertValueAndReturn<MultiTypePrimitive, orxonox::ColourValue>(*this);
121}
122
123MultiTypeMath::operator orxonox::Radian() const
124{
125    return (this->type_ == MT_radian) ? this->radian_ : ConvertValueAndReturn<MultiTypePrimitive, orxonox::Radian>(*this);
126}
127
128MultiTypeMath::operator orxonox::Degree() const
129{
130    return (this->type_ == MT_degree) ? this->degree_ : ConvertValueAndReturn<MultiTypePrimitive, orxonox::Degree>(*this);
131}
132
133void MultiTypeMath::setValue(const MultiTypeMath& mtm)
134{
135    this->type_ = mtm.type_;
136    this->value_ = mtm.value_;
137}
138
139std::string MultiTypeMath::toString() const
140{
141    std::string output;
142
143    if (this->type_ == MT_vector2)
144        ConvertValue(&output, this->vector2_);
145    else if (this->type_ == MT_vector3)
146        ConvertValue(&output, this->vector3_);
147    else if (this->type_ == MT_colourvalue)
148        ConvertValue(&output, this->colourvalue_);
149    else if (this->type_ == MT_quaternion)
150        ConvertValue(&output, this->quaternion_);
151    else if (this->type_ == MT_radian)
152        ConvertValue(&output, this->radian_);
153    else if (this->type_ == MT_degree)
154        ConvertValue(&output, this->degree_);
155    else
156        return MultiTypeString::toString();
157
158    return output;
159}
160
161bool MultiTypeMath::fromString(const std::string value)
162{
163    if (this->type_ == MT_vector2)
164        return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0));
165    else if (this->type_ == MT_vector3)
166        return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0));
167    else if (this->type_ == MT_colourvalue)
168        return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0));
169    else if (this->type_ == MT_quaternion)
170        return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0));
171    else if (this->type_ == MT_radian)
172        return ConvertValue(&this->radian_, value, orxonox::Radian(0));
173    else if (this->type_ == MT_degree)
174        return ConvertValue(&this->degree_, value, orxonox::Degree(0));
175    else
176        return MultiTypeString::fromString(value);
177}
178
179std::ostream& operator<<(std::ostream& out, MultiTypeMath& mtm)
180{
181    if (mtm.isA(MT_vector2))
182        out << mtm.getVector2();
183    else if (mtm.isA(MT_vector3))
184        out << mtm.getVector3();
185    else if (mtm.isA(MT_colourvalue))
186        out << mtm.getColourValue();
187    else if (mtm.isA(MT_quaternion))
188        out << mtm.getQuaternion();
189    else if (mtm.isA(MT_radian))
190        out << mtm.getRadian();
191    else if (mtm.isA(MT_degree))
192        out << mtm.getDegree();
193    else
194        out << ((MultiTypeString)mtm);
195
196    return out;
197}
Note: See TracBrowser for help on using the repository browser.