feat: Buttons + events
wip: malloc feat: Buttons
This commit is contained in:
33
src/modules/system.rs
Normal file
33
src/modules/system.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use crate::sys;
|
||||
use alloc::string::String;
|
||||
use cstr_core::CStr;
|
||||
|
||||
trait Debuggable {
|
||||
fn data_iter(self) -> impl Iterator<Item = u8>;
|
||||
}
|
||||
|
||||
impl Debuggable for &str {
|
||||
fn data_iter(self) -> impl Iterator<Item = u8> {
|
||||
self.bytes()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debuggable for &String {
|
||||
fn data_iter(self) -> impl Iterator<Item = u8> {
|
||||
self.as_bytes().iter().copied()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debuggable for &CStr {
|
||||
fn data_iter(self) -> impl Iterator<Item = u8> {
|
||||
self.to_bytes().iter().copied()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn debug_write<Str: Debuggable>(text: Str) {
|
||||
for byte in text.data_iter() {
|
||||
unsafe {
|
||||
sys::_debug_write(byte);
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,12 +8,24 @@ pub fn exit() -> ! {
|
||||
pub enum Event {
|
||||
Redraw,
|
||||
KeyPress,
|
||||
BtnPress,
|
||||
BgRedraw,
|
||||
Mouse,
|
||||
IPC,
|
||||
Network,
|
||||
Debug,
|
||||
}
|
||||
|
||||
pub fn fetch_event() -> Option<Event> {
|
||||
match unsafe { sys::wait_event() } {
|
||||
1 => Some(Event::Redraw),
|
||||
2 => Some(Event::KeyPress),
|
||||
3 => Some(Event::BtnPress),
|
||||
5 => Some(Event::BgRedraw),
|
||||
6 => Some(Event::Mouse),
|
||||
7 => Some(Event::IPC),
|
||||
8 => Some(Event::Network),
|
||||
9 => Some(Event::Debug),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@@ -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() }
|
||||
}
|
||||
|
Reference in New Issue
Block a user