Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor icon support through Game trait #112

Merged
merged 7 commits into from
Oct 26, 2019
Merged
7 changes: 6 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod r#loop;

pub(crate) use r#loop::Loop;

use crate::graphics::{Frame, Window, WindowSettings};
use crate::graphics::{CursorIcon, Frame, Window, WindowSettings};
use crate::input::{keyboard, Input};
use crate::load::{LoadingScreen, Task};
use crate::{Debug, Result, Timer};
Expand Down Expand Up @@ -124,6 +124,11 @@ pub trait Game {
/// [`Window`]: graphics/struct.Window.html
fn update(&mut self, _window: &Window) {}

/// TODO!
fn cursor_icon(&self) -> CursorIcon {
CursorIcon::Default
}

/// Displays debug information.
///
/// This method is called after [`draw`] once per frame when debug has been
Expand Down
21 changes: 16 additions & 5 deletions src/game/loop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::debug::Debug;
use crate::graphics::window::winit;
use crate::graphics::{Window, WindowSettings};
use crate::graphics::{CursorIcon, Window, WindowSettings};
use crate::input::{self, gamepad, keyboard, mouse, window, Input};
use crate::load::{Join, LoadingScreen, Task};
use crate::{Result, Timer};
Expand Down Expand Up @@ -204,7 +204,9 @@ fn try_into_input_event(
}
}

pub struct Default {}
pub struct Default {
cursor_icon: CursorIcon,
}

impl<Game: super::Game> Loop<Game> for Default
where
Expand All @@ -217,7 +219,9 @@ where
_game: &mut Game,
_window: &Window,
) -> Self {
Self {}
Self {
cursor_icon: CursorIcon::default(),
}
}

fn load(_window: &Window) -> Task<Self::Attributes> {
Expand All @@ -226,10 +230,17 @@ where

fn after_draw(
&mut self,
_game: &mut Game,
game: &mut Game,
_input: &mut Game::Input,
_window: &mut Window,
window: &mut Window,
_debug: &mut Debug,
) {
// Update the cursor icon if it has changed
let game_cursor_icon = game.cursor_icon();
if self.cursor_icon != game_cursor_icon {
window.set_cursor_visible(game_cursor_icon != CursorIcon::Hidden);
window.update_cursor(game_cursor_icon.into());
self.cursor_icon = game_cursor_icon;
}
}
}
2 changes: 1 addition & 1 deletion src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ pub use text::{HorizontalAlignment, Text, VerticalAlignment};
pub use texture_array::TextureArray;
pub use transformation::Transformation;
pub use vector::Vector;
pub use window::{Frame, Settings as WindowSettings, Window};
pub use window::{CursorIcon, Frame, Settings as WindowSettings, Window};
6 changes: 6 additions & 0 deletions src/graphics/window.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
mod cursor_icon;
mod frame;
mod settings;

pub(crate) use winit;

pub use cursor_icon::CursorIcon;
pub use frame::Frame;
pub use settings::Settings;

Expand Down Expand Up @@ -135,6 +137,10 @@ impl Window {
) {
self.surface.window().set_cursor_icon(new_cursor);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, we can make this take an Option<winit::Window::CursorIcon> and remove set_cursor_visible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 66828da.


pub(crate) fn set_cursor_visible(&self, visible: bool) {
self.surface.window().set_cursor_visible(visible);
}
}

