|
| 1 | +use bevy_color::Color; |
| 2 | +use bevy_gizmos::{config::GizmoConfigGroup, prelude::Gizmos}; |
| 3 | +use bevy_math::{Vec2, Vec2Swizzles}; |
| 4 | +use bevy_reflect::Reflect; |
| 5 | +use bevy_transform::prelude::GlobalTransform; |
| 6 | +use bevy_utils::HashMap; |
| 7 | + |
| 8 | +use super::{CameraQuery, LayoutRect}; |
| 9 | + |
| 10 | +// Function used here so we don't need to redraw lines that are fairly close to each other. |
| 11 | +fn approx_eq(compared: f32, other: f32) -> bool { |
| 12 | + (compared - other).abs() < 0.001 |
| 13 | +} |
| 14 | + |
| 15 | +fn rect_border_axis(rect: LayoutRect) -> (f32, f32, f32, f32) { |
| 16 | + let pos = rect.pos; |
| 17 | + let size = rect.size; |
| 18 | + let offset = pos + size; |
| 19 | + (pos.x, offset.x, pos.y, offset.y) |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)] |
| 23 | +enum Dir { |
| 24 | + Start, |
| 25 | + End, |
| 26 | +} |
| 27 | +impl Dir { |
| 28 | + const fn increments(self) -> i64 { |
| 29 | + match self { |
| 30 | + Dir::Start => 1, |
| 31 | + Dir::End => -1, |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +impl From<i64> for Dir { |
| 36 | + fn from(value: i64) -> Self { |
| 37 | + if value.is_positive() { |
| 38 | + Dir::Start |
| 39 | + } else { |
| 40 | + Dir::End |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +/// Collection of axis aligned "lines" (actually just their coordinate on |
| 45 | +/// a given axis). |
| 46 | +#[derive(Debug, Clone)] |
| 47 | +struct DrawnLines { |
| 48 | + lines: HashMap<i64, Dir>, |
| 49 | + width: f32, |
| 50 | +} |
| 51 | +#[allow(clippy::cast_precision_loss, clippy::cast_possible_truncation)] |
| 52 | +impl DrawnLines { |
| 53 | + fn new(width: f32) -> Self { |
| 54 | + DrawnLines { |
| 55 | + lines: HashMap::new(), |
| 56 | + width, |
| 57 | + } |
| 58 | + } |
| 59 | + /// Return `value` offset by as many `increment`s as necessary to make it |
| 60 | + /// not overlap with already drawn lines. |
| 61 | + fn inset(&self, value: f32) -> f32 { |
| 62 | + let scaled = value / self.width; |
| 63 | + let fract = scaled.fract(); |
| 64 | + let mut on_grid = scaled.floor() as i64; |
| 65 | + for _ in 0..10 { |
| 66 | + let Some(dir) = self.lines.get(&on_grid) else { |
| 67 | + break; |
| 68 | + }; |
| 69 | + // TODO(clean): This fixes a panic, but I'm not sure how valid this is |
| 70 | + let Some(added) = on_grid.checked_add(dir.increments()) else { |
| 71 | + break; |
| 72 | + }; |
| 73 | + on_grid = added; |
| 74 | + } |
| 75 | + ((on_grid as f32) + fract) * self.width |
| 76 | + } |
| 77 | + /// Remove a line from the collection of drawn lines. |
| 78 | + /// |
| 79 | + /// Typically, we only care for pre-existing lines when drawing the children |
| 80 | + /// of a container, nothing more. So we remove it after we are done with |
| 81 | + /// the children. |
| 82 | + fn remove(&mut self, value: f32, increment: i64) { |
| 83 | + let mut on_grid = (value / self.width).floor() as i64; |
| 84 | + loop { |
| 85 | + // TODO(clean): This fixes a panic, but I'm not sure how valid this is |
| 86 | + let Some(next_cell) = on_grid.checked_add(increment) else { |
| 87 | + return; |
| 88 | + }; |
| 89 | + if !self.lines.contains_key(&next_cell) { |
| 90 | + self.lines.remove(&on_grid); |
| 91 | + return; |
| 92 | + } |
| 93 | + on_grid = next_cell; |
| 94 | + } |
| 95 | + } |
| 96 | + /// Add a line from the collection of drawn lines. |
| 97 | + fn add(&mut self, value: f32, increment: i64) { |
| 98 | + let mut on_grid = (value / self.width).floor() as i64; |
| 99 | + loop { |
| 100 | + let old_value = self.lines.insert(on_grid, increment.into()); |
| 101 | + if old_value.is_none() { |
| 102 | + return; |
| 103 | + } |
| 104 | + // TODO(clean): This fixes a panic, but I'm not sure how valid this is |
| 105 | + let Some(added) = on_grid.checked_add(increment) else { |
| 106 | + return; |
| 107 | + }; |
| 108 | + on_grid = added; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[derive(GizmoConfigGroup, Reflect, Default)] |
| 114 | +pub struct UiGizmosDebug; |
| 115 | + |
| 116 | +pub(super) struct InsetGizmo<'w, 's> { |
| 117 | + draw: Gizmos<'w, 's, UiGizmosDebug>, |
| 118 | + cam: CameraQuery<'w, 's>, |
| 119 | + known_y: DrawnLines, |
| 120 | + known_x: DrawnLines, |
| 121 | +} |
| 122 | +impl<'w, 's> InsetGizmo<'w, 's> { |
| 123 | + pub(super) fn new( |
| 124 | + draw: Gizmos<'w, 's, UiGizmosDebug>, |
| 125 | + cam: CameraQuery<'w, 's>, |
| 126 | + line_width: f32, |
| 127 | + ) -> Self { |
| 128 | + InsetGizmo { |
| 129 | + draw, |
| 130 | + cam, |
| 131 | + known_y: DrawnLines::new(line_width), |
| 132 | + known_x: DrawnLines::new(line_width), |
| 133 | + } |
| 134 | + } |
| 135 | + fn relative(&self, mut position: Vec2) -> Vec2 { |
| 136 | + let zero = GlobalTransform::IDENTITY; |
| 137 | + let Ok(cam) = self.cam.get_single() else { |
| 138 | + return Vec2::ZERO; |
| 139 | + }; |
| 140 | + if let Some(new_position) = cam.world_to_viewport(&zero, position.extend(0.)) { |
| 141 | + position = new_position; |
| 142 | + }; |
| 143 | + position.xy() |
| 144 | + } |
| 145 | + fn line_2d(&mut self, mut start: Vec2, mut end: Vec2, color: Color) { |
| 146 | + if approx_eq(start.x, end.x) { |
| 147 | + start.x = self.known_x.inset(start.x); |
| 148 | + end.x = start.x; |
| 149 | + } else if approx_eq(start.y, end.y) { |
| 150 | + start.y = self.known_y.inset(start.y); |
| 151 | + end.y = start.y; |
| 152 | + } |
| 153 | + let (start, end) = (self.relative(start), self.relative(end)); |
| 154 | + self.draw.line_2d(start, end, color); |
| 155 | + } |
| 156 | + pub(super) fn set_scope(&mut self, rect: LayoutRect) { |
| 157 | + let (left, right, top, bottom) = rect_border_axis(rect); |
| 158 | + self.known_x.add(left, 1); |
| 159 | + self.known_x.add(right, -1); |
| 160 | + self.known_y.add(top, 1); |
| 161 | + self.known_y.add(bottom, -1); |
| 162 | + } |
| 163 | + pub(super) fn clear_scope(&mut self, rect: LayoutRect) { |
| 164 | + let (left, right, top, bottom) = rect_border_axis(rect); |
| 165 | + self.known_x.remove(left, 1); |
| 166 | + self.known_x.remove(right, -1); |
| 167 | + self.known_y.remove(top, 1); |
| 168 | + self.known_y.remove(bottom, -1); |
| 169 | + } |
| 170 | + pub(super) fn rect_2d(&mut self, rect: LayoutRect, color: Color) { |
| 171 | + let (left, right, top, bottom) = rect_border_axis(rect); |
| 172 | + if approx_eq(left, right) { |
| 173 | + self.line_2d(Vec2::new(left, top), Vec2::new(left, bottom), color); |
| 174 | + } else if approx_eq(top, bottom) { |
| 175 | + self.line_2d(Vec2::new(left, top), Vec2::new(right, top), color); |
| 176 | + } else { |
| 177 | + let inset_x = |v| self.known_x.inset(v); |
| 178 | + let inset_y = |v| self.known_y.inset(v); |
| 179 | + let (left, right) = (inset_x(left), inset_x(right)); |
| 180 | + let (top, bottom) = (inset_y(top), inset_y(bottom)); |
| 181 | + let strip = [ |
| 182 | + Vec2::new(left, top), |
| 183 | + Vec2::new(left, bottom), |
| 184 | + Vec2::new(right, bottom), |
| 185 | + Vec2::new(right, top), |
| 186 | + Vec2::new(left, top), |
| 187 | + ]; |
| 188 | + self.draw |
| 189 | + .linestrip_2d(strip.map(|v| self.relative(v)), color); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments