Files
openworm/src/bin/client/ui/misc.rs

102 lines
2.7 KiB
Rust

use super::*;
pub fn werror(msg: &str, rsc: &mut Rsc) -> StrongWidget {
wtext(msg)
.size(20)
.pad(10)
.background(rect(Color::RED).radius(10))
.add_strong(rsc)
}
pub fn hint(msg: impl Into<String>) -> TextBuilder<Rsc> {
wtext(msg).size(20).color(Color::GRAY)
}
pub fn field_widget(name: &str, hint_text: &str, rsc: &mut Rsc) -> WeakWidget<TextEdit> {
wtext(name)
.editable(EditMode::SingleLine)
.size(20)
.hint(hint(hint_text))
.add(rsc)
}
pub fn field_box(field: WeakWidget<TextEdit>, rsc: &mut Rsc) -> WeakWidget {
field
.pad(10)
.background(rect(Color::BLACK.brighter(0.1)).radius(15))
.attr::<Selector>(field)
.add(rsc)
}
#[derive(Clone, Copy, WidgetView)]
pub struct Button {
#[root]
root: WeakWidget,
rect: WeakWidget<Rect>,
color: UiColor,
enabled: WeakState<bool>,
}
impl Button {
pub fn new(text: &str, color: UiColor, rsc: &mut Rsc) -> Self {
let rect = rect(color).radius(15).add(rsc);
let enabled = rsc.create_state(rect, true);
let root = rect
.on(
CursorSense::HoverStart | CursorSense::unclick(),
move |ctx, rsc: &mut Rsc| {
if !rsc[enabled] {
return;
}
rsc[ctx.widget].color = color.brighter(0.1);
},
)
.on(CursorSense::HoverEnd, move |ctx, rsc| {
if !rsc[enabled] {
return;
}
rsc[ctx.widget].color = color;
})
.height(60)
.foreground(wtext(text).size(25).text_align(Align::CENTER))
.add(rsc);
rect.on(CursorSense::click(), move |ctx, rsc: &mut Rsc| {
if !rsc[enabled] {
return;
}
rsc[ctx.widget].color = color.darker(0.2);
rsc.run_event::<Submit>(root, (), ctx.state);
})
.add(rsc);
Self {
root,
rect,
color,
enabled,
}
}
pub fn submit(text: &str, rsc: &mut Rsc) -> Self {
Self::new(text, color::GREEN, rsc)
}
pub fn disable(&self, rsc: &mut Rsc) {
rsc[self.enabled] = false;
rsc[self.rect].color = self.color.darker(0.8);
}
}
widget_trait! {
pub trait Stuff<Rsc: UiRsc + 'static>;
fn modal(self, width: impl UiNum) -> impl WidgetIdFn<Rsc> {
|rsc| {
self
.pad(15)
.background(rect(color::MODAL_BG).radius(15))
.width(width)
.align(Align::CENTER)
.add(rsc)
}
}
}