/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #include "ui/ui_item.h" #include "ui/ui_item_automated_property_provider.h" #include "log/log.h" using namespace GS; using namespace GS::S2D; //------------------------------------------------------------------------------ void Item::SetParent(Item *item) { if (parent) parent->GetChildren().Remove(this); if ((parent = item) != NULL) parent->GetChildren().Add(this); } bool Item::Inherits(const Item *item) const { if (this == item) return true; for (Item *p = GetParent(); p; p = p->GetParent()) if (p == item) return true; return false; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ void Item::SnapshotTransformation(const Matrix3 &matrix) { Vector4 p = matrix.GetRow(2); SetPosition(p.x, p.y); Vector2 u(matrix.m[0][0], matrix.m[1][0]), v(matrix.m[0][1], matrix.m[1][1]); SetScale(u.Len(), v.Len()); u.Normalize(); float a = Math::ACos(u.x); if (u.y < 0.f) a = 2.f * Math::Pi - a; SetRotation(a); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ void Item::ComputeMatrix() { local_matrix = Matrix3::RotationMatrixZAxis(angle); local_matrix.m[0][2] = position.x; local_matrix.m[1][2] = position.y; local_matrix = local_matrix * Matrix3(scale.x, 0, 0, 0, scale.y, 0, -pivot.x * scale.x, -pivot.y * scale.y, 1); if (GetParent()) { GetParent()->ComputeMatrix(); matrix = GetParent()->GetMatrix() * local_matrix; } else matrix = local_matrix; matrix.Inverse(imatrix); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ iRect Item::GetRect() const { return iRect(0, 0, (int)size.x, (int)size.y); } iRect Item::GetScreenRect() const { Vector4 _v[4], _o[4]; _v[0].Set(0, 0, 1); _v[1].Set(size.x, 0, 1); _v[2].Set(size.x, size.y, 1); _v[3].Set(0, size.y, 1); matrix.Apply(_o, _v, 4); Vector4 mn = _o[0], mx = _o[0]; for (int n = 1; n < 4; ++n) { mn = Vector4::Minimum(mn, _o[n]); mx = Vector4::Maximum(mx, _o[n]); } return iRect((int)mn.x, (int)mn.y, (int)mx.x, (int)mx.y); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ iRect Item::LocalToScreen(const iRect &r) const { Vector4 sxy((float)r.sx, (float)r.sy, 1, 1), exy((float)r.ex, (float)r.ey, 1, 1); Vector4 tsxy = sxy * matrix, texy = exy * matrix; return iRect((int)tsxy.x, (int)tsxy.y, (int)texy.x, (int)texy.y); } iRect Item::ScreenToLocal(const iRect &r) const { Vector4 sxy((float)r.sx, (float)r.sy, 1, 1), exy((float)r.ex, (float)r.ey, 1, 1); Vector4 tsxy = sxy * imatrix, texy = exy * imatrix; return iRect((int)tsxy.x, (int)tsxy.y, (int)texy.x, (int)texy.y); } Vector2 Item::LocalToScreen(const Vector2 &v) const { Vector4 o = Vector4(v.x, v.y, 1) * matrix; return Vector2(o.x, o.y); } Vector2 Item::ScreenToLocal(const Vector2 &v) const { Vector4 o = Vector4(v.x, v.y, 1) * imatrix; return Vector2(o.x, o.y); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ void Item::Setup() { item_event->OnSetup(this); item_event->OnSetupDone(this); } void Item::Reset() { SetPivot(0, 0); SetScale(1, 1); SetZOrder(0.5); SetRotation(0); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ Item::Item() { uid = uint(~0); item_flags.Raise(Flag_ItemActive); type = Type_None; parent = 0; Reset(); item_event = new IItemEvent; automation_player = new GS::Automation::Player; automation_player->property_provider = new Automation::ItemPropertyProvider(this); } Item::~Item() { SetParent(NULL); ListForeachPtr(Item *, c, children) c->SetParent(NULL); } //------------------------------------------------------------------------------