From e54f46035b34a0c9f79c458dc6d662ba4dbbc446 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 26 Dec 2018 22:26:15 +0100 Subject: [PATCH] Radio buttons --- src/app.rs | 21 +++++++++++++++++++++ src/gui.rs | 22 ++++++++++++++++++++++ src/style.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/types.rs | 12 ++++++++++++ 4 files changed, 106 insertions(+) diff --git a/src/app.rs b/src/app.rs index bbe7679d..c2975462 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,6 +3,7 @@ use crate::{gui::Gui, math::*, types::*}; pub struct App { checked: bool, count: i32, + selected_alternative: i32, width: f32, height: f32, @@ -14,6 +15,7 @@ impl Default for App { fn default() -> App { App { checked: false, + selected_alternative: 0, count: 0, width: 100.0, height: 50.0, @@ -27,6 +29,25 @@ impl App { pub fn show_gui(&mut self, gui: &mut Gui) { gui.checkbox("checkbox", &mut self.checked); + if gui + .radio("First alternative", self.selected_alternative == 0) + .clicked + { + self.selected_alternative = 0; + } + if gui + .radio("Second alternative", self.selected_alternative == 1) + .clicked + { + self.selected_alternative = 1; + } + if gui + .radio("Final alternative", self.selected_alternative == 2) + .clicked + { + self.selected_alternative = 2; + } + if gui.button("Click me").clicked { self.count += 1; } diff --git a/src/gui.rs b/src/gui.rs index a18a0e71..df38201b 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -82,6 +82,28 @@ impl Gui { self.cursor.y += 16.0; // Padding } + /// A radio button + pub fn radio>(&mut self, label: S, checked: bool) -> InteractInfo { + let label: String = label.into(); + let id = self.get_id(&label); + let rect = Rect { + pos: self.cursor, + size: Vec2 { x: 200.0, y: 24.0 }, // TODO: get from some settings + }; + + let interact = self.interactive_rect(id, &rect); + + self.commands.push(GuiCmd::RadioButton { + checked, + interact, + rect, + text: label, + }); + + self.cursor.y += rect.size.y + 16.0; + interact + } + pub fn slider_f32>( &mut self, label: S, diff --git a/src/style.rs b/src/style.rs index b8d7bfd7..f68cb630 100644 --- a/src/style.rs +++ b/src/style.rs @@ -94,6 +94,57 @@ fn translate_cmd(out_commands: &mut Vec, cmd: GuiCmd) { text_align: TextAlign::Start, }); } + GuiCmd::RadioButton { + checked, + interact, + rect, + text, + } => { + let fill_style = if interact.active { + "#888888ff".to_string() + } else if interact.hovered { + "#666666ff".to_string() + } else { + "#444444ff".to_string() + }; + + let stroke_style = if interact.active { + "#ffffffff".to_string() + } else if interact.hovered { + "#ffffffcc".to_string() + } else { + "#ffffffaa".to_string() + }; + + let circle_radius = 8.0; + let circle_center = vec2(rect.min().x + circle_radius, rect.center().y); + out_commands.push(PaintCmd::Circle { + center: circle_center, + fill_style: Some(fill_style), + outline: None, + radius: circle_radius, + }); + + if checked { + out_commands.push(PaintCmd::Circle { + center: circle_center, + fill_style: Some("#000000ff".to_string()), + outline: None, + radius: circle_radius * 0.5, + }); + } + + out_commands.push(PaintCmd::Text { + fill_style: stroke_style.clone(), + font: "14px Palatino".to_string(), + pos: Vec2 { + x: rect.min().x + 2.0 * circle_radius + 4.0, + y: rect.center().y + 14.0 / 2.0, + }, + text, + text_align: TextAlign::Start, + }); + } GuiCmd::Slider { interact, label, diff --git a/src/types.rs b/src/types.rs index 5e48f507..0a9e0161 100644 --- a/src/types.rs +++ b/src/types.rs @@ -109,6 +109,12 @@ pub enum GuiCmd { rect: Rect, text: String, }, + RadioButton { + checked: bool, + interact: InteractInfo, + rect: Rect, + text: String, + }, Slider { interact: InteractInfo, label: String, @@ -138,6 +144,12 @@ pub struct Outline { #[derive(Clone, Debug, Serialize)] // TODO: copy #[serde(rename_all = "snake_case", tag = "kind")] pub enum PaintCmd { + Circle { + center: Vec2, + fill_style: Option