major update font handling update
This commit is contained in:
parent
3ec9a0de9f
commit
be8c85f184
35
Makefile
35
Makefile
@ -1,35 +0,0 @@
|
||||
# Install
|
||||
BIN = opengl
|
||||
|
||||
# Compiler
|
||||
CC = gcc
|
||||
DCC = clang
|
||||
|
||||
# Flags
|
||||
CFLAGS = -std=c89 -pedantic -Wdeprecated-declarations
|
||||
CFLAGS += -g -Wall -Wextra -Wformat-security -Wunreachable-code
|
||||
CFLAGS += -fstack-protector-strong -Winline -Wshadow -Wwrite-strings -fstrict-aliasing
|
||||
CFLAGS += -Wstrict-prototypes -Wold-style-definition -Wconversion -Wfloat-equal
|
||||
CFLAGS += -Wredundant-decls -Wnested-externs -Wmissing-include-dirs
|
||||
CFLAGS += -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wmissing-prototypes -Wconversion
|
||||
CFLAGS += -Wswitch-default -Wundef -Wno-unused -Wstrict-overflow=5 -Wsign-conversion
|
||||
CFLAGS += -Winit-self -Wstrict-aliasing -fsanitize=address -fsanitize=undefined -ftrapv
|
||||
CFLAGS += -Wswitch-enum -Winvalid-pch -Wbad-function-cast
|
||||
|
||||
SRC = gui.c demo/opengl.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
# Modes
|
||||
.PHONY: gcc
|
||||
gcc: CC = gcc
|
||||
gcc: $(BIN)
|
||||
|
||||
.PHONY: clang
|
||||
clang: CC = clang
|
||||
clang: $(BIN)
|
||||
|
||||
$(BIN):
|
||||
@mkdir -p bin
|
||||
rm -f bin/$(BIN) $(OBJS)
|
||||
$(CC) $(SRC) $(CFLAGS) -o bin/$(BIN) -lSDL2 -lGL -lGLU
|
||||
|
132
demo/example.c
132
demo/example.c
@ -40,136 +40,14 @@ ldfile(const char* path, size_t* siz)
|
||||
return buf;
|
||||
}
|
||||
|
||||
static GLuint
|
||||
ldbmp(gui_byte *data, uint32_t *width, uint32_t *height)
|
||||
{
|
||||
/* texture */
|
||||
GLuint texture;
|
||||
gui_byte *header;
|
||||
gui_byte *target;
|
||||
gui_byte *writer;
|
||||
gui_byte *reader;
|
||||
uint32_t ioff;
|
||||
uint32_t j;
|
||||
int32_t i;
|
||||
|
||||
header = data;
|
||||
memcpy(width, &header[0x12], sizeof(uint32_t));
|
||||
memcpy(height, &header[0x16], sizeof(uint32_t));
|
||||
memcpy(&ioff, &header[0x0A], sizeof(uint32_t));
|
||||
|
||||
data = data + ioff;
|
||||
reader = data;
|
||||
target = calloc(*width * *height * 4, 1);
|
||||
for (i = (int32_t)*height-1; i >= 0; i--) {
|
||||
writer = target + (i * (int32_t)*width * 4);
|
||||
for (j = 0; j < *width; j++) {
|
||||
*writer++ = *(reader + (j * 4) + 1);
|
||||
*writer++ = *(reader + (j * 4) + 2);
|
||||
*writer++ = *(reader + (j * 4) + 3);
|
||||
*writer++ = *(reader + (j * 4) + 0);
|
||||
}
|
||||
reader += *width * 4;
|
||||
}
|
||||
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)*width, (GLsizei)*height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, target);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
free(target);
|
||||
return texture;
|
||||
}
|
||||
|
||||
static struct gui_font*
|
||||
ldfont(const char *name, unsigned char height)
|
||||
{
|
||||
size_t mem;
|
||||
size_t size;
|
||||
size_t i = 0;
|
||||
gui_byte *iter;
|
||||
gui_texture tex;
|
||||
short max_height = 0;
|
||||
struct gui_font *font;
|
||||
uint32_t img_width, img_height;
|
||||
struct gui_font_glyph *glyphes;
|
||||
|
||||
uint16_t num;
|
||||
uint16_t indexes;
|
||||
uint16_t tex_width;
|
||||
uint16_t tex_height;
|
||||
|
||||
gui_byte *buffer = (gui_byte*)ldfile(name, &size);
|
||||
memcpy(&num, buffer, sizeof(uint16_t));
|
||||
memcpy(&indexes, &buffer[0x02], sizeof(uint16_t));
|
||||
memcpy(&tex_width, &buffer[0x04], sizeof(uint16_t));
|
||||
memcpy(&tex_height, &buffer[0x06], sizeof(uint16_t));
|
||||
|
||||
iter = &buffer[0x08];
|
||||
mem = sizeof(struct gui_font_glyph) * ((size_t)indexes + 1);
|
||||
glyphes = calloc(mem, 1);
|
||||
for(i = 0; i < num; ++i) {
|
||||
uint16_t id, x, y, w, h;
|
||||
float xoff, yoff, xadv;
|
||||
|
||||
memcpy(&id, iter, sizeof(uint16_t));
|
||||
memcpy(&x, &iter[0x02], sizeof(uint16_t));
|
||||
memcpy(&y, &iter[0x04], sizeof(uint16_t));
|
||||
memcpy(&w, &iter[0x06], sizeof(uint16_t));
|
||||
memcpy(&h, &iter[0x08], sizeof(uint16_t));
|
||||
memcpy(&xoff, &iter[10], sizeof(float));
|
||||
memcpy(&yoff, &iter[14], sizeof(float));
|
||||
memcpy(&xadv, &iter[18], sizeof(float));
|
||||
|
||||
glyphes[id].code = id;
|
||||
glyphes[id].width = (short)w;
|
||||
glyphes[id].height = (short)h;
|
||||
glyphes[id].xoff = xoff;
|
||||
glyphes[id].yoff = yoff;
|
||||
glyphes[id].xadvance = xadv;
|
||||
glyphes[id].uv[0].u = (float)x/(float)tex_width;
|
||||
glyphes[id].uv[0].v = (float)y/(float)tex_height;
|
||||
glyphes[id].uv[1].u = (float)(x+w)/(float)tex_width;
|
||||
glyphes[id].uv[1].v = (float)(y+h)/(float)tex_height;
|
||||
if (glyphes[id].height > max_height) max_height = glyphes[id].height;
|
||||
iter += 22;
|
||||
}
|
||||
|
||||
font = calloc(sizeof(struct gui_font), 1);
|
||||
font->height = height;
|
||||
font->scale = (float)height/(float)max_height;
|
||||
font->texture.gl = ldbmp(iter, &img_width, &img_height);
|
||||
font->tex_size.x = tex_width;
|
||||
font->tex_size.y = tex_height;
|
||||
font->fallback = &glyphes['?'];
|
||||
font->glyphes = glyphes;
|
||||
font->glyph_count = indexes + 1;
|
||||
free(buffer);
|
||||
return font;
|
||||
}
|
||||
|
||||
static void
|
||||
delfont(struct gui_font *font)
|
||||
{
|
||||
if (!font) return;
|
||||
if (font->glyphes)
|
||||
free(font->glyphes);
|
||||
free(font);
|
||||
}
|
||||
|
||||
static gui_int
|
||||
message_panel(struct gui_context *ctx, struct gui_panel *panel)
|
||||
{
|
||||
gui_int ret = -1;
|
||||
gui_begin_panel(ctx, panel, "Error", GUI_PANEL_MOVEABLE|GUI_PANEL_BORDER);
|
||||
gui_panel_layout(panel, 30, 2);
|
||||
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_SWITCH)) ret = 1;
|
||||
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_SWITCH)) ret = 0;
|
||||
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_DEFAULT)) ret = 1;
|
||||
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_DEFAULT)) ret = 0;
|
||||
if (ret != -1) gui_panel_hide(panel);
|
||||
gui_end_panel(ctx, panel, NULL);
|
||||
return ret;
|
||||
@ -197,8 +75,8 @@ color_picker_panel(struct gui_context *ctx, struct gui_panel *panel, struct colo
|
||||
|
||||
gui_panel_layout(panel, 30, 4);
|
||||
gui_panel_seperator(panel, 1);
|
||||
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_SWITCH)) ret = 1;
|
||||
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_SWITCH)) ret = 0;
|
||||
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_DEFAULT)) ret = 1;
|
||||
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_DEFAULT)) ret = 0;
|
||||
if (ret != -1) gui_panel_hide(panel);
|
||||
gui_end_panel(ctx, panel, NULL);
|
||||
return ret;
|
||||
@ -241,7 +119,7 @@ demo_panel(struct gui_context *ctx, struct gui_panel *panel, struct demo *demo)
|
||||
/* Group */
|
||||
gui_panel_group_begin(panel, &demo->group, "Options");
|
||||
gui_panel_layout(&demo->group, 30, 1);
|
||||
if (gui_panel_button_text(&demo->group, "button", GUI_BUTTON_SWITCH))
|
||||
if (gui_panel_button_text(&demo->group, "button", GUI_BUTTON_DEFAULT))
|
||||
fprintf(stdout, "button pressed!\n");
|
||||
demo->check = gui_panel_check(&demo->group, "advanced", demo->check);
|
||||
demo->slider = gui_panel_slider(&demo->group, 0, demo->slider, 10, 1.0f, GUI_HORIZONTAL);
|
||||
|
BIN
demo/mono.sdf
BIN
demo/mono.sdf
Binary file not shown.
145
demo/opengl.c
145
demo/opengl.c
@ -25,13 +25,58 @@
|
||||
|
||||
#include "example.c"
|
||||
|
||||
/* functions */
|
||||
static void kpress(struct gui_input*, SDL_Event*);
|
||||
static void bpress(struct gui_input*, SDL_Event*);
|
||||
static void brelease(struct gui_input*, SDL_Event*);
|
||||
static void bmotion(struct gui_input*, SDL_Event*);
|
||||
static void
|
||||
delfont(struct gui_font *font)
|
||||
{
|
||||
free(font->glyphes);
|
||||
free(font);
|
||||
}
|
||||
|
||||
static struct gui_font*
|
||||
mkfont(const char *path, gui_uint font_height, gui_uint bake_height,
|
||||
gui_size range, enum gui_font_atlas_dimension dim)
|
||||
{
|
||||
gui_byte *ttf_blob;
|
||||
gui_size ttf_blob_size;
|
||||
struct gui_font_atlas atlas;
|
||||
struct gui_font *font = calloc(sizeof(struct gui_font), 1);
|
||||
|
||||
atlas.dim = dim;
|
||||
atlas.range = range;
|
||||
atlas.size = gui_font_atlas_alloc_size(atlas.dim);
|
||||
atlas.memory = calloc((gui_size)atlas.size, 1);
|
||||
|
||||
memset(font, 0, sizeof(*font));
|
||||
font->glyph_count = (gui_uint)atlas.range;
|
||||
font->glyphes = calloc(atlas.range, sizeof(struct gui_font_glyph));
|
||||
font->fallback = &font->glyphes['?'];
|
||||
font->scale = (gui_float)font_height / (gui_float)bake_height;
|
||||
font->height = (gui_float)font_height;
|
||||
|
||||
ttf_blob = (gui_byte*)ldfile(path, &ttf_blob_size);
|
||||
if (!gui_font_load(font, &atlas, bake_height, ttf_blob, ttf_blob_size))
|
||||
goto failed;
|
||||
|
||||
glGenTextures(1, &font->texture.gl);
|
||||
glBindTexture(GL_TEXTURE_2D, font->texture.gl);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dim, dim, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, atlas.memory);
|
||||
|
||||
free(atlas.memory);
|
||||
free(ttf_blob);
|
||||
return font;
|
||||
|
||||
failed:
|
||||
free(atlas.memory);
|
||||
free(ttf_blob);
|
||||
delfont(font);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* gobals */
|
||||
static void
|
||||
key(struct gui_input *in, SDL_Event* e, gui_bool down)
|
||||
{
|
||||
@ -143,6 +188,52 @@ draw(int width, int height, struct gui_draw_call_list **list, gui_size count)
|
||||
glPopAttrib();
|
||||
}
|
||||
|
||||
static void
|
||||
draw_font(struct gui_font *font, int width, int height)
|
||||
{
|
||||
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDisable(GL_CULL_FACE);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glEnableClientState(GL_COLOR_ARRAY);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glBindTexture(GL_TEXTURE_2D, font->texture.gl);
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(0,0);
|
||||
glVertex2f(0, 0);
|
||||
glTexCoord2f(0,1);
|
||||
glVertex2f(0, 250);
|
||||
glTexCoord2f(1,1);
|
||||
glVertex2f(250, 250);
|
||||
glTexCoord2f(1,0);
|
||||
glVertex2f(250, 0);
|
||||
glEnd();
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
glDisableClientState(GL_COLOR_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glPopMatrix();
|
||||
glPopAttrib();
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
@ -151,37 +242,29 @@ main(int argc, char *argv[])
|
||||
gui_bool running;
|
||||
SDL_Window *win;
|
||||
SDL_GLContext glContext;
|
||||
struct color_picker picker;
|
||||
struct demo demo;
|
||||
|
||||
struct gui_memory memory;
|
||||
struct gui_config config;
|
||||
struct gui_font *font;
|
||||
struct gui_input input;
|
||||
struct gui_output output;
|
||||
struct gui_font *font;
|
||||
struct gui_context *ctx;
|
||||
struct gui_panel *panel;
|
||||
struct gui_panel *message;
|
||||
struct gui_panel *color_panel;
|
||||
struct demo demo;
|
||||
struct color_picker picker;
|
||||
|
||||
/* Window */
|
||||
UNUSED(argc); UNUSED(argv);
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
win = SDL_CreateWindow("clone",
|
||||
0, 0, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN);
|
||||
win = SDL_CreateWindow("clone", 0, 0,
|
||||
WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN);
|
||||
glContext = SDL_GL_CreateContext(win);
|
||||
glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);
|
||||
|
||||
/* GUI */
|
||||
memset(&input, 0, sizeof(input));
|
||||
memory.max_panels = 8;
|
||||
memory.max_depth = 4;
|
||||
memory.memory = calloc(MAX_MEMORY , 1);
|
||||
memory.size = MAX_MEMORY;
|
||||
memory.vertex_percentage = 0.80f;
|
||||
memory.command_percentage = 0.19f;
|
||||
|
||||
memset(&demo, 0, sizeof(demo));
|
||||
memset(&picker, 0, sizeof(picker));
|
||||
demo.tab.minimized = gui_true;
|
||||
@ -189,16 +272,22 @@ main(int argc, char *argv[])
|
||||
demo.slider = 2.0f;
|
||||
demo.prog = 60;
|
||||
|
||||
font = ldfont("mono.sdf", 16);
|
||||
/* Context */
|
||||
memset(&input, 0, sizeof(input));
|
||||
memory.max_panels = 8;
|
||||
memory.max_depth = 4;
|
||||
memory.memory = calloc(MAX_MEMORY , 1);
|
||||
memory.size = MAX_MEMORY;
|
||||
memory.vertex_percentage = 0.80f;
|
||||
memory.command_percentage = 0.19f;
|
||||
ctx = gui_new(&memory, &input);
|
||||
|
||||
/* Panel */
|
||||
gui_default_config(&config);
|
||||
config.colors[GUI_COLOR_TEXT].r = 255;
|
||||
config.colors[GUI_COLOR_TEXT].g = 255;
|
||||
config.colors[GUI_COLOR_TEXT].b = 255;
|
||||
config.colors[GUI_COLOR_TEXT].a = 255;
|
||||
font = mkfont("mono.ttf", 14, 16, 255, GUI_ATLAS_DIM_256);
|
||||
panel = gui_panel_new(ctx, 50, 50, 500, 320, &config, font);
|
||||
message = gui_panel_new(ctx, 150, 150, 200, 100, &config, font);
|
||||
color_panel = gui_panel_new(ctx, 250, 250, 400, 250, &config, font);
|
||||
color_panel = gui_panel_new(ctx, 250, 250, 400, 260, &config, font);
|
||||
|
||||
running = gui_true;
|
||||
while (running) {
|
||||
@ -213,7 +302,7 @@ main(int argc, char *argv[])
|
||||
else if (ev.type == SDL_KEYDOWN) key( &input, &ev, gui_true);
|
||||
else if (ev.type == SDL_KEYUP) key(&input, &ev, gui_false);
|
||||
else if (ev.type == SDL_TEXTINPUT) text(&input, &ev);
|
||||
else if (ev.type == SDL_QUIT) running = gui_false;
|
||||
else if (ev.type == SDL_QUIT) goto cleanup;
|
||||
}
|
||||
gui_input_end(&input);
|
||||
SDL_GetWindowSize(win, &width, &height);
|
||||
@ -222,7 +311,7 @@ main(int argc, char *argv[])
|
||||
gui_begin(ctx, (gui_float)width, (gui_float)height);
|
||||
running = demo_panel(ctx, panel, &demo);
|
||||
if (message_panel(ctx, message) >= 0)
|
||||
fprintf(stdout, "message closed\n");
|
||||
running = gui_false;
|
||||
if (color_picker_panel(ctx, color_panel, &picker) >= 0) {
|
||||
struct gui_color c = picker.color;
|
||||
fprintf(stdout, "color picked: {%u, %u, %u, %u}\n", c.r, c.g, c.b, c.a);
|
||||
@ -234,6 +323,7 @@ main(int argc, char *argv[])
|
||||
glClearColor(120.0f/255.0f, 120.0f/255.0f, 120.0f/255.0f, 120.0f/255.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
draw(width, height, output.list, output.list_size);
|
||||
/*draw_font(font, width, height);*/
|
||||
SDL_GL_SwapWindow(win);
|
||||
|
||||
/* Timing */
|
||||
@ -243,6 +333,7 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
/* Cleanup */
|
||||
cleanup:
|
||||
free(memory.memory);
|
||||
delfont(font);
|
||||
SDL_GL_DeleteContext(glContext);
|
||||
|
285
gui.c
285
gui.c
@ -67,7 +67,7 @@ static gui_float
|
||||
fsqrt(float x)
|
||||
{
|
||||
float xhalf = 0.5f*x;
|
||||
union {float f; long i;} val;
|
||||
union {gui_float f; gui_int i;} val;
|
||||
val.i = 0;
|
||||
val.f = ABS(x);
|
||||
val.i = 0x5f375a86 - (val.i >> 1);
|
||||
@ -115,16 +115,19 @@ itos(char *buffer, gui_int num)
|
||||
char *p = buffer;
|
||||
if (!buffer)
|
||||
return 0;
|
||||
|
||||
if (num < 0) {
|
||||
num = ABS(num);
|
||||
*p++ = '-';
|
||||
}
|
||||
shifter = num;
|
||||
|
||||
do {
|
||||
++p;
|
||||
shifter = shifter/10;
|
||||
} while (shifter);
|
||||
*p = '\0';
|
||||
|
||||
len = (gui_size)(p - buffer);
|
||||
do {
|
||||
*--p = digit[num % 10];
|
||||
@ -221,6 +224,7 @@ gui_triangle_from_direction(struct gui_vec2 *result, gui_float x, gui_float y,
|
||||
{
|
||||
gui_float w_half, h_half;
|
||||
assert(result);
|
||||
|
||||
w = MAX(4 * pad_x, w);
|
||||
h = MAX(4 * pad_y, h);
|
||||
w = w - 2 * pad_x;
|
||||
@ -229,6 +233,7 @@ gui_triangle_from_direction(struct gui_vec2 *result, gui_float x, gui_float y,
|
||||
h_half = h / 2.0f;
|
||||
x = x + pad_x;
|
||||
y = y + pad_y;
|
||||
|
||||
if (direction == GUI_UP) {
|
||||
result[0].x = x + w_half;
|
||||
result[0].y = y;
|
||||
@ -325,6 +330,144 @@ gui_input_end(struct gui_input *in)
|
||||
vec2_sub(in->mouse_delta, in->mouse_pos, in->mouse_prev);
|
||||
}
|
||||
|
||||
#ifdef GUI_USE_FREETYPE_FONT
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include FT_GLYPH_H
|
||||
|
||||
static void
|
||||
gui_font_load_glyph(gui_uint code, struct gui_font_glyph *glyph, FT_GlyphSlot slot)
|
||||
{
|
||||
glyph->code = code;
|
||||
glyph->width = (gui_short)slot->bitmap.width;
|
||||
glyph->height = (gui_short)slot->bitmap.rows;
|
||||
glyph->xoff = (gui_float)slot->bitmap_left;
|
||||
glyph->yoff = (gui_float)slot->bitmap_top;
|
||||
glyph->xadvance = (gui_float)(slot->advance.x >> 6);
|
||||
}
|
||||
|
||||
static void
|
||||
gui_font_load_glyphes(FT_Face face, struct gui_font *font, gui_size range)
|
||||
{
|
||||
gui_size i;
|
||||
gui_int ft_err;
|
||||
for (i = 0; i < range; ++i) {
|
||||
gui_uint index = FT_Get_Char_Index(face, i);
|
||||
if (!index) continue;
|
||||
ft_err = FT_Load_Glyph(face, index, 0);
|
||||
if (ft_err) continue;
|
||||
ft_err = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
|
||||
if (ft_err) continue;
|
||||
gui_font_load_glyph(index, &font->glyphes[i], face->glyph);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
gui_font_pack_glyphes(struct gui_font *font, gui_float width, gui_float height, gui_size range)
|
||||
{
|
||||
gui_size i;
|
||||
gui_float xoff = 0, yoff = 0;
|
||||
gui_float max_height = 0.0f;
|
||||
for (i = 0; i < range; ++i) {
|
||||
struct gui_font_glyph *glyph = &font->glyphes[i];
|
||||
if ((xoff + glyph->width) > width) {
|
||||
yoff += max_height;
|
||||
max_height = 0.0f;
|
||||
xoff = 0.0f;
|
||||
}
|
||||
|
||||
glyph->uv[0].u = xoff / width;
|
||||
glyph->uv[0].v = yoff / height;
|
||||
glyph->uv[1].u = (xoff + glyph->width) / width;
|
||||
glyph->uv[1].v = (yoff + glyph->height) / height;
|
||||
if (glyph->height > max_height)
|
||||
max_height = glyph->height;
|
||||
xoff += glyph->width;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
gui_font_atlas_blit(struct gui_font_atlas *atlas, FT_GlyphSlot glyph,
|
||||
gui_size off_x, gui_size off_y)
|
||||
{
|
||||
gui_size y, x;
|
||||
gui_size width = glyph->bitmap.width;
|
||||
gui_size height = glyph->bitmap.rows;
|
||||
const gui_size pitch = atlas->dim * GUI_ATLAS_DEPTH;
|
||||
for (y = 0; y < height; y++) {
|
||||
gui_size x_off = off_x * GUI_ATLAS_DEPTH;
|
||||
gui_size y_off = (off_y + y) * pitch;
|
||||
gui_byte *dst = &atlas->memory[y_off + x_off];
|
||||
for (x = 0; x < width; ++x) {
|
||||
dst[0] = 255;
|
||||
dst[1] = 255;
|
||||
dst[2] = 255;
|
||||
dst[3] = glyph->bitmap.buffer[y * width + x];
|
||||
dst += 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
gui_font_bake_glyphes(FT_Face face, struct gui_font_atlas *atlas,
|
||||
const struct gui_font *font)
|
||||
{
|
||||
gui_size i;
|
||||
gui_int ft_err;
|
||||
for (i = 0; i < atlas->range; ++i) {
|
||||
gui_size x, y;
|
||||
struct gui_font_glyph *glyph = &font->glyphes[i];
|
||||
gui_uint index = FT_Get_Char_Index(face, i);
|
||||
if (!index) continue;
|
||||
ft_err = FT_Load_Glyph(face, index, 0);
|
||||
if (ft_err) continue;
|
||||
ft_err = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
|
||||
if (ft_err) continue;
|
||||
|
||||
x = (gui_size)(glyph->uv[0].u * (gui_float)atlas->dim);
|
||||
y = (gui_size)(glyph->uv[0].v * (gui_float)atlas->dim);
|
||||
gui_font_atlas_blit(atlas, face->glyph, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
gui_bool
|
||||
gui_font_load(struct gui_font *font, struct gui_font_atlas *atlas, gui_uint height,
|
||||
const gui_byte *data, size_t size)
|
||||
{
|
||||
gui_bool ret = gui_true;
|
||||
FT_Library ft_lib;
|
||||
FT_Face ft_face;
|
||||
|
||||
assert(font);
|
||||
assert(atlas);
|
||||
assert(font->glyphes);
|
||||
assert(atlas->memory);
|
||||
|
||||
if (!font || !atlas)
|
||||
return gui_false;
|
||||
if (!font->glyphes || !atlas->memory)
|
||||
return gui_false;
|
||||
if (FT_Init_FreeType(&ft_lib))
|
||||
return gui_false;
|
||||
if (FT_New_Memory_Face(ft_lib, data, (FT_Long)size, 0, &ft_face))
|
||||
goto cleanup;
|
||||
if (FT_Select_Charmap(ft_face, FT_ENCODING_UNICODE))
|
||||
goto failed;
|
||||
if (FT_Set_Char_Size(ft_face, height << 6, height << 6, 96, 96))
|
||||
goto failed;
|
||||
|
||||
gui_font_load_glyphes(ft_face, font, atlas->range);
|
||||
gui_font_pack_glyphes(font, atlas->dim, atlas->dim, atlas->range);
|
||||
gui_font_bake_glyphes(ft_face, atlas, font);
|
||||
|
||||
failed:
|
||||
FT_Done_Face(ft_face);
|
||||
cleanup:
|
||||
FT_Done_FreeType(ft_lib);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
static gui_size
|
||||
gui_font_text_width(const struct gui_font *font, const gui_char *t, gui_size l)
|
||||
{
|
||||
@ -651,6 +794,7 @@ gui_draw_diamondf(struct gui_draw_buffer *buffer, gui_float x, gui_float y,
|
||||
|
||||
wh = w * 0.5f;
|
||||
hh = h * 0.5f;
|
||||
|
||||
gui_push_vertex(buffer, x+wh, y, col, 0.0f, 0.0f);
|
||||
gui_push_vertex(buffer, x+w, y+hh, col, 0.0f, 0.0f);
|
||||
gui_push_vertex(buffer, x+wh, y+h, col, 0.0f, 0.0f);
|
||||
@ -685,27 +829,26 @@ gui_draw_string(struct gui_draw_buffer *buffer, const struct gui_font *font, gui
|
||||
|
||||
text_len = utf_decode(text, &unicode, len);
|
||||
while (text_len <= len) {
|
||||
gui_float x1, y1, x2, y2, char_width = 0;
|
||||
gui_float gx, gy, gw, gh, char_width = 0;
|
||||
if (unicode == UTF_INVALID) break;
|
||||
g = (unicode < font->glyph_count) ?
|
||||
&font->glyphes[unicode] :
|
||||
font->fallback;
|
||||
g = (g->code == 0) ? font->fallback : g;
|
||||
|
||||
y1 = (gui_float)(y + (g->yoff * font->scale));
|
||||
y2 = (gui_float)(y1 + (gui_float)g->height * font->scale);
|
||||
x1 = (gui_float)(x + g->xoff * font->scale);
|
||||
x2 = (gui_float)(x1 + (gui_float)g->width * font->scale);
|
||||
char_width = g->xadvance * font->scale;
|
||||
gw = g->width * font->scale;
|
||||
gh = g->height * font->scale;
|
||||
gx = x + g->xoff * font->scale;
|
||||
gy = y + (font->height - g->yoff * font->scale);
|
||||
|
||||
gui_push_vertex(buffer, x1, y1, col, g->uv[0].u, g->uv[0].v);
|
||||
gui_push_vertex(buffer, x2, y1, col, g->uv[1].u, g->uv[0].v);
|
||||
gui_push_vertex(buffer, x2, y2, col, g->uv[1].u, g->uv[1].v);
|
||||
gui_push_vertex(buffer, x1, y1, col, g->uv[0].u, g->uv[0].v);
|
||||
gui_push_vertex(buffer, x2, y2, col, g->uv[1].u, g->uv[1].v);
|
||||
gui_push_vertex(buffer, x1, y2, col, g->uv[0].u, g->uv[1].v);
|
||||
gui_push_vertex(buffer, gx, gy, col, g->uv[0].u, g->uv[0].v);
|
||||
gui_push_vertex(buffer, gx + gw, gy, col, g->uv[1].u, g->uv[0].v);
|
||||
gui_push_vertex(buffer, gx + gw, gy + gh, col, g->uv[1].u, g->uv[1].v);
|
||||
gui_push_vertex(buffer, gx, gy, col, g->uv[0].u, g->uv[0].v);
|
||||
gui_push_vertex(buffer, gx + gw, gy + gh, col, g->uv[1].u, g->uv[1].v);
|
||||
gui_push_vertex(buffer, gx, gy + gh, col, g->uv[0].u, g->uv[1].v);
|
||||
text_len += utf_decode(text + text_len, &unicode, len - text_len);
|
||||
x += char_width;
|
||||
x += g->xadvance * font->scale;
|
||||
}
|
||||
gui_pop_clip(buffer);
|
||||
}
|
||||
@ -753,7 +896,7 @@ gui_widget_text(struct gui_draw_buffer *buffer, const struct gui_text *text,
|
||||
} else if (text->align == GUI_TEXT_RIGHT) {
|
||||
label_x = MAX(text->x, (text->x + text->w) - (2 * text->pad_x + (gui_float)text_width));
|
||||
label_w = (gui_float)text_width + 2 * text->pad_x;
|
||||
}
|
||||
} else return;
|
||||
|
||||
gui_draw_rectf(buffer, text->x, text->y, text->w, text->h, text->background);
|
||||
gui_draw_string(buffer, font, label_x, label_y, label_w, label_h,
|
||||
@ -776,6 +919,7 @@ gui_widget_image(struct gui_draw_buffer *buffer, const struct gui_image *image)
|
||||
image_y = image->y + image->pad_y;
|
||||
image_w = MAX(0, image->w - 2 * image->pad_x);
|
||||
image_h = MAX(0, image->h - 2 * image->pad_y);
|
||||
|
||||
gui_draw_rectf(buffer, image->x, image->y, image->w, image->h, image->background);
|
||||
gui_draw_image(buffer, image_x, image_y, image_w, image_h,
|
||||
image->texture, image->uv[0], image->uv[1], image->color);
|
||||
@ -787,6 +931,7 @@ gui_widget_button(struct gui_draw_buffer *buffer, const struct gui_button *butto
|
||||
{
|
||||
gui_bool ret = gui_false;
|
||||
struct gui_color background, highlight;
|
||||
|
||||
assert(buffer);
|
||||
assert(button);
|
||||
if (!buffer || !button)
|
||||
@ -797,7 +942,7 @@ gui_widget_button(struct gui_draw_buffer *buffer, const struct gui_button *butto
|
||||
background = button->highlight;
|
||||
if (INBOX(in->mouse_clicked_pos.x, in->mouse_clicked_pos.y,
|
||||
button->x, button->y, button->w, button->h)) {
|
||||
if (button->behavior == GUI_BUTTON_SWITCH)
|
||||
if (button->behavior == GUI_BUTTON_DEFAULT)
|
||||
ret = (in->mouse_down && in->mouse_clicked);
|
||||
else
|
||||
ret = in->mouse_down;
|
||||
@ -858,6 +1003,7 @@ gui_widget_button_triangle(struct gui_draw_buffer *buffer, struct gui_button* bu
|
||||
gui_bool pressed;
|
||||
struct gui_color col;
|
||||
struct gui_vec2 points[3];
|
||||
|
||||
pressed = gui_widget_button(buffer, button, in);
|
||||
gui_triangle_from_direction(points, button->x, button->y, button->w, button->h,
|
||||
button->pad_x, button->pad_y, heading);
|
||||
@ -933,6 +1079,7 @@ gui_widget_toggle(struct gui_draw_buffer *buffer, const struct gui_toggle *toggl
|
||||
cursor_x = select_x + cursor_pad;
|
||||
cursor_y = select_y + cursor_pad;
|
||||
cursor_size = select_size - 2 * cursor_pad;
|
||||
|
||||
if (in && !in->mouse_down && in->mouse_clicked) {
|
||||
if (INBOX(in->mouse_clicked_pos.x, in->mouse_clicked_pos.y,
|
||||
cursor_x, cursor_y, cursor_size, cursor_size))
|
||||
@ -1269,15 +1416,17 @@ gui_widget_plot(struct gui_draw_buffer *buffer, const struct gui_plot *plot,
|
||||
const struct gui_input *in)
|
||||
{
|
||||
gui_size i;
|
||||
gui_float canvas_x, canvas_y;
|
||||
gui_float canvas_w, canvas_h;
|
||||
|
||||
gui_size plot_step;
|
||||
gui_float plot_last_x;
|
||||
gui_float plot_last_y;
|
||||
gui_float plot_w, plot_h;
|
||||
gui_int plot_selected = -1;
|
||||
gui_float plot_max_value, plot_min_value;
|
||||
gui_float plot_value_range, plot_value_ratio;
|
||||
gui_float canvas_x, canvas_y;
|
||||
gui_float canvas_w, canvas_h;
|
||||
gui_float plot_last_x;
|
||||
gui_float plot_last_y;
|
||||
|
||||
struct gui_color col;
|
||||
|
||||
assert(buffer);
|
||||
@ -1409,6 +1558,7 @@ gui_widget_scroll(struct gui_draw_buffer *buffer, const struct gui_scroll *scrol
|
||||
assert(buffer);
|
||||
assert(scroll);
|
||||
if (!buffer || !scroll) return 0;
|
||||
|
||||
scroll_w = MAX(scroll->w, 0);
|
||||
scroll_h = MAX(scroll->h, 2 * scroll_w);
|
||||
gui_draw_rectf(buffer, scroll->x, scroll->y, scroll_w, scroll_h, scroll->background);
|
||||
@ -1424,7 +1574,7 @@ gui_widget_scroll(struct gui_draw_buffer *buffer, const struct gui_scroll *scrol
|
||||
button.background = scroll->foreground;
|
||||
button.foreground = scroll->background;
|
||||
button.highlight = scroll->foreground;
|
||||
button.behavior = GUI_BUTTON_SWITCH;
|
||||
button.behavior = GUI_BUTTON_DEFAULT;
|
||||
button_up_pressed = gui_widget_button_triangle(buffer, &button, GUI_UP, in);
|
||||
button.y = scroll->y + scroll_h - button.h;
|
||||
button_down_pressed = gui_widget_button_triangle(buffer, &button, GUI_DOWN, in);
|
||||
@ -1557,21 +1707,20 @@ gui_panel_begin(struct gui_panel *panel, struct gui_draw_buffer *out,
|
||||
panel->y = y;
|
||||
panel->width = w;
|
||||
panel->at_y = y;
|
||||
panel->flags = f;
|
||||
panel->index = 0;
|
||||
panel->row_columns = 0;
|
||||
panel->flags = f;
|
||||
if (!(panel->flags & GUI_PANEL_TAB))
|
||||
panel->flags |= GUI_PANEL_SCROLLBAR;
|
||||
|
||||
config = panel->config;
|
||||
header = &config->colors[GUI_COLOR_TITLEBAR];
|
||||
header_x = x + config->panel_padding.x;
|
||||
header_w = w - 2 * config->panel_padding.x;
|
||||
mouse_x = (panel->in) ? panel->in->mouse_pos.x : -1;
|
||||
mouse_y = (panel->in) ? panel->in->mouse_pos.y: -1;
|
||||
clicked_x = (panel->in) ? panel->in->mouse_clicked_pos.x: - 1;
|
||||
clicked_y = (panel->in) ? panel->in->mouse_clicked_pos.y: - 1;
|
||||
header_x = x + config->panel_padding.x;
|
||||
header_w = w - 2 * config->panel_padding.x;
|
||||
|
||||
if (!(panel->flags & GUI_PANEL_TAB))
|
||||
panel->flags |= GUI_PANEL_SCROLLBAR;
|
||||
|
||||
panel->header_height = panel->font->height + 3 * config->item_padding.y;
|
||||
panel->header_height += config->panel_padding.y;
|
||||
@ -1706,9 +1855,9 @@ gui_panel_seperator(struct gui_panel *panel, gui_size cols)
|
||||
assert(panel);
|
||||
assert(panel->config);
|
||||
assert(panel->out);
|
||||
|
||||
if (!panel) return;
|
||||
if (panel->minimized || (panel->flags & GUI_PANEL_HIDDEN)) return;
|
||||
|
||||
config = panel->config;
|
||||
cols = MIN(cols, panel->row_columns - panel->index);
|
||||
panel->index += cols;
|
||||
@ -1896,6 +2045,7 @@ gui_panel_button_icon(struct gui_panel *panel, gui_texture tex,
|
||||
if (!panel || !panel->config || !panel->out) return 0;
|
||||
if (panel->minimized || (panel->flags & GUI_PANEL_HIDDEN)) return 0;
|
||||
gui_panel_alloc_space(&bounds, panel);
|
||||
|
||||
config = panel->config;
|
||||
button.x = bounds.x;
|
||||
button.y = bounds.y;
|
||||
@ -1935,9 +2085,10 @@ gui_panel_button_toggle(struct gui_panel *panel, const char *str, gui_bool value
|
||||
button.y = bounds.y;
|
||||
button.w = bounds.w;
|
||||
button.h = bounds.h;
|
||||
button.behavior = GUI_BUTTON_SWITCH;
|
||||
button.behavior = GUI_BUTTON_DEFAULT;
|
||||
button.pad_x = config->item_padding.x;
|
||||
button.pad_y = config->item_padding.y;
|
||||
|
||||
if (!value) {
|
||||
button.background = config->colors[GUI_COLOR_BUTTON];
|
||||
button.foreground = config->colors[GUI_COLOR_BUTTON_BORDER];
|
||||
@ -2173,7 +2324,7 @@ gui_panel_shell(struct gui_panel *panel, gui_char *buffer, gui_size *length,
|
||||
button.x = field.x + field.w - button.w + 1;
|
||||
button.pad_x = field.pad_x;
|
||||
button.pad_y = field.pad_y;
|
||||
button.behavior = GUI_BUTTON_SWITCH;
|
||||
button.behavior = GUI_BUTTON_DEFAULT;
|
||||
button.background = config->colors[GUI_COLOR_BUTTON];
|
||||
button.foreground = config->colors[GUI_COLOR_BUTTON_BORDER];
|
||||
button.content = config->colors[GUI_COLOR_TEXT];
|
||||
@ -2233,7 +2384,7 @@ gui_panel_spinner(struct gui_panel *panel, gui_int min, gui_int *value,
|
||||
button.x = bounds.x + bounds.w - button.w - 1;
|
||||
button.pad_x = MAX(3, button.h - panel->font->height);
|
||||
button.pad_y = MAX(3, button.h - panel->font->height);
|
||||
button.behavior = GUI_BUTTON_SWITCH;
|
||||
button.behavior = GUI_BUTTON_DEFAULT;
|
||||
button.background = config->colors[GUI_COLOR_BUTTON];
|
||||
button.foreground = config->colors[GUI_COLOR_BUTTON_BORDER];
|
||||
button.content = config->colors[GUI_COLOR_TEXT];
|
||||
@ -2305,8 +2456,8 @@ gui_panel_selector(struct gui_panel *panel, const char *items[],
|
||||
button.x = bounds.x + bounds.w - button.w - 1;
|
||||
button.pad_x = MAX(3, button.h - panel->font->height);
|
||||
button.pad_y = MAX(3, button.h - panel->font->height);
|
||||
button.behavior = GUI_BUTTON_SWITCH;
|
||||
button.behavior = GUI_BUTTON_SWITCH;
|
||||
button.behavior = GUI_BUTTON_DEFAULT;
|
||||
button.behavior = GUI_BUTTON_DEFAULT;
|
||||
button.background = config->colors[GUI_COLOR_BUTTON];
|
||||
button.foreground = config->colors[GUI_COLOR_BUTTON_BORDER];
|
||||
button.content = config->colors[GUI_COLOR_TEXT];
|
||||
@ -2355,6 +2506,7 @@ gui_panel_plot(struct gui_panel *panel, const gui_float *values, gui_size count)
|
||||
plot.pad_y = config->item_padding.y;
|
||||
plot.value_count = count;
|
||||
plot.values = values;
|
||||
|
||||
plot.background = config->colors[GUI_COLOR_PLOT];
|
||||
plot.foreground = config->colors[GUI_COLOR_PLOT_LINES];
|
||||
plot.highlight = config->colors[GUI_COLOR_PLOT_HIGHLIGHT];
|
||||
@ -2387,6 +2539,7 @@ gui_panel_histo(struct gui_panel *panel, const gui_float *values, gui_size count
|
||||
histo.pad_y = config->item_padding.y;
|
||||
histo.values = values;
|
||||
histo.value_count = count;
|
||||
|
||||
histo.background = config->colors[GUI_COLOR_HISTO];
|
||||
histo.foreground = config->colors[GUI_COLOR_HISTO_BARS];
|
||||
histo.negative = config->colors[GUI_COLOR_HISTO_NEGATIVE];
|
||||
@ -2430,10 +2583,11 @@ gui_panel_tab_begin(struct gui_panel *panel, gui_tab *tab, const char *title)
|
||||
void
|
||||
gui_panel_tab_end(struct gui_panel *panel, struct gui_panel *tab)
|
||||
{
|
||||
assert(panel);
|
||||
assert(tab);
|
||||
if (!panel || !tab) return;
|
||||
if (panel->minimized || (panel->flags & GUI_PANEL_HIDDEN)) return;
|
||||
assert(panel);
|
||||
if (!panel || !tab || panel->minimized || (panel->flags & GUI_PANEL_HIDDEN))
|
||||
return;
|
||||
|
||||
gui_panel_end(tab);
|
||||
panel->at_y -= panel->row_height;
|
||||
panel->at_y += tab->height + tab->config->item_spacing.y;
|
||||
@ -2443,18 +2597,20 @@ void
|
||||
gui_panel_group_begin(struct gui_panel *panel, gui_group *group, const char *title)
|
||||
{
|
||||
gui_flags flags;
|
||||
struct gui_rect bounds;
|
||||
gui_float offset;
|
||||
struct gui_rect bounds;
|
||||
|
||||
assert(panel);
|
||||
assert(group);
|
||||
if (!panel || !group) return;
|
||||
if ((panel->flags & GUI_PANEL_HIDDEN) || panel->minimized) return;
|
||||
if (!panel || !group || (panel->flags & GUI_PANEL_HIDDEN) || panel->minimized)
|
||||
return;
|
||||
|
||||
offset = group->offset;
|
||||
gui_panel_alloc_space(&bounds, panel);
|
||||
gui_panel_init(group, panel->config, panel->font);
|
||||
flags = GUI_PANEL_BORDER|GUI_PANEL_SCROLLBAR|GUI_PANEL_TAB;
|
||||
|
||||
group->offset = offset;
|
||||
flags = GUI_PANEL_BORDER|GUI_PANEL_SCROLLBAR|GUI_PANEL_TAB;
|
||||
gui_panel_begin(group, panel->out, panel->in, title,
|
||||
bounds.x, bounds.y, bounds.w, bounds.h, flags);
|
||||
}
|
||||
@ -2473,14 +2629,14 @@ gui_size
|
||||
gui_panel_shelf_begin(struct gui_panel *panel, gui_shelf *shelf,
|
||||
const char *tabs[], gui_size size, gui_size active)
|
||||
{
|
||||
gui_size i;
|
||||
gui_flags flags;
|
||||
struct gui_rect bounds;
|
||||
const struct gui_config *config;
|
||||
gui_float header_x, header_y;
|
||||
gui_float header_w, header_h;
|
||||
struct gui_rect bounds;
|
||||
gui_float item_width;
|
||||
gui_float offset;
|
||||
gui_flags flags;
|
||||
gui_size i;
|
||||
|
||||
assert(panel);
|
||||
assert(tabs);
|
||||
@ -2499,6 +2655,7 @@ gui_panel_shelf_begin(struct gui_panel *panel, gui_shelf *shelf,
|
||||
header_w = bounds.w;
|
||||
header_h = config->panel_padding.y + 3 * config->item_padding.y + panel->font->height;
|
||||
item_width = (header_w - (gui_float)size) / (gui_float)size;
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
struct gui_button button;
|
||||
button.y = header_y;
|
||||
@ -2507,21 +2664,15 @@ gui_panel_shelf_begin(struct gui_panel *panel, gui_shelf *shelf,
|
||||
button.w = item_width;
|
||||
button.pad_x = config->item_padding.x;
|
||||
button.pad_y = config->item_padding.y;
|
||||
button.behavior = GUI_BUTTON_SWITCH;
|
||||
if (active == i) {
|
||||
button.background = config->colors[GUI_COLOR_BUTTON];
|
||||
button.foreground = config->colors[GUI_COLOR_BUTTON_BORDER];
|
||||
button.content = config->colors[GUI_COLOR_TEXT];
|
||||
button.highlight = config->colors[GUI_COLOR_BUTTON];
|
||||
button.highlight_content = config->colors[GUI_COLOR_TEXT];
|
||||
} else {
|
||||
button.behavior = GUI_BUTTON_DEFAULT;
|
||||
button.background = config->colors[GUI_COLOR_BUTTON];
|
||||
button.foreground = config->colors[GUI_COLOR_BUTTON_BORDER];
|
||||
button.content = config->colors[GUI_COLOR_TEXT];
|
||||
button.highlight = config->colors[GUI_COLOR_BUTTON];
|
||||
button.highlight_content = config->colors[GUI_COLOR_TEXT];
|
||||
if (active != i) {
|
||||
button.y += config->item_padding.y;
|
||||
button.h -= config->item_padding.y;
|
||||
button.background = config->colors[GUI_COLOR_BUTTON];
|
||||
button.foreground = config->colors[GUI_COLOR_BUTTON_BORDER];
|
||||
button.content = config->colors[GUI_COLOR_TEXT];
|
||||
button.highlight = config->colors[GUI_COLOR_BUTTON];
|
||||
button.highlight_content = config->colors[GUI_COLOR_TEXT];
|
||||
}
|
||||
if (gui_widget_button_text(panel->out, &button, tabs[i], strsiz(tabs[i]),
|
||||
panel->font, panel->in)) active = i;
|
||||
@ -2531,6 +2682,7 @@ gui_panel_shelf_begin(struct gui_panel *panel, gui_shelf *shelf,
|
||||
bounds.h -= header_h;
|
||||
offset = shelf->offset;
|
||||
gui_panel_init(shelf, panel->config, panel->font);
|
||||
|
||||
shelf->offset = offset;
|
||||
flags = GUI_PANEL_BORDER|GUI_PANEL_SCROLLBAR|GUI_PANEL_TAB;
|
||||
gui_panel_begin(shelf, panel->out, panel->in, NULL,
|
||||
@ -2561,18 +2713,20 @@ gui_panel_end(struct gui_panel *panel)
|
||||
config = panel->config;
|
||||
if (panel->flags & GUI_PANEL_SCROLLBAR && !panel->minimized) {
|
||||
gui_float panel_y;
|
||||
|
||||
struct gui_scroll scroll;
|
||||
scroll.x = panel->x + panel->width;
|
||||
scroll.y = (panel->flags & GUI_PANEL_BORDER) ? panel->y + 1 : panel->y;
|
||||
scroll.y += panel->header_height;
|
||||
scroll.w = config->scrollbar_width;
|
||||
scroll.h = panel->height;
|
||||
scroll.offset = panel->offset;
|
||||
scroll.step = panel->height * 0.25f;
|
||||
scroll.background = config->colors[GUI_COLOR_SCROLLBAR];
|
||||
scroll.foreground = config->colors[GUI_COLOR_SCROLLBAR_CURSOR];
|
||||
scroll.border = config->colors[GUI_COLOR_SCROLLBAR_BORDER];
|
||||
scroll.h = panel->height;
|
||||
scroll.y += panel->header_height;
|
||||
if (panel->flags & GUI_PANEL_BORDER) scroll.h -= 1;
|
||||
if (panel->flags & GUI_PANEL_BORDER)
|
||||
scroll.h -= 1;
|
||||
scroll.target = (panel->at_y - panel->y) - panel->header_height;
|
||||
|
||||
panel->offset = (gui_float)gui_widget_scroll(panel->out, &scroll, panel->in);
|
||||
@ -2677,6 +2831,7 @@ gui_stack_push(struct gui_context *ctx, struct gui_context_panel *panel)
|
||||
ctx->stack_end = panel;
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->stack_end->next = panel;
|
||||
panel->prev = ctx->stack_end;
|
||||
panel->next = NULL;
|
||||
@ -2715,6 +2870,7 @@ gui_panel_new(struct gui_context *ctx,
|
||||
cpanel->x = x, cpanel->y = y;
|
||||
cpanel->w = w, cpanel->h = h;
|
||||
cpanel->next = NULL; cpanel->prev = NULL;
|
||||
|
||||
gui_panel_init(&cpanel->panel, config, font);
|
||||
gui_stack_push(ctx, cpanel);
|
||||
ctx->panel_size++;
|
||||
@ -2728,6 +2884,7 @@ gui_panel_del(struct gui_context *ctx, struct gui_panel *panel)
|
||||
assert(ctx);
|
||||
assert(panel);
|
||||
if (!ctx || !panel) return;
|
||||
|
||||
cpanel = (struct gui_context_panel*)panel;
|
||||
gui_stack_remove(ctx, cpanel);
|
||||
gui_free_panel(ctx, cpanel);
|
||||
@ -2752,6 +2909,7 @@ gui_get_panel_position(const struct gui_context *ctx, const struct gui_panel *pa
|
||||
assert(ctx && panel);
|
||||
pos.x = 0; pos.y = 0;
|
||||
if (!ctx || !panel) return pos;
|
||||
|
||||
cpanel = (const struct gui_context_panel*)panel;
|
||||
pos.x = cpanel->x;
|
||||
pos.y = cpanel->y;
|
||||
@ -2767,6 +2925,7 @@ gui_get_panel_size(const struct gui_context *ctx, const struct gui_panel *panel)
|
||||
assert(ctx && panel);
|
||||
size.x = 0; size.y = 0;
|
||||
if (!ctx || !panel) return size;
|
||||
|
||||
cpanel = (const struct gui_context_panel*)panel;
|
||||
size.x = cpanel->w;
|
||||
size.y = cpanel->h;
|
||||
@ -2791,6 +2950,8 @@ gui_begin_panel(struct gui_context *ctx, struct gui_panel *panel,
|
||||
return gui_false;
|
||||
if (panel->flags & GUI_PANEL_HIDDEN)
|
||||
return gui_false;
|
||||
if (!(flags & GUI_PANEL_TAB))
|
||||
flags |= GUI_PANEL_SCROLLBAR;
|
||||
|
||||
in = ctx->input;
|
||||
cpanel = (struct gui_context_panel*)panel;
|
||||
@ -2810,15 +2971,13 @@ gui_begin_panel(struct gui_context *ctx, struct gui_panel *panel,
|
||||
}
|
||||
}
|
||||
|
||||
if (!(flags & GUI_PANEL_TAB))
|
||||
flags |= GUI_PANEL_SCROLLBAR;
|
||||
|
||||
if (ctx->active == cpanel && (flags & GUI_PANEL_MOVEABLE)) {
|
||||
gui_bool incursor;
|
||||
const gui_float header_x = cpanel->x;
|
||||
const gui_float header_y = cpanel->y;
|
||||
const gui_float header_w = cpanel->w;
|
||||
const gui_float header_h = cpanel->panel.header_height;
|
||||
|
||||
incursor = INBOX(in->mouse_prev.x,in->mouse_prev.y,header_x, header_y, header_w, header_h);
|
||||
if (in->mouse_down && incursor) {
|
||||
cpanel->x = CLAMP(0, cpanel->x + in->mouse_delta.x, ctx->width - cpanel->w);
|
||||
@ -2833,6 +2992,7 @@ gui_begin_panel(struct gui_context *ctx, struct gui_panel *panel,
|
||||
const gui_float scaler_y = (cpanel->y + cpanel->h) - config->scaler_size.y;
|
||||
const gui_float scaler_w = config->scaler_size.x;
|
||||
const gui_float scaler_h = config->scaler_size.y;
|
||||
|
||||
incursor = INBOX(in->mouse_prev.x,in->mouse_prev.y,scaler_x, scaler_y, scaler_w, scaler_h);
|
||||
if (in->mouse_down && incursor) {
|
||||
cpanel->x = CLAMP(0, cpanel->x + in->mouse_delta.x, ctx->width - cpanel->w);
|
||||
@ -2876,6 +3036,7 @@ gui_end_panel(struct gui_context *ctx, struct gui_panel *panel,
|
||||
const struct gui_config *config = panel->config;
|
||||
struct gui_color col = config->colors[GUI_COLOR_SCALER];
|
||||
const gui_float height = panel->height + panel->header_height;
|
||||
|
||||
gui_float scaler_x = panel->x + config->item_padding.x;
|
||||
gui_float scaler_y = panel->y + height - config->scaler_size.y;
|
||||
gui_float scaler_w = MAX(0, config->scaler_size.x - config->item_padding.x);
|
||||
|
69
gui.h
69
gui.h
@ -6,21 +6,43 @@
|
||||
#ifndef GUI_H_
|
||||
#define GUI_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GUI_UTF_SIZE 4
|
||||
#define GUI_INPUT_MAX 16
|
||||
#define GUI_ATLAS_DEPTH 4
|
||||
|
||||
#ifdef GUI_USE_FIXED_TYPES
|
||||
#include <stdint.h>
|
||||
typedef int32_t gui_int;
|
||||
typedef int32_t gui_bool;
|
||||
typedef int16_t gui_short;
|
||||
typedef int64_t gui_long;
|
||||
typedef float gui_float;
|
||||
typedef uint32_t gui_uint;
|
||||
typedef uint64_t gui_ulong;
|
||||
typedef uint32_t gui_flags;
|
||||
typedef uint8_t gui_char;
|
||||
typedef uint8_t gui_byte;
|
||||
typedef uint32_t gui_flag;
|
||||
typedef uint64_t gui_size;
|
||||
#else
|
||||
typedef int gui_int;
|
||||
typedef unsigned int gui_uint;
|
||||
typedef int gui_bool;
|
||||
typedef short gui_short;
|
||||
typedef long gui_long;
|
||||
typedef float gui_float;
|
||||
typedef unsigned int gui_uint;
|
||||
typedef unsigned long gui_ulong;
|
||||
typedef unsigned int gui_bool;
|
||||
typedef unsigned int gui_flags;
|
||||
typedef unsigned char gui_char;
|
||||
typedef float gui_float;
|
||||
typedef unsigned char gui_byte;
|
||||
typedef unsigned int gui_flag;
|
||||
typedef unsigned long gui_size;
|
||||
#endif
|
||||
|
||||
typedef gui_char gui_glyph[GUI_UTF_SIZE];
|
||||
typedef union {void* dx; gui_uint gl;} gui_texture;
|
||||
typedef struct gui_panel gui_tab;
|
||||
@ -37,8 +59,27 @@ struct gui_vec2 {gui_float x,y;};
|
||||
struct gui_rect {gui_float x,y,w,h;};
|
||||
struct gui_key {gui_bool down, clicked;};
|
||||
|
||||
#ifdef GUI_USE_FREETYPE_FONT
|
||||
enum gui_font_atlas_dimension {
|
||||
GUI_ATLAS_DIM_64 = 64,
|
||||
GUI_ATLAS_DIM_128 = 128,
|
||||
GUI_ATLAS_DIM_256 = 256,
|
||||
GUI_ATLAS_DIM_512 = 512,
|
||||
GUI_ATLAS_DIM_1024 = 1024,
|
||||
GUI_ATLAS_DIM_2048 = 2048,
|
||||
GUI_ATLAS_DIM_4096 = 4095
|
||||
};
|
||||
|
||||
struct gui_font_atlas {
|
||||
enum gui_font_atlas_dimension dim;
|
||||
gui_size range;
|
||||
gui_size size;
|
||||
gui_byte *memory;
|
||||
};
|
||||
#endif
|
||||
|
||||
struct gui_font_glyph {
|
||||
gui_ulong code;
|
||||
gui_uint code;
|
||||
gui_float xadvance;
|
||||
gui_short width, height;
|
||||
gui_float xoff, yoff;
|
||||
@ -50,7 +91,7 @@ struct gui_font {
|
||||
gui_float scale;
|
||||
gui_texture texture;
|
||||
struct gui_vec2 tex_size;
|
||||
gui_long glyph_count;
|
||||
gui_uint glyph_count;
|
||||
struct gui_font_glyph *glyphes;
|
||||
const struct gui_font_glyph *fallback;
|
||||
};
|
||||
@ -163,7 +204,7 @@ struct gui_image {
|
||||
};
|
||||
|
||||
enum gui_button_behavior {
|
||||
GUI_BUTTON_SWITCH,
|
||||
GUI_BUTTON_DEFAULT,
|
||||
GUI_BUTTON_REPEATER
|
||||
};
|
||||
|
||||
@ -368,6 +409,13 @@ void gui_output_begin(struct gui_draw_buffer*, const struct gui_memory*);
|
||||
void gui_output_end(struct gui_draw_buffer*, struct gui_draw_call_list*,
|
||||
struct gui_memory_status*);
|
||||
|
||||
/* font */
|
||||
#ifdef GUI_USE_FREETYPE_FONT
|
||||
#define gui_font_atlas_alloc_size(sz) ((sz) * (sz) * GUI_ATLAS_DEPTH)
|
||||
gui_bool gui_font_load(struct gui_font*, struct gui_font_atlas*, gui_uint height,
|
||||
const gui_byte*, gui_size);
|
||||
#endif
|
||||
|
||||
/* Widgets */
|
||||
void gui_widget_text(struct gui_draw_buffer*, const struct gui_text*,
|
||||
const struct gui_font*);
|
||||
@ -402,14 +450,14 @@ gui_int gui_widget_plot(struct gui_draw_buffer*, const struct gui_plot*,
|
||||
|
||||
/* Panel */
|
||||
void gui_default_config(struct gui_config*);
|
||||
void gui_panel_init(struct gui_panel*, const struct gui_config*,
|
||||
const struct gui_font*);
|
||||
void gui_panel_show(struct gui_panel*);
|
||||
void gui_panel_hide(struct gui_panel*);
|
||||
void gui_panel_init(struct gui_panel*, const struct gui_config*, const struct gui_font*);
|
||||
gui_bool gui_panel_is_hidden(const struct gui_panel*);
|
||||
gui_bool gui_panel_begin(struct gui_panel*, struct gui_draw_buffer*,
|
||||
const struct gui_input*, const char*,
|
||||
gui_float x, gui_float y, gui_float w, gui_float h, gui_flags);
|
||||
void gui_panel_end(struct gui_panel*);
|
||||
void gui_panel_layout(struct gui_panel*, gui_float height, gui_size cols);
|
||||
void gui_panel_seperator(struct gui_panel*, gui_size cols);
|
||||
void gui_panel_text(struct gui_panel*, const char *str, gui_size len, enum gui_text_align);
|
||||
@ -442,7 +490,6 @@ void gui_panel_group_end(gui_group*, gui_group* tab);
|
||||
gui_size gui_panel_shelf_begin(gui_shelf*, gui_shelf *shelf, const char *tabs[],
|
||||
gui_size size, gui_size active);
|
||||
void gui_panel_shelf_end(gui_shelf*, gui_shelf *shelf);
|
||||
void gui_panel_end(struct gui_panel*);
|
||||
|
||||
/* Context */
|
||||
struct gui_context;
|
||||
@ -457,4 +504,8 @@ struct gui_vec2 gui_get_panel_size(const struct gui_context*, const struct gui_p
|
||||
gui_bool gui_begin_panel(struct gui_context*, struct gui_panel*, const char *title, gui_flags flags);
|
||||
void gui_end_panel(struct gui_context*, struct gui_panel*, struct gui_memory_status*);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user