renaming & comments
This commit is contained in:
@@ -10,10 +10,6 @@ use crate::{
|
||||
util::{HashMap, Id},
|
||||
};
|
||||
|
||||
pub trait CursorCtx {
|
||||
fn cursor_state(&self) -> &CursorState;
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Button {
|
||||
Left,
|
||||
@@ -22,7 +18,7 @@ pub enum Button {
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum Sense {
|
||||
pub enum CursorSense {
|
||||
PressStart(Button),
|
||||
Pressing(Button),
|
||||
PressEnd(Button),
|
||||
@@ -32,9 +28,9 @@ pub enum Sense {
|
||||
Scroll,
|
||||
}
|
||||
|
||||
pub struct Senses(Vec<Sense>);
|
||||
pub struct CursorSenses(Vec<CursorSense>);
|
||||
|
||||
impl Sense {
|
||||
impl CursorSense {
|
||||
pub fn click() -> Self {
|
||||
Self::PressStart(Button::Left)
|
||||
}
|
||||
@@ -90,8 +86,13 @@ pub enum ActivationState {
|
||||
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 senses: Senses,
|
||||
pub senses: CursorSenses,
|
||||
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
||||
}
|
||||
|
||||
@@ -109,12 +110,12 @@ pub struct CursorData {
|
||||
pub scroll_delta: Vec2,
|
||||
}
|
||||
|
||||
pub struct SensorModule<Ctx> {
|
||||
pub struct CursorModule<Ctx> {
|
||||
map: SensorMap<Ctx, CursorData>,
|
||||
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) {
|
||||
if self.map.contains_key(&inst.id) {
|
||||
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) {
|
||||
for (id, group) in other.map {
|
||||
for sensor in group.sensors {
|
||||
@@ -162,11 +163,11 @@ pub trait SensorCtx: UiCtx {
|
||||
|
||||
impl<Ctx: UiCtx + 'static> SensorCtx for Ctx {
|
||||
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) {
|
||||
let layers = std::mem::take(&mut ctx.ui().layers);
|
||||
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() {
|
||||
if match sense {
|
||||
Sense::PressStart(button) => cursor.buttons.select(button).is_start(),
|
||||
Sense::Pressing(button) => cursor.buttons.select(button).is_on(),
|
||||
Sense::PressEnd(button) => cursor.buttons.select(button).is_end(),
|
||||
Sense::HoverStart => hover.is_start(),
|
||||
Sense::Hovering => hover.is_on(),
|
||||
Sense::HoverEnd => hover.is_end(),
|
||||
Sense::Scroll => cursor.scroll_delta != Vec2::ZERO,
|
||||
CursorSense::PressStart(button) => cursor.buttons.select(button).is_start(),
|
||||
CursorSense::Pressing(button) => cursor.buttons.select(button).is_on(),
|
||||
CursorSense::PressEnd(button) => cursor.buttons.select(button).is_end(),
|
||||
CursorSense::HoverStart => hover.is_start(),
|
||||
CursorSense::Hovering => hover.is_on(),
|
||||
CursorSense::HoverEnd => hover.is_end(),
|
||||
CursorSense::Scroll => cursor.scroll_delta != Vec2::ZERO,
|
||||
} {
|
||||
return true;
|
||||
}
|
||||
@@ -269,18 +270,18 @@ impl ActivationState {
|
||||
}
|
||||
}
|
||||
|
||||
impl Event for Senses {
|
||||
type Module<Ctx: 'static> = SensorModule<Ctx>;
|
||||
impl Event for CursorSenses {
|
||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||
type Data = CursorData;
|
||||
}
|
||||
|
||||
impl Event for Sense {
|
||||
type Module<Ctx: 'static> = SensorModule<Ctx>;
|
||||
impl Event for CursorSense {
|
||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||
type Data = CursorData;
|
||||
}
|
||||
|
||||
impl<E: Event<Data = <Senses as Event>::Data> + Into<Senses>, Ctx: 'static> EventModule<E, Ctx>
|
||||
for SensorModule<Ctx>
|
||||
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static> EventModule<E, Ctx>
|
||||
for CursorModule<Ctx>
|
||||
{
|
||||
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
|
||||
@@ -324,44 +325,44 @@ impl<Ctx, Data> Default for SensorGroup<Ctx, Data> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for Senses {
|
||||
type Target = Vec<Sense>;
|
||||
impl Deref for CursorSenses {
|
||||
type Target = Vec<CursorSense>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Senses {
|
||||
impl DerefMut for CursorSenses {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sense> for Senses {
|
||||
fn from(val: Sense) -> Self {
|
||||
Senses(vec![val])
|
||||
impl From<CursorSense> for CursorSenses {
|
||||
fn from(val: CursorSense) -> Self {
|
||||
CursorSenses(vec![val])
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr for Sense {
|
||||
type Output = Senses;
|
||||
impl BitOr for CursorSense {
|
||||
type Output = CursorSenses;
|
||||
|
||||
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;
|
||||
|
||||
fn bitor(mut self, rhs: Sense) -> Self::Output {
|
||||
fn bitor(mut self, rhs: CursorSense) -> Self::Output {
|
||||
self.0.push(rhs);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Default for SensorModule<Ctx> {
|
||||
impl<Ctx> Default for CursorModule<Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
map: Default::default(),
|
||||
|
||||
@@ -53,7 +53,7 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
}
|
||||
|
||||
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);
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user