egui/src/style.rs

242 lines
7.6 KiB
Rust
Raw Normal View History

2018-12-26 16:01:46 +00:00
use crate::{math::*, types::*};
2018-12-26 13:38:46 +00:00
2018-12-26 22:08:50 +00:00
#[derive(Clone, Copy, Debug)]
pub struct Style {
/// For stuff like checkmarks in check boxes
pub line_width: f32,
}
impl Default for Style {
fn default() -> Style {
Style { line_width: 2.0 }
}
}
2018-12-26 13:38:46 +00:00
/// TODO: a Style struct which defines colors etc
2018-12-26 22:08:50 +00:00
fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
2018-12-26 13:38:46 +00:00
match cmd {
2018-12-26 16:32:58 +00:00
GuiCmd::PaintCommands(mut commands) => out_commands.append(&mut commands),
2018-12-26 21:17:33 +00:00
GuiCmd::Button {
interact,
2018-12-26 13:38:46 +00:00
rect,
2018-12-26 21:17:33 +00:00
text,
} => {
2018-12-27 16:47:32 +00:00
let rect_fill_color = if interact.active {
srgba(136, 136, 136, 255)
2018-12-26 21:17:33 +00:00
} else if interact.hovered {
2018-12-27 16:47:32 +00:00
srgba(100, 100, 100, 255)
2018-12-26 21:17:33 +00:00
} else {
2018-12-27 16:47:32 +00:00
srgba(68, 68, 68, 255)
2018-12-26 21:17:33 +00:00
};
out_commands.push(PaintCmd::Rect {
corner_radius: 5.0,
2018-12-27 16:47:32 +00:00
fill_color: Some(rect_fill_color),
2018-12-26 21:17:33 +00:00
outline: None,
pos: rect.pos,
size: rect.size,
});
// TODO: clip-rect of text
out_commands.push(PaintCmd::Text {
2018-12-27 16:47:32 +00:00
fill_color: srgba(255, 255, 255, 187),
2018-12-26 21:17:33 +00:00
font: "14px Palatino".to_string(),
pos: Vec2 {
x: rect.center().x,
2018-12-26 22:08:50 +00:00
y: rect.center().y + 6.0,
2018-12-26 21:17:33 +00:00
},
text,
text_align: TextAlign::Center,
});
}
GuiCmd::Checkbox {
checked,
2018-12-26 13:38:46 +00:00
interact,
2018-12-26 21:17:33 +00:00
rect,
text,
} => {
2018-12-27 16:47:32 +00:00
let fill_color = if interact.active {
srgba(136, 136, 136, 255)
2018-12-26 21:17:33 +00:00
} else if interact.hovered {
2018-12-27 16:47:32 +00:00
srgba(100, 100, 100, 255)
2018-12-26 21:17:33 +00:00
} else {
2018-12-27 16:47:32 +00:00
srgba(68, 68, 68, 255)
2018-12-26 21:17:33 +00:00
};
2018-12-27 16:47:32 +00:00
let stroke_color = if interact.active {
srgba(255, 255, 255, 255)
2018-12-26 21:17:33 +00:00
} else if interact.hovered {
2018-12-27 16:47:32 +00:00
srgba(255, 255, 255, 200)
2018-12-26 21:17:33 +00:00
} else {
2018-12-27 16:47:32 +00:00
srgba(255, 255, 255, 170)
2018-12-26 21:17:33 +00:00
};
let box_side = 16.0;
let box_rect = Rect::from_center_size(
vec2(rect.min().x + box_side * 0.5, rect.center().y),
vec2(box_side, box_side),
);
out_commands.push(PaintCmd::Rect {
corner_radius: 3.0,
2018-12-27 16:47:32 +00:00
fill_color: Some(fill_color),
2018-12-26 21:17:33 +00:00
outline: None,
pos: box_rect.pos,
size: box_rect.size,
});
if checked {
let smaller_rect = Rect::from_center_size(box_rect.center(), vec2(10.0, 10.0));
out_commands.push(PaintCmd::Line {
points: vec![
vec2(smaller_rect.min().x, smaller_rect.center().y),
vec2(smaller_rect.center().x, smaller_rect.max().y),
vec2(smaller_rect.max().x, smaller_rect.min().y),
],
2018-12-27 16:47:32 +00:00
color: stroke_color,
2018-12-26 22:08:50 +00:00
width: style.line_width,
2018-12-26 16:01:46 +00:00
});
2018-12-26 13:38:46 +00:00
}
2018-12-26 21:17:33 +00:00
out_commands.push(PaintCmd::Text {
2018-12-27 16:47:32 +00:00
fill_color: stroke_color,
2018-12-26 21:17:33 +00:00
font: "14px Palatino".to_string(),
pos: Vec2 {
x: box_rect.max().x + 4.0,
2018-12-26 22:08:50 +00:00
y: rect.center().y + 5.0,
2018-12-26 21:17:33 +00:00
},
text,
text_align: TextAlign::Start,
});
}
2018-12-26 21:26:15 +00:00
GuiCmd::RadioButton {
checked,
interact,
rect,
text,
} => {
2018-12-27 16:47:32 +00:00
let fill_color = if interact.active {
srgba(136, 136, 136, 255)
2018-12-26 21:26:15 +00:00
} else if interact.hovered {
2018-12-27 16:47:32 +00:00
srgba(100, 100, 100, 255)
2018-12-26 21:26:15 +00:00
} else {
2018-12-27 16:47:32 +00:00
srgba(68, 68, 68, 255)
2018-12-26 21:26:15 +00:00
};
2018-12-27 16:47:32 +00:00
let stroke_color = if interact.active {
srgba(255, 255, 255, 255)
2018-12-26 21:26:15 +00:00
} else if interact.hovered {
2018-12-27 16:47:32 +00:00
srgba(255, 255, 255, 200)
2018-12-26 21:26:15 +00:00
} else {
2018-12-27 16:47:32 +00:00
srgba(255, 255, 255, 170)
2018-12-26 21:26:15 +00:00
};
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,
2018-12-27 16:47:32 +00:00
fill_color: Some(fill_color),
2018-12-26 21:26:15 +00:00
outline: None,
radius: circle_radius,
});
if checked {
out_commands.push(PaintCmd::Circle {
center: circle_center,
2018-12-27 16:47:32 +00:00
fill_color: Some(srgba(0, 0, 0, 255)),
2018-12-26 21:26:15 +00:00
outline: None,
radius: circle_radius * 0.5,
});
}
out_commands.push(PaintCmd::Text {
2018-12-27 16:47:32 +00:00
fill_color: stroke_color,
2018-12-26 21:26:15 +00:00
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,
});
}
2018-12-26 16:01:46 +00:00
GuiCmd::Slider {
interact,
label,
max,
min,
rect,
value,
} => {
2018-12-26 22:08:50 +00:00
let thin_rect = Rect::from_min_size(
vec2(rect.min().x, lerp(rect.min().y, rect.max().y, 2.0 / 3.0)),
vec2(rect.size.x, 8.0),
);
2018-12-26 16:01:46 +00:00
let marker_center_x = remap_clamp(value, min, max, rect.min().x, rect.max().x);
2018-12-26 22:08:50 +00:00
let marker_rect = Rect::from_center_size(
vec2(marker_center_x, thin_rect.center().y),
vec2(16.0, 16.0),
);
2018-12-26 16:01:46 +00:00
2018-12-27 16:47:32 +00:00
let marker_fill_color = if interact.active {
srgba(136, 136, 136, 255)
2018-12-26 16:01:46 +00:00
} else if interact.hovered {
2018-12-27 16:47:32 +00:00
srgba(100, 100, 100, 255)
2018-12-26 16:01:46 +00:00
} else {
2018-12-27 16:47:32 +00:00
srgba(68, 68, 68, 255)
2018-12-26 16:01:46 +00:00
};
2018-12-26 16:32:58 +00:00
out_commands.push(PaintCmd::Rect {
2018-12-26 16:01:46 +00:00
corner_radius: 2.0,
2018-12-27 16:47:32 +00:00
fill_color: Some(srgba(34, 34, 34, 255)),
2018-12-26 16:32:58 +00:00
outline: None,
2018-12-26 16:01:46 +00:00
pos: thin_rect.pos,
size: thin_rect.size,
});
2018-12-26 16:32:58 +00:00
out_commands.push(PaintCmd::Rect {
2018-12-26 16:01:46 +00:00
corner_radius: 3.0,
2018-12-27 16:47:32 +00:00
fill_color: Some(marker_fill_color),
2018-12-26 16:32:58 +00:00
outline: None,
2018-12-26 16:01:46 +00:00
pos: marker_rect.pos,
size: marker_rect.size,
});
out_commands.push(PaintCmd::Text {
2018-12-27 16:47:32 +00:00
fill_color: srgba(255, 255, 255, 187),
2018-12-26 16:01:46 +00:00
font: "14px Palatino".to_string(),
2018-12-26 22:08:50 +00:00
pos: vec2(
rect.min().x,
lerp(rect.min().y, rect.max().y, 1.0 / 3.0) + 6.0,
),
2018-12-26 16:01:46 +00:00
text: format!("{}: {:.3}", label, value),
2018-12-26 16:32:58 +00:00
text_align: TextAlign::Start,
});
}
GuiCmd::Text {
pos,
text,
text_align,
style,
} => {
2018-12-27 16:47:32 +00:00
let fill_color = match style {
TextStyle::Label => srgba(255, 255, 255, 187),
2018-12-26 16:32:58 +00:00
};
out_commands.push(PaintCmd::Text {
2018-12-27 16:47:32 +00:00
fill_color,
2018-12-26 16:32:58 +00:00
font: "14px Palatino".to_string(),
2018-12-26 22:08:50 +00:00
pos: pos + vec2(0.0, 7.0), // TODO: FIXME
2018-12-26 16:32:58 +00:00
text,
text_align,
2018-12-26 16:01:46 +00:00
});
2018-12-26 13:38:46 +00:00
}
}
}
2018-12-26 22:08:50 +00:00
pub fn into_paint_commands(gui_commands: &[GuiCmd], style: &Style) -> Vec<PaintCmd> {
2018-12-26 16:01:46 +00:00
let mut paint_commands = vec![];
for gui_cmd in gui_commands {
2018-12-26 22:08:50 +00:00
translate_cmd(&mut paint_commands, style, gui_cmd.clone())
2018-12-26 16:01:46 +00:00
}
paint_commands
2018-12-26 13:38:46 +00:00
}