Skip to content

Commit

Permalink
add interact box
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 23, 2023
1 parent f8fea89 commit c08e278
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 18 deletions.
40 changes: 22 additions & 18 deletions assets_gui/src/yakui_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use engine::wgpu::RenderPass;
use engine::{set_cursor_icon, CursorIcon, Drawable, GfxContext, Mesh, SpriteBatch};
use geom::Matrix4;
use goryak::{
background, button_primary, button_secondary, center_width, checkbox_value, combo_box,
debug_constraints, debug_size, dragvalue, icon, is_hovered, labelc, on_background,
on_secondary, outline_variant, scroll_vertical, secondary, secondary_container, set_theme,
stretch_width, use_changed, CountGrid, Draggable, MainAxisAlignItems, RoundRect, Theme,
background, button_primary, center_width, checkbox_value, combo_box, dragvalue, icon,
interact_box, is_hovered, labelc, on_background, on_secondary, on_surface, outline_variant,
scroll_vertical, secondary, secondary_container, set_theme, stretch_width, surface,
surface_variant, use_changed, CountGrid, Draggable, MainAxisAlignItems, RoundRect, Theme,
};

use crate::companies::Companies;
Expand Down Expand Up @@ -114,22 +114,26 @@ impl State {
}

fn explore_item(indent: usize, name: String, folder: Option<bool>, on_click: impl FnOnce()) {
let mut p = Pad::ZERO;
p.left = indent as f32 * 4.0 + if folder.is_none() { 12.0 } else { 0.0 };
p.top = 4.0;
p.show(|| {
let mut l = List::row();
l.cross_axis_alignment = CrossAxisAlignment::Center;
l.show(|| {
if let Some(v) = folder {
let triangle = if v { "caret-down" } else { "caret-right" };
icon(on_background(), triangle);
}
if button_secondary(name).clicked {
on_click();
}
let r = interact_box(surface(), surface_variant(), surface_variant(), || {
let mut p = Pad::ZERO;
p.left = indent as f32 * 4.0 + if folder.is_none() { 12.0 } else { 0.0 };
p.top = 4.0;
p.bottom = 4.0;
p.show(|| {
let mut l = List::row();
l.cross_axis_alignment = CrossAxisAlignment::Center;
l.show(|| {
if let Some(v) = folder {
let triangle = if v { "caret-down" } else { "caret-right" };
icon(on_surface(), triangle);
}
labelc(on_surface(), name);
});
});
});
if r.just_clicked {
on_click();
}
}

fn model_properties(&mut self) {
Expand Down
164 changes: 164 additions & 0 deletions goryak/src/interact_box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
use yakui_core::event::{EventInterest, EventResponse, WidgetEvent};
use yakui_core::geometry::{Color, Constraints, Vec2};
use yakui_core::input::MouseButton;
use yakui_core::paint::PaintRect;
use yakui_core::widget::{EventContext, LayoutContext, PaintContext, Widget};
use yakui_core::Response;
use yakui_widgets::shapes::RoundedRectangle;

/**
A colored box that can contain children.
Responds with [InteractBoxResponse].
*/
#[derive(Debug, Clone)]
pub struct InteractBox {
pub color: Color,
pub hover_color: Color,
pub click_color: Color,
pub border_radius: f32,
}

impl InteractBox {
pub fn empty() -> Self {
Self {
color: Color::WHITE,
hover_color: Color::WHITE,
click_color: Color::WHITE,
border_radius: 0.0,
}
}

pub fn show(self) -> Response<InteractBoxResponse> {
yakui_widgets::util::widget::<InteractBoxWidget>(self)
}

pub fn show_children<F: FnOnce()>(self, children: F) -> Response<InteractBoxResponse> {
yakui_widgets::util::widget_children::<InteractBoxWidget, F>(children, self)
}
}

pub fn interact_box_radius(
color: Color,
hover_color: Color,
click_color: Color,
border_radius: f32,
children: impl FnOnce(),
) -> Response<InteractBoxResponse> {
InteractBox {
color,
hover_color,
click_color,
border_radius,
}
.show_children(children)
}

pub fn interact_box(
color: Color,
hover_color: Color,
click_color: Color,
children: impl FnOnce(),
) -> Response<InteractBoxResponse> {
InteractBox {
color,
hover_color,
click_color,
border_radius: 0.0,
}
.show_children(children)
}

#[derive(Debug)]
pub struct InteractBoxWidget {
props: InteractBox,
resp: InteractBoxResponse,
}

#[derive(Copy, Clone, Debug, Default)]
pub struct InteractBoxResponse {
pub hovered: bool,
pub mousedown: bool,
pub just_hovered: bool,
pub just_clicked: bool,
}

impl Widget for InteractBoxWidget {
type Props<'a> = InteractBox;
type Response = InteractBoxResponse;

fn new() -> Self {
Self {
props: InteractBox::empty(),
resp: InteractBoxResponse::default(),
}
}

fn update(&mut self, props: Self::Props<'_>) -> Self::Response {
self.props = props;
let resp = self.resp;
self.resp.just_hovered = false;
self.resp.just_clicked = false;
resp
}

fn paint(&self, mut ctx: PaintContext<'_>) {
let node = ctx.dom.get_current();
let layout_node = ctx.layout.get(ctx.dom.current()).unwrap();

let curcolor = if self.resp.mousedown {
self.props.click_color
} else if self.resp.hovered {
self.props.hover_color
} else {
self.props.color
};

if self.props.border_radius > 0.0 {
let mut rect = RoundedRectangle::new(layout_node.rect, self.props.border_radius);
rect.color = curcolor;
rect.add(ctx.paint);
} else {
let mut rect = PaintRect::new(layout_node.rect);
rect.color = curcolor;
rect.add(ctx.paint);
}

for &child in &node.children {
ctx.paint(child);
}
}

fn event_interest(&self) -> EventInterest {
EventInterest::MOUSE_ALL
}

fn event(&mut self, _: EventContext<'_>, event: &WidgetEvent) -> EventResponse {
match event {
WidgetEvent::MouseEnter => {
self.resp.just_hovered = true;
self.resp.hovered = true;
EventResponse::Bubble
}
WidgetEvent::MouseLeave => {
self.resp.hovered = false;
EventResponse::Bubble
}
WidgetEvent::MouseButtonChanged {
button: MouseButton::One,
down,
inside,
..
} => {
if *down && *inside {
self.resp.just_clicked = true;
self.resp.mousedown = true;
} else {
self.resp.mousedown = false;
}
EventResponse::Bubble
}
_ => EventResponse::Bubble,
}
}
}
2 changes: 2 additions & 0 deletions goryak/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod decoration;
mod dragvalue;
mod hovered;
mod icon;
mod interact_box;
mod layout;
mod roundrect;
mod scroll;
Expand All @@ -16,6 +17,7 @@ pub use decoration::*;
pub use dragvalue::*;
pub use hovered::*;
pub use icon::icon;
pub use interact_box::*;
pub use layout::*;
pub use roundrect::*;
pub use scroll::*;
Expand Down

0 comments on commit c08e278

Please sign in to comment.