brewpanel/sdlgui.c

changeset 412
f1a042a59b61
parent 410
e3f8a51b566a
child 415
d9b7e0705f56
equal deleted inserted replaced
411:ae85e91dcc58 412:f1a042a59b61
31 #include "lcdfont10x16.h" 31 #include "lcdfont10x16.h"
32 32
33 33
34 static SDL_Surface *pSdlGuiScrn; /* Pointer to the actual main SDL screen surface */ 34 static SDL_Surface *pSdlGuiScrn; /* Pointer to the actual main SDL screen surface */
35 static SDL_Surface *pFontGfx = NULL; /* The LCD font graphics */ 35 static SDL_Surface *pFontGfx = NULL; /* The LCD font graphics */
36 static SDL_Surface *pBgSurface; /* Pointer to the application SDL screen surface */
37 static SDL_Rect dlgrect, bgrect;
36 static int fontwidth, fontheight; /* Width & height of the actual font */ 38 static int fontwidth, fontheight; /* Width & height of the actual font */
37 TTF_Font *pFont = NULL; /* TTF font for buttons etc. */ 39 TTF_Font *pFont = NULL; /* TTF font for buttons etc. */
38 40
39 extern int my_shutdown; 41 extern int my_shutdown;
40 42
84 */ 86 */
85 int SDLGui_Init(void) 87 int SDLGui_Init(void)
86 { 88 {
87 char *Pt = NULL; 89 char *Pt = NULL;
88 90
89 SDL_Color blackWhiteColors[2] = {{255, 255, 255, 0}, {0, 0, 0, 0}}; 91 SDL_Color blackWhiteColors[2] = {{255, 255, 255, 0}, {53, 59, 61, 0}};
90 92
91 /* 93 /*
92 * Initialize the LCD font graphics: 94 * Initialize the LCD font graphics:
93 */ 95 */
94 pFontGfx = SDLGui_LoadXBM(lcdfont10x16_width, lcdfont10x16_height, lcdfont10x16_bits); 96 pFontGfx = SDLGui_LoadXBM(lcdfont10x16_width, lcdfont10x16_height, lcdfont10x16_bits);
217 } 219 }
218 220
219 221
220 222
221 /* 223 /*
224 * Draw the cursor
225 */
226 void SDLGui_Cursor(int x, int y)
227 {
228 SDL_Rect dr;
229 Uint32 color = SDL_MapRGB(pSdlGuiScrn->format, 53, 59, 61);
230
231 dr.x=x;
232 dr.y=y;
233 dr.w=fontwidth;
234 dr.h=fontheight;
235
236 SDL_FillRect(pSdlGuiScrn, &dr, color);
237 SDL_UpdateRect(pSdlGuiScrn, x, y, fontwidth, fontheight);
238 }
239
240
241
242 /*
243 * Draw a text character
244 */
245 void SDLGui_Char(int x, int y, Uint8 c, int bLight)
246 {
247 SDL_Rect sr, dr;
248 Uint32 bg;
249
250 if (bLight)
251 bg = SDL_MapRGB(pSdlGuiScrn->format,156,235, 4);
252 else
253 bg = SDL_MapRGB(pSdlGuiScrn->format, 94,147, 69);
254
255 sr.x=fontwidth*(c%16);
256 sr.y=fontheight*(c/16);
257 sr.w=fontwidth;
258 sr.h=fontheight;
259 dr.x=x;
260 dr.y=y;
261 dr.w=fontwidth;
262 dr.h=fontheight;
263
264 SDL_FillRect(pSdlGuiScrn, &dr, bg);
265 SDL_BlitSurface(pFontGfx, &sr, pSdlGuiScrn, &dr);
266 SDL_UpdateRect(pSdlGuiScrn, x, y, fontwidth, fontheight);
267 }
268
269
270
271 /*
222 * Draw a text string. 272 * Draw a text string.
223 */ 273 */
224 static void SDLGui_Text(int x, int y, const char *txt) 274 static void SDLGui_Text(int x, int y, const char *txt)
225 { 275 {
226 int i; 276 int i;
227 char c; 277 Uint8 c;
228 SDL_Rect sr, dr; 278 SDL_Rect sr, dr;
229 279
230 for (i=0; txt[i]!=0; i++) { 280 for (i=0; txt[i]!=0; i++) {
231 c = txt[i]; 281 c = txt[i] & 0xff;
232 sr.x=fontwidth*(c%16); 282 sr.x=fontwidth*(c%16);
233 sr.y=fontheight*(c/16); 283 sr.y=fontheight*(c/16);
234 sr.w=fontwidth; 284 sr.w=fontwidth;
235 sr.h=fontheight; 285 sr.h=fontheight;
236 dr.x=x+i*(fontwidth+2); 286 dr.x=x+i*(fontwidth+2);
517 } 567 }
518 568
519 569
520 570
521 /* 571 /*
522 * Show and process a dialog. Returns the button number that has been 572 * Show dialog.
523 * pressed or SDLGUI_UNKNOWNEVENT if an unsupported event occured (will be 573 */
524 * stored in parameter pEventOut). 574 int SDLGui_DoDialogInit(SGOBJ *dlg)
525 */ 575 {
526 int SDLGui_DoDialog(SGOBJ *dlg, SDL_Event *pEventOut) 576 if ((pSdlGuiScrn->h < dlg[0].h) && (pSdlGuiScrn->w < dlg[0].w)) {
527 { 577 syslog(LOG_NOTICE, "Screen size too small for dialog!");
528 int obj = 0, oldbutton = 0, retbutton = 0, i, j, b; 578 return SDLGUI_ERROR;
529 SDL_Event sdlEvent; 579 }
530 SDL_Surface *pBgSurface;
531 SDL_Rect dlgrect, bgrect;
532
533 // if (pSdlGuiScrn->h / fontheight < dlg[0].h)
534 // {
535 // syslog(LOG_NOTICE, "Screen size too small for dialog!");
536 // return SDLGUI_ERROR;
537 // }
538 580
539 dlgrect.x = dlg[0].x; 581 dlgrect.x = dlg[0].x;
540 dlgrect.y = dlg[0].y; 582 dlgrect.y = dlg[0].y;
541 dlgrect.w = dlg[0].w; 583 dlgrect.w = dlg[0].w;
542 dlgrect.h = dlg[0].h; 584 dlgrect.h = dlg[0].h;
560 syslog(LOG_NOTICE, "SDLGUI_DoDialog: CreateRGBSurface failed: %s", SDL_GetError()); 602 syslog(LOG_NOTICE, "SDLGUI_DoDialog: CreateRGBSurface failed: %s", SDL_GetError());
561 } 603 }
562 604
563 /* (Re-)draw the dialog */ 605 /* (Re-)draw the dialog */
564 SDLGui_DrawDialog(dlg); 606 SDLGui_DrawDialog(dlg);
607 return 0;
608 }
609
610
611
612 /*
613 * Process a dialog. Returns the button number that has been pressed
614 */
615 int SDLGui_DoDialogLoop(SGOBJ *dlg)
616 {
617 int obj = 0, oldbutton = 0, retbutton = 0, b, i, j;
618 SDL_Event sdlEvent;
565 619
566 /* 620 /*
567 * Is the left mouse button still pressed? Yes -> Handle TOUCHEXIT objects here 621 * Is the left mouse button still pressed? Yes -> Handle TOUCHEXIT objects here
568 */ 622 */
569 SDL_PumpEvents(); 623 SDL_PumpEvents();
586 break; 640 break;
587 641
588 case SDL_MOUSEBUTTONDOWN: 642 case SDL_MOUSEBUTTONDOWN:
589 if (sdlEvent.button.button != SDL_BUTTON_LEFT) { 643 if (sdlEvent.button.button != SDL_BUTTON_LEFT) {
590 /* Not left mouse button -> unsupported event */ 644 /* Not left mouse button -> unsupported event */
591 if (pEventOut)
592 retbutton = SDLGUI_UNKNOWNEVENT;
593 break; 645 break;
594 } 646 }
595 /* It was the left button: Find the object under the mouse cursor */ 647 /* It was the left button: Find the object under the mouse cursor */
596 obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y); 648 obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y);
597 if (obj > 0) { 649 if (obj > 0) {
606 retbutton = obj; 658 retbutton = obj;
607 } 659 }
608 } 660 }
609 break; 661 break;
610 662
611 case SDL_MOUSEBUTTONUP: 663 case SDL_MOUSEBUTTONUP:
612 if (sdlEvent.button.button != SDL_BUTTON_LEFT) { 664 if (sdlEvent.button.button != SDL_BUTTON_LEFT) {
613 /* Not left mouse button -> unsupported event */ 665 /* Not left mouse button -> unsupported event */
614 if (pEventOut)
615 retbutton = SDLGUI_UNKNOWNEVENT;
616 break; 666 break;
617 } 667 }
618 /* It was the left button: Find the object under the mouse cursor */ 668 /* It was the left button: Find the object under the mouse cursor */
619 obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y); 669 obj = SDLGui_FindObj(dlg, sdlEvent.button.x, sdlEvent.button.y);
620 if (obj > 0) { 670 if (obj > 0) {
626 } 676 }
627 } 677 }
628 if (oldbutton > 0) { 678 if (oldbutton > 0) {
629 dlg[oldbutton].state &= ~SG_SELECTED; 679 dlg[oldbutton].state &= ~SG_SELECTED;
630 SDLGui_DrawButton(dlg, oldbutton); 680 SDLGui_DrawButton(dlg, oldbutton);
631 SDL_UpdateRect(pSdlGuiScrn, (dlg[0].x+dlg[oldbutton].x)*fontwidth-2, (dlg[0].y+dlg[oldbutton].y)*fontheight-2, 681 SDL_UpdateRect(pSdlGuiScrn, dlg[0].x+dlg[oldbutton].x-2, dlg[0].y+dlg[oldbutton].y-2, dlg[oldbutton].w+4, dlg[oldbutton].h+4);
632 dlg[oldbutton].w*fontwidth+4, dlg[oldbutton].h*fontheight+4);
633 oldbutton = 0; 682 oldbutton = 0;
634 } 683 }
635 if (obj >= 0 && (dlg[obj].flags & SG_EXIT)) { 684 if (obj >= 0 && (dlg[obj].flags & SG_EXIT)) {
636 retbutton = obj; 685 retbutton = obj;
637 } 686 }
638 break; 687 break;
639 688
640 case SDL_MOUSEMOTION: 689 case SDL_MOUSEMOTION:
641 break; 690 break;
642 691
643 case SDL_KEYDOWN: /* Key pressed */ 692 case SDL_KEYDOWN: /* Key pressed */
644 if (sdlEvent.key.keysym.sym == SDLK_RETURN || sdlEvent.key.keysym.sym == SDLK_KP_ENTER) { 693 if (sdlEvent.key.keysym.sym == SDLK_RETURN || sdlEvent.key.keysym.sym == SDLK_KP_ENTER) {
645 retbutton = SDLGui_SearchFlaggedButton(dlg, SG_DEFAULT); 694 retbutton = SDLGui_SearchFlaggedButton(dlg, SG_DEFAULT);
646 } else if (sdlEvent.key.keysym.sym == SDLK_ESCAPE) { 695 } else if (sdlEvent.key.keysym.sym == SDLK_ESCAPE) {
647 retbutton = SDLGui_SearchFlaggedButton(dlg, SG_CANCEL); 696 retbutton = SDLGui_SearchFlaggedButton(dlg, SG_CANCEL);
648 } else if (pEventOut) {
649 retbutton = SDLGUI_UNKNOWNEVENT;
650 } 697 }
651 break; 698 break;
652
653 default:
654 if (pEventOut)
655 retbutton = SDLGUI_UNKNOWNEVENT;
656 break;
657 } 699 }
658 } 700 }
659 701
702 if (retbutton == SDLGUI_QUIT)
703 my_shutdown = TRUE;
704
705 return retbutton;
706 }
707
708
709
710 void SDLGui_DoDialogEnd(void)
711 {
660 /* Restore background */ 712 /* Restore background */
661 if (pBgSurface) { 713 if (pBgSurface) {
662 SDL_BlitSurface(pBgSurface, &bgrect, pSdlGuiScrn, &dlgrect); 714 SDL_BlitSurface(pBgSurface, &bgrect, pSdlGuiScrn, &dlgrect);
663 SDL_FreeSurface(pBgSurface); 715 SDL_FreeSurface(pBgSurface);
664 } 716 }
665 717 }
666 /* Copy event data of unsupported events if caller wants to have it */ 718
667 if (retbutton == SDLGUI_UNKNOWNEVENT && pEventOut) 719
668 memcpy(pEventOut, &sdlEvent, sizeof(SDL_Event)); 720
669 721 /*
670 if (retbutton == SDLGUI_QUIT) 722 * Initialize a LCD object. Set the coordinates and dimenstions. Return index.
671 my_shutdown = TRUE; 723 */
672 724 int SDLGui_LCDinit(SGOBJ *dlg, int *x, int *y, int *w, int *h, int *cols, int *rows, int lcdindex)
673 return retbutton; 725 {
674 } 726 int i, index;
675 727
676 728 /*
677 729 * Search the LCD display
730 */
731 *x = *y = *w = *h = *cols = *rows = i = index = 0;
732 for (;;) {
733 if (dlg[i].type == -1) {
734 syslog(LOG_NOTICE, "SDLGui_LCDinit() lcdindex=%d not found", lcdindex);
735 return -1;
736 }
737 if (dlg[i].type == SGLCD) {
738 if (index == lcdindex)
739 break;
740 index++;
741 }
742 i++;
743 }
744 fprintf(stdout, "SDLGui_LCDinit=%d LCD=%dx%d %dx%d\n", i, dlg[i].x, dlg[i].y, dlg[i].w, dlg[i].h);
745
746 *cols = dlg[i].w;
747 *rows = dlg[i].h;
748 *w = dlg[i].w * (fontwidth + 2) + 10;
749 *h = dlg[i].h * (fontheight + 2) + 4;
750 if (dlg[i].x == -1) {
751 *x = (dlg[0].w - *w) / 2;
752 } else {
753 *x = dlg[i].x;
754 }
755 *y = dlg[i].y;
756
757 return lcdindex;
758 }
759
760
761 /*
678 void SDLGui_LCDwrite(SGOBJ *dlg, int x, int y, Uint8 c, int lcdindex) 762 void SDLGui_LCDwrite(SGOBJ *dlg, int x, int y, Uint8 c, int lcdindex)
679 { 763 {
680 int i, index; 764 int i, index;
681 765
682 fprintf(stdout, "SDLGui_LCDwrite( , %d, %d, %c, %d)\n", x, y, c, lcdindex); 766 fprintf(stdout, "SDLGui_LCDwrite( , %d, %d, %c, %d)\n", x, y, c, lcdindex);
695 i++; 779 i++;
696 } 780 }
697 fprintf(stdout, "SDLGui_LCDwrite i=%d LCD=%dx%d\n", i, dlg[i].w, dlg[i].h); 781 fprintf(stdout, "SDLGui_LCDwrite i=%d LCD=%dx%d\n", i, dlg[i].w, dlg[i].h);
698 782
699 } 783 }
784 */
700 785
701 #endif 786 #endif
702 787

mercurial