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
let mut tick: usize = 0;
let tick_duration = std::time::Duration::from_millis(1000/30);
let mut last_tick = std::time::Instant::now();
loop {
if tick > 99 {
tick = 0;
let now = std::time::Instant::now();
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);
tick += 1;
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")
}
if let Some(remaining) = tick_duration.checked_sub(elapsed) {
std::thread::sleep(remaining);
}
}
}
+53 -40
View File
@@ -1,67 +1,76 @@
#[macro_export]
macro_rules! __oml {
() => { [String::new(), Vec::new()] };
macro_rules! __rsx {
() => {
(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)
($tag:ident { $($inner:tt)* }) => {{
(String::new(), vec![rsx!($tag {$($inner)*}) as Box<dyn osui::Element>])
($tag:path { $($inner:tt)* }) => {{
(String::new(), vec![rsx!($tag {$($inner)*})])
}};
($tag:ident { $($inner:tt)* } $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Element>> = osui::__oml!($($rest)+).1;
comps.insert(0, rsx!($tag {$($inner)*}) as Box<dyn osui::Element> );
($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)
}};
($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)
}};
($text:expr) => {{
(format!($text), vec![])
(format!($text), Vec::new())
}};
}
#[macro_export]
/// Write OSUI Markup Language directly with rust.
macro_rules! rsx {
( $tag:ident { $($inner:tt)* } ) => {{
( $tag:path { $($k:ident: $v:expr),*; $($inner:tt)* } ) => {{
let mut c = $tag();
let a = osui::__oml!($($inner)*);
let a = osui::__rsx!($($inner)*);
c.text = a.0;
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
c as Box<dyn osui::Element>
}};
// ( $tag:ident { $text:expr } ) => {{
// let mut c = $tag();
// c.text = format!($text);
// c
// }};
( $tag:path { $($k:ident: $v:expr),* } ) => {{
let mut c = $tag();
$(
c.$k = $v;
)*
c as Box<dyn osui::Element>
}};
// ( $tag:ident { $($k:ident: $v:expr),+, $text:expr } ) => {{
// let mut c = $tag();
// c.text = format!($text);
// $(
// c.$k = $v;
// )*
// c
// }};
( $tag:path { $($inner:tt)* } ) => {{
let mut c = $tag();
let a = osui::__rsx!($($inner)*);
c.text = a.0;
c.children = a.1;
c as Box<dyn osui::Element>
}};
// ($tag:ident { $($k:ident: $v:expr),* } ) => {{
// let mut c = $tag();
// $(
// c.$k = $v;
// )*
// c
// }};
( $p:expr; ) => {{
$p as Box<dyn osui::Element>
}};
}
#[macro_export]
@@ -155,7 +164,11 @@ macro_rules! element {
}
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};
fn main() {
let mut app_screen = App::new();
app_screen.set_component(rsx! {
button { "test" }
});
let element = rsx! {
button { "Hello, World!" }
};
let mut app_screen = App::from(element);
app_screen.run();
}
+6 -9
View File
@@ -1,17 +1,13 @@
use std::collections::HashMap;
use crate::{
element,
key::KeyKind,
ui::Style,
Element, ElementData, UpdateContext,
};
use crate::{element, key::KeyKind, ui::Style, Element, ElementData, UpdateContext};
element! {
Text {}
defaults {}
fn render(&mut self, _: usize) -> String {
self.style.write(&self.text)
fn render(&mut self, tick: usize) -> String {
// 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 v == "click" {
self.clicked = true;
self.add_action(ctx.tick+5, "un_click");
self.add_action(ctx.tick+2, "un_click");
(self.on_click)(self);
}
}
@@ -44,6 +40,7 @@ element! {
}
if self.clicked {
// return self.style.write(&format!("{tick}"))
return self.style.write_clicked(&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.
pub fn button() -> Box<Button> {
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)
}