commit bfdf4abc7b3855bf18bdeb3124cbe538daec0407
parent 2fa2cf60d22cf7e9b52175e4bc0300b920c1e9f0
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 2 Jan 2023 11:04:03 -0500
gr/cursor: first steps
not quite working, but getting there. It also explains why I've been fliddling
with Plane API like this in the last few commits...
Diffstat:
3 files changed, 70 insertions(+), 0 deletions(-)
diff --git a/fs/doc/gr/cursor.txt b/fs/doc/gr/cursor.txt
@@ -0,0 +1,37 @@
+# Cursor
+
+A cursor is a small Plane (gr/plane) drawn on top of a bigger one and that can
+move around. The challenge here is to be able to draw what was "under" the
+cursor when the cursor moves.
+
+The idea is that the cursor's Plane buffer holds the image *under* it. When it
+is initialized, it copies the contents of its "parent" buffer into itself, then
+it draws itself into the parent buffer.
+
+When the cursor moves, it copies back its contents into the parent buffer, then
+moves itself, then copies the contents of the parent buffer into itself, and
+then draws itself into the parent.
+
+When the parent draws anything, it checks if the drawing touches the cursor
+area. If it does, before drawing, it offloads its content into the parent, then
+the parent draws its thing, then the cursor refreshes its buffer and draws
+itself again.
+
+For now, the cursor is drawn as a simple rectangle of a single color.
+
+## API
+
+Fields:
+
+parent The Plane upon which this cursor is drawn
+
+Methods:
+
+:new ( parent -- cursor )
+ Create a new cursor. Allocates a buffer, initializes itself to pos 0,0 and
+ makes its first initial copy of the parent buffer.
+
+:move ( x y self -- )
+ Copies self's buffer to parent at old position, move self (through Rect
+ :move), then copy parent buffer into self at new position, then draw the
+ cursor at new position.
diff --git a/fs/gr/cursor.fs b/fs/gr/cursor.fs
@@ -0,0 +1,27 @@
+?f<< /gr/plane.fs
+?f<< /gr/color.fs
+
+extends Plane struct[ Cursor
+ sfield parent
+
+ 8 const CURSZ
+ CURSZ CURSZ COLOR_RGB24 Plane :new const CURSOR
+ CURSOR :allotbuf
+ 0 0 255 r8g8b8>rgb24 to CURSOR color
+ CURSZ CURSZ CURSOR :fill
+
+ : parent>cursor ( self -- ) dup dup parent swap :copy< ;
+ : cursor>parent ( self -- ) dup dup parent swap :copy> ;
+
+ : :new ( parent -- cursor )
+ dup encoding CURSZ CURSZ rot Plane :new ( parent cursor )
+ swap , ( cursor )
+ \ We allocate a buffer that is enough to fit 32-bit color in case we need
+ \ to change encoding later.
+ here over to buffer CURSZ CURSZ * 4 * allot ( self )
+ dup parent>cursor ;
+
+ : :move ( x y self -- ) >r \ V1=self
+ r@ cursor>parent r@ Rect :move r@ parent>cursor
+ r@ r@ parent CURSOR :copy> ;
+]struct
diff --git a/fs/tests/manual/cursor.fs b/fs/tests/manual/cursor.fs
@@ -0,0 +1,6 @@
+f<< /gr/cursor.fs
+
+screen :self Cursor :new structbind Cursor cursor
+screen :activate
+12 12 cursor :move
+