[ui, win32]

- change coord system of gl canvas to match ui
- change mouse coordinate system to match ui
- fix mouse delta
This commit is contained in:
martinfouilleul 2023-03-09 17:38:41 +01:00
parent c7d5cf113e
commit 9accf4c666
7 changed files with 115 additions and 73 deletions

View File

@ -450,7 +450,7 @@ int main()
ui_style_next(&(ui_style){.size.width = {UI_SIZE_PIXELS, 300},
.size.height = {UI_SIZE_TEXT}},
UI_STYLE_SIZE);
static str8 text = {};
static str8 text = {0};
ui_text_box_result res = ui_text_box("textbox", mem_scratch(), text);
if(res.changed)
{

View File

@ -2,7 +2,7 @@
*
* file: glsl_shaders.h
* note: string literals auto-generated by embed_text.py
* date: 03/032023
* date: 09/032023
*
**********************************************************************/
#ifndef __GLSL_SHADERS_H__
@ -35,11 +35,13 @@ const char* glsl_blit_vertex =
"\n"
"void main()\n"
"{\n"
" /* generate (0, 0) (1, 0) (1, 1) (1, 1) (0, 1) (0, 0)*/\n"
"\n"
" float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);\n"
" float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);\n"
"\n"
" gl_Position = vec4(-1.0f + x*2.0f, -1.0f+y*2.0f, 0.0f, 1.0f);\n"
" uv = vec2(x, y);\n"
" uv = vec2(x, 1-y);\n"
"}\n";
//NOTE: string imported from src\glsl_shaders\blit_fragment.glsl

View File

@ -5,9 +5,11 @@ out vec2 uv;
void main()
{
/* generate (0, 0) (1, 0) (1, 1) (1, 1) (0, 1) (0, 0)*/
float x = float(((uint(gl_VertexID) + 2u) / 3u)%2u);
float y = float(((uint(gl_VertexID) + 1u) / 3u)%2u);
gl_Position = vec4(-1.0f + x*2.0f, -1.0f+y*2.0f, 0.0f, 1.0f);
uv = vec2(x, y);
uv = vec2(x, 1-y);
}

View File

@ -6,6 +6,7 @@
* @revision:
*
*****************************************************************/
#include"platform.h"
#include"memory.h"
#include"hash.h"
#include"platform_clock.h"
@ -33,7 +34,7 @@ static ui_style UI_STYLE_DEFAULTS =
// context
//-----------------------------------------------------------------------------
const u32 UI_MAX_INPUT_CHAR_PER_FRAME = 64;
enum { UI_MAX_INPUT_CHAR_PER_FRAME = 64 };
typedef struct ui_input_text
{
@ -60,7 +61,7 @@ typedef struct ui_tag_elt
ui_tag tag;
} ui_tag_elt;
const u64 UI_BOX_MAP_BUCKET_COUNT = 1024;
enum { UI_BOX_MAP_BUCKET_COUNT = 1024 };
typedef struct ui_context
{
@ -94,8 +95,8 @@ typedef struct ui_context
} ui_context;
__thread ui_context __uiThreadContext = {0};
__thread ui_context* __uiCurrentContext = 0;
mp_thread_local ui_context __uiThreadContext = {0};
mp_thread_local ui_context* __uiCurrentContext = 0;
ui_context* ui_get_context(void)
{
@ -240,7 +241,7 @@ ui_key ui_key_make_str8(str8 string)
seed = parent->key.hash;
}
ui_key key = {};
ui_key key = {0};
key.hash = mp_hash_aes_string_seed(string, seed);
return(key);
}
@ -1909,7 +1910,7 @@ ui_sig ui_menu_button(const char* name)
|UI_STYLE_LAYOUT_MARGIN_X
|UI_STYLE_BG_COLOR);
ui_pattern pattern = {};
ui_pattern pattern = {0};
ui_pattern_push(&ui->frameArena, &pattern, (ui_selector){.kind = UI_SEL_STATUS, .status = UI_HOVER});
ui_style style = {.bgColor = {0, 0, 1, 1}};
@ -1937,7 +1938,7 @@ str32 ui_edit_replace_selection_with_codepoints(ui_context* ui, str32 codepoints
str32 before = str32_slice(codepoints, 0, start);
str32 after = str32_slice(codepoints, end, codepoints.len);
str32_list list = {};
str32_list list = {0};
str32_list_push(&ui->frameArena, &list, before);
str32_list_push(&ui->frameArena, &list, input);
str32_list_push(&ui->frameArena, &list, after);

View File

@ -327,24 +327,24 @@ typedef struct ui_context ui_context;
//-------------------------------------------------------------------------------------
// UI context initialization and frame cycle
//-------------------------------------------------------------------------------------
void ui_init(void);
ui_context* ui_get_context(void);
void ui_set_context(ui_context* context);
MP_API void ui_init(void);
MP_API ui_context* ui_get_context(void);
MP_API void ui_set_context(ui_context* context);
void ui_begin_frame(ui_style* defaultStyle, ui_style_mask mask);
void ui_end_frame(void);
void ui_draw(void);
MP_API void ui_begin_frame(ui_style* defaultStyle, ui_style_mask mask);
MP_API void ui_end_frame(void);
MP_API void ui_draw(void);
#define ui_frame(s, m) defer_loop(ui_begin_frame((s), (m)), ui_end_frame())
//-------------------------------------------------------------------------------------
// Box keys
//-------------------------------------------------------------------------------------
ui_key ui_key_make_str8(str8 string);
ui_key ui_key_make_path(str8_list path);
MP_API ui_key ui_key_make_str8(str8 string);
MP_API ui_key ui_key_make_path(str8_list path);
ui_box* ui_box_lookup_key(ui_key key);
ui_box* ui_box_lookup_str8(str8 string);
MP_API ui_box* ui_box_lookup_key(ui_key key);
MP_API ui_box* ui_box_lookup_str8(str8 string);
// C-string helper
#define ui_key_make(s) ui_key_make_str8(STR8(s))
@ -353,17 +353,17 @@ ui_box* ui_box_lookup_str8(str8 string);
//-------------------------------------------------------------------------------------
// Box hierarchy building
//-------------------------------------------------------------------------------------
ui_box* ui_box_make_str8(str8 string, ui_flags flags);
ui_box* ui_box_begin_str8(str8 string, ui_flags flags);
MP_API ui_box* ui_box_make_str8(str8 string, ui_flags flags);
MP_API ui_box* ui_box_begin_str8(str8 string, ui_flags flags);
ui_box* ui_box_end(void);
MP_API ui_box* ui_box_end(void);
#define ui_container(name, flags) defer_loop(ui_box_begin(name, flags), ui_box_end())
void ui_box_push(ui_box* box);
void ui_box_pop(void);
ui_box* ui_box_top(void);
MP_API void ui_box_push(ui_box* box);
MP_API void ui_box_pop(void);
MP_API ui_box* ui_box_top(void);
void ui_box_set_render_proc(ui_box* box, ui_box_render_proc proc, void* data);
MP_API void ui_box_set_render_proc(ui_box* box, ui_box_render_proc proc, void* data);
// C-string helpers
#define ui_box_lookup(s) ui_box_lookup_str8(STR8(s))
@ -373,24 +373,24 @@ void ui_box_set_render_proc(ui_box* box, ui_box_render_proc proc, void* data);
//-------------------------------------------------------------------------------------
// Box status and signals
//-------------------------------------------------------------------------------------
bool ui_box_closed(ui_box* box);
void ui_box_set_closed(ui_box* box, bool closed);
MP_API bool ui_box_closed(ui_box* box);
MP_API void ui_box_set_closed(ui_box* box, bool closed);
bool ui_box_active(ui_box* box);
void ui_box_activate(ui_box* box);
void ui_box_deactivate(ui_box* box);
MP_API bool ui_box_active(ui_box* box);
MP_API void ui_box_activate(ui_box* box);
MP_API void ui_box_deactivate(ui_box* box);
bool ui_box_hot(ui_box* box);
void ui_box_set_hot(ui_box* box, bool hot);
MP_API bool ui_box_hot(ui_box* box);
MP_API void ui_box_set_hot(ui_box* box, bool hot);
ui_sig ui_box_sig(ui_box* box);
MP_API ui_sig ui_box_sig(ui_box* box);
//-------------------------------------------------------------------------------------
// Tagging
//-------------------------------------------------------------------------------------
ui_tag ui_tag_make_str8(str8 string);
void ui_tag_box_str8(ui_box* box, str8 string);
void ui_tag_next_str8(str8 string);
MP_API ui_tag ui_tag_make_str8(str8 string);
MP_API void ui_tag_box_str8(ui_box* box, str8 string);
MP_API void ui_tag_next_str8(str8 string);
// C-string helpers
#define ui_tag_make(s) ui_tag_make_str8(STR8(s))
@ -403,15 +403,15 @@ void ui_tag_next_str8(str8 string);
//NOTE: styling API
//WARN: You can use a pattern in multiple rules, but be aware that a pattern is references an underlying list of selectors,
// hence pushing to a pattern also modifies rules in which the pattern was previously used!
void ui_apply_style_with_mask(ui_style* dst, ui_style* src, ui_style_mask mask);
MP_API void ui_apply_style_with_mask(ui_style* dst, ui_style* src, ui_style_mask mask);
void ui_pattern_push(mem_arena* arena, ui_pattern* pattern, ui_selector selector);
ui_pattern ui_pattern_all(void);
ui_pattern ui_pattern_owner(void);
MP_API void ui_pattern_push(mem_arena* arena, ui_pattern* pattern, ui_selector selector);
MP_API ui_pattern ui_pattern_all(void);
MP_API ui_pattern ui_pattern_owner(void);
void ui_style_next(ui_style* style, ui_style_mask mask);
void ui_style_match_before(ui_pattern pattern, ui_style* style, ui_style_mask mask);
void ui_style_match_after(ui_pattern pattern, ui_style* style, ui_style_mask mask);
MP_API void ui_style_next(ui_style* style, ui_style_mask mask);
MP_API void ui_style_match_before(ui_pattern pattern, ui_style* style, ui_style_mask mask);
MP_API void ui_style_match_after(ui_pattern pattern, ui_style* style, ui_style_mask mask);
//-------------------------------------------------------------------------
// Basic widget helpers
@ -426,30 +426,27 @@ enum {
UI_STYLE_TAG_MENU
};
ui_sig ui_label(const char* label);
MP_API ui_sig ui_label(const char* label);
MP_API ui_sig ui_button(const char* label);
MP_API ui_box* ui_slider(const char* label, f32 thumbRatio, f32* scrollValue);
ui_sig ui_button(const char* label);
ui_box* ui_slider(const char* label, f32 thumbRatio, f32* scrollValue);
ui_box* ui_scrollbar(const char* label, f32 thumbRatio, f32* scrollValue);
void ui_panel_begin(const char* name, ui_flags flags);
void ui_panel_end(void);
MP_API void ui_panel_begin(const char* name, ui_flags flags);
MP_API void ui_panel_end(void);
#define ui_panel(s, f) defer_loop(ui_panel_begin(s, f), ui_panel_end())
ui_sig ui_tooltip_begin(const char* name);
void ui_tooltip_end(void);
MP_API ui_sig ui_tooltip_begin(const char* name);
MP_API void ui_tooltip_end(void);
#define ui_tooltip(name) defer_loop(ui_tooltip_begin(name), ui_tooltip_end())
void ui_menu_bar_begin(const char* label);
void ui_menu_bar_end(void);
MP_API void ui_menu_bar_begin(const char* label);
MP_API void ui_menu_bar_end(void);
#define ui_menu_bar(name) defer_loop(ui_menu_bar_begin(name), ui_menu_bar_end())
void ui_menu_begin(const char* label);
void ui_menu_end(void);
MP_API void ui_menu_begin(const char* label);
MP_API void ui_menu_end(void);
#define ui_menu(name) defer_loop(ui_menu_begin(name), ui_menu_end())
ui_sig ui_menu_button(const char* name);
MP_API ui_sig ui_menu_button(const char* name);
typedef struct ui_text_box_result
{
@ -459,7 +456,7 @@ typedef struct ui_text_box_result
}ui_text_box_result;
ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text);
MP_API ui_text_box_result ui_text_box(const char* name, mem_arena* arena, str8 text);
#ifdef __cplusplus
} // extern "C"

