shithub: bez

ref: cc9f77d9164026cadceff38e977439c516453743
dir: /bez.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>

// 7c bez.c && 7l -o bez bez.7

#define GRIDSIZE 50


//Channel* ptchan;
Mousectl* mctl;
Keyboardctl* kctl;

Rectangle
getgridrect(void)
{
	Rectangle grid;
	Point size, origin;
	int shortxy, sqsize, cellsize;
	
	origin = screen->r.min;
	size = Pt(Dx(screen->r), Dy(screen->r));
	shortxy = (size.x >= size.y) ? size.y : size.x;
	sqsize = shortxy - (shortxy % GRIDSIZE);
	cellsize = sqsize / GRIDSIZE;

	grid.min.x = origin.x + (size.x - sqsize) / 2;
	grid.min.y = origin.y + (size.y - sqsize) / 2;
	grid.max.x = grid.min.x + (cellsize * GRIDSIZE);
	grid.max.y = grid.min.y + (cellsize * GRIDSIZE);

	return grid;	
}

void
redraw(void)
{
    Rectangle gridrect, R1;
	Point P1, P2;
	int cellsize, i;
	int ctlpts [GRIDSIZE * GRIDSIZE + 1];
	
	memset(ctlpts, 0, GRIDSIZE * GRIDSIZE * sizeof(int));

	// ctlpts[0] is starting point of linked list.
	
	gridrect = getgridrect();
	cellsize = (gridrect.max.x - gridrect.min.x) / GRIDSIZE;
	
	//Draw Grid
	draw(screen, screen->r, display->black, nil, ZP);
	for(i = 1; i < GRIDSIZE; i++){
		P1.x = gridrect.min.x + i * cellsize;
		P1.y = gridrect.min.y;
		P2.x = gridrect.min.x + i * cellsize;
		P2.y = gridrect.max.y;
		line(screen, P1, P2, 0, 0, 0.1, display->white, ZP);
		P1.x = gridrect.min.x;
		P1.y = gridrect.min.y + i * cellsize;
		P2.x = gridrect.max.x;
		P2.y = gridrect.min.y + i * cellsize;
		line(screen, P1, P2, 0, 0, 0.1, display->white, ZP);
	}
	for (i = 1; i < GRIDSIZE * GRIDSIZE +1; i++){
		if (ctlpts[i] == 1){
			R1.min.x = gridrect.min.x + ((i - 1)  % GRIDSIZE) * cellsize;
			R1.min.y = gridrect.min.y + ((i - 1) / GRIDSIZE) * cellsize;
			R1.max.x = R1.min.x + cellsize;
			R1.max.y = R1.min.y + cellsize;
			draw(screen, R1, display->white, nil, ZP);
		}
	}
	flushimage(display, 1);
}


void
terminate(void)
{
	closekeyboard(kctl);
	closemouse(mctl);
	closedisplay(display);
	threadexitsall(nil);
}

void
resizethread(void* arg)
{
	redraw();
	Mousectl*mctl = arg;
	for(;;){
		recvul(mctl->resizec);
		if(getwindow(display, Refnone) < 0)
			sysfatal("getwindow: %r");
		redraw();
	}

}

void
keyboardthread(void* arg)
{
	Keyboardctl *kctl = arg;
	Rune r;
	for(;;){
		recv(kctl->c, &r);
		switch(r){
		case Kdel:
		case Kesc:
		case 'q' :	
			terminate();
			break;
		}
	}
}

void
mousethread(void* arg)
{
	Mousectl*mctl = arg;
	Mouse m;
	for(;;){
		recv(mctl->c, &m);
		if(m.buttons){
//			if mpos inside rect {gridrectmin , gridrectmax}
//				divide mpos.x by gridsize and mpos.y by gridsize  					
		}
	}
}

void
threadmain(int, char*argv[])
{
	Mouse m;
	mctl = initmouse("/dev/mouse", nil);
	if(mctl == nil)
		sysfatal("initmouse: %r");
	kctl = initkeyboard("/dev/cons");
	if(kctl == nil)
		sysfatal("initkeyboard: %r");
	if(initdraw(nil, nil, argv[0]) < 0)
		sysfatal("initdraw: %r");
 
//	ptchannel = chancreate(GRIDSIZE * GRIDSIZE + 1 * sizeof(int), 0);

	threadcreate(resizethread, mctl, 8*1024);
	threadcreate(mousethread, mctl, 8*1024);
	threadcreate(keyboardthread, kctl, 8*1024);

//	for(;;){
//		recv(ptchan)
//	}

	threadexits(nil);
}