feat: Buttons + events

wip: malloc

feat: Buttons
This commit is contained in:
2024-01-13 13:51:44 +03:00
parent c6351affe3
commit 59d825577a
14 changed files with 502 additions and 42 deletions

View File

@@ -1,5 +1,8 @@
use crate::graphics::{Color, Dot, Size};
use crate::sys;
use crate::system::debug_write;
use crate::throw_new;
use cstr_core::{cstr, CStr};
#[repr(u32)]
pub enum WindowKind {
@@ -16,6 +19,8 @@ pub struct WindowParams<'a> {
pub title: Option<&'a cstr_core::CStr>,
}
pub const CLOSE_BUTTON: u32 = 1;
pub fn define_window(start: Dot, size: Size, params: WindowParams<'_>) {
const RELATIVE_FLAG: u32 = 0x20;
@@ -34,6 +39,68 @@ pub fn define_window(start: Dot, size: Size, params: WindowParams<'_>) {
}
}
pub fn define_button(
start: Dot,
size: Size,
id: u32,
draw: bool,
border: bool,
color: Option<Color>,
) {
if 0 >= size.width || size.width >= 0x8000 || 0 >= size.height || size.height >= 0x8000 {
crate::graphics::display_message(
Dot { x: 10, y: 200 },
Color::rgb(255, 0, 0),
CStr::from_bytes_with_nul(
format!(
"x:{:?} y:{:?} w:{:?} h:{:?}\n\0",
start.x, start.y, size.width, size.height
)
.as_bytes(),
)
.unwrap_or(cstr!("String error")),
None,
);
throw_new!(format!(
"x:{:?} y:{:?} w:{:?} h:{:?}\n",
start.x, start.y, size.width, size.height
));
return;
}
if id > 0xFFFFFF {
throw_new!("Invalid button ID");
return;
}
let mut flags = 0;
if !draw {
flags += 1 << 30
};
if !border {
flags += 1 << 29
};
unsafe {
sys::define_button(
start.x << 16 | size.width,
start.y << 16 | size.height,
flags << 29 | id,
color.unwrap_or(Color::rgb(255, 255, 255)).as_rgb_val(),
);
}
}
// TODO: mouse button info
pub fn get_button_id() -> Option<u32> {
unsafe {
let eax = sys::get_button_id();
if eax == 1 {
return None;
}
return Some(eax >> 8);
}
}
pub fn start_window_draw() {
unsafe { sys::start_window_draw() }
}