Radio buttons
This commit is contained in:
parent
4bca549de1
commit
e54f46035b
4 changed files with 106 additions and 0 deletions
21
src/app.rs
21
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;
|
||||
}
|
||||
|
|
22
src/gui.rs
22
src/gui.rs
|
@ -82,6 +82,28 @@ impl Gui {
|
|||
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>>(
|
||||
&mut self,
|
||||
label: S,
|
||||
|
|
51
src/style.rs
51
src/style.rs
|
@ -94,6 +94,57 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, 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,
|
||||
|
|
12
src/types.rs
12
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<Style>,
|
||||
outline: Option<Outline>,
|
||||
radius: f32,
|
||||
},
|
||||
Clear {
|
||||
fill_style: Style,
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue