shithub: pain

Download patch

ref: d4bb352f8314035b5b5a2130bd7d93e74d9fec4e
parent: 1b81533742fa32d4ba26f7fcacb942becfd89c50
author: jmq <jmq@jmq.sh>
date: Sun Nov 17 22:51:31 EST 2024

bindigns: implement bindings

--- a/bindings.c
+++ b/bindings.c
@@ -13,13 +13,54 @@
 	int type;
 	int code;
 	char * command;
-	void (*func)(void);
+	void (*func)(int);
 } DefaultBinding;
 
+static void
+BindingQuit(int)
+{
+	quitloop();
+}
+
+static void
+BindingStroke(int e)
+{
+	if (e != BEenter)
+		return;
+
+	stroke();
+}
+
+static void
+BindingZoomIn(int)
+{
+	zoom(ZoomSensitivity);
+}
+
+static void
+BindingZoomOut(int)
+{
+	zoom(-ZoomSensitivity);
+}
+
+static void
+BindingMoveCanvas(int e)
+{
+	print("e = %d\n", e);
+	if (e != BEenter)
+		return;
+
+	CanvasAt = addpt(CanvasAt, subpt(MousePosition, PastMousePosition));
+	CanvasMoved = DrawAllLayers = 1;
+}
+
 DefaultBinding DefaultBindings[] = 
 {
-	{BKeyboard, 'q', nil, quitloop},
-	{BMouse, 1, nil, stroke}
+	{BKeyboard, 'q', nil, BindingQuit},
+	{BMouse, 1, nil, BindingStroke},
+	{BMouse, 8, nil, BindingZoomIn},
+	{BMouse, 16, nil, BindingZoomOut},
+	{BMouse, 2, nil, BindingMoveCanvas}
 };
 
 void
@@ -87,16 +128,36 @@
 runbindings(int type, int code)
 {
 	int rc;
+	int e;
 	Binding * b;
 	// print("handling bindings for %d %d\n", type,code);
 
 	rc = 0;
 	for (b = BindingRoot; b != nil; b = b->next) {
-		if (b->type != type || b->code != code)
+		if (b->type != type || b->code != code) {
+			if (b->type == type && b->state != BEleave && b->func != nil)
+				b->func(b->state = BEleave);				
 			continue;
+		}
 		
+		e = BEleave;
+		switch (b->state) {
+			case BEenter:
+				e = BEin;
+				break;
+			case BEin:
+				e = BEin;
+				break;
+			case BEleave:
+				e = BEenter;
+				break;
+			default:
+				werrstr("invalid state");
+				break;
+		}
+		
 		if (b->func != nil)
-			b->func();
+			b->func(e);
 		if (b->command != nil)
 			NOOP();	// TODO: Add shell command support
 		rc++;
--- a/dat.h
+++ b/dat.h
@@ -10,6 +10,12 @@
 	BMouse,
 	BKeyboard
 };
+enum
+{
+	BEleave,
+	BEenter,
+	BEin,
+};
 
 typedef struct Binding
 {
@@ -16,15 +22,22 @@
 	struct Binding * prev, * next;
 	int code;
 	int type;
+	int state;
 	char * command;
-	void (*func)(void);
+	void (*func)(int);
 } Binding;
 
 // extern Rectangle CanvasSize = {0};
 // extern Point CanvasAt = {0};
-extern Layer * LayerRoot = nil;
-extern Binding * BindingRoot = nil;
-extern int RunLoop = 0;
+extern Layer * LayerRoot;
+extern Binding * BindingRoot;
+extern int RunLoop;
+extern int ZoomSensitivity;
+extern Point MousePosition;
+extern Point PastMousePosition;
+extern Point CanvasAt;
+extern int CanvasMoved;
+extern int DrawAllLayers;
 // extern Layer * CurrentLayer = nil;
 // extern Image * Background = nil;
 // extern Image * Canvas = nil;
--- a/fns.h
+++ b/fns.h
@@ -7,6 +7,8 @@
 void setbackground(ulong col);
 void drawcanvas(void);
 void zoom(int);
+void zoomin(void);
+void zoomout(void);
 void stroke(void);
 void clearlayer(Layer *);
 void setbrushcolor(ulong col);
--- a/main.c
+++ b/main.c
@@ -15,6 +15,7 @@
 #define Ar(r) (Dx(r)*Dy(r))
 #define RECTOPPT(r, op)	((Rectangle){op(r.min),op(r.max)})
 #define DEFAULTCANVASSIZE RECTANGLE(640, 640)
+#define STROKEEND Endsquare
 
 int CanvasMoved = 1;
 Rectangle CanvasSize;
@@ -101,7 +102,7 @@
 		if (resizeimage(ZoomedImage, vr, Zoom, ViewImage, ZP) < 0)
 			sysfatal("resizeimage: %r");
 	}
