gaming
This commit is contained in:
@@ -1,15 +1,23 @@
|
|||||||
use crate::{Painter, UIColor, Widget, primitive::RoundedRectData};
|
use crate::{Painter, UiColor, Widget, primitive::RoundedRectData};
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct Rect {
|
pub struct Rect {
|
||||||
pub color: UIColor,
|
pub color: UiColor,
|
||||||
pub radius: f32,
|
pub radius: f32,
|
||||||
pub thickness: f32,
|
pub thickness: f32,
|
||||||
pub inner_radius: f32,
|
pub inner_radius: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rect {
|
impl Rect {
|
||||||
pub fn color(mut self, color: UIColor) -> Self {
|
pub fn new(color: UiColor) -> Self {
|
||||||
|
Self {
|
||||||
|
color,
|
||||||
|
radius: 0.0,
|
||||||
|
inner_radius: 0.0,
|
||||||
|
thickness: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn color(mut self, color: UiColor) -> Self {
|
||||||
self.color = color;
|
self.color = color;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,13 @@ pub struct SpanLenSums {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Span {
|
impl Span {
|
||||||
|
pub fn empty(dir: Dir) -> Self {
|
||||||
|
Self {
|
||||||
|
children: Vec::new(),
|
||||||
|
dir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn sums(&self) -> SpanLenSums {
|
pub fn sums(&self) -> SpanLenSums {
|
||||||
self.lengths().fold(SpanLenSums::default(), |mut s, l| {
|
self.lengths().fold(SpanLenSums::default(), |mut s, l| {
|
||||||
match l {
|
match l {
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::{UIRegion, Vec2, WidgetArrLike, WidgetFn, WidgetLike};
|
use crate::{UIRegion, Ui, Vec2, WidgetArrLike, WidgetFn, WidgetLike};
|
||||||
|
|
||||||
pub trait WidgetUtil {
|
pub trait BaseWidget {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>;
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>;
|
||||||
fn center(self, size: impl Into<Vec2>) -> impl WidgetLike<Widget = Regioned>;
|
fn center(self, size: impl Into<Vec2>) -> impl WidgetLike<Widget = Regioned>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike> WidgetUtil for W {
|
impl<W: WidgetLike> BaseWidget for W {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned> {
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned> {
|
||||||
WidgetFn(|ui| Regioned {
|
WidgetFn(|ui| Regioned {
|
||||||
region: padding.into().region(),
|
region: padding.into().region(),
|
||||||
@@ -22,12 +22,20 @@ impl<W: WidgetLike> WidgetUtil for W {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetArrUtil<const LEN: usize> {
|
pub trait BaseWidgetArr<const LEN: usize> {
|
||||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetLike<Widget = Span>;
|
fn span(
|
||||||
|
self,
|
||||||
|
dir: Dir,
|
||||||
|
lengths: [impl Into<SpanLen>; LEN],
|
||||||
|
) -> WidgetFn<impl FnOnce(&mut Ui) -> Span, Span>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa {
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> BaseWidgetArr<LEN> for Wa {
|
||||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> impl WidgetLike<Widget = Span> {
|
fn span(
|
||||||
|
self,
|
||||||
|
dir: Dir,
|
||||||
|
lengths: [impl Into<SpanLen>; LEN],
|
||||||
|
) -> WidgetFn<impl FnOnce(&mut Ui) -> Span, Span> {
|
||||||
let lengths = lengths.map(Into::into);
|
let lengths = lengths.map(Into::into);
|
||||||
WidgetFn(move |ui| Span {
|
WidgetFn(move |ui| Span {
|
||||||
dir,
|
dir,
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone, Copy, bytemuck::Zeroable)]
|
#[derive(Clone, Copy, bytemuck::Zeroable)]
|
||||||
pub struct Color<T: ColorNum> {
|
pub struct Color<T: ColorNum> {
|
||||||
r: T,
|
pub r: T,
|
||||||
g: T,
|
pub g: T,
|
||||||
b: T,
|
pub b: T,
|
||||||
a: T,
|
pub a: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ColorNum> Color<T> {
|
impl<T: ColorNum> Color<T> {
|
||||||
|
|||||||
@@ -12,4 +12,4 @@ pub use vec2::*;
|
|||||||
pub use widget::*;
|
pub use widget::*;
|
||||||
pub use painter::*;
|
pub use painter::*;
|
||||||
|
|
||||||
pub type UIColor = Color<u8>;
|
pub type UiColor = Color<u8>;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ impl<'a> Painter<'a> {
|
|||||||
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
|
.extend_from_slice(bytemuck::cast_slice::<_, u32>(&[data]));
|
||||||
}
|
}
|
||||||
pub fn draw(&mut self, node: &WidgetId) {
|
pub fn draw(&mut self, node: &WidgetId) {
|
||||||
self.nodes.get(node).draw(self);
|
self.nodes.get_dyn(node).draw(self);
|
||||||
}
|
}
|
||||||
pub fn draw_within(&mut self, node: &WidgetId, region: UIRegion) {
|
pub fn draw_within(&mut self, node: &WidgetId, region: UIRegion) {
|
||||||
let old = self.region;
|
let old = self.region;
|
||||||
|
|||||||
127
src/layout/ui.rs
127
src/layout/ui.rs
@@ -1,88 +1,115 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
HashMap, Painter, Widget, WidgetId, WidgetLike, WidgetRef,
|
HashMap, Painter, Widget, WidgetId, WidgetLike,
|
||||||
primitive::Primitives,
|
primitive::Primitives,
|
||||||
util::{ID, IDTracker},
|
util::{ID, IDTracker},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
cell::RefCell,
|
ops::{Index, IndexMut},
|
||||||
rc::Rc,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct UI {
|
#[derive(Default)]
|
||||||
|
pub struct Ui {
|
||||||
ids: IDTracker,
|
ids: IDTracker,
|
||||||
base: Option<WidgetId>,
|
base: Option<WidgetId>,
|
||||||
pub widgets: Widgets,
|
widgets: Widgets,
|
||||||
|
updates: Vec<WidgetId>,
|
||||||
|
primitives: Primitives,
|
||||||
|
full_redraw: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct Widgets(HashMap<ID, Box<dyn Widget>>);
|
pub struct Widgets(HashMap<ID, Box<dyn Widget>>);
|
||||||
|
|
||||||
#[derive(Clone)]
|
impl Ui {
|
||||||
pub struct UIBuilder {
|
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetId<W> {
|
||||||
ui: Rc<RefCell<UI>>,
|
w.add(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<UI> for UIBuilder {
|
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||||
fn from(ui: UI) -> Self {
|
self.push(w)
|
||||||
UIBuilder {
|
|
||||||
ui: Rc::new(RefCell::new(ui)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UIBuilder {
|
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||||
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetRef<W> {
|
let id = self.ids.next();
|
||||||
WidgetRef::new([w.add(self).erase_type()])
|
self.widgets.insert(id.duplicate(), w);
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
|
|
||||||
WidgetRef::new([self.push(w)])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId {
|
|
||||||
let mut ui = self.ui.borrow_mut();
|
|
||||||
let id = ui.ids.next();
|
|
||||||
ui.widgets.insert(id.duplicate(), w);
|
|
||||||
WidgetId::new(id, TypeId::of::<W>())
|
WidgetId::new(id, TypeId::of::<W>())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish<W: WidgetLike>(mut self, base: W) -> UI {
|
pub fn set<W: Widget>(&mut self, i: &WidgetId<W>, w: W) {
|
||||||
let base = base.add(&mut self).erase_type();
|
self.widgets.insert(i.id.duplicate(), w);
|
||||||
let mut ui = Rc::into_inner(self.ui).unwrap().into_inner();
|
|
||||||
ui.base = Some(base);
|
|
||||||
ui
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UI {
|
pub fn set_base(&mut self, w: impl WidgetLike) {
|
||||||
pub fn build() -> UIBuilder {
|
self.base = Some(w.add(self).erase_type());
|
||||||
Self::empty().into()
|
self.full_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn empty() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self::default()
|
||||||
ids: IDTracker::new(),
|
|
||||||
base: None,
|
|
||||||
widgets: Widgets::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_primitives(&self) -> Primitives {
|
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||||
|
self.widgets.get(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
||||||
|
self.widgets.get_mut(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
|
||||||
|
WidgetId::new(self.ids.next(), TypeId::of::<W>())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn redraw_all(&mut self) {
|
||||||
let mut painter = Painter::new(&self.widgets);
|
let mut painter = Painter::new(&self.widgets);
|
||||||
if let Some(base) = &self.base {
|
if let Some(base) = &self.base {
|
||||||
painter.draw(base);
|
painter.draw(base);
|
||||||
}
|
}
|
||||||
painter.finish()
|
self.primitives = painter.finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self) -> Option<&Primitives> {
|
||||||
|
if self.full_redraw {
|
||||||
|
self.redraw_all();
|
||||||
|
self.full_redraw = false;
|
||||||
|
return Some(&self.primitives);
|
||||||
|
}
|
||||||
|
if self.updates.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
self.redraw_all();
|
||||||
|
self.updates.drain(..);
|
||||||
|
Some(&self.primitives)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn needs_redraw(&self) -> bool {
|
||||||
|
self.full_redraw || !self.updates.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: Widget> Index<&WidgetId<W>> for Ui {
|
||||||
|
type Output = W;
|
||||||
|
|
||||||
|
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
|
||||||
|
self.get(id).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
|
||||||
|
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
|
||||||
|
self.updates.push(id.clone().erase_type());
|
||||||
|
self.get_mut(id).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widgets {
|
impl Widgets {
|
||||||
fn new() -> Self {
|
pub fn get_dyn(&self, id: &WidgetId) -> &dyn Widget {
|
||||||
Self(HashMap::new())
|
self.0.get(&id.id).unwrap().as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, id: &WidgetId) -> &dyn Widget {
|
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||||
self.0.get(&id.id).unwrap().as_ref()
|
self.0.get(&id.id).unwrap().as_any().downcast_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
||||||
@@ -99,6 +126,10 @@ impl Widgets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl dyn Widget {
|
impl dyn Widget {
|
||||||
|
pub fn as_any(&self) -> &dyn Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_any_mut(&mut self) -> &mut dyn Any {
|
pub fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,20 +3,19 @@ use std::{
|
|||||||
marker::PhantomData,
|
marker::PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Painter, util::ID, UIBuilder};
|
use crate::{Painter, Ui, util::ID};
|
||||||
|
|
||||||
pub trait Widget: 'static + Any {
|
pub trait Widget: 'static + Any {
|
||||||
fn draw(&self, painter: &mut Painter);
|
fn draw(&self, painter: &mut Painter);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget> Widget for (W,) {
|
|
||||||
fn draw(&self, painter: &mut Painter) {
|
|
||||||
self.0.draw(painter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AnyWidget;
|
pub struct AnyWidget;
|
||||||
|
|
||||||
|
/// An identifier for a widget.
|
||||||
|
/// Can index a UI to get the associated widget.
|
||||||
|
///
|
||||||
|
/// W does not need to implement widget so that AnyWidget is valid;
|
||||||
|
/// Instead, add generic bounds on methods that take an ID if they need specific data.
|
||||||
#[derive(Eq, Hash, PartialEq, Debug)]
|
#[derive(Eq, Hash, PartialEq, Debug)]
|
||||||
pub struct WidgetId<W = AnyWidget> {
|
pub struct WidgetId<W = AnyWidget> {
|
||||||
pub(super) ty: TypeId,
|
pub(super) ty: TypeId,
|
||||||
@@ -54,42 +53,83 @@ impl<W> WidgetId<W> {
|
|||||||
|
|
||||||
pub trait WidgetLike {
|
pub trait WidgetLike {
|
||||||
type Widget;
|
type Widget;
|
||||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
|
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// wouldn't be needed if negative trait bounds & disjoint impls existed
|
/// A function that returns a widget given a UI.
|
||||||
pub struct WidgetFn<F: FnOnce(&mut UIBuilder) -> W, W>(pub F);
|
/// Useful for defining trait functions on widgets that create a parent widget so that the children
|
||||||
|
/// don't need to be IDs yet
|
||||||
|
pub struct WidgetFn<F: FnOnce(&mut Ui) -> W, W>(pub F);
|
||||||
|
pub struct WidgetIdFn<F: FnOnce(&mut Ui) -> WidgetId<W>, W>(pub F);
|
||||||
|
|
||||||
impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
|
pub trait _WidgetFn<W: Widget> {
|
||||||
|
fn call(ui: &mut Ui) -> W;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Idable {
|
||||||
|
type Widget: Widget;
|
||||||
|
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait WidgetFns<W> {
|
||||||
|
fn id(self, id: &WidgetId<W>) -> impl WidgetLike<Widget = W>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I: Idable> WidgetFns<I::Widget> for I {
|
||||||
|
fn id(self, id: &WidgetId<I::Widget>) -> impl WidgetLike<Widget = I::Widget> {
|
||||||
|
WidgetIdFn(|ui| {
|
||||||
|
self.set(ui, id);
|
||||||
|
id.clone()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: Widget> Idable for W {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
|
||||||
|
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
|
||||||
|
ui.set(id, self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F: for<'a> FnOnce(&'a mut Ui) -> W, W: Widget> Idable for WidgetFn<F, W> {
|
||||||
|
type Widget = W;
|
||||||
|
|
||||||
|
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
|
||||||
|
let w = self.0(ui);
|
||||||
|
ui.set(id, w);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike for WidgetFn<F, W> {
|
||||||
|
type Widget = W;
|
||||||
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||||
let w = (self.0)(ui);
|
let w = (self.0)(ui);
|
||||||
ui.add_widget(w).to_id()
|
ui.add(w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: Widget, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike for WidgetIdFn<F, W> {
|
||||||
|
type Widget = W;
|
||||||
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||||
|
(self.0)(ui)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget> WidgetLike for W {
|
impl<W: Widget> WidgetLike for W {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||||
ui.add_widget(self).to_id()
|
ui.add_widget(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> WidgetLike for WidgetId<W> {
|
impl<W> WidgetLike for WidgetId<W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
fn add(self, _: &mut Ui) -> WidgetId<W> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W> WidgetLike for WidgetArr<1, (W,)> {
|
|
||||||
type Widget = W;
|
|
||||||
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
|
||||||
let [id] = self.arr;
|
|
||||||
id.cast_type()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WidgetArr<const LEN: usize, Ws> {
|
pub struct WidgetArr<const LEN: usize, Ws> {
|
||||||
pub arr: [WidgetId; LEN],
|
pub arr: [WidgetId; LEN],
|
||||||
_pd: PhantomData<Ws>,
|
_pd: PhantomData<Ws>,
|
||||||
@@ -104,37 +144,31 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type WidgetRef<W> = WidgetArr<1, (W,)>;
|
|
||||||
|
|
||||||
impl<W> WidgetRef<W> {
|
|
||||||
pub fn handle(&self) -> WidgetId<W> {
|
|
||||||
let [id] = &self.arr;
|
|
||||||
id.clone().cast_type()
|
|
||||||
}
|
|
||||||
pub fn to_id(self) -> WidgetId<W> {
|
|
||||||
let [id] = self.arr;
|
|
||||||
id.cast_type()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait WidgetArrLike<const LEN: usize> {
|
pub trait WidgetArrLike<const LEN: usize> {
|
||||||
type Ws;
|
type Ws;
|
||||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Self::Ws>;
|
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
|
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
|
||||||
type Ws = Ws;
|
type Ws = Ws;
|
||||||
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
|
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<W: WidgetLike> WidgetArrLike<1> for W {
|
||||||
|
type Ws = (W::Widget,);
|
||||||
|
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
|
||||||
|
WidgetArr::new([self.add(ui).erase_type()])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// I hate this language it's so bad why do I even use it
|
// I hate this language it's so bad why do I even use it
|
||||||
macro_rules! impl_widget_arr {
|
macro_rules! impl_widget_arr {
|
||||||
($n:expr;$($T:tt)*) => {
|
($n:expr;$($T:tt)*) => {
|
||||||
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
|
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
|
||||||
type Ws = ($($T::Widget,)*);
|
type Ws = ($($T::Widget,)*);
|
||||||
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> {
|
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($T::Widget,)*)> {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
let ($($T,)*) = self;
|
let ($($T,)*) = self;
|
||||||
WidgetArr::new(
|
WidgetArr::new(
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{UI, primitive::PrimitiveInstance, render::util::ArrBuf};
|
use crate::{Ui, primitive::PrimitiveInstance, render::util::ArrBuf};
|
||||||
use data::WindowUniform;
|
use data::WindowUniform;
|
||||||
use wgpu::{
|
use wgpu::{
|
||||||
util::{BufferInitDescriptor, DeviceExt},
|
util::{BufferInitDescriptor, DeviceExt},
|
||||||
@@ -32,8 +32,8 @@ impl UIRenderNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &UI) {
|
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
|
||||||
let primitives = ui.to_primitives();
|
if let Some(primitives) = ui.update() {
|
||||||
self.instance.update(device, queue, &primitives.instances);
|
self.instance.update(device, queue, &primitives.instances);
|
||||||
self.data.update(device, queue, &primitives.data);
|
self.data.update(device, queue, &primitives.data);
|
||||||
self.bind_group = Self::bind_group(
|
self.bind_group = Self::bind_group(
|
||||||
@@ -43,6 +43,7 @@ impl UIRenderNode {
|
|||||||
&self.data.buffer,
|
&self.data.buffer,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, size: &PhysicalSize<u32>, queue: &Queue) {
|
pub fn resize(&mut self, size: &PhysicalSize<u32>, queue: &Queue) {
|
||||||
let slice = &[WindowUniform {
|
let slice = &[WindowUniform {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use app::App;
|
use app::App;
|
||||||
use gui::{Dir, Rect, UI, UIColor, WidgetArrUtil, WidgetUtil, fixed, ratio, rel};
|
use gui::*;
|
||||||
use render::Renderer;
|
use render::Renderer;
|
||||||
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
|
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
|
||||||
|
|
||||||
@@ -14,44 +14,46 @@ pub fn main() {
|
|||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
renderer: Renderer,
|
renderer: Renderer,
|
||||||
|
ui: Ui,
|
||||||
|
test: WidgetId<Span>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn new(window: Arc<Window>) -> Self {
|
pub fn new(window: Arc<Window>) -> Self {
|
||||||
let mut renderer = Renderer::new(window);
|
let renderer = Renderer::new(window);
|
||||||
let rect = Rect {
|
let rect = Rect {
|
||||||
color: UIColor::WHITE,
|
color: UiColor::WHITE,
|
||||||
radius: 20.0,
|
radius: 20.0,
|
||||||
thickness: 0.0,
|
thickness: 0.0,
|
||||||
inner_radius: 0.0,
|
inner_radius: 0.0,
|
||||||
};
|
};
|
||||||
let mut ui = UI::build();
|
let mut ui = Ui::new();
|
||||||
let blue = ui.add(rect.color(UIColor::BLUE));
|
let test = ui.id();
|
||||||
let handle = blue.handle();
|
ui.set_base(
|
||||||
let mut ui = ui.finish(
|
|
||||||
(
|
(
|
||||||
(
|
(
|
||||||
blue,
|
rect.color(UiColor::BLUE),
|
||||||
(
|
(
|
||||||
rect.color(UIColor::RED).center((100.0, 100.0)),
|
rect.color(UiColor::RED).center((100.0, 100.0)),
|
||||||
(
|
(
|
||||||
rect.color(UIColor::ORANGE),
|
rect.color(UiColor::ORANGE),
|
||||||
rect.color(UIColor::LIME).pad(10.0),
|
rect.color(UiColor::LIME).pad(10.0),
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT, [1, 1]),
|
.span(Dir::RIGHT, [1, 1]),
|
||||||
rect.color(UIColor::YELLOW),
|
rect.color(UiColor::YELLOW),
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT, [2, 2, 1])
|
.span(Dir::RIGHT, [2, 2, 1])
|
||||||
.pad(10),
|
.pad(10),
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT, [1, 3]),
|
.span(Dir::RIGHT, [1, 3]),
|
||||||
|
Span::empty(Dir::RIGHT).id(&test),
|
||||||
(
|
(
|
||||||
rect.color(UIColor::GREEN),
|
rect.color(UiColor::GREEN),
|
||||||
rect.color(UIColor::ORANGE),
|
rect.color(UiColor::ORANGE),
|
||||||
rect.color(UIColor::CYAN),
|
rect.color(UiColor::CYAN),
|
||||||
rect.color(UIColor::BLUE),
|
rect.color(UiColor::BLUE),
|
||||||
rect.color(UIColor::MAGENTA),
|
rect.color(UiColor::MAGENTA),
|
||||||
rect.color(UIColor::RED),
|
rect.color(UiColor::RED),
|
||||||
)
|
)
|
||||||
.span(
|
.span(
|
||||||
Dir::LEFT,
|
Dir::LEFT,
|
||||||
@@ -65,19 +67,27 @@ impl Client {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.span(Dir::DOWN, [3, 1])
|
.span(Dir::DOWN, [3, 1, 1])
|
||||||
.pad(10),
|
.pad(10),
|
||||||
);
|
);
|
||||||
ui.widgets.get_mut(&handle).unwrap().color = UIColor::MAGENTA;
|
Self { renderer, ui, test }
|
||||||
renderer.update(&ui);
|
|
||||||
Self { renderer }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested => event_loop.exit(),
|
WindowEvent::CloseRequested => event_loop.exit(),
|
||||||
WindowEvent::RedrawRequested => self.renderer.draw(),
|
WindowEvent::RedrawRequested => {
|
||||||
|
self.renderer.update(&mut self.ui);
|
||||||
|
self.renderer.draw()
|
||||||
|
}
|
||||||
WindowEvent::Resized(size) => self.renderer.resize(&size),
|
WindowEvent::Resized(size) => self.renderer.resize(&size),
|
||||||
|
WindowEvent::KeyboardInput { event, .. } => {
|
||||||
|
if event.state.is_pressed() {
|
||||||
|
let child = self.ui.add(Rect::new(Color::YELLOW)).erase_type();
|
||||||
|
self.ui[&self.test].children.push((child, fixed(20.0)));
|
||||||
|
self.renderer.window().request_redraw();
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use gui::{UIRenderNode, UI};
|
use gui::{UIRenderNode, Ui};
|
||||||
use pollster::FutureExt;
|
use pollster::FutureExt;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use wgpu::util::StagingBelt;
|
use wgpu::util::StagingBelt;
|
||||||
@@ -7,6 +7,7 @@ use winit::{dpi::PhysicalSize, window::Window};
|
|||||||
pub const CLEAR_COLOR: wgpu::Color = wgpu::Color::BLACK;
|
pub const CLEAR_COLOR: wgpu::Color = wgpu::Color::BLACK;
|
||||||
|
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
|
window: Arc<Window>,
|
||||||
surface: wgpu::Surface<'static>,
|
surface: wgpu::Surface<'static>,
|
||||||
device: wgpu::Device,
|
device: wgpu::Device,
|
||||||
queue: wgpu::Queue,
|
queue: wgpu::Queue,
|
||||||
@@ -17,7 +18,7 @@ pub struct Renderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
pub fn update(&mut self, ui: &UI) {
|
pub fn update(&mut self, ui: &mut Ui) {
|
||||||
self.ui_node.update(&self.device, &self.queue, ui);
|
self.ui_node.update(&self.device, &self.queue, ui);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +128,11 @@ impl Renderer {
|
|||||||
encoder,
|
encoder,
|
||||||
staging_belt,
|
staging_belt,
|
||||||
ui_node: shape_pipeline,
|
ui_node: shape_pipeline,
|
||||||
|
window,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn window(&self) -> &Window {
|
||||||
|
self.window.as_ref()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user