Skip to content
Marek Maskarinec edited this page Apr 23, 2021 · 7 revisions

A simple ui framework.

usage

// start of the program
mainbox := ui.mkbox([]ui.element{ui.mklabel("Hello World", 1, 0xff)}, false)

ui.main = mainbox

// in loop

ui.handle(cam.w, cam.h)

consts

TOP - used for text alignment to top/left

MID - used for text alignment to middle

BOT - used for text alignment ot bottom/right

CENTER - used for image scaling. This will not resize it at all. SCALE - used for image scaling. This will resize it, but not deform.

type element

type element* = interface {
	draw()
	calculate()
	handle()
	gspan(): ^int
	gpadding(): ^rectangle.rect
	gdimensions(): ^rectangle.rect
}

Element interface. Element is the main element of ui.

type box

type box* = struct {
	children: []element // children in the box
	padding, dimensions: rectangle.rect
	span: int
	vertical: bool // switch between vbox and hbox

	image: image.img // background image
	imagemode: int // image mode. see constants
	imgs: int
	imgx, imgy: int
}

Box element. It organizes other elements. You can even nest them.

type label

type label* = struct {
	text: str
	textsize: real
	textcolor: uint32
	valign: int
	halign: int
	bgcolor: uint32
	padding, dimensions: rectangle.rect
	span: int
}

Label element. Self explanatory.

type button

type button* = struct {
	label: label
	toggled: bool
	pressed: bool
	justpressed: bool
	clickfunc: bool

	padding, dimensions: rectangle.rect
	span: int

	onpress, onjustpress: fn()
	ontoggle: fn(toggled: bool)
}

Button element. You can either hook up callbacks or use the pressed, justpressed and toggled fields.

fn mkbox

fn mkbox*(c: []element, vertical: bool): box

Makes a box.

fn mklabel

fn mklabel*(text: str, textsize: real, textcolor: uint32): label

Makes a label.

fn mkbutton

fn mkbutton*(l: label): button

Makes a button.

fn handle

fn handle*(w, h: int32)

Handles ui. Pass w and h of the camera.

Clone this wiki locally