shithub: puzzles

Download patch

ref: 9c7d5576c074bd7b9d0fe8a249df31da2df31c9e
parent: cec6a8217fa2ae90a72226959e215b663c88bc41
author: sirjofri <[email protected]>
date: Mon May 13 13:24:30 EDT 2024

trying to get it work with libcontrol

input works, just drawing doesn't work for some reason

--- a/mkfile
+++ b/mkfile
@@ -1,6 +1,6 @@
 </$objtype/mkfile
 
-CFLAGS=$CFLAGS -p -I/sys/include/npe -D__plan9__ -D__${objtype}__ -DNO_TGMATH_H -I. -D__plan9_keep_main__
+CFLAGS=$CFLAGS -p -I/sys/include/npe -D__plan9__ -D__${objtype}__ -DNO_TGMATH_H -I. # -D__plan9_keep_main__
 
 TARG=\
 	blackbox\
--- a/plan9.c
+++ b/plan9.c
@@ -1,6 +1,9 @@
 #include <npe.h>
+#include <thread.h>
 #include <draw.h>
-#include <event.h>
+#include <mouse.h>
+#include <keyboard.h>
+#include <control.h>
 
 #undef PI
 #include "puzzles.h"
@@ -17,6 +20,10 @@
 	int ncolors;
 	Point ZP;
 	int topbarh;
+	Controlset *cs;
+	Channel *c;
+	int showframe;
+	Rectangle rect;
 };
 
 frontend *fe;
@@ -82,7 +89,7 @@
 static void p9_draw_text(void *handle, int x, int y, int fonttype, int fontsize, int align, int color, const char *text)
 {
 	frontend *fe = (frontend*)handle;
-	string(fe->image, addpt(fe->ZP, Pt(x, y)), fe->colors[color], ZP, font, text);
+	string(fe->image, Pt(x, y), fe->colors[color], ZP, font, text);
 }
 
 static void
@@ -89,7 +96,7 @@
 p9_draw_rect(void *handle, int x, int y, int w, int h, int color)
 {
 	frontend *fe = (frontend*)handle;
-	draw(fe->image, rectaddpt(Rect(x, y, x+w, y+h), fe->ZP), fe->colors[color], nil, ZP);
+	draw(fe->image, Rect(x, y, x+w, y+h), fe->colors[color], nil, ZP);
 }
 
 static void
@@ -96,7 +103,7 @@
 p9_draw_line(void *handle, int x1, int y1, int x2, int y2, int color)
 {
 	frontend *fe = (frontend*)handle;
-	line(fe->image, addpt(fe->ZP, Pt(x1, y1)), addpt(fe->ZP, Pt(x2, y2)), Endsquare, Endsquare, 1, fe->colors[color], ZP);
+	line(fe->image, Pt(x1, y1), Pt(x2, y2), Endsquare, Endsquare, 1, fe->colors[color], ZP);
 }
 
 static void
@@ -103,7 +110,7 @@
 p9_draw_thick_line(void *handle, float thickness, float x1, float y1, float x2, float y2, int color)
 {
 	frontend *fe = (frontend*)handle;
-	line(fe->image, addpt(fe->ZP, Pt(x1, y1)), addpt(fe->ZP, Pt(x2, y2)), Endsquare, Endsquare, thickness, fe->colors[color], ZP);
+	line(fe->image, Pt(x1, y1), Pt(x2, y2), Endsquare, Endsquare, thickness, fe->colors[color], ZP);
 }
 
 static void
@@ -114,14 +121,14 @@
 	
 	points = malloc(npoints * sizeof(Point));
 	for (int i = 0; i < npoints; i++) {
-		points[i].x = coords[i*2+0] + fe->ZP.x;
-		points[i].y = coords[i*2+1] + fe->ZP.y;
+		points[i].x = coords[i*2+0];
+		points[i].y = coords[i*2+1];
 	}
 	
 	if (fillcolor > 0)
-		fillpoly(fe->image, points, npoints, 0, fe->colors[fillcolor], fe->ZP);
+		fillpoly(fe->image, points, npoints, 0, fe->colors[fillcolor], ZP);
 	if (outlinecolor > 0)
-		poly(fe->image, points, npoints, Endsquare, Endsquare, 1, fe->colors[outlinecolor], fe->ZP);
+		poly(fe->image, points, npoints, Endsquare, Endsquare, 1, fe->colors[outlinecolor], ZP);
 	
 	free(points);
 }
@@ -130,7 +137,7 @@
 p9_draw_circle(void *handle, int cx, int cy, int radius, int fillcolor, int outlinecolor)
 {
 	frontend *fe = (frontend*)handle;
-	Point c = addpt(fe->ZP, Pt(cx, cy));
+	Point c = Pt(cx, cy);
 	fillellipse(fe->image, c, radius, radius, fe->colors[fillcolor], ZP);
 	ellipse(fe->image, c, radius, radius, 0, fe->colors[outlinecolor], ZP);
 }
