Move code from glium example into glium library
This commit is contained in:
parent
14db237b1d
commit
4c79064760
5 changed files with 172 additions and 160 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -189,8 +189,10 @@ dependencies = [
|
||||||
name = "emigui_glium"
|
name = "emigui_glium"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"emigui 0.1.0",
|
"emigui 0.1.0",
|
||||||
"glium 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glium 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"webbrowser 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -209,11 +211,9 @@ dependencies = [
|
||||||
name = "example_glium"
|
name = "example_glium"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clipboard 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"emigui 0.1.0",
|
"emigui 0.1.0",
|
||||||
"emigui_glium 0.1.0",
|
"emigui_glium 0.1.0",
|
||||||
"glium 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glium 0.24.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"webbrowser 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -7,4 +7,6 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
emigui = { path = "../emigui" }
|
emigui = { path = "../emigui" }
|
||||||
|
|
||||||
|
clipboard = "0.5"
|
||||||
glium = "0.24"
|
glium = "0.24"
|
||||||
|
webbrowser = "0.5"
|
||||||
|
|
|
@ -3,3 +3,165 @@
|
||||||
mod painter;
|
mod painter;
|
||||||
|
|
||||||
pub use painter::Painter;
|
pub use painter::Painter;
|
||||||
|
|
||||||
|
use {
|
||||||
|
clipboard::{ClipboardContext, ClipboardProvider},
|
||||||
|
emigui::*,
|
||||||
|
glium::glutin::{self, VirtualKeyCode},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn init_clipboard() -> Option<ClipboardContext> {
|
||||||
|
match ClipboardContext::new() {
|
||||||
|
Ok(clipboard) => Some(clipboard),
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Failed to initialize clipboard: {}", err);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn input_event(
|
||||||
|
event: glutin::Event,
|
||||||
|
clipboard: Option<&mut ClipboardContext>,
|
||||||
|
raw_input: &mut RawInput,
|
||||||
|
running: &mut bool,
|
||||||
|
) {
|
||||||
|
use glutin::WindowEvent::*;
|
||||||
|
match event {
|
||||||
|
glutin::Event::WindowEvent { event, .. } => match event {
|
||||||
|
CloseRequested | Destroyed => *running = false,
|
||||||
|
|
||||||
|
DroppedFile(path) => raw_input.dropped_files.push(path),
|
||||||
|
HoveredFile(path) => raw_input.hovered_files.push(path),
|
||||||
|
|
||||||
|
Resized(glutin::dpi::LogicalSize { width, height }) => {
|
||||||
|
raw_input.screen_size = vec2(width as f32, height as f32);
|
||||||
|
}
|
||||||
|
MouseInput { state, .. } => {
|
||||||
|
raw_input.mouse_down = state == glutin::ElementState::Pressed;
|
||||||
|
}
|
||||||
|
CursorMoved { position, .. } => {
|
||||||
|
raw_input.mouse_pos = Some(pos2(position.x as f32, position.y as f32));
|
||||||
|
}
|
||||||
|
CursorLeft { .. } => {
|
||||||
|
raw_input.mouse_pos = None;
|
||||||
|
}
|
||||||
|
ReceivedCharacter(ch) => {
|
||||||
|
raw_input.events.push(Event::Text(ch.to_string()));
|
||||||
|
}
|
||||||
|
KeyboardInput { input, .. } => {
|
||||||
|
if let Some(virtual_keycode) = input.virtual_keycode {
|
||||||
|
// TODO: If mac
|
||||||
|
if input.modifiers.logo && virtual_keycode == VirtualKeyCode::Q {
|
||||||
|
*running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
match virtual_keycode {
|
||||||
|
VirtualKeyCode::Paste => {
|
||||||
|
if let Some(clipboard) = clipboard {
|
||||||
|
match clipboard.get_contents() {
|
||||||
|
Ok(contents) => {
|
||||||
|
raw_input.events.push(Event::Text(contents));
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Paste error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VirtualKeyCode::Copy => raw_input.events.push(Event::Copy),
|
||||||
|
VirtualKeyCode::Cut => raw_input.events.push(Event::Cut),
|
||||||
|
_ => {
|
||||||
|
if let Some(key) = translate_virtual_key_code(virtual_keycode) {
|
||||||
|
raw_input.events.push(Event::Key {
|
||||||
|
key,
|
||||||
|
pressed: input.state == glutin::ElementState::Pressed,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MouseWheel { delta, .. } => {
|
||||||
|
match delta {
|
||||||
|
glutin::MouseScrollDelta::LineDelta(x, y) => {
|
||||||
|
raw_input.scroll_delta = vec2(x, y) * 24.0;
|
||||||
|
}
|
||||||
|
glutin::MouseScrollDelta::PixelDelta(delta) => {
|
||||||
|
// Actually point delta
|
||||||
|
raw_input.scroll_delta = vec2(delta.x as f32, delta.y as f32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: HiDpiFactorChanged
|
||||||
|
_ => {
|
||||||
|
// dbg!(event);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate_virtual_key_code(key: glutin::VirtualKeyCode) -> Option<emigui::Key> {
|
||||||
|
use VirtualKeyCode::*;
|
||||||
|
|
||||||
|
Some(match key {
|
||||||
|
Escape => Key::Escape,
|
||||||
|
Insert => Key::Insert,
|
||||||
|
Home => Key::Home,
|
||||||
|
Delete => Key::Delete,
|
||||||
|
End => Key::End,
|
||||||
|
PageDown => Key::PageDown,
|
||||||
|
PageUp => Key::PageUp,
|
||||||
|
Left => Key::Left,
|
||||||
|
Up => Key::Up,
|
||||||
|
Right => Key::Right,
|
||||||
|
Down => Key::Down,
|
||||||
|
Back => Key::Backspace,
|
||||||
|
Return => Key::Return,
|
||||||
|
// Space => Key::Space,
|
||||||
|
Tab => Key::Tab,
|
||||||
|
|
||||||
|
LAlt | RAlt => Key::Alt,
|
||||||
|
LShift | RShift => Key::Shift,
|
||||||
|
LControl | RControl => Key::Control,
|
||||||
|
LWin | RWin => Key::Logo,
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate_cursor(cursor_icon: emigui::CursorIcon) -> glutin::MouseCursor {
|
||||||
|
match cursor_icon {
|
||||||
|
CursorIcon::Default => glutin::MouseCursor::Default,
|
||||||
|
CursorIcon::PointingHand => glutin::MouseCursor::Hand,
|
||||||
|
CursorIcon::ResizeNwSe => glutin::MouseCursor::NwseResize,
|
||||||
|
CursorIcon::Text => glutin::MouseCursor::Text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_output(
|
||||||
|
output: emigui::Output,
|
||||||
|
display: &glium::backend::glutin::Display,
|
||||||
|
clipboard: Option<&mut ClipboardContext>,
|
||||||
|
) {
|
||||||
|
if let Some(url) = output.open_url {
|
||||||
|
if let Err(err) = webbrowser::open(&url) {
|
||||||
|
eprintln!("Failed to open url: {}", err); // TODO show error in imgui
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !output.copied_text.is_empty() {
|
||||||
|
if let Some(clipboard) = clipboard {
|
||||||
|
if let Err(err) = clipboard.set_contents(output.copied_text) {
|
||||||
|
eprintln!("Copy/Cut error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
display
|
||||||
|
.gl_window()
|
||||||
|
.set_cursor(translate_cursor(output.cursor_icon));
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,4 @@ edition = "2018"
|
||||||
emigui = { path = "../emigui" }
|
emigui = { path = "../emigui" }
|
||||||
emigui_glium = { path = "../emigui_glium" }
|
emigui_glium = { path = "../emigui_glium" }
|
||||||
|
|
||||||
clipboard = "0.5"
|
|
||||||
glium = "0.24"
|
glium = "0.24"
|
||||||
webbrowser = "0.5"
|
|
||||||
|
|
|
@ -3,14 +3,10 @@
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use {
|
use {
|
||||||
clipboard::{ClipboardContext, ClipboardProvider},
|
|
||||||
emigui::{containers::*, example_app::ExampleApp, widgets::*, *},
|
emigui::{containers::*, example_app::ExampleApp, widgets::*, *},
|
||||||
emigui_glium::Painter,
|
glium::glutin,
|
||||||
glium::glutin::{self, VirtualKeyCode},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: move more code into emigui_glium care
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut events_loop = glutin::EventsLoop::new();
|
let mut events_loop = glutin::EventsLoop::new();
|
||||||
let window = glutin::WindowBuilder::new().with_title("Emigui example");
|
let window = glutin::WindowBuilder::new().with_title("Emigui example");
|
||||||
|
@ -28,7 +24,7 @@ fn main() {
|
||||||
let pixels_per_point = display.gl_window().get_hidpi_factor() as f32;
|
let pixels_per_point = display.gl_window().get_hidpi_factor() as f32;
|
||||||
|
|
||||||
let mut emigui = Emigui::new(pixels_per_point);
|
let mut emigui = Emigui::new(pixels_per_point);
|
||||||
let mut painter = Painter::new(&display);
|
let mut painter = emigui_glium::Painter::new(&display);
|
||||||
|
|
||||||
let mut raw_input = emigui::RawInput {
|
let mut raw_input = emigui::RawInput {
|
||||||
screen_size: {
|
screen_size: {
|
||||||
|
@ -39,24 +35,13 @@ fn main() {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut running = true;
|
|
||||||
|
|
||||||
// used to keep track of time for animations
|
// used to keep track of time for animations
|
||||||
let start_time = Instant::now();
|
let start_time = Instant::now();
|
||||||
|
let mut running = true;
|
||||||
let mut frame_start = Instant::now();
|
let mut frame_start = Instant::now();
|
||||||
|
|
||||||
let mut frame_times = std::collections::VecDeque::new();
|
let mut frame_times = std::collections::VecDeque::new();
|
||||||
|
|
||||||
let mut example_app = ExampleApp::default();
|
let mut example_app = ExampleApp::default();
|
||||||
|
let mut clipboard = emigui_glium::init_clipboard();
|
||||||
let mut clipboard: Option<ClipboardContext> = match ClipboardContext::new() {
|
|
||||||
Ok(clipboard) => Some(clipboard),
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Failed to initialize clipboard: {}", err);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
while running {
|
while running {
|
||||||
{
|
{
|
||||||
|
@ -75,7 +60,7 @@ fn main() {
|
||||||
raw_input.hovered_files.clear();
|
raw_input.hovered_files.clear();
|
||||||
raw_input.events.clear();
|
raw_input.events.clear();
|
||||||
events_loop.poll_events(|event| {
|
events_loop.poll_events(|event| {
|
||||||
input_event(event, clipboard.as_mut(), &mut raw_input, &mut running)
|
emigui_glium::input_event(event, clipboard.as_mut(), &mut raw_input, &mut running)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,141 +111,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
painter.paint_batches(&display, paint_batches, emigui.texture());
|
painter.paint_batches(&display, paint_batches, emigui.texture());
|
||||||
|
emigui_glium::handle_output(output, &display, clipboard.as_mut());
|
||||||
let cursor = match output.cursor_icon {
|
|
||||||
CursorIcon::Default => glutin::MouseCursor::Default,
|
|
||||||
CursorIcon::PointingHand => glutin::MouseCursor::Hand,
|
|
||||||
CursorIcon::ResizeNwSe => glutin::MouseCursor::NwseResize,
|
|
||||||
CursorIcon::Text => glutin::MouseCursor::Text,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(url) = output.open_url {
|
|
||||||
if let Err(err) = webbrowser::open(&url) {
|
|
||||||
eprintln!("Failed to open url: {}", err); // TODO show error in imgui
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !output.copied_text.is_empty() {
|
|
||||||
if let Some(clipboard) = clipboard.as_mut() {
|
|
||||||
if let Err(err) = clipboard.set_contents(output.copied_text) {
|
|
||||||
eprintln!("Copy/Cut error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
display.gl_window().set_cursor(cursor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn input_event(
|
|
||||||
event: glutin::Event,
|
|
||||||
clipboard: Option<&mut ClipboardContext>,
|
|
||||||
raw_input: &mut RawInput,
|
|
||||||
running: &mut bool,
|
|
||||||
) {
|
|
||||||
use glutin::WindowEvent::*;
|
|
||||||
match event {
|
|
||||||
glutin::Event::WindowEvent { event, .. } => match event {
|
|
||||||
CloseRequested | Destroyed => *running = false,
|
|
||||||
|
|
||||||
DroppedFile(path) => raw_input.dropped_files.push(path),
|
|
||||||
HoveredFile(path) => raw_input.hovered_files.push(path),
|
|
||||||
|
|
||||||
Resized(glutin::dpi::LogicalSize { width, height }) => {
|
|
||||||
raw_input.screen_size = vec2(width as f32, height as f32);
|
|
||||||
}
|
|
||||||
MouseInput { state, .. } => {
|
|
||||||
raw_input.mouse_down = state == glutin::ElementState::Pressed;
|
|
||||||
}
|
|
||||||
CursorMoved { position, .. } => {
|
|
||||||
raw_input.mouse_pos = Some(pos2(position.x as f32, position.y as f32));
|
|
||||||
}
|
|
||||||
CursorLeft { .. } => {
|
|
||||||
raw_input.mouse_pos = None;
|
|
||||||
}
|
|
||||||
ReceivedCharacter(ch) => {
|
|
||||||
raw_input.events.push(Event::Text(ch.to_string()));
|
|
||||||
}
|
|
||||||
KeyboardInput { input, .. } => {
|
|
||||||
if let Some(virtual_keycode) = input.virtual_keycode {
|
|
||||||
// TODO: If mac
|
|
||||||
if input.modifiers.logo && virtual_keycode == VirtualKeyCode::Q {
|
|
||||||
*running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
match virtual_keycode {
|
|
||||||
VirtualKeyCode::Paste => {
|
|
||||||
if let Some(clipboard) = clipboard {
|
|
||||||
match clipboard.get_contents() {
|
|
||||||
Ok(contents) => {
|
|
||||||
raw_input.events.push(Event::Text(contents));
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Paste error: {}", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VirtualKeyCode::Copy => raw_input.events.push(Event::Copy),
|
|
||||||
VirtualKeyCode::Cut => raw_input.events.push(Event::Cut),
|
|
||||||
_ => {
|
|
||||||
if let Some(key) = translate_virtual_key_code(virtual_keycode) {
|
|
||||||
raw_input.events.push(Event::Key {
|
|
||||||
key,
|
|
||||||
pressed: input.state == glutin::ElementState::Pressed,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MouseWheel { delta, .. } => {
|
|
||||||
match delta {
|
|
||||||
glutin::MouseScrollDelta::LineDelta(x, y) => {
|
|
||||||
raw_input.scroll_delta = vec2(x, y) * 24.0;
|
|
||||||
}
|
|
||||||
glutin::MouseScrollDelta::PixelDelta(delta) => {
|
|
||||||
// Actually point delta
|
|
||||||
raw_input.scroll_delta = vec2(delta.x as f32, delta.y as f32);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO: HiDpiFactorChanged
|
|
||||||
_ => {
|
|
||||||
// dbg!(event);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn translate_virtual_key_code(key: glutin::VirtualKeyCode) -> Option<emigui::Key> {
|
|
||||||
use VirtualKeyCode::*;
|
|
||||||
|
|
||||||
Some(match key {
|
|
||||||
Escape => Key::Escape,
|
|
||||||
Insert => Key::Insert,
|
|
||||||
Home => Key::Home,
|
|
||||||
Delete => Key::Delete,
|
|
||||||
End => Key::End,
|
|
||||||
PageDown => Key::PageDown,
|
|
||||||
PageUp => Key::PageUp,
|
|
||||||
Left => Key::Left,
|
|
||||||
Up => Key::Up,
|
|
||||||
Right => Key::Right,
|
|
||||||
Down => Key::Down,
|
|
||||||
Back => Key::Backspace,
|
|
||||||
Return => Key::Return,
|
|
||||||
// Space => Key::Space,
|
|
||||||
Tab => Key::Tab,
|
|
||||||
|
|
||||||
LAlt | RAlt => Key::Alt,
|
|
||||||
LShift | RShift => Key::Shift,
|
|
||||||
LControl | RControl => Key::Control,
|
|
||||||
LWin | RWin => Key::Logo,
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue