1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#include "UnityPrefix.h"
#include "GUILayer.h"
#include "Runtime/Camera/Camera.h"
#include "Runtime/Graphics/Transform.h"
#include "Runtime/Camera/RenderManager.h"
#include "Runtime/Math/Vector2.h"
#include "Runtime/BaseClasses/Tags.h"
#include "Runtime/Allocator/MemoryMacros.h"
#include "Runtime/Profiler/ExternalGraphicsProfiler.h"
PROFILER_INFORMATION(gGUILayerProfile, "Camera.GUILayer", kProfilerRender);
GUILayer::GUIElements* GUILayer::ms_GUIElements = NULL;
GUILayer::GUILayer(MemLabelId label, ObjectCreationMode mode)
: Super(label, mode)
{
}
GUILayer::~GUILayer ()
{
}
inline bool SortGUIByDepth (GUIElement* lhs, GUIElement* rhs)
{
return lhs->GetComponent (Transform).GetLocalPosition ().z < rhs->GetComponent (Transform).GetLocalPosition ().z;
}
void GUILayer::InitializeClass ()
{
ms_GUIElements = new GUIElements;
}
void GUILayer::CleanupClass()
{
delete ms_GUIElements;
}
void GUILayer::RenderGUILayer ()
{
PROFILER_AUTO_GFX(gGUILayerProfile, this);
ms_GUIElements->apply_delayed ();
if (ms_GUIElements->empty())
return;
typedef UNITY_TEMP_VECTOR(GUIElement*) TempElements;
TempElements elements (ms_GUIElements->begin (), ms_GUIElements->end ());
std::sort (elements.begin (), elements.end (), SortGUIByDepth);
Camera& camera = GetComponent(Camera);
UInt32 cullingMask = camera.GetCullingMask ();
Rectf cameraRect = camera.GetScreenViewportRect();
for (TempElements::iterator it=elements.begin(), itEnd = elements.end(); it != itEnd; ++it)
{
GUIElement& element = **it;
if (element.GetGameObject ().GetLayerMask () & cullingMask)
element.RenderGUIElement (cameraRect);
}
}
GUIElement* GUILayer::HitTest (const Vector2f& screenPosition)
{
Camera& camera = GetComponent (Camera);
Vector3f viewportPos3D = camera.ScreenToViewportPoint (Vector3f (screenPosition.x, screenPosition.y, camera.GetNear()));
Vector2f viewportPos (viewportPos3D.x, viewportPos3D.y);
Rectf normalized (0.0F,0.0F,1.0F,1.0F);
if (!normalized.Contains (viewportPos.x, viewportPos.y))
return NULL;
Rectf cameraRect = camera.GetScreenViewportRect();
Rectf windowRect = GetRenderManager ().GetWindowRect ();
viewportPos.x *= windowRect.Width ();
viewportPos.y *= windowRect.Height ();
GUIElement* topmost = NULL;
float topmostZ = -std::numeric_limits<float>::infinity ();
// GUI hit testing always ignores IgnoreRaycast layer
UInt32 cullingMask = camera.GetCullingMask() & ~(kIgnoreRaycastMask);
for (GUIElements::iterator it=ms_GUIElements->begin (), itEnd = ms_GUIElements->end (); it != itEnd; ++it)
{
GUIElement* element = *it;
if (element && (element->GetGameObject ().GetLayerMask () & cullingMask) && element->HitTest (viewportPos, cameraRect))
{
float z = element->GetComponent (Transform).GetLocalPosition ().z;
if (z > topmostZ)
{
topmost = element;
topmostZ = z;
}
}
}
return topmost;
}
IMPLEMENT_CLASS_HAS_INIT(GUILayer)
|