@@ -158,6 +165,8 @@
 static void
 p9_end_draw(void *handle)
 {
+	frontend* fe = (frontend*)handle;
+	chanprint(fe->cs->ctl, "c_game image frame");
 }
 
 static void
@@ -164,6 +173,10 @@
 p9_status_bar(void *handle, const char *text)
 {
 	frontend *fe = (frontend*)handle;
+	
+	chanprint(fe->cs->ctl, "l_status value %q", text);
+	
+	return;
 	draw(fe->image, Rect(fe->ZP.x+10, fe->image->r.max.y-20, fe->image->r.max.x, fe->image->r.max.y), fe->background, nil, ZP);
 	string(fe->image, Pt(fe->ZP.x+10, fe->image->r.max.y-20), display->black, ZP, font, text);
 }
@@ -234,8 +247,55 @@
 }
 
 void
-initfe(frontend *fe)
+initui(Controlset *cs, Channel *c)
 {
+	Control *b_game, *b_settings, *c_game, *c_settings, *stackmain, *menu;
+	Control *l_status;
+	Point p;
+	
+	menu = createrow(cs, "rowmain");
+	chanprint(cs->ctl, "rowmain border 1");
+	
+	stackmain = createstack(cs, "stackmain");
+	chanprint(cs->ctl, "stackmain border 1");
+	controlwire(stackmain, "event", c);
+	
+	b_game = createtextbutton(cs, "b_game");
+	p = stringsize(font, "game");
+	chanprint(cs->ctl, "b_game border 1");
+	chanprint(cs->ctl, "b_game align center");
+	chanprint(cs->ctl, "b_game text game");
+	chanprint(cs->ctl, "b_game image i_white");
+	chanprint(cs->ctl, "b_game light i_black");
+	chanprint(cs->ctl, "b_game size %d %d 100 %d", p.x, p.y, p.y);
+	c_game = createbox(cs, "c_game");
+	chanprint(cs->ctl, "c_game image frame");
+	chanprint(cs->ctl, "c_game border 1");
+	controlwire(c_game, "event", c);
+	controlwire(b_game, "event", c);
+	
+	b_settings = createtextbutton(cs, "b_settings");
+	p = stringsize(font, "settings");
+	chanprint(cs->ctl, "b_settings text settings");
+	chanprint(cs->ctl, "b_settings border 1");
+	chanprint(cs->ctl, "b_settings size %d %d 100 %d", p.x, p.y, p.y);
+	c_settings = createcolumn(cs, "c_settings");
+	controlwire(c_settings, "event", c);
+	controlwire(b_settings, "event", c);
+	
+	l_status = createlabel(cs, "l_status");
+	
+	chanprint(cs->ctl, "stackmain add c_game c_settings");
+	chanprint(cs->ctl, "rowmain add b_game\nrowmain add b_settings");
+	
+	activate(b_game);
+	activate(b_settings);
+	activate(c_game);
+}
+
+void
+initfe(frontend *fe, Channel *c)
+{
 	float *colors;
 	int ncolors;
 	int r, g, b;
@@ -243,7 +303,7 @@
 	float bgcol[3];
 	
 	fe->topbarh = 20;
-	fe->image = screen;
+	fe->image = allocimage(display, screen->r, screen->chan, 0, 0);
 	fe->ZP = screen->r.min;
 	fe->ZP.y += fe->topbarh;
 	
@@ -259,6 +319,15 @@
 		fe->colors[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, rgb2col(r, g, b));
 	}
 	free(colors);
+	
+	fe->cs = newcontrolset(fe->image, nil, nil, nil);
+	fe->cs->clicktotype = 1;
+	fe->c = c;
+	ctldeletequits = 1;
+	namectlimage(fe->image, "frame");
+	namectlimage(display->black, "i_black");
+	namectlimage(display->white, "i_white");
+	initui(fe->cs, fe->c);
 }
 
 void
@@ -272,45 +341,44 @@
 }
 
 void
