brewpanel/sdlgui.c

changeset 412
f1a042a59b61
parent 410
e3f8a51b566a
child 415
d9b7e0705f56
--- a/brewpanel/sdlgui.c	Sat Nov 07 22:42:47 2015 +0100
+++ b/brewpanel/sdlgui.c	Sun Nov 08 17:49:29 2015 +0100
@@ -33,6 +33,8 @@
 
 static SDL_Surface	*pSdlGuiScrn;            	/* Pointer to the actual main SDL screen surface 	*/
 static SDL_Surface	*pFontGfx = NULL;        	/* The LCD font graphics 				*/
+static SDL_Surface	*pBgSurface;			/* Pointer to the application SDL screen surface	*/
+static SDL_Rect		dlgrect, bgrect;
 static int		fontwidth, fontheight;		/* Width & height of the actual font 			*/
 TTF_Font                *pFont = NULL;			/* TTF font for buttons etc.				*/
 
@@ -86,7 +88,7 @@
 {
     char	*Pt = NULL;
 
-    SDL_Color blackWhiteColors[2] = {{255, 255, 255, 0}, {0, 0, 0, 0}};
+    SDL_Color blackWhiteColors[2] = {{255, 255, 255, 0}, {53, 59, 61, 0}};
 
     /* 
      * Initialize the LCD font graphics: 
@@ -219,16 +221,64 @@
 
 
 /*
+ * Draw the cursor
+ */
+void SDLGui_Cursor(int x, int y)
+{
+    SDL_Rect	dr;
+    Uint32      color = SDL_MapRGB(pSdlGuiScrn->format, 53, 59, 61);
+
+    dr.x=x;
+    dr.y=y;
+    dr.w=fontwidth;
+    dr.h=fontheight;
+
+    SDL_FillRect(pSdlGuiScrn, &dr, color);
+    SDL_UpdateRect(pSdlGuiScrn, x, y, fontwidth, fontheight);
+}
+
+
+
+/*
+ * Draw a text character
+ */
+void SDLGui_Char(int x, int y, Uint8 c, int bLight)
+{
+    SDL_Rect    sr, dr;
+    Uint32	bg;
+
+    if (bLight)
+	bg = SDL_MapRGB(pSdlGuiScrn->format,156,235,  4);
+    else
+	bg = SDL_MapRGB(pSdlGuiScrn->format, 94,147, 69);
+
+    sr.x=fontwidth*(c%16);
+    sr.y=fontheight*(c/16);
+    sr.w=fontwidth;
+    sr.h=fontheight;
+    dr.x=x;
+    dr.y=y;
+    dr.w=fontwidth;
+    dr.h=fontheight;
+
+    SDL_FillRect(pSdlGuiScrn, &dr, bg);
+    SDL_BlitSurface(pFontGfx, &sr, pSdlGuiScrn, &dr);
+    SDL_UpdateRect(pSdlGuiScrn, x, y, fontwidth, fontheight);
+}
+
+
+
+/*
  * Draw a text string.
  */
 static void SDLGui_Text(int x, int y, const char *txt)
 {
     int		i;
-    char	c;
+    Uint8	c;
     SDL_Rect	sr, dr;
 
     for (i=0; txt[i]!=0; i++) {
-	c = txt[i];
+	c = txt[i] & 0xff;
 	sr.x=fontwidth*(c%16);
 	sr.y=fontheight*(c/16);
 	sr.w=fontwidth;
@@ -519,22 +569,14 @@
 
 
 /*
- * Show and process a dialog. Returns the button number that has been
- * pressed or SDLGUI_UNKNOWNEVENT if an unsupported event occured (will be
- * stored in parameter pEventOut).
+ * Show dialog.
  */
-int SDLGui_DoDialog(SGOBJ *dlg, SDL_Event *pEventOut)
+int SDLGui_DoDialogInit(SGOBJ *dlg)
 {
-    int		obj = 0, oldbutton = 0, retbutton = 0, i, j, b;
-    SDL_Event	sdlEvent;
-    SDL_Surface	*pBgSurface;
-    SDL_Rect	dlgrect, bgrect;
-
-//	if (pSdlGuiScrn->h / fontheight < dlg[0].h)
-//	{
-//		syslog(LOG_NOTICE, "Screen size too small for dialog!");
-//		return SDLGUI_ERROR;
-//	}
+    if ((pSdlGuiScrn->h < dlg[0].h) && (pSdlGuiScrn->w < dlg[0].w)) {
+	syslog(LOG_NOTICE, "Screen size too small for dialog!");
+	return SDLGUI_ERROR;
+    }
 
     dlgrect.x = dlg[0].x;
     dlgrect.y = dlg[0].y;
@@ -562,6 +604,18 @@
 
     /* (Re-)draw the dialog */
     SDLGui_DrawDialog(dlg);
+    return 0;
+}
+
+
+
+/*
+ * Process a dialog. Returns the button number that has been pressed
+ */
+int SDLGui_DoDialogLoop(SGOBJ *dlg)
+{
+    int         obj = 0, oldbutton = 0, retbutton = 0, b, i, j;
+    SDL_Event   sdlEvent;
 
     /* 
      * Is the left mouse button still pressed? Yes -> Handle TOUCHEXIT objects here
@@ -588,8 +642,6 @@
 		case SDL_MOUSEBUTTONDOWN:
 				if (sdlEvent.button.button != SDL_BUTTON_LEFT) {
 				    /* Not left mouse button -> unsupported event */
-				    if (pEventOut)
-					retbutton = SDLGUI_UNKNOWNEVENT;
 				    break;
 				}
 				/* It was the left button: Find the object under the mouse cursor */
@@ -608,11 +660,9 @@
 				}
 				break;
 
-			 case SDL_MOUSEBUTTONUP:
+		 case SDL_MOUSEBUTTONUP:
 				if (sdlEvent.button.button != SDL_BUTTON_LEFT) {
 				    /* Not left mouse button -> unsupported event */
-				    if (pEventOut)
-					retbutton = SDLGUI_UNKNOWNEVENT;
 				    break;
 				}
 				/* It was the left button: Find the object under the mouse cursor */
@@ -628,8 +678,7 @@
 				if (oldbutton > 0) {
 				    dlg[oldbutton].state &= ~SG_SELECTED;
 				    SDLGui_DrawButton(dlg, oldbutton);
-				    SDL_UpdateRect(pSdlGuiScrn, (dlg[0].x+dlg[oldbutton].x)*fontwidth-2, (dlg[0].y+dlg[oldbutton].y)*fontheight-2,
-						dlg[oldbutton].w*fontwidth+4, dlg[oldbutton].h*fontheight+4);
+				    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);
 				    oldbutton = 0;
 				}
 				if (obj >= 0 && (dlg[obj].flags & SG_EXIT)) {
@@ -637,36 +686,19 @@
 				}
 				break;
 
-			 case SDL_MOUSEMOTION:
+		 case SDL_MOUSEMOTION:
 				break;
 
-			 case SDL_KEYDOWN:                     /* Key pressed */
+		 case SDL_KEYDOWN:                     /* Key pressed */
 				if (sdlEvent.key.keysym.sym == SDLK_RETURN || sdlEvent.key.keysym.sym == SDLK_KP_ENTER) {
 				    retbutton = SDLGui_SearchFlaggedButton(dlg, SG_DEFAULT);
 				} else if (sdlEvent.key.keysym.sym == SDLK_ESCAPE) {
 				    retbutton = SDLGui_SearchFlaggedButton(dlg, SG_CANCEL);
-				} else if (pEventOut) {
-				    retbutton = SDLGUI_UNKNOWNEVENT;
 				}
 				break;
-
-			 default:
-				if (pEventOut)
-				    retbutton = SDLGUI_UNKNOWNEVENT;
-				break;
 	    }
     }
 
