Skip to content

Remove event::Raw in favour of using event::Input #855

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

Merged
merged 5 commits into from
Nov 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pistoncore-event_loop = { version = "0.26.0", optional = true }
piston2d-gfx_graphics = { version = "0.31.1", optional = true }
piston-texture = { version = "0.5.0", optional = true }
shader_version = { version = "0.2.0", optional = true }
pistoncore-glutin_window = { version = "0.31.0", optional = true }
pistoncore-glutin_window = { version = "0.32.0", optional = true }

[features]
default = ["piston"]
Expand Down
4 changes: 3 additions & 1 deletion examples/all_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ fn main() {
let mut app = support::DemoApp::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().theme(support::theme()).build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64])
.theme(support::theme())
.build();

// Add a `Font` to the `Ui`'s `font::Map` from file.
let assets = find_folder::Search::KidsThenParents(3, 5).for_folder("assets").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let mut events = WindowEvents::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// Add a `Font` to the `Ui`'s `font::Map` from file.
let assets = find_folder::Search::KidsThenParents(3, 5).for_folder("assets").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
let mut events = WindowEvents::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// Generate the widget identifiers.
widget_ids!(struct Ids { canvas, counter });
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub fn main() {
let mut events = WindowEvents::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// The `widget_ids` macro is a easy, safe way of generating a type for producing `widget::Id`s.
widget_ids! {
Expand Down
2 changes: 1 addition & 1 deletion examples/file_navigator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let mut events = WindowEvents::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// A unique identifier for each widget.
widget_ids!(struct Ids { canvas, file_navigator });
Expand Down
5 changes: 1 addition & 4 deletions examples/glutin_gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod feature {
let mut app = support::DemoApp::new();

// Create Ui and Ids of widgets to instantiate
let mut ui = conrod::UiBuilder::new().theme(support::theme()).build();
let mut ui = conrod::UiBuilder::new([WIN_W as f64, WIN_H as f64]).theme(support::theme()).build();
let ids = support::Ids::new(ui.widget_id_generator());

// Load font from file
Expand Down Expand Up @@ -209,9 +209,6 @@ mod feature {

let dpi_factor = window.hidpi_factor();

let dt_secs = 0.0;
ui.handle_event(conrod::event::render(dt_secs, win_w, win_h, dpi_factor as conrod::Scalar));

if let Some(mut primitives) = ui.draw_if_changed() {
let (screen_width, screen_height) = (win_w as f32 * dpi_factor, win_h as f32 * dpi_factor);
let mut vertices = Vec::new();
Expand Down
10 changes: 2 additions & 8 deletions examples/glutin_glium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod feature {
let mut app = support::DemoApp::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().theme(support::theme()).build();
let mut ui = conrod::UiBuilder::new([WIN_W as f64, WIN_H as f64]).theme(support::theme()).build();

// The `widget::Id` of each widget instantiated in `support::gui`.
let ids = support::Ids::new(ui.widget_id_generator());
Expand Down Expand Up @@ -81,12 +81,6 @@ mod feature {
// - Repeat.
'main: loop {

// Construct a render event for conrod at the beginning of rendering.
// NOTE: This will be removed in a future version of conrod as Render events shouldn't
// be necessary.
let window = display.get_window().unwrap();
ui.handle_event(conrod::backend::glutin::render_event(window).unwrap());

// Poll for events.
for event in display.poll_events() {

Expand Down Expand Up @@ -115,7 +109,7 @@ mod feature {
let (win_w, win_h) = (win_rect.w() as u32, win_rect.h() as u32);
let (w, h) = display.get_window().unwrap().get_inner_size_points().unwrap();
if w != win_w || h != win_h {
let event: conrod::event::Raw = conrod::event::Input::Resize(w, h).into();
let event = conrod::event::Input::Resize(w, h);
ui.handle_event(event);
}
}
Expand Down
8 changes: 6 additions & 2 deletions examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
let mut events = WindowEvents::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// Create an empty texture to pass for the text cache as we're not drawing any text.
let mut text_texture_cache = piston::window::GlyphCache::new(&mut window, 0, 0);
Expand All @@ -47,7 +47,11 @@ fn main() {

// Poll events from the window.
while let Some(event) = window.next_event(&mut events) {
ui.handle_event(event.clone());

// Convert the piston event to a conrod input event.
if let Some(e) = piston::window::convert_event(event.clone(), &window) {
ui.handle_event(e);
}

window.draw_2d(&event, |c, g| {
if let Some(primitives) = ui.draw_if_changed() {
Expand Down
2 changes: 1 addition & 1 deletion examples/image_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
let mut events = WindowEvents::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// Add a `Font` to the `Ui`'s `font::Map` from file.
let assets = find_folder::Search::KidsThenParents(3, 5).for_folder("assets").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
let mut events = WindowEvents::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// Unique identifier for each widget.
let ids = Ids::new(ui.widget_id_generator());
Expand Down
2 changes: 1 addition & 1 deletion examples/list_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() {
let mut events = WindowEvents::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// A unique identifier for each widget.
let ids = Ids::new(ui.widget_id_generator());
Expand Down
2 changes: 1 addition & 1 deletion examples/old_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn main() {
let mut events = WindowEvents::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// Identifiers used for instantiating our widgets.
let mut ids = Ids::new(ui.widget_id_generator());
Expand Down
6 changes: 4 additions & 2 deletions examples/plot_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ widget_ids! {
}

fn main() {
const WIDTH: u32 = 720;
const HEIGHT: u32 = 360;

// Construct the window.
let mut window: Window =
piston::window::WindowSettings::new("PlotPath Demo", [720, 360])
piston::window::WindowSettings::new("PlotPath Demo", [WIDTH, HEIGHT])
.opengl(OpenGL::V3_2)
.samples(4)
.exit_on_esc(true)
Expand All @@ -23,7 +25,7 @@ fn main() {
let mut events = WindowEvents::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// A unique identifier for each widget.
let ids = Ids::new(ui.widget_id_generator());
Expand Down
6 changes: 4 additions & 2 deletions examples/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ widget_ids! {


fn main() {
const WIDTH: u32 = 400;
const HEIGHT: u32 = 720;

// Change this to OpenGL::V2_1 if not working.
let opengl = OpenGL::V3_2;

// Construct the window.
let mut window: Window =
piston::window::WindowSettings::new("Primitives Demo", [400, 720])
piston::window::WindowSettings::new("Primitives Demo", [WIDTH, HEIGHT])
.opengl(opengl).samples(4).exit_on_esc(true).build().unwrap();

// Create the event loop.
let mut events = WindowEvents::new();

// construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// A unique identifier for each widget.
let ids = Ids::new(ui.widget_id_generator());
Expand Down
2 changes: 1 addition & 1 deletion examples/range_slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() {
let mut events = WindowEvents::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// A unique identifier for each widget.
let ids = Ids::new(ui.widget_id_generator());
Expand Down
2 changes: 1 addition & 1 deletion examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
let mut events = WindowEvents::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// A unique identifier for each widget.
let ids = Ids::new(ui.widget_id_generator());
Expand Down
2 changes: 1 addition & 1 deletion examples/text_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
let mut events = WindowEvents::new();

// Construct our `Ui`.
let mut ui = conrod::UiBuilder::new().build();
let mut ui = conrod::UiBuilder::new([WIDTH as f64, HEIGHT as f64]).build();

// A unique identifier for each widget.
let ids = Ids::new(ui.widget_id_generator());
Expand Down
23 changes: 4 additions & 19 deletions src/backend/glutin.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! A function for converting a `glutin::Event` to a `conrod::event::Raw`.
//! A function for converting a `glutin::Event` to a `conrod::event::Input`.
//!
//! The following is adapted from the piston `glutin_window` crate.

extern crate glutin;

use Scalar;
use event::{self, Input, Motion};
use event::{Input, Motion};
use input;
use std;

/// A function for converting a `glutin::Event` to a `conrod::event::Raw`.
pub fn convert<W>(e: glutin::Event, window: W) -> Option<event::Raw>
/// A function for converting a `glutin::Event` to a `conrod::event::Input`.
pub fn convert<W>(e: glutin::Event, window: W) -> Option<Input>
where W: std::ops::Deref<Target=glutin::Window>,
{

Expand Down Expand Up @@ -96,21 +96,6 @@ pub fn convert<W>(e: glutin::Event, window: W) -> Option<event::Raw>
}
}

/// Creates a `event::Raw::Render`.
///
/// Returns `None` if the window is no longer open.
///
/// NOTE: This will be removed in a future version of conrod as Render events shouldn't be
/// necessary.
pub fn render_event<W>(window: W) -> Option<event::Raw>
where W: std::ops::Deref<Target=glutin::Window>,
{
window.get_inner_size_pixels().map(|(win_w, win_h)| {
let dpi_factor = window.hidpi_factor();
event::render(0.0, win_w, win_h, dpi_factor as Scalar)
})
}

/// Maps Glutin's key to a conrod `Key`.
pub fn map_key(keycode: glutin::VirtualKeyCode) -> input::keyboard::Key {
use input::keyboard::Key;
Expand Down
39 changes: 16 additions & 23 deletions src/backend/piston/event.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,60 @@
//! A backend for converting piston events to conrod's `event::Raw` type.
//!
//! The module also allows provides an `EventWindow` impl for the default piston game event loop
//! from pistoncore-event_loop, allowing compatibility with conrod's custom piston `Window`.
//! A backend for converting piston events to conrod's `Input` type.

use {Point, Scalar};
use event::{self, Input, Motion, RawEvent};

use event::{Input, Motion};
pub use piston_input::{GenericEvent, UpdateEvent};


/// Converts any `GenericEvent` to a `Raw` conrod event.
pub fn convert<E>(event: E, win_w: Scalar, win_h: Scalar) -> Option<event::Raw>
/// Converts any `GenericEvent` to an `Input` event for conrod.
///
/// The given `width` and `height` must be `Scalar` (DPI agnostic) values.
pub fn convert<E>(event: E, win_w: Scalar, win_h: Scalar) -> Option<Input>
where E: GenericEvent,
{
// Translate the coordinates from top-left-origin-with-y-down to centre-origin-with-y-up.
let translate_coords = |xy: Point| (xy[0] - win_w / 2.0, -(xy[1] - win_h / 2.0));

if let Some(args) = event.render_args() {
return Some(RawEvent::Render(args));
}

if let Some(xy) = event.mouse_cursor_args() {
let (x, y) = translate_coords(xy);
return Some(Input::Move(Motion::MouseCursor(x, y)).into());
return Some(Input::Move(Motion::MouseCursor(x, y)));
}

if let Some(rel_xy) = event.mouse_relative_args() {
let (rel_x, rel_y) = translate_coords(rel_xy);
return Some(Input::Move(Motion::MouseRelative(rel_x, rel_y)).into());
return Some(Input::Move(Motion::MouseRelative(rel_x, rel_y)));
}

if let Some(xy) = event.mouse_scroll_args() {
// Invert the scrolling of the *y* axis as *y* is up in conrod.
let (x, y) = (xy[0], -xy[1]);
return Some(Input::Move(Motion::MouseScroll(x, y)).into());
return Some(Input::Move(Motion::MouseScroll(x, y)));
}

if let Some(args) = event.controller_axis_args() {
return Some(Input::Move(Motion::ControllerAxis(args)).into());
return Some(Input::Move(Motion::ControllerAxis(args)));
}

if let Some(button) = event.press_args() {
return Some(Input::Press(button).into());
return Some(Input::Press(button));
}

if let Some(button) = event.release_args() {
return Some(Input::Release(button).into());
return Some(Input::Release(button));
}

if let Some(text) = event.text_args() {
return Some(Input::Text(text).into());
return Some(Input::Text(text));
}

if let Some(dim) = event.resize_args() {
return Some(Input::Resize(dim[0], dim[1]).into());
return Some(Input::Resize(dim[0], dim[1]));
}

if let Some(b) = event.focus_args() {
return Some(Input::Focus(b).into());
return Some(Input::Focus(b));
}

if let Some(b) = event.cursor_args() {
return Some(Input::Cursor(b).into());
return Some(Input::Cursor(b));
}

None
Expand Down
2 changes: 1 addition & 1 deletion src/backend/piston/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl EventWindow<WindowEvents> for Window {
}

/// Converts any `GenericEvent` to a `Raw` conrod event.
pub fn convert_event<E, B>(event: E, window: &Window<B>) -> Option<event::Raw>
pub fn convert_event<E, B>(event: E, window: &Window<B>) -> Option<event::Input>
where E: GenericEvent,
B: BasicWindow,
{
Expand Down
Loading