change slider + progressbar style

This commit is contained in:
vurtun 2015-09-07 18:37:30 +02:00
parent f25790c430
commit e2e1b4b51e
6 changed files with 48 additions and 101 deletions

View File

@ -14,6 +14,7 @@ application and does not have any direct dependencies.
- No direct dependencies (not even libc!)
- Configurable style and colors
- UTF-8 support
- Optional vertex output
## Gallery
![gui demo](/screen/demo.png?raw=true)
@ -25,7 +26,7 @@ application and does not have any direct dependencies.
/* allocate memory to hold the draw commands */
struct gui_command_queue queue;
void *memory = malloc(MEMORY_SIZE);
gui_command_queue_init_fixed(&buffer, memory, MEMORY_SIZE);
gui_command_queue_init_fixed(&queue, memory, MEMORY_SIZE);
/* setup configuration */
struct gui_font font;

View File

@ -513,7 +513,6 @@ widget_panel(struct gui_context *panel, struct state *demo)
fprintf(stdout, "right triangle button pressed!\n");
if (gui_button_text_symbol(panel,GUI_SYMBOL_TRIANGLE_LEFT,"previous",GUI_TEXT_RIGHT,GUI_BUTTON_DEFAULT))
fprintf(stdout, "left triangle button pressed!\n");
demo->toggle = gui_button_toggle(panel, "toggle", demo->toggle);
/* Checkbox + Radio buttons */
demo->checkbox = gui_check(panel, "checkbox", demo->checkbox);

View File

