404 lines
11 KiB
Rust
404 lines
11 KiB
Rust
use std::{
|
|
collections::{HashMap, HashSet},
|
|
hash::Hash,
|
|
marker::PhantomData,
|
|
str::FromStr,
|
|
};
|
|
|
|
use crate::components::Block;
|
|
use lan_party_core::event::{
|
|
free_for_all_game::FreeForAllGameSpec, team_game::TeamGameSpec, test::TestSpec, EventSpec,
|
|
EventTypeSpec,
|
|
};
|
|
use log::debug;
|
|
use paste::paste;
|
|
use sycamore::{builder::prelude::*, prelude::*};
|
|
|
|
use super::{BlockProps, Button, ButtonProps};
|
|
|
|
macro_rules! editable {
|
|
($type:ty => $editor:ty) => {
|
|
impl<'a, G: Html> Editable<'a, G> for $type {
|
|
type Editor = $editor;
|
|
}
|
|
};
|
|
}
|
|
|
|
macro_rules! edit_fields {
|
|
($cx:ident, $(($name:expr, $prop:expr)),* $(,)?) => {
|
|
view! { $cx,
|
|
$(
|
|
p {
|
|
label { ($name) }
|
|
($prop.edit($cx))
|
|
}
|
|
)*
|
|
}
|
|
};
|
|
}
|
|
|
|
macro_rules! link_fields {
|
|
($cx:ident, $($field:ident),* $(,)? => $state:ident as $t:ident) => {
|
|
$(let $field = create_signal($cx, $state.get_untracked().$field.clone());)*
|
|
|
|
create_effect($cx, || {
|
|
$state.set($t {
|
|
$($field: $field.get().as_ref().clone(),)*
|
|
..Default::default()
|
|
});
|
|
});
|
|
};
|
|
}
|
|
|
|
macro_rules! edit_struct {
|
|
($struct:ident => $(($name:expr, $prop:ident)),* $(,)?) => {
|
|
paste! {
|
|
pub struct [<$struct Edit>];
|
|
|
|
impl<'a, G: Html> Editor<'a, G, $struct> for [<$struct Edit>] {
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, $struct>) -> View<G> {
|
|
let state = props.state;
|
|
link_fields!(cx, $($prop,)* => state as $struct);
|
|
view! { cx,
|
|
Block(title=stringify!($struct).into()) {
|
|
(edit_fields!(cx, $(($name, $prop),)*))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
editable!($struct => [<$struct Edit>]);
|
|
}
|
|
};
|
|
}
|
|
|
|
macro_rules! link_variants {
|
|
($cx:ident, $selected:ident => $($index:literal: $var_name:ident = $variant:ident: $var_type:ty),* $(,)? => $state:ident as $t:ident) => {
|
|
let $selected = create_signal($cx, String::from("0"));
|
|
|
|
$(let $var_name = if let $t::$variant(v) = $state.get_untracked().as_ref().clone() {
|
|
create_signal($cx, v.clone())
|
|
} else {
|
|
create_signal($cx, <$var_type>::default())
|
|
};)*
|
|
|
|
create_effect($cx, || {
|
|
debug!("{:#?}", $selected.get());
|
|
match $selected.get().as_str() {
|
|
$(stringify!($index) => $state.set($t::$variant($var_name.get().as_ref().clone())),)*
|
|
_ => unreachable!()
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
macro_rules! edit_enum {
|
|
($enum:ident => $selected:ident => $($index:literal: $var_name:ident = $variant:ident: $var_type:ty),* $(,)?) => {
|
|
paste! {
|
|
pub struct [<$enum Edit>];
|
|
|
|
impl<'a, G: Html> Editor<'a, G, $enum> for [<$enum Edit>] {
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, $enum>) -> View<G> {
|
|
let state = props.state;
|
|
|
|
link_variants!(cx, $selected =>
|
|
$($index: $var_name = $variant: $var_type,)*
|
|
=> state as $enum
|
|
);
|
|
|
|
view! { cx,
|
|
Block(title=stringify!($enum).to_string()) {
|
|
select(bind:value=$selected) {
|
|
$(option(value={stringify!($index)}, selected=$index==0) { (stringify!($variant)) })*
|
|
}
|
|
(match $selected.get().as_str() {
|
|
$(stringify!($index) => $var_name.edit(cx),)*
|
|
_ => unreachable!()
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
editable!($enum => [<$enum Edit>]);
|
|
}
|
|
};
|
|
}
|
|
|
|
#[derive(Prop)]
|
|
pub struct EditProps<'a, T> {
|
|
pub state: &'a Signal<T>,
|
|
}
|
|
|
|
impl<'a, T> From<&'a Signal<T>> for EditProps<'a, T> {
|
|
fn from(state: &'a Signal<T>) -> Self {
|
|
EditProps { state }
|
|
}
|
|
}
|
|
|
|
pub trait IntoEdit<'a, G: Html> {
|
|
fn edit(self, cx: Scope<'a>) -> View<G>;
|
|
}
|
|
|
|
impl<'a, G: Html, T: Editable<'a, G>> IntoEdit<'a, G> for &'a Signal<T>
|
|
where
|
|
EditProps<'a, T>: From<&'a Signal<T>>,
|
|
{
|
|
fn edit(self, cx: Scope<'a>) -> View<G> {
|
|
T::edit(cx, self.into())
|
|
}
|
|
}
|
|
|
|
pub trait Edit<'a, G: Html>: Sized {
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, Self>) -> View<G>;
|
|
}
|
|
|
|
impl<'a, G, E, Type> Edit<'a, G> for Type
|
|
where
|
|
G: Html,
|
|
E: Editor<'a, G, Type>,
|
|
Type: Editable<'a, G, Editor = E>,
|
|
{
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, Self>) -> View<G> {
|
|
E::edit(cx, props)
|
|
}
|
|
}
|
|
|
|
pub trait Editor<'a, G: Html, Type>: Sized {
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, Type>) -> View<G>;
|
|
}
|
|
|
|
pub trait Editable<'a, G: Html>: Sized {
|
|
type Editor: Editor<'a, G, Self>;
|
|
}
|
|
|
|
edit_struct!(EventSpec => ("Name", name), ("Description", description), ("Event type", event_type));
|
|
|
|
edit_enum!(EventTypeSpec => selected =>
|
|
0: test = Test: TestSpec,
|
|
1: team_game = TeamGame: TeamGameSpec,
|
|
2: free_for_all_game = FreeForAllGame: FreeForAllGameSpec
|
|
);
|
|
|
|
edit_struct!(TestSpec => ("Number of players", num_players));
|
|
edit_struct!(TeamGameSpec => ("Teams", teams), ("Win rewards", win_rewards), ("Lose rewards", lose_rewards));
|
|
edit_struct!(FreeForAllGameSpec => ("Participants", participants), ("Win rewards", win_rewards), ("Lose rewards", lose_rewards));
|
|
|
|
pub struct StringEdit;
|
|
|
|
impl<'a, G: Html> Editor<'a, G, String> for StringEdit {
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, String>) -> View<G> {
|
|
view! { cx,
|
|
input(bind:value=props.state)
|
|
}
|
|
}
|
|
}
|
|
|
|
editable!(String => StringEdit);
|
|
|
|
pub struct StubEdit;
|
|
|
|
impl<'a, G: Html, T> Editor<'a, G, T> for StubEdit
|
|
where
|
|
T: Editable<'a, G, Editor = StubEdit>,
|
|
{
|
|
fn edit(cx: Scope<'a>, _props: EditProps<'a, T>) -> View<G> {
|
|
view! { cx,
|
|
i { "Editor Unimplemented" }
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct InputEdit;
|
|
|
|
impl<'a, G: Html, T> Editor<'a, G, T> for InputEdit
|
|
where
|
|
T: Editable<'a, G, Editor = InputEdit> + FromStr + ToString + Default,
|
|
{
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, T>) -> View<G> {
|
|
let value = create_signal(cx, props.state.get_untracked().to_string());
|
|
|
|
create_effect(cx, || {
|
|
props
|
|
.state
|
|
.set((*value.get()).parse().unwrap_or(T::default()))
|
|
});
|
|
|
|
view! { cx,
|
|
input(bind:value=value)
|
|
}
|
|
}
|
|
}
|
|
|
|
editable!(i64 => InputEdit);
|
|
editable!(i32 => InputEdit);
|
|
editable!(isize => InputEdit);
|
|
|
|
editable!(u64 => InputEdit);
|
|
editable!(u32 => InputEdit);
|
|
editable!(usize => InputEdit);
|
|
|
|
editable!(f64 => InputEdit);
|
|
editable!(f32 => InputEdit);
|
|
|
|
pub struct BoolEdit;
|
|
|
|
impl<'a, G: Html> Editor<'a, G, bool> for BoolEdit {
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, bool>) -> View<G> {
|
|
view! { cx,
|
|
input(type="checkbox", bind:checked=props.state)
|
|
}
|
|
}
|
|
}
|
|
|
|
editable!(bool => BoolEdit);
|
|
|
|
pub struct VecEdit;
|
|
|
|
impl<'a, G, T, I> Editor<'a, G, I> for VecEdit
|
|
where
|
|
G: Html,
|
|
T: Editable<'a, G> + Clone + PartialEq + Default + 'a,
|
|
I: IntoIterator<Item = T> + FromIterator<T> + Clone,
|
|
{
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, I>) -> View<G> {
|
|
let vec = create_signal(
|
|
cx,
|
|
props
|
|
.state
|
|
.get_untracked()
|
|
.as_ref()
|
|
.clone()
|
|
.into_iter()
|
|
.map(|x| create_signal(cx, x))
|
|
.collect::<Vec<_>>(),
|
|
);
|
|
|
|
create_effect(cx, || {
|
|
props.state.set(
|
|
vec.get()
|
|
.as_ref()
|
|
.iter()
|
|
.cloned()
|
|
.map(|x| x.get().as_ref().clone())
|
|
.collect(),
|
|
)
|
|
});
|
|
|
|
let onadd = move |_| vec.modify().push(create_signal(cx, T::default()));
|
|
|
|
Block(
|
|
cx,
|
|
BlockProps {
|
|
title: "List".into(),
|
|
children: Children::new(cx, move |_| {
|
|
div()
|
|
.dyn_c(move || {
|
|
View::new_fragment(
|
|
vec.get()
|
|
.as_ref()
|
|
.clone()
|
|
.into_iter()
|
|
.map(|x| div().c(x.edit(cx)).c(br()).view(cx))
|
|
.collect(),
|
|
)
|
|
})
|
|
.c(Button(
|
|
cx,
|
|
ButtonProps {
|
|
onclick: onadd,
|
|
text: "Add new".into(),
|
|
icon: "mdi-plus".into(),
|
|
},
|
|
))
|
|
.view(cx)
|
|
}),
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
impl<'a, G, T> Editable<'a, G> for Vec<T>
|
|
where
|
|
G: Html,
|
|
T: Editable<'a, G> + Clone + PartialEq + Default + 'a,
|
|
{
|
|
type Editor = VecEdit;
|
|
}
|
|
|
|
impl<'a, G, T> Editable<'a, G> for HashSet<T>
|
|
where
|
|
G: Html,
|
|
T: Editable<'a, G> + Clone + PartialEq + Default + Hash + Eq + 'a,
|
|
{
|
|
type Editor = VecEdit;
|
|
}
|
|
|
|
impl<'a, G, K, V> Editable<'a, G> for HashMap<K, V>
|
|
where
|
|
G: Html,
|
|
K: Clone + Hash + Eq,
|
|
V: Clone,
|
|
(K, V): Editable<'a, G> + Clone + PartialEq + Default + Hash + Eq + 'a,
|
|
{
|
|
type Editor = VecEdit;
|
|
}
|
|
|
|
pub struct TupleEdit;
|
|
|
|
impl<'a, 'b, G: Html, A: Editable<'a, G> + Clone, B: Editable<'a, G> + Clone> Editor<'a, G, (A, B)>
|
|
for TupleEdit
|
|
{
|
|
fn edit(cx: Scope<'a>, props: EditProps<'a, (A, B)>) -> View<G> {
|
|
let state = props.state;
|
|
let (a, b) = state.get_untracked().as_ref().clone();
|
|
|
|
let a = create_signal(cx, a.clone());
|
|
let b = create_signal(cx, b.clone());
|
|
|
|
create_effect(cx, || {
|
|
props
|
|
.state
|
|
.set((a.get().as_ref().clone(), b.get().as_ref().clone()))
|
|
});
|
|
|
|
Block(
|
|
cx,
|
|
BlockProps {
|
|
title: "Tuple".into(),
|
|
children: Children::new(cx, move |_| {
|
|
div()
|
|
.dyn_c(move || a.edit(cx))
|
|
.dyn_c(move || b.edit(cx))
|
|
.view(cx)
|
|
}),
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
impl<'a, G, A, B> Editable<'a, G> for (A, B)
|
|
where
|
|
G: Html,
|
|
A: Editable<'a, G> + Clone,
|
|
B: Editable<'a, G> + Clone,
|
|
{
|
|
type Editor = TupleEdit;
|
|
}
|
|
|
|
#[derive(Default, Clone, Debug)]
|
|
pub struct Test {
|
|
inner: TestInner,
|
|
inner2: TestInner,
|
|
}
|
|
|
|
edit_struct!(Test => ("Inner", inner), ("Inner 2", inner2));
|
|
|
|
#[derive(Default, Clone, Debug)]
|
|
pub struct TestInner {
|
|
some_text: String,
|
|
some_number: usize,
|
|
}
|
|
|
|
edit_struct!(TestInner => ("Text", some_text), ("Number", some_number));
|