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::*; 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)] #[derive(Clone, Copy, Debug)]
pub enum ControlState { pub enum ControlState {
Default, Default,

View File

@ -1,5 +1,7 @@
use super::ButtonStyle; 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> { pub trait StyledButtons<'a> {
fn btn_primary_dark(&self) -> ButtonBuilder<'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> { fn btn_plain_light_icon(&self, image_path: &'a str) -> ButtonBuilder<'a> {
icon_button(self.btn_plain_light().image_path(image_path)) 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> { fn btn_plain_light_icon_bytes(&self, labeled_bytes: (&'a str, &'a [u8])) -> ButtonBuilder<'a> {
icon_button(self.btn_plain_light().image_bytes(image_bytes)) 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> { fn btn_plain_light_icon_text(&self, image_path: &'a str, text: &'a str) -> ButtonBuilder<'a> {
self.btn_plain_light() 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> { fn back_button<'a>(builder: ButtonBuilder<'a>, title: &'a str) -> ButtonBuilder<'a> {
// DESIGN REVIEW: this button seems absurdly large // DESIGN REVIEW: this button seems absurdly large
builder builder
.image_path("system/assets/pregame/back.svg") .image_bytes(include_labeled_bytes!("../../icons/nav_back.svg"))
.label_text(title) .label_text(title)
.padding_left(8.0) .padding_left(8.0)
.font_size(30) .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> { fn dropdown_button<'a>(builder: ButtonBuilder<'a>) -> ButtonBuilder<'a> {
builder builder
.image_bytes(( .image_bytes(include_labeled_bytes!("../../icons/arrow_drop_down.svg"))
include_bytes!("../../icons/arrow_drop_down.svg"),
"../../icons/arrow_drop_down.svg",
))
.image_dims(12.0) .image_dims(12.0)
.stack_spacing(12.0) .stack_spacing(12.0)
.label_first() .label_first()

View File

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

View File

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

View File

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

View File

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