aboutsummaryrefslogtreecommitdiffstats
path: root/src/gwin/slider.c
blob: f18c665b66ea20eb9e5c5c83678c82ecf8be70b4 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * This file is subject to the terms of the GFX License, v1.0. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://chibios-gfx.com/license.html
 */

/**
 * @file    src/gwin/slider.c
 * @brief   GWIN sub-system slider code.
 *
 * @defgroup Slider Slider
 * @ingroup GWIN
 *
 * @{
 */

#include "gfx.h"

#if (GFX_USE_GWIN && GWIN_NEED_SLIDER) || defined(__DOXYGEN__)

#include "gwin/class_gwin.h"

#ifndef GWIN_SLIDER_DEAD_BAND
	#define GWIN_SLIDER_DEAD_BAND	5
#endif

// Prototypes for slider VMT functions
static void MouseUp(GWidgetObject *gw, coord_t x, coord_t y);
static void MouseMove(GWidgetObject *gw, coord_t x, coord_t y);
static void DialMove(GWidgetObject *gw, uint16_t instance, uint16_t value);

// The button VMT table
static const gwidgetVMT sliderVMT = {
	{
		"Slider",				// The classname
		_gwidgetDestroy,		// The destroy routine
		0,						// The after-clear routine
	},
	gwinSliderDraw_Std,		// The default drawing routine
	MouseMove,				// Process mouse down events (AS MOUSEMOVE)
	MouseUp,				// Process mouse up events
	MouseMove,				// Process mouse move events
	0,						// Process toggle off events (NOT USED)
	0,						// Process toggle on events (NOT USED)
	DialMove,				// Process dial move events
	0,						// Process all events (NOT USED)
	0,						// AssignToggle (NOT USED)
	0,						// AssignDial (NOT USED)
};

static const GSliderColors GSliderDefaultColors = {
	HTML2COLOR(0x404040),		// color_edge
	HTML2COLOR(0x000000),		// color_thumb
	HTML2COLOR(0x00E000),		// color_active
	HTML2COLOR(0xE0E0E0),		// color_inactive
	HTML2COLOR(0xFFFFFF),		// color_txt
};

// Send the slider event
static void SendSliderEvent(GWidgetObject *gw) {
	GSourceListener	*	psl;
	GEvent *			pe;
	#define pse			((GEventGWinSlider *)pe)

	// Trigger a GWIN Button Event
	psl = 0;
	while ((psl = geventGetSourceListener((GSourceHandle)gw, psl))) {
		if (!(pe = geventGetEventBuffer(psl)))
			continue;
		pse->type = GEVENT_GWIN_SLIDER;
		pse->slider = (GHandle)gw;
		pse->position = ((GSliderObject *)gw)->pos;
		geventSendEvent(psl);
	}

	#undef pbe
}

// Reset the display position back to the value predicted by the saved slider position
static void ResetDisplayPos(GSliderObject *gsw) {
	if (gsw->w.g.width < gsw->w.g.height)
		gsw->dpos = gsw->w.g.height-1-((gsw->w.g.height-1)*(gsw->pos-gsw->min))/(gsw->max-gsw->min);
	else
		gsw->dpos = ((gsw->w.g.width-1)*(gsw->pos-gsw->min))/(gsw->max-gsw->min);
}

// A mouse up event
static void MouseUp(GWidgetObject *gw, coord_t x, coord_t y) {
	#define gsw		((GSliderObject *)gw)
	#define gh		((GHandle)gw)

	#if GWIN_BUTTON_LAZY_RELEASE
		// Clip to the slider
		if (x < 0) x = 0;
		else if (x >= gh->width) x = gh->width-1;
		if (y < 0) y = 0;
		else if (y >= gh->height) x = gh->height-1;
	#else
		// Are we over the slider?
		if (x < 0 || x >= gh->width || y < 0 || y >= gh->height) {
			// No - restore the slider
			ResetDisplayPos(gsw);
			gwinDraw(gh);
			return;
		}
	#endif

	// Set the new position
	if (gh->width < gh->height)
		gsw->pos = (uint16_t)((uint32_t)(gh->height-1-y-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->height-2*GWIN_SLIDER_DEAD_BAND) + gsw->min);
	else
		gsw->pos = (uint16_t)((uint32_t)(x-GWIN_SLIDER_DEAD_BAND)*(gsw->max-gsw->min)/(gh->width-2*GWIN_SLIDER_DEAD_BAND) + gsw->min);

	ResetDisplayPos(gsw);
	gwinDraw(gh);

	// Generate the event
	SendSliderEvent(gw);
	#undef gh
	#undef gsw
}

