Browse Source

add drag mode to control

master
Alexandros Theodotou 3 years ago
parent
commit
c405469472
Signed by: alex
GPG Key ID: 022EAE42313D70F3
  1. 14
      inc/ztoolkit/ztk_control.h
  2. 29
      src/ztk_control.c

14
inc/ztoolkit/ztk_control.h

@ -33,6 +33,13 @@ typedef float (*ZtkControlGetter) ( @@ -33,6 +33,13 @@ typedef float (*ZtkControlGetter) (
ZtkControl * control,
void * object);
typedef enum ZtkControlDragMode
{
ZTK_CTRL_DRAG_HORIZONTAL,
ZTK_CTRL_DRAG_VERTICAL,
ZTK_CTRL_DRAG_BOTH,
} ZtkControlDragMode;
/**
* Generic control with custom drawing.
*/
@ -55,12 +62,18 @@ typedef struct ZtkControl @@ -55,12 +62,18 @@ typedef struct ZtkControl
float min; ///< min value (eg. 1)
float max; ///< max value (eg. 180)
/** Drag mode. */
ZtkControlDragMode drag_mode;
/** Used during drags. */
double last_x;
/** Used during drags. */
double last_y;
/** Sensitivity value (default 0.007f). */
float sensitivity;
} ZtkControl;
/**
@ -77,6 +90,7 @@ ztk_control_new ( @@ -77,6 +90,7 @@ ztk_control_new (
ZtkControlGetter get_val,
ZtkControlSetter set_val,
ZtkWidgetDrawCallback draw_cb,
ZtkControlDragMode drag_mode,
void * object,
float min,
float max,

29
src/ztk_control.c

@ -77,15 +77,37 @@ update_cb ( @@ -77,15 +77,37 @@ update_cb (
ZtkApp * app = w->app;
if (w->state & ZTK_WIDGET_STATE_PRESSED)
{
double delta =
double dx =
app->prev_press_x -
app->offset_press_x;
dx = - dx;
double dy =
app->prev_press_y -
app->offset_press_y;
double delta = 0.0;
switch (self->drag_mode)
{
case ZTK_CTRL_DRAG_HORIZONTAL:
delta = dx;
break;
case ZTK_CTRL_DRAG_VERTICAL:
delta = dy;
break;
case ZTK_CTRL_DRAG_BOTH:
if (fabs (dx) > fabs (dy))
delta = dx;
else
delta = dy;
break;
default:
break;
}
SET_REAL_VAL (
REAL_VAL_FROM_CONTROL (
CLAMP (
CONTROL_VAL_FROM_REAL (GET_REAL_VAL) +
0.007f * (float) delta,
self->sensitivity * (float) delta,
0.0f, 1.0f)));
}
}
@ -114,6 +136,7 @@ ztk_control_new ( @@ -114,6 +136,7 @@ ztk_control_new (
ZtkControlGetter get_val,
ZtkControlSetter set_val,
ZtkWidgetDrawCallback draw_cb,
ZtkControlDragMode drag_mode,
void * object,
float min,
float max,
@ -125,11 +148,13 @@ ztk_control_new ( @@ -125,11 +148,13 @@ ztk_control_new (
update_cb, draw_cb,
control_free);
self->drag_mode = drag_mode;
self->getter = get_val;
self->setter = set_val;
self->object = object;
self->min = min;
self->max = max;
self->sensitivity = 0.007f;
return self;
}

Loading…
Cancel
Save