@ -304,7 +304,7 @@ main(int argc, char *argv[])
run_demo(&gui);
/* Draw */
glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
glClearColor(0.8588f, 0.835f, 0.86f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
draw(vg, &gui.queue, width, height);
SDL_GL_SwapWindow(win);

View File

@ -476,7 +476,7 @@ device_init(struct device *dev)
glBindTexture(GL_TEXTURE_2D, dev->null_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 64, 64, 0,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0,
GL_RGBA, GL_UNSIGNED_BYTE, mem);
free(mem);
glerror();
@ -549,7 +549,6 @@ device_draw(struct device *dev, struct gui_command_queue *queue, int width, int
const struct gui_draw_command *cmd;
static const GLsizeiptr max_vertex_memory = 128 * 1024;
static const GLsizeiptr max_element_memory = 32 * 1024;
struct gui_buffer vbuf, ebuf;
void *vertexes, *elements;
const gui_draw_index *offset = NULL;
@ -566,6 +565,7 @@ device_draw(struct device *dev, struct gui_command_queue *queue, int width, int
vertexes = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
{
struct gui_buffer vbuf, ebuf;
gui_buffer_init_fixed(&vbuf, vertexes, (gui_size)max_vertex_memory);
gui_buffer_init_fixed(&ebuf, elements, (gui_size)max_element_memory);
gui_draw_list_init(&draw_list, &dev->cmds, &vbuf, &ebuf,
@ -667,7 +667,6 @@ resize(SDL_Event *evt)
glViewport(0, 0, evt->window.data1, evt->window.data2);
}
int
main(int argc, char *argv[])
{
@ -746,7 +745,7 @@ main(int argc, char *argv[])
/* Draw */
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.4f, 0.4f, 0.4f, 1.0f);
glClearColor(0.8588f, 0.835f, 0.86f, 1.0f);
device_draw(&device, &gui.queue, width, height);
SDL_GL_SwapWindow(win);

112
gui.c
View File

@ -1165,7 +1165,6 @@ gui_command_buffer_next(struct gui_command_buffer *buffer,
memory = (gui_byte*)buffer->base->memory.ptr;
return gui_ptr_add_const(struct gui_command, memory, cmd->next);
}
/*
* ==============================================================
*
@ -2453,6 +2452,14 @@ gui_draw_list_path_stroke(struct gui_draw_list *list, struct gui_color color,
gui_draw_list_path_clear(list);
}
#endif
/*
* ==============================================================
*
* Font
*
* ===============================================================
*/
/*
* ==============================================================
*
@ -2825,15 +2832,6 @@ gui_edit_box_len(struct gui_edit_box *eb)
GUI_ASSERT(eb);
return eb->glyphes;
}
/*
* ==============================================================
*
* Font
*
* ===============================================================
*/
/*
* ==============================================================
*
@ -3210,8 +3208,8 @@ gui_widget_slider(struct gui_command_buffer *out, struct gui_rect slider,
/* make sure the provided values are correct */
slider.x = slider.x + s->padding.x;
slider.y = slider.y + s->padding.y;
slider.h = MAX(slider.h, 2 * s->padding.y + 5);
slider.w = MAX(slider.w, 1 + slider.h + 2 * s->padding.x + 5);
slider.h = MAX(slider.h, 2 * s->padding.y);
slider.w = MAX(slider.w, 1 + slider.h + 2 * s->padding.x);
slider.h -= 2 * s->padding.y;
slider.w -= 2 * s->padding.y;
slider_max = MAX(min, max);
@ -3229,9 +3227,9 @@ gui_widget_slider(struct gui_command_buffer *out, struct gui_rect slider,
/* calculate slider background bar bounds */
bar.x = slider.x;
bar.y = slider.y;
bar.y = (slider.y + cursor.h/2) - cursor.h/8;
bar.w = slider.w;
bar.h = slider.h;
bar.h = slider.h/4;
/* updated the slider value by user input */
inslider = in && gui_input_is_mouse_hovering_rect(in, slider);
@ -3259,18 +3257,15 @@ gui_widget_slider(struct gui_command_buffer *out, struct gui_rect slider,
(slider_value >= slider_max) ? ((bar.x + bar.w) - cursor.w) :
cursor.x - (cursor.w/2);
fill.x = bar.x + 2;
fill.y = bar.y + 2;
fill.x = bar.x;
fill.y = bar.y;
fill.w = (cursor.x + (cursor.w/2.0f)) - bar.x;
fill.h = bar.h - 4;
fill.h = bar.h;
/* draw slider with background and circle cursor*/
gui_command_buffer_push_rect(out, bar, s->rounding, s->border);
gui_command_buffer_push_rect(out, gui_shrink_rect(bar, 1), s->rounding, s->bg);
gui_command_buffer_push_rect(out, fill, s->rounding, s->border);
gui_command_buffer_push_rect(out, gui_shrink_rect(fill,1), s->rounding, col);
gui_command_buffer_push_circle(out, cursor, s->border);
gui_command_buffer_push_circle(out, gui_shrink_rect(cursor, 1), col);
gui_command_buffer_push_rect(out, bar, 0, s->bg);
gui_command_buffer_push_rect(out, fill, 0, col);
gui_command_buffer_push_circle(out, cursor, col);
}
return slider_value;
}
@ -3280,7 +3275,6 @@ gui_widget_progress(struct gui_command_buffer *out, struct gui_rect r,
gui_size value, gui_size max, gui_bool modifyable,
const struct gui_progress *prog, const struct gui_input *in)
{
struct gui_rect cursor;
gui_float prog_scale;
gui_size prog_value;
struct gui_color col;
@ -3309,15 +3303,10 @@ gui_widget_progress(struct gui_command_buffer *out, struct gui_rect r,
prog_value = MIN(prog_value, max);
prog_scale = (gui_float)prog_value / (gui_float)max;
/* calculate progress bar cursor */
cursor = gui_shrink_rect(r, 2);
cursor.w = (r.w - 2) * prog_scale;
/* draw progressbar width background and cursor */
gui_command_buffer_push_rect(out, r, prog->rounding, prog->border);
gui_command_buffer_push_rect(out, gui_shrink_rect(r, 1), prog->rounding, prog->background);
gui_command_buffer_push_rect(out, cursor, prog->rounding, prog->border);
gui_command_buffer_push_rect(out, gui_shrink_rect(cursor,1), prog->rounding, col);
gui_command_buffer_push_rect(out, r, prog->rounding, prog->background);
r.w = (r.w - 2) * prog_scale;
gui_command_buffer_push_rect(out, r, prog->rounding, col);
return prog_value;
}
@ -3923,9 +3912,9 @@ gui_style_default_rounding(struct gui_style *style)
{
style->rounding[GUI_ROUNDING_BUTTON] = 4.0f;
style->rounding[GUI_ROUNDING_SLIDER] = 8.0f;
style->rounding[GUI_ROUNDING_PROGRESS] = 8.0f;
style->rounding[GUI_ROUNDING_PROGRESS] = 4.0f;
style->rounding[GUI_ROUNDING_CHECK] = 0.0f;
style->rounding[GUI_ROUNDING_INPUT] = 4.0f;
style->rounding[GUI_ROUNDING_INPUT] = 0.0f;
style->rounding[GUI_ROUNDING_GRAPH] = 4.0f;
style->rounding[GUI_ROUNDING_SCROLLBAR] = 5.0f;
}
@ -3945,11 +3934,11 @@ gui_style_default_color(struct gui_style *style)
style->colors[GUI_COLOR_TOGGLE] = gui_rgba(100, 100, 100, 255);
style->colors[GUI_COLOR_TOGGLE_HOVER] = gui_rgba(120, 120, 120, 255);
style->colors[GUI_COLOR_TOGGLE_CURSOR] = gui_rgba(45, 45, 45, 255);
style->colors[GUI_COLOR_SLIDER] = gui_rgba(43, 43, 43, 255);
style->colors[GUI_COLOR_SLIDER] = gui_rgba(38, 38, 38, 255);
style->colors[GUI_COLOR_SLIDER_CURSOR] = gui_rgba(100, 100, 100, 255);
style->colors[GUI_COLOR_SLIDER_CURSOR_HOVER] = gui_rgba(120, 120, 120, 255);
style->colors[GUI_COLOR_SLIDER_CURSOR_ACTIVE] = gui_rgba(150, 150, 150, 255);
style->colors[GUI_COLOR_PROGRESS] = gui_rgba(43, 43, 43, 255);
style->colors[GUI_COLOR_PROGRESS] = gui_rgba(38, 38, 38, 255);
style->colors[GUI_COLOR_PROGRESS_CURSOR] = gui_rgba(100, 100, 100, 255);
style->colors[GUI_COLOR_PROGRESS_CURSOR_HOVER] = gui_rgba(120, 120, 120, 255);
style->colors[GUI_COLOR_PROGRESS_CURSOR_ACTIVE] = gui_rgba(150, 150, 150, 255);
@ -5718,42 +5707,6 @@ gui_button_image(struct gui_context *layout, struct gui_image image,
return gui_widget_button_image(layout->buffer, bounds, image, behavior, &button, i);
}
gui_bool
gui_button_toggle(struct gui_context *layout, const char *str, gui_bool value)
{
struct gui_rect bounds;
struct gui_button_text button;
const struct gui_style *config;
const struct gui_input *i;
enum gui_widget_state state;
state = gui_button(&button.base, &bounds, layout);
if (!state) return value;
i = (state == GUI_WIDGET_ROM || layout->flags & GUI_WINDOW_ROM) ? 0 : layout->input;
config = layout->style;
button.base.border = config->colors[GUI_COLOR_BORDER];
button.alignment = GUI_TEXT_CENTERED;
if (!value) {
button.base.normal = config->colors[GUI_COLOR_BUTTON];
button.base.hover = config->colors[GUI_COLOR_BUTTON_HOVER];
button.base.active = config->colors[GUI_COLOR_BUTTON_ACTIVE];
button.normal = config->colors[GUI_COLOR_TEXT];
button.hover = config->colors[GUI_COLOR_TEXT_HOVERING];
button.active = config->colors[GUI_COLOR_TEXT_ACTIVE];
} else {
button.base.normal = config->colors[GUI_COLOR_BUTTON_ACTIVE];
button.base.hover = config->colors[GUI_COLOR_BUTTON_HOVER];
button.base.active = config->colors[GUI_COLOR_BUTTON];
button.normal = config->colors[GUI_COLOR_TEXT_ACTIVE];
button.hover = config->colors[GUI_COLOR_TEXT_HOVERING];
button.active = config->colors[GUI_COLOR_TEXT];
}
if (gui_widget_button_text(layout->buffer, bounds, str, GUI_BUTTON_DEFAULT,
&button, i, &config->font)) value = !value;
return value;
}
gui_bool
gui_button_text_symbol(struct gui_context *layout, enum gui_symbol symbol,
const char *text, enum gui_text_align align, enum gui_button_behavior behavior)
@ -6582,15 +6535,20 @@ gui_combo_begin(struct gui_context *parent, struct gui_context *combo,
{
/* button setup and execution */
struct gui_button_symbol button;
bounds.y = header.y;
bounds.h = header.h;
bounds.w = header.h;
bounds.x = header.x + header.w - bounds.w;
bounds.y = header.y + 1;
bounds.h = MAX(2, header.h);
bounds.h = bounds.h - 2;
bounds.w = bounds.h - 2;
bounds.x = (header.x + header.w) - (bounds.w+2);
gui_fill_button(config, &button.base);
button.base.rounding = 0;
button.base.border_width = 0;
button.base.padding.x = bounds.w/4.0f;
button.base.padding.y = bounds.h/4.0f;
button.base.border = config->colors[GUI_COLOR_SPINNER];
button.base.normal = config->colors[GUI_COLOR_SPINNER];
button.base.hover = config->colors[GUI_COLOR_SPINNER];
button.base.active = config->colors[GUI_COLOR_SPINNER];
button.normal = config->colors[GUI_COLOR_TEXT];
button.hover = config->colors[GUI_COLOR_TEXT_HOVERING];
button.active = config->colors[GUI_COLOR_TEXT_ACTIVE];

24
gui.h
View File

@ -977,7 +977,7 @@ const struct gui_command* gui_command_queue_next(struct gui_command_queue*,
can be interpreted by render frameworks (OpenGL, DirectX, ...).
In addition to just provide a way to convert commands the draw list has
a primitives and stateful path drawing API, which allows to draw into the
draw list as well. The actuall drawing support in addition Anti-aliasing.
draw list even with anti-aliasing.
The draw list consist internaly of three user provided buffers that will be
filled with data. The first buffer is the the draw command and temporary
@ -1642,11 +1642,13 @@ struct gui_toggle {
struct gui_vec2 padding;
/* padding between bounds and content */
struct gui_color font;
/* text color */
/* text background */
struct gui_color background;
/* text color background */
struct gui_color normal;
/* toggle normal background color*/
/* toggle cursor background normal color*/
struct gui_color hover;
/* toggle hover background color*/
/* toggle cursor background hove color*/
struct gui_color cursor;
/* toggle cursor color*/
};
@ -2054,6 +2056,7 @@ enum gui_style_colors {
GUI_COLOR_BUTTON_HOVER,
GUI_COLOR_BUTTON_ACTIVE,
GUI_COLOR_TOGGLE,
GUI_COLOR_TOGGLE_BACK,
GUI_COLOR_TOGGLE_HOVER,
GUI_COLOR_TOGGLE_CURSOR,
GUI_COLOR_SLIDER,
@ -2495,10 +2498,6 @@ struct gui_context {
/* position and size of the window in the os window */
struct gui_vec2 offset;
/* window scrollbar offset */
gui_bool is_table;
/* flag indicating if the window is currently creating a table */
gui_flags tbl_flags;
/* flags describing the line drawing for every row in the table */
gui_bool valid;
/* flag inidicating if the window is visible */
gui_float at_x, at_y, max_x;
@ -2835,7 +2834,6 @@ void gui_layout_pop(struct gui_context*);
gui_button_color -- colored button widget without content
gui_button_symbol -- button with triangle either up-/down-/left- or right
gui_button_image -- button widget width icon content
gui_button_toggle -- toggle button with either active or inactive state
gui_button_text_image -- button widget with text and icon
gui_button_text_symbol -- button widget with text and a triangle
gui_button_fitting -- button widget without border and fitting space
@ -3005,14 +3003,6 @@ gui_bool gui_button_fitting(struct gui_context *layout, const char *text,
- gui_true if the button was transistioned from unpressed to pressed with
default button behavior or pressed if repeater behavior.
*/
gui_bool gui_button_toggle(struct gui_context*, const char*,gui_bool value);
/* this function creates a toggle button which is either active or inactive
Input:
- label describing the toggle button
- current state of the toggle
Output:
- from user input updated toggle state
*/
gui_float gui_slider(struct gui_context*, gui_float min, gui_float val,
gui_float max, gui_float step);
/* this function creates a slider for value manipulation