clean up style.rs

This commit is contained in:
Emil Ernerfeldt 2018-12-27 19:30:31 +01:00
parent 52eb5bdf2c
commit 4e03a607c6
2 changed files with 89 additions and 102 deletions

View File

@ -58,10 +58,12 @@ impl GuiSettings for App {
gui.label(format!("The button have been clicked {} times", self.count));
gui.slider_f32("width", &mut self.width, 0.0, 500.0);
gui.slider_f32("height", &mut self.height, 0.0, 500.0);
gui.slider_f32("corner_radius", &mut self.corner_radius, 0.0, 50.0);
gui.slider_f32("stroke_width", &mut self.stroke_width, 0.0, 10.0);
gui.foldable("Box rendering options", |gui| {
gui.slider_f32("width", &mut self.width, 0.0, 500.0);
gui.slider_f32("height", &mut self.height, 0.0, 500.0);
gui.slider_f32("corner_radius", &mut self.corner_radius, 0.0, 50.0);
gui.slider_f32("stroke_width", &mut self.stroke_width, 0.0, 10.0);
});
gui.commands
.push(GuiCmd::PaintCommands(vec![PaintCmd::Rect {

View File

@ -25,6 +25,55 @@ impl Default for Style {
}
}
impl Style {
/// e.g. the background of the slider
fn background_fill_color(&self) -> Color {
srgba(34, 34, 34, 255)
}
fn text_color(&self) -> Color {
srgba(255, 255, 255, 187)
}
/// Fill color of the interactive part of a component (button, slider grab, checkbox, ...)
fn interact_fill_color(&self, interact: &InteractInfo) -> Color {
if interact.active {
srgba(136, 136, 136, 255)
} else if interact.hovered {
srgba(100, 100, 100, 255)
} else {
srgba(68, 68, 68, 220)
}
}
/// Stroke and text color of the interactive part of a component (button, slider grab, checkbox, ...)
fn interact_stroke_color(&self, interact: &InteractInfo) -> Color {
if interact.active {
srgba(255, 255, 255, 255)
} else if interact.hovered {
srgba(255, 255, 255, 200)
} else {
srgba(255, 255, 255, 170)
}
}
/// Returns small icon rectangle, big icon rectangle, and the remaining rectangle
fn icon_rectangles(&self, rect: &Rect) -> (Rect, Rect, Rect) {
let box_side = 16.0;
let big_icon_rect = Rect::from_center_size(
vec2(rect.min().x + 4.0 + box_side * 0.5, rect.center().y),
vec2(box_side, box_side),
);
let small_icon_rect = Rect::from_center_size(big_icon_rect.center(), vec2(10.0, 10.0));
let rest_rect =
Rect::from_min_size(vec2(big_icon_rect.max().x + 4.0, rect.min().y), rect.size);
(small_icon_rect, big_icon_rect, rest_rect)
}
}
fn debug_rect(rect: Rect) -> PaintCmd {
PaintCmd::Rect {
corner_radius: 0.0,
@ -47,23 +96,16 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
rect,
text,
} => {
let rect_fill_color = if interact.active {
srgba(136, 136, 136, 255)
} else if interact.hovered {
srgba(100, 100, 100, 255)
} else {
srgba(68, 68, 68, 255)
};
out_commands.push(PaintCmd::Rect {
corner_radius: 5.0,
fill_color: Some(rect_fill_color),
fill_color: Some(style.interact_fill_color(&interact)),
outline: None,
pos: rect.pos,
size: rect.size,
});
// TODO: clip-rect of text
out_commands.push(PaintCmd::Text {
fill_color: srgba(255, 255, 255, 187),
fill_color: style.interact_stroke_color(&interact),
font_name: style.font_name.clone(),
font_size: style.font_size,
pos: Vec2 {
@ -84,42 +126,23 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
rect,
text,
} => {
let fill_color = if interact.active {
srgba(136, 136, 136, 255)
} else if interact.hovered {
srgba(100, 100, 100, 255)
} else {
srgba(68, 68, 68, 255)
};
let stroke_color = if interact.active {
srgba(255, 255, 255, 255)
} else if interact.hovered {
srgba(255, 255, 255, 200)
} else {
srgba(255, 255, 255, 170)
};
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),
);
let (small_icon_rect, big_icon_rect, rest_rect) = style.icon_rectangles(&rect);
out_commands.push(PaintCmd::Rect {
corner_radius: 3.0,
fill_color: Some(fill_color),
fill_color: Some(style.interact_fill_color(&interact)),
outline: None,
pos: box_rect.pos,
size: box_rect.size,
pos: big_icon_rect.pos,
size: big_icon_rect.size,
});
let stroke_color = style.interact_stroke_color(&interact);
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),
vec2(small_icon_rect.min().x, small_icon_rect.center().y),
vec2(small_icon_rect.center().x, small_icon_rect.max().y),
vec2(small_icon_rect.max().x, small_icon_rect.min().y),
],
color: stroke_color,
width: style.line_width,
@ -131,8 +154,8 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
font_name: style.font_name.clone(),
font_size: style.font_size,
pos: Vec2 {
x: box_rect.max().x + 4.0,
y: rect.center().y - 4.0,
x: rest_rect.min().x,
y: rest_rect.center().y - 4.0,
},
text,
text_align: TextAlign::Start,
@ -148,21 +171,8 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
open,
rect,
} => {
let fill_color = if interact.active {
srgba(136, 136, 136, 255)
} else if interact.hovered {
srgba(100, 100, 100, 255)
} else {
srgba(68, 68, 68, 255)
};
let stroke_color = if interact.active {
srgba(255, 255, 255, 255)
} else if interact.hovered {
srgba(255, 255, 255, 200)
} else {
srgba(255, 255, 255, 170)
};
let fill_color = style.interact_fill_color(&interact);
let stroke_color = style.interact_stroke_color(&interact);
out_commands.push(PaintCmd::Rect {
corner_radius: 3.0,
@ -174,16 +184,12 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
// TODO: paint a little triangle or arrow or something instead of this
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),
);
let (small_icon_rect, _, rest_rect) = style.icon_rectangles(&rect);
// Draw a minus:
out_commands.push(PaintCmd::Line {
points: vec![
vec2(box_rect.min().x, box_rect.center().y),
vec2(box_rect.max().x, box_rect.center().y),
vec2(small_icon_rect.min().x, small_icon_rect.center().y),
vec2(small_icon_rect.max().x, small_icon_rect.center().y),
],
color: stroke_color,
width: style.line_width,
@ -192,8 +198,8 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
// Draw it as a plus:
out_commands.push(PaintCmd::Line {
points: vec![
vec2(box_rect.center().x, box_rect.min().y),
vec2(box_rect.center().x, box_rect.max().y),
vec2(small_icon_rect.center().x, small_icon_rect.min().y),
vec2(small_icon_rect.center().x, small_icon_rect.max().y),
],
color: stroke_color,
width: style.line_width,
@ -205,7 +211,7 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
font_name: style.font_name.clone(),
font_size: style.font_size,
pos: Vec2 {
x: box_rect.max().x + 4.0,
x: rest_rect.min().x,
y: rect.center().y - style.font_size / 2.0,
},
text: label,
@ -218,37 +224,24 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
rect,
text,
} => {
let fill_color = if interact.active {
srgba(136, 136, 136, 255)
} else if interact.hovered {
srgba(100, 100, 100, 255)
} else {
srgba(68, 68, 68, 255)
};
let fill_color = style.interact_fill_color(&interact);
let stroke_color = style.interact_stroke_color(&interact);
let stroke_color = if interact.active {
srgba(255, 255, 255, 255)
} else if interact.hovered {
srgba(255, 255, 255, 200)
} else {
srgba(255, 255, 255, 170)
};
let (small_icon_rect, big_icon_rect, rest_rect) = style.icon_rectangles(&rect);
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,
center: big_icon_rect.center(),
fill_color: Some(fill_color),
outline: None,
radius: circle_radius,
radius: big_icon_rect.size.x / 2.0,
});
if checked {
out_commands.push(PaintCmd::Circle {
center: circle_center,
fill_color: Some(srgba(0, 0, 0, 255)),
center: small_icon_rect.center(),
fill_color: Some(stroke_color),
outline: None,
radius: circle_radius * 0.5,
radius: small_icon_rect.size.x / 2.0,
});
}
@ -257,7 +250,7 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
font_name: style.font_name.clone(),
font_size: style.font_size,
pos: Vec2 {
x: rect.min().x + 2.0 * circle_radius + 4.0,
x: rest_rect.min().x,
y: rect.center().y - 4.0,
},
text,
@ -288,17 +281,9 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
vec2(16.0, 16.0),
);
let marker_fill_color = if interact.active {
srgba(136, 136, 136, 255)
} else if interact.hovered {
srgba(100, 100, 100, 255)
} else {
srgba(68, 68, 68, 255)
};
out_commands.push(PaintCmd::Rect {
corner_radius: 2.0,
fill_color: Some(srgba(34, 34, 34, 255)),
fill_color: Some(style.background_fill_color()),
outline: None,
pos: thin_rect.pos,
size: thin_rect.size,
@ -306,14 +291,14 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
out_commands.push(PaintCmd::Rect {
corner_radius: 3.0,
fill_color: Some(marker_fill_color),
fill_color: Some(style.interact_fill_color(&interact)),
outline: None,
pos: marker_rect.pos,
size: marker_rect.size,
});
out_commands.push(PaintCmd::Text {
fill_color: srgba(255, 255, 255, 187),
fill_color: style.interact_stroke_color(&interact),
font_name: style.font_name.clone(),
font_size: style.font_size,
pos: vec2(
@ -335,7 +320,7 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
style: text_style,
} => {
let fill_color = match text_style {
TextStyle::Label => srgba(255, 255, 255, 187),
TextStyle::Label => style.text_color(),
};
out_commands.push(PaintCmd::Text {
fill_color,