SpiecsEngine
 
Loading...
Searching...
No Matches
WorldPickIDQuerier.cpp
Go to the documentation of this file.
1/**
2* @file WorldPickIDQuerier.cpp.
3* @brief The WorldPickIDQuerier & NativeScriptRegister Class Implementation.
4* @author Spices.
5*/
6
7#include "Pchheader.h"
9#include "Systems/SlateSystem.h"
10#include "Slate/Imgui/ViewPort/ImguiViewport.h"
11#include "Render/Vulkan/VulkanRenderBackend.h"
12#include "Core/Input/MouseButtonCodes.h"
13#include "Core/Input/Input.h"
14#include "Core/Input/KeyCodes.h"
15#include "World/Entity.h"
16#include "World/World/World.h"
17
18namespace Spices {
19
21 {
22 EventDispatcher dispatcher(e);
23
25 }
26
28 {
30
31 /**
32 * @brief The first frame, we will not get register pointer.
33 */
34 if (!SlateSystem::GetRegister()) return false;
35
36 /**
37 * @brief Get viewport.
38 */
39 if (!m_ViewPort.lock()) m_ViewPort = SlateSystem::GetRegister()->GetViewPort();
40
41 if (!m_ViewPort.lock()->IsHovered() || m_ViewPort.lock()->GetGizmo()->IsOver()) return false;
42
43 if (m_ViewPort.lock()->GetMousePosInViewport().first < 0 ||
44 m_ViewPort.lock()->GetMousePosInViewport().first > m_ViewPort.lock()->GetPanelSize().x ||
45 m_ViewPort.lock()->GetMousePosInViewport().second < 0 ||
46 m_ViewPort.lock()->GetMousePosInViewport().second > m_ViewPort.lock()->GetPanelSize().y
47 )
48 {
49 return false;
50 }
51
53 {
54 /**
55 * @brief Add pick.
56 */
58 {
59 auto pair = m_ViewPort.lock()->GetMousePosInViewport();
60
61 VulkanRenderBackend::GetRendererResourcePool()
62 ->AccessRowResource("EntityID")
63 ->CopyImageTexelToBuffer(
64 pair.first,
65 pair.second,
66 reinterpret_cast<void*>(&m_WorldPickID[0])
67 );
68
69 Entity entity((entt::entity)m_WorldPickID[0], FrameInfo::Get().m_World.get());
70 std::string entityName = *entity.GetComponent<TagComponent>().GetTag().begin();
71
72 FrameInfo::Get().m_PickEntityID.push_back(static_cast<int>(m_WorldPickID[0]), entityName);
73
74 std::stringstream ss;
75 ss << "Select entity: " << entityName;
76
77 SPICES_CORE_TRACE(ss.str())
78 }
79
80 /**
81 * @breif Sub pick.
82 */
84 {
85 auto pair = m_ViewPort.lock()->GetMousePosInViewport();
86
87 VulkanRenderBackend::GetRendererResourcePool()
88 ->AccessRowResource("EntityID")
89 ->CopyImageTexelToBuffer(
90 pair.first,
91 pair.second,
92 reinterpret_cast<void*>(&m_WorldPickID[0])
93 );
94
95 std::string* ptr = FrameInfo::Get().m_PickEntityID.find_value(static_cast<int>(m_WorldPickID[0]));
96 if (ptr)
97 {
98 std::stringstream ss;
99 ss << "Deselect entity: " << *ptr;
100
101 SPICES_CORE_TRACE(ss.str())
102
103 FrameInfo::Get().m_PickEntityID.erase(static_cast<int>(m_WorldPickID[0]));
104 }
105 }
106
107 /**
108 * @breif Single Select.
109 */
110 else
111 {
112 FrameInfo::Get().m_PickEntityID.clear();
113
114 auto pair = m_ViewPort.lock()->GetMousePosInViewport();
115
116 VulkanRenderBackend::GetRendererResourcePool()
117 ->AccessRowResource("EntityID")
118 ->CopyImageTexelToBuffer(
119 pair.first,
120 pair.second,
121 reinterpret_cast<void*>(&m_WorldPickID[0])
122 );
123
124 Entity entity((entt::entity)m_WorldPickID[0], FrameInfo::Get().m_World.get());
125 std::string entityName = *entity.GetComponent<TagComponent>().GetTag().begin();
126
127 FrameInfo::Get().m_PickEntityID.push_back(static_cast<int>(m_WorldPickID[0]), entityName);
128
129 std::stringstream ss;
130 ss << "Select entity: " << entityName;
131
132 SPICES_CORE_TRACE(ss.str())
133 }
134 }
135
136 /**
137 * @breif Cancle Select.
138 */
140 {
141 FrameInfo::Get().m_PickEntityID.clear();
142
143 SPICES_CORE_TRACE("Cancel all selected entity")
144 }
145
146 return false;
147 }
148}
#define BIND_EVENT_FN(x)
Bind Event.
Definition Event.h:88
#define SPICES_PROFILE_ZONE
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
EventDispatcher(Event &event)
Constructor Function.
Definition Event.h:172
This Class store a Specific Event type first and Dispatch a event handle function to it.
Definition Event.h:156
This Class is the basic Event Class. Inherit from it and create specific event class.
Definition Event.h:96
static FrameInfo & Get()
Get FrameInfo.
Definition FrameInfo.cpp:14
FrameInfo Class. This class defines the FrameInfo data.
Definition FrameInfo.h:32
static bool IsKeyPressed(const int &keycode)
Query If given Key is Pressed.
Definition Input.h:34
This Class Is a wrapper of Platform Specific Input Query.
Definition Input.h:16
const int & GetMouseButton() const
Destructor Function.
Definition MouseEvent.h:167
This Class is inherited from Event Class.
Definition MouseEvent.h:194
TagComponent Class. This class defines the specific behaves of TagComponent.
bool OnMouseButtonPressed(MouseButtonPressedEvent &e)
Event OnKeyPressed.
float m_WorldPickID[4]
World Picked entity id (only use channel 0).
virtual void OnEvent(Event &e) override
This interface defines the behave on specific component event happened.
Script of handle world entity pick.