-redrawui(void)
+resizecontrolset(Controlset *cs)
 {
-	Point to = fe->ZP;
-	to.x = fe->image->r.max.x;
-	line(fe->image, fe->ZP, to, Endsquare, Endsquare, 0, display->black, ZP);
-}
-
-void
-eresized(int new)
-{
-	if (new && getwindow(display, Refnone) < 0) {
-		sysfatal("can't reattach to window: %r");
+	Rectangle rmenu, rarea, sarea;
+	Control *ctl;
+	
+	if (getwindow(display, Refnone) < 0) {
+		sysfatal("resize failed: %r");
 	}
 	
-	fe->image = screen;
-	fe->ZP = screen->r.min;
-	fe->ZP.y += fe->topbarh;
+	rmenu = screen->r;
+	rmenu.max.y = rmenu.min.y + 16;
+	rarea = screen->r;
+	rarea.min.y = rmenu.min.y + 16;
+	rarea.max.y = rarea.max.y - 16;
+	sarea = screen->r;
+	sarea.min.y = sarea.max.y - 16;
+	
+	if (fe->image) {
+		freeimage(fe->image);
+		fe->image = nil;
+	}
+	fe->image = allocimage(display, screen->r, screen->chan, 0, 0);
+	fe->rect = rarea;
 	draw(screen, screen->r, fe->background, nil, ZP);
 	resize();
 	midend_redraw(fe->me);
-	redrawui();
+	
+	chanprint(cs->ctl, "rowmain rect %R\nrowmain show", rmenu);
+	chanprint(cs->ctl, "c_game rect %R\nc_settings rect %R", rarea, rarea);
+	chanprint(cs->ctl, "stackmain rect %R\nstackmain show", rarea);
+	chanprint(cs->ctl, "stackmain reveal %d", fe->showframe);
+	chanprint(cs->ctl, "l_status rect %R\nl_status show", sarea);
+	
+	ctl = controlcalled("c_game");
+	fe->ZP = ctl->rect.min;
 }
 
 void
-keyboardev(int c, Event *ev)
-{
-	switch (c) {
-	case 'q':
-	case 127: /* DEL */
-		exits(nil);
-		break;
-	default:
-		if (c >= 0 && midend_process_key(fe->me, 0, 0, c) == PKR_QUIT) {
-			exits(nil);
-		}
-	}
-}
-
-void
 printoptions(config_item *c)
 {
 	char *t;
@@ -391,30 +459,35 @@
 }
 
 void
-hittopbar(int x, int buttons)
+showframe(int frame)
 {
-	if (buttons&1) {
-		print("left mouse\n");
-	} else
-	if (buttons&2) {
-		print("middle mouse\n");
-	} else
-	if (buttons&4) {
-		print("right mouse\n");
-	} else /* no button at all */
-		return;
-	print("pos: %d\n", x);
+	fe->showframe = frame;
+	chanprint(fe->cs->ctl, "stackmain reveal %d", frame);
 }
 
+int
+keyev(int k)
+{
+	switch (k) {
+	case 'q':
+	case 127:
+		return 1; /* return 1 to quit */
+	default:
+		if (k >= 0 && midend_process_key(fe->me, 0, 0, k) == PKR_QUIT)
+			return 1;
+	}
+	return 0;
+}
+
 void
-main(int argc, char **argv)
+threadmain(int argc, char **argv)
 {
-	Event ev;
-	int e;
-	int x, y;
-	char *s;
+	int x, y, n;
+	long l;
+	char *s, *args[6];
 	int doprintoptions = 0;
 	char *wintitle;
+	Channel *c;
 	config_item *cfg;
 	int changedprefs = 0;
 	
@@ -453,30 +526,48 @@
 		sysfatal("initdraw failed: %r");
 	}
 	
-	initfe(fe);
+	initcontrols();
+	
+	c = chancreate(sizeof(char*), 0);
+	initfe(fe, c);
 	midend_new_game(fe->me);
-	einit(Emouse|Ekeyboard);
-	eresized(0);
+	resizecontrolset(fe->cs);
 	
 	for (;;) {
-		switch (event(&ev)) {
-		case Emouse:
-			x = ev.mouse.xy.x - fe->ZP.x;
-			y = ev.mouse.xy.y - fe->ZP.y;
-			if (y < fe->topbarh) {
-				hittopbar(x, ev.mouse.buttons);
-			}
-			if (ev.mouse.buttons&1) {
-				midend_process_key(fe->me, x, y, LEFT_BUTTON);
+		s = recvp(fe->c);
+		n = tokenize(s, args, nelem(args));
+		
+		if (strcmp(args[0], "c_settings:") == 0) {
+			print("c_settings event: %s\n", args[1]);
+		} else
+		if (strcmp(args[0], "c_game:") == 0) {
+			if (strcmp(args[1], "mouse") == 0) {
+				x = atoi(args[2]+1) - fe->ZP.x; /* ignore '[' */
+				y = atoi(args[3]) - fe->ZP.y;
+				n = atoi(args[4]);
+				if (n&1)
+					midend_process_key(fe->me, x, y, LEFT_BUTTON);
+				if (n&2)
+					midend_process_key(fe->me, x, y, MIDDLE_BUTTON);
+				if (n&4)
+					midend_process_key(fe->me, x, y, RIGHT_BUTTON);
 			} else
-			if (ev.mouse.buttons&2) {
-				midend_process_key(fe->me, x, y, MIDDLE_BUTTON);
+			if (strcmp(args[1], "key") == 0) {
+				l = strtol(args[2], nil, 0);
+				if (keyev(l))
+					break;
 			} else
-			if (ev.mouse.buttons&4) {
-				midend_process_key(fe->me, x, y, RIGHT_BUTTON);
-			}
-		case Ekeyboard:
-			keyboardev(ev.kbdc, &ev);
-		}
+			print("c_game event: %s\n", args[1]);
+		} else
+		if (strcmp(args[0], "b_game:") == 0) {
+			print("b_game pressed\n");
+			showframe(0);
+		} else
+		if (strcmp(args[0], "b_settings:") == 0) {
+			print("b_settings pressed\n");
+			showframe(1);
+		} else
+			print("event from %s: %s\n", args[0], args[1]);
 	}
+	threadexitsall(nil);
 }