Skip to content

Commit

Permalink
start working on bottom tool picker
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 24, 2024
1 parent 1e7b039 commit 05494cb
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 37 deletions.
36 changes: 8 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions assets/ui/icons/curved_road.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/ui/icons/straight_road.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions engine/src/yakui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl YakuiWrapper {
gfx.null_texture.mip_view(0),
wgpu::FilterMode::Linear,
wgpu::FilterMode::Linear,
wgpu::FilterMode::Linear,
);

goryak::set_blur_texture(texture_id);
Expand All @@ -62,6 +63,7 @@ impl YakuiWrapper {
Arc::new(tex.texture.create_view(&TextureViewDescriptor::default())),
wgpu::FilterMode::Linear,
wgpu::FilterMode::Linear,
wgpu::FilterMode::Linear,
)
}

Expand Down
164 changes: 164 additions & 0 deletions goryak/src/imagebutton.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, Rect, Vec2};
use yakui_core::input::MouseButton;
use yakui_core::paint::PaintRect;
use yakui_core::widget::{EventContext, LayoutContext, PaintContext, Widget};
use yakui_core::{Response, TextureId};

/**
A button based on an image
Responds with [ImageButtonResponse].
*/
#[derive(Debug, Clone)]
pub struct ImageButton {
pub texture: Option<TextureId>,
pub size: Vec2,
pub color: Color,
pub hover_color: Color,
pub active_color: Color,
}

impl ImageButton {
pub fn empty() -> Self {
Self {
texture: None,
size: Vec2::ZERO,
color: Color::WHITE,
hover_color: Color::WHITE,
active_color: Color::WHITE,
}
}

pub fn new(
texture: TextureId,
size: Vec2,
color: Color,
hover_color: Color,
active_color: Color,
) -> Self {
Self {
texture: Some(texture),
size,
color,
hover_color,
active_color,
}
}

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

pub fn image_button(
texture: TextureId,
size: Vec2,
color: Color,
hover_color: Color,
active_color: Color,
) -> Response<ImageButtonResponse> {
ImageButton {
texture: Some(texture),
size,
color,
hover_color,
active_color,
}
.show()
}

#[derive(Debug)]
pub struct ImageButtonWidget {
props: ImageButton,
resp: ImageButtonResponse,
}

#[derive(Copy, Clone, Debug, Default)]
pub struct ImageButtonResponse {
pub hovering: bool,
pub mouse_down: bool,
pub mouse_entered: bool,
pub clicked: bool,
}

impl Widget for ImageButtonWidget {
type Props<'a> = ImageButton;
type Response = ImageButtonResponse;

fn new() -> Self {
Self {
props: ImageButton::empty(),
resp: ImageButtonResponse::default(),
}
}

fn update(&mut self, props: Self::Props<'_>) -> Self::Response {
self.props = props;
let resp = self.resp;
self.resp.mouse_entered = false;
self.resp.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.mouse_down {
self.props.active_color
} else if self.resp.hovering {
self.props.hover_color
} else {
self.props.color
};

let mut rect = PaintRect::new(layout_node.rect);
rect.color = curcolor;
if let Some(tex) = self.props.texture {
rect.texture = Some((tex, Rect::ONE));
}
rect.add(ctx.paint);

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

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

fn layout(&self, _ctx: LayoutContext<'_>, input: Constraints) -> Vec2 {
input.constrain_min(self.props.size)
}

fn event(&mut self, _: EventContext<'_>, event: &WidgetEvent) -> EventResponse {
match event {
WidgetEvent::MouseEnter => {
self.resp.mouse_entered = true;
self.resp.hovering = true;
EventResponse::Bubble
}
WidgetEvent::MouseLeave => {
self.resp.hovering = false;
EventResponse::Bubble
}
WidgetEvent::MouseButtonChanged {
button: MouseButton::One,
down,
inside,
..
} => {
if *down && *inside {
self.resp.clicked = true;
self.resp.mouse_down = true;
} else {
self.resp.mouse_down = 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 @@ -6,6 +6,7 @@ mod divider;
mod dragvalue;
mod hovered;
mod icon;
mod imagebutton;
mod interact_box;
mod layout;
mod roundrect;
Expand All @@ -22,6 +23,7 @@ pub use divider::*;
pub use dragvalue::*;
pub use hovered::*;
pub use icon::*;
pub use imagebutton::*;
pub use interact_box::*;
pub use layout::*;
pub use roundrect::*;
Expand Down
5 changes: 4 additions & 1 deletion native_app/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ impl UiTextures {
}

pub fn get_yakui(&self, name: &str) -> yakui::TextureId {
self.yakui_textures.get(name).unwrap().clone()
match self.yakui_textures.get(name) {
None => panic!("Couldn't find texture {}", name),
Some(x) => *x,
}
}

pub fn try_get(&self, name: &str) -> Option<TextureId> {
Expand Down
23 changes: 18 additions & 5 deletions native_app/src/gui/tools/roadbuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl RoadBuildResource {
points: Option<PolyLine3>,
) {
let mut proj_pos = proj.pos;
proj_pos.z += 0.1;
proj_pos.z += 0.4;
let col = if is_valid {
simulation::config().gui_primary
} else {
Expand All @@ -413,7 +413,11 @@ impl RoadBuildResource {
for i in 0..=32 {
let ang = std::f32::consts::PI * i as f32 * (2.0 / 32.0);
let mut v = Vec3::from_angle(ang, dir.z);
let center = if v.dot(dir) < 0.0 { x.pos } else { proj.pos };
let center = if v.dot(dir) < 0.0 {
x.pos.up(0.4)
} else {
proj_pos
};

v = v * patwidth * 0.5;
v.z = 0.0;
Expand All @@ -439,8 +443,17 @@ impl RoadBuildResource {
.color(col);
}

immdraw.circle(p.first(), patwidth * 0.5).color(col);
immdraw.circle(p.last(), patwidth * 0.5).color(col);
immdraw.polyline(p.into_vec(), patwidth, false).color(col);
immdraw.circle(p.first().up(0.4), patwidth * 0.5).color(col);
immdraw.circle(p.last().up(0.4), patwidth * 0.5).color(col);
immdraw
.polyline(
p.into_vec()
.into_iter()
.map(|v| v.up(0.4))
.collect::<Vec<_>>(),
patwidth,
false,
)
.color(col);
}
}
Loading

0 comments on commit 05494cb

Please sign in to comment.