Skip to content

Commit

Permalink
Base class code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaWillems committed Mar 20, 2024
1 parent c6cd406 commit 52779a1
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 74 deletions.
117 changes: 62 additions & 55 deletions base/vulkanexamplebase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,10 @@ void VulkanExampleBase::updateOverlay()
io.DisplaySize = ImVec2((float)width, (float)height);
io.DeltaTime = frameTimer;

io.MousePos = ImVec2(mousePos.x, mousePos.y);
io.MouseDown[0] = mouseButtons.left && UIOverlay.visible;
io.MouseDown[1] = mouseButtons.right && UIOverlay.visible;
io.MouseDown[2] = mouseButtons.middle && UIOverlay.visible;
io.MousePos = ImVec2(mouseState.position.x, mouseState.position.y);
io.MouseDown[0] = mouseState.buttons.left && UIOverlay.visible;
io.MouseDown[1] = mouseState.buttons.right && UIOverlay.visible;
io.MouseDown[2] = mouseState.buttons.middle && UIOverlay.visible;

ImGui::NewFrame();

Expand Down Expand Up @@ -721,8 +721,8 @@ void VulkanExampleBase::updateOverlay()
}

#if defined(VK_USE_PLATFORM_ANDROID_KHR)
if (mouseButtons.left) {
mouseButtons.left = false;
if (mouseState.buttons.left) {
mouseState.buttons.left = false;
}
#endif
}
Expand Down Expand Up @@ -1276,6 +1276,13 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
UIOverlay.visible = !UIOverlay.visible;
UIOverlay.updated = true;
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:
PostQuitMessage(0);
break;
Expand Down Expand Up @@ -1323,25 +1330,25 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
}
break;
case WM_LBUTTONDOWN:
mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseButtons.left = true;
mouseState.position = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseState.buttons.left = true;
break;
case WM_RBUTTONDOWN:
mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseButtons.right = true;
mouseState.position = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseState.buttons.right = true;
break;
case WM_MBUTTONDOWN:
mousePos = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseButtons.middle = true;
mouseState.position = glm::vec2((float)LOWORD(lParam), (float)HIWORD(lParam));
mouseState.buttons.middle = true;
break;
case WM_LBUTTONUP:
mouseButtons.left = false;
mouseState.buttons.left = false;
break;
case WM_RBUTTONUP:
mouseButtons.right = false;
mouseState.buttons.right = false;
break;
case WM_MBUTTONUP:
mouseButtons.middle = false;
mouseState.buttons.middle = false;
break;
case WM_MOUSEWHEEL:
{
Expand Down Expand Up @@ -1421,7 +1428,7 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent*
float x = AMotionEvent_getX(event, 0) - vulkanExample->touchPos.x;
float y = AMotionEvent_getY(event, 0) - vulkanExample->touchPos.y;
if ((x * x + y * y) < deadZone) {
vulkanExample->mouseButtons.left = true;
vulkanExample->mouseState.buttons.left = true;
}
};

