Skip to content

Commit

Permalink
Add vizia_input
Browse files Browse the repository at this point in the history
  • Loading branch information
kdecay authored and geom3trik committed Aug 13, 2022
1 parent daaf1e6 commit dd74e09
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 45 deletions.
3 changes: 2 additions & 1 deletion crates/vizia_baseview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ description = "Baseview backend for vizia"

[dependencies]
vizia_core = { path = "../vizia_core" }
vizia_input = { path = "../vizia_input" }

baseview = { git = "https://github.com/RustAudio/baseview.git", rev = "b3712638bacb3fdf2883cb5aa3f6caed0e91ac8c", features = ["opengl"] }
keyboard-types = { version = "0.6.2", default-features = false }
raw-window-handle = "0.4"
femtovg = { git = "https://github.com/femtovg/femtovg", rev = "87fe627794f3f793d8e3a338c838a2f8e8a8aa9d", default-features = false }
lazy_static = "1.4.0"
4 changes: 2 additions & 2 deletions crates/vizia_baseview/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ impl ApplicationRunner {
},
baseview::Event::Keyboard(event) => {
let (s, pressed) = match event.state {
keyboard_types::KeyState::Down => (MouseButtonState::Pressed, true),
keyboard_types::KeyState::Up => (MouseButtonState::Released, false),
vizia_input::KeyState::Down => (MouseButtonState::Pressed, true),
vizia_input::KeyState::Up => (MouseButtonState::Released, false),
};

match event.code {
Expand Down
5 changes: 3 additions & 2 deletions crates/vizia_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ x11 = ["copypasta?/x11"]
wayland = ["copypasta?/wayland"]

[dependencies]
vizia_derive = {version = "0.1.0", path = "../vizia_derive"}
vizia_derive = { path = "../vizia_derive" }
vizia_storage = { path = "../vizia_storage" }
vizia_id = { path = "../vizia_id" }
vizia_input = { path = "../vizia_input" }

femtovg = { git = "https://github.com/femtovg/femtovg", rev = "87fe627794f3f793d8e3a338c838a2f8e8a8aa9d", default-features = false, features = ["image-loading"] }
image = { version = "0.24.0", default-features = false, features = ["png"] } # inherited from femtovg
morphorm = {git = "https://github.com/vizia/morphorm", features = ["rounding"], rev = "c14e888b1d66719fc3f13ccdb1ea01a10368ef9e" }
bitflags = "1.3.2"
fnv = "1.0.7"
keyboard-types = { version = "0.6.2", default-features = false }
fluent-bundle = "0.15.2"
fluent-langneg = "0.13"
fluent-syntax = "0.11.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/vizia_core/src/context/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use morphorm::Units;

use crate::cache::{BoundingBox, CachedData};
use crate::events::ViewHandler;
use crate::input::{Modifiers, MouseState};
use crate::prelude::*;
use crate::resource::ResourceManager;
use crate::state::ModelDataStore;
use crate::style::{LinearGradient, Style};
use crate::text::Selection;
use vizia_input::{Modifiers, MouseState};
use vizia_storage::SparseSet;

/// Cached data used for drawing.
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct DrawContext<'a> {
pub resource_manager: &'a ResourceManager,
pub text_context: &'a TextContext,
pub modifiers: &'a Modifiers,
pub mouse: &'a MouseState,
pub mouse: &'a MouseState<Entity>,
}

macro_rules! style_getter_units {
Expand Down
4 changes: 2 additions & 2 deletions crates/vizia_core/src/context/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use fnv::FnvHashMap;

use crate::cache::CachedData;
use crate::events::ViewHandler;
use crate::input::{Modifiers, MouseState};
use crate::prelude::*;
use crate::resource::ResourceManager;
use crate::state::ModelDataStore;
use crate::style::Style;
use vizia_id::GenerationalId;
use vizia_input::{Modifiers, MouseState};
use vizia_storage::SparseSet;

#[cfg(feature = "clipboard")]
Expand All @@ -38,7 +38,7 @@ pub struct EventContext<'a> {
pub resource_manager: &'a ResourceManager,
pub text_context: &'a TextContext,
pub modifiers: &'a Modifiers,
pub mouse: &'a MouseState,
pub mouse: &'a MouseState<Entity>,
pub(crate) event_queue: &'a mut VecDeque<Event>,
cursor_icon_locked: &'a mut bool,
#[cfg(feature = "clipboard")]
Expand Down
4 changes: 2 additions & 2 deletions crates/vizia_core/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ pub use proxy::*;
use crate::cache::CachedData;
use crate::environment::Environment;
use crate::events::ViewHandler;
use crate::input::{Modifiers, MouseState};
use crate::prelude::*;
use crate::resource::{FontOrId, ImageOrId, ImageRetentionPolicy, ResourceManager, StoredImage};
use crate::state::{BindingHandler, ModelDataStore};
use crate::style::Style;
use crate::tree::TreeExt;
use vizia_id::{GenerationalId, IdManager};
use vizia_input::{Modifiers, MouseState};
use vizia_storage::SparseSet;

static DEFAULT_THEME: &str = include_str!("../../resources/themes/default_theme.css");
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct Context {

pub(crate) canvases: HashMap<Entity, crate::prelude::Canvas>,
//environment: Environment,
pub(crate) mouse: MouseState,
pub(crate) mouse: MouseState<Entity>,
pub(crate) modifiers: Modifiers,

pub(crate) captured: Entity,
Expand Down
6 changes: 0 additions & 6 deletions crates/vizia_core/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,5 @@ pub use chord::*;
mod keymap;
pub use keymap::*;

mod modifiers;
pub use modifiers::*;

mod mouse;
pub use mouse::*;

mod entry;
pub use entry::*;
7 changes: 2 additions & 5 deletions crates/vizia_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ pub mod prelude {
pub use super::environment::{Environment, EnvironmentEvent};
pub use super::events::{Event, Message, Propagation};
pub use super::handle::Handle;
pub use super::input::{
KeyChord, Keymap, KeymapEntry, KeymapEvent, Modifiers, MouseButton, MouseButtonState,
};
pub use super::input::{KeyChord, Keymap, KeymapEntry, KeymapEvent};
pub use super::localization::Localized;
pub use super::modifiers::Actions;
pub use super::state::{Binding, Data, Lens, LensExt, Model, Res};
Expand All @@ -58,15 +56,14 @@ pub mod prelude {
pub use super::window::{
CursorIcon, WindowDescription, WindowEvent, WindowModifiers, WindowSize,
};

pub use vizia_derive::{Data, Lens};
pub use vizia_input::{Code, Key, Modifiers, MouseButton, MouseButtonState};

pub use super::style::{
Abilities, BorderCornerShape, Color, Display, GradientDirection, GradientStop,
LinearGradient, Opacity, Overflow, PseudoClass, Visibility,
};

pub use keyboard_types::{Code, Key};
pub use morphorm::Units::*;
pub use morphorm::{GeometryChanged, LayoutType, PositionType, Units};
pub use unic_langid::LanguageIdentifier;
Expand Down
6 changes: 2 additions & 4 deletions crates/vizia_core/src/views/list.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::marker::PhantomData;

use keyboard_types::Code;

use crate::prelude::*;
use crate::state::{Index, Then};
use std::marker::PhantomData;
use vizia_input::Code;

/// A view for creating a list of items from a binding to a Vec<T>
pub struct List<L, T: 'static>
Expand Down
2 changes: 1 addition & 1 deletion crates/vizia_core/src/views/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::text::{
EditableText, Movement, Selection,
};
use crate::tree::TreeExt;
use keyboard_types::Code;
use std::sync::Arc;
use vizia_id::GenerationalId;
use vizia_input::Code;

#[derive(Lens)]
pub struct TextboxData {
Expand Down
2 changes: 1 addition & 1 deletion crates/vizia_core/src/window/window_event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use keyboard_types::{Code, Key};
use morphorm::GeometryChanged;
use vizia_input::{Code, Key};

use crate::prelude::*;
use crate::window::Position;
Expand Down
15 changes: 15 additions & 0 deletions crates/vizia_input/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "vizia_input"
version = "0.1.0"
authors = ["George Atkinson"]
license = "MIT"
repository = "https://github.com/vizia/vizia"
edition = "2021"
description = "The input components of vizia"
rust-version = "1.60"

[dependencies]
vizia_id = { path = "../vizia_id" }

keyboard-types = { version = "0.6.2", default-features = false }
bitflags = "1.3.2"
7 changes: 7 additions & 0 deletions crates/vizia_input/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod modifiers;
pub use modifiers::*;

mod mouse;
pub use mouse::*;

pub use keyboard_types::{Code, Key, KeyState};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::entity::Entity;
use vizia_id::GenerationalId;

/// A mouse button.
Expand Down Expand Up @@ -29,34 +28,43 @@ pub enum MouseButtonState {

/// Data which describes the current state of a mouse button.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct MouseButtonData {
pub struct MouseButtonData<I>
where
I: GenerationalId,
{
/// The state of the mouse button (pressed/released).
pub state: MouseButtonState,
/// The position of the mouse cursor when the mouse button was last pressed.
pub pos_down: (f32, f32),
/// The position of the mouse cursor when the mouse button was last released.
pub pos_up: (f32, f32),
/// The hovered entity when the mouse button was last pressed.
pub pressed: Entity,
pub pressed: I,
/// The hovered entity when the mouse button was last released.
pub released: Entity,
pub released: I,
}

impl Default for MouseButtonData {
impl<I> Default for MouseButtonData<I>
where
I: GenerationalId,
{
fn default() -> Self {
MouseButtonData {
state: MouseButtonState::Released,
pos_down: (0.0, 0.0),
pos_up: (0.0, 0.0),
pressed: Entity::null(),
released: Entity::null(),
pressed: I::null(),
released: I::null(),
}
}
}

/// The current state of the mouse cursor and buttons.
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct MouseState {
pub struct MouseState<I>
where
I: GenerationalId,
{
/// The horizontal mouse cursor position of the frame.
pub cursorx: f32,
/// The vertical mouse cursor position of the frame.
Expand All @@ -66,14 +74,17 @@ pub struct MouseState {
/// The vertical mouse cursor position of the previous frame.
pub previous_cursory: f32,
/// The state of the left mouse button.
pub left: MouseButtonData,
pub left: MouseButtonData<I>,
/// The state of the right mouse button.
pub right: MouseButtonData,
pub right: MouseButtonData<I>,
/// The state of the middle mouse button.
pub middle: MouseButtonData,
pub middle: MouseButtonData<I>,
}

impl Default for MouseState {
impl<I> Default for MouseState<I>
where
I: GenerationalId,
{
fn default() -> Self {
MouseState {
cursorx: -1.0,
Expand All @@ -87,7 +98,10 @@ impl Default for MouseState {
}
}

impl MouseState {
impl<I> MouseState<I>
where
I: GenerationalId,
{
/// Returns the delta of the mouse cursor position of the current and previous frame.
pub fn frame_delta(&self) -> (f32, f32) {
(self.cursorx - self.previous_cursorx, self.cursory - self.previous_cursory)
Expand Down
5 changes: 3 additions & 2 deletions crates/vizia_winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ x11 = ["winit/x11", "glutin?/x11"]
wayland = ["winit/wayland", "winit/wayland-dlopen", "glutin?/wayland", "glutin?/wayland-dlopen"]

[dependencies]
vizia_input = { path = "../vizia_input" }
vizia_core = { path = "../vizia_core" }

winit = { version = "0.26.1", default-features = false }
femtovg = { git = "https://github.com/femtovg/femtovg", rev = "87fe627794f3f793d8e3a338c838a2f8e8a8aa9d", default-features = false }
keyboard-types = { version = "0.6.2", default-features = false }
vizia_core = { path = "../vizia_core", version = "0.1" }
glutin = { version = "0.28.0", default-features = false, optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/vizia_winit/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use keyboard_types::Code as ViziaCode;
use keyboard_types::Key as ViziaKey;
use vizia_core::prelude::CursorIcon as ViziaCursorIcon;
use vizia_input::Code as ViziaCode;
use vizia_input::Key as ViziaKey;
use winit::event::VirtualKeyCode as WinitVirtualKeyCode;
use winit::window::CursorIcon as WinitCursorIcon;

Expand Down

0 comments on commit dd74e09

Please sign in to comment.