-    /* Restore background */
-    if (pBgSurface) {
-	SDL_BlitSurface(pBgSurface, &bgrect, pSdlGuiScrn,  &dlgrect);
-	SDL_FreeSurface(pBgSurface);
-    }
-
-    /* Copy event data of unsupported events if caller wants to have it */
-    if (retbutton == SDLGUI_UNKNOWNEVENT && pEventOut)
-	memcpy(pEventOut, &sdlEvent, sizeof(SDL_Event));
-
     if (retbutton == SDLGUI_QUIT)
 	my_shutdown = TRUE;
 
@@ -675,6 +707,58 @@
 
 
 
+void SDLGui_DoDialogEnd(void)
+{
+    /* Restore background */
+    if (pBgSurface) {
+	SDL_BlitSurface(pBgSurface, &bgrect, pSdlGuiScrn,  &dlgrect);
+	SDL_FreeSurface(pBgSurface);
+    }
+}
+
+
+
+/*
+ * Initialize a LCD object. Set the coordinates and dimenstions. Return index.
+ */
+int  SDLGui_LCDinit(SGOBJ *dlg, int *x, int *y, int *w, int *h, int *cols, int *rows, int lcdindex)
+{
+    int		i, index;
+
+    /*
+     * Search the LCD display
+     */
+    *x = *y = *w = *h = *cols = *rows = i = index = 0;
+    for (;;) {
+	if (dlg[i].type == -1) {
+	    syslog(LOG_NOTICE, "SDLGui_LCDinit() lcdindex=%d not found", lcdindex);
+	    return -1;
+	}
+	if (dlg[i].type == SGLCD) {
+	    if (index == lcdindex)
+		break;
+	    index++;
+	}
+	i++;
+    }
+    fprintf(stdout, "SDLGui_LCDinit=%d LCD=%dx%d %dx%d\n", i, dlg[i].x, dlg[i].y, dlg[i].w, dlg[i].h);
+
+    *cols = dlg[i].w;
+    *rows = dlg[i].h;
+    *w = dlg[i].w * (fontwidth + 2) + 10;
+    *h = dlg[i].h * (fontheight + 2) + 4;
+    if (dlg[i].x == -1) {
+	*x = (dlg[0].w - *w) / 2;
+    } else {
+	*x = dlg[i].x;
+    }
+    *y = dlg[i].y;
+
+    return lcdindex;
+}
+
+
+/*
 void SDLGui_LCDwrite(SGOBJ *dlg, int x, int y, Uint8 c, int lcdindex)
 {
     int		i, index;
@@ -697,6 +781,7 @@
     fprintf(stdout, "SDLGui_LCDwrite i=%d LCD=%dx%d\n", i, dlg[i].w, dlg[i].h);
 
 }
+*/
 
 #endif
 

mercurial