Skip to content

Commit

Permalink
Merge pull request #96 from Samdal/master
Browse files Browse the repository at this point in the history
GUI ID changes, copy/paste support, gui_textbox paste and word-deletion
  • Loading branch information
MrFrenik authored Feb 7, 2024
2 parents 95abb9d + 363bfcb commit 7c9b32c
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 71 deletions.
2 changes: 2 additions & 0 deletions gs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5402,6 +5402,8 @@ GS_API_DECL void gs_platform_framebuffer_size(uint32_t handle, u
GS_API_DECL uint32_t gs_platform_framebuffer_width(uint32_t handle);
GS_API_DECL uint32_t gs_platform_framebuffer_height(uint32_t handle);
GS_API_DECL gs_vec2 gs_platform_monitor_sizev(uint32_t id);
GS_API_DECL void gs_platform_window_set_clipboard(uint32_t handle, const char* str);
GS_API_DECL const char* gs_platform_window_get_clipboard(uint32_t handle);

// Platform callbacks
GS_API_DECL void gs_platform_set_framebuffer_resize_callback(uint32_t handle, gs_framebuffer_resize_callback_t cb);
Expand Down
11 changes: 11 additions & 0 deletions impl/gs_platform_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,17 @@ GS_API_DECL gs_vec2 gs_platform_monitor_sizev(uint32_t id)
return ms;
}

GS_API_DECL void gs_platform_window_set_clipboard(uint32_t handle, const char* str)
{
gs_platform_window_t* win = gs_slot_array_getp(gs_subsystem(platform)->windows, handle);
glfwSetClipboardString((GLFWwindow*)win->hndl, str);
}
GS_API_DECL const char* gs_platform_window_get_clipboard(uint32_t handle)
{
gs_platform_window_t* win = gs_slot_array_getp(gs_subsystem(platform)->windows, handle);
return glfwGetClipboardString((GLFWwindow*)win->hndl);
}