View File

@ -176,7 +176,7 @@ void* mg_wgl_get_proc(const char* name)
mg_surface_data* mg_wgl_surface_create_for_window(mp_window window)
{
mg_surface* surface = 0;
mg_wgl_surface* surface = 0;
mp_window_data* windowData = mp_window_ptr_from_handle(window);
if(windowData)
@ -184,7 +184,7 @@ mg_surface_data* mg_wgl_surface_create_for_window(mp_window window)
wgl_init();
//NOTE: fill surface data and load api
mg_wgl_surface* surface = malloc_type(mg_wgl_surface);
surface = malloc_type(mg_wgl_surface);
if(surface)
{
mg_surface_init_for_window((mg_surface_data*)surface, windowData);

View File

@ -177,9 +177,9 @@ static mp_key_code mp_convert_win32_key(int code)
return(__mpApp.keyCodes[code]);
}
static mp_key_mods mp_get_mod_keys()
static mp_keymod_flags mp_get_mod_keys()
{
mp_key_mods mods = 0;
mp_keymod_flags mods = 0;
if(GetKeyState(VK_SHIFT) & 0x8000)
{
mods |= MP_KEYMOD_SHIFT;
@ -218,7 +218,7 @@ static void process_mouse_event(mp_window_data* window, mp_key_action action, mp
}
}
mp_update_key_state(&__mpApp.inputState.mouse.buttons[button], action == MP_KEY_PRESS);
mp_update_key_state(&__mpApp.inputState.mouse.buttons[button], action);
//TODO click/double click
mp_event event = {0};
@ -236,7 +236,7 @@ static void process_wheel_event(mp_window_data* window, f32 x, f32 y)
mp_event event = {0};
event.window = mp_window_handle_from_ptr(window);
event.type = MP_EVENT_MOUSE_WHEEL;
event.move.deltaX = -x/30.0f;
event.move.deltaX = x/30.0f;
event.move.deltaY = y/30.0f;
event.move.mods = mp_get_mod_keys();
@ -385,7 +385,7 @@ LRESULT WinProc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
event.window = mp_window_handle_from_ptr(mpWindow);
event.type = MP_EVENT_MOUSE_MOVE;
event.move.x = LOWORD(lParam) / scaling;
event.move.y = (rect.bottom - HIWORD(lParam)) / scaling;
event.move.y = HIWORD(lParam) / scaling;
if(__mpApp.inputState.mouse.posValid)
{
@ -414,8 +414,8 @@ LRESULT WinProc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
.move.y = event.move.y};
mp_queue_event(&enter);
}
__mpApp.inputState.mouse.pos.x = event.move.x;
__mpApp.inputState.mouse.pos.y = event.move.y;
mp_update_mouse_move(event.move.x, event.move.y, event.move.deltaX, event.move.deltaY);
mp_queue_event(&event);
} break;
@ -515,13 +515,14 @@ void mp_request_quit()
void mp_pump_events(f64 timeout)
{
__mpApp.inputState.frameCounter++;
MSG message;
while(PeekMessage(&message, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
__mpApp.inputState.frameCounter++;
}
//--------------------------------------------------------------------
@ -777,6 +778,33 @@ mp_rect mp_window_get_content_rect(mp_window window)
return(rect);
}
//--------------------------------------------------------------------------------
// clipboard functions
//--------------------------------------------------------------------------------
MP_API void mp_clipboard_clear(void)
{
//TODO
}
MP_API void mp_clipboard_set_string(str8 string)
{
//TODO
}
MP_API str8 mp_clipboard_get_string(mem_arena* arena)
{
//TODO
return((str8){0});
}
MP_API str8 mp_clipboard_copy_string(str8 backing)
{
//TODO
return((str8){0});
}
//--------------------------------------------------------------------------------
// win32 surfaces
//--------------------------------------------------------------------------------
@ -869,6 +897,18 @@ void mg_surface_cleanup(mg_surface_data* surface)
DestroyWindow(surface->layer.hWnd);
}
LRESULT LayerWinProc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message == WM_NCHITTEST)
{
return(HTTRANSPARENT);
}
else
{
return(DefWindowProc(windowHandle, message, wParam, lParam));
}
}
void mg_surface_init_for_window(mg_surface_data* surface, mp_window_data* window)
{
surface->contentsScaling = mg_win32_surface_contents_scaling;
@ -880,7 +920,7 @@ void mg_surface_init_for_window(mg_surface_data* surface, mp_window_data* window
//NOTE(martin): create a child window for the surface
WNDCLASS layerWindowClass = {.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
.lpfnWndProc = DefWindowProc,
.lpfnWndProc = LayerWinProc,
.hInstance = GetModuleHandleW(NULL),
.lpszClassName = "layer_window_class",
.hCursor = LoadCursor(0, IDC_ARROW)};