Use Rect in draw calls
This commit is contained in:
parent
87ec9f0f52
commit
4e78cfb3d4
204
src/draw.rs
204
src/draw.rs
|
@ -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 {
|
||||
panel: UiPanel,
|
||||
offset: Vec2,
|
||||
offset: Option<Vec2>,
|
||||
clip_rect: Option<Rect>,
|
||||
opacity: Option<f32>,
|
||||
}
|
||||
|
||||
|
@ -33,15 +112,18 @@ impl DrawContext {
|
|||
pub fn new(panel: UiPanel) -> Self {
|
||||
Self {
|
||||
panel,
|
||||
offset: Vec2::ZERO,
|
||||
offset: None,
|
||||
clip_rect: None,
|
||||
opacity: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_triangle(&self, v1: Vec2, v2: Vec2, v3: Vec2, mut color: Color) {
|
||||
let v1 = v1 + self.offset;
|
||||
let v2 = v2 + self.offset;
|
||||
let v3 = v3 + self.offset;
|
||||
pub fn draw_triangle(&self, mut v1: Vec2, mut v2: Vec2, mut v3: Vec2, mut color: Color) {
|
||||
if let Some(offset) = self.offset {
|
||||
v1 += offset;
|
||||
v2 += offset;
|
||||
v3 += offset;
|
||||
}
|
||||
|
||||
if let Some(opacity) = self.opacity.as_ref() {
|
||||
color.a *= opacity;
|
||||
|
@ -112,111 +194,99 @@ impl DrawContext {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn draw_rect(&self, xy: Vec2, size: Vec2, color: Color) {
|
||||
let v1 = xy;
|
||||
let v2 = v1 + Vec2::new(size.x, 0.0);
|
||||
let v3 = v1 + Vec2::new(0.0, size.y);
|
||||
let v4 = v1 + size;
|
||||
pub fn draw_rect(&self, rect: Rect, color: Color) {
|
||||
let v1 = rect.bl;
|
||||
let v2 = Vec2::new(rect.bl.x, rect.tr.y);
|
||||
let v3 = Vec2::new(rect.tr.x, rect.bl.y);
|
||||
let v4 = rect.tr;
|
||||
|
||||
self.draw_triangle(v1, v2, v3, color);
|
||||
self.draw_triangle(v2, v3, v4, color);
|
||||
}
|
||||
|
||||
pub fn draw_rounded_rect(&self, xy: Vec2, size: Vec2, radius: f32, color: Color) {
|
||||
self.draw_partially_rounded_rect(CornerFlags::ALL, xy, size, radius, color);
|
||||
pub fn draw_rounded_rect(&self, rect: Rect, radius: f32, color: Color) {
|
||||
self.draw_partially_rounded_rect(CornerFlags::ALL, rect, radius, color);
|
||||
}
|
||||
|
||||
pub fn draw_partially_rounded_rect(
|
||||
&self,
|
||||
corners: CornerFlags,
|
||||
xy: Vec2,
|
||||
size: Vec2,
|
||||
rect: Rect,
|
||||
radius: f32,
|
||||
color: Color,
|
||||
) {
|
||||
if corners.is_empty() {
|
||||
self.draw_rect(xy, size, color);
|
||||
self.draw_rect(rect, color);
|
||||
return;
|
||||
}
|
||||
|
||||
let has_top = corners.intersects(CornerFlags::TOP);
|
||||
let has_bottom = corners.intersects(CornerFlags::BOTTOM);
|
||||
let edge_size = Vec2::new(size.x, radius);
|
||||
let mut inner_rect = rect;
|
||||
let inset = rect.inset(radius);
|
||||
|
||||
let xoff = Vec2::new(radius, 0.0);
|
||||
let yoff = Vec2::new(0.0, radius);
|
||||
let sizex = Vec2::new(size.x, 0.0);
|
||||
let sizey = Vec2::new(0.0, size.y);
|
||||
if corners.intersects(CornerFlags::BOTTOM) {
|
||||
inner_rect.bl.y += radius;
|
||||
|
||||
let tr = xy + size - xoff - yoff;
|
||||
let br = xy + sizex - xoff + yoff;
|
||||
let bl = xy + xoff + yoff;
|
||||
let tl = xy + sizey + xoff - yoff;
|
||||
|
||||
if has_bottom {
|
||||
let mut bottom_xy = xy;
|
||||
let mut bottom_size = edge_size;
|
||||
let mut bottom_edge = Rect {
|
||||
bl: rect.bl,
|
||||
tr: Vec2::new(rect.tr.x, rect.bl.y + radius),
|
||||
};
|
||||
|
||||
if corners.contains(CornerFlags::BOTTOM_LEFT) {
|
||||
bottom_xy.x += radius;
|
||||
bottom_size.x -= radius;
|
||||
self.draw_quarter_circle(Corner::BottomLeft, bl, radius, color);
|
||||
bottom_edge.bl.x += radius;
|
||||
self.draw_quarter_circle(Corner::BottomLeft, inset.bl, radius, color);
|
||||
}
|
||||
|
||||
if corners.contains(CornerFlags::BOTTOM_RIGHT) {
|
||||
bottom_size.x -= radius;
|
||||
self.draw_quarter_circle(Corner::BottomRight, br, radius, color);
|
||||
bottom_edge.tr.x -= radius;
|
||||
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 {
|
||||
let mut top_xy = Vec2::new(xy.x, xy.y + size.y - radius);
|
||||
let mut top_size = edge_size;
|
||||
if corners.intersects(CornerFlags::TOP) {
|
||||
inner_rect.tr.y -= radius;
|
||||
|
||||
let mut top_edge = Rect {
|
||||
bl: Vec2::new(rect.bl.x, rect.tr.y - radius),
|
||||
tr: rect.tr,
|
||||
};
|
||||
|
||||
if corners.contains(CornerFlags::TOP_LEFT) {
|
||||
top_xy.x += radius;
|
||||
top_size.x -= radius;
|
||||
self.draw_quarter_circle(Corner::TopLeft, tl, radius, color);
|
||||
top_edge.bl.x += radius;
|
||||
self.draw_quarter_circle(Corner::TopLeft, inset.tl(), radius, color);
|
||||
}
|
||||
|
||||
if corners.contains(CornerFlags::TOP_RIGHT) {
|
||||
top_size.x -= radius;
|
||||
self.draw_quarter_circle(Corner::TopRight, tr, radius, color);
|
||||
top_edge.tr.x -= radius;
|
||||
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;
|
||||
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);
|
||||
self.draw_rect(inner_rect, 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 {
|
||||
offset: self.offset + offset,
|
||||
offset: Some(offset),
|
||||
..*self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_opacity(&self, opacity: f32) -> Self {
|
||||
let opacity = if let Some(old) = self.opacity.as_ref() {
|
||||
opacity * old
|
||||
} else {
|
||||
opacity
|
||||
};
|
||||
pub fn with_opacity(&self, mut opacity: f32) -> Self {
|
||||
if let Some(old) = self.opacity {
|
||||
opacity *= old;
|
||||
}
|
||||
|
||||
Self {
|
||||
opacity: Some(opacity),
|
||||
|
|
103
src/widgets.rs
103
src/widgets.rs
|
@ -1,5 +1,5 @@
|
|||
use crate::anim::Animation;
|
||||
use crate::draw::{Corner, CornerFlags, DrawContext};
|
||||
use crate::draw::{Corner, CornerFlags, DrawContext, Rect};
|
||||
use crate::{Color, CursorEventKind, Vec2};
|
||||
use keyframe::functions::*;
|
||||
|
||||
|
@ -91,8 +91,7 @@ impl Widget for RoundButton {
|
|||
}
|
||||
|
||||
pub struct RectButton {
|
||||
pub pos: Vec2,
|
||||
pub size: Vec2,
|
||||
pub rect: Rect,
|
||||
pub rounded_corners: CornerFlags,
|
||||
pub radius: f32,
|
||||
pub was_clicked: bool,
|
||||
|
@ -112,10 +111,9 @@ impl RectButton {
|
|||
pub const HOVER_COLOR: Color = Color::new(1., 1., 1., 0.8);
|
||||
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 {
|
||||
pos,
|
||||
size,
|
||||
rect,
|
||||
rounded_corners,
|
||||
radius,
|
||||
was_clicked: false,
|
||||
|
@ -140,16 +138,14 @@ impl Widget for RectButton {
|
|||
fn draw(&mut self, ctx: &DrawContext) {
|
||||
ctx.draw_partially_rounded_rect(
|
||||
self.rounded_corners,
|
||||
self.pos,
|
||||
self.size,
|
||||
self.rect,
|
||||
self.radius,
|
||||
self.color_anim.get(),
|
||||
);
|
||||
}
|
||||
|
||||
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
|
||||
let local = at - self.pos;
|
||||
let is_on = local.x > 0. && local.y > 0. && local.x < self.size.x && local.y < self.size.y;
|
||||
let is_on = self.rect.contains_point(at);
|
||||
|
||||
match kind {
|
||||
CursorEventKind::Hover | CursorEventKind::Drag => {
|
||||
|
@ -220,15 +216,23 @@ pub struct ScrollBar {
|
|||
style: ScrollBarStyle,
|
||||
scroll: f32,
|
||||
content_height: f32,
|
||||
rail_rect: Rect,
|
||||
}
|
||||
|
||||
impl ScrollBar {
|
||||
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 {
|
||||
height,
|
||||
style,
|
||||
scroll: height,
|
||||
content_height,
|
||||
rail_rect,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -238,16 +242,15 @@ impl Widget for ScrollBar {
|
|||
|
||||
fn draw(&mut self, ctx: &DrawContext) {
|
||||
let style = &self.style;
|
||||
let center_x = style.body_width / 2.0 + style.margin.x;
|
||||
let rail_xy = Vec2::new(center_x - style.rail_width / 2.0, self.style.margin.y);
|
||||
let rail_size = Vec2::new(style.rail_width, self.height - style.margin.y * 2.0);
|
||||
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 rail_height = self.rail_rect.height();
|
||||
let body_height = (self.height / self.content_height) * rail_height;
|
||||
let body_y = rail_height - (self.scroll / self.content_height) * rail_height - body_height;
|
||||
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_rect = Rect::from_xy_size(body_xy, body_size);
|
||||
|
||||
ctx.draw_rect(rail_xy, rail_size, style.rail_color);
|
||||
ctx.draw_rounded_rect(body_xy, body_size, style.body_radius, style.body_idle_color);
|
||||
ctx.draw_rect(self.rail_rect, style.rail_color);
|
||||
ctx.draw_rounded_rect(body_rect, style.body_radius, style.body_idle_color);
|
||||
}
|
||||
|
||||
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {}
|
||||
|
@ -609,6 +612,8 @@ impl Widget for MainMenu {
|
|||
pub struct TabMenu {
|
||||
tabs: Vec<RectButton>,
|
||||
scroll_bar: Offset<ScrollBar>,
|
||||
head_rect: Rect,
|
||||
separator_rect: Rect,
|
||||
}
|
||||
|
||||
impl TabMenu {
|
||||
|
@ -636,15 +641,38 @@ impl TabMenu {
|
|||
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 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));
|
||||
let tab_list_height = Self::TAB_NUM as f32 * Self::TAB_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,
|
||||
};
|
||||
|
||||
// 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
|
||||
ctx.draw_partially_rounded_rect(
|
||||
CornerFlags::BOTTOM_RIGHT,
|
||||
separator_xy,
|
||||
separator_size,
|
||||
self.separator_rect,
|
||||
Self::INNER_RADIUS,
|
||||
head_color,
|
||||
);
|
||||
|
||||
ctx.draw_rect(head_inner_xy, head_inner_size, head_color);
|
||||
ctx.draw_rect(head_edge_xy, head_edge_size, head_color);
|
||||
ctx.draw_quarter_circle(Corner::TopLeft, head_tl_xy, Self::HEAD_RADIUS, head_color);
|
||||
ctx.draw_quarter_circle(Corner::TopRight, head_tr_xy, Self::HEAD_RADIUS, head_color);
|
||||
ctx.draw_partially_rounded_rect(
|
||||
CornerFlags::TOP_LEFT | CornerFlags::TOP_RIGHT,
|
||||
self.head_rect,
|
||||
Self::HEAD_RADIUS,
|
||||
head_color,
|
||||
);
|
||||
|
||||
self.scroll_bar.draw(ctx);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue