tick system

This commit is contained in:
2024-11-03 11:03:54 +01:00
parent da7e293b3b
commit 196c6094d2
5 changed files with 91 additions and 70 deletions
+25 -16
View File
@@ -131,24 +131,33 @@ impl App {
// Start the render loop // Start the render loop
let mut tick: usize = 0; let mut tick: usize = 0;
let tick_duration = std::time::Duration::from_millis(1000/30);
let mut last_tick = std::time::Instant::now();
loop { loop {
if tick > 99 { let now = std::time::Instant::now();
tick = 0; let elapsed = now.duration_since(last_tick);
if elapsed >= tick_duration {
last_tick = now;
if tick > 99 {
tick = 0;
}
self.render(tick);
tick += 1;
match rx.try_recv() {
Ok(k) => self.update(&mut UpdateContext {
key: k,
tick,
response: UpdateResponse::None,
}),
Err(std::sync::mpsc::TryRecvError::Empty) => {}
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
panic!("disconnected")
}
}
} }
self.render(tick); if let Some(remaining) = tick_duration.checked_sub(elapsed) {
tick += 1; std::thread::sleep(remaining);
match rx.try_recv() {
Ok(k) => self.update(&mut UpdateContext {
key: k,
tick,
response: UpdateResponse::None,
}),
Err(std::sync::mpsc::TryRecvError::Empty) => {
std::thread::sleep(std::time::Duration::from_millis(10));
}
Err(std::sync::mpsc::TryRecvError::Disconnected) => {
panic!("disconnected")
}
} }
} }
} }
+53 -40
View File
@@ -1,67 +1,76 @@
#[macro_export] #[macro_export]
macro_rules! __oml { macro_rules! __rsx {
() => { [String::new(), Vec::new()] }; () => {
(String::new(), Vec::new())
};
($p:expr;) => {{
(String::new(), vec![$p as Box<dyn osui::Element>])
}};
($p:expr; $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Element>> = osui::__rsx!($($rest)+).1;
comps.insert(0, $p as Box<dyn osui::Element>);
(String::new(), comps)
}};
// Props, With components (PC) // Props, With components (PC)
($tag:ident { $($inner:tt)* }) => {{ ($tag:path { $($inner:tt)* }) => {{
(String::new(), vec![rsx!($tag {$($inner)*}) as Box<dyn osui::Element>]) (String::new(), vec![rsx!($tag {$($inner)*})])
}}; }};
($tag:ident { $($inner:tt)* } $($rest:tt)+) => {{ ($tag:path { $($inner:tt)* } $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Element>> = osui::__oml!($($rest)+).1; let mut comps: Vec<Box<dyn osui::Element>> = osui::__rsx!($($rest)+).1;
comps.insert(0, rsx!($tag {$($inner)*}) as Box<dyn osui::Element> ); comps.insert(0, rsx!($tag {$($inner)*}) );
(String::new(), comps)
}};
($tag:path { $($inner:tt)* }) => {{
(String::new(), vec![rsx!($tag {$($inner)*})])
}};
($tag:path { $($inner:tt)* } $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Element>> = osui::__rsx!($($rest)+).1;
comps.insert(0, rsx!($tag {$($inner)*}) );
(String::new(), comps) (String::new(), comps)
}}; }};
($text:expr) => {{ ($text:expr) => {{
(format!($text), vec![]) (format!($text), Vec::new())
}}; }};
} }
#[macro_export] #[macro_export]
/// Write OSUI Markup Language directly with rust. /// Write OSUI Markup Language directly with rust.
macro_rules! rsx { macro_rules! rsx {
( $tag:path { $($k:ident: $v:expr),*; $($inner:tt)* } ) => {{
( $tag:ident { $($inner:tt)* } ) => {{
let mut c = $tag(); let mut c = $tag();
let a = osui::__oml!($($inner)*); let a = osui::__rsx!($($inner)*);
c.text = a.0; c.text = a.0;
c.children = a.1; c.children = a.1;
c
}};
( $tag:ident { $($k:ident: $v:expr),*, $($inner:tt)* } ) => {{
let mut c = $tag();
c.children = osui::__oml!($($inner)*);
c.text = String::from($text);
$( $(
c.$k = $v; c.$k = $v;
)* )*
c c as Box<dyn osui::Element>
}}; }};
// ( $tag:ident { $text:expr } ) => {{ ( $tag:path { $($k:ident: $v:expr),* } ) => {{
// let mut c = $tag(); let mut c = $tag();
// c.text = format!($text); $(
// c c.$k = $v;
// }}; )*
c as Box<dyn osui::Element>
}};
// ( $tag:ident { $($k:ident: $v:expr),+, $text:expr } ) => {{ ( $tag:path { $($inner:tt)* } ) => {{
// let mut c = $tag(); let mut c = $tag();
// c.text = format!($text); let a = osui::__rsx!($($inner)*);
// $( c.text = a.0;
// c.$k = $v; c.children = a.1;
// )* c as Box<dyn osui::Element>
// c }};
// }};
// ($tag:ident { $($k:ident: $v:expr),* } ) => {{ ( $p:expr; ) => {{
// let mut c = $tag(); $p as Box<dyn osui::Element>
// $( }};
// c.$k = $v;
// )*
// c
// }};
} }
#[macro_export] #[macro_export]
@@ -155,7 +164,11 @@ macro_rules! element {
} }
pub fn add_action(&mut self, tick: usize, action: &str) { pub fn add_action(&mut self, tick: usize, action: &str) {
self.tick_line.insert(tick, action.to_string()); if tick > 99 {
self.tick_line.insert(tick-100, action.to_string());
} else {
self.tick_line.insert(tick, action.to_string());
}
} }
} }
}; };
+5 -4
View File
@@ -1,9 +1,10 @@
use osui::{rsx, ui::*, App}; use osui::{rsx, ui::*, App};
fn main() { fn main() {
let mut app_screen = App::new(); let element = rsx! {
app_screen.set_component(rsx! { button { "Hello, World!" }
button { "test" } };
});
let mut app_screen = App::from(element);
app_screen.run(); app_screen.run();
} }
+6 -9
View File
@@ -1,17 +1,13 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::{ use crate::{element, key::KeyKind, ui::Style, Element, ElementData, UpdateContext};
element,
key::KeyKind,
ui::Style,
Element, ElementData, UpdateContext,
};
element! { element! {
Text {} Text {}
defaults {} defaults {}
fn render(&mut self, _: usize) -> String { fn render(&mut self, tick: usize) -> String {
self.style.write(&self.text) // self.style.write(&self.text)
self.style.write(&format!("{tick}"))
} }
} }
@@ -31,7 +27,7 @@ element! {
if let Some(v) = self.binds.get(&ctx.key.kind) { if let Some(v) = self.binds.get(&ctx.key.kind) {
if v == "click" { if v == "click" {
self.clicked = true; self.clicked = true;
self.add_action(ctx.tick+5, "un_click"); self.add_action(ctx.tick+2, "un_click");
(self.on_click)(self); (self.on_click)(self);
} }
} }
@@ -44,6 +40,7 @@ element! {
} }
if self.clicked { if self.clicked {
// return self.style.write(&format!("{tick}"))
return self.style.write_clicked(&self.text); return self.style.write_clicked(&self.text);
} }
self.style.write(&self.text) self.style.write(&self.text)
+2 -1
View File
@@ -27,6 +27,7 @@ pub fn text() -> Box<Text> {
/// A boxed `Button` element, initialized and ready for configuration. /// A boxed `Button` element, initialized and ready for configuration.
pub fn button() -> Box<Button> { pub fn button() -> Box<Button> {
let mut btn = Button::new(); let mut btn = Button::new();
btn.style.clicked_bg = styles::Color::Red; btn.style.clicked_bg = styles::Color::White;
btn.style.clicked_fg = styles::Color::Black;
Box::new(btn) Box::new(btn)
} }