Add mouse button down and mouse button up events support to Win32Window
This commit is contained in:
parent
2c421c37ff
commit
20d8236701
46
src/game/window/mouse.cpp
Normal file
46
src/game/window/mouse.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
PolyGun
|
||||||
|
|
||||||
|
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "game/window/mouse.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
using namespace polygun::window;
|
||||||
|
|
||||||
|
static const std::string g_mouse_button_names[] = {
|
||||||
|
"Unknown",
|
||||||
|
"Left",
|
||||||
|
"Middle",
|
||||||
|
"Right",
|
||||||
|
"Aux 1",
|
||||||
|
"Aux 2",
|
||||||
|
"Aux 3"
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::string& mouse::mouse_button_name(MouseButton button) {
|
||||||
|
static_assert(sizeof(g_mouse_button_names)/sizeof(std::string)==MouseButton::MOUSE_BUTTON_COUNT);
|
||||||
|
if(button>=MouseButton::MOUSE_BUTTON_COUNT)
|
||||||
|
return g_mouse_button_names[0];
|
||||||
|
return g_mouse_button_names[button];
|
||||||
|
}
|
@ -25,15 +25,23 @@ SOFTWARE.
|
|||||||
#ifndef POLYGUN_WINDOW_MOUSE_HPP
|
#ifndef POLYGUN_WINDOW_MOUSE_HPP
|
||||||
#define POLYGUN_WINDOW_MOUSE_HPP
|
#define POLYGUN_WINDOW_MOUSE_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace polygun::window {
|
namespace polygun::window {
|
||||||
enum MouseButton {
|
enum MouseButton {
|
||||||
|
MOUSE_BUTTON_NONE,
|
||||||
MOUSE_BUTTON_LEFT,
|
MOUSE_BUTTON_LEFT,
|
||||||
MOUSE_BUTTON_WHEEL,
|
MOUSE_BUTTON_MIDDLE,
|
||||||
MOUSE_BUTTON_RIGHT,
|
MOUSE_BUTTON_RIGHT,
|
||||||
MOUSE_BUTTON_AUX_1,
|
MOUSE_BUTTON_AUX_1,
|
||||||
MOUSE_BUTTON_AUX_2,
|
MOUSE_BUTTON_AUX_2,
|
||||||
MOUSE_BUTTON_AUX_3
|
MOUSE_BUTTON_AUX_3,
|
||||||
|
MOUSE_BUTTON_COUNT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace mouse {
|
||||||
|
const std::string& mouse_button_name(MouseButton button);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // POLYGUN_WINDOW_MOUSE_HPP
|
#endif // POLYGUN_WINDOW_MOUSE_HPP
|
@ -256,6 +256,7 @@ static WPARAM map_virtual_key_code(WPARAM virtual_key, LPARAM param) {
|
|||||||
static LRESULT CALLBACK window_proc(HWND window_handle, UINT message, WPARAM param1, LPARAM param2) {
|
static LRESULT CALLBACK window_proc(HWND window_handle, UINT message, WPARAM param1, LPARAM param2) {
|
||||||
LRESULT result = 0;
|
LRESULT result = 0;
|
||||||
Event event;
|
Event event;
|
||||||
|
bool handled = false;
|
||||||
switch(message) {
|
switch(message) {
|
||||||
case WM_KEYDOWN:
|
case WM_KEYDOWN:
|
||||||
case WM_SYSKEYDOWN:
|
case WM_SYSKEYDOWN:
|
||||||
@ -265,8 +266,8 @@ static LRESULT CALLBACK window_proc(HWND window_handle, UINT message, WPARAM par
|
|||||||
event.m_data.m_key_event.m_key = KeyCode::KEY_NONE;
|
event.m_data.m_key_event.m_key = KeyCode::KEY_NONE;
|
||||||
else
|
else
|
||||||
event.m_data.m_key_event.m_key = g_virtual_keycode_to_keycode[param1];
|
event.m_data.m_key_event.m_key = g_virtual_keycode_to_keycode[param1];
|
||||||
g_current_window.load()->push_event(event);
|
|
||||||
break;
|
break;
|
||||||
|
handled = true;
|
||||||
case WM_KEYUP:
|
case WM_KEYUP:
|
||||||
case WM_SYSKEYUP:
|
case WM_SYSKEYUP:
|
||||||
event.m_type = EventType::EVENT_TYPE_KEY_UP;
|
event.m_type = EventType::EVENT_TYPE_KEY_UP;
|
||||||
@ -275,18 +276,50 @@ static LRESULT CALLBACK window_proc(HWND window_handle, UINT message, WPARAM par
|
|||||||
event.m_data.m_key_event.m_key = KeyCode::KEY_NONE;
|
event.m_data.m_key_event.m_key = KeyCode::KEY_NONE;
|
||||||
else
|
else
|
||||||
event.m_data.m_key_event.m_key = g_virtual_keycode_to_keycode[param1];
|
event.m_data.m_key_event.m_key = g_virtual_keycode_to_keycode[param1];
|
||||||
g_current_window.load()->push_event(event);
|
handled = true;
|
||||||
|
break;
|
||||||
|
case WM_LBUTTONDOWN:
|
||||||
|
event.m_type = EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN;
|
||||||
|
event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_LEFT;
|
||||||
|
handled = true;
|
||||||
|
break;
|
||||||
|
case WM_LBUTTONUP:
|
||||||
|
event.m_type = EventType::EVENT_TYPE_MOUSE_BUTTON_UP;
|
||||||
|
event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_LEFT;
|
||||||
|
handled = true;
|
||||||
|
break;
|
||||||
|
case WM_MBUTTONDOWN:
|
||||||
|
event.m_type = EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN;
|
||||||
|
event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_MIDDLE;
|
||||||
|
handled = true;
|
||||||
|
break;
|
||||||
|
case WM_MBUTTONUP:
|
||||||
|
event.m_type = EventType::EVENT_TYPE_MOUSE_BUTTON_UP;
|
||||||
|
event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_MIDDLE;
|
||||||
|
handled = true;
|
||||||
|
break;
|
||||||
|
case WM_RBUTTONDOWN:
|
||||||
|
event.m_type = EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN;
|
||||||
|
event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_RIGHT;
|
||||||
|
handled = true;
|
||||||
|
break;
|
||||||
|
case WM_RBUTTONUP:
|
||||||
|
event.m_type = EventType::EVENT_TYPE_MOUSE_BUTTON_UP;
|
||||||
|
event.m_data.m_mouse_event.m_button = MouseButton::MOUSE_BUTTON_RIGHT;
|
||||||
|
handled = true;
|
||||||
break;
|
break;
|
||||||
case WM_SIZE:
|
case WM_SIZE:
|
||||||
event.m_type = EventType::EVENT_TYPE_WINDOW_RESIZE;
|
event.m_type = EventType::EVENT_TYPE_WINDOW_RESIZE;
|
||||||
event.m_data.m_window_event.m_width = LOWORD(param2);
|
event.m_data.m_window_event.m_width = LOWORD(param2);
|
||||||
event.m_data.m_window_event.m_height = HIWORD(param2);
|
event.m_data.m_window_event.m_height = HIWORD(param2);
|
||||||
g_current_window.load()->push_event(event);
|
handled = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
result = DefWindowProc(window_handle, message, param1, param2);
|
result = DefWindowProc(window_handle, message, param1, param2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if(handled)
|
||||||
|
g_current_window.load()->push_event(event);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user