cleanup with include_labeled_bytes!

This commit is contained in:
Michael Kirk 2021-01-22 09:30:20 -06:00
parent 4e48de6185
commit 202aec77cf
7 changed files with 51 additions and 70 deletions

View File

Before

Width:  |  Height:  |  Size: 280 B

After

Width:  |  Height:  |  Size: 280 B

View File

@ -93,6 +93,14 @@ mod backend {
pub use crate::backend_glow::*;
}
/// return the name of the included file, along with its bytes.
#[macro_export]
macro_rules! include_labeled_bytes {
($file:expr) => {
($file, include_bytes!($file))
};
}
#[derive(Clone, Copy, Debug)]
pub enum ControlState {
Default,

View File

@ -1,5 +1,7 @@
use super::ButtonStyle;
use crate::{ButtonBuilder, ControlState, EventCtx, ScreenDims, Style, Widget};
use crate::{
include_labeled_bytes, ButtonBuilder, ControlState, EventCtx, ScreenDims, Style, Widget,
};
pub trait StyledButtons<'a> {
fn btn_primary_dark(&self) -> ButtonBuilder<'a>;
@ -87,8 +89,8 @@ pub trait StyledButtons<'a> {
fn btn_plain_light_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
icon_button(self.btn_plain_light().image_path(image_path))
}
fn btn_plain_light_icon_bytes(&self, image_bytes: (&'a [u8], &'a str)) -> ButtonBuilder<'a> {
icon_button(self.btn_plain_light().image_bytes(image_bytes))
fn btn_plain_light_icon_bytes(&self, labeled_bytes: (&'a str, &'a [u8])) -> ButtonBuilder<'a> {
icon_button(self.btn_plain_light().image_bytes(labeled_bytes))
}
fn btn_plain_light_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
self.btn_plain_light()
@ -297,7 +299,7 @@ fn icon_button<'a>(builder: ButtonBuilder<'a>) -> ButtonBuilder<'a> {
fn back_button<'a>(builder: ButtonBuilder<'a>, title: &'a str) -> ButtonBuilder<'a> {
// DESIGN REVIEW: this button seems absurdly large
builder
.image_path("system/assets/pregame/back.svg")
.image_bytes(include_labeled_bytes!("../../icons/nav_back.svg"))
.label_text(title)
.padding_left(8.0)
.font_size(30)
@ -305,10 +307,7 @@ fn back_button<'a>(builder: ButtonBuilder<'a>, title: &'a str) -> ButtonBuilder<
fn dropdown_button<'a>(builder: ButtonBuilder<'a>) -> ButtonBuilder<'a> {
builder
.image_bytes((
include_bytes!("../../icons/arrow_drop_down.svg"),
"../../icons/arrow_drop_down.svg",
))
.image_bytes(include_labeled_bytes!("../../icons/arrow_drop_down.svg"))
.image_dims(12.0)
.stack_spacing(12.0)
.label_first()

View File