impl std::fmt::Debug for Window {
Expand Down
37 changes: 37 additions & 0 deletions src/graphics/window/cursor_icon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::graphics::window::winit;

/// TODO
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CursorIcon {
/// The platform-dependent default cursor.
Default,
/// A simple crosshair.
Crosshair,
/// A hand (often used to indicate links in web browsers).
Hand,
/// Hides the cursor.
Hidden,
/// Indicates something is to be moved.
Move,
}

impl Default for CursorIcon {
fn default() -> Self {
Self::Default
}
}

impl From<CursorIcon> for winit::window::CursorIcon {
fn from(cursor_icon: CursorIcon) -> winit::window::CursorIcon {
match cursor_icon {
// If the cursor is hidden, it doesn't matter which type it is, so the default makes
// the most sense.
CursorIcon::Default | CursorIcon::Hidden => {
winit::window::CursorIcon::Default
}
CursorIcon::Crosshair => winit::window::CursorIcon::Crosshair,
CursorIcon::Hand => winit::window::CursorIcon::Hand,
CursorIcon::Move => winit::window::CursorIcon::Move,
}
}
}
33 changes: 24 additions & 9 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub type Panel<'a, Message> = widget::Panel<'a, Message, Renderer>;
pub type Element<'a, Message> = self::core::Element<'a, Message, Renderer>;

use crate::game::{self, Loop as _};
use crate::graphics::{Point, Window, WindowSettings};
use crate::graphics::{CursorIcon, Point, Window, WindowSettings};
use crate::input::{self, mouse, Input as _};
use crate::load::Task;
use crate::ui::core::{Event, Interface, MouseCursor, Renderer as _};
Expand Down Expand Up @@ -277,7 +277,8 @@ pub trait UserInterface: Game {
struct Loop<UI: UserInterface> {
renderer: UI::Renderer,
messages: Vec<UI::Message>,
mouse_cursor: MouseCursor,
game_cursor: CursorIcon,
ui_cursor: MouseCursor,
cache: Option<core::Cache>,
cursor_position: Point,
events: Vec<Event>,
Expand All @@ -291,7 +292,8 @@ impl<UI: UserInterface> game::Loop<UI> for Loop<UI> {
Loop {
renderer,
messages: Vec::new(),
mouse_cursor: MouseCursor::OutOfBounds,
game_cursor: CursorIcon::Default,
ui_cursor: MouseCursor::OutOfBounds,
cache: Some(cache),
cursor_position: Point::new(0.0, 0.0),
events: Vec::new(),
Expand Down Expand Up @@ -338,23 +340,36 @@ impl<UI: UserInterface> game::Loop<UI> for Loop<UI> {
interface.on_event(event, cursor_position, messages)
});

let new_cursor = interface.draw(
let new_ui_cursor = interface.draw(
&mut self.renderer,
&mut window.frame(),
cursor_position,
);

self.cache = Some(interface.cache());

if new_cursor != self.mouse_cursor {
if new_cursor == MouseCursor::OutOfBounds {
if new_ui_cursor != self.ui_cursor {
if new_ui_cursor == MouseCursor::OutOfBounds {
input.update(input::Event::Mouse(mouse::Event::CursorReturned));
} else if self.mouse_cursor == MouseCursor::OutOfBounds {
} else if self.ui_cursor == MouseCursor::OutOfBounds {
input.update(input::Event::Mouse(mouse::Event::CursorTaken));
}

window.update_cursor(new_cursor.into());
self.mouse_cursor = new_cursor;
self.ui_cursor = new_ui_cursor;
}
let new_game_cursor = ui.cursor_icon();
if new_game_cursor != self.game_cursor {
self.game_cursor = new_game_cursor;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may be able to move this inside the if branch below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 13429f2.


// TODO: Only update the cursor if it has changed once `MouseCursor` & `CursorIcon` merges
// Use the game cursor if cursor is not on a UI element, use the mouse cursor otherwise
if self.ui_cursor == MouseCursor::OutOfBounds {
window.set_cursor_visible(self.game_cursor != CursorIcon::Hidden);
window.update_cursor(self.game_cursor.into());
} else {
window.set_cursor_visible(true);
window.update_cursor(self.ui_cursor.into());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid cursor flickering, it may be a good idea to save the last cursor and visibility in graphics::Window and only actually change state if necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 13429f2.

}

for message in messages.drain(..) {
Expand Down