Crafter.Graphics/interfaces/Crafter.Graphics-Window.cppm

112 lines
4.5 KiB
Text
Raw Normal View History

2025-05-07 19:21:51 +02:00
/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2025-11-17 00:44:45 +01:00
module;
#include <wayland-client.h>
#include <xkbcommon/xkbcommon.h>
#include "../lib/xdg-shell-client-protocol.h"
#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h"
2025-04-16 00:43:33 +02:00
export module Crafter.Graphics:Window;
2025-11-16 15:32:11 +01:00
import std;
2025-04-16 00:43:33 +02:00
import Crafter.Event;
import :UiElement;
import :Types;
export namespace Crafter {
2025-06-13 23:59:36 +02:00
struct ScaleData {
2025-11-16 15:32:11 +01:00
std::int32_t x;
std::int32_t y;
std::int32_t width;
std::int32_t height;
2025-06-13 23:59:36 +02:00
};
2025-04-16 00:43:33 +02:00
class Window {
public:
2025-11-17 00:44:45 +01:00
Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr;
2025-04-16 00:43:33 +02:00
Event<MousePoint> onMouseRightClick;
Event<MousePoint> onMouseLeftClick;
Event<MousePoint> onMouseRightHold;
Event<MousePoint> onMouseLeftHold;
Event<MousePoint> onMouseRightRelease;
Event<MousePoint> onMouseLeftRelease;
Event<MouseMoveEvent> onMouseMove;
Event<MouseMoveEvent> onMouseEnter;
Event<MouseMoveEvent> onMouseLeave;
Event<double> onMouseScroll;
Event<void> onClose;
MousePoint currentMousePos;
MousePoint lastMousePos;
MousePoint mouseDelta;
bool mouseLeftHeld = false;
bool mouseRightHeld = false;
2025-05-04 05:15:31 +02:00
bool heldkeys[255] = {};
Event<void> onKeyDown[255];
Event<void> onKeyHold[255];
Event<void> onKeyUp[255];
2025-06-13 23:59:36 +02:00
Event<char> onAnyKeyDown;
Event<char> onAnyKeyHold;
Event<char> onAnyKeyUp;
2025-05-25 23:04:56 +02:00
std::vector<UiElement> elements;
2025-04-19 15:46:26 +02:00
std::string name;
2025-04-16 00:43:33 +02:00
std::uint32_t width;
std::uint32_t height;
float scale = 1;
2025-04-19 15:46:26 +02:00
bool open = true;
2025-11-17 00:44:45 +01:00
2025-04-16 00:43:33 +02:00
Window(std::string name, std::uint32_t width, std::uint32_t height);
2025-11-17 00:44:45 +01:00
~Window();
void StartSync();
2025-06-13 23:59:36 +02:00
ScaleData ScaleElement(const UiElement& element);
2025-11-17 00:44:45 +01:00
protected:
bool configured = false;
wl_shm* shm = NULL;
wl_seat* seat = NULL;
xdg_toplevel* xdgToplevel = NULL;
xdg_wm_base* xdgWmBase = NULL;
zxdg_decoration_manager_v1* manager = NULL;
wl_surface* surface = NULL;
wl_buffer* buffer = NULL;
wl_buffer* backBuffer = NULL;
xdg_surface* xdgSurface = NULL;
wl_display* display = NULL;
wl_callback* cb = nullptr;
inline static wl_compositor* compositor = NULL;
static wl_pointer_listener pointer_listener;
static wl_keyboard_listener keyboard_listener;
static wl_seat_listener seat_listener;
static wl_registry_listener registry_listener;
static xdg_surface_listener xdg_surface_listener;
static xdg_toplevel_listener xdg_toplevel_listener;
static wl_callback_listener surface_frame_listener;
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y);
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value);
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);
static void PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*);
static void xdg_toplevel_handle_close(void* data, xdg_toplevel*);
static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version);
static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state);
static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities);
static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial);
2025-04-16 00:43:33 +02:00
};
}