void gs_platform_set_cursor(uint32_t handle, gs_platform_cursor cursor)
{
gs_platform_t* platform = gs_subsystem(platform);
Expand Down
196 changes: 125 additions & 71 deletions util/gs_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,38 @@ typedef enum gs_gui_alt_drag_mode_type {
} gs_gui_alt_drag_mode_type;

enum {
GS_GUI_OPT_NOSTYLESHADOW = (1 << 0),
GS_GUI_OPT_NOSTYLEBORDER = (1 << 1),
GS_GUI_OPT_NOINTERACT = (1 << 2),
GS_GUI_OPT_NOFRAME = (1 << 3),
GS_GUI_OPT_NORESIZE = (1 << 4),
GS_GUI_OPT_NOSCROLL = (1 << 5),
GS_GUI_OPT_NOCLOSE = (1 << 6),
GS_GUI_OPT_NOTITLE = (1 << 7),
GS_GUI_OPT_HOLDFOCUS = (1 << 8),
GS_GUI_OPT_AUTOSIZE = (1 << 9),
GS_GUI_OPT_POPUP = (1 << 10),
GS_GUI_OPT_CLOSED = (1 << 11),
GS_GUI_OPT_EXPANDED = (1 << 12),
GS_GUI_OPT_NOHOVER = (1 << 13),
GS_GUI_OPT_FORCESETRECT = (1 << 14),
GS_GUI_OPT_NOFOCUS = (1 << 15),
GS_GUI_OPT_FORCEFOCUS = (1 << 16),
GS_GUI_OPT_NOMOVE = (1 << 17),
GS_GUI_OPT_NOCLIP = (1 << 18),
GS_GUI_OPT_NODOCK = (1 << 19),
GS_GUI_OPT_FULLSCREEN = (1 << 20),
GS_GUI_OPT_DOCKSPACE = (1 << 21),
GS_GUI_OPT_NOBRINGTOFRONT = (1 << 22),
GS_GUI_OPT_LEFTCLICKONLY = (1 << 23),
GS_GUI_OPT_NOSWITCHSTATE = (1 << 24),
GS_GUI_OPT_NOBORDER = (1 << 25),
GS_GUI_OPT_ISCONTENT = (1 << 26),
GS_GUI_OPT_NOCARET = (1 << 27),
GS_GUI_OPT_NOSCROLLHORIZONTAL = (1 << 28),
GS_GUI_OPT_NOSCROLLVERTICAL = (1 << 29),
GS_GUI_OPT_NOSTYLEBACKGROUND = (1 << 30)
GS_GUI_OPT_NOSTYLESHADOW = (1ULL << 0),
GS_GUI_OPT_NOSTYLEBORDER = (1ULL << 1),
GS_GUI_OPT_NOINTERACT = (1ULL << 2),
GS_GUI_OPT_NOFRAME = (1ULL << 3),
GS_GUI_OPT_NORESIZE = (1ULL << 4),
GS_GUI_OPT_NOSCROLL = (1ULL << 5),
GS_GUI_OPT_NOCLOSE = (1ULL << 6),
GS_GUI_OPT_NOTITLE = (1ULL << 7),
GS_GUI_OPT_HOLDFOCUS = (1ULL << 8),
GS_GUI_OPT_AUTOSIZE = (1ULL << 9),
GS_GUI_OPT_POPUP = (1ULL << 10),
GS_GUI_OPT_CLOSED = (1ULL << 11),
GS_GUI_OPT_EXPANDED = (1ULL << 12),
GS_GUI_OPT_NOHOVER = (1ULL << 13),
GS_GUI_OPT_FORCESETRECT = (1ULL << 14),
GS_GUI_OPT_NOFOCUS = (1ULL << 15),
GS_GUI_OPT_FORCEFOCUS = (1ULL << 16),
GS_GUI_OPT_NOMOVE = (1ULL << 17),
GS_GUI_OPT_NOCLIP = (1ULL << 18),
GS_GUI_OPT_NODOCK = (1ULL << 19),
GS_GUI_OPT_FULLSCREEN = (1ULL << 20),
GS_GUI_OPT_DOCKSPACE = (1ULL << 21),
GS_GUI_OPT_NOBRINGTOFRONT = (1ULL << 22),
GS_GUI_OPT_LEFTCLICKONLY = (1ULL << 23),
GS_GUI_OPT_NOSWITCHSTATE = (1ULL << 24),
GS_GUI_OPT_NOBORDER = (1ULL << 25),
GS_GUI_OPT_ISCONTENT = (1ULL << 26),
GS_GUI_OPT_NOCARET = (1ULL << 27),
GS_GUI_OPT_NOSCROLLHORIZONTAL = (1ULL << 28),
GS_GUI_OPT_NOSCROLLVERTICAL = (1ULL << 29),
GS_GUI_OPT_NOSTYLEBACKGROUND = (1ULL << 30),
GS_GUI_OPT_PARSEIDTAGONLY = (1ULL << 31),
};

enum {
Expand Down Expand Up @@ -325,7 +326,7 @@ typedef union
struct gs_gui_context_t;

typedef void (* gs_gui_on_draw_button_callback)(struct gs_gui_context_t* ctx, gs_gui_rect_t rect,
gs_gui_id id, bool hovered, bool focused, int32_t opt, const char* label, int32_t icon);
gs_gui_id id, bool hovered, bool focused, uint64_t opt, const char* label, int32_t icon);

typedef enum {
GS_GUI_LAYOUT_ANCHOR_TOPLEFT = 0x00,
Expand Down Expand Up @@ -994,7 +995,7 @@ GS_API_DECL void gs_gui_render(gs_gui_context_t* ctx, gs_command_buffer_t* cb);
//=== Util ===//
GS_API_DECL void gs_gui_renderpass_submit(gs_gui_context_t* ctx, gs_command_buffer_t* cb, gs_color_t clear);
GS_API_DECL void gs_gui_renderpass_submit_ex(gs_gui_context_t* ctx, gs_command_buffer_t* cb, gs_graphics_clear_action_t* action);
GS_API_DECL void gs_gui_parse_id_tag(gs_gui_context_t* ctx, const char* str, char* buffer, size_t sz);
GS_API_DECL void gs_gui_parse_id_tag(gs_gui_context_t* ctx, const char* str, char* buffer, size_t sz, uint64_t opt);
GS_API_DECL void gs_gui_parse_label_tag(gs_gui_context_t* ctx, const char* str, char* buffer, size_t sz);

//=== Main API ===//
Expand Down Expand Up @@ -1042,6 +1043,8 @@ GS_API_DECL void gs_gui_style_sheet_destroy(gs_gui_style_sheet_t* ss);
GS_API_DECL void gs_gui_set_element_style(gs_gui_context_t* ctx, gs_gui_element_type element, gs_gui_element_state state, gs_gui_style_element_t* style, size_t size);
GS_API_DECL void gs_gui_style_sheet_set_element_styles(gs_gui_style_sheet_t* style_sheet, gs_gui_element_type element, gs_gui_element_state state, gs_gui_style_element_t* styles, size_t size);
GS_API_DECL void gs_gui_set_style_sheet(gs_gui_context_t* ctx, gs_gui_style_sheet_t* style_sheet);
GS_API_DECL void gs_gui_push_inline_style(gs_gui_context_t* ctx, gs_gui_element_type elementid, gs_gui_inline_style_desc_t* desc);
GS_API_DECL void gs_gui_pop_inline_style(gs_gui_context_t* ctx, gs_gui_element_type elementid);

//=== Resource Loading ===//

Expand Down Expand Up @@ -2153,7 +2156,7 @@ static gs_gui_style_t* gs_gui_push_style(gs_gui_context_t* ctx, gs_gui_style_t*
return save;
}

static void gs_gui_push_inline_style(gs_gui_context_t* ctx, gs_gui_element_type elementid, gs_gui_inline_style_desc_t* desc)
GS_API_DECL void gs_gui_push_inline_style(gs_gui_context_t* ctx, gs_gui_element_type elementid, gs_gui_inline_style_desc_t* desc)
{
if (elementid >= GS_GUI_ELEMENT_COUNT || !desc)
{
Expand Down Expand Up @@ -2242,7 +2245,7 @@ static void gs_gui_push_inline_style(gs_gui_context_t* ctx, gs_gui_element_type
gs_dyn_array_push(iss->animation_counts, anim_ct[2]);
}

static void gs_gui_pop_inline_style(gs_gui_context_t* ctx, gs_gui_element_type elementid)
GS_API_DECL void gs_gui_pop_inline_style(gs_gui_context_t* ctx, gs_gui_element_type elementid)
{
if (elementid >= GS_GUI_ELEMENT_COUNT)
{
Expand Down Expand Up @@ -4111,15 +4114,15 @@ gs_gui_begin(gs_gui_context_t* ctx, const gs_gui_hints_t* hints)
r.y -= 10.f;
r.h += 20.f;
gs_snprintfc(TMP, 256, "!dockspace%zu", (size_t)split);
int32_t opt = GS_GUI_OPT_NOFRAME |
GS_GUI_OPT_FORCESETRECT |
GS_GUI_OPT_NOMOVE |
GS_GUI_OPT_NOTITLE |
GS_GUI_OPT_NOSCROLL |
GS_GUI_OPT_NOCLIP |
GS_GUI_OPT_NODOCK |
GS_GUI_OPT_DOCKSPACE |
GS_GUI_OPT_NOBORDER;
uint64_t opt = GS_GUI_OPT_NOFRAME |
GS_GUI_OPT_FORCESETRECT |
GS_GUI_OPT_NOMOVE |
GS_GUI_OPT_NOTITLE |
GS_GUI_OPT_NOSCROLL |
GS_GUI_OPT_NOCLIP |
GS_GUI_OPT_NODOCK |
GS_GUI_OPT_DOCKSPACE |
GS_GUI_OPT_NOBORDER;
gs_gui_window_begin_ex(ctx, TMP, r, NULL, opt);
{
// Set zindex for sorting (always below the bottom most window in this split tree)
Expand Down Expand Up @@ -6311,7 +6314,7 @@ gs_gui_label_ex(gs_gui_context_t* ctx, const char* label, const gs_gui_selector_

char id_tag[256] = gs_default_val();
char label_tag[256] = gs_default_val();
gs_gui_parse_id_tag(ctx, label, id_tag, sizeof(id_tag));
gs_gui_parse_id_tag(ctx, label, id_tag, sizeof(id_tag), opt);
gs_gui_parse_label_tag(ctx, label, label_tag, sizeof(label_tag));

if (id_tag) gs_gui_push_id(ctx, id_tag, strlen(id_tag));
Expand Down Expand Up @@ -6471,14 +6474,15 @@ gs_gui_parse_label_tag(gs_gui_context_t* ctx, const char* str, char* buffer, siz
memcpy(buffer, str, gs_min(sz, strlen(str) + 1));
}

GS_API_DECL void gs_gui_parse_id_tag(gs_gui_context_t* ctx, const char* str, char* buffer, size_t sz)
GS_API_DECL void gs_gui_parse_id_tag(gs_gui_context_t* ctx, const char* str, char* buffer, size_t sz, uint64_t opt)
{
gs_lexer_t lex = gs_lexer_c_ctor(str);
while (gs_lexer_can_lex(&lex))
{
gs_token_t token = gs_lexer_next_token(&lex);
switch (token.type)
if (opt & GS_GUI_OPT_PARSEIDTAGONLY) {
gs_lexer_t lex = gs_lexer_c_ctor(str);
while (gs_lexer_can_lex(&lex))
{
gs_token_t token = gs_lexer_next_token(&lex);
switch (token.type)
{
case GS_TOKEN_HASH:
{
if (gs_lexer_peek(&lex).type == GS_TOKEN_HASH)
Expand All @@ -6493,8 +6497,14 @@ GS_API_DECL void gs_gui_parse_id_tag(gs_gui_context_t* ctx, const char* str, cha
return;
}
} break;
}
}
}
} else {
size_t str_sz = strlen(str);
size_t actual_sz = gs_min(str_sz, sz-1);
memcpy(buffer, str, actual_sz);
buffer[actual_sz] = 0;
}
}

GS_API_DECL int32_t
Expand All @@ -6508,7 +6518,7 @@ gs_gui_button_ex(gs_gui_context_t* ctx, const char* label, const gs_gui_selector

char id_tag[256] = gs_default_val();
char label_tag[256] = gs_default_val();
gs_gui_parse_id_tag(ctx, label, id_tag, sizeof(id_tag));
gs_gui_parse_id_tag(ctx, label, id_tag, sizeof(id_tag), opt);
gs_gui_parse_label_tag(ctx, label, label_tag, sizeof(label_tag));

gs_gui_style_t style = gs_default_val();
Expand Down Expand Up @@ -6598,7 +6608,7 @@ GS_API_DECL int32_t gs_gui_checkbox_ex(gs_gui_context_t* ctx, const char* label,
return res;
}

GS_API_DECL int32_t gs_gui_textbox_raw(gs_gui_context_t* ctx, char* buf, int32_t bufsz, gs_gui_id id, gs_gui_rect_t rect,
GS_API_DECL int32_t gs_gui_textbox_raw(gs_gui_context_t* ctx, char* buf, int32_t bufsz, gs_gui_id id, gs_gui_rect_t rect,
const gs_gui_selector_desc_t* desc, uint64_t opt)
{
int32_t res = 0;
Expand Down Expand Up @@ -6641,14 +6651,37 @@ GS_API_DECL int32_t gs_gui_textbox_raw(gs_gui_context_t* ctx, char* buf, int32_t
res |= GS_GUI_RES_CHANGE;
}

/* handle backspace */
if (ctx->key_pressed & GS_GUI_KEY_BACKSPACE && len > 0)
/* handle backspace */
if (ctx->key_pressed & GS_GUI_KEY_BACKSPACE && len > 0)
{
/* skip utf-8 continuation bytes */
while ((buf[--len] & 0xc0) == 0x80 && len > 0);
buf[len] = '\0';
res |= GS_GUI_RES_CHANGE;
}
if (ctx->key_down & GS_GUI_KEY_CTRL) {
for (--len; len > 0 ;len--) {
/* skip utf-8 continuation bytes */
if ((buf[len-1] & 0xc0) == 0x80) continue;
/* seek until seperator character */
if (strchr(" ()[]{},.-+*=/\\^~|\"'&%#@!<>;:", buf[len-1])) break;
}
} else {
/* skip utf-8 continuation bytes */
while ((buf[--len] & 0xc0) == 0x80 && len > 0);
}
buf[len] = '\0';
res |= GS_GUI_RES_CHANGE;
}

/* handle paste */
if (gs_platform_key_pressed(GS_KEYCODE_V) && ctx->key_down & GS_GUI_KEY_CTRL)
{
const char* clipboard = gs_platform_window_get_clipboard(ctx->window_hndl);
printf("%s --\n", clipboard);
int32_t n = gs_min(bufsz - len - 1, (int32_t) strlen(clipboard));
if (n > 0) {
memcpy(buf + len, clipboard, n);
len += n;
buf[len] = '\0';
res |= GS_GUI_RES_CHANGE;
}
}

/* handle return */
if (ctx->key_pressed & GS_GUI_KEY_RETURN)
Expand Down Expand Up @@ -6955,16 +6988,17 @@ static int32_t _gs_gui_header(gs_gui_context_t *ctx, const char *label, int32_t
{
gs_gui_rect_t r;
int32_t active, expanded;
gs_gui_id id = gs_gui_get_id(ctx, label, strlen(label));
int32_t idx = gs_gui_pool_get(ctx, ctx->treenode_pool, GS_GUI_TREENODEPOOL_SIZE, id);
int32_t width = -1;
gs_gui_layout_row(ctx, 1, &width, 0);

char id_tag[256] = gs_default_val();
char label_tag[256] = gs_default_val();
gs_gui_parse_id_tag(ctx, label, id_tag, sizeof(id_tag));
gs_gui_parse_id_tag(ctx, label, id_tag, sizeof(id_tag), opt);
gs_gui_parse_label_tag(ctx, label, label_tag, sizeof(label_tag));

gs_gui_id id = gs_gui_get_id(ctx, id_tag, strlen(id_tag));
int32_t idx = gs_gui_pool_get(ctx, ctx->treenode_pool, GS_GUI_TREENODEPOOL_SIZE, id);

if (id_tag) gs_gui_push_id(ctx, id_tag, strlen(id_tag));

active = (idx >= 0);
Expand Down Expand Up @@ -7094,7 +7128,7 @@ gs_gui_window_begin_ex(gs_gui_context_t * ctx, const char* title, gs_gui_rect_t

char id_tag[256] = gs_default_val();
char label_tag[256] = gs_default_val();
gs_gui_parse_id_tag(ctx, title, id_tag, sizeof(id_tag));
gs_gui_parse_id_tag(ctx, title, id_tag, sizeof(id_tag), opt);
gs_gui_parse_label_tag(ctx, title, label_tag, sizeof(label_tag));

gs_gui_id id = 0x00;
Expand Down Expand Up @@ -7449,11 +7483,15 @@ gs_gui_window_begin_ex(gs_gui_context_t * ctx, const char* title, gs_gui_rect_t
gs_gui_id id = gs_gui_get_id(ctx, "!body", 5);
// gs_gui_update_control(ctx, id, br, (opt | GS_GUI_OPT_NOSWITCHSTATE));

// Need to move the entire thing
if (ctx->hover_root == cnt && !ctx->focus_split && !ctx->focus && !ctx->lock_focus && !ctx->hover && ctx->mouse_down == GS_GUI_MOUSE_LEFT)
if (ctx->hover_root == cnt && !ctx->focus_split && !ctx->lock_focus && ctx->mouse_down == GS_GUI_MOUSE_LEFT)
{
ctx->active_root = cnt;
ctx->next_focus_root = cnt;
ctx->next_focus_root = cnt;
}
// Need to move the entire thing

if (ctx->hover_root == cnt && !ctx->focus_split && !ctx->focus && !ctx->lock_focus && !ctx->hover && ctx->mouse_down == GS_GUI_MOUSE_LEFT)
{
if (root_split)
{
gs_gui_request_t req = gs_default_val();
Expand Down Expand Up @@ -8115,7 +8153,23 @@ gs_gui_window_end(gs_gui_context_t *ctx)
{
r->x = gs_min(r->x + ctx->mouse_delta.x, max_x);
}
});
});

// move instead of resize?
_gui_window_resize_ctrl(
gs_gui_get_id(ctx, "!res_c", 6),
gs_gui_rect(r->x + r->w * border_ratio,
r->y + r->h * border_ratio,
r->w * border_ratio,
r->h * border_ratio),
GS_GUI_MOUSE_LEFT,
GS_GUI_SPLIT_MOVE,
GS_GUI_KEY_ALT,
{
ctx->next_focus_root = cnt;
r->x += ctx->mouse_delta.x;
r->y += ctx->mouse_delta.y;
});

static bool capture = false;
static gs_vec2 mp = {0};
Expand Down Expand Up @@ -8226,7 +8280,7 @@ GS_API_DECL void
gs_gui_popup_open(gs_gui_context_t* ctx, const char* name)
{
char id_tag[256] = gs_default_val();
gs_gui_parse_id_tag(ctx, name, id_tag, sizeof(id_tag));
gs_gui_parse_id_tag(ctx, name, id_tag, sizeof(id_tag), 0);

gs_gui_container_t *cnt = gs_gui_get_container(ctx, *id_tag ? id_tag : name);

Expand Down Expand Up @@ -8259,7 +8313,7 @@ gs_gui_panel_begin_ex(gs_gui_context_t* ctx, const char* name, const gs_gui_sele
gs_gui_container_t *cnt;
const int32_t elementid = GS_GUI_ELEMENT_PANEL;
char id_tag[256] = gs_default_val();
gs_gui_parse_id_tag(ctx, name, id_tag, sizeof(id_tag));
gs_gui_parse_id_tag(ctx, name, id_tag, sizeof(id_tag), opt);

// if (id_tag) gs_gui_push_id(ctx, id_tag, strlen(id_tag));
// else gs_gui_push_id(ctx, name, strlen(name));
Expand Down

0 comments on commit 7c9b32c

Please sign in to comment.