shithub: battleship

Download patch

ref: ccd149a329f9a33195c2c6216158e283f8993076
parent: b555a1ad730994a42cfecf88bba15ece20d1e6f9
author: rodri <rgl@antares-labs.eu>
date: Sat Nov 16 06:22:16 EST 2024

fix the menulist (make it play nice with periodic refreshing).

before the addition of the timer for periodic matches updates
the menulist was supposed to be refreshed manually by the user,
and we didn't take into account the fact that every time you
clear it and refill it loses its navigational parameters, i.e.
which entry was selected and what's the offset if there are
more entries than the visible window (Maxvisitems).

so now we store those and reconfigure the menulist, adapting
to its new state, so that the user experiences as little
disruption as possible.

--- a/bts.c
+++ b/bts.c
@@ -955,6 +955,9 @@
 	Point2 cell;
 	uchar buf[BY2MAP];
 	int i, idx;
+	struct {
+		int high, off;
+	} menuparams;
 
 	if(debug)
 		fprint(2, "rcvd '%s'\n", cmd);
@@ -987,6 +990,8 @@
 			break;
 		case CMmatches:
 			if(!matches->filling){
+				menuparams.high = matches->high;
+				menuparams.off = matches->off;
 				matches->clear(matches);
 				matches->filling = 1;
 			}
@@ -997,8 +1002,12 @@
 						smprint("%s vs %s", cb->f[2], cb->f[3]));
 			break;
 		case CMendmatches:
-			if(matches->filling)
+			if(matches->filling){
+				matches->high = min(menuparams.high, matches->nentries-1);
+				matches->off = menuparams.off > matches->nentries - matches->maxvis?
+					0: menuparams.off;
 				matches->filling = 0;
+			}
 			break;
 		case CMwatching:
 			match.id = strtoul(cb->f[1], nil, 10);
--- a/dat.h
+++ b/dat.h
@@ -236,8 +236,9 @@
 	Mlist;
 	char *title;
 	Rectangle r, sr;	/* content and scroll rects */
+	int maxvis;		/* max amount of visible entries */
 	int high;		/* [-1,nitems) where -1 is none */
-	int off;		/* entry offset ∈ [0, nitems-Maxvisitems] */
+	int off;		/* entry offset ∈ [0, nitems-maxvis] */
 
 	void (*add)(Menulist*, int, char*);
 	void (*clear)(Menulist*);
--- a/menulist.c
+++ b/menulist.c
@@ -32,7 +32,7 @@
 		ml->r.max.x = ml->r.min.x + ew;
 	}
 
-	if(ml->nentries > Maxvisitems){
+	if(ml->nentries > ml->maxvis){
 		ml->sr.min = subpt(ml->r.min, Pt(Scrollwidth+2*Menuborder,0));
 		ml->sr.max = Pt(ml->r.min.x-2*Menuborder, ml->r.max.y);
 	}else if(ml->nentries > 1)
@@ -74,7 +74,7 @@
 	if(ml->nentries < 1)
 		return -1;
 
-	r = ml->nentries > Maxvisitems? Rpt(ml->sr.min, ml->r.max): ml->r;
+	r = ml->nentries > ml->maxvis? Rpt(ml->sr.min, ml->r.max): ml->r;
 
 	selected = -1;
 	if(ptinrect(mc->xy, r)){
@@ -88,7 +88,7 @@
 					lastlmbms = mc->msec;
 			}
 		}
-		if(mc->buttons != oldm.buttons && ml->nentries > Maxvisitems)
+		if(mc->buttons != oldm.buttons && ml->nentries > ml->maxvis)
 			/* scrolling */
 			switch(mc->buttons){
 			case 1: if(!ptinrect(mc->xy, ml->sr)) break;
@@ -97,7 +97,7 @@
 				break;
 			case 4: if(!ptinrect(mc->xy, ml->sr)) break;
 			case 16:
-				ml->off = min(ml->off + (mc->xy.y - ml->sr.min.y)/(font->height+Vspace), ml->nentries-Maxvisitems);
+				ml->off = min(ml->off + (mc->xy.y - ml->sr.min.y)/(font->height+Vspace), ml->nentries-ml->maxvis);
 				break;
 			}
 	}
@@ -145,10 +145,10 @@
 	}
 
 	/* draw scroll */
-	if(ml->nentries > Maxvisitems){
+	if(ml->nentries > ml->maxvis){
 		border(dst, ml->sr, -Menuborder, bc, ZP);
 		draw(dst, ml->sr, display->black, nil, ZP);
-		draw(dst, Rpt(addpt(ml->sr.min, Pt(0,ml->off*Dy(ml->sr)/ml->nentries)), Pt(ml->sr.max.x,ml->sr.min.y + (ml->off+Maxvisitems)*Dy(ml->sr)/ml->nentries)), display->white, nil, ZP);
+		draw(dst, Rpt(addpt(ml->sr.min, Pt(0,ml->off*Dy(ml->sr)/ml->nentries)), Pt(ml->sr.max.x,ml->sr.min.y + (ml->off+ml->maxvis)*Dy(ml->sr)/ml->nentries)), display->white, nil, ZP);
 	}
 }
 
@@ -164,6 +164,7 @@
 	w = max(stringwidth(font, title), stringwidth(font, none));
 	ml->r.min = Pt(SCRW/2 - w/2, topmargin);
 	ml->r.max = addpt(ml->r.min, Pt(w, font->height+Vspace));
+	ml->maxvis = Maxvisitems;
 	ml->high = -1;
 	ml->add = menulist_add;
 	ml->clear = menulist_clear;