fixed slider step

This commit is contained in:
vurtun 2015-04-07 18:20:28 +02:00
parent f5670d319f
commit 0a7b0b44f8
4 changed files with 97 additions and 18 deletions

View File

@ -20,6 +20,11 @@ struct demo {
gui_size current;
};
struct color_picker {
gui_bool active[4];
struct gui_color color;
};
static char*
ldfile(const char* path, size_t* siz)
{
@ -165,11 +170,38 @@ message_panel(struct gui_context *ctx, struct gui_panel *panel)
gui_panel_layout(panel, 30, 2);
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_SWITCH))
fprintf(stdout, "ok pressed!\n");
if (gui_panel_button_text(panel, "cancle", GUI_BUTTON_SWITCH))
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_SWITCH))
fprintf(stdout, "cancel pressed!\n");
gui_end_panel(ctx, panel, NULL);
}
static gui_int
color_picker_panel(struct gui_context *ctx, struct gui_panel *panel, struct color_picker *picker)
{
gui_size i;
gui_int ret = -1;
gui_byte *ptr = &picker->color.r;
gui_begin_panel(ctx, panel, "Color Picker",
GUI_PANEL_HEADER|GUI_PANEL_MOVEABLE|GUI_PANEL_BORDER);
gui_panel_layout(panel, 30, 2);
for (i = 0; i < 4; ++i) {
gui_int ivalue;
gui_float fvalue = (gui_float)*ptr;
fvalue = gui_panel_slider(panel, 0, fvalue, 255.0f, 10.0f, GUI_HORIZONTAL);
ivalue = (gui_int)fvalue;
picker->active[i] = gui_panel_spinner(panel, 0, &ivalue, 255, 1, picker->active[i]);
*ptr = (gui_byte)ivalue;
ptr++;
}
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;
gui_end_panel(ctx, panel, NULL);
return ret;
}
static gui_bool
demo_panel(struct gui_context *ctx, struct gui_panel *panel, struct demo *demo)
{

View File

@ -20,10 +20,8 @@
#define WIN_WIDTH 800
#define WIN_HEIGHT 600
#define MAX_MEMORY (256 * 1024)
#define MAX_PANELS 8
#define DTIME 33
#define MAX_BUFFER 64
#define DTIME 33
#include "example.c"
@ -162,7 +160,9 @@ main(int argc, char *argv[])
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);
@ -183,6 +183,7 @@ main(int argc, char *argv[])
memory.command_percentage = 0.19f;
memset(&demo, 0, sizeof(demo));
memset(&picker, 0, sizeof(picker));
demo.tab.minimized = gui_true;
demo.spinner = 250;
demo.slider = 2.0f;
@ -197,6 +198,7 @@ main(int argc, char *argv[])
config.colors[GUI_COLOR_TEXT].a = 255;
panel = gui_panel_new(ctx, 50, 50, 500, 300, &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);
running = gui_true;
while (running) {
@ -220,6 +222,11 @@ main(int argc, char *argv[])
gui_begin(ctx, (gui_float)width, (gui_float)height);
running = demo_panel(ctx, panel, &demo);
message_panel(ctx, message);
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);
gui_panel_hide(color_panel);
}
gui_end(ctx, &output, NULL);
/* ---------------------------------------------------------*/

65
gui.c
View File

