-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfgui_extra.c
292 lines (249 loc) · 7.53 KB
/
fgui_extra.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
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
/*
FemtoGUI
fgui_extra.c:
Extra's
copyright:
Copyright (c) 2020 Bastiaan van Kesteren <[email protected]>
This program comes with ABSOLUTELY NO WARRANTY; for details see the file LICENSE.
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
remarks:
*/
#include "fgui_extra.h"
#include "fgui.h"
#define FGUI_CODE
#include "fgui_helper.h"
extern fgui_t fgui;
static char *fgui_printbuffer;
static int fgui_printbufferlength;
void fgui_setprintbuffer(char* buffer, int bufferlength)
/*
Set buffer (and length) to be used by fgui_print and fgui_printline
*/
{
fgui_printbuffer = buffer;
fgui_printbufferlength = bufferlength;
}
char fgui_print(int x, int y, char *fmt, ...)
/*
Print text (printf format) at given x/y location.
Returns 1 on success, false otherwise
*/
{
if(fgui_printbuffer && fgui_printbufferlength) {
va_list argpointer;
va_start(argpointer, fmt);
FGUI_VSNPRINTF(fgui_printbuffer, fgui_printbufferlength, fmt, argpointer);
fgui_text(x, y, fgui_printbuffer);
va_end(argpointer);
return 1;
}
return 0;
}
char fgui_printline(int y, fgui_linealignment_t alignment, char invert, char *fmt, ...)
/*
Print a line of text at x/y location 0/y. This'll clear the whole line before printing the text!
Returns 1 on success, false otherwise
*/
{
int x;
if(fgui_printbuffer && fgui_printbufferlength) {
/* Get text-string */
va_list argpointer;
va_start(argpointer, fmt);
FGUI_VSNPRINTF(fgui_printbuffer, fgui_printbufferlength, fmt, argpointer);
va_end(argpointer);
/* Determine x location */
switch(alignment) {
case fgui_left:
x=0;
break;
case fgui_center:
x = fgui_strlen(fgui_printbuffer);
if(x>FGUI_SCR_W) {
x=0-((x-FGUI_SCR_W)/2);
}
else {
x = (FGUI_SCR_W/2) - (x/2);
}
break;
case fgui_right:
x = fgui_strlen(fgui_printbuffer);
if(x>FGUI_SCR_W) {
x=0-(x-FGUI_SCR_W);
}
else {
x = FGUI_SCR_W - x;
}
break;
default:
return 0;
}
/* Print text on screen */
if(invert) {
fgui.fgcolor = ~fgui.fgcolor;
}
fgui_clearregion(0, y, FGUI_SCR_W, fgui_charheight());
fgui_text(x, y, fgui_printbuffer);
if(invert) {
fgui.fgcolor = ~fgui.fgcolor;
}
return 1;
}
return 0;
}
int fgui_printblock(int y, char *text, unsigned int start, char showscrollbar)
/*
Print a block of text (text[start]) starting at x/y location 0/y.
When 'showscrollbar' > 0 a scrollbar is drawn at the right side of the screen.
Index of the next char to print is returned.
*/
{
unsigned int i,x,w;
if(showscrollbar) {
w = FGUI_SCR_W - 3;
/* 'items' argument is round up; for x/y, rounding up is done using (x + y - 1) / y */
fgui_scrollbar(w, y, FGUI_SCR_H-y, (strlen(text) + fgui_blocklength(y, showscrollbar) - 1) / fgui_blocklength(y, showscrollbar),
start / fgui_blocklength(y, showscrollbar));
}
else {
w = FGUI_SCR_W;
}
x=0;
i = start;
while(text[i]) {
fgui_char(x,y,text[i]);
x+=fgui_charwidth();
if(x+fgui_charwidth() > w) {
y+=fgui_charheight();
if(y+fgui_charheight() > FGUI_SCR_H) {
i++;
break;
}
x=0;
}
i++;
}
return i;
}
int fgui_blocklength(int y, char showscrollbar)
/*
Returns no. of chars that'll fit in one block
*/
{
if(showscrollbar) {
return ((FGUI_SCR_H-y)/fgui_charheight()) * ((FGUI_SCR_W - 3) / fgui_charwidth());
}
else {
return ((FGUI_SCR_H-y)/fgui_charheight()) * (FGUI_SCR_W / fgui_charwidth());
}
}
int fgui_linetoy(unsigned int line)
/*
Convert given y location to a line location, using the active font
*/
{
return line*fgui_charheight();
}
int fgui_lines()
/*
No. of text-lines that'll fit on the display, using the active font
*/
{
return FGUI_SCR_H/fgui_charheight();
}
char fgui_progressbar(int x, int y, int w, int h, unsigned char progress, char showText)
/*
Draw a horizontal progress bar at given x/y location of w*h pixels.
Returns 1 on success, false otherwise
*/
{
int i;
if(h > 4 && w > 4) {
/* Box */
fgui_lineh(x, y, w);
fgui_linev(x, y, h);
fgui_lineh(x, y + h - 1, w);
fgui_linev(x + w - 1, y, h);
/* Bar */
if(progress > 100) {
progress = 100;
}
fgui_clearregion(x+2, y+2, w-4, h-4);
for(i=0;i<h-4;i++) {
fgui_lineh(x+2, y+2+i, progress * ((w-4)/100.0));
}
/* Text, if there's enough room */
if(showText && h-4 > fgui_charheight() && w-4 > fgui_charwidth() * 3) {
fgui_print(x + (w/2) - fgui_charwidth(), y + (h/2) - (fgui_charheight()/2), "%d%%", progress);
}
return 1;
}
return 0;
}
char fgui_scrollbar(int x, int y, int h, unsigned int items, unsigned int location)
/*
Draw a vertical scroll bar bar at given x/y location with given height and a width of 3 pixels.
Returns 1 on success, false otherwise
*/
{
double indicatorlength;
if(h > 6) {
/* Top terminator (/\) */
fgui_pixel(x+1, y, FGUI_BLACK);
fgui_lineh(x, y+1, 3);
/* Vertical line */
fgui_linev(x+1, y+3, h-6);
/* Bottom terminator (\/) */
fgui_lineh(x, y+h-2, 3);
fgui_pixel(x+1, y+h-1, FGUI_BLACK);
/* Location indicator */
if(location >= items-1) {
location = items-1;
}
/* 'indicatorlength' is round up; for x/y, rounding up is done using (x + y - 1) / y */
indicatorlength = ((h-6.0) + items - 1)/items;
fgui_linev(x, y + 3 + (location * (h-6.0)/items), indicatorlength);
fgui_linev(x + 2, y + 3 + (location * (h-6.0)/items), indicatorlength);
return 1;
}
return 0;
}
char fgui_busyindicator(int x, int y, int w, int h, int tick)
/*
Draw busy indicator at given x/y location of w*h pixels.
Returns > 0 on success, 0 otherwise. On successive calls, use the return-value of the previous call for the 'tick' argument
*/
{
int i;
int dotspacing = ((w+3-1)/3.0);
if(tick < 1) {
tick = 1;
}
else if(tick > 3) {
tick = 2;
}
if(h > 2 && w > 5) {
for(i=0;i<3;i++) {
if(i == tick-1) {
/* Filled dot */
fgui_fillregion(x + (i*dotspacing), y, w/3, h);
}
else {
/* Empty dot */
fgui_clearregion(x + (i*dotspacing), y, w/3, h);
fgui_lineh(x + (i*dotspacing), y, w/3);
fgui_linev(x + (i*dotspacing), y, h);
fgui_lineh(x + (i*dotspacing), y + h - 1, w/3);
fgui_linev(x + (i*dotspacing) + (w/3) - 1, y, h);
}
}
tick++;
if(tick > 3) {
tick = 1;
}
return tick;
}
return 0;
}