shithub: asif

Download patch

ref: e3f2bce8fd80c286f2dba6d063ad1df4d2d0e51d
parent: d646db5e552bd420f992294e72f9fbbf98ad532d
author: qwx <qwx@sciops.net>
date: Thu Oct 6 18:48:03 EDT 2022

path(1): implement panning and show more information on HUD

--- a/app/path/client.c
+++ b/app/path/client.c
@@ -11,7 +11,7 @@
 #include "dat.h"
 #include "fns.h"
 
-int	(*mousefn)(Mouse);
+int	(*mousefn)(Mouse, Point);
 int	(*keyfn)(Rune);
 
 static Keyboardctl *kc;
@@ -29,6 +29,7 @@
 evloop(void)
 {
 	Rune r;
+	Mouse mold;
 
 	enum{
 		Aresize,
@@ -48,10 +49,12 @@
 			if(getwindow(display, Refnone) < 0)
 				sysfatal("resize failed: %r");
 			resetdrw();
+			mold = mc->Mouse;
 			break;
 		case Amouse:
-			if(mousefn(mc->Mouse))
+			if(mousefn(mc->Mouse, subpt(mc->Mouse.xy, mold.xy)))
 				updatedrw(0);
+			mold = mc->Mouse;
 			break;
 		case Akbd:
 			keyfn(r);
--- a/app/path/drw.c
+++ b/app/path/drw.c
@@ -11,6 +11,7 @@
 Node *selected;
 int showgrid;
 int nodesz = 1;
+Point pan;
 
 typedef Vertex Point;
 typedef Vectangle Rectangle;
@@ -42,10 +43,17 @@
 	return i;
 }
 
+void
+dopan(Point p)
+{
+	pan.x -= p.x;
+	pan.y -= p.y;
+}
+
 Node *
 scrselect(Point p)
 {
-	p = subpt(addpt(subpt(p, screen->r.min), viewΔ), Pt(1,1));
+	p = addpt(subpt(addpt(subpt(p, screen->r.min), viewΔ), Pt(1,1)), pan);
 	if(!ptinrect(p, viewr)){
 		selected = nil;
 		return nil;
@@ -60,7 +68,7 @@
 static void
 flushdrw(void)
 {
-	draw(screen, screen->r, view, nil, viewΔ);
+	draw(screen, screen->r, view, nil, addpt(viewΔ, pan));
 	flushimage(display, 1);
 }
 
@@ -67,19 +75,40 @@
 static void
 drawhud(void)
 {
-	char s[64], *sp;
+	char s[128], *sp;
 	Node *n;
+	Point p;
 
 	draw(screen, hudr, col[Cbg], nil, ZP);
-	sp = seprint(s, s+sizeof s, "grid size: %d,%d", viewr.max.x-1, viewr.max.y-1);
+	sp = seprint(s, s+sizeof s, "grid size: %dx%d (x%d)", gridwidth, gridheight, nodesz);
 	if((n = selected) != nil){
 		assert(n >= grid && n < grid + gridwidth * gridheight);
-		sp = seprint(sp, s+sizeof s, " selected: %P%s",
-			Pt((n-grid) % gridwidth, (n-grid) / gridwidth),
-			isblocked(n) ? ": blocked" : "");
+		sp = seprint(sp, s+sizeof s, " selected: %P ",
+			Pt((n-grid) % gridwidth, (n-grid) / gridwidth));
+		if(isblocked(n))
+			sp = strecpy(sp, s+sizeof s, "B");
+		else if(n == start)
+			sp = strecpy(sp, s+sizeof s, "s");
+		else if(n == goal)
+			sp = strecpy(sp, s+sizeof s, "g");
+		else if(n->to != nil)
+			sp = strecpy(sp, s+sizeof s, "p");
+		else if(n->closed)
+			sp = strecpy(sp, s+sizeof s, "C");
+		else if(n->open)
+			sp = strecpy(sp, s+sizeof s, "O");
 	}
 	USED(sp);
-	string(screen, addpt(screen->r.min, subpt(Pt(2, viewr.max.y+2), viewΔ)), col[Cfree], ZP, font, s);
+	p = addpt(screen->r.min, subpt(Pt(2, viewr.max.y+2), viewΔ));
+	string(screen, p, col[Cfree], ZP, font, s);
+	if(start != nil && goal != nil){
+		seprint(s, s+sizeof s,
+			"path len=%d Δ=%.2f $=%.2f opened=%d expanded=%d updated=%d closed=%d",
+			stats.steps, stats.dist, stats.cost, stats.touched, stats.opened,
+			stats.updated, stats.closed);
+		p.y += font->height;
+		string(screen, p, col[Cfree], ZP, font, s);
+	}
 }
 
 static void
@@ -123,7 +152,6 @@
 			continue;
 		p1 = addpt(n2s(n), Pt(nodesz / 2, nodesz / 2));
 		p0 = addpt(n2s(n->from), Pt(nodesz / 2, nodesz / 2));
-		//line(view, p0, p1, Endarrow, 0, 0, col[Cgrid], ZP);
 		line(view, p0, p1, 0, 0, 0, col[Cgrid], ZP);
 	}
 }
@@ -175,8 +203,10 @@
 {
 	viewr = Rpt(ZP, Pt(gridwidth*nodesz+1, gridheight*nodesz+1));
 	viewΔ = divpt(addpt(subpt(ZP, subpt(screen->r.max, screen->r.min)), viewr.max), 2);
+	if(-viewΔ.y < font->height * 2)
+		viewΔ.y = 0;
 	hudr.min = addpt(screen->r.min, subpt(Pt(2, viewr.max.y+2), viewΔ));
-	hudr.max = addpt(hudr.min, Pt(screen->r.max.x, font->height*3));
+	hudr.max = addpt(hudr.min, Pt(screen->r.max.x, font->height*2));
 	freeimage(view);
 	view = eallocimage(viewr, 0, DNofill);
 	updatedrw(1);
@@ -199,8 +229,9 @@
 	col[Cstart] = eallocimage(Rect(0,0,1,1), 1, 0x00cc00ff);
 	col[Cgoal] = eallocimage(Rect(0,0,1,1), 1, 0xcc0000ff);
 	zx = Dx(screen->r) / gridwidth;
-	zy = Dy(screen->r) / gridheight;
+	zy = (Dy(screen->r) - font->height*2) / gridheight;
 	z = MIN(zx, zy);
-	nodesz = z <= 1 ? 1 : z > 2 ? z & ~1 : z;
+	//nodesz = z <= 1 ? 1 : z > 2 ? z & ~1 : z;
+	nodesz = z <= 1 ? 1 : z;
 	resetdrw();
 }
--- a/app/path/fs.c
+++ b/app/path/fs.c
@@ -30,7 +30,7 @@
 	start = p2n(sp->start);
 	goal = p2n(sp->goal);
 	if(pathfn(start, goal) < 0)
-		fprint(2, "runscens: findpath from %N to %N: %r\n",
+		fprint(2, "showscen: findpath from %N to %N: %r\n",
 			start, goal);
 }
 
@@ -43,7 +43,7 @@
 	Sim *sp, *se;
 
 	if((bf = Bopen(path, OREAD)) == nil)
-		sysfatal("readscen: %r");
+		sysfatal("readresults: %r");
 	sp = sims->p;
 	se = sp + sims->n;
 	while(sp < se){
@@ -77,7 +77,7 @@
 		if(pathfn(start, goal) < 0)
 			fprint(2, "runscens: findpath from %N to %N: %r\n",
 				start, goal);
-		memcpy(sp, &stats, sizeof stats);
+		sp->Prof = stats;
 		fprint(2, "%zd\t%d\t%d\t%d\t%d\t%d\t%.3f\t%.3f\n",
 			sp - (Sim*)sims->p,
 			stats.steps, stats.touched, stats.opened,
--- a/app/path/path.c
+++ b/app/path/path.c
@@ -11,7 +11,9 @@
 #include "dat.h"
 #include "fns.h"
 
-extern int	(*mousefn)(Mouse);
+extern int	(*mousefn)(Mouse, Point);
+extern void	dopan(Point);
+extern Point pan;
 extern int	(*keyfn)(Rune);
 
 mainstacksize = 512*1024;
@@ -21,14 +23,17 @@
 static setgoalmode;
 
 static int
-grmouse(Mouse m)
+grmouse(Mouse m, Point Δ)
 {
 	static Node *old;
 	Node *n;
 
-	if(m.buttons == 0){
-		old = nil;
+	if(m.buttons == 0)
 		return 0;
+	else if((m.buttons & 7) == 2){
+		dopan(Δ);
+		updatedrw(1);
+		return 1;
 	}
 	if((n = scrselect(m.xy)) == nil || old == n)
 		return 0;
@@ -37,8 +42,6 @@
 		if(old == nil || isblocked(n) ^ isblocked(old))
 			toggleblocked(n);
 		break;
-	case 2:
-		break;
 	case 4:
 		if(setgoalmode){
 			if(start != n && !isblocked(n))
@@ -120,6 +123,10 @@
 			nodesz >>= 1;
 			resetdrw();
 		}
+		break;
+	case 'z':
+		pan = ZP;
+		updatedrw(1);
 		break;
 	}
 	return 0;