use geom::{Distance, Polygon};
use crate::{
text::Font, Color, ContentMode, ControlState, CornerRounding, Drawable, EdgeInsets, EventCtx,
GeomBatch, GfxCtx, Image, Line, MultiKey, Outcome, OutlineStyle, RewriteColor, ScreenDims,
ScreenPt, ScreenRectangle, Text, Widget, WidgetImpl, WidgetOutput,
};
use crate::geom::geom_batch_stack::{Axis, GeomBatchStack};
pub struct Button {
pub action: String,
draw_normal: Drawable,
draw_hovered: Drawable,
draw_disabled: Drawable,
pub(crate) hotkey: Option<MultiKey>,
tooltip: Text,
hitbox: Polygon,
pub(crate) hovering: bool,
is_disabled: bool,
pub(crate) top_left: ScreenPt,
pub(crate) dims: ScreenDims,
}
impl Button {
fn new(
ctx: &EventCtx,
normal: GeomBatch,
hovered: GeomBatch,
disabled: GeomBatch,
hotkey: Option<MultiKey>,
action: &str,
maybe_tooltip: Option<Text>,
hitbox: Polygon,
is_disabled: bool,
) -> Button {
let bounds = hitbox.get_bounds();
let dims = ScreenDims::new(bounds.width(), bounds.height());
assert!(!action.is_empty());
Button {
action: action.to_string(),
draw_normal: ctx.upload(normal),
draw_hovered: ctx.upload(hovered),
draw_disabled: ctx.upload(disabled),
tooltip: if let Some(t) = maybe_tooltip {
t
} else {
Text::tooltip(ctx, hotkey.clone(), action)
},
hotkey,
hitbox,
is_disabled,
hovering: false,
top_left: ScreenPt::new(0.0, 0.0),
dims,
}
}
pub fn is_enabled(&self) -> bool {
!self.is_disabled
}
}
impl WidgetImpl for Button {
fn get_dims(&self) -> ScreenDims {
self.dims
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.top_left = top_left;
}
fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput) {
if ctx.redo_mouseover() {
if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
self.hovering = self
.hitbox
.translate(self.top_left.x, self.top_left.y)
.contains_pt(pt.to_pt());
} else {
self.hovering = false;
}
}
if self.is_disabled {
return;
}
if self.hovering && ctx.normal_left_click() {
self.hovering = false;
output.outcome = Outcome::Clicked(self.action.clone());
return;
}
if ctx.input.pressed(self.hotkey.clone()) {
self.hovering = false;
output.outcome = Outcome::Clicked(self.action.clone());
return;
}
if self.hovering {
ctx.cursor_clickable();
}
}
fn draw(&self, g: &mut GfxCtx) {
if self.is_disabled {
g.redraw_at(self.top_left, &self.draw_disabled);
} else if self.hovering {
g.redraw_at(self.top_left, &self.draw_hovered);
if !self.tooltip.is_empty() {
g.draw_mouse_tooltip(self.tooltip.clone());
}
} else {
g.redraw_at(self.top_left, &self.draw_normal);
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ButtonBuilder<'a, 'c> {
padding: EdgeInsets,
stack_spacing: f64,
hotkey: Option<MultiKey>,
tooltip: Option<Text>,
stack_axis: Option<Axis>,
is_label_before_image: bool,
corner_rounding: Option<CornerRounding>,
is_disabled: bool,
default_style: ButtonStateStyle<'a, 'c>,
hover_style: ButtonStateStyle<'a, 'c>,
disable_style: ButtonStateStyle<'a, 'c>,
}
#[derive(Clone, Debug, Default)]
struct ButtonStateStyle<'a, 'c> {
image: Option<Image<'a, 'c>>,
label: Option<Label>,
outline: Option<OutlineStyle>,
bg_color: Option<Color>,
custom_batch: Option<GeomBatch>,
}
impl<'b, 'a: 'b, 'c> ButtonBuilder<'a, 'c> {
pub fn new() -> Self {
ButtonBuilder {
padding: EdgeInsets {
top: 8.0,
bottom: 8.0,
left: 16.0,
right: 16.0,
},
stack_spacing: 10.0,
..Default::default()
}
}
pub fn padding<EI: Into<EdgeInsets>>(mut self, padding: EI) -> Self {
self.padding = padding.into();
self
}
pub fn padding_top(mut self, padding: f64) -> Self {
self.padding.top = padding;
self
}
pub fn padding_left(mut self, padding: f64) -> Self {
self.padding.left = padding;
self
}
pub fn padding_bottom(mut self, padding: f64) -> Self {
self.padding.bottom = padding;
self
}
pub fn padding_right(mut self, padding: f64) -> Self {
self.padding.right = padding;
self
}
pub fn label_text<I: Into<String>>(mut self, text: I) -> Self {
let mut label = self.default_style.label.take().unwrap_or_default();
label.text = Some(text.into());
self.default_style.label = Some(label);
self
}
pub fn label_underlined_text(mut self, text: &'a str) -> Self {
let mut label = self.default_style.label.take().unwrap_or_default();
label.text = Some(text.to_string());
label.styled_text = Some(Text::from(Line(text).underlined()));
self.default_style.label = Some(label);
self
}
pub fn label_styled_text(mut self, styled_text: Text, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut label = state_style.label.take().unwrap_or_default();
label.styled_text = Some(styled_text);
label.text = None;
state_style.label = Some(label);
self
}
pub fn label_color(mut self, color: Color, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut label = state_style.label.take().unwrap_or_default();
label.color = Some(color);
state_style.label = Some(label);
self
}
pub fn font(mut self, font: Font) -> Self {
let mut label = self.default_style.label.take().unwrap_or_default();
label.font = Some(font);
self.default_style.label = Some(label);
self
}
pub fn font_size(mut self, font_size: usize) -> Self {
let mut label = self.default_style.label.take().unwrap_or_default();
label.font_size = Some(font_size);
self.default_style.label = Some(label);
self
}
pub fn image_path(mut self, path: &'a str) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.source_path(path);
self.default_style.image = Some(image);
self
}
pub fn image_bytes(mut self, labeled_bytes: (&'a str, &'a [u8])) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.source_bytes(labeled_bytes);
self.default_style.image = Some(image);
self
}
pub fn image_batch(mut self, batch: GeomBatch, bounds: geom::Bounds) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.source_batch(batch, bounds);
self.default_style.image = Some(image);
self
}
pub fn image_color<C: Into<RewriteColor>>(mut self, color: C, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut image = state_style.image.take().unwrap_or_default();
image = image.color(color);
state_style.image = Some(image);
self
}
pub fn image_bg_color(mut self, color: Color, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut image = state_style.image.take().unwrap_or_default();
image = image.bg_color(color);
state_style.image = Some(image);
self
}
pub fn image_dims<D: Into<ScreenDims>>(mut self, dims: D) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.dims(dims);
self.default_style.image = Some(image);
self
}
pub fn image_content_mode(mut self, content_mode: ContentMode) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.content_mode(content_mode);
self.default_style.image = Some(image);
self
}
pub fn image_corner_rounding<R: Into<CornerRounding>>(mut self, value: R) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.corner_rounding(value);
self.default_style.image = Some(image);
self
}
pub fn image_padding<EI: Into<EdgeInsets>>(mut self, value: EI) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.padding(value);
self.default_style.image = Some(image);
self
}
pub fn bg_color(mut self, color: Color, for_state: ControlState) -> Self {
self.style_mut(for_state).bg_color = Some(color);
self
}
pub fn outline(mut self, outline: OutlineStyle, for_state: ControlState) -> Self {
self.style_mut(for_state).outline = Some(outline);
self
}
pub fn custom_batch(mut self, batch: GeomBatch, for_state: ControlState) -> Self {
self.style_mut(for_state).custom_batch = Some(batch);
self
}
pub fn hotkey<MK: Into<Option<MultiKey>>>(mut self, key: MK) -> Self {
self.hotkey = key.into();
self
}
pub fn tooltip(mut self, tooltip: impl Into<Text>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
pub fn no_tooltip(mut self) -> Self {
self.tooltip = Some(Text::new());
self
}
pub fn vertical(mut self) -> Self {
self.stack_axis = Some(Axis::Vertical);
self
}
pub fn horizontal(mut self) -> Self {
self.stack_axis = Some(Axis::Horizontal);
self
}
pub fn disabled(mut self, is_disabled: bool) -> Self {
self.is_disabled = is_disabled;
self
}
pub fn label_first(mut self) -> Self {
self.is_label_before_image = true;
self
}
pub fn image_first(mut self) -> Self {
self.is_label_before_image = false;
self
}
pub fn stack_spacing(mut self, value: f64) -> Self {
self.stack_spacing = value;
self
}
pub fn corner_rounding<R: Into<CornerRounding>>(mut self, value: R) -> Self {
self.corner_rounding = Some(value.into());
self
}
pub fn build(&self, ctx: &EventCtx, action: &str) -> Button {
let normal = self.batch(ctx, ControlState::Default);
let hovered = self.batch(ctx, ControlState::Hovered);
let disabled = self.batch(ctx, ControlState::Disabled);
assert!(
normal.get_bounds() != geom::Bounds::zero(),
"button was empty"
);
let hitbox = normal.get_bounds().get_rectangle();
Button::new(
ctx,
normal,
hovered,
disabled,
self.hotkey.clone(),
action,
self.tooltip.clone(),
hitbox,
self.is_disabled,
)
}
pub fn build_widget<I: AsRef<str>>(&self, ctx: &EventCtx, action: I) -> Widget {
let action = action.as_ref();
Widget::new(Box::new(self.build(ctx, action))).named(action)
}
pub fn build_def(&self, ctx: &EventCtx) -> Widget {
let action = self
.default_style
.label
.as_ref()
.and_then(|label| label.text.as_ref())
.expect("Must set `label_text` before calling build_def");
self.build_widget(ctx, action)
}
fn style_mut(&'b mut self, state: ControlState) -> &'b mut ButtonStateStyle<'a, 'c> {
match state {
ControlState::Default => &mut self.default_style,
ControlState::Hovered => &mut self.hover_style,
ControlState::Disabled => &mut self.disable_style,
}
}
fn style(&'b self, state: ControlState) -> &'b ButtonStateStyle<'a, 'c> {
match state {
ControlState::Default => &self.default_style,
ControlState::Hovered => &self.hover_style,
ControlState::Disabled => &self.disable_style,
}
}
fn batch(&self, ctx: &EventCtx, for_state: ControlState) -> GeomBatch {
let state_style = self.style(for_state);
if let Some(custom_batch) = state_style.custom_batch.as_ref() {
return custom_batch.clone();
}
let default_style = &self.default_style;
if let Some(custom_batch) = default_style.custom_batch.as_ref() {
return custom_batch.clone();
}
let image_batch: Option<GeomBatch> = match (&state_style.image, &default_style.image) {
(Some(state_image), Some(default_image)) => default_image
.merged_image_style(state_image)
.build_batch(ctx),
(None, Some(default_image)) => default_image.build_batch(ctx),
(None, None) => None,
(Some(_), None) => {
debug_assert!(
false,
"unexpectedly found a per-state image with no default image"
);
None
}
}
.map(|b| b.0);
let label_batch = state_style
.label
.as_ref()
.or_else(|| default_style.label.as_ref())
.and_then(|label| {
let default = default_style.label.as_ref();
if let Some(styled_text) = label
.styled_text
.as_ref()
.or_else(|| default.and_then(|d| d.styled_text.as_ref()))
{
return Some(styled_text.clone().bg(Color::CLEAR).render(ctx));
}
let text = label
.text
.clone()
.or_else(|| default.and_then(|d| d.text.clone()))?;
let color = label
.color
.or_else(|| default.and_then(|d| d.color))
.unwrap_or_else(|| ctx.style().text_primary_color);
let mut line = Line(text).fg(color);
if let Some(font_size) = label
.font_size
.or_else(|| default.and_then(|d| d.font_size))
{
line = line.size(font_size);
}
if let Some(font) = label.font.or_else(|| default.and_then(|d| d.font)) {
line = line.font(font);
}
Some(
Text::from(line)
.bg(Color::CLEAR)
.render(ctx),
)
});
let mut items = vec![];
if let Some(image_batch) = image_batch {
items.push(image_batch);
}
if let Some(label_batch) = label_batch {
items.push(label_batch);
}
if self.is_label_before_image {
items.reverse()
}
let mut stack = GeomBatchStack::horizontal(items);
if let Some(stack_axis) = self.stack_axis {
stack.set_axis(stack_axis);
}
stack.set_spacing(self.stack_spacing);
let mut button_widget = stack
.batch()
.batch()
.container()
.padding(self.padding)
.bg(state_style
.bg_color
.or(default_style.bg_color)
.unwrap_or(Color::CLEAR));
if let Some(outline) = state_style.outline.or(default_style.outline) {
button_widget = button_widget.outline(outline);
}
if let Some(corner_rounding) = self.corner_rounding {
button_widget = button_widget.corner_rounding(corner_rounding);
}
let (geom_batch, _hitbox) = button_widget.into_geom(ctx, None);
geom_batch
}
}
#[derive(Clone, Debug, Default)]
struct Label {
text: Option<String>,
color: Option<Color>,
styled_text: Option<Text>,
font_size: Option<usize>,
font: Option<Font>,
}
pub struct MultiButton {
draw: Drawable,
hitboxes: Vec<(Polygon, String)>,
hovering: Option<usize>,
top_left: ScreenPt,
dims: ScreenDims,
}
impl MultiButton {
pub fn new_widget(
ctx: &EventCtx,
batch: GeomBatch,
hitboxes: Vec<(Polygon, String)>,
) -> Widget {
Widget::new(Box::new(MultiButton {
dims: batch.get_dims(),
top_left: ScreenPt::new(0.0, 0.0),
draw: ctx.upload(batch),
hitboxes,
hovering: None,
}))
}
}
impl WidgetImpl for MultiButton {
fn get_dims(&self) -> ScreenDims {
self.dims
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.top_left = top_left;
}
fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput) {
if ctx.redo_mouseover() {
self.hovering = None;
if let Some(cursor) = ctx.canvas.get_cursor_in_screen_space() {
if !ScreenRectangle::top_left(self.top_left, self.dims).contains(cursor) {
return;
}
let translated =
ScreenPt::new(cursor.x - self.top_left.x, cursor.y - self.top_left.y).to_pt();
for (idx, (region, _)) in self.hitboxes.iter().enumerate() {
if region.contains_pt(translated) {
self.hovering = Some(idx);
break;
}
}
}
}
if let Some(idx) = self.hovering {
if ctx.normal_left_click() {
self.hovering = None;
output.outcome = Outcome::Clicked(self.hitboxes[idx].1.clone());
}
}
}
fn draw(&self, g: &mut GfxCtx) {
g.redraw_at(self.top_left, &self.draw);
if let Some(idx) = self.hovering {
if let Ok(p) = self.hitboxes[idx].0.to_outline(Distance::meters(1.0)) {
let draw = g.upload(GeomBatch::from(vec![(Color::YELLOW, p)]));
g.redraw_at(self.top_left, &draw);
}
}
}
}