undar-lang/docs/draw.ul

79 lines
1.9 KiB
Fortran

str screen_namespace = "/dev/screen/0";
str mouse_namespace = "/dev/mouse/0";
byte selected_color = 255;
const byte BLACK = 0;
const byte WHITE = 255;
const byte DARK_GRAY = 73;
const byte GRAY = 146;
const byte LIGHT_GRAY = 182;
function main() {
Device screen(screen_namespace);
screen.open(0);
Device mouse(mouse_namespace);
mouse.open(0);
outline_swatch(screen, BLACK, 1, 1);
outline_swatch(screen, WHITE, 21, 1);
screen.draw();
while (true) {
mouse.read();
if (mouse.btn1) {
int box_size = 20;
int x = 1;
int y = 1;
byte color = BLACK;
outlined_swatch(screen, color, x, y);
set_color_if_clicked(box_size, x, y, mouse.x, mouse.y, color, mouse.btn1);
color = WHITE;
x = 21;
outlined_swatch(screen, color, x, y);
set_color_if_clicked(box_size, x, y, mouse.x, mouse.y, color, mouse.btn1);
screen.draw();
box(screen, selected_color, x, y, 5, 5);
}
}
exit(0);
}
function set_color_if_clicked(int box_size, int bx, int by, int mx, int my, byte color, bool btn1_down) {
int right = bx + box_size;
int bottom = by + box_size;
if (mx < mx) return;
if (mx > right) return;
if (my < by) return;
if (my > bottom) return;
if (not btn1_down) return;
selected_color = color;
return;
}
function outline_swatch(ref Device screen, byte color, int x, int y) {
byte bg_color = GRAY;
if (selected_color == color) {
bg_color = DARK_GRAY;
}
box(screen, bg_color, x, y, 20, 20);
box(screen, color, x + 2, y + 2, 17, 17);
return;
}
function box(ref Device screen, byte color, int x, int y, int width, int height) {
int pixel = y * width + x + &screen.buffer + 4;
do (int i = height, i > 0, i--) {
int row = pixel + width;
screen.set(row, color, width);
pixel += width;
}
return;
}