shithub: libnate

ref: 7d7806db6cd48b8ebd565f636eebc59939e72c53
dir: /n_image.c/

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

#define N_TYPE NImage_Type
char *NImage_Type = "NImage";

static Rectangle
image_calcsize(Nelem *nelem, Image*, Rectangle r)
{
	Point p;
	NImage *i = (NImage*)nelem;
	GUARD(i);
	
	if (!i->autosize) {
		i->r = r;
		return i->r;
	}
	
	p = ZP;
	if (i->image)
		p = Pt(Dx(i->image->r), Dy(i->image->r));
	r.max = addpt(r.min, p);
	i->r = r;
	return i->r;
}

static void
image_draw(Nelem *nelem, Image *img)
{
	NImage *i = (NImage*)nelem;
	GUARD(i);
	
	if (!i->image) {
		draw(img, i->r, display->white, nil, ZP);
		return;
	}
	draw(img, i->r, i->image, nil, i->offset);
}

static Nelemfunctions Nimagefunctions = {
	.calcsize = image_calcsize,
	.draw = image_draw,
};

DEF_ACCESSOR_OneParam(NImage, image_image, Image*, image);
DEF_ACCESSOR_OneParam(NImage, image_offset, Point, offset);
DEF_ACCESSOR_OneParam(NImage, image_autosize, int, autosize);

NImage*
New_Image(char *name)
{
	NImage *i = MakeNelem(NImage, NImage_Type, &Nimagefunctions, name, 0);
	
	i->image = nil;
	i->offset = ZP;
	
	i->Image = image_image;
	i->Offset = image_offset;
	i->AutoSize = image_autosize;
	nc_push(i);
	return i;
}