Base class code cleanup

This commit is contained in:
Sascha Willems 2024-03-20 07:49:06 +01:00
parent c6cd406c6f
commit 52779a1bd1
4 changed files with 85 additions and 74 deletions

View file

@ -686,10 +686,10 @@ void VulkanExampleBase::updateOverlay()
io.DisplaySize = ImVec2((float)width, (float)height); io.DisplaySize = ImVec2((float)width, (float)height);
io.DeltaTime = frameTimer; io.DeltaTime = frameTimer;
io.MousePos = ImVec2(mousePos.x, mousePos.y); io.MousePos = ImVec2(mouseState.position.x, mouseState.position.y);
io.MouseDown[0] = mouseButtons.left && UIOverlay.visible; io.MouseDown[0] = mouseState.buttons.left && UIOverlay.visible;
io.MouseDown[1] = mouseButtons.right && UIOverlay.visible; io.MouseDown[1] = mouseState.buttons.right && UIOverlay.visible;
io.MouseDown[2] = mouseButtons.middle && UIOverlay.visible; io.MouseDown[2] = mouseState.buttons.middle && UIOverlay.visible;
ImGui::NewFrame(); ImGui::NewFrame();
@ -721,8 +721,8 @@ void VulkanExampleBase::updateOverlay()
} }
#if defined(VK_USE_PLATFORM_ANDROID_KHR) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
if (mouseButtons.left) { if (mouseState.buttons.left) {
mouseButtons.left = false; mouseState.buttons.left = false;
} }
#endif #endif
} }
@ -1276,6 +1276,13 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
UIOverlay.visible = !UIOverlay.visible; UIOverlay.visible = !UIOverlay.visible;
UIOverlay.updated = true; UIOverlay.updated = true;
break; break;
case KEY_F2:
if (camera.type == Camera::CameraType::lookat) {
camera.type = Camera::CameraType::firstperson;
}else {
camera.type = Camera::CameraType::lookat;
}
break;
case KEY_ESCAPE: case KEY_ESCAPE:
PostQuitMessage(0); PostQuitMessage(0);
break; break;
@ -1323,25 +1330,25 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
} }
break; break;
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam)); mouseState.position = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseButtons.left = true; mouseState.buttons.left = true;
break; break;
case WM_RBUTTONDOWN: case WM_RBUTTONDOWN:
mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam)); mouseState.position = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseButtons.right = true; mouseState.buttons.right = true;
break; break;
case WM_MBUTTONDOWN: case WM_MBUTTONDOWN:
mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam)); mouseState.position = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseButtons.middle = true; mouseState.buttons.middle = true;
break; break;
case WM_LBUTTONUP: case WM_LBUTTONUP:
mouseButtons.left = false; mouseState.buttons.left = false;
break; break;
case WM_RBUTTONUP: case WM_RBUTTONUP:
mouseButtons.right = false; mouseState.buttons.right = false;
break; break;
case WM_MBUTTONUP: case WM_MBUTTONUP:
mouseButtons.middle = false; mouseState.buttons.middle = false;
break; break;
case WM_MOUSEWHEEL: case WM_MOUSEWHEEL:
{ {
@ -1421,7 +1428,7 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent*
float x = AMotionEvent_getX(event, 0) - vulkanExample->touchPos.x; float x = AMotionEvent_getX(event, 0) - vulkanExample->touchPos.x;
float y = AMotionEvent_getY(event, 0) - vulkanExample->touchPos.y; float y = AMotionEvent_getY(event, 0) - vulkanExample->touchPos.y;
if ((x * x + y * y) < deadZone) { if ((x * x + y * y) < deadZone) {
vulkanExample->mouseButtons.left = true; vulkanExample->mouseState.buttons.left = true;
} }
}; };
@ -1445,8 +1452,8 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent*
} }
vulkanExample->touchPos.x = AMotionEvent_getX(event, 0); vulkanExample->touchPos.x = AMotionEvent_getX(event, 0);
vulkanExample->touchPos.y = AMotionEvent_getY(event, 0); vulkanExample->touchPos.y = AMotionEvent_getY(event, 0);
vulkanExample->mousePos.x = AMotionEvent_getX(event, 0); vulkanExample->mouseState.position.x = AMotionEvent_getX(event, 0);
vulkanExample->mousePos.y = AMotionEvent_getY(event, 0); vulkanExample->mouseState.position.y = AMotionEvent_getY(event, 0);
break; break;
} }
case AMOTION_EVENT_ACTION_MOVE: { case AMOTION_EVENT_ACTION_MOVE: {
@ -1757,37 +1764,37 @@ static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CV
- (void)mouseDown:(NSEvent *)event - (void)mouseDown:(NSEvent *)event
{ {
auto point = [self getMouseLocalPoint:event]; auto point = [self getMouseLocalPoint:event];
vulkanExample->mousePos = glm::vec2(point.x, point.y); vulkanExample->mouseState.position = glm::vec2(point.x, point.y);
vulkanExample->mouseButtons.left = true; vulkanExample->mouseState.buttons.left = true;
} }
- (void)mouseUp:(NSEvent *)event - (void)mouseUp:(NSEvent *)event
{ {
vulkanExample->mouseButtons.left = false; vulkanExample->mouseState.buttons.left = false;
} }
- (void)rightMouseDown:(NSEvent *)event - (void)rightMouseDown:(NSEvent *)event
{ {
auto point = [self getMouseLocalPoint:event]; auto point = [self getMouseLocalPoint:event];
vulkanExample->mousePos = glm::vec2(point.x, point.y); vulkanExample->mouseState.position = glm::vec2(point.x, point.y);
vulkanExample->mouseButtons.right = true; vulkanExample->mouseState.buttons.right = true;
} }
- (void)rightMouseUp:(NSEvent *)event - (void)rightMouseUp:(NSEvent *)event
{ {
vulkanExample->mouseButtons.right = false; vulkanExample->mouseState.buttons.right = false;
} }
- (void)otherMouseDown:(NSEvent *)event - (void)otherMouseDown:(NSEvent *)event
{ {
auto point = [self getMouseLocalPoint:event]; auto point = [self getMouseLocalPoint:event];
vulkanExample->mousePos = glm::vec2(point.x, point.y); vulkanExample->mouseState.position = glm::vec2(point.x, point.y);
vulkanExample->mouseButtons.middle = true; vulkanExample->mouseState.buttons.middle = true;
} }
- (void)otherMouseUp:(NSEvent *)event - (void)otherMouseUp:(NSEvent *)event
{ {
vulkanExample->mouseButtons.middle = false; vulkanExample->mouseState.buttons.middle = false;
} }
- (void)mouseDragged:(NSEvent *)event - (void)mouseDragged:(NSEvent *)event
@ -2043,13 +2050,13 @@ void VulkanExampleBase::handleEvent(const DFBWindowEvent *event)
switch (event->button) switch (event->button)
{ {
case DIBI_LEFT: case DIBI_LEFT:
mouseButtons.left = true; mouseState.buttons.left = true;
break; break;
case DIBI_MIDDLE: case DIBI_MIDDLE:
mouseButtons.middle = true; mouseState.buttons.middle = true;
break; break;
case DIBI_RIGHT: case DIBI_RIGHT:
mouseButtons.right = true; mouseState.buttons.right = true;
break; break;
default: default:
break; break;
@ -2059,13 +2066,13 @@ void VulkanExampleBase::handleEvent(const DFBWindowEvent *event)
switch (event->button) switch (event->button)
{ {
case DIBI_LEFT: case DIBI_LEFT:
mouseButtons.left = false; mouseState.buttons.left = false;
break; break;
case DIBI_MIDDLE: case DIBI_MIDDLE:
mouseButtons.middle = false; mouseState.buttons.middle = false;
break; break;
case DIBI_RIGHT: case DIBI_RIGHT:
mouseButtons.right = false; mouseState.buttons.right = false;
break; break;
default: default:
break; break;
@ -2181,13 +2188,13 @@ void VulkanExampleBase::pointerButton(struct wl_pointer *pointer,
switch (button) switch (button)
{ {
case BTN_LEFT: case BTN_LEFT:
mouseButtons.left = !!state; mouseState.buttons.left = !!state;
break; break;
case BTN_MIDDLE: case BTN_MIDDLE:
mouseButtons.middle = !!state; mouseState.buttons.middle = !!state;
break; break;
case BTN_RIGHT: case BTN_RIGHT:
mouseButtons.right = !!state; mouseState.buttons.right = !!state;
break; break;
default: default:
break; break;
@ -2588,22 +2595,22 @@ void VulkanExampleBase::handleEvent(const xcb_generic_event_t *event)
{ {
xcb_button_press_event_t *press = (xcb_button_press_event_t *)event; xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
if (press->detail == XCB_BUTTON_INDEX_1) if (press->detail == XCB_BUTTON_INDEX_1)
mouseButtons.left = true; mouseState.buttons.left = true;
if (press->detail == XCB_BUTTON_INDEX_2) if (press->detail == XCB_BUTTON_INDEX_2)
mouseButtons.middle = true; mouseState.buttons.middle = true;
if (press->detail == XCB_BUTTON_INDEX_3) if (press->detail == XCB_BUTTON_INDEX_3)
mouseButtons.right = true; mouseState.buttons.right = true;
} }
break; break;
case XCB_BUTTON_RELEASE: case XCB_BUTTON_RELEASE:
{ {
xcb_button_press_event_t *press = (xcb_button_press_event_t *)event; xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
if (press->detail == XCB_BUTTON_INDEX_1) if (press->detail == XCB_BUTTON_INDEX_1)
mouseButtons.left = false; mouseState.buttons.left = false;
if (press->detail == XCB_BUTTON_INDEX_2) if (press->detail == XCB_BUTTON_INDEX_2)
mouseButtons.middle = false; mouseState.buttons.middle = false;
if (press->detail == XCB_BUTTON_INDEX_3) if (press->detail == XCB_BUTTON_INDEX_3)
mouseButtons.right = false; mouseState.buttons.right = false;
} }
break; break;
case XCB_KEY_PRESS: case XCB_KEY_PRESS:
@ -2812,29 +2819,29 @@ void VulkanExampleBase::handleEvent()
} }
if ((mouse_buttons & SCREEN_LEFT_MOUSE_BUTTON) == 0) { if ((mouse_buttons & SCREEN_LEFT_MOUSE_BUTTON) == 0) {
if ((val & SCREEN_LEFT_MOUSE_BUTTON) == SCREEN_LEFT_MOUSE_BUTTON) { if ((val & SCREEN_LEFT_MOUSE_BUTTON) == SCREEN_LEFT_MOUSE_BUTTON) {
mouseButtons.left = true; mouseState.buttons.left = true;
} }
} else { } else {
if ((val & SCREEN_LEFT_MOUSE_BUTTON) == 0) { if ((val & SCREEN_LEFT_MOUSE_BUTTON) == 0) {
mouseButtons.left = false; mouseState.buttons.left = false;
} }
} }
if ((mouse_buttons & SCREEN_RIGHT_MOUSE_BUTTON) == 0) { if ((mouse_buttons & SCREEN_RIGHT_MOUSE_BUTTON) == 0) {
if ((val & SCREEN_RIGHT_MOUSE_BUTTON) == SCREEN_RIGHT_MOUSE_BUTTON) { if ((val & SCREEN_RIGHT_MOUSE_BUTTON) == SCREEN_RIGHT_MOUSE_BUTTON) {
mouseButtons.right = true; mouseState.buttons.right = true;
} }
} else { } else {
if ((val & SCREEN_RIGHT_MOUSE_BUTTON) == 0) { if ((val & SCREEN_RIGHT_MOUSE_BUTTON) == 0) {
mouseButtons.right = false; mouseState.buttons.right = false;
} }
} }
if ((mouse_buttons & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) { if ((mouse_buttons & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) {
if ((val & SCREEN_MIDDLE_MOUSE_BUTTON) == SCREEN_MIDDLE_MOUSE_BUTTON) { if ((val & SCREEN_MIDDLE_MOUSE_BUTTON) == SCREEN_MIDDLE_MOUSE_BUTTON) {
mouseButtons.middle = true; mouseState.buttons.middle = true;
} }
} else { } else {
if ((val & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) { if ((val & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) {
mouseButtons.middle = false; mouseState.buttons.middle = false;
} }
} }
mouse_buttons = val; mouse_buttons = val;
@ -3188,8 +3195,8 @@ void VulkanExampleBase::windowResize()
void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y) void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y)
{ {
int32_t dx = (int32_t)mousePos.x - x; int32_t dx = (int32_t)mouseState.position.x - x;
int32_t dy = (int32_t)mousePos.y - y; int32_t dy = (int32_t)mouseState.position.y - y;
bool handled = false; bool handled = false;
@ -3200,23 +3207,23 @@ void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y)
mouseMoved((float)x, (float)y, handled); mouseMoved((float)x, (float)y, handled);
if (handled) { if (handled) {
mousePos = glm::vec2((float)x, (float)y); mouseState.position = glm::vec2((float)x, (float)y);
return; return;
} }
if (mouseButtons.left) { if (mouseState.buttons.left) {
camera.rotate(glm::vec3(dy * camera.rotationSpeed, -dx * camera.rotationSpeed, 0.0f)); camera.rotate(glm::vec3(dy * camera.rotationSpeed, -dx * camera.rotationSpeed, 0.0f));
viewUpdated = true; viewUpdated = true;
} }
if (mouseButtons.right) { if (mouseState.buttons.right) {
camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f)); camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f));
viewUpdated = true; viewUpdated = true;
} }
if (mouseButtons.middle) { if (mouseState.buttons.middle) {
camera.translate(glm::vec3(-dx * 0.005f, -dy * 0.005f, 0.0f)); camera.translate(glm::vec3(-dx * 0.005f, -dy * 0.005f, 0.0f));
viewUpdated = true; viewUpdated = true;
} }
mousePos = glm::vec2((float)x, (float)y); mouseState.position = glm::vec2((float)x, (float)y);
} }
void VulkanExampleBase::windowResized() {} void VulkanExampleBase::windowResized() {}

View file

@ -184,6 +184,22 @@ public:
bool overlay = true; bool overlay = true;
} settings; } settings;
/** @brief State of gamepad input (only used on Android) */
struct {
glm::vec2 axisLeft = glm::vec2(0.0f);
glm::vec2 axisRight = glm::vec2(0.0f);
} gamePadState;
/** @brief State of mouse/touch input */
struct {
struct {
bool left = false;
bool right = false;
bool middle = false;
} buttons;
glm::vec2 position;
} mouseState;
VkClearColorValue defaultClearColor = { { 0.025f, 0.025f, 0.025f, 1.0f } }; VkClearColorValue defaultClearColor = { { 0.025f, 0.025f, 0.025f, 1.0f } };
static std::vector<const char*> args; static std::vector<const char*> args;
@ -196,7 +212,6 @@ public:
bool paused = false; bool paused = false;
Camera camera; Camera camera;
glm::vec2 mousePos;
std::string title = "Vulkan Example"; std::string title = "Vulkan Example";
std::string name = "vulkanExample"; std::string name = "vulkanExample";
@ -209,17 +224,6 @@ public:
VkImageView view; VkImageView view;
} depthStencil{}; } depthStencil{};
struct {
glm::vec2 axisLeft = glm::vec2(0.0f);
glm::vec2 axisRight = glm::vec2(0.0f);
} gamePadState;
struct {
bool left = false;
bool right = false;
bool middle = false;
} mouseButtons;
// OS specific // OS specific
#if defined(_WIN32) #if defined(_WIN32)
HWND window; HWND window;