// A mouse move (or mouse down) event
static void MouseMove(GWidgetObject *gw, coord_t x, coord_t y) {
	#define gsw		((GSliderObject *)gw)

	// Determine the temporary display position (with range checking)
	if (gw->g.width < gw->g.height) {
		if (y < 0)
			gsw->dpos = 0;
		else if (y >= gw->g.height)
			gsw->dpos = gw->g.height-1;
		else
			gsw->dpos = y;
	} else {
		if (x < 0)
			gsw->dpos = 0;
		else if (x >= gw->g.width)
			gsw->dpos = gw->g.width-1;
		else
			gsw->dpos = x;
	}

	// Update the display
	gwinDraw(&gw->g);
	#undef gsw
}

// A dial move event
static void DialMove(GWidgetObject *gw, uint16_t instance, uint16_t value) {
#if GFX_USE_GINPUT && GINPUT_NEED_DIAL
	#define gsw		((GSliderObject *)gw)

	// Set the new position
	gsw->pos = (uint16_t)((uint32_t)value*(gsw->max-gsw->min)/ginputGetDialRange(instance) + gsw->min);

	ResetDisplayPos(gsw);
	gwinDraw(&gw->g);

	// Generate the event
	SendSliderEvent(gw);
	#undef gsw
#else
	(void)gw; (void)instance; (void)value;
#endif
}

GHandle gwinCreateSlider(GSliderObject *gs, coord_t x, coord_t y, coord_t width, coord_t height) {
	if (!(gs = (GSliderObject *)_gwidgetInit((GWidgetObject *)gs, x, y, width, height, sizeof(GSliderObject), &sliderVMT)))
		return 0;
	gs->c = GSliderDefaultColors;
	gs->min = 0;
	gs->max = 100;
	gs->pos = 0;
	ResetDisplayPos(gs);
	return (GHandle)gs;
}

void gwinSetSliderRange(GHandle gh, int min, int max) {
	#define gsw		((GSliderObject *)gh)

	if (gh->vmt != (gwinVMT *)&sliderVMT)
		return;

	if (min == max)		// prevent divide by 0 errors.
		max++;
	gsw->min = min;
	gsw->max = max;
	gsw->pos = min;
	ResetDisplayPos(gsw);
	#undef gsw
}

void gwinSetSliderPosition(GHandle gh, int pos) {
	#define gsw		((GSliderObject *)gh)

	if (gh->vmt != (gwinVMT *)&sliderVMT)
		return;

	if (gsw->min <= gsw->max) {
		if (pos < gsw->min) gsw->pos = gsw->min;
		else if (pos > gsw->max) gsw->pos = gsw->max;
		else gsw->pos = pos;
	} else {
		if (pos > gsw->min) gsw->pos = gsw->min;
		else if (pos < gsw->max) gsw->pos = gsw->max;
		else gsw->pos = pos;
	}
	ResetDisplayPos(gsw);
	#undef gsw
}

void gwinSetSliderColors(GHandle gh, const GSliderColors *pColors) {
	if (gh->vmt != (gwinVMT *)&sliderVMT)
		return;

	((GSliderObject *)gh)->c = *pColors;
}

