From a5612d965fbfd96f1b4c2138ba480b72a228bb87 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 28 Nov 2023 20:13:14 +0100 Subject: [PATCH] major ui restructure, wip interactable api --- Cargo.lock | 49 +++++++++--------- Cargo.toml | 2 +- README.md | 1 + kubi-ui-glium/Cargo.toml | 11 ++++ .../shaders/fragment.frag | 0 .../shaders/vertex.vert | 0 .../glium.rs => kubi-ui-glium/src/lib.rs | 7 ++- kubi-ui/Cargo.toml | 7 ++- kubi-ui/examples/stress.rs | 8 ++- kubi-ui/examples/test.rs | 17 +++++-- kubi-ui/src/backend.rs | 2 - kubi-ui/src/interaction.rs | 51 +++++++++++++++++++ kubi-ui/src/lib.rs | 2 +- kubi/Cargo.toml | 1 + kubi/src/guiv2_integration.rs | 3 +- 15 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 kubi-ui-glium/Cargo.toml rename {kubi-ui => kubi-ui-glium}/shaders/fragment.frag (100%) rename {kubi-ui => kubi-ui-glium}/shaders/vertex.vert (100%) rename kubi-ui/src/backend/glium.rs => kubi-ui-glium/src/lib.rs (95%) delete mode 100644 kubi-ui/src/backend.rs create mode 100644 kubi-ui/src/interaction.rs diff --git a/Cargo.lock b/Cargo.lock index da2aab8..afed83d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -543,12 +543,12 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "fontdue" -version = "0.7.3" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0793f5137567643cf65ea42043a538804ff0fbf288649e2141442b602d81f9bc" +checksum = "9099a2f86b8e674b75d03ff154b3fe4c5208ed249ced8d69cc313a9fa40bb488" dependencies = [ - "hashbrown 0.13.2", - "ttf-parser 0.15.2", + "hashbrown", + "ttf-parser", ] [[package]] @@ -766,15 +766,6 @@ dependencies = [ "byteorder", ] -[[package]] -name = "hashbrown" -version = "0.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" -dependencies = [ - "ahash", -] - [[package]] name = "hashbrown" version = "0.14.2" @@ -850,7 +841,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" dependencies = [ "equivalent", - "hashbrown 0.14.2", + "hashbrown", ] [[package]] @@ -958,11 +949,12 @@ dependencies = [ "glium", "glutin", "glutin-winit", - "hashbrown 0.14.2", + "hashbrown", "image", "kubi-logging", "kubi-shared", "kubi-ui", + "kubi-ui-glium", "log", "lz4_flex", "ndk", @@ -1000,7 +992,7 @@ dependencies = [ "anyhow", "flume", "glam", - "hashbrown 0.14.2", + "hashbrown", "kubi-logging", "kubi-shared", "log", @@ -1023,7 +1015,7 @@ dependencies = [ "bincode", "bracket-noise", "glam", - "hashbrown 0.14.2", + "hashbrown", "nohash-hasher", "num_enum", "nz", @@ -1043,14 +1035,25 @@ dependencies = [ "fontdue", "glam", "glium", - "hashbrown 0.14.2", + "hashbrown", "kubi-logging", + "kubi-ui-glium", "log", "nohash-hasher", "rect_packer", "winit", ] +[[package]] +name = "kubi-ui-glium" +version = "0.1.0" +dependencies = [ + "glam", + "glium", + "kubi-ui", + "log", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -1384,7 +1387,7 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4586edfe4c648c71797a74c84bacb32b52b212eff5dfe2bb9f2c599844023e7" dependencies = [ - "ttf-parser 0.20.0", + "ttf-parser", ] [[package]] @@ -1766,7 +1769,7 @@ name = "shipyard" version = "0.6.0" source = "git+https://github.com/leudz/shipyard?rev=8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49#8ef90ea6c4d1eb6c9cb0988f0d2f873f75044d49" dependencies = [ - "hashbrown 0.14.2", + "hashbrown", "lock_api", "rayon", "serde", @@ -2035,12 +2038,6 @@ version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" -[[package]] -name = "ttf-parser" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b3e06c9b9d80ed6b745c7159c40b311ad2916abb34a49e9be2653b90db0d8dd" - [[package]] name = "ttf-parser" version = "0.20.0" diff --git a/Cargo.toml b/Cargo.toml index 0cf01a1..51aadc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["kubi", "kubi-server", "kubi-shared", "kubi-logging", "kubi-pool", "kubi-ui"] +members = ["kubi", "kubi-server", "kubi-shared", "kubi-logging", "kubi-pool", "kubi-ui", "kubi-ui-glium"] default-members = ["kubi"] resolver = "2" diff --git a/README.md b/README.md index ece3c44..3545100 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,7 @@ name = "Kubi Server" # server name - [`kubi-ui`](kubi-ui): semi-imm.mode backend-agnostic ui system\ mostly ready to use, it has already replaced the Kubi legacy ui +- [`kubi-ui-glium`](kubi-ui-backend-glium) Glium-based backend for `kubi-ui` - [`kubi-pool`](kubi-pool): very early work-in-progress work-stealing threadpool system\ aiming to replace `rayon` threadpool that's currently used inside the kubi client (for more control over task priority) - [`kubi-logging`](kubi-logging) fancy-ass custom formatter for `env-logger` diff --git a/kubi-ui-glium/Cargo.toml b/kubi-ui-glium/Cargo.toml new file mode 100644 index 0000000..13c29b8 --- /dev/null +++ b/kubi-ui-glium/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "kubi-ui-glium" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" } +kubi-ui = { path = "../kubi-ui", no-default-features = true } +glam = "0.24" +log = "0.4" diff --git a/kubi-ui/shaders/fragment.frag b/kubi-ui-glium/shaders/fragment.frag similarity index 100% rename from kubi-ui/shaders/fragment.frag rename to kubi-ui-glium/shaders/fragment.frag diff --git a/kubi-ui/shaders/vertex.vert b/kubi-ui-glium/shaders/vertex.vert similarity index 100% rename from kubi-ui/shaders/vertex.vert rename to kubi-ui-glium/shaders/vertex.vert diff --git a/kubi-ui/src/backend/glium.rs b/kubi-ui-glium/src/lib.rs similarity index 95% rename from kubi-ui/src/backend/glium.rs rename to kubi-ui-glium/src/lib.rs index 88f79de..643ceec 100644 --- a/kubi-ui/src/backend/glium.rs +++ b/kubi-ui-glium/src/lib.rs @@ -6,11 +6,10 @@ use glium::{ index::PrimitiveType, implement_vertex, uniform, }; +use kubi_ui::draw::{UiDrawPlan, UiVertex}; -use crate::draw::{UiDrawPlan, UiVertex}; - -const VERTEX_SHADER: &str = include_str!("../../shaders/vertex.vert"); -const FRAGMENT_SHADER: &str = include_str!("../../shaders/fragment.frag"); +const VERTEX_SHADER: &str = include_str!("../shaders/vertex.vert"); +const FRAGMENT_SHADER: &str = include_str!("../shaders/fragment.frag"); #[derive(Clone, Copy)] #[repr(C)] diff --git a/kubi-ui/Cargo.toml b/kubi-ui/Cargo.toml index f0eadda..f70dbd5 100644 --- a/kubi-ui/Cargo.toml +++ b/kubi-ui/Cargo.toml @@ -8,19 +8,18 @@ publish = false hashbrown = "0.14" nohash-hasher = "0.2" glam = "0.24" -glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf", optional = true } -fontdue = "0.7" +fontdue = "0.8" rect_packer = "0.2" log = "0.4" [dev-dependencies] kubi-logging = { path = "../kubi-logging" } +kubi-ui-glium = { path = "../kubi-ui-glium" } glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" } winit = "0.29" [features] -default = ["builtin_elements", "builtin_font", "backend_glium"] -backend_glium = ["dep:glium"] +default = ["builtin_elements", "builtin_font"] builtin_font = [] builtin_elements = [] #parallel = ["dep:rayon", "fontdue/parallel"] diff --git a/kubi-ui/examples/stress.rs b/kubi-ui/examples/stress.rs index 622f4b6..8c00100 100644 --- a/kubi-ui/examples/stress.rs +++ b/kubi-ui/examples/stress.rs @@ -7,8 +7,14 @@ use winit::{ }; use kubi_ui::{ KubiUi, - backend::glium::GliumUiRenderer, element::{progress_bar::ProgressBar, container::{Container, Sides}, UiElement}, UiSize + element::{ + UiElement, + progress_bar::ProgressBar, + container::{Container, Sides} + }, + UiSize }; +use kubi_ui_glium::GliumUiRenderer; fn main() { kubi_logging::init(); diff --git a/kubi-ui/examples/test.rs b/kubi-ui/examples/test.rs index be02dd7..443f90f 100644 --- a/kubi-ui/examples/test.rs +++ b/kubi-ui/examples/test.rs @@ -1,5 +1,5 @@ use std::time::Instant; -use glam::{Vec2, IVec2, UVec2, vec4}; +use glam::{UVec2, vec4}; use glium::{backend::glutin::SimpleWindowBuilder, Surface}; use winit::{ event::{Event, WindowEvent}, @@ -7,8 +7,17 @@ use winit::{ }; use kubi_ui::{ KubiUi, - backend::glium::GliumUiRenderer, element::{progress_bar::ProgressBar, container::{Container, Sides, Alignment}, UiElement, rect::Rect}, UiSize, UiDirection + element::{ + UiElement, + progress_bar::ProgressBar, + container::{Container, Sides, Alignment}, + rect::Rect + }, + interaction::IntoInteractable, + UiSize, + UiDirection, }; +use kubi_ui_glium::GliumUiRenderer; fn main() { kubi_logging::init(); @@ -109,7 +118,9 @@ fn main() { Box::new(Rect { size: (UiSize::Pixels(50.), UiSize::Pixels(50.)), color: Some(vec4(1., 1., 1., 0.75)) - }) + }.into_interactable().on_click(|| { + println!("clicked"); + })) ], ..Default::default() }) diff --git a/kubi-ui/src/backend.rs b/kubi-ui/src/backend.rs deleted file mode 100644 index 4271bfd..0000000 --- a/kubi-ui/src/backend.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[cfg(feature = "backend_glium")] -pub mod glium; diff --git a/kubi-ui/src/interaction.rs b/kubi-ui/src/interaction.rs new file mode 100644 index 0000000..92ff63f --- /dev/null +++ b/kubi-ui/src/interaction.rs @@ -0,0 +1,51 @@ +use crate::element::UiElement; + +pub struct Interactable { + pub element: T, + pub hovered: Option>, + pub clicked: Option>, +} + +impl Interactable { + pub fn new(element: T) -> Self { + Self { + element, + hovered: None, + clicked: None, + } + } + + pub fn on_click(self, clicked: impl FnOnce() + 'static) -> Self { + Self { + clicked: Some(Box::new(clicked)), + ..self + } + } + + pub fn on_hover(self, clicked: impl FnOnce() + 'static) -> Self { + Self { + clicked: Some(Box::new(clicked)), + ..self + } + } +} + +impl UiElement for Interactable { + fn measure(&self, state: &crate::state::StateRepo, layout: &crate::LayoutInfo) -> crate::measure::Response { + self.element.measure(state, layout) + } + + fn process(&self, measure: &crate::measure::Response, state: &mut crate::state::StateRepo, layout: &crate::LayoutInfo, draw: &mut Vec) { + self.element.process(measure, state, layout, draw) + } +} + +pub trait IntoInteractable: UiElement { + fn into_interactable(self) -> Interactable; +} + +impl IntoInteractable for T { + fn into_interactable(self) -> Interactable { + Interactable::new(self) + } +} diff --git a/kubi-ui/src/lib.rs b/kubi-ui/src/lib.rs index 7505eaa..7cff89c 100644 --- a/kubi-ui/src/lib.rs +++ b/kubi-ui/src/lib.rs @@ -4,10 +4,10 @@ use glam::Vec2; pub mod element; pub mod event; pub mod draw; -pub mod backend; pub mod measure; pub mod state; pub mod text; +pub mod interaction; use element::UiElement; use state::StateRepo; diff --git a/kubi/Cargo.toml b/kubi/Cargo.toml index e80805b..53d6945 100644 --- a/kubi/Cargo.toml +++ b/kubi/Cargo.toml @@ -12,6 +12,7 @@ crate-type = ["lib", "cdylib"] kubi-shared = { path = "../kubi-shared" } kubi-logging = { path = "../kubi-logging" } kubi-ui = { path = "../kubi-ui" } +kubi-ui-glium = { path = "../kubi-ui-glium" } log = "0.4" glium = { git = "https://github.com/glium/glium", rev = "968fc92378caf" } glutin = "0.31" diff --git a/kubi/src/guiv2_integration.rs b/kubi/src/guiv2_integration.rs index bca9eb5..856ebef 100644 --- a/kubi/src/guiv2_integration.rs +++ b/kubi/src/guiv2_integration.rs @@ -1,4 +1,5 @@ -use kubi_ui::{KubiUi, backend::glium::GliumUiRenderer}; +use kubi_ui::KubiUi; +use kubi_ui_glium::GliumUiRenderer; use shipyard::{AllStoragesView, Unique, UniqueView, NonSendSync, UniqueViewMut}; use crate::rendering::{Renderer, RenderTarget, WindowSize};