remove context generic
This commit is contained in:
@@ -5,8 +5,8 @@ pub struct Regioned {
|
|||||||
pub inner: WidgetId,
|
pub inner: WidgetId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: 'static> Widget<Ctx> for Regioned {
|
impl Widget for Regioned {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
fn draw(&self, painter: &mut Painter) {
|
||||||
painter.draw_within(&self.inner, self.region);
|
painter.draw_within(&self.inner, self.region);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ pub struct Image {
|
|||||||
handle: Option<TextureHandle>,
|
handle: Option<TextureHandle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Widget<Ctx> for Image {
|
impl Widget for Image {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
fn draw(&self, painter: &mut Painter) {
|
||||||
if let Some(handle) = &self.handle {
|
if let Some(handle) = &self.handle {
|
||||||
painter.draw_texture(handle);
|
painter.draw_texture(handle);
|
||||||
} else {
|
} else {
|
||||||
@@ -20,7 +20,7 @@ impl<Ctx> Widget<Ctx> for Image {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn image<Ctx>(image: impl LoadableImage) -> WidgetFnRet!(Image, Ctx) {
|
pub fn image(image: impl LoadableImage) -> WidgetFnRet!(Image) {
|
||||||
let image = match image.get_image() {
|
let image = match image.get_image() {
|
||||||
Ok(image) => Some(image),
|
Ok(image) => Some(image),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ impl Rect {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Widget<Ctx> for Rect {
|
impl Widget for Rect {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
fn draw(&self, painter: &mut Painter) {
|
||||||
painter.write(RectPrimitive {
|
painter.write(RectPrimitive {
|
||||||
color: self.color,
|
color: self.color,
|
||||||
radius: self.radius,
|
radius: self.radius,
|
||||||
|
|||||||
@@ -1,25 +1,21 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub trait Sensable<W, Ctx, Tag> {
|
pub trait Sensable<W, Tag> {
|
||||||
fn on(self, sense: Sense, f: impl SenseFn<Ctx>) -> WidgetIdFnRet!(W, Ctx);
|
fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W);
|
||||||
fn id_on(
|
fn id_on(
|
||||||
self,
|
self,
|
||||||
sense: Sense,
|
sense: Sense,
|
||||||
f: impl FnMut(&WidgetId<W>, SenseCtx<Ctx>) + 'static + Clone,
|
f: impl FnMut(&WidgetId<W>, &mut Ui) + 'static + Clone,
|
||||||
) -> WidgetIdFnRet!(W, Ctx)
|
) -> WidgetIdFnRet!(W)
|
||||||
where
|
where
|
||||||
W: Widget<Ctx>;
|
W: Widget;
|
||||||
fn edit_on(
|
fn edit_on(self, sense: Sense, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFnRet!(W)
|
||||||
self,
|
|
||||||
sense: Sense,
|
|
||||||
f: impl FnMut(&mut W, &mut Ctx) + 'static + Clone,
|
|
||||||
) -> WidgetIdFnRet!(W, Ctx)
|
|
||||||
where
|
where
|
||||||
W: Widget<Ctx>;
|
W: Widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
||||||
fn on(self, sense: Sense, f: impl SenseFn<Ctx>) -> WidgetIdFnRet!(W::Widget, Ctx) {
|
fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget) {
|
||||||
move |ui| {
|
move |ui| {
|
||||||
let id = self.add(ui);
|
let id = self.add(ui);
|
||||||
ui.add_sensor(
|
ui.add_sensor(
|
||||||
@@ -35,10 +31,10 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
|||||||
fn id_on(
|
fn id_on(
|
||||||
self,
|
self,
|
||||||
sense: Sense,
|
sense: Sense,
|
||||||
mut f: impl FnMut(&WidgetId<W::Widget>, SenseCtx<Ctx>) + 'static + Clone,
|
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui) + 'static + Clone,
|
||||||
) -> WidgetIdFnRet!(W::Widget, Ctx)
|
) -> WidgetIdFnRet!(W::Widget)
|
||||||
where
|
where
|
||||||
W::Widget: Widget<Ctx>,
|
W::Widget: Widget,
|
||||||
{
|
{
|
||||||
self.with_id(move |ui, id| {
|
self.with_id(move |ui, id| {
|
||||||
let id2 = id.clone();
|
let id2 = id.clone();
|
||||||
@@ -48,11 +44,11 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
|||||||
fn edit_on(
|
fn edit_on(
|
||||||
self,
|
self,
|
||||||
sense: Sense,
|
sense: Sense,
|
||||||
mut f: impl FnMut(&mut W::Widget, &mut Ctx) + 'static + Clone,
|
mut f: impl FnMut(&mut W::Widget) + 'static + Clone,
|
||||||
) -> WidgetIdFnRet!(W::Widget, Ctx)
|
) -> WidgetIdFnRet!(W::Widget)
|
||||||
where
|
where
|
||||||
W::Widget: Widget<Ctx>,
|
W::Widget: Widget,
|
||||||
{
|
{
|
||||||
self.id_on(sense, move |id, ctx| f(&mut ctx.ui[id], ctx.app))
|
self.id_on(sense, move |id, ui| f(&mut ui[id]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ pub struct Span {
|
|||||||
pub dir: Dir,
|
pub dir: Dir,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: 'static> Widget<Ctx> for Span {
|
impl Widget for Span {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
fn draw(&self, painter: &mut Painter) {
|
||||||
let total = self.sums();
|
let total = self.sums();
|
||||||
let mut start = UIScalar::min();
|
let mut start = UIScalar::min();
|
||||||
for (child, length) in &self.children {
|
for (child, length) in &self.children {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ pub struct Stack {
|
|||||||
pub children: Vec<WidgetId>,
|
pub children: Vec<WidgetId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Widget<Ctx> for Stack {
|
impl Widget for Stack {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
fn draw(&self, painter: &mut Painter) {
|
||||||
for child in &self.children {
|
for child in &self.children {
|
||||||
painter.draw(child);
|
painter.draw(child);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ impl Text {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Widget<Ctx> for Text {
|
impl Widget for Text {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
fn draw(&self, painter: &mut Painter) {
|
||||||
// TODO: when on_update is added to painter,
|
// TODO: when on_update is added to painter,
|
||||||
// return & store TextureHandle to reuse
|
// return & store TextureHandle to reuse
|
||||||
painter.draw_text(&self.content, &self.attrs);
|
painter.draw_text(&self.content, &self.attrs);
|
||||||
|
|||||||
@@ -3,36 +3,36 @@ use crate::layout::{
|
|||||||
Dir, UiPos, UiRegion, Vec2, WidgetArrLike, WidgetFnRet, WidgetIdFnRet, WidgetLike,
|
Dir, UiPos, UiRegion, Vec2, WidgetArrLike, WidgetFnRet, WidgetIdFnRet, WidgetLike,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait CoreWidget<W, Ctx: 'static, Tag> {
|
pub trait CoreWidget<W, Tag> {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned, Ctx);
|
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned);
|
||||||
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned, Ctx);
|
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned);
|
||||||
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned, Ctx);
|
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned);
|
||||||
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W, Ctx);
|
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
|
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned, Ctx) {
|
fn pad(self, padding: impl Into<Padding>) -> WidgetFnRet!(Regioned) {
|
||||||
|ui| Regioned {
|
|ui| Regioned {
|
||||||
region: padding.into().region(),
|
region: padding.into().region(),
|
||||||
inner: self.add(ui).erase_type(),
|
inner: self.add(ui).erase_type(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned, Ctx) {
|
fn center(self, size: impl Into<Vec2>) -> WidgetFnRet!(Regioned) {
|
||||||
|ui| Regioned {
|
|ui| Regioned {
|
||||||
region: UiPos::center().expand(size.into()),
|
region: UiPos::center().expand(size.into()),
|
||||||
inner: self.add(ui).erase_type(),
|
inner: self.add(ui).erase_type(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned, Ctx) {
|
fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned) {
|
||||||
move |ui| Regioned {
|
move |ui| Regioned {
|
||||||
region,
|
region,
|
||||||
inner: self.add(ui).erase_type(),
|
inner: self.add(ui).erase_type(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W::Widget, Ctx) {
|
fn label(self, label: impl Into<String>) -> WidgetIdFnRet!(W::Widget) {
|
||||||
|ui| {
|
|ui| {
|
||||||
let id = self.add(ui);
|
let id = self.add(ui);
|
||||||
ui.set_label(&id, label.into());
|
ui.set_label(&id, label.into());
|
||||||
@@ -41,22 +41,20 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> CoreWidget<W::Widget, Ctx, Tag>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait CoreWidgetArr<const LEN: usize, Ctx: 'static, Tag> {
|
pub trait CoreWidgetArr<const LEN: usize, Tag> {
|
||||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span, Ctx);
|
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span);
|
||||||
fn stack(self) -> WidgetFnRet!(Stack, Ctx);
|
fn stack(self) -> WidgetFnRet!(Stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx: 'static, Tag>
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
|
||||||
CoreWidgetArr<LEN, Ctx, Tag> for Wa
|
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span) {
|
||||||
{
|
|
||||||
fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span, Ctx) {
|
|
||||||
let lengths = lengths.map(Into::into);
|
let lengths = lengths.map(Into::into);
|
||||||
move |ui| Span {
|
move |ui| Span {
|
||||||
dir,
|
dir,
|
||||||
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn stack(self) -> WidgetFnRet!(Stack, Ctx) {
|
fn stack(self) -> WidgetFnRet!(Stack) {
|
||||||
move |ui| Stack {
|
move |ui| Stack {
|
||||||
children: self.ui(ui).arr.to_vec(),
|
children: self.ui(ui).arr.to_vec(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,24 +119,24 @@ impl<W> Drop for WidgetId<W> {
|
|||||||
pub struct IdTag;
|
pub struct IdTag;
|
||||||
pub struct IdFnTag;
|
pub struct IdFnTag;
|
||||||
|
|
||||||
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
|
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui) -> WidgetId<W>;
|
||||||
macro_rules! WidgetIdFnRet {
|
macro_rules! WidgetIdFnRet {
|
||||||
($W:ty, $Ctx:ty) => {
|
($W:ty) => {
|
||||||
impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $crate::layout::WidgetId<$W>
|
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W>
|
||||||
};
|
};
|
||||||
($W:ty, $Ctx:ty, $($use:tt)*) => {
|
($W:ty, $($use:tt)*) => {
|
||||||
impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $crate::layout::WidgetId<$W> + use<$($use)*>
|
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> + use<$($use)*>
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
pub(crate) use WidgetIdFnRet;
|
pub(crate) use WidgetIdFnRet;
|
||||||
|
|
||||||
pub trait Idable<Ctx, Tag> {
|
pub trait Idable<Tag> {
|
||||||
type Widget: Widget<Ctx>;
|
type Widget: Widget;
|
||||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
|
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
|
||||||
fn id<'a>(
|
fn id<'a>(
|
||||||
self,
|
self,
|
||||||
id: &WidgetId<Self::Widget>,
|
id: &WidgetId<Self::Widget>,
|
||||||
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
|
) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag)
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
@@ -149,7 +149,7 @@ pub trait Idable<Ctx, Tag> {
|
|||||||
fn id_static<'a>(
|
fn id_static<'a>(
|
||||||
self,
|
self,
|
||||||
id: StaticWidgetId<Self::Widget>,
|
id: StaticWidgetId<Self::Widget>,
|
||||||
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
|
) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag)
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
@@ -161,33 +161,33 @@ pub trait Idable<Ctx, Tag> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
|
impl<W: Widget> Idable<WidgetTag> for W {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
|
|
||||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
|
||||||
ui.set(id, self);
|
ui.set(id, self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
|
impl<F: FnOnce(&mut Ui) -> W, W: Widget> Idable<FnTag> for F {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
|
|
||||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
|
||||||
let w = self(ui);
|
let w = self(ui);
|
||||||
ui.set(id, w);
|
ui.set(id, w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for WidgetId<W> {
|
impl<W: 'static> WidgetLike<IdTag> for WidgetId<W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
|
fn add(self, _: &mut Ui) -> WidgetId<W> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdFnTag> for F {
|
impl<W: 'static, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike<IdFnTag> for F {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||||
self(ui)
|
self(ui)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -198,9 +198,9 @@ impl<W> StaticWidgetId<W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for StaticWidgetId<W> {
|
impl<W: 'static> WidgetLike<IdTag> for StaticWidgetId<W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||||
self.id(&ui.send)
|
self.id(&ui.send)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,10 +18,9 @@ struct State {
|
|||||||
id: Option<Id>,
|
id: Option<Id>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Painter<'a, Ctx: 'static> {
|
pub struct Painter<'a> {
|
||||||
widgets: &'a Widgets<Ctx>,
|
widgets: &'a Widgets,
|
||||||
ctx: &'a mut Ctx,
|
sensors_map: &'a SensorMap,
|
||||||
sensors_map: &'a SensorMap<Ctx>,
|
|
||||||
pub(super) active: &'a mut Active,
|
pub(super) active: &'a mut Active,
|
||||||
pub(super) primitives: &'a mut Primitives,
|
pub(super) primitives: &'a mut Primitives,
|
||||||
textures: &'a mut Textures,
|
textures: &'a mut Textures,
|
||||||
@@ -31,13 +30,12 @@ pub struct Painter<'a, Ctx: 'static> {
|
|||||||
state: State,
|
state: State,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Ctx> Painter<'a, Ctx> {
|
impl<'a> Painter<'a> {
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub(super) fn new(
|
pub(super) fn new(
|
||||||
nodes: &'a Widgets<Ctx>,
|
nodes: &'a Widgets,
|
||||||
primitives: &'a mut Primitives,
|
primitives: &'a mut Primitives,
|
||||||
ctx: &'a mut Ctx,
|
sensors_map: &'a SensorMap,
|
||||||
sensors_map: &'a SensorMap<Ctx>,
|
|
||||||
active: &'a mut Active,
|
active: &'a mut Active,
|
||||||
text: &'a mut TextData,
|
text: &'a mut TextData,
|
||||||
textures: &'a mut Textures,
|
textures: &'a mut Textures,
|
||||||
@@ -45,7 +43,6 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
widgets: nodes,
|
widgets: nodes,
|
||||||
ctx,
|
|
||||||
active,
|
active,
|
||||||
sensors_map,
|
sensors_map,
|
||||||
primitives,
|
primitives,
|
||||||
@@ -68,34 +65,22 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a widget within this widget's region.
|
/// Draws a widget within this widget's region.
|
||||||
pub fn draw<W>(&mut self, id: &WidgetId<W>)
|
pub fn draw<W>(&mut self, id: &WidgetId<W>) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
self.draw_at(id, self.state.region);
|
self.draw_at(id, self.state.region);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a widget somewhere within this one.
|
/// Draws a widget somewhere within this one.
|
||||||
/// Useful for drawing child widgets in select areas.
|
/// Useful for drawing child widgets in select areas.
|
||||||
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
|
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
self.draw_at(id, region.within(&self.state.region));
|
self.draw_at(id, region.within(&self.state.region));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Draws a widget in an arbitrary region.
|
/// Draws a widget in an arbitrary region.
|
||||||
pub fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion)
|
pub fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
self.draw_raw_at(&id.id, region);
|
self.draw_raw_at(&id.id, region);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_raw_at(&mut self, id: &Id, region: UiRegion)
|
fn draw_raw_at(&mut self, id: &Id, region: UiRegion) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
if self.active.widgets.contains_key(id) {
|
if self.active.widgets.contains_key(id) {
|
||||||
panic!("widget drawn twice!");
|
panic!("widget drawn twice!");
|
||||||
}
|
}
|
||||||
@@ -158,10 +143,6 @@ impl<'a, Ctx> Painter<'a, Ctx> {
|
|||||||
self.state.region
|
self.state.region
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ctx(&mut self) -> &mut Ctx {
|
|
||||||
self.ctx
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn region_size(&self) -> Vec2 {
|
pub fn region_size(&self) -> Vec2 {
|
||||||
self.state.region.in_size(self.screen_size)
|
self.state.region.in_size(self.screen_size)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,12 +30,12 @@ pub enum ActivationState {
|
|||||||
Off,
|
Off,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Sensor<Ctx> {
|
pub struct Sensor {
|
||||||
pub sense: Sense,
|
pub sense: Sense,
|
||||||
pub f: Box<dyn SenseFn<Ctx>>,
|
pub f: Box<dyn SenseFn>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
|
pub type SensorMap = HashMap<Id, SensorGroup>;
|
||||||
pub type ActiveSensors = HashMap<Id, SenseShape>;
|
pub type ActiveSensors = HashMap<Id, SenseShape>;
|
||||||
pub type SenseShape = UiRegion;
|
pub type SenseShape = UiRegion;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -43,26 +43,22 @@ pub struct SenseTrigger {
|
|||||||
pub shape: SenseShape,
|
pub shape: SenseShape,
|
||||||
pub sense: Sense,
|
pub sense: Sense,
|
||||||
}
|
}
|
||||||
pub struct SensorGroup<Ctx> {
|
pub struct SensorGroup {
|
||||||
pub hover: ActivationState,
|
pub hover: ActivationState,
|
||||||
pub cursor: ActivationState,
|
pub cursor: ActivationState,
|
||||||
pub sensors: Vec<Sensor<Ctx>>,
|
pub sensors: Vec<Sensor>,
|
||||||
}
|
}
|
||||||
pub struct SenseCtx<'a, Ctx> {
|
pub trait SenseFn: FnMut(&mut Ui) + 'static {
|
||||||
pub ui: &'a mut Ui<Ctx>,
|
fn box_clone(&self) -> Box<dyn SenseFn>;
|
||||||
pub app: &'a mut Ctx,
|
|
||||||
}
|
}
|
||||||
pub trait SenseFn<Ctx>: FnMut(SenseCtx<Ctx>) + 'static {
|
impl<F: FnMut(&mut Ui) + 'static + Clone> SenseFn for F {
|
||||||
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>>;
|
fn box_clone(&self) -> Box<dyn SenseFn> {
|
||||||
}
|
|
||||||
impl<F: FnMut(SenseCtx<Ctx>) + 'static + Clone, Ctx> SenseFn<Ctx> for F {
|
|
||||||
fn box_clone(&self) -> Box<dyn SenseFn<Ctx>> {
|
|
||||||
Box::new(self.clone())
|
Box::new(self.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Ui<Ctx> {
|
impl Ui {
|
||||||
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor<Ctx>) {
|
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor) {
|
||||||
self.sensor_map
|
self.sensor_map
|
||||||
.entry(id.id.duplicate())
|
.entry(id.id.duplicate())
|
||||||
.or_default()
|
.or_default()
|
||||||
@@ -70,10 +66,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
.push(f);
|
.push(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_sensors(&mut self, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2)
|
pub fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
let active = std::mem::take(&mut self.active.sensors);
|
let active = std::mem::take(&mut self.active.sensors);
|
||||||
let mut map = std::mem::take(&mut self.sensor_map);
|
let mut map = std::mem::take(&mut self.sensor_map);
|
||||||
for (id, shape) in active.iter() {
|
for (id, shape) in active.iter() {
|
||||||
@@ -85,7 +78,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
|
|
||||||
for sensor in &mut group.sensors {
|
for sensor in &mut group.sensors {
|
||||||
if should_run(sensor.sense, group.cursor, group.hover) {
|
if should_run(sensor.sense, group.cursor, group.hover) {
|
||||||
(sensor.f.box_clone())(SenseCtx { ui: self, app: ctx });
|
(sensor.f.box_clone())(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,7 +134,7 @@ impl ActivationState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Default for SensorGroup<Ctx> {
|
impl Default for SensorGroup {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
hover: Default::default(),
|
hover: Default::default(),
|
||||||
@@ -150,7 +143,7 @@ impl<Ctx> Default for SensorGroup<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<Ctx: 'static> Clone for SensorGroup<Ctx> {
|
impl Clone for SensorGroup {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
hover: self.hover,
|
hover: self.hover,
|
||||||
@@ -159,7 +152,7 @@ impl<Ctx: 'static> Clone for SensorGroup<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<Ctx: 'static> Clone for Sensor<Ctx> {
|
impl Clone for Sensor {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
sense: self.sense,
|
sense: self.sense,
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ use std::{
|
|||||||
sync::mpsc::{Receiver, Sender, channel},
|
sync::mpsc::{Receiver, Sender, channel},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Ui<Ctx> {
|
pub struct Ui {
|
||||||
base: Option<WidgetId>,
|
base: Option<WidgetId>,
|
||||||
widgets: Widgets<Ctx>,
|
widgets: Widgets,
|
||||||
labels: HashMap<Id, String>,
|
labels: HashMap<Id, String>,
|
||||||
updates: Vec<Id>,
|
updates: Vec<Id>,
|
||||||
recv: Receiver<Id>,
|
recv: Receiver<Id>,
|
||||||
@@ -29,26 +29,23 @@ pub struct Ui<Ctx> {
|
|||||||
full_redraw: bool,
|
full_redraw: bool,
|
||||||
|
|
||||||
pub(super) active: Active,
|
pub(super) active: Active,
|
||||||
pub(super) sensor_map: SensorMap<Ctx>,
|
pub(super) sensor_map: SensorMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Widgets<Ctx> {
|
pub struct Widgets {
|
||||||
ids: IdTracker,
|
ids: IdTracker,
|
||||||
map: HashMap<Id, Box<dyn Widget<Ctx>>>,
|
map: HashMap<Id, Box<dyn Widget>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Ui<Ctx> {
|
impl Ui {
|
||||||
pub fn add<W: Widget<Ctx>, Tag>(
|
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetId<W> {
|
||||||
&mut self,
|
|
||||||
w: impl WidgetLike<Ctx, Tag, Widget = W>,
|
|
||||||
) -> WidgetId<W> {
|
|
||||||
w.add(self)
|
w.add(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_static<W: Widget<Ctx>, Tag>(
|
pub fn add_static<W: Widget, Tag>(
|
||||||
&mut self,
|
&mut self,
|
||||||
w: impl WidgetLike<Ctx, Tag, Widget = W>,
|
w: impl WidgetLike<Tag, Widget = W>,
|
||||||
) -> StaticWidgetId<W> {
|
) -> StaticWidgetId<W> {
|
||||||
let id = w.add(self);
|
let id = w.add(self);
|
||||||
id.into_static()
|
id.into_static()
|
||||||
@@ -59,11 +56,11 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
self.labels.insert(id.id.duplicate(), label);
|
self.labels.insert(id.id.duplicate(), label);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_widget<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
|
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||||
self.push(w)
|
self.push(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
|
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||||
let id = self.id();
|
let id = self.id();
|
||||||
self.labels
|
self.labels
|
||||||
.insert(id.id.duplicate(), std::any::type_name::<W>().to_string());
|
.insert(id.id.duplicate(), std::any::type_name::<W>().to_string());
|
||||||
@@ -71,31 +68,28 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>, w: W) {
|
pub fn set<W: Widget>(&mut self, id: &WidgetId<W>, w: W) {
|
||||||
self.widgets.insert(id.id.duplicate(), w);
|
self.widgets.insert(id.id.duplicate(), w);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
|
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
||||||
self.base = Some(w.add(self).erase_type());
|
self.base = Some(w.add(self).erase_type());
|
||||||
self.full_redraw = true;
|
self.full_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Self
|
pub fn new() -> Self {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
|
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||||
self.widgets.get(id)
|
self.widgets.get(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
||||||
self.widgets.get_mut(id)
|
self.widgets.get_mut(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
|
pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
|
||||||
WidgetId::new(
|
WidgetId::new(
|
||||||
self.widgets.reserve(),
|
self.widgets.reserve(),
|
||||||
TypeId::of::<W>(),
|
TypeId::of::<W>(),
|
||||||
@@ -104,7 +98,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn id_static<W: Widget<Ctx>>(&mut self) -> StaticWidgetId<W> {
|
pub fn id_static<W: Widget>(&mut self) -> StaticWidgetId<W> {
|
||||||
let id = self.id();
|
let id = self.id();
|
||||||
id.into_static()
|
id.into_static()
|
||||||
}
|
}
|
||||||
@@ -117,10 +111,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
self.size = size.into();
|
self.size = size.into();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redraw_all(&mut self, ctx: &mut Ctx)
|
pub fn redraw_all(&mut self) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
self.active.clear();
|
self.active.clear();
|
||||||
self.primitives.clear();
|
self.primitives.clear();
|
||||||
// free before bc nothing should exist
|
// free before bc nothing should exist
|
||||||
@@ -128,7 +119,6 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
let mut painter = Painter::new(
|
let mut painter = Painter::new(
|
||||||
&self.widgets,
|
&self.widgets,
|
||||||
&mut self.primitives,
|
&mut self.primitives,
|
||||||
ctx,
|
|
||||||
&self.sensor_map,
|
&self.sensor_map,
|
||||||
&mut self.active,
|
&mut self.active,
|
||||||
&mut self.text,
|
&mut self.text,
|
||||||
@@ -141,26 +131,19 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
self.primitives.replace();
|
self.primitives.replace();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, ctx: &mut Ctx)
|
pub fn update(&mut self) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
if self.full_redraw {
|
if self.full_redraw {
|
||||||
self.redraw_all(ctx);
|
self.redraw_all();
|
||||||
self.full_redraw = false;
|
self.full_redraw = false;
|
||||||
} else if !self.updates.is_empty() {
|
} else if !self.updates.is_empty() {
|
||||||
self.redraw_updates(ctx);
|
self.redraw_updates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn redraw_updates(&mut self, ctx: &mut Ctx)
|
fn redraw_updates(&mut self) {
|
||||||
where
|
|
||||||
Ctx: 'static,
|
|
||||||
{
|
|
||||||
let mut painter = Painter::new(
|
let mut painter = Painter::new(
|
||||||
&self.widgets,
|
&self.widgets,
|
||||||
&mut self.primitives,
|
&mut self.primitives,
|
||||||
ctx,
|
|
||||||
&self.sensor_map,
|
&self.sensor_map,
|
||||||
&mut self.active,
|
&mut self.active,
|
||||||
&mut self.text,
|
&mut self.text,
|
||||||
@@ -191,7 +174,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
|
impl<W: Widget> Index<&WidgetId<W>> for Ui {
|
||||||
type Output = W;
|
type Output = W;
|
||||||
|
|
||||||
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
|
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
|
||||||
@@ -199,14 +182,14 @@ impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
|
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
|
||||||
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
|
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
|
||||||
self.updates.push(id.id.duplicate());
|
self.updates.push(id.id.duplicate());
|
||||||
self.get_mut(id).unwrap()
|
self.get_mut(id).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
|
impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
|
||||||
type Output = W;
|
type Output = W;
|
||||||
|
|
||||||
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
|
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
|
||||||
@@ -214,14 +197,14 @@ impl<W: Widget<Ctx>, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> IndexMut<StaticWidgetId<W>> for Ui<Ctx> {
|
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
|
||||||
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
|
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
|
||||||
self.updates.push(id.id.id());
|
self.updates.push(id.id.id());
|
||||||
self.widgets.get_static_mut(&id).unwrap()
|
self.widgets.get_static_mut(&id).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Widgets<Ctx> {
|
impl Widgets {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
ids: IdTracker::default(),
|
ids: IdTracker::default(),
|
||||||
@@ -229,15 +212,15 @@ impl<Ctx> Widgets<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_dyn(&self, id: &Id) -> &dyn Widget<Ctx> {
|
pub fn get_dyn(&self, id: &Id) -> &dyn Widget {
|
||||||
self.map.get(id).unwrap().as_ref()
|
self.map.get(id).unwrap().as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_static<W: Widget<Ctx>>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
|
pub fn get_static<W: Widget>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
|
||||||
self.map.get(&id.id.id()).unwrap().as_any().downcast_ref()
|
self.map.get(&id.id.id()).unwrap().as_any().downcast_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_static_mut<W: Widget<Ctx>>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
|
pub fn get_static_mut<W: Widget>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
|
||||||
self.map
|
self.map
|
||||||
.get_mut(&id.id.id())
|
.get_mut(&id.id.id())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -245,11 +228,11 @@ impl<Ctx> Widgets<Ctx> {
|
|||||||
.downcast_mut()
|
.downcast_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
|
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||||
self.map.get(&id.id).unwrap().as_any().downcast_ref()
|
self.map.get(&id.id).unwrap().as_any().downcast_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut<W: Widget<Ctx>>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
||||||
self.map
|
self.map
|
||||||
.get_mut(&id.id)
|
.get_mut(&id.id)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -257,11 +240,11 @@ impl<Ctx> Widgets<Ctx> {
|
|||||||
.downcast_mut()
|
.downcast_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, id: Id, widget: impl Widget<Ctx>) {
|
pub fn insert(&mut self, id: Id, widget: impl Widget) {
|
||||||
self.map.insert(id, Box::new(widget));
|
self.map.insert(id, Box::new(widget));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
|
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>) {
|
||||||
self.map.insert(id, widget);
|
self.map.insert(id, widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,7 +266,7 @@ impl<Ctx> Widgets<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> dyn Widget<Ctx> {
|
impl dyn Widget {
|
||||||
pub fn as_any(&self) -> &dyn Any {
|
pub fn as_any(&self) -> &dyn Any {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@@ -293,7 +276,7 @@ impl<Ctx> dyn Widget<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: 'static> Default for Ui<Ctx> {
|
impl Default for Ui {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let (send, recv) = channel();
|
let (send, recv) = channel();
|
||||||
Self {
|
Self {
|
||||||
@@ -341,7 +324,7 @@ impl Active {
|
|||||||
instance
|
instance
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add<Ctx>(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap<Ctx>) {
|
pub fn add(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap) {
|
||||||
if sensors_map.get(id).is_some() {
|
if sensors_map.get(id).is_some() {
|
||||||
self.sensors.insert(id.duplicate(), instance.region);
|
self.sensors.insert(id.duplicate(), instance.region);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,20 +2,20 @@ use crate::layout::{Painter, Ui, WidgetId, WidgetIdFnRet};
|
|||||||
|
|
||||||
use std::{any::Any, marker::PhantomData};
|
use std::{any::Any, marker::PhantomData};
|
||||||
|
|
||||||
pub trait Widget<Ctx>: Any {
|
pub trait Widget: Any {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>);
|
fn draw(&self, painter: &mut Painter);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetTag;
|
pub struct WidgetTag;
|
||||||
pub struct FnTag;
|
pub struct FnTag;
|
||||||
|
|
||||||
pub trait WidgetLike<Ctx, Tag> {
|
pub trait WidgetLike<Tag> {
|
||||||
type Widget: 'static;
|
type Widget: 'static;
|
||||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
|
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
|
||||||
fn with_id<W2>(
|
fn with_id<W2>(
|
||||||
self,
|
self,
|
||||||
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
||||||
) -> WidgetIdFnRet!(W2, Ctx)
|
) -> WidgetIdFnRet!(W2)
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
@@ -33,22 +33,22 @@ pub trait WidgetLike<Ctx, Tag> {
|
|||||||
/// don't need to be IDs yet
|
/// don't need to be IDs yet
|
||||||
/// currently a macro for rust analyzer (doesn't support trait aliases atm)
|
/// currently a macro for rust analyzer (doesn't support trait aliases atm)
|
||||||
macro_rules! WidgetFnRet {
|
macro_rules! WidgetFnRet {
|
||||||
($W:ty, $Ctx:ty) => {
|
($W:ty) => {
|
||||||
impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $W
|
impl FnOnce(&mut $crate::layout::Ui) -> $W
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
pub(crate) use WidgetFnRet;
|
pub(crate) use WidgetFnRet;
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
|
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||||
self(ui).add(ui)
|
self(ui).add(ui)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
|
impl<W: Widget> WidgetLike<WidgetTag> for W {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||||
ui.add_widget(self)
|
ui.add_widget(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,21 +68,21 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ArrTag;
|
pub struct ArrTag;
|
||||||
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
|
pub trait WidgetArrLike<const LEN: usize, Tags> {
|
||||||
type Ws;
|
type Ws;
|
||||||
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
|
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
|
impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
|
||||||
type Ws = Ws;
|
type Ws = Ws;
|
||||||
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
|
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
|
impl<W: WidgetLike<WidgetTag>> WidgetArrLike<1, WidgetTag> for W {
|
||||||
type Ws = (W::Widget,);
|
type Ws = (W::Widget,);
|
||||||
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
|
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
|
||||||
WidgetArr::new([self.add(ui).erase_type()])
|
WidgetArr::new([self.add(ui).erase_type()])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,9 +93,9 @@ macro_rules! impl_widget_arr {
|
|||||||
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
||||||
};
|
};
|
||||||
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
||||||
impl<$($W: WidgetLike<Ctx, $Tag>,$Tag,)* Ctx> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
|
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
|
||||||
type Ws = ($($W::Widget,)*);
|
type Ws = ($($W::Widget,)*);
|
||||||
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
|
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
let ($($W,)*) = self;
|
let ($($W,)*) = self;
|
||||||
WidgetArr::new(
|
WidgetArr::new(
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ impl UiRenderer {
|
|||||||
pass.draw(0..4, 0..self.instance.len() as u32);
|
pass.draw(0..4, 0..self.instance.len() as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update<Ctx>(&mut self, device: &Device, queue: &Queue, ui: &mut Ui<Ctx>) {
|
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
|
||||||
if ui.primitives.updated {
|
if ui.primitives.updated {
|
||||||
self.instance
|
self.instance
|
||||||
.update(device, queue, &ui.primitives.instances);
|
.update(device, queue, &ui.primitives.instances);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use super::Client;
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct App {
|
pub struct App {
|
||||||
client: Option<(Client, Ui<Client>)>,
|
client: Option<(Client, Ui)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ pub struct ClientUi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn create_ui() -> (Ui<Self>, ClientUi) {
|
pub fn create_ui() -> (Ui, ClientUi) {
|
||||||
let mut ui = Ui::new();
|
let mut ui = Ui::new();
|
||||||
let rect = Rect {
|
let rect = Rect {
|
||||||
color: UiColor::WHITE,
|
color: UiColor::WHITE,
|
||||||
@@ -78,17 +78,17 @@ impl Client {
|
|||||||
|
|
||||||
let switch_button = |color, to, label| {
|
let switch_button = |color, to, label| {
|
||||||
let rect = Rect::new(color)
|
let rect = Rect::new(color)
|
||||||
.id_on(Sense::PressStart, move |id, ctx| {
|
.id_on(Sense::PressStart, move |id, ui| {
|
||||||
ctx.ui[main].inner.set_static(to);
|
ui[main].inner.set_static(to);
|
||||||
ctx.ui[id].color = color.add_rgb(-0.2);
|
ui[id].color = color.add_rgb(-0.2);
|
||||||
})
|
})
|
||||||
.edit_on(Sense::HoverStart, move |r, _| {
|
.edit_on(Sense::HoverStart, move |r| {
|
||||||
r.color = color.add_rgb(0.4);
|
r.color = color.add_rgb(0.4);
|
||||||
})
|
})
|
||||||
.edit_on(Sense::PressEnd, move |r, _| {
|
.edit_on(Sense::PressEnd, move |r| {
|
||||||
r.color = color.add_rgb(0.4);
|
r.color = color.add_rgb(0.4);
|
||||||
})
|
})
|
||||||
.edit_on(Sense::HoverEnd, move |r, _| {
|
.edit_on(Sense::HoverEnd, move |r| {
|
||||||
r.color = color;
|
r.color = color;
|
||||||
});
|
});
|
||||||
(rect, text(label).size(30)).stack()
|
(rect, text(label).size(30)).stack()
|
||||||
@@ -104,12 +104,11 @@ impl Client {
|
|||||||
);
|
);
|
||||||
let add_button = Rect::new(Color::LIME)
|
let add_button = Rect::new(Color::LIME)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(Sense::PressStart, move |ctx| {
|
.on(Sense::PressStart, move |ui| {
|
||||||
let child = ctx
|
let child = ui
|
||||||
.ui
|
|
||||||
.add(image(include_bytes!("assets/sungals.png")))
|
.add(image(include_bytes!("assets/sungals.png")))
|
||||||
.erase_type();
|
.erase_type();
|
||||||
ctx.ui[span_add].children.push((child, ratio(1)));
|
ui[span_add].children.push((child, ratio(1)));
|
||||||
})
|
})
|
||||||
.region(
|
.region(
|
||||||
UiPos::corner(Corner::BotRight)
|
UiPos::corner(Corner::BotRight)
|
||||||
@@ -119,8 +118,8 @@ impl Client {
|
|||||||
|
|
||||||
let del_button = Rect::new(Color::RED)
|
let del_button = Rect::new(Color::RED)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(Sense::PressStart, move |ctx| {
|
.on(Sense::PressStart, move |ui| {
|
||||||
ctx.ui[span_add].children.pop();
|
ui[span_add].children.pop();
|
||||||
})
|
})
|
||||||
.region(
|
.region(
|
||||||
UiPos::corner(Corner::BotLeft)
|
UiPos::corner(Corner::BotLeft)
|
||||||
@@ -167,15 +166,15 @@ impl Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui<Self>) {
|
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui) {
|
||||||
self.input.event(&event);
|
self.input.event(&event);
|
||||||
let cursor_state = self.cursor_state();
|
let cursor_state = self.cursor_state();
|
||||||
let window_size = self.window_size();
|
let window_size = self.window_size();
|
||||||
ui.run_sensors(self, &cursor_state, window_size);
|
ui.run_sensors(&cursor_state, window_size);
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested => event_loop.exit(),
|
WindowEvent::CloseRequested => event_loop.exit(),
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
ui.update(self);
|
ui.update();
|
||||||
self.renderer.update(ui);
|
self.renderer.update(ui);
|
||||||
self.renderer.draw()
|
self.renderer.draw()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ pub struct Renderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Renderer {
|
impl Renderer {
|
||||||
pub fn update<Ctx>(&mut self, updates: &mut Ui<Ctx>) {
|
pub fn update(&mut self, updates: &mut Ui) {
|
||||||
self.ui.update(&self.device, &self.queue, updates);
|
self.ui.update(&self.device, &self.queue, updates);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user