Пример #1
0
	void Transform2D::ApplyAnchors(const Box& prevBounds)
	{
		const Transform2D* parent = m_node.GetParent();
		const Transform2D* constThis = this;
		if (nullptr == parent)
			return;

		const Box& parentBounds = parent->GetWorldBounds();
		Box newBounds = constThis->GetWorldBounds();

		// min.x
		{
			// 
			const float anchorInf = 1.0f - (m_anchor.lowerBound.x * 2.0f);
			const float diff = parentBounds.lowerBound.x - prevBounds.lowerBound.x;
			const float endDistance = diff * anchorInf;
			m_dimensions.x += -endDistance;
			newBounds.lowerBound.x += endDistance;
		}

		// max.x
		{
			const float anchorInf = 1.0f - ((1.0f - m_anchor.upperBound.x) * 2.0f);
			const float diff = parentBounds.upperBound.x - prevBounds.upperBound.x;
			const float endDistance = diff * anchorInf;
			m_dimensions.x += endDistance;
			newBounds.upperBound.x += endDistance;
		}

		// min.y
		{
			const float anchorInf = 1.0f - (m_anchor.lowerBound.y * 2.0f);
			const float diff = parentBounds.lowerBound.y - prevBounds.lowerBound.y;
			const float endDistance = diff * anchorInf;
			m_dimensions.y += -endDistance;
			newBounds.lowerBound.y += endDistance;
		}

		// max.y
		{
			const float anchorInf = 1.0f - ((1.0f - m_anchor.upperBound.y) * 2.0f);
			const float diff = parentBounds.upperBound.y - prevBounds.upperBound.y;
			const float endDistance = diff * anchorInf;
			m_dimensions.y += endDistance;
			newBounds.upperBound.y += endDistance;
		}

		m_position = (newBounds.CalculatePointWithPivot(m_pivot) - parentBounds.CalculateCenter()) / parent->GetWorldTransform().CalculateScale();
	}