-	if (CanvasMoved) {
+	if (CanvasMoved || sr) {
 		draw(screen, screen->r, Background, nil, ZP);
 		draw(screen, RECTOPPT(ZoomedSize, screentoglobalatcanvaspt), ZoomedImage, nil, ZP);
 	}
@@ -129,11 +130,9 @@
 void
 zoom(int z)
 {
-	int mc;
 	int osx, osy;
 	Point c;
-	
-	mc = Zoom > 1;
+
 	if ((Zoom + z) <= 1)
 		Zoom = 1;
 	else
@@ -140,32 +139,31 @@
 		Zoom += z;
  
 	c = subpt(CanvasAt, MousePosition);
-getsize:
 	osx = Dx(ZoomedSize);
 	osy = Dy(ZoomedSize);
-	
-	if ((osx == 0 || osy == 0) && 
-		Dx(CanvasSize) != 0 && Dy(CanvasSize) != 0) {
-		ZoomedSize = CanvasSize;
-		goto getsize;
-	}
 
-	ZoomedSize.min = mulpt(ZoomedSize.min, Zoom);
-	ZoomedSize.max = mulpt(ZoomedSize.max, Zoom);
+	ZoomedSize.min = mulpt(CanvasSize.min, Zoom);
+	ZoomedSize.max = mulpt(CanvasSize.max, Zoom);
 	
 	if (ZoomedImage != nil) {
 		freeimage(ZoomedImage);
 		ZoomedImage = nil;
 	}
-	if (mc || Zoom > 1)
-		CanvasAt = addpt(
-			Pt(Dx(ZoomedSize)*c.x/osx, Dy(ZoomedSize)*c.y/osy), 
-			MousePosition
-		);
+
+	CanvasAt = addpt(
+		Pt(Dx(ZoomedSize)*c.x/osx, Dy(ZoomedSize)*c.y/osy), 
+		MousePosition
+	);
+
  	DrawAllLayers = 1;
 }
 
 void
+movecanvas()
+{
+}
+
+void
 stroke()
 {
 	Point t, f;
@@ -186,14 +184,14 @@
 
 	f = screentocanvaspt(subpt(f, viewAtCanvas.min));
 	t = screentocanvaspt(subpt(t, viewAtCanvas.min));
-	line(CurrentLayer->image, f, t, Enddisc, Enddisc, 1, BrushImage, ZP);
+	line(CurrentLayer->image, f, t, STROKEEND, STROKEEND, 1, BrushImage, ZP);
 	f = mulpt(f, Zoom);
 	t = mulpt(t, Zoom);
 	if (ZoomedImage != nil)
-		line(ZoomedImage, f, t, Enddisc, Enddisc, 1*Zoom, BrushImage, ZP);
-	f = screentoglobalpt(f);
-	t = screentoglobalpt(t);
-	line(screen, f, t, Enddisc, Enddisc, 1*Zoom, BrushImage, ZP);
+		line(ZoomedImage, f, t, STROKEEND, STROKEEND, 1*Zoom, BrushImage, ZP);
+	f = screentoglobalatcanvaspt(f);
+	t = screentoglobalatcanvaspt(t);
+	line(screen, f, t, STROKEEND, STROKEEND, 1*Zoom, BrushImage, ZP);
 	
 }
 
@@ -328,6 +326,7 @@
 	if (!atexit(fsclose))
 		sysfatal("atexit: %r");
 	
+	CanvasSize = ZoomedSize = DEFAULTCANVASSIZE;
 	setcanvassize(DEFAULTCANVASSIZE);
 	newlayer(DEFAULTLAYERCOLOR);
 	zoom(0);
--