shithub: blie

ref: 2d1e449a474444fa3288c56937a033c4f5f019f7
dir: /util.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include <event.h>
#include <keyboard.h>
#include "blie.h"

Memimage*
dupmemimage(Memimage *i)
{
	Memimage *d;
	d = gencanvas(i);
	
	memimagedraw(d, i->r, i, ZP, nil, ZP, SoverD);
	return d;
}

Memimage*
gencanvas(Memimage *i)
{
	return allocmemimage(i->r, RGBA32);
}

Memimage*
gencomposite(Memimage *A, Memimage *B, Memimage *M, Drawop op)
{
	Memimage *d;
	
	d = dupmemimage(A);
	memimagedraw(d, B->r, B, ZP, M, ZP, op);
	return d;
}

Memimage*
ecomposite(Layer *l, Memimage *img)
{
	Memimage *s;
	Memimage *m;
	
	s = l->editor->raw(l);
	m = l->editor->mask(l);
	
	if (!img) {
		if (bliedebug > 1)
			fprint(2, "  c: raw image\n");
		return dupmemimage(s);
	}
	if (bliedebug > 1)
		fprint(2, "  c: combine images\n");
	return gencomposite(img, s, m, l->op);
}

Point
scalepos(Point xy)
{
	xy = addpt(xy, vstate.offset);
	xy.x = xy.x / vstate.zoom;
	xy.y = xy.y / vstate.zoom;
	return xy;
}

ulong
color2value(Color c)
{
	int r, g, b, a;
	r = c.r * 256;
	g = c.g * 256;
	b = c.b * 256;
	a = c.a * 256;
	return r<<24 | g<<16 | b<<8 | a;
}

Image **askcolimage;
char *askcolprompt;

static void
askcolorredraw(Image *i)
{
	border(i, i->r, 1, display->black, ZP);
	string(i, addpt(i->r.min, Pt(10, 0)), display->black, ZP, font, askcolprompt);
}

Color
askcolor(char *prompt)
{
	Event ev;
	int e;
	Color c;
	Rectangle r;
	Image *col = nil;
	ulong val;
	Point xy;
	
	r.min = vstate.mousepos;
	r.max = addpt(r.min, Pt(100, 100+vdata.fontheight));
	
	askcolprompt = prompt;
	askcolimage = reqwin(r, 0xccccccff, askcolorredraw);
	r.min.y += vdata.fontheight;
	
	for (;;) {
		e = event(&ev);
		switch (e) {
		case Ekeyboard:
			if (ev.kbdc == Kesc || ev.kbdc == 'q')
				goto Abort;
			else if (ev.kbdc == '\n')
				goto Accept;
			break;
		case Emouse:
			if (!ev.mouse.buttons)
				break;
			if (!ptinrect(ev.mouse.xy, r))
				break;
			xy = subpt(ev.mouse.xy, (*askcolimage)->r.min);
			xy.y -= vdata.fontheight;
			c.r = (double)xy.x / 100;
			c.g = (double)xy.y / 100;
			c.b = 0.;
			c.a = 1.;
			val = color2value(c);
			if (col)
				freeimage(col);
			col = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, val);
			border(*askcolimage, (*askcolimage)->r, 2, col, ZP);
//			fprint(2, "col: %f %f %f %f\n", c.r, c.g, c.b, c.a);
			break;
		}
	}

Abort:
	c.r = -1.;
Accept:
	setmodedirty(1);
	unreqwin(askcolimage);
	askcolimage = nil;
	return c;
}