plumb tooltips for image buttons. small refactor making a

color+rectangle for an image. organizing NewSpeedControls a bit
This commit is contained in:
Dustin Carlino 2019-11-28 09:55:56 -08:00
parent d553530ee7
commit 229bac4149
8 changed files with 60 additions and 53 deletions

View File

@ -1,6 +1,6 @@
use crate::{Color, ScreenDims, ScreenPt, ScreenRectangle, Text, UserInput};
use abstutil::Timer;
use geom::{Bounds, Polygon, Pt2D};
use geom::{Bounds, Distance, Polygon, Pt2D};
use glium::texture::Texture2dArray;
use glium_glyph::glyph_brush::rusttype::Scale;
use glium_glyph::glyph_brush::GlyphCruncher;
@ -206,6 +206,19 @@ impl Canvas {
panic!("Don't know texture {}", filename);
}
pub fn texture_rect(&self, filename: &str) -> (Color, Polygon) {
let color = self.texture(filename);
let dims = color.texture_dims();
(
color,
Polygon::rectangle_topleft(
Pt2D::new(0.0, 0.0),
Distance::meters(dims.width),
Distance::meters(dims.height),
),
)
}
pub fn save_camera_state(&self, map_name: &str) {
let state = CameraState {
cam_x: self.cam_x,

View File

@ -167,14 +167,13 @@ impl Button {
Button::new(normal, hovered, key, "", bg)
}
pub fn rectangle_img_no_bg(filename: &str, key: Option<MultiKey>, ctx: &EventCtx) -> Button {
let color = ctx.canvas.texture(filename);
let dims = color.texture_dims();
let rect = Polygon::rectangle_topleft(
Pt2D::new(0.0, 0.0),
Distance::meters(dims.width),
Distance::meters(dims.height),
);
pub fn rectangle_img_no_bg(
filename: &str,
tooltip: &str,
key: Option<MultiKey>,
ctx: &EventCtx,
) -> Button {
let (color, rect) = ctx.canvas.texture_rect(filename);
let normal = DrawBoth::new(ctx, GeomBatch::from(vec![(color, rect.clone())]), vec![]);
let hovered = DrawBoth::new(
@ -182,7 +181,7 @@ impl Button {
GeomBatch::from(vec![(color.with_masking(), rect.clone())]),
vec![],
);
Button::new(normal, hovered, key, "", rect)
Button::new(normal, hovered, key, tooltip, rect)
}
pub fn icon_btn_bg(

View File

@ -1,6 +1,5 @@
use crate::layout::Widget;
use crate::{DrawBoth, EventCtx, GeomBatch, GfxCtx, ScreenDims, ScreenPt, Text};
use geom::{Distance, Polygon, Pt2D};
// Just draw something. A widget just so layouting works.
pub struct JustDraw {
@ -11,16 +10,8 @@ pub struct JustDraw {
impl JustDraw {
pub fn image(filename: &str, ctx: &EventCtx) -> JustDraw {
let color = ctx.canvas.texture(filename);
let dims = color.texture_dims();
let batch = GeomBatch::from(vec![(
color,
Polygon::rectangle_topleft(
Pt2D::new(0.0, 0.0),
Distance::meters(dims.width),
Distance::meters(dims.height),
),
)]);
let (color, rect) = ctx.canvas.texture_rect(filename);
let batch = GeomBatch::from(vec![(color, rect)]);
JustDraw {
draw: DrawBoth::new(ctx, batch, vec![]),
top_left: ScreenPt::new(0.0, 0.0),

View File

@ -82,6 +82,7 @@ pub fn challenges_picker(ctx: &EventCtx) -> Box<dyn State> {
state.img_button_no_bg(
"assets/pregame/back.png",
"back",
hotkey(Key::Escape),
Box::new(|_, _| Some(Transition::Pop)),
);

View File

@ -16,7 +16,7 @@ const PANEL_RECT: ScreenRectangle = ScreenRectangle {
const ADJUST_SPEED_PERCENT: f64 = 0.01;
pub struct SpeedControls {
slider: Slider,
speed_slider: Slider,
state: State,
speed_cap: f64,
@ -89,10 +89,10 @@ impl SpeedControls {
// 10 sim minutes / real second normally, or 1 sim hour / real second for dev mode
let speed_cap: f64 = if dev_mode { 3600.0 } else { 600.0 };
let mut slider = Slider::new(150.0);
let mut speed_slider = Slider::new(150.0);
// Start with speed=1.0
slider.set_percent(ctx, (speed_cap / 1.0).powf(-1.0 / std::f64::consts::E));
slider.set_pos(ScreenPt::new(0.0, 100.0));
speed_slider.set_percent(ctx, (speed_cap / 1.0).powf(-1.0 / std::f64::consts::E));
speed_slider.set_pos(ScreenPt::new(0.0, 100.0));
let (small_step_btn, large_step_btn, jump_to_time_btn) = if step_controls {
let small = Button::icon_btn(
@ -128,7 +128,7 @@ impl SpeedControls {
};
SpeedControls {
slider,
speed_slider,
state: State::Paused,
speed_cap,
@ -150,16 +150,16 @@ impl SpeedControls {
let desired_speed = self.desired_speed();
if self.speed_up_btn.clicked() && desired_speed != self.speed_cap {
self.slider.set_percent(
self.speed_slider.set_percent(
ctx,
(self.slider.get_percent() + ADJUST_SPEED_PERCENT).min(1.0),
(self.speed_slider.get_percent() + ADJUST_SPEED_PERCENT).min(1.0),
);
} else if self.slow_down_btn.clicked() && desired_speed != 0.0 {
self.slider.set_percent(
self.speed_slider.set_percent(
ctx,
(self.slider.get_percent() - ADJUST_SPEED_PERCENT).max(0.0),
(self.speed_slider.get_percent() - ADJUST_SPEED_PERCENT).max(0.0),
);
} else if self.slider.event(ctx) {
} else if self.speed_slider.event(ctx) {
// Keep going
}
@ -235,7 +235,7 @@ impl SpeedControls {
}
self.slow_down_btn.draw(g);
self.speed_up_btn.draw(g);
self.slider.draw(g);
self.speed_slider.draw(g);
g.draw_text_at_screenspace_topleft(
&Text::from(Line(format!("{:.2}x", self.desired_speed()))).no_bg(),
ScreenPt::new(150.0, 100.0),
@ -266,6 +266,6 @@ impl SpeedControls {
}
fn desired_speed(&self) -> f64 {
self.speed_cap * self.slider.get_percent().powf(std::f64::consts::E)
self.speed_cap * self.speed_slider.get_percent().powf(std::f64::consts::E)
}
}

View File

@ -22,10 +22,11 @@ impl<'a> ManagedGUIStateBuilder<'a> {
pub fn img_button_no_bg(
&mut self,
filename: &str,
tooltip: &str,
hotkey: Option<MultiKey>,
onclick: Callback,
) {
let btn = Button::rectangle_img_no_bg(filename, hotkey, self.ctx);
let btn = Button::rectangle_img_no_bg(filename, tooltip, hotkey, self.ctx);
self.state.buttons.push((btn, onclick));
}

View File

@ -116,6 +116,7 @@ pub fn main_menu(ctx: &EventCtx, ui: &UI) -> Box<dyn State> {
);
state.img_button_no_bg(
"assets/pregame/quit.png",
"quit",
hotkey(Key::Escape),
Box::new(|_, _| {
// TODO before_quit?
@ -144,6 +145,7 @@ fn about(ctx: &EventCtx) -> Box<dyn State> {
state.img_button_no_bg(
"assets/pregame/back.png",
"back",
hotkey(Key::Escape),
Box::new(|_, _| Some(Transition::Pop)),
);

View File

@ -20,7 +20,7 @@ const PANEL_RECT: ScreenRectangle = ScreenRectangle {
const ADJUST_SPEED_PERCENT: f64 = 0.01;
pub struct SpeedControls {
slider: Slider,
speed_slider: Slider,
state: State,
speed_cap: f64,
@ -62,14 +62,14 @@ impl SpeedControls {
let resume_btn = Button::rectangle_img_no_bg(
"assets/ui/resume.png",
//"resume",
"resume",
hotkey(Key::Space),
ctx,
)
.at(ScreenPt::new(10.0, 10.0));
let pause_btn = Button::rectangle_img_no_bg(
"assets/ui/pause.png",
//"pause",
"pause",
hotkey(Key::Space),
ctx,
)
@ -77,7 +77,7 @@ impl SpeedControls {
let jump_to_time_btn = Button::rectangle_img_no_bg(
"assets/ui/jump_to_time.png",
//"jump to specific time",
"jump to specific time",
hotkey(Key::B),
ctx,
)
@ -85,7 +85,7 @@ impl SpeedControls {
let small_step_btn = Button::rectangle_img_no_bg(
"assets/ui/small_step.png",
//"step forwards 0.1s",
"step forwards 0.1s",
hotkey(Key::M),
ctx,
)
@ -93,7 +93,7 @@ impl SpeedControls {
let large_step_btn = Button::rectangle_img_no_bg(
"assets/ui/large_step.png",
//"step forwards 10 mins",
"step forwards 10 mins",
hotkey(Key::N),
ctx,
)
@ -106,28 +106,28 @@ impl SpeedControls {
// 10 sim minutes / real second normally, or 1 sim hour / real second for dev mode
let speed_cap: f64 = if dev_mode { 3600.0 } else { 600.0 };
let mut slider = Slider::new(270.0);
let mut speed_slider = Slider::new(270.0);
// Start with speed=1.0
slider.set_percent(ctx, (speed_cap / 1.0).powf(-1.0 / std::f64::consts::E));
slider.set_pos(ScreenPt::new(50.0, 150.0));
speed_slider.set_percent(ctx, (speed_cap / 1.0).powf(-1.0 / std::f64::consts::E));
speed_slider.set_pos(ScreenPt::new(50.0, 150.0));
let slow_down_btn = Button::rectangle_img_no_bg(
"assets/ui/slow_down.png",
//"slow down",
"slow down",
hotkey(Key::LeftBracket),
ctx,
)
.at(ScreenPt::new(290.0, 170.0));
let speed_up_btn = Button::rectangle_img_no_bg(
"assets/ui/speed_up.png",
//"speed up",
"speed up",
hotkey(Key::RightBracket),
ctx,
)
.at(ScreenPt::new(425.0, 160.0));
SpeedControls {
slider,
speed_slider,
state: State::Paused,
speed_cap,
@ -150,16 +150,16 @@ impl SpeedControls {
let desired_speed = self.desired_speed();
if self.speed_up_btn.clicked() && desired_speed != self.speed_cap {
self.slider.set_percent(
self.speed_slider.set_percent(
ctx,
(self.slider.get_percent() + ADJUST_SPEED_PERCENT).min(1.0),
(self.speed_slider.get_percent() + ADJUST_SPEED_PERCENT).min(1.0),
);
} else if self.slow_down_btn.clicked() && desired_speed != 0.0 {
self.slider.set_percent(
self.speed_slider.set_percent(
ctx,
(self.slider.get_percent() - ADJUST_SPEED_PERCENT).max(0.0),
(self.speed_slider.get_percent() - ADJUST_SPEED_PERCENT).max(0.0),
);
} else if self.slider.event(ctx) {
} else if self.speed_slider.event(ctx) {
// Keep going
}
@ -328,7 +328,7 @@ impl SpeedControls {
ScreenPt::new(10.0, y1),
);
self.slider.draw(g);
self.speed_slider.draw(g);
self.slow_down_btn.draw(g);
@ -356,7 +356,7 @@ impl SpeedControls {
}
fn desired_speed(&self) -> f64 {
self.speed_cap * self.slider.get_percent().powf(std::f64::consts::E)
self.speed_cap * self.speed_slider.get_percent().powf(std::f64::consts::E)
}
}