2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <dali/internal/event/actors/actor-coords.h>
18 #include <dali/internal/event/common/projection.h>
20 namespace Dali::Internal
22 bool ConvertScreenToLocal(
23 const Matrix& viewMatrix,
24 const Matrix& projectionMatrix,
25 const Matrix& worldMatrix,
26 const Vector3& currentSize,
27 const Viewport& viewport,
33 // Get the ModelView matrix
35 Matrix::Multiply(modelView, worldMatrix, viewMatrix);
37 // Calculate the inverted ModelViewProjection matrix; this will be used for 2 unprojects
38 Matrix invertedMvp(false /*don't init*/);
39 Matrix::Multiply(invertedMvp, modelView, projectionMatrix);
40 bool success = invertedMvp.Invert();
42 // Convert to GL coordinates
43 Vector4 screenPos(screenX - static_cast<float>(viewport.x), static_cast<float>(viewport.height) - screenY - static_cast<float>(viewport.y), 0.f, 1.f);
48 success = Unproject(screenPos, invertedMvp, static_cast<float>(viewport.width), static_cast<float>(viewport.height), nearPos);
55 success = Unproject(screenPos, invertedMvp, static_cast<float>(viewport.width), static_cast<float>(viewport.height), farPos);
61 if(XyPlaneIntersect(nearPos, farPos, local))
63 Vector3 size = currentSize;
64 localX = local.x + size.x * 0.5f;
65 localY = local.y + size.y * 0.5f;
76 bool ConvertScreenToLocalRenderTask(
77 const RenderTask& renderTask,
78 const Matrix& worldMatrix,
79 const Vector3& currentSize,
86 CameraActor* camera = renderTask.GetCameraActor();
90 renderTask.GetViewport(viewport);
92 // need to translate coordinates to render tasks coordinate space
93 Vector2 converted(screenX, screenY);
94 if(renderTask.TranslateCoordinates(converted))
96 success = ConvertScreenToLocal(camera->GetViewMatrix(), camera->GetProjectionMatrix(), worldMatrix, currentSize, viewport, localX, localY, converted.x, converted.y);
102 bool ConvertScreenToLocalRenderTaskList(
103 const RenderTaskList& renderTaskList,
104 const Matrix& worldMatrix,
105 const Vector3& currentSize,
111 // do a reverse traversal of all lists (as the default onscreen one is typically the last one)
112 uint32_t taskCount = renderTaskList.GetTaskCount();
113 for(uint32_t i = taskCount; i > 0; --i)
115 RenderTaskPtr task = renderTaskList.GetTask(i - 1);
116 if(ConvertScreenToLocalRenderTask(*task, worldMatrix, currentSize, localX, localY, screenX, screenY))
118 // found a task where this conversion was ok so return
125 } // namespace Dali::Internal