renaming & comments
This commit is contained in:
@@ -10,10 +10,6 @@ use crate::{
|
|||||||
util::{HashMap, Id},
|
util::{HashMap, Id},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait CursorCtx {
|
|
||||||
fn cursor_state(&self) -> &CursorState;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub enum Button {
|
pub enum Button {
|
||||||
Left,
|
Left,
|
||||||
@@ -22,7 +18,7 @@ pub enum Button {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub enum Sense {
|
pub enum CursorSense {
|
||||||
PressStart(Button),
|
PressStart(Button),
|
||||||
Pressing(Button),
|
Pressing(Button),
|
||||||
PressEnd(Button),
|
PressEnd(Button),
|
||||||
@@ -32,9 +28,9 @@ pub enum Sense {
|
|||||||
Scroll,
|
Scroll,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Senses(Vec<Sense>);
|
pub struct CursorSenses(Vec<CursorSense>);
|
||||||
|
|
||||||
impl Sense {
|
impl CursorSense {
|
||||||
pub fn click() -> Self {
|
pub fn click() -> Self {
|
||||||
Self::PressStart(Button::Left)
|
Self::PressStart(Button::Left)
|
||||||
}
|
}
|
||||||
@@ -90,8 +86,13 @@ pub enum ActivationState {
|
|||||||
Off,
|
Off,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// this and other similar stuff has a generic
|
||||||
|
/// because I kind of want to make CursorModule generic
|
||||||
|
/// or basically have some way to have custom senses
|
||||||
|
/// that depend on active widget positions
|
||||||
|
/// but I'm not sure how or if worth it
|
||||||
pub struct Sensor<Ctx, Data> {
|
pub struct Sensor<Ctx, Data> {
|
||||||
pub senses: Senses,
|
pub senses: CursorSenses,
|
||||||
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,12 +110,12 @@ pub struct CursorData {
|
|||||||
pub scroll_delta: Vec2,
|
pub scroll_delta: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SensorModule<Ctx> {
|
pub struct CursorModule<Ctx> {
|
||||||
map: SensorMap<Ctx, CursorData>,
|
map: SensorMap<Ctx, CursorData>,
|
||||||
active: HashMap<usize, HashMap<Id, SenseShape>>,
|
active: HashMap<usize, HashMap<Id, SenseShape>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: 'static> UiModule for SensorModule<Ctx> {
|
impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
|
||||||
fn on_draw(&mut self, inst: &WidgetInstance) {
|
fn on_draw(&mut self, inst: &WidgetInstance) {
|
||||||
if self.map.contains_key(&inst.id) {
|
if self.map.contains_key(&inst.id) {
|
||||||
self.active
|
self.active
|
||||||
@@ -146,7 +147,7 @@ impl<Ctx: 'static> UiModule for SensorModule<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> SensorModule<Ctx> {
|
impl<Ctx> CursorModule<Ctx> {
|
||||||
pub fn merge(&mut self, other: Self) {
|
pub fn merge(&mut self, other: Self) {
|
||||||
for (id, group) in other.map {
|
for (id, group) in other.map {
|
||||||
for sensor in group.sensors {
|
for sensor in group.sensors {
|
||||||
@@ -162,11 +163,11 @@ pub trait SensorCtx: UiCtx {
|
|||||||
|
|
||||||
impl<Ctx: UiCtx + 'static> SensorCtx for Ctx {
|
impl<Ctx: UiCtx + 'static> SensorCtx for Ctx {
|
||||||
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
|
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
|
||||||
SensorModule::<Ctx>::run(self, cursor, window_size);
|
CursorModule::<Ctx>::run(self, cursor, window_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
|
impl<Ctx: UiCtx + 'static> CursorModule<Ctx> {
|
||||||
pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||||
let layers = std::mem::take(&mut ctx.ui().layers);
|
let layers = std::mem::take(&mut ctx.ui().layers);
|
||||||
let mut module = std::mem::take(ctx.ui().modules.get_mut::<Self>());
|
let mut module = std::mem::take(ctx.ui().modules.get_mut::<Self>());
|
||||||
@@ -209,16 +210,16 @@ impl<Ctx: UiCtx + 'static> SensorModule<Ctx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn should_run(senses: &Senses, cursor: &CursorState, hover: ActivationState) -> bool {
|
pub fn should_run(senses: &CursorSenses, cursor: &CursorState, hover: ActivationState) -> bool {
|
||||||
for sense in senses.iter() {
|
for sense in senses.iter() {
|
||||||
if match sense {
|
if match sense {
|
||||||
Sense::PressStart(button) => cursor.buttons.select(button).is_start(),
|
CursorSense::PressStart(button) => cursor.buttons.select(button).is_start(),
|
||||||
Sense::Pressing(button) => cursor.buttons.select(button).is_on(),
|
CursorSense::Pressing(button) => cursor.buttons.select(button).is_on(),
|
||||||
Sense::PressEnd(button) => cursor.buttons.select(button).is_end(),
|
CursorSense::PressEnd(button) => cursor.buttons.select(button).is_end(),
|
||||||
Sense::HoverStart => hover.is_start(),
|
CursorSense::HoverStart => hover.is_start(),
|
||||||
Sense::Hovering => hover.is_on(),
|
CursorSense::Hovering => hover.is_on(),
|
||||||
Sense::HoverEnd => hover.is_end(),
|
CursorSense::HoverEnd => hover.is_end(),
|
||||||
Sense::Scroll => cursor.scroll_delta != Vec2::ZERO,
|
CursorSense::Scroll => cursor.scroll_delta != Vec2::ZERO,
|
||||||
} {
|
} {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -269,18 +270,18 @@ impl ActivationState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for Senses {
|
impl Event for CursorSenses {
|
||||||
type Module<Ctx: 'static> = SensorModule<Ctx>;
|
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||||
type Data = CursorData;
|
type Data = CursorData;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for Sense {
|
impl Event for CursorSense {
|
||||||
type Module<Ctx: 'static> = SensorModule<Ctx>;
|
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||||
type Data = CursorData;
|
type Data = CursorData;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Event<Data = <Senses as Event>::Data> + Into<Senses>, Ctx: 'static> EventModule<E, Ctx>
|
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static> EventModule<E, Ctx>
|
||||||
for SensorModule<Ctx>
|
for CursorModule<Ctx>
|
||||||
{
|
{
|
||||||
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
||||||
// TODO: does not add to active if currently active
|
// TODO: does not add to active if currently active
|
||||||
@@ -324,44 +325,44 @@ impl<Ctx, Data> Default for SensorGroup<Ctx, Data> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Senses {
|
impl Deref for CursorSenses {
|
||||||
type Target = Vec<Sense>;
|
type Target = Vec<CursorSense>;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DerefMut for Senses {
|
impl DerefMut for CursorSenses {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
&mut self.0
|
&mut self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Sense> for Senses {
|
impl From<CursorSense> for CursorSenses {
|
||||||
fn from(val: Sense) -> Self {
|
fn from(val: CursorSense) -> Self {
|
||||||
Senses(vec![val])
|
CursorSenses(vec![val])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BitOr for Sense {
|
impl BitOr for CursorSense {
|
||||||
type Output = Senses;
|
type Output = CursorSenses;
|
||||||
|
|
||||||
fn bitor(self, rhs: Self) -> Self::Output {
|
fn bitor(self, rhs: Self) -> Self::Output {
|
||||||
Senses(vec![self, rhs])
|
CursorSenses(vec![self, rhs])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BitOr<Sense> for Senses {
|
impl BitOr<CursorSense> for CursorSenses {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn bitor(mut self, rhs: Sense) -> Self::Output {
|
fn bitor(mut self, rhs: CursorSense) -> Self::Output {
|
||||||
self.0.push(rhs);
|
self.0.push(rhs);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Default for SensorModule<Ctx> {
|
impl<Ctx> Default for CursorModule<Ctx> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
map: Default::default(),
|
map: Default::default(),
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn scroll(self) -> impl WidgetIdFn<Offset> {
|
fn scroll(self) -> impl WidgetIdFn<Offset> {
|
||||||
self.offset(UiVec2::ZERO).edit_on(Sense::Scroll, |w, data| {
|
self.offset(UiVec2::ZERO).edit_on(CursorSense::Scroll, |w, data| {
|
||||||
w.amt += UiVec2::abs(data.scroll_delta * 50.0);
|
w.amt += UiVec2::abs(data.scroll_delta * 50.0);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ impl Client {
|
|||||||
|
|
||||||
let add_button = rect(Color::LIME)
|
let add_button = rect(Color::LIME)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(Sense::click(), move |ctx: &mut Client, _| {
|
.on(CursorSense::click(), move |ctx: &mut Client, _| {
|
||||||
let child = ctx
|
let child = ctx
|
||||||
.ui
|
.ui
|
||||||
.add(image(include_bytes!("assets/sungals.png")).center())
|
.add(image(include_bytes!("assets/sungals.png")).center())
|
||||||
@@ -93,7 +93,7 @@ impl Client {
|
|||||||
|
|
||||||
let del_button = rect(Color::RED)
|
let del_button = rect(Color::RED)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(Sense::click(), move |ctx: &mut Client, _| {
|
.on(CursorSense::click(), move |ctx: &mut Client, _| {
|
||||||
ctx.ui[span_add].children.pop();
|
ctx.ui[span_add].children.pop();
|
||||||
})
|
})
|
||||||
.sized(150)
|
.sized(150)
|
||||||
@@ -133,14 +133,14 @@ impl Client {
|
|||||||
let add_text = text_edit("add")
|
let add_text = text_edit("add")
|
||||||
.text_align(Align::Left)
|
.text_align(Align::Left)
|
||||||
.size(30)
|
.size(30)
|
||||||
.id_on(Sense::click(), |id, client: &mut Client, ctx| {
|
.id_on(CursorSense::click(), |id, client: &mut Client, ctx| {
|
||||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||||
client.focus = Some(id.clone());
|
client.focus = Some(id.clone());
|
||||||
})
|
})
|
||||||
.id_on(Submit, move |id, client: &mut Client, _| {
|
.id_on(Submit, move |id, client: &mut Client, _| {
|
||||||
let content = client.ui.text(id).take();
|
let content = client.ui.text(id).take();
|
||||||
let text = text_edit(content).size(30).text_align(Align::Left).id_on(
|
let text = text_edit(content).size(30).text_align(Align::Left).id_on(
|
||||||
Sense::click(),
|
CursorSense::click(),
|
||||||
|id, client: &mut Client, ctx| {
|
|id, client: &mut Client, ctx| {
|
||||||
client.ui.text(id).select(ctx.cursor, ctx.size);
|
client.ui.text(id).select(ctx.cursor, ctx.size);
|
||||||
client.focus = Some(id.clone());
|
client.focus = Some(id.clone());
|
||||||
@@ -160,7 +160,7 @@ impl Client {
|
|||||||
(
|
(
|
||||||
add_text.clone(),
|
add_text.clone(),
|
||||||
Rect::new(Color::GREEN)
|
Rect::new(Color::GREEN)
|
||||||
.on(Sense::click(), move |client: &mut Client, _| {
|
.on(CursorSense::click(), move |client: &mut Client, _| {
|
||||||
client.run_event(&add_text, Submit, ());
|
client.run_event(&add_text, Submit, ());
|
||||||
})
|
})
|
||||||
.sized(40),
|
.sized(40),
|
||||||
@@ -178,14 +178,17 @@ impl Client {
|
|||||||
|
|
||||||
let switch_button = |color, to, label| {
|
let switch_button = |color, to, label| {
|
||||||
let rect = rect(color)
|
let rect = rect(color)
|
||||||
.id_on(Sense::click(), move |id, ui: &mut Ui, _| {
|
.id_on(CursorSense::click(), move |id, ui: &mut Ui, _| {
|
||||||
ui[main].inner.set_static(to);
|
ui[main].inner.set_static(to);
|
||||||
ui[id].color = color.darker(0.3);
|
ui[id].color = color.darker(0.3);
|
||||||
})
|
})
|
||||||
.edit_on(Sense::HoverStart | Sense::unclick(), move |r, _| {
|
.edit_on(
|
||||||
|
CursorSense::HoverStart | CursorSense::unclick(),
|
||||||
|
move |r, _| {
|
||||||
r.color = color.brighter(0.2);
|
r.color = color.brighter(0.2);
|
||||||
})
|
},
|
||||||
.edit_on(Sense::HoverEnd, move |r, _| {
|
)
|
||||||
|
.edit_on(CursorSense::HoverEnd, move |r, _| {
|
||||||
r.color = color;
|
r.color = color;
|
||||||
});
|
});
|
||||||
(rect, text(label).size(30)).stack()
|
(rect, text(label).size(30)).stack()
|
||||||
|
|||||||
Reference in New Issue
Block a user