new input system

This commit is contained in:
Jorijn van der Graaf 2026-05-12 00:24:48 +02:00
commit ac2eb7fb0a
31 changed files with 3292 additions and 781 deletions

View file

@ -46,6 +46,7 @@ module;
export module Crafter.Graphics:Window;
import std;
import :Types;
import :Keys;
import Crafter.Event;
export namespace Crafter {
@ -66,15 +67,21 @@ export namespace Crafter {
Event<void> onClose;
Event<void> onBeforeUpdate;
Event<FrameTime> onUpdate;
// Fires when the swapchain has been recreated for a new size.
// width/height already reflect the new size. Passes that hold
// descriptors referring to window.imageViews[] must re-write them
// here (the old VkImage handles have been destroyed).
Event<void> onResize;
bool open = true;
bool updating = false;
bool heldkeys[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)] = {};
Event<void> onKeyDown[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
Event<void> onKeyHold[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
Event<void> onKeyUp[static_cast<std::uint32_t>(CrafterKeys::CrafterKeysMax)];
Event<CrafterKeys> onAnyKeyDown;
Event<CrafterKeys> onAnyKeyHold;
Event<CrafterKeys> onAnyKeyUp;
// Currently-pressed raw key codes. The runtime stores raw platform
// codes only (Win32 PS/2 scancode + extended bit; Wayland kernel
// keycode). Cross-platform default bindings use `Key(CrafterKeys::X)`
// from :Keys to obtain the right code at compile time.
std::unordered_set<KeyCode> heldKeys;
Event<KeyCode> onRawKeyDown;
Event<KeyCode> onRawKeyHold;
Event<KeyCode> onRawKeyUp;
Event<const std::string_view> onTextInput;
Event<void> onMouseRightClick;
Event<void> onMouseLeftClick;
@ -141,6 +148,11 @@ export namespace Crafter {
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
float scale = 1.0f;
bool configured = false;
// Pending size from the most recent xdg_toplevel.configure, in
// surface-local (logical DP) units. Applied on xdg_surface.configure
// via Resize(); 0 means "compositor has no preference, keep current".
std::int32_t pendingLogicalWidth = 0;
std::int32_t pendingLogicalHeight = 0;
xdg_toplevel* xdgToplevel = nullptr;
wp_viewport* wpViewport = nullptr;
wl_surface* surface = nullptr;
@ -186,6 +198,12 @@ export namespace Crafter {
VkCommandBuffer GetCmd();
void EndCmd(VkCommandBuffer cmd);
void CreateSwapchain();
// Tear-and-rebuild helper used by Resize() and the OUT_OF_DATE
// recovery in Render(). Calls CreateSwapchain() and re-issues the
// initial PRESENT_SRC_KHR layout transition for the new images so
// Render()'s barriers (which assume oldLayout = PRESENT_SRC_KHR)
// stay valid. Does NOT fire onResize — callers do that.
void RecreateSwapchainAndImages();
// Save the current swapchain image (state after Render() returns) to
// a PNG file. Allocates a one-shot staging buffer + command buffer,