forked from 0x3333/UGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ugui.c
1929 lines (1718 loc) · 46.3 KB
/
ugui.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -------------------------------------------------------------------------------- */
/* -- µGUI - Generic GUI module (C)Achim Döbler, 2015 -- */
/* -------------------------------------------------------------------------------- */
// µGUI is a generic GUI module for embedded systems.
// This is a free software that is open for education, research and commercial
// developments under license policy of following terms.
//
// Copyright (C) 2015, Achim Döbler, all rights reserved.
// URL: http://www.embeddedlightning.com/
//
// * The µGUI module is a free software and there is NO WARRANTY.
// * No restriction on use. You can use, modify and redistribute it for
// personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
// * Redistributions of source code must retain the above copyright notice.
//
/* -------------------------------------------------------------------------------- */
#include "ugui.h"
#include "ugui_fonts_data.h"
/* Static functions */
static UG_RESULT _UG_WindowDrawTitle( UG_WINDOW* wnd );
static void _UG_WindowUpdate( UG_WINDOW* wnd );
static UG_RESULT _UG_WindowClear( UG_WINDOW* wnd );
static void _UG_PutChar( char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc, const UG_FONT* font);
static const UG_COLOR pal_window[] = {
C_PAL_WINDOW
};
/* Pointer to the gui */
static UG_GUI* gui;
UG_S16 UG_Init( UG_GUI* g, UG_DEVICE *device )
{
UG_U8 i;
g->device = device;
#if defined(UGUI_USE_CONSOLE)
g->console.x_start = 4;
g->console.y_start = 4;
g->console.x_end = g->device->x_dim - g->console.x_start-1;
g->console.y_end = g->device->y_dim - g->console.x_start-1;
g->console.x_pos = g->console.x_end;
g->console.y_pos = g->console.y_end;
#endif
g->char_h_space = 1;
g->char_v_space = 1;
g->font.p = NULL;
g->font.char_height = 0;
g->font.char_width = 0;
g->font.start_char = 0;
g->font.end_char = 0;
g->font.widths = NULL;
g->desktop_color = C_DESKTOP_COLOR;
g->fore_color = C_WHITE;
g->back_color = C_BLACK;
g->next_window = NULL;
g->active_window = NULL;
g->last_window = NULL;
/* Clear drivers */
for(i=0;i<NUMBER_OF_DRIVERS;i++)
{
g->driver[i].driver = NULL;
g->driver[i].state = 0;
}
gui = g;
return 1;
}
UG_S16 UG_SelectGUI( UG_GUI* g )
{
gui = g;
return 1;
}
UG_GUI* UG_GetGUI( void )
{
return gui;
}
void UG_FontSelect( const UG_FONT* font )
{
gui->font = *font;
}
void UG_FillScreen( UG_COLOR c )
{
UG_FillFrame(0,0,gui->device->x_dim-1,gui->device->y_dim-1,c);
}
void UG_FillFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c )
{
UG_S16 n,m;
if ( x2 < x1 )
{
n = x2;
x2 = x1;
x1 = n;
}
if ( y2 < y1 )
{
n = y2;
y2 = y1;
y1 = n;
}
/* Is hardware acceleration available? */
if ( gui->driver[DRIVER_FILL_FRAME].state & DRIVER_ENABLED )
{
if( ((UG_RESULT(*)(UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c))gui->driver[DRIVER_FILL_FRAME].driver)(x1,y1,x2,y2,c) == UG_RESULT_OK ) return;
}
for( m=y1; m<=y2; m++ )
{
for( n=x1; n<=x2; n++ )
{
gui->device->pset(n,m,c);
}
}
}
void UG_FillRoundFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_S16 r, UG_COLOR c )
{
UG_S16 x,y,xd;
if ( x2 < x1 )
{
x = x2;
x2 = x1;
x1 = x;
}
if ( y2 < y1 )
{
y = y2;
y2 = y1;
y1 = y;
}
if ( r<=0 ) return;
xd = 3 - (r << 1);
x = 0;
y = r;
UG_FillFrame(x1 + r, y1, x2 - r, y2, c);
while ( x <= y )
{
if( y > 0 )
{
UG_DrawLine(x2 + x - r, y1 - y + r, x2+ x - r, y + y2 - r, c);
UG_DrawLine(x1 - x + r, y1 - y + r, x1- x + r, y + y2 - r, c);
}
if( x > 0 )
{
UG_DrawLine(x1 - y + r, y1 - x + r, x1 - y + r, x + y2 - r, c);
UG_DrawLine(x2 + y - r, y1 - x + r, x2 + y - r, x + y2 - r, c);
}
if ( xd < 0 )
{
xd += (x << 2) + 6;
}
else
{
xd += ((x - y) << 2) + 10;
y--;
}
x++;
}
}
void UG_DrawMesh( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c )
{
UG_S16 n,m;
if ( x2 < x1 )
{
n = x2;
x2 = x1;
x1 = n;
}
if ( y2 < y1 )
{
n = y2;
y2 = y1;
y1 = n;
}
for( m=y1; m<=y2; m+=2 )
{
for( n=x1; n<=x2; n+=2 )
{
gui->device->pset(n,m,c);
}
}
}
void UG_DrawFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c )
{
UG_DrawLine(x1,y1,x2,y1,c);
UG_DrawLine(x1,y2,x2,y2,c);
UG_DrawLine(x1,y1,x1,y2,c);
UG_DrawLine(x2,y1,x2,y2,c);
}
void UG_DrawRoundFrame( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_S16 r, UG_COLOR c )
{
if(r == 0)
{
UG_DrawFrame(x1, y1, x2, y2, c);
return;
}
UG_S16 n;
if ( x2 < x1 )
{
n = x2;
x2 = x1;
x1 = n;
}
if ( y2 < y1 )
{
n = y2;
y2 = y1;
y1 = n;
}
if ( r > x2 ) return;
if ( r > y2 ) return;
UG_DrawLine(x1+r, y1, x2-r, y1, c);
UG_DrawLine(x1+r, y2, x2-r, y2, c);
UG_DrawLine(x1, y1+r, x1, y2-r, c);
UG_DrawLine(x2, y1+r, x2, y2-r, c);
UG_DrawArc(x1+r, y1+r, r, 0x0C, c);
UG_DrawArc(x2-r, y1+r, r, 0x03, c);
UG_DrawArc(x1+r, y2-r, r, 0x30, c);
UG_DrawArc(x2-r, y2-r, r, 0xC0, c);
}
void UG_DrawPixel( UG_S16 x0, UG_S16 y0, UG_COLOR c )
{
gui->device->pset(x0,y0,c);
}
void UG_DrawCircle( UG_S16 x0, UG_S16 y0, UG_S16 r, UG_COLOR c )
{
UG_S16 x,y,xd,yd,e;
if ( x0<0 ) return;
if ( y0<0 ) return;
if ( r<=0 ) return;
xd = 1 - (r << 1);
yd = 0;
e = 0;
x = r;
y = 0;
while ( x >= y )
{
gui->device->pset(x0 - x, y0 + y, c);
gui->device->pset(x0 - x, y0 - y, c);
gui->device->pset(x0 + x, y0 + y, c);
gui->device->pset(x0 + x, y0 - y, c);
gui->device->pset(x0 - y, y0 + x, c);
gui->device->pset(x0 - y, y0 - x, c);
gui->device->pset(x0 + y, y0 + x, c);
gui->device->pset(x0 + y, y0 - x, c);
y++;
e += yd;
yd += 2;
if ( ((e << 1) + xd) > 0 )
{
x--;
e += xd;
xd += 2;
}
}
}
void UG_FillCircle( UG_S16 x0, UG_S16 y0, UG_S16 r, UG_COLOR c )
{
UG_S16 x,y,xd;
if ( x0<0 ) return;
if ( y0<0 ) return;
if ( r<=0 ) return;
xd = 3 - (r << 1);
x = 0;
y = r;
while ( x <= y )
{
if( y > 0 )
{
UG_DrawLine(x0 - x, y0 - y,x0 - x, y0 + y, c);
UG_DrawLine(x0 + x, y0 - y,x0 + x, y0 + y, c);
}
if( x > 0 )
{
UG_DrawLine(x0 - y, y0 - x,x0 - y, y0 + x, c);
UG_DrawLine(x0 + y, y0 - x,x0 + y, y0 + x, c);
}
if ( xd < 0 )
{
xd += (x << 2) + 6;
}
else
{
xd += ((x - y) << 2) + 10;
y--;
}
x++;
}
UG_DrawCircle(x0, y0, r,c);
}
void UG_DrawArc( UG_S16 x0, UG_S16 y0, UG_S16 r, UG_U8 s, UG_COLOR c )
{
UG_S16 x,y,xd,yd,e;
if ( x0<0 ) return;
if ( y0<0 ) return;
if ( r<=0 ) return;
xd = 1 - (r << 1);
yd = 0;
e = 0;
x = r;
y = 0;
while ( x >= y )
{
// Q1
if ( s & 0x01 ) gui->device->pset(x0 + x, y0 - y, c);
if ( s & 0x02 ) gui->device->pset(x0 + y, y0 - x, c);
// Q2
if ( s & 0x04 ) gui->device->pset(x0 - y, y0 - x, c);
if ( s & 0x08 ) gui->device->pset(x0 - x, y0 - y, c);
// Q3
if ( s & 0x10 ) gui->device->pset(x0 - x, y0 + y, c);
if ( s & 0x20 ) gui->device->pset(x0 - y, y0 + x, c);
// Q4
if ( s & 0x40 ) gui->device->pset(x0 + y, y0 + x, c);
if ( s & 0x80 ) gui->device->pset(x0 + x, y0 + y, c);
y++;
e += yd;
yd += 2;
if ( ((e << 1) + xd) > 0 )
{
x--;
e += xd;
xd += 2;
}
}
}
void UG_DrawLine( UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c )
{
UG_S16 n, dx, dy, sgndx, sgndy, dxabs, dyabs, x, y, drawx, drawy;
/* Is hardware acceleration available? */
if ( gui->driver[DRIVER_DRAW_LINE].state & DRIVER_ENABLED )
{
if( ((UG_RESULT(*)(UG_S16 x1, UG_S16 y1, UG_S16 x2, UG_S16 y2, UG_COLOR c))gui->driver[DRIVER_DRAW_LINE].driver)(x1,y1,x2,y2,c) == UG_RESULT_OK ) return;
}
dx = x2 - x1;
dy = y2 - y1;
dxabs = (dx>0)?dx:-dx;
dyabs = (dy>0)?dy:-dy;
sgndx = (dx>0)?1:-1;
sgndy = (dy>0)?1:-1;
x = dyabs >> 1;
y = dxabs >> 1;
drawx = x1;
drawy = y1;
gui->device->pset(drawx, drawy,c);
if( dxabs >= dyabs )
{
for( n=0; n<dxabs; n++ )
{
y += dyabs;
if( y >= dxabs )
{
y -= dxabs;
drawy += sgndy;
}
drawx += sgndx;
gui->device->pset(drawx, drawy,c);
}
}
else
{
for( n=0; n<dyabs; n++ )
{
x += dxabs;
if( x >= dyabs )
{
x -= dyabs;
drawx += sgndx;
}
drawy += sgndy;
gui->device->pset(drawx, drawy,c);
}
}
}
void UG_PutString( UG_S16 x, UG_S16 y, const char* str )
{
UG_S16 xp,yp;
UG_U8 cw;
char chr;
xp=x;
yp=y;
while ( *str != 0 )
{
chr = *str++;
if (chr < gui->font.start_char || chr > gui->font.end_char) continue;
if ( chr == '\n' )
{
xp = gui->device->x_dim;
continue;
}
cw = gui->font.widths ? gui->font.widths[chr - gui->font.start_char] : gui->font.char_width;
if ( xp + cw > gui->device->x_dim - 1 )
{
xp = x;
yp += gui->font.char_height+gui->char_v_space;
}
UG_PutChar(chr, xp, yp, gui->fore_color, gui->back_color);
xp += cw + gui->char_h_space;
}
}
void UG_PutChar( const char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc )
{
_UG_PutChar(chr,x,y,fc,bc,&gui->font);
}
#if defined(UGUI_USE_CONSOLE)
void UG_ConsolePutString( const char* str )
{
char chr;
UG_U8 cw;
while ( *str != 0 )
{
chr = *str;
if ( chr == '\n' )
{
gui->console.x_pos = gui->device->x_dim;
str++;
continue;
}
cw = gui->font.widths ? gui->font.widths[chr - gui->font.start_char] : gui->font.char_width;
gui->console.x_pos += cw+gui->char_h_space;
if ( gui->console.x_pos+cw > gui->console.x_end )
{
gui->console.x_pos = gui->console.x_start;
gui->console.y_pos += gui->font.char_height+gui->char_v_space;
}
if ( gui->console.y_pos+gui->font.char_height > gui->console.y_end )
{
gui->console.x_pos = gui->console.x_start;
gui->console.y_pos = gui->console.y_start;
UG_FillFrame(gui->console.x_start,gui->console.y_start,gui->console.x_end,gui->console.y_end,gui->console.back_color);
}
UG_PutChar(chr, gui->console.x_pos, gui->console.y_pos, gui->console.fore_color, gui->console.back_color);
str++;
}
}
void UG_ConsoleSetArea( UG_S16 xs, UG_S16 ys, UG_S16 xe, UG_S16 ye )
{
gui->console.x_start = xs;
gui->console.y_start = ys;
gui->console.x_end = xe;
gui->console.y_end = ye;
}
void UG_ConsoleSetForecolor( UG_COLOR c )
{
gui->console.fore_color = c;
}
void UG_ConsoleSetBackcolor( UG_COLOR c )
{
gui->console.back_color = c;
}
#endif
void UG_SetForecolor( UG_COLOR c )
{
gui->fore_color = c;
}
void UG_SetBackcolor( UG_COLOR c )
{
gui->back_color = c;
}
UG_S16 UG_GetXDim( void )
{
return gui->device->x_dim;
}
UG_S16 UG_GetYDim( void )
{
return gui->device->y_dim;
}
void UG_FontSetHSpace( UG_U16 s )
{
gui->char_h_space = s;
}
void UG_FontSetVSpace( UG_U16 s )
{
gui->char_v_space = s;
}
/* -------------------------------------------------------------------------------- */
/* -- INTERNAL FUNCTIONS -- */
/* -------------------------------------------------------------------------------- */
static void _UG_PutChar( const char chr, UG_S16 x, UG_S16 y, UG_COLOR fc, UG_COLOR bc, const UG_FONT* font )
{
UG_U16 i,j,k,xo,yo,c,bn,actual_char_width;
UG_U8 b,bt;
UG_U32 index;
#if defined(UGUI_USE_COLOR_RGB888) || defined(UGUI_USE_COLOR_RGB565)
UG_COLOR color;
#endif
void(*push_pixel)(UG_COLOR);
bt = (UG_U8)chr;
switch ( bt )
{
case 0xF6: bt = 0x94; break; // ö
case 0xD6: bt = 0x99; break; // Ö
case 0xFC: bt = 0x81; break; // ü
case 0xDC: bt = 0x9A; break; // Ü
case 0xE4: bt = 0x84; break; // ä
case 0xC4: bt = 0x8E; break; // Ä
case 0xB5: bt = 0xE6; break; // µ
case 0xB0: bt = 0xF8; break; // °
}
if (bt < font->start_char || bt > font->end_char) return;
yo = y;
bn = font->char_width;
if ( !bn ) return;
bn >>= 3;
if ( font->char_width % 8 ) bn++;
actual_char_width = (font->widths ? font->widths[bt - font->start_char] : font->char_width);
/* Is hardware acceleration available? */
if ( gui->driver[DRIVER_FILL_AREA].state & DRIVER_ENABLED )
{
push_pixel = ((void*(*)(UG_S16, UG_S16, UG_S16, UG_S16))gui->driver[DRIVER_FILL_AREA].driver)(x,y,x+actual_char_width-1,y+font->char_height-1);
if (font->font_type == FONT_TYPE_1BPP)
{
index = (bt - font->start_char)* font->char_height * bn;
for( j=0;j<font->char_height;j++ )
{
c=actual_char_width;
for( i=0;i<bn;i++ )
{
b = font->p[index++];
for( k=0;(k<8) && c;k++ )
{
if( b & 0x01 )
{
push_pixel(fc);
}
else
{
push_pixel(bc);
}
b >>= 1;
c--;
}
}
}
}
#if defined(UGUI_USE_COLOR_RGB888) || defined(UGUI_USE_COLOR_RGB565)
else if (font->font_type == FONT_TYPE_8BPP)
{
index = (bt - font->start_char)* font->char_height * font->char_width;
for( j=0;j<font->char_height;j++ )
{
for( i=0;i<actual_char_width;i++ )
{
b = font->p[index++];
color = ((((fc & 0xFF) * b + (bc & 0xFF) * (256 - b)) >> 8) & 0xFF) | //Blue component
((((fc & 0xFF00) * b + (bc & 0xFF00) * (256 - b)) >> 8) & 0xFF00) | //Green component
((((fc & 0xFF0000) * b + (bc & 0xFF0000) * (256 - b)) >> 8) & 0xFF0000); //Red component
push_pixel(color);
}
index += font->char_width - actual_char_width;
}
}
#endif
}
else
{
/*Not accelerated output*/
if (font->font_type == FONT_TYPE_1BPP)
{
index = (bt - font->start_char)* font->char_height * bn;
for( j=0;j<font->char_height;j++ )
{
xo = x;
c=actual_char_width;
for( i=0;i<bn;i++ )
{
b = font->p[index++];
for( k=0;(k<8) && c;k++ )
{
if( b & 0x01 )
{
gui->device->pset(xo,yo,fc);
}
else
{
gui->device->pset(xo,yo,bc);
}
b >>= 1;
xo++;
c--;
}
}
yo++;
}
}
#if defined(UGUI_USE_COLOR_RGB888) || defined(UGUI_USE_COLOR_RGB565)
else if (font->font_type == FONT_TYPE_8BPP)
{
index = (bt - font->start_char)* font->char_height * font->char_width;
for( j=0;j<font->char_height;j++ )
{
xo = x;
for( i=0;i<actual_char_width;i++ )
{
b = font->p[index++];
color = ((((fc & 0xFF) * b + (bc & 0xFF) * (256 - b)) >> 8) & 0xFF) | //Blue component
((((fc & 0xFF00) * b + (bc & 0xFF00) * (256 - b)) >> 8) & 0xFF00) | //Green component
((((fc & 0xFF0000) * b + (bc & 0xFF0000) * (256 - b)) >> 8) & 0xFF0000); //Red component
gui->device->pset(xo,yo,color);
xo++;
}
index += font->char_width - actual_char_width;
yo++;
}
}
#endif
}
}
#ifdef UGUI_USE_TOUCH
static void _UG_ProcessTouchData( UG_WINDOW* wnd )
{
UG_S16 xp,yp;
UG_U16 i,objcnt;
UG_OBJECT* obj;
UG_U8 objstate;
UG_U8 objtouch;
UG_U8 tchstate;
xp = gui->touch.xp;
yp = gui->touch.yp;
tchstate = gui->touch.state;
objcnt = wnd->objcnt;
for(i=0; i<objcnt; i++)
{
obj = (UG_OBJECT*)&wnd->objlst[i];
objstate = obj->state;
objtouch = obj->touch_state;
if ( !(objstate & OBJ_STATE_FREE) && (objstate & OBJ_STATE_VALID) && (objstate & OBJ_STATE_VISIBLE) && !(objstate & OBJ_STATE_REDRAW))
{
/* Process touch data */
if ( (tchstate) && xp != -1 )
{
if ( !(objtouch & OBJ_TOUCH_STATE_IS_PRESSED) )
{
objtouch |= OBJ_TOUCH_STATE_PRESSED_OUTSIDE_OBJECT | OBJ_TOUCH_STATE_CHANGED;
objtouch &= ~(OBJ_TOUCH_STATE_RELEASED_ON_OBJECT | OBJ_TOUCH_STATE_RELEASED_OUTSIDE_OBJECT);
}
objtouch &= ~OBJ_TOUCH_STATE_IS_PRESSED_ON_OBJECT;
if ( xp >= obj->a_abs.xs )
{
if ( xp <= obj->a_abs.xe )
{
if ( yp >= obj->a_abs.ys )
{
if ( yp <= obj->a_abs.ye )
{
objtouch |= OBJ_TOUCH_STATE_IS_PRESSED_ON_OBJECT;
if ( !(objtouch & OBJ_TOUCH_STATE_IS_PRESSED) )
{
objtouch &= ~OBJ_TOUCH_STATE_PRESSED_OUTSIDE_OBJECT;
objtouch |= OBJ_TOUCH_STATE_PRESSED_ON_OBJECT;
}
}
}
}
}
objtouch |= OBJ_TOUCH_STATE_IS_PRESSED;
}
else if ( objtouch & OBJ_TOUCH_STATE_IS_PRESSED )
{
if ( objtouch & OBJ_TOUCH_STATE_IS_PRESSED_ON_OBJECT )
{
objtouch |= OBJ_TOUCH_STATE_RELEASED_ON_OBJECT;
}
else
{
objtouch |= OBJ_TOUCH_STATE_RELEASED_OUTSIDE_OBJECT;
}
if ( objtouch & OBJ_TOUCH_STATE_IS_PRESSED )
{
objtouch |= OBJ_TOUCH_STATE_CHANGED;
}
objtouch &= ~(OBJ_TOUCH_STATE_PRESSED_OUTSIDE_OBJECT | OBJ_TOUCH_STATE_PRESSED_ON_OBJECT | OBJ_TOUCH_STATE_IS_PRESSED);
}
}
obj->touch_state = objtouch;
}
}
#endif
static void _UG_UpdateObjects( UG_WINDOW* wnd )
{
UG_U16 i,objcnt;
UG_OBJECT* obj;
UG_U8 objstate;
#ifdef UGUI_USE_TOUCH
UG_U8 objtouch;
#endif
/* Check each object, if it needs to be updated? */
objcnt = wnd->objcnt;
for(i=0; i<objcnt; i++)
{
obj = (UG_OBJECT*)&wnd->objlst[i];
objstate = obj->state;
#ifdef UGUI_USE_TOUCH
objtouch = obj->touch_state;
#endif
if ( !(objstate & OBJ_STATE_FREE) && (objstate & OBJ_STATE_VALID) )
{
if ( objstate & OBJ_STATE_UPDATE )
{
obj->update(wnd,obj);
}
#ifdef UGUI_USE_TOUCH
if ( (objstate & OBJ_STATE_VISIBLE) && (objstate & OBJ_STATE_TOUCH_ENABLE) )
{
if ( (objtouch & (OBJ_TOUCH_STATE_CHANGED | OBJ_TOUCH_STATE_IS_PRESSED)) )
{
obj->update(wnd,obj);
}
}
#endif
}
}
}
static void _UG_HandleEvents( UG_WINDOW* wnd )
{
UG_U16 i,objcnt;
UG_OBJECT* obj;
UG_U8 objstate;
static UG_MESSAGE msg;
msg.src = NULL;
/* Handle window-related events */
//ToDo
/* Handle object-related events */
msg.type = MSG_TYPE_OBJECT;
objcnt = wnd->objcnt;
for(i=0; i<objcnt; i++)
{
obj = (UG_OBJECT*)&wnd->objlst[i];
objstate = obj->state;
if ( !(objstate & OBJ_STATE_FREE) && (objstate & OBJ_STATE_VALID) )
{
if ( obj->event != OBJ_EVENT_NONE )
{
msg.src = obj;
msg.id = obj->type;
msg.sub_id = obj->id;
msg.event = obj->event;
wnd->cb( &msg );
obj->event = OBJ_EVENT_NONE;
}
}
}
}
/* -------------------------------------------------------------------------------- */
/* -- INTERNAL API FUNCTIONS -- */
/* -------------------------------------------------------------------------------- */
void _UG_PutText(UG_TEXT* txt)
{
UG_U16 sl,rc,wl;
UG_S16 xp,yp;
UG_S16 xs=txt->a.xs;
UG_S16 ys=txt->a.ys;
UG_S16 xe=txt->a.xe;
UG_S16 ye=txt->a.ye;
UG_U8 align=txt->align;
UG_S16 char_width=txt->font->char_width;
UG_S16 char_height=txt->font->char_height;
UG_S16 char_h_space=txt->h_space;
UG_S16 char_v_space=txt->v_space;
char chr;
char* str = txt->str;
char* c = str;
if ( txt->font->p == NULL ) return;
if ( str == NULL ) return;
if ( (ye - ys) < txt->font->char_height ) return;
rc=1;
c=str;
while ( *c != 0 )
{
if ( *c == '\n' ) rc++;
c++;
}
yp = 0;
if ( align & (ALIGN_V_CENTER | ALIGN_V_BOTTOM) )
{
yp = ye - ys + 1;
yp -= char_height*rc;
yp -= char_v_space*(rc-1);
if ( yp < 0 ) return;
}
if ( align & ALIGN_V_CENTER ) yp >>= 1;
yp += ys;
while( 1 )
{
sl=0;
c=str;
wl = 0;
while( (*c != 0) && (*c != '\n') )
{
if (*c < txt->font->start_char || *c > txt->font->end_char) {c++; continue;}
sl++;
wl += (txt->font->widths ? txt->font->widths[*c - txt->font->start_char] : char_width) + char_h_space;
c++;
}
wl -= char_h_space;
xp = xe - xs + 1;
xp -= wl;
if ( xp < 0 ) return;
if ( align & ALIGN_H_LEFT ) xp = 0;
else if ( align & ALIGN_H_CENTER ) xp >>= 1;
xp += xs;
while( (*str != '\n') )
{
chr = *str++;
if ( chr == 0 ) return;
_UG_PutChar(chr,xp,yp,txt->fc,txt->bc,txt->font);
xp += (txt->font->widths ? txt->font->widths[chr - txt->font->start_char] : char_width) + char_h_space;
}
str++;
yp += char_height + char_v_space;
}
}
UG_OBJECT* _UG_SearchObject( UG_WINDOW* wnd, UG_U8 type, UG_U8 id )
{
UG_U8 i;
UG_OBJECT* obj=(UG_OBJECT*)wnd->objlst;
for(i=0;i<wnd->objcnt;i++)
{
obj = (UG_OBJECT*)(&wnd->objlst[i]);
if ( !(obj->state & OBJ_STATE_FREE) && (obj->state & OBJ_STATE_VALID) )
{
if ( (obj->type == type) && (obj->id == id) )
{
/* Requested object found! */
return obj;
}
}
}
return NULL;
}
void _UG_DrawObjectFrame( UG_S16 xs, UG_S16 ys, UG_S16 xe, UG_S16 ye, UG_COLOR* p )
{
// Frame 0
UG_DrawLine(xs, ys , xe-1, ys , *p++);
UG_DrawLine(xs, ys+1, xs , ye-1, *p++);
UG_DrawLine(xs, ye , xe , ye , *p++);
UG_DrawLine(xe, ys , xe , ye-1, *p++);
// Frame 1
UG_DrawLine(xs+1, ys+1, xe-2, ys+1, *p++);
UG_DrawLine(xs+1, ys+2, xs+1, ye-2, *p++);
UG_DrawLine(xs+1, ye-1, xe-1, ye-1, *p++);
UG_DrawLine(xe-1, ys+1, xe-1, ye-2, *p++);
// Frame 2
UG_DrawLine(xs+2, ys+2, xe-3, ys+2, *p++);
UG_DrawLine(xs+2, ys+3, xs+2, ye-3, *p++);
UG_DrawLine(xs+2, ye-2, xe-2, ye-2, *p++);
UG_DrawLine(xe-2, ys+2, xe-2, ye-3, *p);
}
UG_OBJECT* _UG_GetFreeObject( UG_WINDOW* wnd )
{
UG_U8 i;
UG_OBJECT* obj=(UG_OBJECT*)wnd->objlst;
for(i=0;i<wnd->objcnt;i++)
{
obj = (UG_OBJECT*)(&wnd->objlst[i]);
if ( (obj->state & OBJ_STATE_FREE) && (obj->state & OBJ_STATE_VALID) )
{
/* Free object found! */
return obj;
}
}
return NULL;
}
UG_RESULT _UG_DeleteObject( UG_WINDOW* wnd, UG_U8 type, UG_U8 id )
{
UG_OBJECT* obj=NULL;
obj = _UG_SearchObject( wnd, type, id );
/* Object found? */
if ( obj != NULL )
{
/* We dont't want to delete a visible or busy object! */
if ( (obj->state & OBJ_STATE_VISIBLE) || (obj->state & OBJ_STATE_UPDATE) ) return UG_RESULT_FAIL;
obj->state = OBJ_STATE_INIT;
obj->data = NULL;
obj->event = 0;
obj->id = 0;
#ifdef UGUI_USE_TOUCH
obj->touch_state = 0;
#endif
obj->type = 0;
obj->update = NULL;
return UG_RESULT_OK;
}
return UG_RESULT_FAIL;
}
#ifdef UGUI_USE_PRERENDER_EVENT
void _UG_SendObjectPrerenderEvent( UG_WINDOW *wnd, UG_OBJECT *obj )
{
UG_MESSAGE msg;
msg.event = OBJ_EVENT_PRERENDER;
msg.type = MSG_TYPE_OBJECT;
msg.id = obj->type;
msg.sub_id = obj->id;
msg.src = obj;
wnd->cb(&msg);
}