roleplay/client/include/ui.h

318 lines
6.9 KiB
C

#ifndef PIPELINE_H
#define PIPELINE_H
#include "vulkan/vulkan_core.h"
#include "cglm/types.h"
#include "vk_mem_alloc.h"
#include "gpu.h"
#include "ft2build.h"
#include FT_FREETYPE_H
#define ANCHOR_TOP_LEFT 0
#define ANCHOR_TOP_RIGHT 1
#define ANCHOR_BOTTOM_LEFT 2
#define ANCHOR_BOTTOM_RIGHT 3
#define ANCHOR_CENTER 4
#define DRAWABLE_TYPE_RECT 0
#define DRAWABLE_TYPE_GLYPH 1
#define DRAWABLE_TYPE_IMAGE 2
#define DRAWABLE_TYPE_RECT_HSV 3
#define UI_EVENT_CURSOR (1 << 0)
#define UI_EVENT_SCROLL (1 << 1)
#define UI_EVENT_BUTTON (1 << 2)
typedef struct UIPushConstantStruct {
VkDeviceAddress layer;
float time;
uint32_t pad;
} UIPushConstant;
typedef struct GPUFontStruct {
VkDeviceAddress symbol_list;
uint32_t num_symbols;
uint32_t width;
uint32_t height;
} GPUFont;
typedef struct GPUSymbolStruct {
float top;
float left;
float width;
float height;
vec2 advance;
} GPUSymbol;
typedef struct FontStruct {
VmaAllocation symbol_memory;
VmaAllocation image_memory;
VkBuffer symbols;
VkImage image;
VkImageView view;
VkSampler sampler;
uint32_t* charmap;
uint32_t num_symbols;
uint32_t index;
char* family;
char* style;
} Font;
typedef struct TextureStruct {
VmaAllocation image_memory;
VkImage image;
VkImageView view;
VkSampler sampler;
char* path;
} Texture;
typedef struct GPUStringStruct {
vec2 pos;
vec4 color;
float size;
uint32_t offset;
uint32_t length;
uint32_t font;
} GPUString;
typedef struct GPUDrawableStruct {
vec2 pos;
vec2 size;
vec4 color[4];
uint32_t type;
uint32_t var;
uint32_t events;
} GPUDrawable;
typedef struct GPULayerStruct {
DrawCommand draw;
DispatchCommand dispatch_strings;
uint32_t max_drawables;
uint32_t max_codes;
uint32_t max_strings;
uint32_t num_drawables;
VkDeviceAddress strings;
VkDeviceAddress codes;
VkDeviceAddress drawables;
VkDeviceAddress container;
} GPULayer;
typedef struct LayerStruct {
VkBuffer strings[MAX_FRAMES_IN_FLIGHT];
VkBuffer codes[MAX_FRAMES_IN_FLIGHT];
VkBuffer drawables[MAX_FRAMES_IN_FLIGHT];
VkBuffer layer[MAX_FRAMES_IN_FLIGHT];
VmaAllocation strings_memory[MAX_FRAMES_IN_FLIGHT];
VmaAllocation drawables_memory[MAX_FRAMES_IN_FLIGHT];
VmaAllocation codes_memory[MAX_FRAMES_IN_FLIGHT];
VmaAllocation layer_memory[MAX_FRAMES_IN_FLIGHT];
VkDeviceAddress address[MAX_FRAMES_IN_FLIGHT];
GPUDrawable* drawables_buffer;
GPUString* strings_buffer;
uint32_t* codes_buffer;
GPULayer data;
} Layer;
typedef struct LayerInputStruct {
uint32_t num_strings;
uint32_t num_codes;
uint32_t num_drawables;
uint32_t max_codes;
uint32_t max_strings;
uint32_t max_drawables;
GPUString* strings;
uint32_t* codes;
GPUDrawable* drawables;
} LayerInput;
typedef struct GPUContainerStruct {
vec2 offset;
vec2 size;
uint32_t anchor;
VkDeviceAddress context;
} GPUContainer;
typedef struct UIContextStruct UIContext;
typedef void (*ui_text_callback)(void* data, UIContext* ui, RenderContext* gpu, unsigned int codepoint);
typedef bool (*ui_key_callback)(void* data, UIContext* ui, RenderContext* gpu, int key, int action, int mods);
typedef void (*ui_button_callback)(void* data, UIContext* ui, RenderContext* gpu, float x, float y, int button, int action, int mods);
typedef void (*ui_scroll_callback)(void* data, UIContext* ui, RenderContext* gpu, double x, double y);
typedef void (*ui_cursor_callback)(void* data, UIContext* ui, RenderContext* gpu, float x, float y);
typedef void (*ui_deselect_callback)(void* data, UIContext* ui, RenderContext* gpu);
typedef struct UICallbacksStruct {
uint32_t layer;
uint32_t element;
void* data;
ui_text_callback text;
ui_key_callback key;
ui_button_callback button;
ui_scroll_callback scroll;
ui_cursor_callback cursor;
ui_deselect_callback deselect;
} UICallbacks;
typedef struct ContainerStruct {
VkBuffer container[MAX_FRAMES_IN_FLIGHT];
VmaAllocation container_memory[MAX_FRAMES_IN_FLIGHT];
VkDeviceAddress address[MAX_FRAMES_IN_FLIGHT];
GPUContainer data;
uint32_t id;
uint32_t layer_count;
Layer* layers;
uint32_t callback_count;
UICallbacks* callbacks;
} Container;
typedef struct ContainerInputStruct {
uint32_t id;
uint32_t anchor;
vec2 offset;
vec2 size;
uint32_t layer_count;
LayerInput* layers;
uint32_t callback_count;
UICallbacks* callbacks;
} ContainerInput;
typedef struct GPUUIContextStruct {
VkDeviceAddress font_infos;
vec2 screen;
vec2 extent;
vec2 scale;
} GPUUIContext;
struct UIContextStruct {
VkDeviceAddress address;
VkBuffer context;
VmaAllocation context_memory;
VkBuffer font_infos;
VmaAllocation font_infos_memory;
VkDescriptorSet textures;
VkDescriptorSet samplers;
VkDescriptorSet font_samplers;
VkDescriptorSet font_textures;
VkDescriptorSetLayout samplers_layout;
VkDescriptorSetLayout textures_layout;
VkDescriptorPool fonts_pool;
VkDescriptorPool textures_pool;
GraphicsPipeline pipeline;
ComputePipeline string_pipeline;
uint32_t max_fonts;
uint32_t max_textures;
Font* fonts;
Texture* texture_slots;
uint32_t max_containers;
Container* containers;
GPUUIContext data;
FT_Library freetype;
UICallbacks* active_callbacks;
uint32_t active_container;
uint32_t active_layer;
uint32_t active_element;
double cursor[2];
};
VkResult create_ui_context(
uint32_t max_fonts,
uint32_t max_textures,
uint32_t max_containers,
RenderContext* gpu,
UIContext* context);
VkResult load_font(
uint32_t index,
const char* ttf_file,
uint32_t size,
VkBool32 antialias,
RenderContext* gpu,
UIContext* context);
VkResult load_texture(
const char* png_path,
RenderContext* gpu,
UIContext* context,
uint32_t* index);
VkResult create_container(
ContainerInput* container,
RenderContext* gpu,
UIContext* context);
VkResult create_layer(
uint32_t index,
LayerInput* input,
RenderContext* gpu,
Container* container);
VkResult map_string(
const char * text,
uint32_t* buffer,
uint32_t offset,
uint32_t font,
UIContext* context);
VkResult update_ui_context_resolution(
UIContext* ui,
RenderContext* gpu);
VkResult update_ui_string(
char* string,
uint32_t container_id,
uint32_t layer_index,
uint32_t string_index,
UIContext* ui,
RenderContext* gpu);
Container* context_container(uint32_t container_id, UIContext* ui);
bool ui_intersect(
double cursor[2],
RenderContext* gpu,
UIContext* ui,
uint32_t events,
uint32_t* container,
uint32_t* layer,
uint32_t* element,
vec2 position);
void set_active_element(uint32_t container, uint32_t layer, uint32_t element, UIContext* ui);
void clear_active_element(UIContext* ui, RenderContext* gpu);
void anchor_offset(RenderContext* gpu, Container* container, vec2 offset);
#endif