@ -407,6 +407,10 @@ gui_output_begin(struct gui_draw_buffer *buffer, const struct gui_memory *memory
vertex_size = (gui_size)((gui_float)size * SATURATE(memory->vertex_percentage));
command_size = (gui_size)((gui_float)size * SATURATE(memory->command_percentage));
assert(vertex_size > 0);
assert(command_size > 0);
assert(clip_size > 0);
cmds = (gui_byte*)memory->memory + clip_size;
aligned = ALIGN(cmds, align_cmd);
alignment = (gui_byte*)aligned - (gui_byte*)cmds;
@ -979,9 +983,11 @@ gui_float
gui_widget_slider(struct gui_draw_buffer *buffer, const struct gui_slider *slider,
const struct gui_input *in)
{
gui_float slider_range;
gui_float slider_min, slider_max;
gui_float slider_value, slider_steps;
gui_float slider_w, slider_h;
gui_float cursor_offset;
gui_float cursor_x, cursor_y;
gui_float cursor_w, cursor_h;
@ -995,12 +1001,13 @@ gui_widget_slider(struct gui_draw_buffer *buffer, const struct gui_slider *slide
slider_max = MAX(slider->min, slider->max);
slider_min = MIN(slider->min, slider->max);
slider_value = CLAMP(slider_min, slider->value, slider_max);
slider_steps = (slider_max - slider_min) / slider->step;
slider_range = slider_max - slider_min;
slider_steps = slider_range / slider->step;
cursor_w = (slider_w - 2 * slider->pad_x);
cursor_w = cursor_w / (((slider_max - slider_min) + slider->step) / slider->step);
cursor_offset = (slider_value - slider_min) / slider->step;
cursor_w = (slider_w - 2 * slider->pad_x) / (slider_steps + 1);
cursor_h = slider_h - 2 * slider->pad_y;
cursor_x = slider->x + slider->pad_x + (cursor_w * (slider_value - slider_min));
cursor_x = slider->x + slider->pad_x + (cursor_w * cursor_offset);
cursor_y = slider->y + slider->pad_y;
if (in && in->mouse_down &&
@ -1011,7 +1018,8 @@ gui_widget_slider(struct gui_draw_buffer *buffer, const struct gui_slider *slide
const float d = in->mouse_pos.x - (cursor_x + cursor_w / 2.0f);
const float pxstep = (slider_w - 2 * slider->pad_x) / slider_steps;
if (ABS(d) >= pxstep) {
slider_value += (d < 0) ? -slider->step : slider->step;
const gui_float steps = (gui_float)((gui_int)(ABS(d) / pxstep));
slider_value += (d > 0) ? (slider->step * steps) : -(slider->step * steps);
slider_value = CLAMP(slider_min, slider_value, slider_max);
cursor_x = slider->x + slider->pad_x + (cursor_w * (slider_value - slider_min));
}
@ -1025,9 +1033,11 @@ gui_float
gui_widget_slider_vertical(struct gui_draw_buffer *buffer,
const struct gui_slider *slider, const struct gui_input *in)
{
gui_float slider_range;
gui_float slider_min, slider_max;
gui_float slider_value, slider_steps;
gui_float slider_w, slider_h;
gui_float cursor_offset;
gui_float cursor_x, cursor_y;
gui_float cursor_w, cursor_h;
@ -1041,23 +1051,25 @@ gui_widget_slider_vertical(struct gui_draw_buffer *buffer,
slider_max = MAX(slider->min, slider->max);
slider_min = MIN(slider->min, slider->max);
slider_value = CLAMP(slider_min, slider->value, slider_max);
slider_steps = (slider_max - slider_min) / slider->step;
slider_range = slider_max - slider_min;
slider_steps = slider_range / slider->step;
cursor_w = (slider_w - 2 * slider->pad_x);
cursor_h = slider_h - 2 * slider->pad_y;
cursor_h = cursor_h / (((slider_max - slider_min) + slider->step) / slider->step);
cursor_y = slider->y + slider_h - slider->pad_y - (cursor_h * (slider_value - slider_min));
cursor_offset = (slider_value - slider_min) / slider->step;
cursor_x = slider->x + slider->pad_x;
cursor_w = (slider_w - 2 * slider->pad_x);
cursor_h = (slider_h - 2 * slider->pad_y) / (slider_steps + 1);
cursor_y = slider->y + slider_h - slider->pad_y - (cursor_h * cursor_offset);
if (in && in->mouse_down &&
INBOX(in->mouse_pos.x, in->mouse_pos.y, slider->x, slider->y, slider_w, slider_h) &&
INBOX(in->mouse_clicked_pos.x,in->mouse_clicked_pos.y, slider->x, slider->y,
slider_w, slider_h))
{
const float d = in->mouse_pos.y - (cursor_y + cursor_h / 2.0f);
const float pxstep = (slider_h - 2 * slider->pad_y) / slider_steps;
const gui_float d = in->mouse_pos.y - (cursor_y + cursor_h / 2.0f);
const gui_float pxstep = (slider_h - 2 * slider->pad_y) / slider_steps;
if (ABS(d) >= pxstep) {
slider_value += (d > 0) ? -slider->step : slider->step;
const gui_float steps = (gui_float)((gui_int)(ABS(d) / pxstep));
slider_value += (d > 0) ? -(slider->step * steps) : (slider->step * steps);
slider_value = CLAMP(slider_min, slider_value, slider_max);
cursor_y = slider->y + slider_h - slider->pad_y;
cursor_y -= (cursor_h * (slider_value - slider_min));
@ -1517,6 +1529,7 @@ gui_default_config(struct gui_config *config)
col_load(config->colors[GUI_COLOR_SCROLLBAR], 41, 41, 41, 255);
col_load(config->colors[GUI_COLOR_SCROLLBAR_CURSOR], 70, 70, 70, 255);
col_load(config->colors[GUI_COLOR_SCROLLBAR_BORDER], 45, 45, 45, 255);
col_load(config->colors[GUI_COLOR_SCALER], 100, 100, 100, 255);
}
void
@ -1678,6 +1691,18 @@ gui_panel_begin(struct gui_panel *panel, struct gui_draw_buffer *out,
return ret;
}
void
gui_panel_show(struct gui_panel *panel)
{
panel->flags = panel->flags & (gui_flags)(~GUI_PANEL_HIDDEN);
}
void
gui_panel_hide(struct gui_panel *panel)
{
panel->flags |= GUI_PANEL_HIDDEN;
}
void
gui_panel_layout(struct gui_panel *panel, gui_float height, gui_size cols)
{
@ -2842,9 +2867,21 @@ gui_end_panel(struct gui_context *ctx, struct gui_panel *panel,
assert(ctx);
assert(panel);
if (!ctx || !panel) return;
cpanel = (struct gui_context_panel*)panel;
gui_panel_end(panel);
if (ctx->active == cpanel && (panel->flags & GUI_PANEL_SCALEABLE) &&
(panel->flags & GUI_PANEL_SCROLLBAR)) {
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);
gui_float scaler_h = MAX(0, config->scaler_size.y - config->item_padding.y);
gui_draw_rectf(&ctx->current_buffer, scaler_x, scaler_y, scaler_w, scaler_h, col);
}
global = &ctx->global_buffer;
global->vertex_size += ctx->current_buffer.vertex_size;
global->command_size += ctx->current_buffer.command_size;

3
gui.h
View File

@ -305,6 +305,7 @@ enum gui_panel_colors {
GUI_COLOR_SCROLLBAR,
GUI_COLOR_SCROLLBAR_CURSOR,
GUI_COLOR_SCROLLBAR_BORDER,
GUI_COLOR_SCALER,
GUI_COLOR_COUNT
};
@ -399,6 +400,8 @@ gui_int gui_widget_plot(struct gui_draw_buffer*, const struct gui_plot*,
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*);
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);