Use Rect in draw calls

This commit is contained in:
mars 2022-07-12 23:08:58 -06:00
parent 87ec9f0f52
commit 4e78cfb3d4
2 changed files with 195 additions and 112 deletions

View File

@ -23,9 +23,88 @@ bitflags! {
} }
} }
#[derive(Copy, Clone)]
pub struct ColoredTriangle {
pub v1: Vec2,
pub v2: Vec2,
pub v3: Vec2,
pub color: Color,
}
#[derive(Copy, Clone)]
pub struct Rect {
pub bl: Vec2,
pub tr: Vec2,
}
impl Rect {
pub fn from_xy_size(xy: Vec2, size: Vec2) -> Self {
Self {
bl: xy,
tr: xy + size,
}
}
pub fn from_circle_bounds(center: Vec2, radius: f32) -> Self {
Self {
bl: center - radius,
tr: center + radius,
}
}
pub fn inset(&self, d: f32) -> Self {
Self {
bl: self.bl + d,
tr: self.tr - d,
}
}
pub fn tl(&self) -> Vec2 {
Vec2::new(self.bl.x, self.tr.y)
}
pub fn br(&self) -> Vec2 {
Vec2::new(self.tr.x, self.bl.y)
}
pub fn offset(&self, offset: Vec2) -> Self {
Self {
bl: self.bl + offset,
tr: self.tr + offset,
}
}
pub fn clip(&self, other: &Self) -> Self {
Self {
bl: self.bl.max(other.bl),
tr: self.tr.min(other.tr),
}
}
pub fn contains_rect(&self, other: &Self) -> bool {
self.bl.x < other.bl.x
&& self.bl.y < other.bl.y
&& self.tr.x > other.tr.x
&& self.tr.y > other.tr.y
}
pub fn contains_point(&self, xy: Vec2) -> bool {
self.bl.x < xy.x && self.bl.y < xy.y && self.tr.x > xy.x && self.tr.y > xy.y
}
pub fn width(&self) -> f32 {
self.tr.x - self.bl.x
}
pub fn height(&self) -> f32 {
self.tr.y - self.bl.y
}
}
pub struct DrawContext { pub struct DrawContext {
panel: UiPanel, panel: UiPanel,
offset: Vec2, offset: Option<Vec2>,
clip_rect: Option<Rect>,
opacity: Option<f32>, opacity: Option<f32>,
} }
@ -33,15 +112,18 @@ impl DrawContext {
pub fn new(panel: UiPanel) -> Self { pub fn new(panel: UiPanel) -> Self {
Self { Self {
panel, panel,
offset: Vec2::ZERO, offset: None,
clip_rect: None,
opacity: None, opacity: None,
} }
} }
pub fn draw_triangle(&self, v1: Vec2, v2: Vec2, v3: Vec2, mut color: Color) { pub fn draw_triangle(&self, mut v1: Vec2, mut v2: Vec2, mut v3: Vec2, mut color: Color) {
let v1 = v1 + self.offset; if let Some(offset) = self.offset {
let v2 = v2 + self.offset; v1 += offset;
let v3 = v3 + self.offset; v2 += offset;
v3 += offset;
}
if let Some(opacity) = self.opacity.as_ref() { if let Some(opacity) = self.opacity.as_ref() {
color.a *= opacity; color.a *= opacity;
@ -112,111 +194,99 @@ impl DrawContext {
} }
} }
pub fn draw_rect(&self, xy: Vec2, size: Vec2, color: Color) { pub fn draw_rect(&self, rect: Rect, color: Color) {
let v1 = xy; let v1 = rect.bl;
let v2 = v1 + Vec2::new(size.x, 0.0); let v2 = Vec2::new(rect.bl.x, rect.tr.y);
let v3 = v1 + Vec2::new(0.0, size.y); let v3 = Vec2::new(rect.tr.x, rect.bl.y);
let v4 = v1 + size; let v4 = rect.tr;
self.draw_triangle(v1, v2, v3, color); self.draw_triangle(v1, v2, v3, color);
self.draw_triangle(v2, v3, v4, color); self.draw_triangle(v2, v3, v4, color);
} }
pub fn draw_rounded_rect(&self, xy: Vec2, size: Vec2, radius: f32, color: Color) { pub fn draw_rounded_rect(&self, rect: Rect, radius: f32, color: Color) {
self.draw_partially_rounded_rect(CornerFlags::ALL, xy, size, radius, color); self.draw_partially_rounded_rect(CornerFlags::ALL, rect, radius, color);
} }
pub fn draw_partially_rounded_rect( pub fn draw_partially_rounded_rect(
&self, &self,
corners: CornerFlags, corners: CornerFlags,
xy: Vec2, rect: Rect,
size: Vec2,
radius: f32, radius: f32,
color: Color, color: Color,
) { ) {
if corners.is_empty() { if corners.is_empty() {
self.draw_rect(xy, size, color); self.draw_rect(rect, color);
return; return;
} }
let has_top = corners.intersects(CornerFlags::TOP); let mut inner_rect = rect;
let has_bottom = corners.intersects(CornerFlags::BOTTOM); let inset = rect.inset(radius);
let edge_size = Vec2::new(size.x, radius);
let xoff = Vec2::new(radius, 0.0); if corners.intersects(CornerFlags::BOTTOM) {
let yoff = Vec2::new(0.0, radius); inner_rect.bl.y += radius;
let sizex = Vec2::new(size.x, 0.0);
let sizey = Vec2::new(0.0, size.y);
let tr = xy + size - xoff - yoff; let mut bottom_edge = Rect {
let br = xy + sizex - xoff + yoff; bl: rect.bl,
let bl = xy + xoff + yoff; tr: Vec2::new(rect.tr.x, rect.bl.y + radius),
let tl = xy + sizey + xoff - yoff; };
if has_bottom {
let mut bottom_xy = xy;
let mut bottom_size = edge_size;
if corners.contains(CornerFlags::BOTTOM_LEFT) { if corners.contains(CornerFlags::BOTTOM_LEFT) {
bottom_xy.x += radius; bottom_edge.bl.x += radius;
bottom_size.x -= radius; self.draw_quarter_circle(Corner::BottomLeft, inset.bl, radius, color);
self.draw_quarter_circle(Corner::BottomLeft, bl, radius, color);
} }
if corners.contains(CornerFlags::BOTTOM_RIGHT) { if corners.contains(CornerFlags::BOTTOM_RIGHT) {
bottom_size.x -= radius; bottom_edge.tr.x -= radius;
self.draw_quarter_circle(Corner::BottomRight, br, radius, color); self.draw_quarter_circle(Corner::BottomRight, inset.br(), radius, color);
} }
self.draw_rect(bottom_xy, bottom_size, color); self.draw_rect(bottom_edge, color);
} }
if has_top { if corners.intersects(CornerFlags::TOP) {
let mut top_xy = Vec2::new(xy.x, xy.y + size.y - radius); inner_rect.tr.y -= radius;
let mut top_size = edge_size;
let mut top_edge = Rect {
bl: Vec2::new(rect.bl.x, rect.tr.y - radius),
tr: rect.tr,
};
if corners.contains(CornerFlags::TOP_LEFT) { if corners.contains(CornerFlags::TOP_LEFT) {
top_xy.x += radius; top_edge.bl.x += radius;
top_size.x -= radius; self.draw_quarter_circle(Corner::TopLeft, inset.tl(), radius, color);
self.draw_quarter_circle(Corner::TopLeft, tl, radius, color);
} }
if corners.contains(CornerFlags::TOP_RIGHT) { if corners.contains(CornerFlags::TOP_RIGHT) {
top_size.x -= radius; top_edge.tr.x -= radius;
self.draw_quarter_circle(Corner::TopRight, tr, radius, color); self.draw_quarter_circle(Corner::TopRight, inset.tr, radius, color);
} }
self.draw_rect(top_xy, top_size, color); self.draw_rect(top_edge, color);
} }
let mut inner_xy = xy; self.draw_rect(inner_rect, color);
let mut inner_size = size;
if has_bottom {
inner_xy.y += radius;
inner_size.y -= radius;
}
if has_top {
inner_size.y -= radius;
}
self.draw_rect(inner_xy, inner_size, color);
} }
pub fn with_offset(&self, offset: Vec2) -> Self { pub fn get_clip_rect(&self) -> &Option<Rect> {
&self.clip_rect
}
pub fn with_offset(&self, mut offset: Vec2) -> Self {
if let Some(old) = self.offset {
offset += old;
}
Self { Self {
offset: self.offset + offset, offset: Some(offset),
..*self ..*self
} }
} }
pub fn with_opacity(&self, opacity: f32) -> Self { pub fn with_opacity(&self, mut opacity: f32) -> Self {
let opacity = if let Some(old) = self.opacity.as_ref() { if let Some(old) = self.opacity {
opacity * old opacity *= old;
} else { }
opacity
};
Self { Self {
opacity: Some(opacity), opacity: Some(opacity),

View File

@ -1,5 +1,5 @@
use crate::anim::Animation; use crate::anim::Animation;
use crate::draw::{Corner, CornerFlags, DrawContext}; use crate::draw::{Corner, CornerFlags, DrawContext, Rect};
use crate::{Color, CursorEventKind, Vec2}; use crate::{Color, CursorEventKind, Vec2};
use keyframe::functions::*; use keyframe::functions::*;
@ -91,8 +91,7 @@ impl Widget for RoundButton {
} }
pub struct RectButton { pub struct RectButton {
pub pos: Vec2, pub rect: Rect,
pub size: Vec2,
pub rounded_corners: CornerFlags, pub rounded_corners: CornerFlags,
pub radius: f32, pub radius: f32,
pub was_clicked: bool, pub was_clicked: bool,
@ -112,10 +111,9 @@ impl RectButton {
pub const HOVER_COLOR: Color = Color::new(1., 1., 1., 0.8); pub const HOVER_COLOR: Color = Color::new(1., 1., 1., 0.8);
pub const SELECTED_COLOR: Color = Color::new(1., 1., 0., 1.); pub const SELECTED_COLOR: Color = Color::new(1., 1., 0., 1.);
pub fn new(pos: Vec2, size: Vec2, rounded_corners: CornerFlags, radius: f32) -> Self { pub fn new(rect: Rect, rounded_corners: CornerFlags, radius: f32) -> Self {
Self { Self {
pos, rect,
size,
rounded_corners, rounded_corners,
radius, radius,
was_clicked: false, was_clicked: false,
@ -140,16 +138,14 @@ impl Widget for RectButton {
fn draw(&mut self, ctx: &DrawContext) { fn draw(&mut self, ctx: &DrawContext) {
ctx.draw_partially_rounded_rect( ctx.draw_partially_rounded_rect(
self.rounded_corners, self.rounded_corners,
self.pos, self.rect,
self.size,
self.radius, self.radius,
self.color_anim.get(), self.color_anim.get(),
); );
} }
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) { fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
let local = at - self.pos; let is_on = self.rect.contains_point(at);
let is_on = local.x > 0. && local.y > 0. && local.x < self.size.x && local.y < self.size.y;
match kind { match kind {
CursorEventKind::Hover | CursorEventKind::Drag => { CursorEventKind::Hover | CursorEventKind::Drag => {
@ -220,15 +216,23 @@ pub struct ScrollBar {
style: ScrollBarStyle, style: ScrollBarStyle,
scroll: f32, scroll: f32,
content_height: f32, content_height: f32,
rail_rect: Rect,
} }
impl ScrollBar { impl ScrollBar {
pub fn new(height: f32, content_height: f32, style: ScrollBarStyle) -> Self { pub fn new(height: f32, content_height: f32, style: ScrollBarStyle) -> Self {
let center_x = style.body_width / 2.0 + style.margin.x;
let rail_rect = Rect {
bl: Vec2::new(center_x - style.rail_width / 2.0, style.margin.y),
tr: Vec2::new(center_x + style.rail_width / 2.0, height - style.margin.y),
};
Self { Self {
height, height,
style, style,
scroll: height, scroll: height,
content_height, content_height,
rail_rect,
} }
} }
} }
@ -238,16 +242,15 @@ impl Widget for ScrollBar {
fn draw(&mut self, ctx: &DrawContext) { fn draw(&mut self, ctx: &DrawContext) {
let style = &self.style; let style = &self.style;
let center_x = style.body_width / 2.0 + style.margin.x; let rail_height = self.rail_rect.height();
let rail_xy = Vec2::new(center_x - style.rail_width / 2.0, self.style.margin.y); let body_height = (self.height / self.content_height) * rail_height;
let rail_size = Vec2::new(style.rail_width, self.height - style.margin.y * 2.0); let body_y = rail_height - (self.scroll / self.content_height) * rail_height - body_height;
let body_height = (self.height / self.content_height) * rail_size.y;
let body_y = rail_size.y - (self.scroll / self.content_height) * rail_size.y - body_height;
let body_xy = Vec2::new(style.margin.x, body_y + style.margin.y); let body_xy = Vec2::new(style.margin.x, body_y + style.margin.y);
let body_size = Vec2::new(style.body_width, body_height); let body_size = Vec2::new(style.body_width, body_height);
let body_rect = Rect::from_xy_size(body_xy, body_size);
ctx.draw_rect(rail_xy, rail_size, style.rail_color); ctx.draw_rect(self.rail_rect, style.rail_color);
ctx.draw_rounded_rect(body_xy, body_size, style.body_radius, style.body_idle_color); ctx.draw_rounded_rect(body_rect, style.body_radius, style.body_idle_color);
} }
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {} fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {}
@ -609,6 +612,8 @@ impl Widget for MainMenu {
pub struct TabMenu { pub struct TabMenu {
tabs: Vec<RectButton>, tabs: Vec<RectButton>,
scroll_bar: Offset<ScrollBar>, scroll_bar: Offset<ScrollBar>,
head_rect: Rect,
separator_rect: Rect,
} }
impl TabMenu { impl TabMenu {
@ -636,15 +641,38 @@ impl TabMenu {
CornerFlags::empty() CornerFlags::empty()
}; };
tabs.push(RectButton::new(pos, tab_size, corners, radius)); let rect = Rect::from_xy_size(pos, tab_size);
tabs.push(RectButton::new(rect, corners, radius));
} }
let scroll_height = Self::TAB_NUM as f32 * Self::TAB_HEIGHT; let tab_list_height = Self::TAB_NUM as f32 * Self::TAB_HEIGHT;
let scroll_bar = ScrollBar::new(scroll_height, scroll_height * 3.0, Default::default());
let scroll_x = Self::TAB_WIDTH + Self::SEPARATOR_WIDTH + Self::CONTENT_WIDTH;
let scroll_bar = Offset::new(scroll_bar, Vec2::new(scroll_x, -scroll_height));
Self { tabs, scroll_bar } let scroll_bar = ScrollBar::new(tab_list_height, tab_list_height * 3.0, Default::default());
let scroll_x = Self::TAB_WIDTH + Self::SEPARATOR_WIDTH + Self::CONTENT_WIDTH;
let scroll_bar = Offset::new(scroll_bar, Vec2::new(scroll_x, -tab_list_height));
let separator_rect = Rect {
bl: Vec2::new(Self::TAB_WIDTH, -tab_list_height),
tr: Vec2::new(Self::TAB_WIDTH + Self::SEPARATOR_WIDTH, 0.0),
};
let head_width = Self::TAB_WIDTH
+ Self::SEPARATOR_WIDTH
+ Self::CONTENT_WIDTH
+ scroll_bar.inner.style.body_width
+ scroll_bar.inner.style.margin.x * 2.0;
let head_rect = Rect {
bl: Vec2::ZERO,
tr: Vec2::new(head_width, Self::HEAD_HEIGHT),
};
Self {
tabs,
scroll_bar,
separator_rect,
head_rect,
}
} }
} }
@ -669,35 +697,20 @@ impl Widget for TabMenu {
a: 1.0, a: 1.0,
}; };
// alignment variables
let tab_list_height = Self::TAB_NUM as f32 * Self::TAB_HEIGHT;
let separator_xy = Vec2::new(Self::TAB_WIDTH, -tab_list_height);
let separator_size = Vec2::new(Self::SEPARATOR_WIDTH, tab_list_height);
let head_width = Self::TAB_WIDTH
+ Self::SEPARATOR_WIDTH
+ Self::CONTENT_WIDTH
+ self.scroll_bar.inner.style.body_width
+ self.scroll_bar.inner.style.margin.x * 2.0;
let head_inner_xy = Vec2::ZERO;
let head_inner_size = Vec2::new(head_width, Self::HEAD_HEIGHT - Self::HEAD_RADIUS);
let head_edge_xy = Vec2::new(Self::HEAD_RADIUS, Self::HEAD_HEIGHT - Self::HEAD_RADIUS);
let head_edge_size = Vec2::new(head_width - Self::HEAD_RADIUS * 2.0, Self::HEAD_RADIUS);
let head_tl_xy = head_edge_xy;
let head_tr_xy = head_tl_xy + Vec2::new(head_edge_size.x, 0.0);
// draw shapes // draw shapes
ctx.draw_partially_rounded_rect( ctx.draw_partially_rounded_rect(
CornerFlags::BOTTOM_RIGHT, CornerFlags::BOTTOM_RIGHT,
separator_xy, self.separator_rect,
separator_size,
Self::INNER_RADIUS, Self::INNER_RADIUS,
head_color, head_color,
); );
ctx.draw_rect(head_inner_xy, head_inner_size, head_color); ctx.draw_partially_rounded_rect(
ctx.draw_rect(head_edge_xy, head_edge_size, head_color); CornerFlags::TOP_LEFT | CornerFlags::TOP_RIGHT,
ctx.draw_quarter_circle(Corner::TopLeft, head_tl_xy, Self::HEAD_RADIUS, head_color); self.head_rect,
ctx.draw_quarter_circle(Corner::TopRight, head_tr_xy, Self::HEAD_RADIUS, head_color); Self::HEAD_RADIUS,
head_color,
);
self.scroll_bar.draw(ctx); self.scroll_bar.draw(ctx);
} }