aboutsummaryrefslogtreecommitdiff
path: root/src/controls.h
blob: 29e687a9780ded76b76925ae70d338014c18e400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef __CONTROLS_H
#define __CONTROLS_H

#include <SDL.h>

#include "osc.h"

struct Paint {
	enum Paint_Type {
		PAINT_PLAIN = 0,
		PAINT_GRAD
	} type;

	union {
		struct Paint_Plain {
			Uint32 color;
		} plain;

		struct Paint_Grad {
			SDL_Color top;
			SDL_Color bottom;
		} grad;
	} u;
};

struct Control {
	enum Control_Type {
		SLIDER = 0,
		FIELD
	} type;

	SDL_Rect geo;

	struct Control_OSC {
		char			*address;
		enum Osc_DataType	datatype;
	} OSC;

	union {
		struct Slider {
			enum Slider_Type {
				SLIDER_BUTTON = 0,
				SLIDER_SET,
				SLIDER_RELATIVE
			} type;
			#define SLIDER_TYPE	\
				"button\0"	\
				"set\0"		\
				"relative\0"

			struct Paint	paint;

			double		min;
			double		max;
			double		step;

			double		value;

			char		*label;
			Uint8		show_value;

			union {
				struct Slider_Button {
					Uint16 button_y;
				} button;
			} u;
		} slider;

		struct Field {
			enum Field_Type {
				FIELD_BUTTON = 0,
				FIELD_SWITCH
			} type;

			Uint32	color;
			Uint8	value;
			char	*label;
		} field;
	} u;
};

				/* slider inner padding is 1/2000 of its area */
#define SLIDER_PADDING(C) ((C)->geo.w*(C)->geo.h/2000)

static inline void Controls_SetSliderValue(struct Slider *slider, double value);
static inline void Controls_InitSliderButton(struct Control *c);

int Controls_Slider(SDL_Surface *s, struct Control *c);
int Controls_Field(SDL_Surface *s, struct Control *c);
int Controls_Draw(SDL_Surface *s, struct Control *c);

static inline void
Controls_SetSliderValue(struct Slider *slider, double value)
{
	slider->value = slider->step ?
			(Uint32)((value - slider->min)/slider->step+.5)*
			slider->step + slider->min : value;
}

static inline void
Controls_InitSliderButton(struct Control *c)
{
	struct Slider	*slider = &c->u.slider;
	Uint16		padding = SLIDER_PADDING(c);

	slider->u.button.button_y = c->geo.y + (c->geo.h - (padding << 1))*
					(1. - (slider->value - slider->min)/
					 (slider->max - slider->min)) - padding;
}

#endif