2021-01-02 10:59:20 +00:00
|
|
|
//! [`egui`] bindings for [`glium`](https://github.com/glium/glium).
|
|
|
|
//!
|
|
|
|
//! This library is an [`epi`] backend.
|
|
|
|
//!
|
|
|
|
//! If you are writing an app, you may want to look at [`eframe`](https://docs.rs/eframe) instead.
|
|
|
|
|
2020-10-18 13:40:23 +00:00
|
|
|
#![forbid(unsafe_code)]
|
2020-12-18 21:19:56 +00:00
|
|
|
#![cfg_attr(not(debug_assertions), deny(warnings))] // Forbid warnings in release builds
|
2021-01-02 11:02:26 +00:00
|
|
|
#![warn(clippy::all, rust_2018_idioms)]
|
2021-01-02 15:31:45 +00:00
|
|
|
#![allow(clippy::manual_range_contains, clippy::single_match)]
|
2020-07-22 16:01:27 +00:00
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
mod backend;
|
2020-12-30 19:56:50 +00:00
|
|
|
#[cfg(feature = "http")]
|
|
|
|
pub mod http;
|
2019-04-21 08:13:05 +00:00
|
|
|
mod painter;
|
2021-01-04 00:44:02 +00:00
|
|
|
#[cfg(feature = "persistence")]
|
|
|
|
pub mod persistence;
|
|
|
|
pub mod window_settings;
|
2019-04-21 08:13:05 +00:00
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub use backend::*;
|
2019-04-21 08:13:05 +00:00
|
|
|
pub use painter::Painter;
|
2020-04-29 19:58:14 +00:00
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
use {
|
|
|
|
clipboard::ClipboardProvider,
|
|
|
|
egui::*,
|
|
|
|
glium::glutin::{self, event::VirtualKeyCode, event_loop::ControlFlow},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub use clipboard::ClipboardContext; // TODO: remove
|
2020-04-29 19:58:14 +00:00
|
|
|
|
2020-11-13 10:04:45 +00:00
|
|
|
pub struct GliumInputState {
|
2021-01-25 17:50:19 +00:00
|
|
|
pub pointer_pos_in_points: Option<Pos2>,
|
2021-01-02 15:37:39 +00:00
|
|
|
pub raw: egui::RawInput,
|
2020-11-13 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GliumInputState {
|
|
|
|
pub fn from_pixels_per_point(pixels_per_point: f32) -> Self {
|
|
|
|
Self {
|
2021-01-25 17:50:19 +00:00
|
|
|
pointer_pos_in_points: Default::default(),
|
2020-11-13 10:04:45 +00:00
|
|
|
raw: egui::RawInput {
|
|
|
|
pixels_per_point: Some(pixels_per_point),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
pub fn input_to_egui(
|
2021-02-21 10:23:33 +00:00
|
|
|
pixels_per_point: f32,
|
2021-01-02 11:02:26 +00:00
|
|
|
event: glutin::event::WindowEvent<'_>,
|
2020-04-29 19:58:14 +00:00
|
|
|
clipboard: Option<&mut ClipboardContext>,
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state: &mut GliumInputState,
|
2020-07-22 10:10:14 +00:00
|
|
|
control_flow: &mut ControlFlow,
|
2020-04-29 19:58:14 +00:00
|
|
|
) {
|
2021-01-17 01:31:37 +00:00
|
|
|
use glutin::event::WindowEvent;
|
2020-04-29 19:58:14 +00:00
|
|
|
match event {
|
2021-01-17 01:31:37 +00:00
|
|
|
WindowEvent::CloseRequested | WindowEvent::Destroyed => *control_flow = ControlFlow::Exit,
|
2021-02-21 10:23:33 +00:00
|
|
|
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
|
|
|
|
input_state.raw.pixels_per_point = Some(scale_factor as f32);
|
|
|
|
}
|
2021-01-25 17:50:19 +00:00
|
|
|
WindowEvent::MouseInput { state, button, .. } => {
|
|
|
|
if let Some(pos_in_points) = input_state.pointer_pos_in_points {
|
|
|
|
if let Some(button) = translate_mouse_button(button) {
|
|
|
|
input_state.raw.events.push(egui::Event::PointerButton {
|
|
|
|
pos: pos_in_points,
|
|
|
|
button,
|
|
|
|
pressed: state == glutin::event::ElementState::Pressed,
|
|
|
|
modifiers: input_state.raw.modifiers,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
2021-01-17 01:31:37 +00:00
|
|
|
WindowEvent::CursorMoved {
|
2020-10-17 21:54:46 +00:00
|
|
|
position: pos_in_pixels,
|
|
|
|
..
|
|
|
|
} => {
|
2021-01-25 17:50:19 +00:00
|
|
|
let pos_in_points = pos2(
|
2021-02-21 10:23:33 +00:00
|
|
|
pos_in_pixels.x as f32 / pixels_per_point,
|
|
|
|
pos_in_pixels.y as f32 / pixels_per_point,
|
2021-01-25 17:50:19 +00:00
|
|
|
);
|
|
|
|
input_state.pointer_pos_in_points = Some(pos_in_points);
|
|
|
|
input_state
|
|
|
|
.raw
|
|
|
|
.events
|
|
|
|
.push(egui::Event::PointerMoved(pos_in_points));
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
2021-01-17 01:31:37 +00:00
|
|
|
WindowEvent::CursorLeft { .. } => {
|
2021-01-25 17:50:19 +00:00
|
|
|
input_state.pointer_pos_in_points = None;
|
|
|
|
input_state.raw.events.push(egui::Event::PointerGone);
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
2021-01-17 01:31:37 +00:00
|
|
|
WindowEvent::ReceivedCharacter(ch) => {
|
2021-01-25 17:50:19 +00:00
|
|
|
if is_printable_char(ch)
|
2020-11-15 13:21:21 +00:00
|
|
|
&& !input_state.raw.modifiers.ctrl
|
|
|
|
&& !input_state.raw.modifiers.mac_cmd
|
|
|
|
{
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state.raw.events.push(Event::Text(ch.to_string()));
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
2021-01-17 01:31:37 +00:00
|
|
|
WindowEvent::KeyboardInput { input, .. } => {
|
2020-11-14 20:01:21 +00:00
|
|
|
if let Some(keycode) = input.virtual_keycode {
|
|
|
|
let pressed = input.state == glutin::event::ElementState::Pressed;
|
2020-11-13 10:04:45 +00:00
|
|
|
|
2021-01-25 17:50:19 +00:00
|
|
|
// We could also use `WindowEvent::ModifiersChanged` instead, I guess.
|
2020-11-14 20:01:21 +00:00
|
|
|
if matches!(keycode, VirtualKeyCode::LAlt | VirtualKeyCode::RAlt) {
|
2020-11-15 13:21:21 +00:00
|
|
|
input_state.raw.modifiers.alt = pressed;
|
2020-11-14 20:01:21 +00:00
|
|
|
}
|
|
|
|
if matches!(keycode, VirtualKeyCode::LControl | VirtualKeyCode::RControl) {
|
2020-11-15 13:21:21 +00:00
|
|
|
input_state.raw.modifiers.ctrl = pressed;
|
2020-11-14 20:01:21 +00:00
|
|
|
if !cfg!(target_os = "macos") {
|
2020-11-15 13:21:21 +00:00
|
|
|
input_state.raw.modifiers.command = pressed;
|
2020-11-14 20:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if matches!(keycode, VirtualKeyCode::LShift | VirtualKeyCode::RShift) {
|
2020-11-15 13:21:21 +00:00
|
|
|
input_state.raw.modifiers.shift = pressed;
|
2020-11-14 20:01:21 +00:00
|
|
|
}
|
|
|
|
if cfg!(target_os = "macos")
|
|
|
|
&& matches!(keycode, VirtualKeyCode::LWin | VirtualKeyCode::RWin)
|
|
|
|
{
|
2020-11-15 13:21:21 +00:00
|
|
|
input_state.raw.modifiers.mac_cmd = pressed;
|
|
|
|
input_state.raw.modifiers.command = pressed;
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
2020-04-29 19:58:14 +00:00
|
|
|
|
2020-11-14 20:01:21 +00:00
|
|
|
if pressed {
|
2020-11-13 10:04:45 +00:00
|
|
|
if cfg!(target_os = "macos")
|
2020-11-15 13:21:21 +00:00
|
|
|
&& input_state.raw.modifiers.mac_cmd
|
2020-11-14 20:01:21 +00:00
|
|
|
&& keycode == VirtualKeyCode::Q
|
2020-11-13 10:04:45 +00:00
|
|
|
{
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// VirtualKeyCode::Paste etc in winit are broken/untrustworthy,
|
|
|
|
// so we detect these things manually:
|
2020-11-15 13:21:21 +00:00
|
|
|
if input_state.raw.modifiers.command && keycode == VirtualKeyCode::X {
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state.raw.events.push(Event::Cut);
|
2020-11-15 13:21:21 +00:00
|
|
|
} else if input_state.raw.modifiers.command && keycode == VirtualKeyCode::C {
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state.raw.events.push(Event::Copy);
|
2020-11-15 13:21:21 +00:00
|
|
|
} else if input_state.raw.modifiers.command && keycode == VirtualKeyCode::V {
|
2020-07-21 22:36:17 +00:00
|
|
|
if let Some(clipboard) = clipboard {
|
|
|
|
match clipboard.get_contents() {
|
|
|
|
Ok(contents) => {
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state.raw.events.push(Event::Text(contents));
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Paste error: {}", err);
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 17:05:50 +00:00
|
|
|
|
|
|
|
if let Some(key) = translate_virtual_key_code(keycode) {
|
|
|
|
input_state.raw.events.push(Event::Key {
|
|
|
|
key,
|
|
|
|
pressed,
|
|
|
|
modifiers: input_state.raw.modifiers,
|
|
|
|
});
|
|
|
|
}
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
2021-01-17 01:31:37 +00:00
|
|
|
WindowEvent::MouseWheel { delta, .. } => {
|
2020-07-21 22:36:17 +00:00
|
|
|
match delta {
|
|
|
|
glutin::event::MouseScrollDelta::LineDelta(x, y) => {
|
|
|
|
let line_height = 24.0; // TODO
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state.raw.scroll_delta = vec2(x, y) * line_height;
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
|
|
|
glutin::event::MouseScrollDelta::PixelDelta(delta) => {
|
|
|
|
// Actually point delta
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state.raw.scroll_delta = vec2(delta.x as f32, delta.y as f32);
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 22:36:17 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// dbg!(event);
|
|
|
|
}
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 09:54:42 +00:00
|
|
|
/// Glium sends special keys (backspace, delete, F1, ...) as characters.
|
|
|
|
/// Ignore those.
|
|
|
|
/// We also ignore '\r', '\n', '\t'.
|
|
|
|
/// Newlines are handled by the `Key::Enter` event.
|
2021-01-25 17:50:19 +00:00
|
|
|
fn is_printable_char(chr: char) -> bool {
|
2020-07-30 09:54:42 +00:00
|
|
|
let is_in_private_use_area = '\u{e000}' <= chr && chr <= '\u{f8ff}'
|
|
|
|
|| '\u{f0000}' <= chr && chr <= '\u{ffffd}'
|
|
|
|
|| '\u{100000}' <= chr && chr <= '\u{10fffd}';
|
|
|
|
|
|
|
|
!is_in_private_use_area && !chr.is_ascii_control()
|
2020-05-16 17:38:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 17:50:19 +00:00
|
|
|
pub fn translate_mouse_button(button: glutin::event::MouseButton) -> Option<egui::PointerButton> {
|
|
|
|
match button {
|
|
|
|
glutin::event::MouseButton::Left => Some(egui::PointerButton::Primary),
|
|
|
|
glutin::event::MouseButton::Right => Some(egui::PointerButton::Secondary),
|
|
|
|
glutin::event::MouseButton::Middle => Some(egui::PointerButton::Middle),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
pub fn translate_virtual_key_code(key: VirtualKeyCode) -> Option<egui::Key> {
|
2020-04-29 19:58:14 +00:00
|
|
|
use VirtualKeyCode::*;
|
|
|
|
|
|
|
|
Some(match key {
|
2021-01-03 10:25:13 +00:00
|
|
|
Down => Key::ArrowDown,
|
2020-11-14 20:01:21 +00:00
|
|
|
Left => Key::ArrowLeft,
|
|
|
|
Right => Key::ArrowRight,
|
2021-01-03 10:25:13 +00:00
|
|
|
Up => Key::ArrowUp,
|
|
|
|
|
|
|
|
Escape => Key::Escape,
|
|
|
|
Tab => Key::Tab,
|
2020-04-29 19:58:14 +00:00
|
|
|
Back => Key::Backspace,
|
2020-07-30 09:54:42 +00:00
|
|
|
Return => Key::Enter,
|
2020-12-13 09:00:20 +00:00
|
|
|
Space => Key::Space,
|
2020-11-15 13:21:21 +00:00
|
|
|
|
2021-01-03 10:25:13 +00:00
|
|
|
Insert => Key::Insert,
|
|
|
|
Delete => Key::Delete,
|
|
|
|
Home => Key::Home,
|
|
|
|
End => Key::End,
|
|
|
|
PageUp => Key::PageUp,
|
|
|
|
PageDown => Key::PageDown,
|
|
|
|
|
|
|
|
Key0 | Numpad0 => Key::Num0,
|
|
|
|
Key1 | Numpad1 => Key::Num1,
|
|
|
|
Key2 | Numpad2 => Key::Num2,
|
|
|
|
Key3 | Numpad3 => Key::Num3,
|
|
|
|
Key4 | Numpad4 => Key::Num4,
|
|
|
|
Key5 | Numpad5 => Key::Num5,
|
|
|
|
Key6 | Numpad6 => Key::Num6,
|
|
|
|
Key7 | Numpad7 => Key::Num7,
|
|
|
|
Key8 | Numpad8 => Key::Num8,
|
|
|
|
Key9 | Numpad9 => Key::Num9,
|
|
|
|
|
2020-11-15 13:21:21 +00:00
|
|
|
A => Key::A,
|
2021-01-03 10:25:13 +00:00
|
|
|
B => Key::B,
|
|
|
|
C => Key::C,
|
|
|
|
D => Key::D,
|
|
|
|
E => Key::E,
|
|
|
|
F => Key::F,
|
|
|
|
G => Key::G,
|
|
|
|
H => Key::H,
|
|
|
|
I => Key::I,
|
|
|
|
J => Key::J,
|
2020-11-15 13:21:21 +00:00
|
|
|
K => Key::K,
|
2021-01-03 10:25:13 +00:00
|
|
|
L => Key::L,
|
|
|
|
M => Key::M,
|
|
|
|
N => Key::N,
|
|
|
|
O => Key::O,
|
|
|
|
P => Key::P,
|
|
|
|
Q => Key::Q,
|
|
|
|
R => Key::R,
|
|
|
|
S => Key::S,
|
|
|
|
T => Key::T,
|
2020-11-15 13:21:21 +00:00
|
|
|
U => Key::U,
|
2021-01-03 10:25:13 +00:00
|
|
|
V => Key::V,
|
2020-11-15 13:21:21 +00:00
|
|
|
W => Key::W,
|
2021-01-03 10:25:13 +00:00
|
|
|
X => Key::X,
|
|
|
|
Y => Key::Y,
|
2020-11-15 13:21:21 +00:00
|
|
|
Z => Key::Z,
|
|
|
|
|
2020-04-29 19:58:14 +00:00
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
pub fn translate_cursor(cursor_icon: egui::CursorIcon) -> glutin::window::CursorIcon {
|
2020-04-29 19:58:14 +00:00
|
|
|
match cursor_icon {
|
2020-07-21 22:36:17 +00:00
|
|
|
CursorIcon::Default => glutin::window::CursorIcon::Default,
|
|
|
|
CursorIcon::PointingHand => glutin::window::CursorIcon::Hand,
|
|
|
|
CursorIcon::ResizeHorizontal => glutin::window::CursorIcon::EwResize,
|
|
|
|
CursorIcon::ResizeNeSw => glutin::window::CursorIcon::NeswResize,
|
|
|
|
CursorIcon::ResizeNwSe => glutin::window::CursorIcon::NwseResize,
|
|
|
|
CursorIcon::ResizeVertical => glutin::window::CursorIcon::NsResize,
|
|
|
|
CursorIcon::Text => glutin::window::CursorIcon::Text,
|
2020-11-02 16:40:05 +00:00
|
|
|
CursorIcon::Grab => glutin::window::CursorIcon::Grab,
|
|
|
|
CursorIcon::Grabbing => glutin::window::CursorIcon::Grabbing,
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_output(
|
2020-05-30 08:22:35 +00:00
|
|
|
output: egui::Output,
|
2020-04-29 19:58:14 +00:00
|
|
|
display: &glium::backend::glutin::Display,
|
|
|
|
clipboard: Option<&mut ClipboardContext>,
|
|
|
|
) {
|
|
|
|
if let Some(url) = output.open_url {
|
|
|
|
if let Err(err) = webbrowser::open(&url) {
|
2020-10-01 20:40:49 +00:00
|
|
|
eprintln!("Failed to open url: {}", err);
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2020-07-21 22:36:17 +00:00
|
|
|
.window()
|
|
|
|
.set_cursor_icon(translate_cursor(output.cursor_icon));
|
2020-04-29 19:58:14 +00:00
|
|
|
}
|
2020-05-02 09:37:12 +00:00
|
|
|
|
2020-07-22 10:10:14 +00:00
|
|
|
pub fn init_clipboard() -> Option<ClipboardContext> {
|
|
|
|
match ClipboardContext::new() {
|
|
|
|
Ok(clipboard) => Some(clipboard),
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Failed to initialize clipboard: {}", err);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 09:37:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-23 10:01:48 +00:00
|
|
|
/// Time of day as seconds since midnight. Used for clock in demo app.
|
2021-01-04 00:44:02 +00:00
|
|
|
pub fn seconds_since_midnight() -> Option<f64> {
|
|
|
|
#[cfg(feature = "time")]
|
|
|
|
{
|
|
|
|
use chrono::Timelike;
|
|
|
|
let time = chrono::Local::now().time();
|
|
|
|
let seconds_since_midnight =
|
|
|
|
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64);
|
|
|
|
Some(seconds_since_midnight)
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "time"))]
|
|
|
|
None
|
2020-05-11 18:21:24 +00:00
|
|
|
}
|
2020-07-22 10:10:14 +00:00
|
|
|
|
2020-10-17 21:54:46 +00:00
|
|
|
pub fn screen_size_in_pixels(display: &glium::Display) -> Vec2 {
|
|
|
|
let (width_in_pixels, height_in_pixels) = display.get_framebuffer_dimensions();
|
|
|
|
vec2(width_in_pixels as f32, height_in_pixels as f32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn native_pixels_per_point(display: &glium::Display) -> f32 {
|
|
|
|
display.gl_window().window().scale_factor() as f32
|
2020-07-22 10:10:14 +00:00
|
|
|
}
|