gaming
This commit is contained in:
@@ -3,20 +3,19 @@ use std::{
|
||||
marker::PhantomData,
|
||||
};
|
||||
|
||||
use crate::{Painter, util::ID, UIBuilder};
|
||||
use crate::{Painter, Ui, util::ID};
|
||||
|
||||
pub trait Widget: 'static + Any {
|
||||
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;
|
||||
|
||||
/// 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)]
|
||||
pub struct WidgetId<W = AnyWidget> {
|
||||
pub(super) ty: TypeId,
|
||||
@@ -54,42 +53,83 @@ impl<W> WidgetId<W> {
|
||||
|
||||
pub trait WidgetLike {
|
||||
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
|
||||
pub struct WidgetFn<F: FnOnce(&mut UIBuilder) -> W, W>(pub F);
|
||||
/// A function that returns a widget given a UI.
|
||||
/// 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;
|
||||
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);
|
||||
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 {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
|
||||
ui.add_widget(self).to_id()
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||
ui.add_widget(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> WidgetLike for WidgetId<W> {
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
|
||||
fn add(self, _: &mut Ui) -> WidgetId<W> {
|
||||
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 arr: [WidgetId; LEN],
|
||||
_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> {
|
||||
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> {
|
||||
type Ws = Ws;
|
||||
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
|
||||
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
|
||||
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
|
||||
macro_rules! impl_widget_arr {
|
||||
($n:expr;$($T:tt)*) => {
|
||||
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
|
||||
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)]
|
||||
let ($($T,)*) = self;
|
||||
WidgetArr::new(
|
||||
|
||||
Reference in New Issue
Block a user