264 lines
6.5 KiB
C
264 lines
6.5 KiB
C
str screen_namespace = "/dev/screen/0";
|
|
str mouse_namespace = "/dev/mouse/0";
|
|
byte BLACK = 0;
|
|
byte WHITE = 255;
|
|
byte DARK_GRAY = 73;
|
|
byte GRAY = 146;
|
|
byte LIGHT_GRAY = 182;
|
|
byte CHARCOAL = 36;
|
|
byte DARK_RED = 128;
|
|
byte RED = 224;
|
|
byte DARK_YELLOW = 144;
|
|
byte YELLOW = 252;
|
|
byte DARK_TEAL = 9;
|
|
byte TEAL = 18;
|
|
byte DARK_GREEN = 12;
|
|
byte GREEN = 16;
|
|
byte LIME = 28;
|
|
byte LIGHT_CYAN = 159;
|
|
byte NAVY = 2;
|
|
byte BLUE = 3;
|
|
byte DEEP_SKY_BLUE = 10;
|
|
byte LIGHT_BLUE = 19;
|
|
byte PURPLE = 131;
|
|
byte LIGHT_PURPLE = 147;
|
|
byte DARK_MAGENTA = 130;
|
|
byte MAGENTA = 227;
|
|
byte PLUM = 129;
|
|
byte PINK = 226;
|
|
byte SADDLE_BROWN = 72;
|
|
byte PERU = 141;
|
|
byte SIENNA = 136;
|
|
byte ORANGE = 241;
|
|
byte DARK_ORANGE = 208;
|
|
byte GOLD = 244;
|
|
byte SELECTED_COLOR = 255;
|
|
|
|
function main () {
|
|
// Open screen
|
|
int mode = 0;
|
|
ptr screen = open(screen_namespace, mode);
|
|
|
|
nat width = &screen + 8;
|
|
nat size = &screen + 12;
|
|
nat screen_buffer = &screen + 16;
|
|
|
|
// open mouse
|
|
ptr mouse = open(mouse_namespace, mode); // Mouse mouse = open("/dev/mouse/0", 0);
|
|
|
|
byte color;
|
|
nat x_pos;
|
|
nat y_pos;
|
|
|
|
color = BLACK;
|
|
x_pos = 1;
|
|
y_pos = 1;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = WHITE;
|
|
x_pos = 21;
|
|
y_pos = 1;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = CHARCOAL;
|
|
x_pos = 1;
|
|
y_pos = 21;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = DARK_GRAY;
|
|
x_pos = 21;
|
|
y_pos = 21;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = RED;
|
|
x_pos = 1;
|
|
y_pos = 41;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = ORANGE;
|
|
x_pos = 21;
|
|
y_pos = 41;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = YELLOW;
|
|
x_pos = 1;
|
|
y_pos = 61;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = GREEN;
|
|
x_pos = 21;
|
|
y_pos = 61;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = BLUE;
|
|
x_pos = 1;
|
|
y_pos = 81;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
color = PURPLE;
|
|
x_pos = 21;
|
|
y_pos = 81;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
|
|
// screen.draw
|
|
write(screen, screen_buffer, size);
|
|
|
|
nat zero = 0;
|
|
|
|
loop draw_loop {
|
|
// load mouse click data
|
|
stat(mouse);
|
|
|
|
byte left_down = &mouse + 16; // load btn1 pressed
|
|
|
|
jump_eq_nat(draw_loop, left_down, zero); // if (!btn1.left) continue;
|
|
|
|
nat mouse_x = &mouse + 8; // load x
|
|
nat mouse_y = &mouse + 12; // load y
|
|
|
|
nat box_size = 20;
|
|
|
|
// first row
|
|
color = BLACK;
|
|
x_pos = 1;
|
|
y_pos = 1;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = WHITE;
|
|
x_pos = 21;
|
|
y_pos = 1;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = CHARCOAL;
|
|
x_pos = 1;
|
|
y_pos = 21;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = DARK_GRAY;
|
|
x_pos = 21;
|
|
y_pos = 21;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = RED;
|
|
x_pos = 1;
|
|
y_pos = 41;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = ORANGE;
|
|
x_pos = 21;
|
|
y_pos = 41;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = YELLOW;
|
|
x_pos = 1;
|
|
y_pos = 61;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = GREEN;
|
|
x_pos = 21;
|
|
y_pos = 61;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = BLUE;
|
|
x_pos = 1;
|
|
y_pos = 81;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
color = PURPLE;
|
|
x_pos = 21;
|
|
y_pos = 81;
|
|
draw_outlined_swatch (screen_buffer, color, x_pos, y_pos, width);
|
|
set_color_if_clicked (mouse_x, mouse_y, x_pos, y_pos, color, box_size);
|
|
|
|
write(screen, screen_buffer, size);
|
|
|
|
byte selected_color = SELECTED_COLOR;
|
|
nat brush_size = 5;
|
|
|
|
draw_box(screen_buffer, width, selected_color, mouse_x, mouse_y, brush_size, brush_size);
|
|
|
|
jump draw_loop;
|
|
}
|
|
|
|
// Flush and exit
|
|
exit 0;
|
|
}
|
|
|
|
function set_color_if_clicked (int click_x, int click_y, int box_x, int box_y, byte check_color, int size) {
|
|
|
|
// Compute right
|
|
int right_edge = box_x + size;
|
|
|
|
// Compute bottom = box_y + size
|
|
int bottom_edge = box_y + size;
|
|
|
|
// Bounds check: x in [box_x, right] and y in [box_y, bottom]
|
|
jump_lt_int(fail, click_x, box_x);
|
|
jump_gt_int(fail, click_x, right_edge);
|
|
jump_lt_int(fail, click_y, box_y);
|
|
jump_gt_int(fail, click_y, bottom_edge);
|
|
|
|
SELECTED_COLOR = check_color;
|
|
|
|
else fail
|
|
return;
|
|
}
|
|
|
|
function draw_outlined_swatch(nat base, byte color, int x, int y, int width) {
|
|
|
|
// Constants
|
|
const byte selected_color = SELECTED_COLOR;
|
|
byte background_color = GRAY;
|
|
|
|
jump_eq_int set_selected color selected_color;
|
|
jump end_set_selected;
|
|
do set_selected
|
|
background_color = DARK_GRAY;
|
|
else end_set_selected
|
|
|
|
const nat outline_size = 20;
|
|
const nat fill_size = 17;
|
|
|
|
draw_box(base, width, background_color, x, y, outline_size, outline_size);
|
|
|
|
const nat offset = 2;
|
|
int x_off = x + offset;
|
|
int y_off = y + offset;
|
|
|
|
draw_box(base, width, color, x_off, y_off, fill_size, fill_size);
|
|
|
|
return;
|
|
}
|
|
|
|
function draw_box (nat db_base, nat screen_width,
|
|
byte box_color, nat x_start, nat y_start,
|
|
nat db_width, nat height) {
|
|
|
|
// Compute start address: base + y*640 + x
|
|
nat fat_ptr_size = 4;
|
|
|
|
nat offset = y_start * screen_width;
|
|
offset = offset + x_start;
|
|
offset = offset + db_base;
|
|
offset = offset + fat_ptr_size; // need to add offset for fat pointer size
|
|
|
|
int i = 0;
|
|
const int zero = 0;
|
|
|
|
loop draw_box_outer {
|
|
memset_8 box_color db_width -> offset; // draw row
|
|
add_int offset screen_width -> offset; // next row += 640
|
|
sub_int height i -> height; // decrement row count
|
|
jump_gt_int draw_box_outer height zero;
|
|
}
|
|
return;
|
|
} |