void gwinSliderDraw_Std(GWidgetObject *gw, void *param) {
	#define gsw		((GSliderObject *)gw)
	(void) param;

	if (gw->g.vmt != (gwinVMT *)&sliderVMT)
		return;

	if (gw->g.width < gw->g.height) {			// Vertical slider
		if (gsw->dpos != gw->g.height-1)
			gdispFillArea(gw->g.x, gw->g.y+gsw->dpos, gw->g.width, gw->g.height - gsw->dpos, gsw->c.color_active);
		if (gsw->dpos != 0)
			gdispFillArea(gw->g.x, gw->g.y, gw->g.width, gsw->dpos, gsw->c.color_inactive);
		gdispDrawBox(gw->g.x, gw->g.y, gw->g.width, gw->g.height, gsw->c.color_edge);
		gdispDrawLine(gw->g.x, gw->g.y+gsw->dpos, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos, gsw->c.color_thumb);
		if (gsw->dpos >= 2)
			gdispDrawLine(gw->g.x, gw->g.y+gsw->dpos-2, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos-2, gsw->c.color_thumb);
		if (gsw->dpos <= gw->g.height-2)
			gdispDrawLine(gw->g.x, gw->g.y+gsw->dpos+2, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos+2, gsw->c.color_thumb);

	// Horizontal slider
	} else {
		if (gsw->dpos != gw->g.width-1)
			gdispFillArea(gw->g.x+gsw->dpos, gw->g.y, gw->g.width-gsw->dpos, gw->g.height, gsw->c.color_inactive);
		if (gsw->dpos != 0)
			gdispFillArea(gw->g.x, gw->g.y, gsw->dpos, gw->g.height, gsw->c.color_active);
		gdispDrawBox(gw->g.x, gw->g.y, gw->g.width, gw->g.height, gsw->c.color_edge);
		gdispDrawLine(gw->g.x+gsw->dpos, gw->g.y, gw->g.x+gsw->dpos, gw->g.y+gw->g.height-1, gsw->c.color_thumb);
		if (gsw->dpos >= 2)
			gdispDrawLine(gw->g.x+gsw->dpos-2, gw->g.y, gw->g.x+gsw->dpos-2, gw->g.y+gw->g.height-1, gsw->c.color_thumb);
		if (gsw->dpos <= gw->g.width-2)
			gdispDrawLine(gw->g.x+gsw->dpos+2, gw->g.y, gw->g.x+gsw->dpos+2, gw->g.y+gw->g.height-1, gsw->c.color_thumb);
	}
	gdispDrawStringBox(gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-2, gw->txt, gw->g.font, gsw->c.color_txt, justifyCenter);

	#undef gsw
}

void gwinSliderDraw_Image(GWidgetObject *gw, void *param) {
	#define gsw		((GSliderObject *)gw)
	#define gi		((gdispImage *)param)
	coord_t			z, v;

	if (gw->g.vmt != (gwinVMT *)&sliderVMT)
		return;

	if (gw->g.width < gw->g.height) {			// Vertical slider
		if (gsw->dpos != 0)							// The unfilled area
			gdispFillArea(gw->g.x, gw->g.y, gw->g.width, gsw->dpos, gsw->c.color_inactive);
		if (gsw->dpos != gw->g.height-1) {			// The filled area
			for(z=gw->g.height, v=gi->height; z > gsw->dpos;) {
				z -= v;
				if (z < gsw->dpos) {
					v -= gsw->dpos - z;
					z = gsw->dpos;
				}
				gdispImageDraw(gi, gw->g.x, gw->g.y+z, gw->g.width, v, 0, gi->height-v);
			}
		}
		gdispDrawBox(gw->g.x, gw->g.y, gw->g.width, gw->g.height, gsw->c.color_edge);
		gdispDrawLine(gw->g.x, gw->g.y+gsw->dpos, gw->g.x+gw->g.width-1, gw->g.y+gsw->dpos, gsw->c.color_thumb);

	// Horizontal slider
	} else {
		if (gsw->dpos != gw->g.width-1)				// The unfilled area
			gdispFillArea(gw->g.x+gsw->dpos, gw->g.y, gw->g.width-gsw->dpos, gw->g.height, gsw->c.color_inactive);
		if (gsw->dpos != 0) {						// The filled area
			for(z=0, v=gi->width; z < gsw->dpos; z += v) {
				if (z+v > gsw->dpos)
					v -= z+v - gsw->dpos;
				gdispImageDraw(gi, gw->g.x+z, gw->g.y, v, gw->g.height, 0, 0);
			}
		}
		gdispDrawBox(gw->g.x, gw->g.y, gw->g.width, gw->g.height, gsw->c.color_edge);
		gdispDrawLine(gw->g.x+gsw->dpos, gw->g.y, gw->g.x+gsw->dpos, gw->g.y+gw->g.height-1, gsw->c.color_thumb);
	}
	gdispDrawStringBox(gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-2, gw->txt, gw->g.font, gsw->c.color_txt, justifyCenter);

	#undef gsw
}

#endif /* GFX_USE_GWIN && GWIN_NEED_BUTTON */
/** @} */