made painter actually how I wanted it (draw now takes in an owned painter)
This commit is contained in:
@@ -6,9 +6,8 @@ pub struct Regioned {
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> Widget<Ctx> for Regioned {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
||||
painter.region.select(&self.region);
|
||||
painter.draw_inner(&self.inner);
|
||||
fn draw(&self, mut painter: Painter<Ctx>) {
|
||||
painter.draw_within(&self.inner, self.region);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::{Color, TextureHandle, Widget, WidgetFnRet, render::RectPrimitive};
|
||||
use crate::{Color, Painter, TextureHandle, Widget, WidgetFnRet, render::RectPrimitive};
|
||||
|
||||
pub struct Image {
|
||||
handle: Option<TextureHandle>,
|
||||
}
|
||||
|
||||
impl<Ctx> Widget<Ctx> for Image {
|
||||
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
|
||||
fn draw(&self, mut painter: Painter<Ctx>) {
|
||||
if let Some(handle) = &self.handle {
|
||||
painter.draw_texture(handle);
|
||||
} else {
|
||||
|
||||
@@ -28,7 +28,7 @@ impl Rect {
|
||||
}
|
||||
|
||||
impl<Ctx> Widget<Ctx> for Rect {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
||||
fn draw(&self, mut painter: Painter<Ctx>) {
|
||||
painter.write(RectPrimitive {
|
||||
color: self.color,
|
||||
radius: self.radius,
|
||||
|
||||
@@ -6,7 +6,7 @@ pub struct Span {
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> Widget<Ctx> for Span {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
||||
fn draw(&self, mut painter: Painter<Ctx>) {
|
||||
let total = self.sums();
|
||||
let mut start = UIScalar::min();
|
||||
for (child, length) in &self.children {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::{Widget, WidgetId};
|
||||
use crate::{Painter, Widget, WidgetId};
|
||||
|
||||
pub struct Stack {
|
||||
pub children: Vec<WidgetId>,
|
||||
}
|
||||
|
||||
impl<Ctx> Widget<Ctx> for Stack {
|
||||
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
|
||||
fn draw(&self, mut painter: Painter<Ctx>) {
|
||||
for child in &self.children {
|
||||
painter.draw(child);
|
||||
}
|
||||
|
||||
@@ -6,17 +6,18 @@ use crate::{
|
||||
pub struct Painter<'a, Ctx: 'static> {
|
||||
nodes: &'a Widgets<Ctx>,
|
||||
ctx: &'a mut Ctx,
|
||||
sensors_map: &'a mut SensorMap<Ctx>,
|
||||
sensors_map: &'a SensorMap<Ctx>,
|
||||
active_sensors: &'a mut ActiveSensors,
|
||||
primitives: Primitives,
|
||||
pub region: UiRegion,
|
||||
primitives: &'a mut Primitives,
|
||||
region: UiRegion,
|
||||
}
|
||||
|
||||
impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
pub fn new(
|
||||
nodes: &'a Widgets<Ctx>,
|
||||
primitives: &'a mut Primitives,
|
||||
ctx: &'a mut Ctx,
|
||||
sensors_map: &'a mut SensorMap<Ctx>,
|
||||
sensors_map: &'a SensorMap<Ctx>,
|
||||
active_sensors: &'a mut ActiveSensors,
|
||||
) -> Self {
|
||||
Self {
|
||||
@@ -24,59 +25,56 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
||||
ctx,
|
||||
active_sensors,
|
||||
sensors_map,
|
||||
primitives: Primitives::default(),
|
||||
primitives,
|
||||
region: UiRegion::full(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes a primitive to be rendered
|
||||
pub fn write<P: Primitive>(&mut self, data: P) {
|
||||
self.primitives.write(data, self.region);
|
||||
}
|
||||
|
||||
/// Draws a widget but does NOT maintain the region.
|
||||
/// Useful if you only have a single widget to draw.
|
||||
pub fn draw_inner<W>(&mut self, id: &WidgetId<W>)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
if self.sensors_map.get(&id.id).is_some() {
|
||||
self.active_sensors.push((self.region, id.id.duplicate()));
|
||||
}
|
||||
self.nodes.get_dyn(id).draw(self);
|
||||
}
|
||||
|
||||
/// Draws a widget and maintains the original region.
|
||||
/// Useful if you need to draw multiple overlapping child widgets.
|
||||
/// Draws a widget within this widget's region.
|
||||
pub fn draw<W>(&mut self, id: &WidgetId<W>)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
let old = self.region;
|
||||
self.draw_inner(id);
|
||||
self.region = old;
|
||||
self.draw_at(id, self.region);
|
||||
}
|
||||
|
||||
/// Draws a widget within an inner region and maintains the original region.
|
||||
/// Useful if you need to draw multiple child widgets in select areas within the original
|
||||
/// region.
|
||||
pub fn draw_within(&mut self, id: &WidgetId, region: UiRegion)
|
||||
/// Draws a widget somewhere within this one.
|
||||
/// Useful for drawing child widgets in select areas.
|
||||
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
let old = self.region;
|
||||
self.region.select(®ion);
|
||||
self.draw_inner(id);
|
||||
self.region = old;
|
||||
self.draw_at(id, region.within(&self.region));
|
||||
}
|
||||
|
||||
/// Draws a widget in an arbitrary region.
|
||||
pub fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
|
||||
where
|
||||
Ctx: 'static,
|
||||
{
|
||||
if self.sensors_map.get(&id.id).is_some() {
|
||||
self.active_sensors.push((region, id.id.duplicate()));
|
||||
}
|
||||
self.nodes.get_dyn(id).draw(Painter {
|
||||
nodes: self.nodes,
|
||||
ctx: self.ctx,
|
||||
sensors_map: self.sensors_map,
|
||||
active_sensors: self.active_sensors,
|
||||
primitives: self.primitives,
|
||||
region,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn draw_texture(&mut self, handle: &TextureHandle) {
|
||||
self.write(handle.inner);
|
||||
}
|
||||
|
||||
pub fn finish(self) -> Primitives {
|
||||
self.primitives
|
||||
}
|
||||
|
||||
pub fn ctx_mut(&mut self) -> &mut Ctx {
|
||||
pub fn ctx(&mut self) -> &mut Ctx {
|
||||
self.ctx
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,16 +93,17 @@ impl<Ctx> Ui<Ctx> {
|
||||
Ctx: 'static,
|
||||
{
|
||||
self.active_sensors.clear();
|
||||
self.primitives.clear();
|
||||
let mut painter = Painter::new(
|
||||
&self.widgets,
|
||||
&mut self.primitives,
|
||||
ctx,
|
||||
&mut self.sensor_map,
|
||||
&self.sensor_map,
|
||||
&mut self.active_sensors,
|
||||
);
|
||||
if let Some(base) = &self.base {
|
||||
painter.draw_inner(base);
|
||||
painter.draw(base);
|
||||
}
|
||||
self.primitives = painter.finish();
|
||||
}
|
||||
|
||||
pub fn update(&mut self, ctx: &mut Ctx)
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::{Painter, Ui, WidgetId, WidgetIdFnRet};
|
||||
use std::{any::Any, marker::PhantomData};
|
||||
|
||||
pub trait Widget<Ctx>: Any {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>);
|
||||
fn draw(&self, painter: Painter<Ctx>);
|
||||
}
|
||||
|
||||
pub struct WidgetTag;
|
||||
|
||||
@@ -61,6 +61,12 @@ macro_rules! primitives {
|
||||
}
|
||||
}
|
||||
|
||||
impl PrimitiveData {
|
||||
pub fn clear(&mut self) {
|
||||
$(self.$name.clear();)*
|
||||
}
|
||||
}
|
||||
|
||||
$(
|
||||
unsafe impl bytemuck::Pod for $ty {}
|
||||
unsafe impl bytemuck::Zeroable for $ty {}
|
||||
@@ -87,6 +93,12 @@ impl Primitives {
|
||||
});
|
||||
vec.push(data);
|
||||
}
|
||||
|
||||
pub(crate) fn clear(&mut self) {
|
||||
self.updated = true;
|
||||
self.instances.clear();
|
||||
self.data.clear();
|
||||
}
|
||||
}
|
||||
|
||||
primitives!(
|
||||
|
||||
@@ -89,10 +89,10 @@ impl Client {
|
||||
ctx.ui[id].color = color.add_rgb(-0.2);
|
||||
})
|
||||
.edit_on(Sense::HoverStart, move |r, _| {
|
||||
r.color = color.add_rgb(0.1);
|
||||
r.color = color.add_rgb(0.4);
|
||||
})
|
||||
.edit_on(Sense::PressEnd, move |r, _| {
|
||||
r.color = color.add_rgb(0.1);
|
||||
r.color = color.add_rgb(0.4);
|
||||
})
|
||||
.edit_on(Sense::HoverEnd, move |r, _| {
|
||||
r.color = color;
|
||||
|
||||
Reference in New Issue
Block a user