Expand All @@ -1445,8 +1452,8 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent*
}
vulkanExample->touchPos.x = AMotionEvent_getX(event, 0);
vulkanExample->touchPos.y = AMotionEvent_getY(event, 0);
vulkanExample->mousePos.x = AMotionEvent_getX(event, 0);
vulkanExample->mousePos.y = AMotionEvent_getY(event, 0);
vulkanExample->mouseState.position.x = AMotionEvent_getX(event, 0);
vulkanExample->mouseState.position.y = AMotionEvent_getY(event, 0);
break;
}
case AMOTION_EVENT_ACTION_MOVE: {
Expand Down Expand Up @@ -1757,37 +1764,37 @@ static CVReturn displayLinkOutputCallback(CVDisplayLinkRef displayLink, const CV
- (void)mouseDown:(NSEvent *)event
{
auto point = [self getMouseLocalPoint:event];
vulkanExample->mousePos = glm::vec2(point.x, point.y);
vulkanExample->mouseButtons.left = true;
vulkanExample->mouseState.position = glm::vec2(point.x, point.y);
vulkanExample->mouseState.buttons.left = true;
}

- (void)mouseUp:(NSEvent *)event
{
vulkanExample->mouseButtons.left = false;
vulkanExample->mouseState.buttons.left = false;
}

- (void)rightMouseDown:(NSEvent *)event
{
auto point = [self getMouseLocalPoint:event];
vulkanExample->mousePos = glm::vec2(point.x, point.y);
vulkanExample->mouseButtons.right = true;
vulkanExample->mouseState.position = glm::vec2(point.x, point.y);
vulkanExample->mouseState.buttons.right = true;
}

- (void)rightMouseUp:(NSEvent *)event
{
vulkanExample->mouseButtons.right = false;
vulkanExample->mouseState.buttons.right = false;
}

- (void)otherMouseDown:(NSEvent *)event
{
auto point = [self getMouseLocalPoint:event];
vulkanExample->mousePos = glm::vec2(point.x, point.y);
vulkanExample->mouseButtons.middle = true;
vulkanExample->mouseState.position = glm::vec2(point.x, point.y);
vulkanExample->mouseState.buttons.middle = true;
}

- (void)otherMouseUp:(NSEvent *)event
{
vulkanExample->mouseButtons.middle = false;
vulkanExample->mouseState.buttons.middle = false;
}

- (void)mouseDragged:(NSEvent *)event
Expand Down Expand Up @@ -2043,13 +2050,13 @@ void VulkanExampleBase::handleEvent(const DFBWindowEvent *event)
switch (event->button)
{
case DIBI_LEFT:
mouseButtons.left = true;
mouseState.buttons.left = true;
break;
case DIBI_MIDDLE:
mouseButtons.middle = true;
mouseState.buttons.middle = true;
break;
case DIBI_RIGHT:
mouseButtons.right = true;
mouseState.buttons.right = true;
break;
default:
break;
Expand All @@ -2059,13 +2066,13 @@ void VulkanExampleBase::handleEvent(const DFBWindowEvent *event)
switch (event->button)
{
case DIBI_LEFT:
mouseButtons.left = false;
mouseState.buttons.left = false;
break;
case DIBI_MIDDLE:
mouseButtons.middle = false;
mouseState.buttons.middle = false;
break;
case DIBI_RIGHT:
mouseButtons.right = false;
mouseState.buttons.right = false;
break;
default:
break;
Expand Down Expand Up @@ -2181,13 +2188,13 @@ void VulkanExampleBase::pointerButton(struct wl_pointer *pointer,
switch (button)
{
case BTN_LEFT:
mouseButtons.left = !!state;
mouseState.buttons.left = !!state;
break;
case BTN_MIDDLE:
mouseButtons.middle = !!state;
mouseState.buttons.middle = !!state;
break;
case BTN_RIGHT:
mouseButtons.right = !!state;
mouseState.buttons.right = !!state;
break;
default:
break;
Expand Down Expand Up @@ -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;
if (press->detail == XCB_BUTTON_INDEX_1)
mouseButtons.left = true;
mouseState.buttons.left = true;
if (press->detail == XCB_BUTTON_INDEX_2)
mouseButtons.middle = true;
mouseState.buttons.middle = true;
if (press->detail == XCB_BUTTON_INDEX_3)
mouseButtons.right = true;
mouseState.buttons.right = true;
}
break;
case XCB_BUTTON_RELEASE:
{
xcb_button_press_event_t *press = (xcb_button_press_event_t *)event;
if (press->detail == XCB_BUTTON_INDEX_1)
mouseButtons.left = false;
mouseState.buttons.left = false;
if (press->detail == XCB_BUTTON_INDEX_2)
mouseButtons.middle = false;
mouseState.buttons.middle = false;
if (press->detail == XCB_BUTTON_INDEX_3)
mouseButtons.right = false;
mouseState.buttons.right = false;
}
break;
case XCB_KEY_PRESS:
Expand Down Expand Up @@ -2812,29 +2819,29 @@ void VulkanExampleBase::handleEvent()
}
if ((mouse_buttons & SCREEN_LEFT_MOUSE_BUTTON) == 0) {
if ((val & SCREEN_LEFT_MOUSE_BUTTON) == SCREEN_LEFT_MOUSE_BUTTON) {
mouseButtons.left = true;
mouseState.buttons.left = true;
}
} else {
if ((val & SCREEN_LEFT_MOUSE_BUTTON) == 0) {
mouseButtons.left = false;
mouseState.buttons.left = false;
}
}
if ((mouse_buttons & SCREEN_RIGHT_MOUSE_BUTTON) == 0) {
if ((val & SCREEN_RIGHT_MOUSE_BUTTON) == SCREEN_RIGHT_MOUSE_BUTTON) {
mouseButtons.right = true;
mouseState.buttons.right = true;
}
} else {
if ((val & SCREEN_RIGHT_MOUSE_BUTTON) == 0) {
mouseButtons.right = false;
mouseState.buttons.right = false;
}
}
if ((mouse_buttons & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) {
if ((val & SCREEN_MIDDLE_MOUSE_BUTTON) == SCREEN_MIDDLE_MOUSE_BUTTON) {
mouseButtons.middle = true;
mouseState.buttons.middle = true;
}
} else {
if ((val & SCREEN_MIDDLE_MOUSE_BUTTON) == 0) {
mouseButtons.middle = false;
mouseState.buttons.middle = false;
}
}
mouse_buttons = val;
Expand Down Expand Up @@ -3188,8 +3195,8 @@ void VulkanExampleBase::windowResize()

void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y)
{
int32_t dx = (int32_t)mousePos.x - x;
int32_t dy = (int32_t)mousePos.y - y;
int32_t dx = (int32_t)mouseState.position.x - x;
int32_t dy = (int32_t)mouseState.position.y - y;

bool handled = false;

Expand All @@ -3200,23 +3207,23 @@ void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y)
mouseMoved((float)x, (float)y, handled);

if (handled) {
mousePos = glm::vec2((float)x, (float)y);
mouseState.position = glm::vec2((float)x, (float)y);
return;
}

if (mouseButtons.left) {
if (mouseState.buttons.left) {
camera.rotate(glm::vec3(dy * camera.rotationSpeed, -dx * camera.rotationSpeed, 0.0f));
viewUpdated = true;
}
if (mouseButtons.right) {
if (mouseState.buttons.right) {
camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f));
viewUpdated = true;
}
if (mouseButtons.middle) {
if (mouseState.buttons.middle) {
camera.translate(glm::vec3(-dx * 0.005f, -dy * 0.005f, 0.0f));
viewUpdated = true;
}
mousePos = glm::vec2((float)x, (float)y);
mouseState.position = glm::vec2((float)x, (float)y);
}

void VulkanExampleBase::windowResized() {}
Expand Down
28 changes: 16 additions & 12 deletions base/vulkanexamplebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ class VulkanExampleBase
bool overlay = true;
} 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 } };

static std::vector<const char*> args;
Expand All @@ -196,7 +212,6 @@ class VulkanExampleBase
bool paused = false;

Camera camera;
glm::vec2 mousePos;

std::string title = "Vulkan Example";
std::string name = "vulkanExample";
Expand All @@ -209,17 +224,6 @@ class VulkanExampleBase
VkImageView view;
} 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
#if defined(_WIN32)
HWND window;
Expand Down
6 changes: 3 additions & 3 deletions examples/computeparticles/computeparticles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* 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)
*/
Expand Down Expand Up @@ -553,8 +553,8 @@ class VulkanExample : public VulkanExampleBase
}
else
{
float normalizedMx = (mousePos.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 normalizedMx = (mouseState.position.x - static_cast<float>(width / 2)) / static_cast<float>(width / 2);
float normalizedMy = (mouseState.position.y - static_cast<float>(height / 2)) / static_cast<float>(height / 2);
compute.uniformData.destX = normalizedMx;
compute.uniformData.destY = normalizedMy;
}
Expand Down
8 changes: 4 additions & 4 deletions examples/imgui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,10 @@ class VulkanExample : public VulkanExampleBase
io.DisplaySize = ImVec2((float)width, (float)height);
io.DeltaTime = frameTimer;

io.MousePos = ImVec2(mousePos.x, mousePos.y);
io.MouseDown[0] = mouseButtons.left && UIOverlay.visible;
io.MouseDown[1] = mouseButtons.right && UIOverlay.visible;
io.MouseDown[2] = mouseButtons.middle && UIOverlay.visible;
io.MousePos = ImVec2(mouseState.position.x, mouseState.position.y);
io.MouseDown[0] = mouseState.buttons.left && UIOverlay.visible;
io.MouseDown[1] = mouseState.buttons.right && UIOverlay.visible;
io.MouseDown[2] = mouseState.buttons.middle && UIOverlay.visible;

draw();
}
Expand Down

0 comments on commit 52779a1

Please sign in to comment.