View file

@ -3,7 +3,7 @@
* *
* Updated compute shader by Lukas Bergdoll (https://github.com/Voultapher) * Updated compute shader by Lukas Bergdoll (https://github.com/Voultapher)
* *
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de * Copyright (C) 2016-2024 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -553,8 +553,8 @@ public:
} }
else else
{ {
float normalizedMx = (mousePos.x - static_cast<float>(width / 2)) / static_cast<float>(width / 2); float normalizedMx = (mouseState.position.x - static_cast<float>(width / 2)) / static_cast<float>(width / 2);
float normalizedMy = (mousePos.y - static_cast<float>(height / 2)) / static_cast<float>(height / 2); float normalizedMy = (mouseState.position.y - static_cast<float>(height / 2)) / static_cast<float>(height / 2);
compute.uniformData.destX = normalizedMx; compute.uniformData.destX = normalizedMx;
compute.uniformData.destY = normalizedMy; compute.uniformData.destY = normalizedMy;
} }

View file

@ -793,10 +793,10 @@ public:
io.DisplaySize = ImVec2((float)width, (float)height); io.DisplaySize = ImVec2((float)width, (float)height);
io.DeltaTime = frameTimer; io.DeltaTime = frameTimer;
io.MousePos = ImVec2(mousePos.x, mousePos.y); io.MousePos = ImVec2(mouseState.position.x, mouseState.position.y);
io.MouseDown[0] = mouseButtons.left && UIOverlay.visible; io.MouseDown[0] = mouseState.buttons.left && UIOverlay.visible;
io.MouseDown[1] = mouseButtons.right && UIOverlay.visible; io.MouseDown[1] = mouseState.buttons.right && UIOverlay.visible;
io.MouseDown[2] = mouseButtons.middle && UIOverlay.visible; io.MouseDown[2] = mouseState.buttons.middle && UIOverlay.visible;
draw(); draw();
} }