Skip to content

Commit

Permalink
add roadtypes select with icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 25, 2024
1 parent 24f5a87 commit 4bd3401
Show file tree
Hide file tree
Showing 27 changed files with 690 additions and 303 deletions.
3 changes: 3 additions & 0 deletions assets/ui/icons/roadtypes_avenue.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/roadtypes_avenue_1way.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/roadtypes_drive.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/roadtypes_drive_1way.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/roadtypes_highway.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/roadtypes_highway_1way.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/roadtypes_rail.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/roadtypes_rail_1way.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/roadtypes_street.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/roadtypes_street_1way.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/snap_grid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 21 additions & 21 deletions assets_gui/src/yakui_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use engine::{set_cursor_icon, CursorIcon, Drawable, GfxContext, InstancedMesh, M
use geom::Matrix4;
use goryak::{
background, button_primary, checkbox_value, divider, dragvalue, icon, interact_box_radius,
is_hovered, labelc, on_secondary_container, on_surface, outline_variant, round_rect,
scroll_vertical, secondary_container, set_theme, surface, surface_variant, use_changed,
CountGrid, RoundRect, Theme,
is_hovered, on_secondary_container, on_surface, outline_variant, round_rect, scroll_vertical,
secondary_container, set_theme, surface, surface_variant, textc, use_changed, CountGrid,
RoundRect, Theme,
};
use prototypes::{prototypes_iter, GoodsCompanyID, GoodsCompanyPrototype};

Expand Down Expand Up @@ -149,7 +149,7 @@ impl State {
let triangle = if v { "caret-down" } else { "caret-right" };
icon(on_surface(), triangle);
}
labelc(on_surface(), name);
textc(on_surface(), name);
});
});
},
Expand All @@ -162,13 +162,13 @@ impl State {
fn model_properties(&mut self) {
Self::model_properties_container(|| {
let tc = on_secondary_container(); // text color
labelc(tc, "Model properties");
textc(tc, "Model properties");
match self.gui.shown {
Shown::None => {
labelc(tc, "No model selected");
textc(tc, "No model selected");
}
Shown::Error(ref e) => {
labelc(tc, e.clone());
textc(tc, e.clone());
}
Shown::Model((ref mesh, _, ref mut props)) => {
let params = use_state(|| LodGenerateParams {
Expand All @@ -185,13 +185,13 @@ impl State {
.main_axis_size(MainAxisSize::Min)
.show(|| {
params.modify(|mut params| {
labelc(tc, "n_lods");
textc(tc, "n_lods");
dragvalue().min(1.0).max(4.0).show(&mut params.n_lods);

labelc(tc, "quality");
textc(tc, "quality");
dragvalue().min(0.0).max(1.0).show(&mut params.quality);

labelc(tc, "sloppy");
textc(tc, "sloppy");
checkbox_value(&mut params.sloppy);

params
Expand All @@ -208,35 +208,35 @@ impl State {
let mut lod_details = CountGrid::col(1 + mesh.lods.len());
lod_details.main_axis_size = MainAxisSize::Min;
lod_details.show(|| {
labelc(tc, "");
textc(tc, "");
for (i, _) in mesh.lods.iter().enumerate() {
labelc(tc, format!("LOD{}", i));
textc(tc, format!("LOD{}", i));
}

labelc(tc, "Vertices");
textc(tc, "Vertices");
for lod in &*mesh.lods {
labelc(tc, format!("{}", lod.n_vertices));
textc(tc, format!("{}", lod.n_vertices));
}

labelc(tc, "Triangles");
textc(tc, "Triangles");
for lod in &*mesh.lods {
labelc(tc, format!("{}", lod.n_indices / 3));
textc(tc, format!("{}", lod.n_indices / 3));
}

labelc(tc, "Draw calls");
textc(tc, "Draw calls");
for lod in &*mesh.lods {
labelc(tc, format!("{}", lod.primitives.len()));
textc(tc, format!("{}", lod.primitives.len()));
}

labelc(tc, "Coverage");
textc(tc, "Coverage");
for lod in &*mesh.lods {
labelc(tc, format!("{:.3}", lod.screen_coverage));
textc(tc, format!("{:.3}", lod.screen_coverage));
}
});
});
}
Shown::Sprite(ref _sprite) => {
labelc(tc, "Sprite");
textc(tc, "Sprite");
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions goryak/src/dragvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use yakui_widgets::widgets::{List, Pad};
use yakui_widgets::{draggable, pad, use_state};

use crate::roundrect::RoundRect;
use crate::{labelc, on_primary, outline, secondary};
use crate::{on_primary, outline, secondary, textc};

pub trait Draggable: Copy {
const DEFAULT_STEP: f64;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl DragValue {
.color(secondary())
.show_children(|| {
pad(Pad::horizontal(10.0), || {
labelc(on_primary(), format!("{:.3}", T::to_f64(*value)));
textc(on_primary(), format!("{:.3}", T::to_f64(*value)));
});
});
});
Expand Down
15 changes: 14 additions & 1 deletion goryak/src/icon.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
use phf::phf_map;
use std::borrow::Cow;
use yakui_core::geometry::{Color, Constraints, Vec2};
use yakui_widgets::font::FontName;
use yakui_widgets::widgets::Text;
use yakui_widgets::widgets::{Button, Text};
use yakui_widgets::{center, constrained};

pub fn icon_map(name: &str) -> (&'static str, FontName) {
let mapped = ICON_NAME_MAPPING.get(name).copied().unwrap_or("?");
(mapped, FontName::new("icons"))
}

#[must_use = "call show() to show the widget"]
pub fn icon_button(mut b: Button) -> Button {
let (mapped, name) = icon_map(&b.text);

b.text = Cow::Borrowed(mapped);
b.style.text.font = name.clone();
b.hover_style.text.font = name.clone();
b.down_style.text.font = name;

b
}

pub fn icon(c: Color, name: &str) {
let (mapped, fontname) = icon_map(name);
let mut t = Text::new(20.0, mapped);
Expand Down
53 changes: 49 additions & 4 deletions goryak/src/imagebutton.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::time::Instant;

use yakui_core::event::{EventInterest, EventResponse, WidgetEvent};
use yakui_core::geometry::{Color, Constraints, Rect, Vec2};
use yakui_core::geometry::{Color, Constraints, Dim2, 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};
use yakui_core::{Alignment, Response, TextureId};
use yakui_widgets::{offset, reflow};

use crate::{on_primary, padxy, primary, round_rect, textc};

/**
A button based on an image
Expand All @@ -17,6 +22,7 @@ pub struct ImageButton {
pub color: Color,
pub hover_color: Color,
pub active_color: Color,
pub tooltip: &'static str,
}

impl ImageButton {
Expand All @@ -27,6 +33,7 @@ impl ImageButton {
color: Color::WHITE,
hover_color: Color::WHITE,
active_color: Color::WHITE,
tooltip: "",
}
}

Expand All @@ -43,6 +50,7 @@ impl ImageButton {
color,
hover_color,
active_color,
tooltip: "",
}
}

Expand All @@ -57,13 +65,15 @@ pub fn image_button(
color: Color,
hover_color: Color,
active_color: Color,
tooltip: &'static str,
) -> Response<ImageButtonResponse> {
ImageButton {
texture: Some(texture),
size,
color,
hover_color,
active_color,
tooltip,
}
.show()
}
Expand All @@ -72,6 +82,8 @@ pub fn image_button(
pub struct ImageButtonWidget {
props: ImageButton,
resp: ImageButtonResponse,
stopped_moving: Option<Instant>,
show_tooltip: bool,
}

#[derive(Copy, Clone, Debug, Default)]
Expand All @@ -90,6 +102,8 @@ impl Widget for ImageButtonWidget {
Self {
props: ImageButton::empty(),
resp: ImageButtonResponse::default(),
stopped_moving: None,
show_tooltip: false,
}
}

Expand All @@ -98,6 +112,26 @@ impl Widget for ImageButtonWidget {
let resp = self.resp;
self.resp.mouse_entered = false;
self.resp.clicked = false;

if !self.props.tooltip.is_empty() {
if let Some(i) = self.stopped_moving {
if i.elapsed().as_millis() > 500 {
self.show_tooltip = true;
}
if self.show_tooltip {
reflow(Alignment::TOP_LEFT, Dim2::pixels(0.0, 0.0), || {
offset(Vec2::new(-10.0, -50.0), || {
round_rect(5.0, primary(), || {
padxy(5.0, 4.0, || {
textc(on_primary(), self.props.tooltip);
});
});
});
});
}
}
}

resp
}

Expand Down Expand Up @@ -130,18 +164,29 @@ impl Widget for ImageButtonWidget {
}

fn layout(&self, _ctx: LayoutContext<'_>, input: Constraints) -> Vec2 {
let _ = self.default_layout(_ctx, input); // tooltip is reflowed

input.constrain_min(self.props.size)
}

fn event(&mut self, _: EventContext<'_>, event: &WidgetEvent) -> EventResponse {
match event {
match *event {
WidgetEvent::MouseMoved(Some(_)) => {
if self.resp.hovering {
self.stopped_moving = Some(Instant::now());
}
self.show_tooltip = false;
EventResponse::Bubble
}
WidgetEvent::MouseEnter => {
self.resp.mouse_entered = true;
self.resp.hovering = true;
EventResponse::Bubble
}
WidgetEvent::MouseLeave => {
self.resp.hovering = false;
self.show_tooltip = false;
self.stopped_moving = None;
EventResponse::Bubble
}
WidgetEvent::MouseButtonChanged {
Expand All @@ -150,7 +195,7 @@ impl Widget for ImageButtonWidget {
inside,
..
} => {
if *down && *inside {
if down && inside {
self.resp.clicked = true;
self.resp.mouse_down = true;
} else {
Expand Down
1 change: 1 addition & 0 deletions goryak/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod roundrect;
mod scroll;
mod text;
mod theme;
mod tooltip;
mod util;

pub use blur_bg::*;
Expand Down
4 changes: 3 additions & 1 deletion goryak/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::DEFAULT_FONT_SIZE;
use std::borrow::Cow;
use yakui_core::geometry::Color;
use yakui_core::Response;
use yakui_widgets::font::FontName;
use yakui_widgets::widgets::{Text, TextResponse};
Expand All @@ -8,8 +9,9 @@ pub fn text<S: Into<Cow<'static, str>>>(text: S) -> Response<TextResponse> {
Text::new(DEFAULT_FONT_SIZE, text.into()).show()
}

pub fn monospace<S: Into<Cow<'static, str>>>(text: S) -> Response<TextResponse> {
pub fn monospace<S: Into<Cow<'static, str>>>(col: Color, text: S) -> Response<TextResponse> {
let mut t = Text::new(DEFAULT_FONT_SIZE, text.into());
t.style.font = FontName::new("monospace");
t.style.color = col;
t.show()
}
Loading

0 comments on commit 4bd3401

Please sign in to comment.