undar-lang/test/paint.ul

118 lines
2.2 KiB
Plaintext
Executable File

/**
* Constants
*/
const byte BLACK = 0;
const byte WHITE = 255;
const byte DARK_GRAY = 73;
const byte GRAY = 146;
byte selected_color = 255;
plex System {
ref function vector(void):void;
byte wst;
byte rst;
u32 pad;
nat r;
nat g;
nat b;
byte debug;
byte halt;
}
plex Screen {
ref function vector(void):void;
nat width;
nat height;
nat auto;
nat x;
nat y;
nat addr;
byte pixel;
byte sprite;
}
plex Mouse {
ref function vector(void):void;
nat x;
nat y;
nat state;
nat z;
nat scrollx;
nat scrolly;
nat unused_;
}
System system = devmap(0x00, System);
Screen screen = devmap(0x20, Screen);
screen.vector = ref render_screen;
Mouse mouse = devmap(0x90, Mouse);
mouse.vector = ref render_screen;
halt;
function on_mouse_down() {
nat box_size = 20;
nat x = 1;
nat y = 1;
byte color = BLACK;
outlined_swatch(screen, color, x, y);
set_color(box_size, x, y, mouse.x, mouse.y, color);
color = WHITE;
x = 21;
outlined_swatch(screen, color, x, y);
set_color(box_size, x, y, mouse.x, mouse.y, color);
rectangle(screen, selected_color, x, y, 5, 5);
}
function render_screen() {
outline_swatch(screen, BLACK, 1, 1);
outline_swatch(screen, WHITE, 21, 1);
}
/**
* Checks if the click is within the bound and update the selected color if so.
*/
function set_color(nat box_size, nat bx, nat by, nat mx, nat my, byte color) {
nat right = bx + box_size;
nat bottom = by + box_size;
if (mx < bx) { return; }
if (mx > right) { return; }
if (my < by) { return; }
if (my > bottom) { return; }
selected_color = color;
}
/**
* Draw a color box with a grey outline, if selected use a darker color
*/
function outline_swatch(byte color, nat x, nat y) {
byte bg_color = GRAY;
if (selected_color == color) {
bg_color = DARK_GRAY;
}
rectangle(bg_color, x, y, 20, 20);
rectangle(color, x + 2, y + 2, 17, 17);
}
/**
* Draw a rectangle
*/
function rectangle(byte color, nat x, nat y, nat width, nat height) {
nat base = y * screen.width + x;
for (nat i = height; i > 0; i--) {
nat row = base + width;
for (nat j = width; j < row; j++) {
screen.x = i;
screen.y = j;
screen.pixel = color;
}
base += screen.width;
}
}