shithub: blie

Download patch

ref: 807afae69b7551b681b638d64f1099af5c672fdd
parent: f5d679a1fea46f5f6128679ed766ec0cace95c79
author: sirjofri <sirjofri@sirjofri.de>
date: Thu Aug 15 17:05:39 EDT 2024

adds first saving functionality

--- a/blie.c
+++ b/blie.c
@@ -427,6 +427,34 @@
 	return 1;
 }
 
+static void
+savetools(void)
+{
+	if (!(estate.l && estate.ed))
+		return;
+	if (estate.ed->savetools)
+		estate.ed->savetools(estate.l);
+}
+
+static void
+savelayer(Layer *l, int, int, void*)
+{
+	if (l->editor && l->editor->savedata)
+		l->editor->savedata(l);
+}
+
+static void
+savecurrentlayer(void)
+{
+	savelayer(estate.l, 0, 0, nil);
+}
+
+static void
+savelayers(void)
+{
+	foreachlayer(savelayer, nil);
+}
+
 static Redrawwin
 askcommand(Event ev)
 {
@@ -455,8 +483,19 @@
 		}
 		return Rnil;
 	}
-	if (strcmp(args[0], "w") == 0) {
-		// TODO: save all
+	if (args[0][0] == 'w') {
+		switch (args[0][1]) {
+		case 'a':
+			savelayers();
+			savetools();
+			return Rnil;
+		case 't':
+			savetools();
+			return Rnil;
+		case 0:
+			savecurrentlayer();
+			return Rnil;
+		}
 		return Rnil;
 	}
 	if (n == 2 && strcmp(args[0], "e") == 0) {
--- a/blie.h
+++ b/blie.h
@@ -84,6 +84,7 @@
 	Redrawwin (*drawinput)(Layer*, int, Event);
 	Redrawwin (*toolinput)(Layer*, int, Event);
 	int (*savedata)(Layer*);
+	int (*savetools)(Layer*);
 };
 
 extern Editor p9image;
--- a/p9image.c
+++ b/p9image.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <bio.h>
 #include <draw.h>
 #include <memdraw.h>
 #include <event.h>
@@ -148,6 +149,64 @@
 }
 
 static void
+readcolors(void)
+{
+	int fd, i;
+	Biobuf *bin;
+	char *s;
+	char *args[2];
+	ulong val;
+	
+	fd = open("p9image/colors", OREAD);
+	if (fd < 0)
+		return;
+	
+	bin = Bfdopen(fd, OREAD);
+	if (!bin)
+		sysfatal("%r");
+	
+	while (s = Brdstr(bin, '\n', 1)) {
+		if (tokenize(s, args, 2) == 2) {
+			i = atoi(args[0]);
+			val = strtoul(args[1], nil, 16);
+			tstate.colordata[i] = val;
+		} else
+			fprint(2, "ignoring line: %s\n", args[0]);
+		free(s);
+	}
+}
+
+static int
+writecolors(void)
+{
+	int fd, i;
+	ulong c;
+	if (access("p9image", AEXIST)) {
+		fd = create("p9image", OREAD, DMDIR|0555);
+		if (fd < 0) {
+			werrstr("p9image: %r");
+			return 0;
+		}
+		close(fd);
+	}
+	fd = open("p9image/colors", OWRITE|OTRUNC);
+	if (fd < 0)
+		fd = create("p9image/colors", OWRITE|OTRUNC, 0666);
+	if (fd < 0) {
+		werrstr("p9image: colors: %r");
+		return 0;
+	}
+	
+	for (i = 0; i < NUMCELLS; i++) {
+		c = tstate.colordata[i];
+		fprint(fd, "%d\t%ulx\n", i, c&0xff ? c : 0);
+	}
+	
+	close(fd);
+	return 1;
+}
+
+static void
 p9initialize()
 {
 	tstate.mode = Composite;
@@ -166,8 +225,70 @@
 	setcolorbrush(DRed);
 	tstate.curbrush = 3;
 	tstate.curcolor = -1;
+	
+	/* load tools */
+	readcolors();
 }
 
+static int
+writelayer(Layer *l)
+{
+	Data *d;
+	int fd;
+	char *s;
+	
+	if (!l->data) {
+		werrstr("p9image: layer not initialized: %s", l->name);
+		return 0;
+	}
+	d = (Data*)l->data;
+	
+	/* image file */
+	if (!d->img) {
+		werrstr("p9image: no image");
+		return 0;
+	}
+	
+	s = smprint("l/%s/img", l->name);
+	fd = open(s, OWRITE|OTRUNC);
+	if (fd < 0)
+		fd = create(s, OWRITE|OTRUNC, 0666);
+	if (fd < 0) {
+		werrstr("p9image: %r");
+		free(s);
+		return 0;
+	}
+	free(s);
+	if (writememimage(fd, d->img)) {
+		close(fd);
+		werrstr("p9image: %r");
+		return 0;
+	}
+	close(fd);
+	
+	/* mask file */
+	if (!d->mask)
+		return 1;
+	
+	s = smprint("l/%s/mask", l->name);
+	fd = open(s, OWRITE|OTRUNC);
+	if (fd < 0)
+		fd = create(s, OWRITE|OTRUNC, 0666);
+	if (fd < 0) {
+		werrstr("p9image: %r");
+		free(s);
+		return 0;
+	}
+	free(s);
+	if (writememimage(fd, d->mask)) {
+		close(fd);
+		werrstr("p9image: %r");
+		return 0;
+	}
+	close(fd);
+	return 1;
+}
+
 static void
 p9init(Layer *l)
 {
@@ -353,9 +474,19 @@
 static int
 p9savedata(Layer *l)
 {
-	return 1;
+	return writelayer(l);
 }
 
+static int
+p9savetools(Layer*)
+{
+	/* at the moment, this will write the data for each layer
+	 * into the same file, which is not wrong, but could
+	 * be improved.
+	 */
+	return writecolors();
+}
+
 static Redrawwin
 drawbrush(Layer *l, int buttons, Point xy)
 {
@@ -546,6 +677,7 @@
 	.toolrect = p9toolrect,
 	.drawtools = p9drawtools,
 	.savedata = p9savedata,
+	.savetools = p9savetools,
 	.drawinput = p9drawinput,
 	.toolinput = p9toolinput,
 };
--- a/words
+++ b/words
@@ -72,6 +72,7 @@
 commands, which work globally:
 
 - q | del: exit
+- tab: open command prompt (see COMMANDS)
 
 Drawing controls:
 
@@ -85,6 +86,19 @@
 Selecting a layer also changes the "editor". The editor is a
 module that takes over part of the control, depending on the
 type (see first section about file format).
+
+
+COMMANDS
+
+following commands exist:
+
+- quality <q>: <q=0|1> - quality mode (higher = better, but slower)
+- w<m>: <m=a|t|nil> - save data
+  - a: save all (all layers and tools)
+  - t: save tools for current editor
+  - nil (absent): save current layer
+  - save functions are forwarded to the layer editor
+    (savedata, savelayer).
 
 
 EDITOR p9image