Core/examples/hwa.rs

108 lines
2.4 KiB
Rust
Raw Normal View History

2021-09-10 16:32:09 +02:00
#![no_std]
#![no_main]
use cstr_core::{cstr, CStr};
use kos::{
graphics::{display_message, Color, Dot, Size},
input::fetch_key,
2024-01-27 22:13:35 +01:00
system::{get_lang, Lang},
threads::{exit, fetch_event, Event},
windows::{
define_button, define_window, end_window_draw, get_button_id, start_window_draw,
WindowKind, WindowParams, CLOSE_BUTTON,
},
};
2021-09-10 16:32:09 +02:00
const HEADER: &CStr = cstr!("Hey Kolibri");
2024-01-12 09:39:38 +01:00
const MSG: &CStr = cstr!("Hello from Rust!");
const BTN: u32 = 42;
2021-09-10 16:32:09 +02:00
#[macro_use]
extern crate alloc;
fn draw_window(c: usize) {
2024-01-12 09:39:38 +01:00
start_window_draw();
2024-01-12 09:39:38 +01:00
define_window(
Dot { x: 50, y: 50 },
Size {
width: 300,
height: 400,
},
WindowParams {
color: Color::rgb(0xff, 0xff, 0xff),
kind: WindowKind::Themed,
title: Some(HEADER),
},
);
display_message(
Dot { x: 10, y: 10 },
Color::rgb(0x66, 0x22, 0x22),
MSG,
None,
);
2024-01-27 22:13:35 +01:00
let btn_str = match get_lang() {
Lang::German => format!("Taste gedrückt: {} mal\0", c),
Lang::Russian => format!("Кнопка нажата: {} раз\0", c),
Lang::French => format!("Button enfoncé : {} fois\0", c),
_ => format!("Button pressed: {} times\0", c),
};
display_message(
Dot { x: 10, y: 30 },
Color::rgb(0, 0, 0),
2024-01-27 22:13:35 +01:00
CStr::from_bytes_with_nul(btn_str.as_bytes()).unwrap_or(cstr!("String error")),
None,
);
define_button(
Dot { x: 10, y: 70 },
Size {
width: 70,
height: 15,
},
BTN,
true,
true,
Some(Color::rgb(128, 255, 128)),
);
2024-01-12 09:39:38 +01:00
end_window_draw();
return;
}
fn button_handler(c: &mut usize) {
let btn_id = get_button_id();
if btn_id.is_some() {
match btn_id.unwrap() {
CLOSE_BUTTON => exit(),
BTN => {
*c += 1;
draw_window(*c);
}
_ => {}
}
}
2021-09-10 16:32:09 +02:00
}
#[no_mangle]
fn kol_main() {
let mut c = 0;
2021-09-10 16:32:09 +02:00
2024-03-11 21:50:22 +01:00
while let Some(ev) =
fetch_event((Event::Redraw as u32) | (Event::KeyPress as u32) | (Event::BtnPress as u32))
{
2021-09-10 16:32:09 +02:00
match ev {
Event::Redraw => draw_window(c),
2024-01-12 09:39:38 +01:00
Event::KeyPress => drop(fetch_key()),
Event::BtnPress => button_handler(&mut c),
2021-09-10 16:32:09 +02:00
_ => break,
}
}
}