@ -270,13 +270,16 @@ impl<'b, 'a: 'b> ButtonBuilder<'a> {
/// This will replace any image previously set by [`image_path`].
///
/// `bytes`: utf-8 encoded bytes of the svg
/// `name`: a label to describe the bytes for debugging purposes
pub fn image_bytes(mut self, bytes_and_cache_key: (&'a [u8], &'a str)) -> Self {
let (bytes, cache_key) = bytes_and_cache_key;
/// `label`: a label to describe the bytes for debugging purposes
pub fn image_bytes(mut self, labeled_bytes: (&'a str, &'a [u8])) -> Self {
let (label, bytes) = labeled_bytes;
// Currently we don't support setting image for other states like "hover", we easily
// could, but the API gets more verbose for a thing we don't currently need.
let mut image = self.default_style.image.take().unwrap_or_default();
image.source = Some(ImageSource::Bytes { bytes, cache_key });
image.source = Some(ImageSource::Bytes {
bytes,
cache_key: label,
});
self.default_style.image = Some(image);
self
}

View File

@ -1,6 +1,7 @@
use crate::{
Button, Color, ControlState, EdgeInsets, EventCtx, GfxCtx, MultiKey, Outcome, RewriteColor,
ScreenDims, ScreenPt, StyledButtons, Text, TextSpan, Widget, WidgetImpl, WidgetOutput,
include_labeled_bytes, Button, Color, ControlState, EdgeInsets, EventCtx, GfxCtx, MultiKey,
Outcome, RewriteColor, ScreenDims, ScreenPt, StyledButtons, Text, TextSpan, Widget, WidgetImpl,
WidgetOutput,
};
pub struct Checkbox {
@ -43,16 +44,10 @@ impl Checkbox {
let off_button = buttons
.clone()
.image_bytes((
include_bytes!("../../icons/toggle_off.svg"),
"../../icons/toggle_off.svg",
))
.image_bytes(include_labeled_bytes!("../../icons/toggle_off.svg"))
.build(ctx, label);
let on_button = buttons
.image_bytes((
include_bytes!("../../icons/toggle_on.svg"),
"../../icons/toggle_on.svg",
))
.image_bytes(include_labeled_bytes!("../../icons/toggle_on.svg"))
.build(ctx, label);
Checkbox::new(enabled, off_button, on_button).named(label)
@ -86,14 +81,11 @@ impl Checkbox {
buttons = buttons.hotkey(hotkey);
}
let false_btn = buttons.clone().image_bytes((
include_bytes!("../../icons/checkbox_unchecked.svg"),
"../../icons/checkbox_unchecked.svg",
));
let true_btn = buttons.image_bytes((
include_bytes!("../../icons/checkbox_checked.svg"),
"../../icons/checkbox_checked.svg",
));
let false_btn = buttons
.clone()
.image_bytes(include_labeled_bytes!("../../icons/checkbox_unchecked.svg"));
let true_btn =
buttons.image_bytes(include_labeled_bytes!("../../icons/checkbox_checked.svg"));
Checkbox::new(
enabled,
@ -132,14 +124,11 @@ impl Checkbox {
buttons = buttons.hotkey(hotkey);
}
let false_btn = buttons.clone().image_bytes((
include_bytes!("../../icons/checkbox_unchecked.svg"),
"../../icons/checkbox_unchecked.svg",
));
let true_btn = buttons.image_bytes((
include_bytes!("../../icons/checkbox_checked.svg"),
"../../icons/checkbox_checked.svg",
));
let false_btn = buttons
.clone()
.image_bytes(include_labeled_bytes!("../../icons/checkbox_unchecked.svg"));
let true_btn =
buttons.image_bytes(include_labeled_bytes!("../../icons/checkbox_checked.svg"));
Checkbox::new(
enabled,
@ -154,20 +143,14 @@ impl Checkbox {
let false_btn = buttons
.clone()
.image_bytes((
include_bytes!("../../icons/checkbox_unchecked.svg"),
"../../icons/checkbox_unchecked.svg",
))
.image_bytes(include_labeled_bytes!("../../icons/checkbox_unchecked.svg"))
.image_color(
RewriteColor::Change(Color::BLACK, color.alpha(0.3)),
ControlState::Default,
);
let true_btn = buttons
.image_bytes((
include_bytes!("../../icons/checkbox_checked.svg"),
"../../icons/checkbox_checked.svg",
))
.image_bytes(include_labeled_bytes!("../../icons/checkbox_checked.svg"))
.image_color(
RewriteColor::Change(Color::BLACK, color),
ControlState::Default,
@ -192,10 +175,7 @@ impl Checkbox {
) -> Widget {
let mut toggle_left_button = ctx
.style()
.btn_plain_light_icon_bytes((
include_bytes!("../../icons/toggle_left.svg"),
"../../icons/toggle_left.svg",
))
.btn_plain_light_icon_bytes(include_labeled_bytes!("../../icons/toggle_left.svg"))
.image_dims(ScreenDims::new(40.0, 40.0))
.padding(4)
.image_color(RewriteColor::NoOp, ControlState::Default);
@ -204,10 +184,9 @@ impl Checkbox {
toggle_left_button = toggle_left_button.hotkey(hotkey);
}
let toggle_right_button = toggle_left_button.clone().image_bytes((
include_bytes!("../../icons/toggle_right.svg"),
"../../icons/toggle_right.svg",
));
let toggle_right_button = toggle_left_button
.clone()
.image_bytes(include_labeled_bytes!("../../icons/toggle_right.svg"));
let left_text_button = ctx
.style()

View File

@ -1,8 +1,8 @@
use geom::{Polygon, Pt2D};
use crate::{
text, Button, EdgeInsets, EventCtx, GeomBatch, GfxCtx, Line, Outcome, ScreenDims, ScreenPt,
ScreenRectangle, StyledButtons, Text, Widget, WidgetImpl, WidgetOutput,
include_labeled_bytes, text, Button, EdgeInsets, EventCtx, GeomBatch, GfxCtx, Line, Outcome,
ScreenDims, ScreenPt, ScreenRectangle, StyledButtons, Text, Widget, WidgetImpl, WidgetOutput,
};
// TODO MAX_CHAR_WIDTH is a hardcoded nonsense value
@ -38,17 +38,11 @@ impl Spinner {
let up = button_builder
.clone()
.image_bytes((
include_bytes!("../../icons/arrow_up.svg"),
"../../icons/arrow_up.svg",
))
.image_bytes(include_labeled_bytes!("../../icons/arrow_up.svg"))
.build(ctx, "increase value");
let down = button_builder
.image_bytes((
include_bytes!("../../icons/arrow_down.svg"),
"../../icons/arrow_down.svg",
))
.image_bytes(include_labeled_bytes!("../../icons/arrow_down.svg"))
.build(ctx, "decrease value");
let dims = ScreenDims::new(

View File

@ -2,7 +2,8 @@ use abstutil::prettyprint_usize;
use geom::Polygon;
use crate::{
Color, ControlState, EventCtx, GeomBatch, Line, Panel, StyledButtons, Text, TextExt, Widget,
include_labeled_bytes, Color, ControlState, EventCtx, GeomBatch, Line, Panel, StyledButtons,
Text, TextExt, Widget,
};
const ROWS: usize = 8;
@ -102,12 +103,9 @@ impl<A, T, F> Table<A, T, F> {
ctx.style()
.btn_primary_dark_icon_text("tmp", &col.name)
.image_bytes(if self.descending {
(
include_bytes!("../../icons/arrow_down.svg"),
"arrow_down.svg",
)
include_labeled_bytes!("../../icons/arrow_down.svg")
} else {
(include_bytes!("../../icons/arrow_up.svg"), "arrow_up.svg")
include_labeled_bytes!("../../icons/arrow_up.svg")
})
.label_first()
.build_widget(ctx, &col.name)