Radio buttons

This commit is contained in:
Emil Ernerfeldt 2018-12-26 22:26:15 +01:00
parent 4bca549de1
commit e54f46035b
4 changed files with 106 additions and 0 deletions

View file

@ -3,6 +3,7 @@ use crate::{gui::Gui, math::*, types::*};
pub struct App { pub struct App {
checked: bool, checked: bool,
count: i32, count: i32,
selected_alternative: i32,
width: f32, width: f32,
height: f32, height: f32,
@ -14,6 +15,7 @@ impl Default for App {
fn default() -> App { fn default() -> App {
App { App {
checked: false, checked: false,
selected_alternative: 0,
count: 0, count: 0,
width: 100.0, width: 100.0,
height: 50.0, height: 50.0,
@ -27,6 +29,25 @@ impl App {
pub fn show_gui(&mut self, gui: &mut Gui) { pub fn show_gui(&mut self, gui: &mut Gui) {
gui.checkbox("checkbox", &mut self.checked); 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 { if gui.button("Click me").clicked {
self.count += 1; self.count += 1;
} }

View file

@ -82,6 +82,28 @@ impl Gui {
self.cursor.y += 16.0; // Padding self.cursor.y += 16.0; // Padding
} }
/// A radio button
pub fn radio<S: Into<String>>(&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<S: Into<String>>( pub fn slider_f32<S: Into<String>>(
&mut self, &mut self,
label: S, label: S,

View file

@ -94,6 +94,57 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, cmd: GuiCmd) {
text_align: TextAlign::Start, 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 { GuiCmd::Slider {
interact, interact,
label, label,

View file

@ -109,6 +109,12 @@ pub enum GuiCmd {
rect: Rect, rect: Rect,
text: String, text: String,
}, },
RadioButton {
checked: bool,
interact: InteractInfo,
rect: Rect,
text: String,
},
Slider { Slider {
interact: InteractInfo, interact: InteractInfo,
label: String, label: String,
@ -138,6 +144,12 @@ pub struct Outline {
#[derive(Clone, Debug, Serialize)] // TODO: copy #[derive(Clone, Debug, Serialize)] // TODO: copy
#[serde(rename_all = "snake_case", tag = "kind")] #[serde(rename_all = "snake_case", tag = "kind")]
pub enum PaintCmd { pub enum PaintCmd {
Circle {
center: Vec2,
fill_style: Option<Style>,
outline: Option<Outline>,
radius: f32,
},
Clear { Clear {
fill_style: Style, fill_style: Style,
}, },