mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-27 05:18:27 +00:00
Update glutin and dependencies that rely on it
This commit is contained in:
parent
c17157b6ec
commit
f33567a1dc
@ -10,9 +10,9 @@ publish = false
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
gfx = "0.16"
|
gfx = "0.16"
|
||||||
gfx_window_glutin = "0.16"
|
gfx_window_glutin = "0.17"
|
||||||
glium = { version = "0.16", default-features = true }
|
glium = { version = "0.17", default-features = true }
|
||||||
glutin = "0.8"
|
glutin = "0.9"
|
||||||
imgui = { version = "0.0.15-pre", path = "../" }
|
imgui = { version = "0.0.15-pre", path = "../" }
|
||||||
imgui-gfx-renderer = { version = "0.0.15-pre", path = "../imgui-gfx-renderer" }
|
imgui-gfx-renderer = { version = "0.0.15-pre", path = "../imgui-gfx-renderer" }
|
||||||
imgui-glium-renderer = { version = "0.0.15-pre", path = "../imgui-glium-renderer" }
|
imgui-glium-renderer = { version = "0.0.15-pre", path = "../imgui-glium-renderer" }
|
||||||
|
|||||||
@ -11,11 +11,9 @@ mod support_gfx;
|
|||||||
|
|
||||||
const CLEAR_COLOR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
const CLEAR_COLOR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
||||||
|
|
||||||
fn main() {
|
fn main() { support_gfx::run("hello_gfx.rs".to_owned(), CLEAR_COLOR, hello_world); }
|
||||||
support_gfx::run("hello_gfx.rs".to_owned(), CLEAR_COLOR, hello_world);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hello_world<'a>(ui: &Ui<'a>) {
|
fn hello_world<'a>(ui: &Ui<'a>) -> bool {
|
||||||
ui.window(im_str!("Hello world"))
|
ui.window(im_str!("Hello world"))
|
||||||
.size((300.0, 100.0), ImGuiSetCond_FirstUseEver)
|
.size((300.0, 100.0), ImGuiSetCond_FirstUseEver)
|
||||||
.build(|| {
|
.build(|| {
|
||||||
@ -23,6 +21,12 @@ fn hello_world<'a>(ui: &Ui<'a>) {
|
|||||||
ui.text(im_str!("This...is...imgui-rs!"));
|
ui.text(im_str!("This...is...imgui-rs!"));
|
||||||
ui.separator();
|
ui.separator();
|
||||||
let mouse_pos = ui.imgui().mouse_pos();
|
let mouse_pos = ui.imgui().mouse_pos();
|
||||||
ui.text(im_str!("Mouse Position: ({:.1},{:.1})", mouse_pos.0, mouse_pos.1));
|
ui.text(im_str!(
|
||||||
})
|
"Mouse Position: ({:.1},{:.1})",
|
||||||
|
mouse_pos.0,
|
||||||
|
mouse_pos.1
|
||||||
|
));
|
||||||
|
});
|
||||||
|
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,32 +5,26 @@ extern crate imgui_glium_renderer;
|
|||||||
|
|
||||||
use imgui::*;
|
use imgui::*;
|
||||||
|
|
||||||
use self::support::Support;
|
|
||||||
|
|
||||||
mod support;
|
mod support;
|
||||||
|
|
||||||
const CLEAR_COLOR: (f32, f32, f32, f32) = (1.0, 1.0, 1.0, 1.0);
|
const CLEAR_COLOR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
|
||||||
|
|
||||||
fn main() {
|
fn main() { support::run("hellow_world.rs".to_owned(), CLEAR_COLOR, hello_world); }
|
||||||
let mut support = Support::init();
|
|
||||||
|
|
||||||
loop {
|
fn hello_world<'a>(ui: &Ui<'a>) -> bool {
|
||||||
support.render(CLEAR_COLOR, hello_world);
|
|
||||||
let active = support.update_events();
|
|
||||||
if !active {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hello_world<'a>(ui: &Ui<'a>) {
|
|
||||||
ui.window(im_str!("Hello world"))
|
ui.window(im_str!("Hello world"))
|
||||||
.size((300.0, 100.0), ImGuiSetCond_FirstUseEver)
|
.size((300.0, 100.0), ImGuiSetCond_FirstUseEver)
|
||||||
.build(|| {
|
.build(|| {
|
||||||
ui.text(im_str!("Hello world!"));
|
ui.text(im_str!("Hello world!"));
|
||||||
ui.text(im_str!("This...is...imgui-rs!"));
|
ui.text(im_str!("This...is...imgui-rs!"));
|
||||||
ui.separator();
|
ui.separator();
|
||||||
let mouse_pos = ui.imgui().mouse_pos();
|
let mouse_pos = ui.imgui().mouse_pos();
|
||||||
ui.text(im_str!("Mouse Position: ({:.1},{:.1})", mouse_pos.0, mouse_pos.1));
|
ui.text(im_str!(
|
||||||
})
|
"Mouse Position: ({:.1},{:.1})",
|
||||||
|
mouse_pos.0,
|
||||||
|
mouse_pos.1
|
||||||
|
));
|
||||||
|
});
|
||||||
|
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,153 +1,173 @@
|
|||||||
use glium::{DisplayBuild, Surface};
|
use imgui::{ImGui, Ui};
|
||||||
use glium::backend::glutin_backend::GlutinFacade;
|
|
||||||
use glium::glutin;
|
|
||||||
use glium::glutin::{ElementState, Event, MouseButton, MouseScrollDelta, VirtualKeyCode, TouchPhase};
|
|
||||||
use imgui::{ImGui, Ui, ImGuiKey};
|
|
||||||
use imgui_glium_renderer::Renderer;
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
pub struct Support {
|
#[derive(Copy, Clone, PartialEq, Debug, Default)]
|
||||||
display: GlutinFacade,
|
struct MouseState {
|
||||||
imgui: ImGui,
|
pos: (i32, i32),
|
||||||
renderer: Renderer,
|
pressed: (bool, bool, bool),
|
||||||
last_frame: Instant,
|
wheel: f32,
|
||||||
mouse_pos: (i32, i32),
|
|
||||||
mouse_pressed: (bool, bool, bool),
|
|
||||||
mouse_wheel: f32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Support {
|
pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) {
|
||||||
pub fn init() -> Support {
|
use glium::glutin;
|
||||||
let display = glutin::WindowBuilder::new().build_glium().unwrap();
|
use glium::{Display, Surface};
|
||||||
|
use imgui_glium_renderer::Renderer;
|
||||||
|
|
||||||
let mut imgui = ImGui::init();
|
let mut events_loop = glutin::EventsLoop::new();
|
||||||
let renderer = Renderer::init(&mut imgui, &display).unwrap();
|
let context = glutin::ContextBuilder::new().with_vsync(true);
|
||||||
|
let window = glutin::WindowBuilder::new()
|
||||||
|
.with_title(title)
|
||||||
|
.with_dimensions(1024, 768);
|
||||||
|
let display = Display::new(window, context, &events_loop).unwrap();
|
||||||
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Tab, 0);
|
let mut imgui = ImGui::init();
|
||||||
imgui.set_imgui_key(ImGuiKey::LeftArrow, 1);
|
let mut renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
|
||||||
imgui.set_imgui_key(ImGuiKey::RightArrow, 2);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::UpArrow, 3);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::DownArrow, 4);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::PageUp, 5);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::PageDown, 6);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Home, 7);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::End, 8);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Delete, 9);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Backspace, 10);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Enter, 11);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Escape, 12);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::A, 13);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::C, 14);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::V, 15);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::X, 16);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Y, 17);
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Z, 18);
|
|
||||||
|
|
||||||
Support {
|
configure_keys(&mut imgui);
|
||||||
display: display,
|
|
||||||
imgui: imgui,
|
|
||||||
renderer: renderer,
|
|
||||||
last_frame: Instant::now(),
|
|
||||||
mouse_pos: (0, 0),
|
|
||||||
mouse_pressed: (false, false, false),
|
|
||||||
mouse_wheel: 0.0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update_mouse(&mut self) {
|
let mut last_frame = Instant::now();
|
||||||
let scale = self.imgui.display_framebuffer_scale();
|
let mut mouse_state = MouseState::default();
|
||||||
self.imgui
|
let mut quit = false;
|
||||||
.set_mouse_pos(self.mouse_pos.0 as f32 / scale.0,
|
|
||||||
self.mouse_pos.1 as f32 / scale.1);
|
|
||||||
self.imgui
|
|
||||||
.set_mouse_down(&[self.mouse_pressed.0,
|
|
||||||
self.mouse_pressed.1,
|
|
||||||
self.mouse_pressed.2,
|
|
||||||
false,
|
|
||||||
false]);
|
|
||||||
self.imgui.set_mouse_wheel(self.mouse_wheel / scale.1);
|
|
||||||
self.mouse_wheel = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render<F: FnMut(&Ui)>(&mut self, clear_color: (f32, f32, f32, f32), mut run_ui: F) {
|
loop {
|
||||||
let now = Instant::now();
|
events_loop.poll_events(|event| {
|
||||||
let delta = now - self.last_frame;
|
use glium::glutin::WindowEvent::*;
|
||||||
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
|
use glium::glutin::ElementState::Pressed;
|
||||||
self.last_frame = now;
|
use glium::glutin::{Event, MouseButton, MouseScrollDelta, TouchPhase};
|
||||||
|
|
||||||
self.update_mouse();
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
|
match event {
|
||||||
|
Resized(_, _) => unimplemented!(),
|
||||||
|
Closed => quit = true,
|
||||||
|
KeyboardInput { input, .. } => {
|
||||||
|
use glium::glutin::VirtualKeyCode as Key;
|
||||||
|
|
||||||
let mut target = self.display.draw();
|
let pressed = input.state == Pressed;
|
||||||
target.clear_color(clear_color.0, clear_color.1, clear_color.2, clear_color.3);
|
match input.virtual_keycode {
|
||||||
|
Some(Key::Tab) => imgui.set_key(0, pressed),
|
||||||
let window = self.display.get_window().unwrap();
|
Some(Key::Left) => imgui.set_key(1, pressed),
|
||||||
let size_points = window.get_inner_size_points().unwrap();
|
Some(Key::Right) => imgui.set_key(2, pressed),
|
||||||
let size_pixels = window.get_inner_size_pixels().unwrap();
|
Some(Key::Up) => imgui.set_key(3, pressed),
|
||||||
|
Some(Key::Down) => imgui.set_key(4, pressed),
|
||||||
let ui = self.imgui.frame(size_points, size_pixels, delta_s);
|
Some(Key::PageUp) => imgui.set_key(5, pressed),
|
||||||
|
Some(Key::PageDown) => imgui.set_key(6, pressed),
|
||||||
run_ui(&ui);
|
Some(Key::Home) => imgui.set_key(7, pressed),
|
||||||
|
Some(Key::End) => imgui.set_key(8, pressed),
|
||||||
self.renderer.render(&mut target, ui).unwrap();
|
Some(Key::Delete) => imgui.set_key(9, pressed),
|
||||||
|
Some(Key::Back) => imgui.set_key(10, pressed),
|
||||||
target.finish().unwrap();
|
Some(Key::Return) => imgui.set_key(11, pressed),
|
||||||
}
|
Some(Key::Escape) => imgui.set_key(12, pressed),
|
||||||
|
Some(Key::A) => imgui.set_key(13, pressed),
|
||||||
pub fn update_events(&mut self) -> bool {
|
Some(Key::C) => imgui.set_key(14, pressed),
|
||||||
for event in self.display.poll_events() {
|
Some(Key::V) => imgui.set_key(15, pressed),
|
||||||
match event {
|
Some(Key::X) => imgui.set_key(16, pressed),
|
||||||
Event::Closed => return false,
|
Some(Key::Y) => imgui.set_key(17, pressed),
|
||||||
Event::KeyboardInput(state, _, code) => {
|
Some(Key::Z) => imgui.set_key(18, pressed),
|
||||||
let pressed = state == ElementState::Pressed;
|
Some(Key::LControl) |
|
||||||
match code {
|
Some(Key::RControl) => imgui.set_key_ctrl(pressed),
|
||||||
Some(VirtualKeyCode::Tab) => self.imgui.set_key(0, pressed),
|
Some(Key::LShift) |
|
||||||
Some(VirtualKeyCode::Left) => self.imgui.set_key(1, pressed),
|
Some(Key::RShift) => imgui.set_key_shift(pressed),
|
||||||
Some(VirtualKeyCode::Right) => self.imgui.set_key(2, pressed),
|
Some(Key::LAlt) | Some(Key::RAlt) => imgui.set_key_alt(pressed),
|
||||||
Some(VirtualKeyCode::Up) => self.imgui.set_key(3, pressed),
|
Some(Key::LWin) | Some(Key::RWin) => imgui.set_key_super(pressed),
|
||||||
Some(VirtualKeyCode::Down) => self.imgui.set_key(4, pressed),
|
_ => {}
|
||||||
Some(VirtualKeyCode::PageUp) => self.imgui.set_key(5, pressed),
|
}
|
||||||
Some(VirtualKeyCode::PageDown) => self.imgui.set_key(6, pressed),
|
|
||||||
Some(VirtualKeyCode::Home) => self.imgui.set_key(7, pressed),
|
|
||||||
Some(VirtualKeyCode::End) => self.imgui.set_key(8, pressed),
|
|
||||||
Some(VirtualKeyCode::Delete) => self.imgui.set_key(9, pressed),
|
|
||||||
Some(VirtualKeyCode::Back) => self.imgui.set_key(10, pressed),
|
|
||||||
Some(VirtualKeyCode::Return) => self.imgui.set_key(11, pressed),
|
|
||||||
Some(VirtualKeyCode::Escape) => self.imgui.set_key(12, pressed),
|
|
||||||
Some(VirtualKeyCode::A) => self.imgui.set_key(13, pressed),
|
|
||||||
Some(VirtualKeyCode::C) => self.imgui.set_key(14, pressed),
|
|
||||||
Some(VirtualKeyCode::V) => self.imgui.set_key(15, pressed),
|
|
||||||
Some(VirtualKeyCode::X) => self.imgui.set_key(16, pressed),
|
|
||||||
Some(VirtualKeyCode::Y) => self.imgui.set_key(17, pressed),
|
|
||||||
Some(VirtualKeyCode::Z) => self.imgui.set_key(18, pressed),
|
|
||||||
Some(VirtualKeyCode::LControl) |
|
|
||||||
Some(VirtualKeyCode::RControl) => self.imgui.set_key_ctrl(pressed),
|
|
||||||
Some(VirtualKeyCode::LShift) |
|
|
||||||
Some(VirtualKeyCode::RShift) => self.imgui.set_key_shift(pressed),
|
|
||||||
Some(VirtualKeyCode::LAlt) |
|
|
||||||
Some(VirtualKeyCode::RAlt) => self.imgui.set_key_alt(pressed),
|
|
||||||
Some(VirtualKeyCode::LWin) |
|
|
||||||
Some(VirtualKeyCode::RWin) => self.imgui.set_key_super(pressed),
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
MouseMoved { position: (x, y), .. } => mouse_state.pos = (x as i32, y as i32),
|
||||||
|
MouseInput { state, button, .. } => {
|
||||||
|
match button {
|
||||||
|
MouseButton::Left => mouse_state.pressed.0 = state == Pressed,
|
||||||
|
MouseButton::Right => mouse_state.pressed.1 = state == Pressed,
|
||||||
|
MouseButton::Middle => mouse_state.pressed.2 = state == Pressed,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MouseWheel {
|
||||||
|
delta: MouseScrollDelta::LineDelta(_, y),
|
||||||
|
phase: TouchPhase::Moved,
|
||||||
|
..
|
||||||
|
} |
|
||||||
|
MouseWheel {
|
||||||
|
delta: MouseScrollDelta::PixelDelta(_, y),
|
||||||
|
phase: TouchPhase::Moved,
|
||||||
|
..
|
||||||
|
} => mouse_state.wheel = y,
|
||||||
|
ReceivedCharacter(c) => imgui.add_input_character(c),
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
Event::MouseMoved(x, y) => self.mouse_pos = (x, y),
|
|
||||||
Event::MouseInput(state, MouseButton::Left) => {
|
|
||||||
self.mouse_pressed.0 = state == ElementState::Pressed
|
|
||||||
}
|
|
||||||
Event::MouseInput(state, MouseButton::Right) => {
|
|
||||||
self.mouse_pressed.1 = state == ElementState::Pressed
|
|
||||||
}
|
|
||||||
Event::MouseInput(state, MouseButton::Middle) => {
|
|
||||||
self.mouse_pressed.2 = state == ElementState::Pressed
|
|
||||||
}
|
|
||||||
Event::MouseWheel(MouseScrollDelta::LineDelta(_, y), TouchPhase::Moved) |
|
|
||||||
Event::MouseWheel(MouseScrollDelta::PixelDelta(_, y), TouchPhase::Moved) => {
|
|
||||||
self.mouse_wheel = y
|
|
||||||
}
|
|
||||||
Event::ReceivedCharacter(c) => self.imgui.add_input_character(c),
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let now = Instant::now();
|
||||||
|
let delta = now - last_frame;
|
||||||
|
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
|
||||||
|
last_frame = now;
|
||||||
|
|
||||||
|
update_mouse(&mut imgui, &mut mouse_state);
|
||||||
|
|
||||||
|
let gl_window = display.gl_window();
|
||||||
|
let size_points = gl_window.get_inner_size_points().unwrap();
|
||||||
|
let size_pixels = gl_window.get_inner_size_pixels().unwrap();
|
||||||
|
|
||||||
|
let ui = imgui.frame(size_points, size_pixels, delta_s);
|
||||||
|
if !run_ui(&ui) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut target = display.draw();
|
||||||
|
target.clear_color(
|
||||||
|
clear_color[0],
|
||||||
|
clear_color[1],
|
||||||
|
clear_color[2],
|
||||||
|
clear_color[3],
|
||||||
|
);
|
||||||
|
renderer.render(&mut target, ui).expect("Rendering failed");
|
||||||
|
target.finish().unwrap();
|
||||||
|
|
||||||
|
if quit {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn configure_keys(imgui: &mut ImGui) {
|
||||||
|
use imgui::ImGuiKey;
|
||||||
|
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Tab, 0);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::LeftArrow, 1);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::RightArrow, 2);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::UpArrow, 3);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::DownArrow, 4);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::PageUp, 5);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::PageDown, 6);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Home, 7);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::End, 8);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Delete, 9);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Backspace, 10);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Enter, 11);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Escape, 12);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::A, 13);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::C, 14);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::V, 15);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::X, 16);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Y, 17);
|
||||||
|
imgui.set_imgui_key(ImGuiKey::Z, 18);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_mouse(imgui: &mut ImGui, mouse_state: &mut MouseState) {
|
||||||
|
let scale = imgui.display_framebuffer_scale();
|
||||||
|
imgui.set_mouse_pos(
|
||||||
|
mouse_state.pos.0 as f32 / scale.0,
|
||||||
|
mouse_state.pos.1 as f32 / scale.1,
|
||||||
|
);
|
||||||
|
imgui.set_mouse_down(
|
||||||
|
&[
|
||||||
|
mouse_state.pressed.0,
|
||||||
|
mouse_state.pressed.1,
|
||||||
|
mouse_state.pressed.2,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
imgui.set_mouse_wheel(mouse_state.wheel / scale.1);
|
||||||
|
mouse_state.wheel = 0.0;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,33 +1,33 @@
|
|||||||
use gfx;
|
use imgui::{ImGui, Ui};
|
||||||
use gfx::Device;
|
|
||||||
use gfx_window_glutin;
|
|
||||||
use glutin;
|
|
||||||
use glutin::{ElementState, MouseButton, MouseScrollDelta, VirtualKeyCode, TouchPhase, WindowEvent};
|
|
||||||
use imgui::{ImGui, Ui, ImGuiKey};
|
|
||||||
use imgui_gfx_renderer::Renderer;
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
type ColorFormat = gfx::format::Rgba8;
|
|
||||||
type DepthFormat = gfx::format::DepthStencil;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Debug, Default)]
|
#[derive(Copy, Clone, PartialEq, Debug, Default)]
|
||||||
struct MouseState {
|
struct MouseState {
|
||||||
pos: (i32, i32),
|
pos: (i32, i32),
|
||||||
pressed: (bool, bool, bool),
|
pressed: (bool, bool, bool),
|
||||||
wheel: f32
|
wheel: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run<F: FnMut(&Ui)>(title: String, clear_color: [f32; 4], mut run_ui: F) {
|
pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) {
|
||||||
let mut imgui = ImGui::init();
|
use gfx::{self, Device};
|
||||||
|
use gfx_window_glutin;
|
||||||
|
use glutin::{self, GlContext};
|
||||||
|
use imgui_gfx_renderer::Renderer;
|
||||||
|
|
||||||
let events_loop = glutin::EventsLoop::new();
|
type ColorFormat = gfx::format::Rgba8;
|
||||||
let builder = glutin::WindowBuilder::new()
|
type DepthFormat = gfx::format::DepthStencil;
|
||||||
|
|
||||||
|
|
||||||
|
let mut events_loop = glutin::EventsLoop::new();
|
||||||
|
let context = glutin::ContextBuilder::new().with_vsync(true);
|
||||||
|
let window = glutin::WindowBuilder::new()
|
||||||
.with_title(title)
|
.with_title(title)
|
||||||
.with_dimensions(1024, 768)
|
.with_dimensions(1024, 768);
|
||||||
.with_vsync();
|
|
||||||
let (window, mut device, mut factory, mut main_color, mut main_depth) =
|
let (window, mut device, mut factory, mut main_color, mut main_depth) =
|
||||||
gfx_window_glutin::init::<ColorFormat, DepthFormat>(builder, &events_loop);
|
gfx_window_glutin::init::<ColorFormat, DepthFormat>(window, context, &events_loop);
|
||||||
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
|
let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
|
||||||
|
|
||||||
|
let mut imgui = ImGui::init();
|
||||||
let mut renderer = Renderer::init(&mut imgui, &mut factory, main_color.clone())
|
let mut renderer = Renderer::init(&mut imgui, &mut factory, main_color.clone())
|
||||||
.expect("Failed to initialize renderer");
|
.expect("Failed to initialize renderer");
|
||||||
|
|
||||||
@ -38,64 +38,73 @@ pub fn run<F: FnMut(&Ui)>(title: String, clear_color: [f32; 4], mut run_ui: F) {
|
|||||||
let mut quit = false;
|
let mut quit = false;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
events_loop.poll_events(|glutin::Event::WindowEvent{event, ..}| {
|
events_loop.poll_events(|event| {
|
||||||
match event {
|
use glutin::WindowEvent::*;
|
||||||
WindowEvent::Resized(_, _) => {
|
use glutin::ElementState::Pressed;
|
||||||
gfx_window_glutin::update_views(&window, &mut main_color, &mut main_depth);
|
use glutin::{Event, MouseButton, MouseScrollDelta, TouchPhase};
|
||||||
renderer.update_render_target(main_color.clone());
|
|
||||||
}
|
if let Event::WindowEvent { event, .. } = event {
|
||||||
WindowEvent::Closed => quit = true,
|
match event {
|
||||||
WindowEvent::KeyboardInput(state, _, code, _) => {
|
Resized(_, _) => {
|
||||||
let pressed = state == ElementState::Pressed;
|
gfx_window_glutin::update_views(&window, &mut main_color, &mut main_depth);
|
||||||
match code {
|
renderer.update_render_target(main_color.clone());
|
||||||
Some(VirtualKeyCode::Tab) => imgui.set_key(0, pressed),
|
|
||||||
Some(VirtualKeyCode::Left) => imgui.set_key(1, pressed),
|
|
||||||
Some(VirtualKeyCode::Right) => imgui.set_key(2, pressed),
|
|
||||||
Some(VirtualKeyCode::Up) => imgui.set_key(3, pressed),
|
|
||||||
Some(VirtualKeyCode::Down) => imgui.set_key(4, pressed),
|
|
||||||
Some(VirtualKeyCode::PageUp) => imgui.set_key(5, pressed),
|
|
||||||
Some(VirtualKeyCode::PageDown) => imgui.set_key(6, pressed),
|
|
||||||
Some(VirtualKeyCode::Home) => imgui.set_key(7, pressed),
|
|
||||||
Some(VirtualKeyCode::End) => imgui.set_key(8, pressed),
|
|
||||||
Some(VirtualKeyCode::Delete) => imgui.set_key(9, pressed),
|
|
||||||
Some(VirtualKeyCode::Back) => imgui.set_key(10, pressed),
|
|
||||||
Some(VirtualKeyCode::Return) => imgui.set_key(11, pressed),
|
|
||||||
Some(VirtualKeyCode::Escape) => quit = true,
|
|
||||||
Some(VirtualKeyCode::A) => imgui.set_key(13, pressed),
|
|
||||||
Some(VirtualKeyCode::C) => imgui.set_key(14, pressed),
|
|
||||||
Some(VirtualKeyCode::V) => imgui.set_key(15, pressed),
|
|
||||||
Some(VirtualKeyCode::X) => imgui.set_key(16, pressed),
|
|
||||||
Some(VirtualKeyCode::Y) => imgui.set_key(17, pressed),
|
|
||||||
Some(VirtualKeyCode::Z) => imgui.set_key(18, pressed),
|
|
||||||
Some(VirtualKeyCode::LControl) |
|
|
||||||
Some(VirtualKeyCode::RControl) => imgui.set_key_ctrl(pressed),
|
|
||||||
Some(VirtualKeyCode::LShift) |
|
|
||||||
Some(VirtualKeyCode::RShift) => imgui.set_key_shift(pressed),
|
|
||||||
Some(VirtualKeyCode::LAlt) |
|
|
||||||
Some(VirtualKeyCode::RAlt) => imgui.set_key_alt(pressed),
|
|
||||||
Some(VirtualKeyCode::LWin) |
|
|
||||||
Some(VirtualKeyCode::RWin) => imgui.set_key_super(pressed),
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
Closed => quit = true,
|
||||||
|
KeyboardInput { input, .. } => {
|
||||||
|
use glutin::VirtualKeyCode as Key;
|
||||||
|
|
||||||
|
let pressed = input.state == Pressed;
|
||||||
|
match input.virtual_keycode {
|
||||||
|
Some(Key::Tab) => imgui.set_key(0, pressed),
|
||||||
|
Some(Key::Left) => imgui.set_key(1, pressed),
|
||||||
|
Some(Key::Right) => imgui.set_key(2, pressed),
|
||||||
|
Some(Key::Up) => imgui.set_key(3, pressed),
|
||||||
|
Some(Key::Down) => imgui.set_key(4, pressed),
|
||||||
|
Some(Key::PageUp) => imgui.set_key(5, pressed),
|
||||||
|
Some(Key::PageDown) => imgui.set_key(6, pressed),
|
||||||
|
Some(Key::Home) => imgui.set_key(7, pressed),
|
||||||
|
Some(Key::End) => imgui.set_key(8, pressed),
|
||||||
|
Some(Key::Delete) => imgui.set_key(9, pressed),
|
||||||
|
Some(Key::Back) => imgui.set_key(10, pressed),
|
||||||
|
Some(Key::Return) => imgui.set_key(11, pressed),
|
||||||
|
Some(Key::Escape) => imgui.set_key(12, pressed),
|
||||||
|
Some(Key::A) => imgui.set_key(13, pressed),
|
||||||
|
Some(Key::C) => imgui.set_key(14, pressed),
|
||||||
|
Some(Key::V) => imgui.set_key(15, pressed),
|
||||||
|
Some(Key::X) => imgui.set_key(16, pressed),
|
||||||
|
Some(Key::Y) => imgui.set_key(17, pressed),
|
||||||
|
Some(Key::Z) => imgui.set_key(18, pressed),
|
||||||
|
Some(Key::LControl) |
|
||||||
|
Some(Key::RControl) => imgui.set_key_ctrl(pressed),
|
||||||
|
Some(Key::LShift) |
|
||||||
|
Some(Key::RShift) => imgui.set_key_shift(pressed),
|
||||||
|
Some(Key::LAlt) | Some(Key::RAlt) => imgui.set_key_alt(pressed),
|
||||||
|
Some(Key::LWin) | Some(Key::RWin) => imgui.set_key_super(pressed),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MouseMoved { position: (x, y), .. } => mouse_state.pos = (x as i32, y as i32),
|
||||||
|
MouseInput { state, button, .. } => {
|
||||||
|
match button {
|
||||||
|
MouseButton::Left => mouse_state.pressed.0 = state == Pressed,
|
||||||
|
MouseButton::Right => mouse_state.pressed.1 = state == Pressed,
|
||||||
|
MouseButton::Middle => mouse_state.pressed.2 = state == Pressed,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MouseWheel {
|
||||||
|
delta: MouseScrollDelta::LineDelta(_, y),
|
||||||
|
phase: TouchPhase::Moved,
|
||||||
|
..
|
||||||
|
} |
|
||||||
|
MouseWheel {
|
||||||
|
delta: MouseScrollDelta::PixelDelta(_, y),
|
||||||
|
phase: TouchPhase::Moved,
|
||||||
|
..
|
||||||
|
} => mouse_state.wheel = y,
|
||||||
|
ReceivedCharacter(c) => imgui.add_input_character(c),
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
WindowEvent::MouseMoved(x, y) => mouse_state.pos = (x, y),
|
|
||||||
WindowEvent::MouseInput(state, MouseButton::Left) => {
|
|
||||||
mouse_state.pressed.0 = state == ElementState::Pressed
|
|
||||||
}
|
|
||||||
WindowEvent::MouseInput(state, MouseButton::Right) => {
|
|
||||||
mouse_state.pressed.1 = state == ElementState::Pressed
|
|
||||||
}
|
|
||||||
WindowEvent::MouseInput(state, MouseButton::Middle) => {
|
|
||||||
mouse_state.pressed.2 = state == ElementState::Pressed
|
|
||||||
}
|
|
||||||
WindowEvent::MouseWheel(MouseScrollDelta::LineDelta(_, y), TouchPhase::Moved) => {
|
|
||||||
mouse_state.wheel = y
|
|
||||||
}
|
|
||||||
WindowEvent::MouseWheel(MouseScrollDelta::PixelDelta(_, y), TouchPhase::Moved) => {
|
|
||||||
mouse_state.wheel = y
|
|
||||||
}
|
|
||||||
WindowEvent::ReceivedCharacter(c) => imgui.add_input_character(c),
|
|
||||||
_ => ()
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -110,22 +119,27 @@ pub fn run<F: FnMut(&Ui)>(title: String, clear_color: [f32; 4], mut run_ui: F) {
|
|||||||
let size_pixels = window.get_inner_size_pixels().unwrap();
|
let size_pixels = window.get_inner_size_pixels().unwrap();
|
||||||
|
|
||||||
let ui = imgui.frame(size_points, size_pixels, delta_s);
|
let ui = imgui.frame(size_points, size_pixels, delta_s);
|
||||||
|
if !run_ui(&ui) {
|
||||||
run_ui(&ui);
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
encoder.clear(&mut main_color, clear_color);
|
encoder.clear(&mut main_color, clear_color);
|
||||||
|
renderer.render(ui, &mut factory, &mut encoder).expect(
|
||||||
renderer.render(ui, &mut factory, &mut encoder)
|
"Rendering failed",
|
||||||
.expect("Rendering failed");
|
);
|
||||||
encoder.flush(&mut device);
|
encoder.flush(&mut device);
|
||||||
window.swap_buffers().unwrap();
|
window.context().swap_buffers().unwrap();
|
||||||
device.cleanup();
|
device.cleanup();
|
||||||
|
|
||||||
if quit { break }
|
if quit {
|
||||||
};
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn configure_keys(imgui: &mut ImGui) {
|
fn configure_keys(imgui: &mut ImGui) {
|
||||||
|
use imgui::ImGuiKey;
|
||||||
|
|
||||||
imgui.set_imgui_key(ImGuiKey::Tab, 0);
|
imgui.set_imgui_key(ImGuiKey::Tab, 0);
|
||||||
imgui.set_imgui_key(ImGuiKey::LeftArrow, 1);
|
imgui.set_imgui_key(ImGuiKey::LeftArrow, 1);
|
||||||
imgui.set_imgui_key(ImGuiKey::RightArrow, 2);
|
imgui.set_imgui_key(ImGuiKey::RightArrow, 2);
|
||||||
@ -149,13 +163,19 @@ fn configure_keys(imgui: &mut ImGui) {
|
|||||||
|
|
||||||
fn update_mouse(imgui: &mut ImGui, mouse_state: &mut MouseState) {
|
fn update_mouse(imgui: &mut ImGui, mouse_state: &mut MouseState) {
|
||||||
let scale = imgui.display_framebuffer_scale();
|
let scale = imgui.display_framebuffer_scale();
|
||||||
imgui.set_mouse_pos(mouse_state.pos.0 as f32 / scale.0,
|
imgui.set_mouse_pos(
|
||||||
mouse_state.pos.1 as f32 / scale.1);
|
mouse_state.pos.0 as f32 / scale.0,
|
||||||
imgui.set_mouse_down(&[mouse_state.pressed.0,
|
mouse_state.pos.1 as f32 / scale.1,
|
||||||
mouse_state.pressed.1,
|
);
|
||||||
mouse_state.pressed.2,
|
imgui.set_mouse_down(
|
||||||
false,
|
&[
|
||||||
false]);
|
mouse_state.pressed.0,
|
||||||
|
mouse_state.pressed.1,
|
||||||
|
mouse_state.pressed.2,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
);
|
||||||
imgui.set_mouse_wheel(mouse_state.wheel / scale.1);
|
imgui.set_mouse_wheel(mouse_state.wheel / scale.1);
|
||||||
mouse_state.wheel = 0.0;
|
mouse_state.wheel = 0.0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,21 +2,14 @@ extern crate glium;
|
|||||||
extern crate imgui;
|
extern crate imgui;
|
||||||
extern crate imgui_glium_renderer;
|
extern crate imgui_glium_renderer;
|
||||||
|
|
||||||
use self::support::Support;
|
|
||||||
|
|
||||||
mod support;
|
mod support;
|
||||||
|
|
||||||
const CLEAR_COLOR: (f32, f32, f32, f32) = (0.2, 0.2, 0.2, 1.0);
|
const CLEAR_COLOR: [f32; 4] = [0.2, 0.2, 0.2, 1.0];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut support = Support::init();
|
support::run("test_window.rs".to_owned(), CLEAR_COLOR, |ui| {
|
||||||
|
|
||||||
loop {
|
|
||||||
let mut open = true;
|
let mut open = true;
|
||||||
support.render(CLEAR_COLOR, |ui| ui.show_test_window(&mut open));
|
ui.show_test_window(&mut open);
|
||||||
let active = support.update_events();
|
open
|
||||||
if !active || !open {
|
});
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,12 +5,9 @@ extern crate imgui_glium_renderer;
|
|||||||
|
|
||||||
use imgui::*;
|
use imgui::*;
|
||||||
|
|
||||||
use self::support::Support;
|
|
||||||
|
|
||||||
mod support;
|
mod support;
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
clear_color: (f32, f32, f32, f32),
|
|
||||||
show_app_metrics: bool,
|
show_app_metrics: bool,
|
||||||
show_app_main_menu_bar: bool,
|
show_app_main_menu_bar: bool,
|
||||||
show_app_console: bool,
|
show_app_console: bool,
|
||||||
@ -54,7 +51,6 @@ impl Default for State {
|
|||||||
let mut text = ImString::with_capacity(128);
|
let mut text = ImString::with_capacity(128);
|
||||||
text.push_str("Hello, world!");
|
text.push_str("Hello, world!");
|
||||||
State {
|
State {
|
||||||
clear_color: (114.0 / 255.0, 144.0 / 255.0, 154.0 / 255.0, 1.0),
|
|
||||||
show_app_metrics: false,
|
show_app_metrics: false,
|
||||||
show_app_main_menu_bar: false,
|
show_app_main_menu_bar: false,
|
||||||
show_app_console: false,
|
show_app_console: false,
|
||||||
@ -109,19 +105,16 @@ impl Default for AutoResizeState {
|
|||||||
fn default() -> Self { AutoResizeState { lines: 10 } }
|
fn default() -> Self { AutoResizeState { lines: 10 } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CLEAR_COLOR: [f32; 4] = [114.0 / 255.0, 144.0 / 255.0, 154.0 / 255.0, 1.0];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut state = State::default();
|
let mut state = State::default();
|
||||||
let mut support = Support::init();
|
|
||||||
let mut opened = true;
|
|
||||||
|
|
||||||
loop {
|
support::run("test_window.rs".to_owned(), CLEAR_COLOR, |ui| {
|
||||||
support.render(state.clear_color,
|
let mut open = true;
|
||||||
|ui| { show_test_window(ui, &mut state, &mut opened); });
|
show_test_window(ui, &mut state, &mut open);
|
||||||
let active = support.update_events();
|
open
|
||||||
if !active || !opened {
|
});
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_user_guide<'a>(ui: &Ui<'a>) {
|
fn show_user_guide<'a>(ui: &Ui<'a>) {
|
||||||
|
|||||||
@ -9,6 +9,6 @@ license = "MIT/Apache-2.0"
|
|||||||
categories = ["gui", "rendering"]
|
categories = ["gui", "rendering"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
glium = { version = "0.16", default-features = false }
|
glium = { version = "0.17", default-features = false }
|
||||||
imgui = { version = "0.0.15-pre", path = "../" }
|
imgui = { version = "0.0.15-pre", path = "../" }
|
||||||
imgui-sys = { version = "0.0.15-pre", path = "../imgui-sys", features = ["glium"] }
|
imgui-sys = { version = "0.0.15-pre", path = "../imgui-sys", features = ["glium"] }
|
||||||
|
|||||||
@ -11,7 +11,7 @@ build = "build.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "0.9"
|
bitflags = "0.9"
|
||||||
glium = { version = "0.16", default-features = false, optional = true }
|
glium = { version = "0.17", default-features = false, optional = true }
|
||||||
gfx = { version = "0.16", optional = true }
|
gfx = { version = "0.16", optional = true }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
|
|||||||
@ -22,13 +22,26 @@ impl Vertex for ImDrawVert {
|
|||||||
fn build_bindings() -> VertexFormat {
|
fn build_bindings() -> VertexFormat {
|
||||||
unsafe {
|
unsafe {
|
||||||
let dummy: &ImDrawVert = mem::transmute(0usize);
|
let dummy: &ImDrawVert = mem::transmute(0usize);
|
||||||
Cow::Owned(vec![("pos".into(),
|
Cow::Owned(vec![
|
||||||
mem::transmute(&dummy.pos),
|
(
|
||||||
<ImVec2 as Attribute>::get_type()),
|
"pos".into(),
|
||||||
("uv".into(),
|
mem::transmute(&dummy.pos),
|
||||||
mem::transmute(&dummy.uv),
|
<ImVec2 as Attribute>::get_type(),
|
||||||
<ImVec2 as Attribute>::get_type()),
|
false
|
||||||
("col".into(), mem::transmute(&dummy.col), AttributeType::U8U8U8U8)])
|
),
|
||||||
|
(
|
||||||
|
"uv".into(),
|
||||||
|
mem::transmute(&dummy.uv),
|
||||||
|
<ImVec2 as Attribute>::get_type(),
|
||||||
|
false
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"col".into(),
|
||||||
|
mem::transmute(&dummy.col),
|
||||||
|
AttributeType::U8U8U8U8,
|
||||||
|
false
|
||||||
|
),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user