Skip to content

Commit

Permalink
[os.input] expanded input API
Browse files Browse the repository at this point in the history
  • Loading branch information
harrand committed Oct 28, 2024
1 parent 0568722 commit fe0f60f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
5 changes: 2 additions & 3 deletions cmake/compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function(configure_msvc)
# TODO
target_compile_definitions(topaz PRIVATE -D_CRT_SECURE_NO_WARNINGS)
if(${CMAKE_BUILD_TYPE} MATCHES "debug")
target_link_options(topaz PUBLIC /MTd)
target_link_options(topaz PRIVATE "/MTd")
else()
target_link_options(topaz PUBLIC /MT)
target_link_options(topaz PRIVATE "/MT")
endif()
endfunction()

Expand All @@ -31,7 +31,6 @@ function(configure_clang)
configure_gnu_like()
if(CMAKE_CXX_SIMULATE_ID MATCHES "MSVC")
# clang-cl (or clang against MSVC). Just pretend we're msvc.
configure_msvc()
return()
endif()
# Code below runs if we're clang proper.
Expand Down
22 changes: 22 additions & 0 deletions include/tz/os/input.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef TOPAZ_OS_INPUT_HPP
#define TOPAZ_OS_INPUT_HPP
#include "tz/core/error.hpp"
#include <utility>

namespace tz::os
{
Expand Down Expand Up @@ -91,6 +92,27 @@ namespace tz::os
* @return True if the given key is currently pressed, otherwise false.
*/
bool is_key_pressed(key k);
constexpr std::pair<unsigned int, unsigned int> invalid_mouse_position{-1u, -1u};
/**
* @ingroup tz_os_input
* @brief Retrieve the mouse cursor's current position, in pixels, relative to the top-left of the window.
*
* If for whatever reason the cursor pos cannot be retrieved, {-1, -1} is returned. Some reasons could include:
* - You haven't opened a window via @ref open_window.
* - The mouse currently lies outside of the window.
*/
std::pair<unsigned int, unsigned int> get_mouse_position();

enum class mouse_button
{
left,
right,
middle,
_count
};

bool is_mouse_clicked(mouse_button b);
std::pair<unsigned int, unsigned int> get_mouse_click_position(mouse_button b);
}

#endif // TOPAZ_OS_INPUT_HPP
46 changes: 46 additions & 0 deletions src/tz/os/impl_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,43 @@ namespace tz::os
return GetAsyncKeyState(key_mappings[static_cast<int>(k)]) & 0x8000;
}

std::pair<unsigned int, unsigned int> get_mouse_position()
{
POINT p;
if(wnd != nullptr && GetCursorPos(&p))
{
if(ScreenToClient(wnd, &p))
{
if(std::cmp_greater(p.x, window_get_width()) || std::cmp_greater(p.y, window_get_height()))
{
return invalid_mouse_position;
}
p.x = std::clamp(static_cast<int>(p.x), 0, static_cast<int>(window_get_width()));
p.y = std::clamp(static_cast<int>(p.y), 0, static_cast<int>(window_get_height()));
return {p.x, p.y};
}
}
return invalid_mouse_position;
}

bool is_mouse_clicked(mouse_button b)
{
std::array<int, static_cast<int>(mouse_button::_count)> button_mappings
{
VK_LBUTTON,
VK_RBUTTON,
VK_MBUTTON
};
return GetAsyncKeyState(button_mappings[static_cast<int>(b)]) & 0x8000;
}

std::pair<unsigned int, unsigned int> last_mouse_click[static_cast<int>(mouse_button::_count)];

std::pair<unsigned int, unsigned int> get_mouse_click_position(mouse_button b)
{
return last_mouse_click[static_cast<int>(b)];
}

std::expected<std::string, tz::error_code> read_file(std::filesystem::path path)
{
HANDLE file = CreateFileA(path.string().c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
Expand Down Expand Up @@ -473,6 +510,15 @@ namespace tz::os
kb_callback(wparam);
}
break;
case WM_RBUTTONDOWN:
last_mouse_click[static_cast<int>(mouse_button::right)] = get_mouse_position();
break;
case WM_LBUTTONDOWN:
last_mouse_click[static_cast<int>(mouse_button::left)] = get_mouse_position();
break;
case WM_MBUTTONDOWN:
last_mouse_click[static_cast<int>(mouse_button::middle)] = get_mouse_position();
break;
}
return DefWindowProcA(hwnd, msg, wparam, lparam);
}
Expand Down

0 comments on commit fe0f60f

Please sign in to comment.