| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- /*
- * This source file is part of libRocket, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://www.librocket.com
- *
- * Copyright (c) 2014 Markus Schöngart
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include "precompiled.h"
- #include "../../Include/Rocket/Core/TransformPrimitive.h"
- #include <iostream>
- namespace Rocket {
- namespace Core {
- namespace Transforms {
- NumericValue::NumericValue() throw()
- : number(), unit(Property::UNKNOWN)
- {
- }
- NumericValue::NumericValue(float number, Property::Unit unit) throw()
- : number(number), unit(unit)
- {
- }
- float NumericValue::Resolve(Element& e, float base) const throw()
- {
- Property prop;
- prop.value = Variant(number);
- prop.unit = unit;
- return e.ResolveProperty(&prop, base);
- }
- float NumericValue::ResolveWidth(Element& e) const throw()
- {
- if(unit & (Property::PX | Property::NUMBER)) return number;
- return Resolve(e, e.GetBox().GetSize().x);
- }
- float NumericValue::ResolveHeight(Element& e) const throw()
- {
- if (unit & (Property::PX | Property::NUMBER)) return number;
- return Resolve(e, e.GetBox().GetSize().y);
- }
- float NumericValue::ResolveDepth(Element& e) const throw()
- {
- if (unit & (Property::PX | Property::NUMBER)) return number;
- Vector2f size = e.GetBox().GetSize();
- return Resolve(e, Math::Max(size.x, size.y));
- }
- float NumericValue::ResolveAbsoluteUnit(Property::Unit base_unit) const throw()
- {
- switch (base_unit)
- {
- case Property::RAD:
- {
- switch (unit)
- {
- case Property::NUMBER:
- case Property::DEG:
- return Math::DegreesToRadians(number);
- case Property::RAD:
- return number;
- case Property::PERCENT:
- return number * 0.01f * 2.0f * Math::ROCKET_PI;
- break;
- }
- }
- }
- return number;
- }
- struct ResolveTransformVisitor
- {
- Matrix4f& m;
- Element& e;
- bool operator()(const Matrix2D& p)
- {
- m = Matrix4f::FromRows(
- Vector4f(p.values[0], p.values[2], 0, p.values[4]),
- Vector4f(p.values[1], p.values[3], 0, p.values[5]),
- Vector4f(0, 0, 1, 0),
- Vector4f(0, 0, 0, 1)
- );
- return true;
- }
- bool operator()(const Matrix3D& p)
- {
- m = Matrix4f::FromRows(
- Vector4f(p.values[0], p.values[1], p.values[2], p.values[3]),
- Vector4f(p.values[4], p.values[5], p.values[6], p.values[7]),
- Vector4f(p.values[8], p.values[9], p.values[10], p.values[11]),
- Vector4f(p.values[12], p.values[13], p.values[14], p.values[15])
- );
- return true;
- }
- bool operator()(const TranslateX& p)
- {
- m = Matrix4f::TranslateX(p.values[0].ResolveWidth(e));
- return true;
- }
- bool operator()(const TranslateY& p)
- {
- m = Matrix4f::TranslateY(p.values[0].ResolveHeight(e));
- return true;
- }
- bool operator()(const TranslateZ& p)
- {
- m = Matrix4f::TranslateZ(p.values[0].ResolveDepth(e));
- return true;
- }
- bool operator()(const Translate2D& p)
- {
- m = Matrix4f::Translate(
- p.values[0].ResolveWidth(e),
- p.values[1].ResolveHeight(e),
- 0
- );
- return true;
- }
- bool operator()(const Translate3D& p)
- {
- m = Matrix4f::Translate(
- p.values[0].ResolveWidth(e),
- p.values[1].ResolveHeight(e),
- p.values[2].ResolveDepth(e)
- );
- return true;
- }
- bool operator()(const ScaleX& p)
- {
- m = Matrix4f::ScaleX(p.values[0]);
- return true;
- }
- bool operator()(const ScaleY& p)
- {
- m = Matrix4f::ScaleY(p.values[0]);
- return true;
- }
- bool operator()(const ScaleZ& p)
- {
- m = Matrix4f::ScaleZ(p.values[0]);
- return true;
- }
- bool operator()(const Scale2D& p)
- {
- m = Matrix4f::Scale(p.values[0], p.values[1], 1);
- return true;
- }
- bool operator()(const Scale3D& p)
- {
- m = Matrix4f::Scale(p.values[0], p.values[1], p.values[2]);
- return true;
- }
- bool operator()(const RotateX& p)
- {
- m = Matrix4f::RotateX(p.values[0]);
- return true;
- }
- bool operator()(const RotateY& p)
- {
- m = Matrix4f::RotateY(p.values[0]);
- return true;
- }
- bool operator()(const RotateZ& p)
- {
- m = Matrix4f::RotateZ(p.values[0]);
- return true;
- }
- bool operator()(const Rotate2D& p)
- {
- m = Matrix4f::RotateZ(p.values[0]);
- return true;
- }
- bool operator()(const Rotate3D& p)
- {
- m = Matrix4f::Rotate(Vector3f(p.values[0], p.values[1], p.values[2]), p.values[3]);
- return true;
- }
- bool operator()(const SkewX& p)
- {
- m = Matrix4f::SkewX(p.values[0]);
- return true;
- }
- bool operator()(const SkewY& p)
- {
- m = Matrix4f::SkewY(p.values[0]);
- return true;
- }
- bool operator()(const Skew2D& p)
- {
- m = Matrix4f::Skew(p.values[0], p.values[1]);
- return true;
- }
- bool operator()(const Perspective& p)
- {
- return false;
- }
- };
- struct SetIdentityVisitor
- {
- template <size_t N>
- void operator()(ResolvedValuesPrimitive<N>& p)
- {
- for (auto& value : p.values)
- value = 0.0f;
- }
- template <size_t N>
- void operator()(UnresolvedValuesPrimitive<N>& p)
- {
- for (auto& value : p.values)
- value.number = 0.0f;
- }
- void operator()(Matrix2D& p)
- {
- for (int i = 0; i < 6; i++)
- p.values[i] = ((i == 0 || i == 3) ? 1.0f : 0.0f);
- }
- void operator()(Matrix3D& p)
- {
- for (int i = 0; i < 16; i++)
- p.values[i] = ((i % 5) == 0 ? 1.0f : 0.0f);
- }
- void operator()(ScaleX& p)
- {
- p.values[0] = 1;
- }
- void operator()(ScaleY& p)
- {
- p.values[0] = 1;
- }
- void operator()(ScaleZ& p)
- {
- p.values[0] = 1;
- }
- void operator()(Scale2D& p)
- {
- p.values[0] = p.values[1] = 1;
- }
- void operator()(Scale3D& p)
- {
- p.values[0] = p.values[1] = p.values[2] = 1;
- }
- };
- void Primitive::SetIdentity() throw()
- {
- std::visit(SetIdentityVisitor{}, primitive);
- }
- bool Primitive::ResolveTransform(Matrix4f & m, Element & e) const throw()
- {
- ResolveTransformVisitor visitor{ m, e };
- bool result = std::visit(visitor, primitive);
- return result;
- }
- bool Primitive::ResolvePerspective(float & p, Element & e) const throw()
- {
- bool result = false;
- if (const Perspective* perspective = std::get_if<Perspective>(&primitive))
- {
- p = perspective->values[0].ResolveDepth(e);
- result = true;
- }
- return result;
- }
- struct InterpolateVisitor
- {
- const PrimitiveVariant& other_variant;
- float alpha;
- template <size_t N>
- void Interpolate(ResolvedValuesPrimitive<N>& p0, const ResolvedValuesPrimitive<N>& p1)
- {
- for (size_t i = 0; i < N; i++)
- p0.values[i] = p0.values[i] * (1.0f - alpha) + p1.values[i] * alpha;
- }
- template <size_t N>
- void Interpolate(UnresolvedValuesPrimitive<N>& p0, const UnresolvedValuesPrimitive<N>& p1)
- {
- // Assumes that the underlying units have been resolved (e.g. to pixels)
- for (size_t i = 0; i < N; i++)
- p0.values[i].number = p0.values[i].number*(1.0f - alpha) + p1.values[i].number * alpha;
- }
- //void Interpolate(Matrix3D& p0, Matrix3D& p1)
- //{
- // // Special interpolation for full matrices TODO
- //}
- template <typename T>
- void operator()(T& p0)
- {
- auto& p1 = std::get<T>(other_variant);
- Interpolate(p0, p1);
- }
- };
- bool Primitive::InterpolateWith(const Primitive & other, float alpha) throw()
- {
- if (primitive.index() != other.primitive.index())
- return false;
- std::visit(InterpolateVisitor{ other.primitive, alpha }, primitive);
- return true;
- }
- struct ResolveUnitsVisitor
- {
- Element& e;
- bool operator()(TranslateX& p)
- {
- p.values[0] = NumericValue{ p.values[0].ResolveWidth(e), Property::PX };
- return true;
- }
- bool operator()(TranslateY& p)
- {
- p.values[0] = NumericValue{ p.values[0].ResolveHeight(e), Property::PX };
- return true;
- }
- bool operator()(TranslateZ& p)
- {
- p.values[0] = NumericValue{ p.values[0].ResolveDepth(e), Property::PX };
- return true;
- }
- bool operator()(Translate2D& p)
- {
- p.values[0] = NumericValue{ p.values[0].ResolveWidth(e), Property::PX };
- p.values[1] = NumericValue{ p.values[1].ResolveHeight(e), Property::PX };
- return true;
- }
- bool operator()(Translate3D& p)
- {
- p.values[0] = NumericValue{ p.values[0].ResolveWidth(e), Property::PX };
- p.values[1] = NumericValue{ p.values[1].ResolveHeight(e), Property::PX };
- p.values[2] = NumericValue{ p.values[2].ResolveDepth(e), Property::PX };
- return true;
- }
- template <size_t N>
- bool operator()(ResolvedValuesPrimitive<N>& p)
- {
- // No conversion needed for resolved transforms
- return true;
- }
- bool operator()(Perspective& p)
- {
- // Perspective is special and not used for transform animations, ignore.
- return false;
- }
- };
- bool Primitive::ResolveUnits(Element & e) throw()
- {
- return std::visit(ResolveUnitsVisitor{ e }, primitive);
- }
- }
- }
- }
|