-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.cpp
More file actions
144 lines (113 loc) · 4.9 KB
/
events.cpp
File metadata and controls
144 lines (113 loc) · 4.9 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <SDL3/SDL.h>
#include <GLES3/gl3.h>
#include "arena.hpp"
#include "math.h"
#include "events.h"
#include "mat4.h"
#include "raycast.h"
#include "tracy/Tracy.hpp"
#include "backends/imgui_impl_sdl3.h"
using namespace linalg;
void processEvents(WindowState& window, Camera& camera, InputState& input, Scene& scene, Arena& frame_arena)
{
ZoneScoped;
// Handle events
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL3_ProcessEvent(&event);
if(const auto& io = ImGui::GetIO(); !io.WantCaptureMouse && !io.WantCaptureKeyboard) {
switch (event.type)
{
case SDL_EVENT_QUIT:
window.should_close = true;
break;
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
{
if (event.window.windowID == window.id)
{
int width = event.window.data1;
int height = event.window.data2;
camera.aspect = (float)width / (float)height;
window.width = width;
window.height = height;
}
break;
}
case SDL_EVENT_MOUSE_BUTTON_DOWN:
{
const auto* e = reinterpret_cast<SDL_MouseButtonEvent *>(&event);
if (event.button.button == 1) {
input.pointer_down = true;
Vec2 pointer_position = {
.x = static_cast<float>(e->x),
.y = static_cast<float>(e->y)
};
input.pointer_position = pointer_position;
// get mouse position ndc
const Vec2 pointer_clip = getPointerClickInClipSpace(
e->x, e->y, window.width, window.height
);
// create ray
const Ray worldRay = getWorldRayFromClipSpaceAndCamera(
pointer_clip,
camera
);
// intersect scene
auto hits = rayIntersectsScene(worldRay, scene, frame_arena);
if (hits.size() == 0) break;
// update floor with color of first hit
sortBySceneDepth(hits, camera);
const auto& clicked = hits[0];
// set the floor node to have the same color at the clicked thing
for (auto& node: scene.nodes) {
if (node->name == "floor") {
const auto floor = node;
if (std::holds_alternative<BasicColorMaterial>(clicked.meshIntersection.meshInfo.material)) {
std::get<BasicColorMaterial>(floor->mesh.value().material).color = std::get<BasicColorMaterial>(clicked.meshIntersection.meshInfo.material).color;
}
}
}
input.selected_entity = {
.id = clicked.id,
.name = clicked.nodeName,
};
}
break;
}
case SDL_EVENT_MOUSE_MOTION:
{
const auto *e = reinterpret_cast<SDL_MouseMotionEvent *>(&event);
if (input.pointer_down) {
const float orbitSensitivity = camera.orbit.sensitivity;
const Vec3 orbitTarget = camera.orbit.target;
const float orbitRadius = camera.orbit.radius;
camera.orbit.azimuth -= e->xrel * orbitSensitivity;
camera.orbit.elevation -= e->yrel * orbitSensitivity;
const Vec3 newCameraPosition = calculateOrbitPosition(
camera.orbit.azimuth,
camera.orbit.elevation,
orbitTarget,
orbitRadius
);
camera.transform = lookAt(newCameraPosition, orbitTarget, camera.up);
const Vec2 pointer_position = {
.x = static_cast<float>(e->x),
.y = static_cast<float>(e->y)
};
input.pointer_position = pointer_position;
}
break;
}
case SDL_EVENT_MOUSE_BUTTON_UP:
{
if (event.button.button == 1) {
input.pointer_down = false;
constexpr Vec2 pointer_position ={ .x = 0, .y = 0 } ;
input.pointer_position = pointer_position;
}
break;
}
}}
}
}