~amavect/makeu

makeu/slider.c -rw-r--r-- 3.9 KiB
8a2a5c28amavect revise error exiting, allocation errors 1 year, 3 months ago
                                                                                
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
/* Amavect! */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <cursor.h>
#include <thread.h>
#include <keyboard.h>
#include <mouse.h>
#include "elementile.h"
#include "fns.h"
#include "components.h"

static
Point
rotpt(Point p)
{
	int t;
	t = p.x;
	p.x = -p.y;
	p.y = t;
	return p;
}

static
void
redraw(Slider *sl)
{
	Rectangle r;
	char txt[32];
	Point pfont;
	int off;
	r = sl->r;
	draw(screenbuf, sl->r, sl->bg, nil, ZP);
	if(sl->ishoriz){
		off = (int)(Dx(r) * sl->val / (sl->max - sl->min));
		draw(screenbuf, r, sl->bka, nil, Pt(-off, 0));
		draw(screenbuf, r, sl->wta, nil, Pt(-off, -Dy(r)));
	}else{
		off = Dy(r) - (int)(Dy(r) * sl->val / (sl->max - sl->min));
		draw(screenbuf, r, sl->bka, nil, Pt(0, -off));
		draw(screenbuf, r, sl->wta, nil, Pt(-Dx(r), -off));
	}
	snprint(txt, 64, sl->fmt, sl->val);
	pfont = stringsize(display->defaultfont, txt);
	pfont.x = (Dx(r) - pfont.x)/2 + r.min.x;
	pfont.y = (Dy(r) - pfont.y)/2 + r.min.y;
	string(screenbuf, pfont, display->black, ZP, display->defaultfont, txt);
}

/*
 * Allocate the images and return the minimum size based on the defaultfont.
 */
Point
sliderinit(Elementile *e)
{
	Slider *sl;
	int n;
	Point pfont;
	char txt[32];
	sl = e->aux;
	sl->bg = eallocimage(Rect(0,0,1,1), RGB24, 1, 0xAAAAAAFF);
	sl->fg = eallocimage(Rect(0,0,1,1), RGB24, 1, 0x333333FF);
	sl->bka = nil;
	sl->wta = nil;
	n = snprint(txt, 64, sl->fmt, sl->val);
	pfont = stringsize(display->defaultfont, txt);
	pfont.x = pfont.x + 2*pfont.x/n;
	pfont.y *= 2;
	return pfont;
}

/*
 * Set the new Rectangle, regenerate indicator arrows, and redraw.
 */
void
sliderresize(Elementile *e, Rectangle r)
{
	Slider *sl;
	int n, i;
	char txt[32];
	Point pfont;
	Point ptb[3] = {{1,0},{0,1},{0,-1}};
	Point ptw[3] = {{-1,0},{0,1},{0,-1}};
	Rectangle rb, rw;
	
	sl = e->aux;
	sl->r = r;
	
	freeimage(sl->bka);
	freeimage(sl->wta);
	n = snprint(txt, 64, sl->fmt, sl->val);
	pfont = stringsize(display->defaultfont, txt);
	if(sl->ishoriz){
		pfont.x /= n;
		pfont.y = (Dy(r) - pfont.y)/2;
		for(i = 0; i < nelem(ptb); i++){
			ptb[i] = rotpt(ptb[i]);
			ptw[i] = rotpt(ptw[i]);
			ptb[i].x *= pfont.x;
			ptw[i].x *= pfont.x;
			ptb[i].y *= pfont.y;
			ptw[i].y *= pfont.y;
		}
		rb = Rect(-pfont.x, 0, pfont.x, pfont.y);
		rw = Rect(-pfont.x, -pfont.y, pfont.x, 0);
	}else{
		pfont.x = (Dx(r) - pfont.x)/2;
		pfont.y /= 2;
		for(i = 0; i < nelem(ptb); i++){
			ptb[i].x *= pfont.x;
			ptw[i].x *= pfont.x;
			ptb[i].y *= pfont.y;
			ptw[i].y *= pfont.y;
		}
		rb = Rect(0, -pfont.y, pfont.x, pfont.y);
		rw = Rect(-pfont.x, -pfont.y, 0, pfont.y);
	}
	sl->bka = eallocimage(rb, RGBA32, 0, DTransparent);
	sl->wta = eallocimage(rw, RGBA32, 0, DTransparent);
	fillpoly(sl->bka, ptb, nelem(ptb), 1, display->black, ZP);
	fillpoly(sl->wta, ptw, nelem(ptw), 1, display->white, ZP);
	redraw(sl);
}

/*
 * Sanitize value and then redraw.
 */
int
sliderupdate(Elementile *e)
{
	Slider *sl;
	sl = e->aux;
	if(sl->val < sl->min)
		sl->val = sl->min;
	else if(sl->val > sl->max)
		sl->val = sl->max;
	redraw(sl);
	return 1;
}

/*
 * Take mouse 1 and scroll wheels, but do not go above max or below min.
 */
int
slidermouse(Elementile *e, Mouse m)
{
	Slider *sl;
	Rectangle rect;
	Point pt;
	sl = e->aux;
	if(m.buttons == 1){
		rect = sl->r;
		pt = ptclip(m.xy, rect);
		if(sl->ishoriz)
			sl->val = (sl->max - sl->min) * (pt.x - rect.min.x) / (rect.max.x - rect.min.x - 1);
		else
			sl->val = (sl->max - sl->min) * (rect.max.y - 1 - pt.y) / (rect.max.y - rect.min.y - 1);
		redraw(sl);
		sl->onchange(sl->val);
	}else if(m.buttons == 8){
		sl->val += sl->inc;
		if(sl->val > sl->max)
			sl->val = sl->max;
		redraw(sl);
		sl->onchange(sl->val);
	}else if(m.buttons == 16){
		sl->val -= sl->inc;
		if(sl->val < sl->min)
			sl->val = sl->min;
		redraw(sl);
		sl->onchange(sl->val);
	}
	if(m.buttons != 0)
		return 1;
	return 0;
}

/*
 * Insert functionality here.
 */
int
sliderkeyboard(Elementile*, Rune)
{
	return 0;
}