-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollable.c
160 lines (127 loc) · 2.8 KB
/
scrollable.c
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
#include "main.h"
void scroll_draw(SCROLLABLE *s, int x, int y, int width, int height)
{
uint32_t c = s->content_height;
uint32_t h = height, m, dy;
if(h >= c) {
m = h;
dy = 0;
}
else {
m = (h * h) / c;
double d = (h - m);
dy = (s->d * d) + 0.5;
if(m <= 10) {
m = 10;
}
}
y += dy;
x += s->x;
if(!s->left) {
x += width - SCROLL_WIDTH;
}
drawalpha(BM_SCROLLHALFTOP, x, y, SCROLL_WIDTH, SCROLL_WIDTH / 2, s->color);
y += SCROLL_WIDTH / 2;
int y2 = y + m - SCROLL_WIDTH, x2 = x + SCROLL_WIDTH;
//drawrect(x, y, x + 1, y2, GRAY(46)); //GRAY(37)
drawrect(x + 0, y, x2 - 0, y2, s->color);
//drawrect(x2 - 1, y, x2, y2, GRAY(46)); alpha lines?
drawalpha(BM_SCROLLHALFBOT, x, y2, SCROLL_WIDTH, SCROLL_WIDTH / 2, s->color);
}
int scroll_gety(SCROLLABLE *s, int height)
{
uint32_t c = s->content_height;
if(c > height)
{
return (s->d * (double)(c - height)) + 0.5;
}
return 0;
}
_Bool scroll_mmove(SCROLLABLE *s, int x, int y, int dy, int width, int height)
{
_Bool draw = 0;
_Bool hit = inrect(x, y, s->left ? SCROLL_WIDTH : (width - SCROLL_WIDTH), 0, width, height);
if(s->mouseover != hit)
{
s->mouseover = hit;
draw = 1;
}
s->mouseover2 = inrect(x, y, 0, 0, width, height);
if(s->mousedown)
{
uint32_t c = s->content_height;
uint32_t h = height;
if(c > h)
{
uint32_t m = (h * h) / c;
double d = (h - m);
s->d = ((s->d * d) + (double)dy) / d;
if(s->d < 0.0)
{
s->d = 0.0;
}
else if(s->d >= 1.0)
{
s->d = 1.0;
}
draw = 1;
}
}
return draw;
}
_Bool scroll_mdown(SCROLLABLE *s)
{
if(s->mouseover)
{
s->mousedown = 1;
return 1;
}
return 0;
}
_Bool scroll_mright(SCROLLABLE *s)
{
return 0;
}
_Bool scroll_mwheel(SCROLLABLE *s, int height, double d)
{
if(s->mouseover2)
{
uint32_t c = s->content_height;
uint32_t h = height;
if(c > h)
{
uint32_t m = (h * h) / c;
double dd = (h - m);
s->d -= 16.0 * d / dd;;
if(s->d < 0.0)
{
s->d = 0.0;
}
else if(s->d >= 1.0)
{
s->d = 1.0;
}
return 1;
}
}
return 0;
}
_Bool scroll_mup(SCROLLABLE *s)
{
if(s->mousedown)
{
s->mousedown = 0;
return 1;
}
return 0;
}
_Bool scroll_mleave(SCROLLABLE *s)
{
if(s->mouseover)
{
s->mouseover = 0;
return 1;
}
s->mouseover2 = 0;
return 0;
}