MCPcopy Create free account
hub / github.com/defold/defold / ScreenToLocalPosition

Function ScreenToLocalPosition

engine/gui/src/gui.cpp:4457–4533  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4455 }
4456
4457 static Vector3 ScreenToLocalPosition(HScene scene, InternalNode* node, InternalNode* parent_node, dmVMath::Vector3 screen_position)
4458 {
4459 Matrix4 parent_m;
4460
4461 if (scene->m_AdjustReference == ADJUST_REFERENCE_DISABLED)
4462 {
4463 if (parent_node == 0x0)
4464 {
4465 return screen_position;
4466 }
4467
4468 CalculateNodeTransform(scene, parent_node, CalculateNodeTransformFlags(), parent_m);
4469 Vector4 local_position = inverse(parent_m) * Vector4(screen_position, 1.0f);
4470 return local_position.getXYZ();
4471 }
4472
4473 Vector4 reference_scale;
4474 Vector4 adjust_scale;
4475 Vector4 offset(0.0f);
4476
4477 Vector3 position = screen_position;
4478
4479 // Calculate the new parents scene transform
4480 // We also need to calculate values relative to adjustments and offset
4481 // corresponding to the values that would be used in AdjustPosScale (see reasoning below).
4482 if (parent_node != 0x0)
4483 {
4484 CalculateNodeTransform(scene, parent_node, CalculateNodeTransformFlags(), parent_m);
4485 reference_scale = parent_node->m_Node.m_LocalAdjustScale;
4486 adjust_scale = ApplyAdjustOnReferenceScale(reference_scale, node->m_Node.m_AdjustMode);
4487
4488 Vector4 parent_local_position = inverse(parent_m) * Vector4(screen_position, 1.0f);
4489 position = parent_local_position.getXYZ();
4490
4491 if (scene->m_AdjustReference == ADJUST_REFERENCE_LEGACY)
4492 {
4493 return position;
4494 }
4495
4496 // For ADJUST_REFERENCE_PARENT the node's local transform is pre-multiplied
4497 // by the inverse reference scale in UpdateLocalTransform. Reapply it before
4498 // undoing the node adjustment below.
4499 position = mulPerElem(position, reference_scale.getXYZ());
4500 } else {
4501 reference_scale = CalculateReferenceScale(scene, 0x0);
4502 adjust_scale = ApplyAdjustOnReferenceScale(reference_scale, node->m_Node.m_AdjustMode);
4503
4504 Vector4 parent_dims = Vector4((float) scene->m_Width, (float) scene->m_Height, 0.0f, 1.0f);
4505 Vector4 adjusted_dims = mulPerElem(parent_dims, adjust_scale);
4506 Vector4 ref_size = Vector4((float) scene->m_AdjustWidth, (float) scene->m_AdjustHeight, 0.0f, 1.0f);
4507 offset = (ref_size - adjusted_dims) * 0.5f;
4508 offset.setX(offset.getX() + scene->m_AdjustOffsetX);
4509 offset.setY(offset.getY() + scene->m_AdjustOffsetY);
4510 }
4511
4512 // We need to perform the inverse of what AdjustPosScale will do to counteract when
4513 // it will be applied during next call to CalculateNodeTransform.
4514 // See AdjustPosScale for comparison on the steps being performed/inversed.

Callers 3

SetScreenPositionFunction · 0.70
LuaScreenToLocalFunction · 0.70
TEST_FFunction · 0.50

Calls 15

CalculateNodeTransformFunction · 0.70
CalculateReferenceScaleFunction · 0.70
GetNodeFunction · 0.70
inverseFunction · 0.50
Vector4Class · 0.50
mulPerElemFunction · 0.50
recipPerElemFunction · 0.50
Vector3Class · 0.50
Point3Class · 0.50
getXYZMethod · 0.45

Tested by 1

TEST_FFunction · 0.40