paint: add 'c' and 'f'

This commit is contained in:
stanley lieber 2012-03-02 04:54:45 +00:00
parent 2fec3c0bc8
commit f93dcaff17
2 changed files with 50 additions and 23 deletions

View file

@ -23,7 +23,14 @@ Type a number,
in the pop-up box and hit enter. in the pop-up box and hit enter.
.TP .TP
.B c .B c
Clear the screen. Any unsaved work will be lost. Change the drawing color to
.I n,
where
0 = black, 1 = white, 2 = red, 3 = green, 4 = blue and 5 = yellow.
Type the corresponding number into the pop-up box and hit enter.
.TP
.B f
Fill the screen with the current color. Any unsaved work will be lost.
.TP .TP
.B o .B o
Open a bitmap image file for editing. Type a path and filename into the Open a bitmap image file for editing. Type a path and filename into the

View file

@ -3,6 +3,10 @@
#include <draw.h> #include <draw.h>
#include <event.h> #include <event.h>
#define NCOLORS 6
Image *colors[NCOLORS];
void void
eresized(int) eresized(int)
{ {
@ -18,17 +22,15 @@ loadimg(char *name)
if((fd = open(name, OREAD)) < 0) if((fd = open(name, OREAD)) < 0)
return -1; return -1;
else { if((b = readimage(display, fd, 0)) == nil){
if((b = readimage(display, fd, 0)) == nil){
close(fd);
return -1;
} else {
draw(screen, screen->r, b, 0, b->r.min);
flushimage(display, 1);
freeimage(b);
}
close(fd); close(fd);
return -1;
} }
draw(screen, screen->r, b, 0, b->r.min);
flushimage(display, 1);
freeimage(b);
close(fd);
return 0;
} }
int int
@ -40,6 +42,7 @@ saveimg(char *name)
return -1; return -1;
writeimage(fd, screen, 0); writeimage(fd, screen, 0);
close(fd); close(fd);
return 0;
} }
void void
@ -47,12 +50,15 @@ main(int argc, char *argv[])
{ {
Event e; Event e;
Point last; Point last;
int haslast; int b = 1;
int brushsize = 1; int c = 0;
int cn, f;
int haslast = 0;
char brush[128]; char brush[128];
char color[NCOLORS];
char file[128]; char file[128];
char fill[NCOLORS];
haslast = 0;
if(initdraw(0, 0, "paint") < 0){ if(initdraw(0, 0, "paint") < 0){
fprint(2, "paint: initdraw failed: %r\n"); fprint(2, "paint: initdraw failed: %r\n");
exits("initdraw"); exits("initdraw");
@ -61,6 +67,13 @@ main(int argc, char *argv[])
draw(screen, screen->r, display->white, 0, ZP); draw(screen, screen->r, display->white, 0, ZP);
flushimage(display, 1); flushimage(display, 1);
colors[0] = display->black;
colors[1] = display->white;
colors[2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DRed);
colors[3] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DGreen);
colors[4] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
colors[5] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DYellow);
ARGBEGIN{ ARGBEGIN{
default: default:
goto Usage; goto Usage;
@ -84,28 +97,35 @@ main(int argc, char *argv[])
case Emouse: case Emouse:
if(e.mouse.buttons & 1){ if(e.mouse.buttons & 1){
if(haslast) if(haslast)
line(screen, last, e.mouse.xy, Enddisc, Enddisc, brushsize, display->black, ZP); line(screen, last, e.mouse.xy, Enddisc, Enddisc, b, colors[c], ZP);
else else
fillellipse(screen, e.mouse.xy, brushsize, brushsize, display->black, ZP); fillellipse(screen, e.mouse.xy, b, b, colors[c], ZP);
last = e.mouse.xy; last = e.mouse.xy;
haslast = 1; haslast = 1;
flushimage(display, 1); flushimage(display, 1);
} else } else
haslast = 0; haslast = 0;
if(e.mouse.buttons & 4){
fillellipse(screen, e.mouse.xy, brushsize, brushsize, display->white, ZP);
flushimage(display, 1);
}
break; break;
case Ekeyboard: case Ekeyboard:
if(e.kbdc == 'b'){ if(e.kbdc == 'b'){
if(eenter("Brush", brush, sizeof(brush), &e.mouse) <= 0) if(eenter("Brush", brush, sizeof(brush), &e.mouse) <= 0)
break; break;
brushsize = atoi(brush); b = atoi(brush);
}
if(e.kbdc == 'c'){
if(eenter("Color", color, sizeof(color), &e.mouse) <= 0)
break;
cn = atoi(color);
if(cn >= 0 && cn < NCOLORS)
c = cn;
}
if(e.kbdc == 'f'){
if(eenter("Fill", fill, sizeof(fill), &e.mouse) <= 0)
break;
f = atoi(fill);
if(f >= 0 && f < NCOLORS)
draw(screen, screen->r, colors[f], 0, ZP);
} }
if(e.kbdc == 'c')
draw(screen, screen->r, display->white, 0, ZP);
if(e.kbdc == 'o'){ if(e.kbdc == 'o'){
if(eenter("Open file", file, sizeof(file), &e.mouse) <= 0) if(eenter("Open file", file, sizeof(file), &e.mouse) <= 0)
break; break;