egui/emigui/src/style.rs

108 lines
3.7 KiB
Rust
Raw Normal View History

2019-04-25 16:07:36 +00:00
use crate::{color::*, math::*, types::*};
2018-12-26 13:38:46 +00:00
#[derive(Clone, Copy, Debug, Serialize)]
2018-12-26 22:08:50 +00:00
pub struct Style {
/// Horizontal and vertical padding within a window frame.
pub window_padding: Vec2,
2018-12-27 17:19:06 +00:00
/// Button size is text size plus this on each side
pub button_padding: Vec2,
/// Horizontal and vertical spacing between widgets
pub item_spacing: Vec2,
/// Indent foldable regions etc by this much.
pub indent: f32,
/// Anything clickable is (at least) this wide.
pub clickable_diameter: f32,
/// Checkboxes, radio button and foldables have an icon at the start.
/// The text starts after this many pixels.
pub start_icon_width: f32,
// -----------------------------------------------
// Purely visual:
2018-12-27 17:19:06 +00:00
/// For stuff like check marks in check boxes.
2018-12-26 22:08:50 +00:00
pub line_width: f32,
}
impl Default for Style {
fn default() -> Self {
2018-12-27 17:19:06 +00:00
Style {
window_padding: vec2(6.0, 6.0),
button_padding: vec2(5.0, 3.0),
item_spacing: vec2(8.0, 4.0),
indent: 21.0,
clickable_diameter: 34.0,
start_icon_width: 20.0,
2018-12-27 17:19:06 +00:00
line_width: 2.0,
}
}
}
2018-12-27 18:30:31 +00:00
impl Style {
/// e.g. the background of the slider
2019-03-11 14:59:49 +00:00
pub fn background_fill_color(&self) -> Color {
gray(34, 230)
2018-12-27 18:30:31 +00:00
}
2019-03-11 14:59:49 +00:00
pub fn text_color(&self) -> Color {
2019-03-11 14:30:32 +00:00
gray(255, 200)
2018-12-27 18:30:31 +00:00
}
/// Fill color of the interactive part of a component (button, slider grab, checkbox, ...)
2019-03-11 14:59:49 +00:00
pub fn interact_fill_color(&self, interact: &InteractInfo) -> Color {
2018-12-27 18:30:31 +00:00
if interact.active {
2019-03-11 14:30:32 +00:00
srgba(100, 100, 200, 255)
2018-12-27 18:30:31 +00:00
} else if interact.hovered {
2019-03-11 14:30:32 +00:00
srgba(100, 100, 150, 255)
2018-12-27 18:30:31 +00:00
} else {
2019-03-11 14:30:32 +00:00
srgba(60, 60, 70, 255)
2018-12-27 18:30:31 +00:00
}
}
/// Stroke and text color of the interactive part of a component (button, slider grab, checkbox, ...)
2019-03-11 14:59:49 +00:00
pub fn interact_stroke_color(&self, interact: &InteractInfo) -> Color {
2018-12-27 18:30:31 +00:00
if interact.active {
2019-03-11 14:30:32 +00:00
gray(255, 255)
2018-12-27 18:30:31 +00:00
} else if interact.hovered {
2019-03-11 14:30:32 +00:00
gray(255, 200)
2018-12-27 18:30:31 +00:00
} else {
2019-03-11 14:30:32 +00:00
gray(255, 170)
2018-12-27 18:30:31 +00:00
}
}
2018-12-27 22:55:16 +00:00
/// Returns small icon rectangle and big icon rectangle
2019-03-11 14:59:49 +00:00
pub fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect) {
2018-12-27 18:30:31 +00:00
let box_side = 16.0;
let big_icon_rect = Rect::from_center_size(
pos2(rect.min().x + 4.0 + box_side * 0.5, rect.center().y),
2018-12-27 18:30:31 +00:00
vec2(box_side, box_side),
);
let small_icon_rect = Rect::from_center_size(big_icon_rect.center(), vec2(10.0, 10.0));
2018-12-27 22:55:16 +00:00
(small_icon_rect, big_icon_rect)
2018-12-27 18:30:31 +00:00
}
}
2020-04-12 10:07:51 +00:00
impl Style {
pub fn ui(&mut self, region: &mut crate::Region) {
use crate::widgets::{Button, Slider};
if region.add(Button::new("Reset style")).clicked {
*self = Default::default();
}
region.add(Slider::f32(&mut self.item_spacing.x, 0.0, 10.0).text("item_spacing.x"));
region.add(Slider::f32(&mut self.item_spacing.y, 0.0, 10.0).text("item_spacing.y"));
region.add(Slider::f32(&mut self.window_padding.x, 0.0, 10.0).text("window_padding.x"));
region.add(Slider::f32(&mut self.window_padding.y, 0.0, 10.0).text("window_padding.y"));
region.add(Slider::f32(&mut self.indent, 0.0, 100.0).text("indent"));
region.add(Slider::f32(&mut self.button_padding.x, 0.0, 20.0).text("button_padding.x"));
region.add(Slider::f32(&mut self.button_padding.y, 0.0, 20.0).text("button_padding.y"));
region.add(Slider::f32(&mut self.clickable_diameter, 0.0, 60.0).text("clickable_diameter"));
region.add(Slider::f32(&mut self.start_icon_width, 0.0, 60.0).text("start_icon_width"));
region.add(Slider::f32(&mut self.line_width, 0.0, 10.0).text("line_width"));
}
}