From 7fb9bbedce758c64633d309ec1dc106ff55cbeb2 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 10 Mar 2013 13:41:36 +0000
Subject: [PATCH] [Add] Custom mouse events for custome widgets

---
 src/land.c    |  2 +-
 src/map.c     | 26 +++++++++++++----
 src/toolkit.c | 78 +++++++++++++++++++++++++++++----------------------
 src/toolkit.h |  6 ++--
 4 files changed, 70 insertions(+), 42 deletions(-)

diff --git a/src/land.c b/src/land.c
index 00e46f2..ba37ad3 100644
--- a/src/land.c
+++ b/src/land.c
@@ -111,7 +111,7 @@ static void outfits(void) {
 				"Sell", outfits_sell);
 
 	window_addCust(secondary_wid, -40-BUTTON_WIDTH, 60+2*BUTTON_HEIGHT,
-				BUTTON_WIDTH, BUTTON_HEIGHT, "cstMod", 0, outfits_renderMod);
+				BUTTON_WIDTH, BUTTON_HEIGHT, "cstMod", 0, outfits_renderMod, NULL);
 
 	window_addText(secondary_wid, 40+200+20, -60,
 				80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole,
diff --git a/src/map.c b/src/map.c
index 2220624..9e3d3d5 100644
--- a/src/map.c
+++ b/src/map.c
@@ -20,9 +20,10 @@ static int map_selected = 0;
 extern StarSystem* systems_stack;
 extern int systems_nstack;
 
-static void map_render(double bx, double by, double w, double h);
 static void map_close(char* str);
 static void map_update(void);
+static void map_render(double bx, double by, double w, double h);
+static void map_mouse(Uint8 type, double mx, double my);
 
 // Open the map window.
 void map_open(void) {
@@ -50,7 +51,7 @@ void map_open(void) {
 				&gl_smallFont, &cBlack, NULL);
 
 	window_addCust(map_wid, 20, 20, MAP_WIDTH - 150, MAP_HEIGHT - 60,
-				"cstMap", 1, map_render);
+				"cstMap", 1, map_render, map_mouse);
 
 	window_addButton(map_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
 				"btnClose", "Close", map_close);
@@ -112,6 +113,12 @@ static void map_update(void) {
 // Render the map as a custom widget.
 static void map_render(double bx, double by, double w, double h) {
 	int i;
+	double x, y, r;
+	StarSystem* sys;
+
+	r = 5.;
+	x = bx - map_xpos + w/2;
+	y = by - map_ypos + h/2;
 	// Background
 	COLOUR(cBlack);
 	glBegin(GL_QUADS);
@@ -125,9 +132,18 @@ static void map_render(double bx, double by, double w, double h) {
 	for(i = 0; i < systems_nstack; i++) {
 		if(&systems_stack[i] == cur_system) COLOUR(cRadar_targ);
 		else COLOUR(cYellow);
-			gl_drawCircleInRect(bx + systems_stack[i].pos.x - map_xpos + w/2,
-						by + systems_stack[i].pos.y - map_ypos + h/2,
-						5, bx, by, w, h);
+			gl_drawCircleInRect(x + systems_stack[i].pos.x,
+						y + systems_stack[i].pos.y,
+						r, bx, by, w, h);
 	}
+	// Selected planet.
+	sys = &systems_stack[map_selected];
+	COLOUR(cRed);
+	gl_drawCircleInRect(x + sys->pos.x, y + sys->pos.y, r+3., bx, by, w, h);
+}
+
+// Map event handling.
+static void map_mouse(Uint8 type, double mx, double my) {
+
 }
 
diff --git a/src/toolkit.c b/src/toolkit.c
index 16e029c..f1a481f 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -66,6 +66,7 @@ typedef struct Widget_ {
 		struct {
 			int border;
 			void(*render) (double bx, double by, double bw, double bh);
+			void(*mouse)	(Uint8 type, double bx, double by);
 		} cst;
   } dat;
 } Widget;
@@ -129,8 +130,9 @@ static void toolkit_alertClose(char* str);
 
 // Add a button that when pressed will trigger call, passing it's name as the
 // only parameter.
-void window_addButton(const unsigned int wid, const int x, const int y, const int w,
-      const int h, char* name, char* display, void (*call)(char*)) {
+void window_addButton(const unsigned int wid, const int x, const int y,
+			const int w, const int h, char* name, 
+			char* display, void (*call)(char*)) {
   
   Window* wdw = window_wget(wid);
   Widget* wgt = window_newWidget(wdw);
@@ -200,8 +202,8 @@ void window_addImage(const unsigned int wid, const int x, const int y,
 }
 
 void window_addList(const unsigned int wid, const int x, const int y,
-      const int w, const int h, char* name, char** items, int nitems, int defitem,
-			void(*call) (char*)) {
+      const int w, const int h, char* name, char** items, int nitems,
+			int defitem, void(*call) (char*)) {
 
   Window *wdw = window_wget(wid);
   Widget* wgt = window_newWidget(wdw);
@@ -226,8 +228,8 @@ void window_addList(const unsigned int wid, const int x, const int y,
 		toolkit_nextFocus();
 }
 
-void window_addRect(const unsigned int wid, const int x, const int y, const int w,
-			const int h, char* name, glColour* colour, int border) {
+void window_addRect(const unsigned int wid, const int x, const int y,
+			const int w, const int h, char* name, glColour* colour, int border) {
 	
 	Window* wdw = window_wget(wid);
 	Widget* wgt = window_newWidget(wdw);
@@ -248,7 +250,8 @@ void window_addRect(const unsigned int wid, const int x, const int y, const int
 
 void window_addCust(const unsigned int wid, const int x, const int y,
 			const int w, const int h, char* name, const int border,
-			void(*render) (double x, double y, double w, double h)) {
+			void(*render) (double x, double y, double w, double h),
+			void(*mouse) (Uint8 type, double x, double y)) {
 	
 	Window* wdw = window_wget(wid);
 	Widget* wgt = window_newWidget(wdw);
@@ -260,6 +263,7 @@ void window_addCust(const unsigned int wid, const int x, const int y,
 	// Specific.
 	wgt->dat.cst.border = border;
 	wgt->dat.cst.render = render;
+	wgt->dat.cst.mouse	= mouse;
 
 	// Position/size.
 	wgt->w = (double)w;
@@ -336,7 +340,8 @@ unsigned int window_get(const char* wdwname) {
 }
 
 // Create a window.
-unsigned int window_create(char* name, const int x, const int y, const int w, const int h) {
+unsigned int window_create(char* name, const int x, const int y,
+			const int w, const int h) {
   if(nwindows >= mwindows) {
     // We have reached our memory limit.
     windows = realloc(windows, sizeof(Window)*(++mwindows));
@@ -783,7 +788,8 @@ static void toolkit_renderList(Widget* lst, double bx, double by) {
 	toolkit_drawOutline(x, y, lst->w, lst->h, 1., toolkit_colDark, NULL);
 
 	// Draw selected.
-	toolkit_drawRect(x, y-1.+lst->h-(1+lst->dat.lst.selected-lst->dat.lst.pos)*(gl_defFont.h+2.),
+	toolkit_drawRect(x,
+				y-1.+lst->h-(1+lst->dat.lst.selected-lst->dat.lst.pos)*(gl_defFont.h+2.),
 				lst->w, gl_defFont.h+2., &cHilight, NULL);
 
 	// Draw content.
@@ -903,36 +909,40 @@ static void toolkit_mouseEvent(SDL_Event* event) {
 
   for(i = 0; i < w->nwidgets; i++) {
     wgt = &w->widgets[i];
-    if((x > wgt->x) && (x < (wgt->x + wgt->w)) && (y > wgt->y) && (y < (wgt->y + wgt->h))) {
-      switch(event->type) {
-        case SDL_MOUSEMOTION:
-          wgt->status = WIDGET_STATUS_MOUSEOVER;
-          break;
-        case SDL_MOUSEBUTTONDOWN:
-          wgt->status = WIDGET_STATUS_MOUSEDOWN;
+    if((x > wgt->x) && (x < (wgt->x + wgt->w)) &&
+					(y > wgt->y) && (y < (wgt->y + wgt->h))) {
+			if((wgt->type == WIDGET_CUST) && wgt->dat.cst.mouse)
+				(*wgt->dat.cst.mouse)(event->type, x, y);
+			else
+      	switch(event->type) {
+        	case SDL_MOUSEMOTION:
+          	wgt->status = WIDGET_STATUS_MOUSEOVER;
+          	break;
+        	case SDL_MOUSEBUTTONDOWN:
+          	wgt->status = WIDGET_STATUS_MOUSEDOWN;
 
-					if(toolkit_isFocusable(wgt))
-						w->focus = i;
+						if(toolkit_isFocusable(wgt))
+							w->focus = i;
 
-					if(wgt->type == WIDGET_LIST)
-						toolkit_listFocus(wgt, x-wgt->x, y-wgt->y);
+						if(wgt->type == WIDGET_LIST)
+							toolkit_listFocus(wgt, x-wgt->x, y-wgt->y);
 
-					break;
-        case SDL_MOUSEBUTTONUP:
-          if(wgt->status == WIDGET_STATUS_MOUSEDOWN) {
-						if(wgt->type == WIDGET_BUTTON) {
-							if(wgt->dat.btn.fptr == NULL)
-								DEBUG("Toolkit: Button '%s' of Window '%s'"
-											"Does not have a function trigger",
-											wgt->name, w->name);
-							else (*wgt->dat.btn.fptr)(wgt->name);
-						}
-          }
-          wgt->status = WIDGET_STATUS_NORMAL;
-          break;
+						break;
+        	case SDL_MOUSEBUTTONUP:
+          	if(wgt->status == WIDGET_STATUS_MOUSEDOWN) {
+							if(wgt->type == WIDGET_BUTTON) {
+								if(wgt->dat.btn.fptr == NULL)
+									DEBUG("Toolkit: Button '%s' of Window '%s'"
+												"Does not have a function trigger",
+												wgt->name, w->name);
+								else (*wgt->dat.btn.fptr)(wgt->name);
+							}
+          	}
+          	wgt->status = WIDGET_STATUS_NORMAL;
+          	break;
       }
     } else
-     wgt->status = WIDGET_STATUS_NORMAL;
+    	wgt->status = WIDGET_STATUS_NORMAL;
   }
 }
 
diff --git a/src/toolkit.h b/src/toolkit.h
index 6213a93..1b274ba 100644
--- a/src/toolkit.h
+++ b/src/toolkit.h
@@ -6,7 +6,8 @@
 extern int toolkit;
 
 // Creation.
-unsigned int window_create(char* name, const int x, const int y, const int w, const int h);
+unsigned int window_create(char* name, const int x, const int y,
+			const int w, const int h);
 
 void window_addButton(const unsigned int wid, const int x, const int y,
       const int w, const int h, char* name, char* display,
@@ -34,7 +35,8 @@ void window_addCust(const unsigned int wid,
 			const int x, const int y, // Position.
 			const int w, const int h,	// Size.
 			char* name, const int border,
-			void(*render) (double x, double y, double w, double h));
+			void(*render) (double x, double y, double w, double h),
+			void(*mouse) (Uint8 type, double x, double y));
 
 // Popups and alerts.
 void toolkit_alert(const char* fmt, ...);