shithub: libnate

ref: 6ff9ef0c4bef23bfd91f33f0c2f5cab0b88c0307
dir: /nate_construct.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include "nate_construct.h"

typedef struct NCelem NCelem;
struct NCelem {
	Nelem* elem;
	NCelem* next;
};

static NCelem* nc_stack = nil;

void
nc_init()
{
	nc_stack = nil;
}

void
nc_push(Nelem* nelem)
{
	NCelem *n;
	if (!nc_stack) {
		nc_stack = malloc(sizeof(NCelem));
		assert(nc_stack);
		nc_stack->next = nil;
		nc_stack->elem = nelem;
	} else {
		n = malloc(sizeof(NCelem));
		assert(n);
		n->next = nc_stack;
		n->elem = nelem;
		nc_stack = n;
	}
}

void
nc_pop()
{
	if (!nc_stack)
		return;
	NCelem *nc = nc_stack;
	nc_stack = nc->next;
	free(nc);
}

Nelem*
nc_get()
{
	return nc_stack ? nc_stack->elem : nil;
}

Nelem* ch_retelem;
Rectangle ch_rect;
Image* ch_screen;
Mouse ch_mouse;

void
fe_checkhit(Nelem* elem, int)
{
	Rectangle r;
	Nelem* e;
	Point s;
	
	s = ncallcalcsize(elem, ch_screen);
	r.min = ch_rect.min;
	r.max = addpt(r.min, s);
	
	if (!ptinrect(ch_mouse.xy, r))
		return;
	
	e = ncallcheckhit(elem, ch_screen, ch_rect, ch_mouse);
	if (e) {
		ch_retelem = e;
	}
}

Nelem*
nd_checkhit(Nelem* nelem, Image* screen, Rectangle r, Mouse m)
{
	Nlist* l = ncallgetchildren(nelem);
	
	if (!l)
		return nil;
	
	ch_retelem = nil;
	ch_rect = r;
	ch_screen = screen;
	ch_mouse = m;
	lforeach(l, fe_checkhit);
	
	return ch_retelem;
}