Merge pull request #64 from brendanzab/update-glutin

Update glutin and dependencies that rely on it
This commit is contained in:
Joonas Javanainen 2017-07-22 20:29:15 +03:00 committed by GitHub
commit 43c40849c3
12 changed files with 330 additions and 294 deletions

View File

@ -5,7 +5,6 @@ rust:
- stable
- beta
- nightly
- 1.16.0
os:
- linux
- osx

View File

@ -2,7 +2,7 @@
**Still fairly experimental!**
Minimum Rust version: 1.16
Minimum Rust version: 1.17
[![Build Status](https://travis-ci.org/Gekkio/imgui-rs.svg?branch=master)](https://travis-ci.org/Gekkio/imgui-rs)
[![Latest release on crates.io](https://meritbadge.herokuapp.com/imgui)](https://crates.io/crates/imgui)
@ -59,8 +59,8 @@ Examples are under the imgui-examples directory.
cargo run --example test_window
cargo run --example test_window_impl
Note to Windows users: You will need to use the *MSVC ABI* version of the Rust compiler along
with its associated [dependencies](https://www.rust-lang.org/en-US/downloads.html#win-foot) to
Note to Windows users: You will need to use the *MSVC ABI* version of the Rust compiler along
with its associated [dependencies](https://www.rust-lang.org/en-US/downloads.html#win-foot) to
build this libary and run the examples.
## How to contribute

View File

@ -10,9 +10,9 @@ publish = false
[dev-dependencies]
gfx = "0.16"
gfx_window_glutin = "0.16"
glium = { version = "0.16", default-features = true }
glutin = "0.8"
gfx_window_glutin = "0.17"
glium = { version = "0.17", default-features = true }
glutin = "0.9"
imgui = { version = "0.0.15-pre", path = "../" }
imgui-gfx-renderer = { version = "0.0.15-pre", path = "../imgui-gfx-renderer" }
imgui-glium-renderer = { version = "0.0.15-pre", path = "../imgui-glium-renderer" }

View File

@ -11,11 +11,9 @@ mod support_gfx;
const CLEAR_COLOR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
fn main() {
support_gfx::run("hello_gfx.rs".to_owned(), CLEAR_COLOR, hello_world);
}
fn main() { 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"))
.size((300.0, 100.0), ImGuiSetCond_FirstUseEver)
.build(|| {
@ -23,6 +21,12 @@ fn hello_world<'a>(ui: &Ui<'a>) {
ui.text(im_str!("This...is...imgui-rs!"));
ui.separator();
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
}

View File

@ -5,32 +5,26 @@ extern crate imgui_glium_renderer;
use imgui::*;
use self::support::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() {
let mut support = Support::init();
fn main() { support::run("hellow_world.rs".to_owned(), CLEAR_COLOR, hello_world); }
loop {
support.render(CLEAR_COLOR, hello_world);
let active = support.update_events();
if !active {
break;
}
}
}
fn hello_world<'a>(ui: &Ui<'a>) {
fn hello_world<'a>(ui: &Ui<'a>) -> bool {
ui.window(im_str!("Hello world"))
.size((300.0, 100.0), ImGuiSetCond_FirstUseEver)
.build(|| {
ui.text(im_str!("Hello world!"));
ui.text(im_str!("This...is...imgui-rs!"));
ui.separator();
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!("Hello world!"));
ui.text(im_str!("This...is...imgui-rs!"));
ui.separator();
let mouse_pos = ui.imgui().mouse_pos();
ui.text(im_str!(
"Mouse Position: ({:.1},{:.1})",
mouse_pos.0,
mouse_pos.1
));
});
true
}

View File

@ -1,153 +1,173 @@
use glium::{DisplayBuild, Surface};
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 imgui::{ImGui, Ui};
use std::time::Instant;
pub struct Support {
display: GlutinFacade,
imgui: ImGui,
renderer: Renderer,
last_frame: Instant,
mouse_pos: (i32, i32),
mouse_pressed: (bool, bool, bool),
mouse_wheel: f32,
#[derive(Copy, Clone, PartialEq, Debug, Default)]
struct MouseState {
pos: (i32, i32),
pressed: (bool, bool, bool),
wheel: f32,
}
impl Support {
pub fn init() -> Support {
let display = glutin::WindowBuilder::new().build_glium().unwrap();
pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) {
use glium::glutin;
use glium::{Display, Surface};
use imgui_glium_renderer::Renderer;
let mut imgui = ImGui::init();
let renderer = Renderer::init(&mut imgui, &display).unwrap();
let mut events_loop = glutin::EventsLoop::new();
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);
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);
let mut imgui = ImGui::init();
let mut renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
Support {
display: display,
imgui: imgui,
renderer: renderer,
last_frame: Instant::now(),
mouse_pos: (0, 0),
mouse_pressed: (false, false, false),
mouse_wheel: 0.0,
}
}
configure_keys(&mut imgui);
pub fn update_mouse(&mut self) {
let scale = self.imgui.display_framebuffer_scale();
self.imgui
.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;
}
let mut last_frame = Instant::now();
let mut mouse_state = MouseState::default();
let mut quit = false;
pub fn render<F: FnMut(&Ui)>(&mut self, clear_color: (f32, f32, f32, f32), mut run_ui: F) {
let now = Instant::now();
let delta = now - self.last_frame;
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
self.last_frame = now;
loop {
events_loop.poll_events(|event| {
use glium::glutin::WindowEvent::*;
use glium::glutin::ElementState::Pressed;
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();
target.clear_color(clear_color.0, clear_color.1, clear_color.2, clear_color.3);
let window = self.display.get_window().unwrap();
let size_points = window.get_inner_size_points().unwrap();
let size_pixels = window.get_inner_size_pixels().unwrap();
let ui = self.imgui.frame(size_points, size_pixels, delta_s);
run_ui(&ui);
self.renderer.render(&mut target, ui).unwrap();
target.finish().unwrap();
}
pub fn update_events(&mut self) -> bool {
for event in self.display.poll_events() {
match event {
Event::Closed => return false,
Event::KeyboardInput(state, _, code) => {
let pressed = state == ElementState::Pressed;
match code {
Some(VirtualKeyCode::Tab) => self.imgui.set_key(0, pressed),
Some(VirtualKeyCode::Left) => self.imgui.set_key(1, pressed),
Some(VirtualKeyCode::Right) => self.imgui.set_key(2, pressed),
Some(VirtualKeyCode::Up) => self.imgui.set_key(3, 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),
_ => {}
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),
_ => (),
}
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;
}

View File

@ -1,33 +1,33 @@
use gfx;
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 imgui::{ImGui, Ui};
use std::time::Instant;
type ColorFormat = gfx::format::Rgba8;
type DepthFormat = gfx::format::DepthStencil;
#[derive(Copy, Clone, PartialEq, Debug, Default)]
struct MouseState {
pos: (i32, i32),
pressed: (bool, bool, bool),
wheel: f32
wheel: f32,
}
pub fn run<F: FnMut(&Ui)>(title: String, clear_color: [f32; 4], mut run_ui: F) {
let mut imgui = ImGui::init();
pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_ui: F) {
use gfx::{self, Device};
use gfx_window_glutin;
use glutin::{self, GlContext};
use imgui_gfx_renderer::Renderer;
let events_loop = glutin::EventsLoop::new();
let builder = glutin::WindowBuilder::new()
type ColorFormat = gfx::format::Rgba8;
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_dimensions(1024, 768)
.with_vsync();
.with_dimensions(1024, 768);
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 imgui = ImGui::init();
let mut renderer = Renderer::init(&mut imgui, &mut factory, main_color.clone())
.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;
loop {
events_loop.poll_events(|glutin::Event::WindowEvent{event, ..}| {
match event {
WindowEvent::Resized(_, _) => {
gfx_window_glutin::update_views(&window, &mut main_color, &mut main_depth);
renderer.update_render_target(main_color.clone());
}
WindowEvent::Closed => quit = true,
WindowEvent::KeyboardInput(state, _, code, _) => {
let pressed = state == ElementState::Pressed;
match code {
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),
_ => {}
events_loop.poll_events(|event| {
use glutin::WindowEvent::*;
use glutin::ElementState::Pressed;
use glutin::{Event, MouseButton, MouseScrollDelta, TouchPhase};
if let Event::WindowEvent { event, .. } = event {
match event {
Resized(_, _) => {
gfx_window_glutin::update_views(&window, &mut main_color, &mut main_depth);
renderer.update_render_target(main_color.clone());
}
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 ui = imgui.frame(size_points, size_pixels, delta_s);
run_ui(&ui);
if !run_ui(&ui) {
break;
}
encoder.clear(&mut main_color, clear_color);
renderer.render(ui, &mut factory, &mut encoder)
.expect("Rendering failed");
renderer.render(ui, &mut factory, &mut encoder).expect(
"Rendering failed",
);
encoder.flush(&mut device);
window.swap_buffers().unwrap();
window.context().swap_buffers().unwrap();
device.cleanup();
if quit { break }
};
if quit {
break;
}
}
}
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);
@ -149,13 +163,19 @@ fn configure_keys(imgui: &mut ImGui) {
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_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;
}

View File

@ -2,21 +2,14 @@ extern crate glium;
extern crate imgui;
extern crate imgui_glium_renderer;
use self::support::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() {
let mut support = Support::init();
loop {
support::run("test_window.rs".to_owned(), CLEAR_COLOR, |ui| {
let mut open = true;
support.render(CLEAR_COLOR, |ui| ui.show_test_window(&mut open));
let active = support.update_events();
if !active || !open {
break;
}
}
ui.show_test_window(&mut open);
open
});
}

View File

@ -5,12 +5,9 @@ extern crate imgui_glium_renderer;
use imgui::*;
use self::support::Support;
mod support;
struct State {
clear_color: (f32, f32, f32, f32),
show_app_metrics: bool,
show_app_main_menu_bar: bool,
show_app_console: bool,
@ -54,7 +51,6 @@ impl Default for State {
let mut text = ImString::with_capacity(128);
text.push_str("Hello, world!");
State {
clear_color: (114.0 / 255.0, 144.0 / 255.0, 154.0 / 255.0, 1.0),
show_app_metrics: false,
show_app_main_menu_bar: false,
show_app_console: false,
@ -109,19 +105,16 @@ impl Default for AutoResizeState {
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() {
let mut state = State::default();
let mut support = Support::init();
let mut opened = true;
loop {
support.render(state.clear_color,
|ui| { show_test_window(ui, &mut state, &mut opened); });
let active = support.update_events();
if !active || !opened {
break;
}
}
support::run("test_window.rs".to_owned(), CLEAR_COLOR, |ui| {
let mut open = true;
show_test_window(ui, &mut state, &mut open);
open
});
}
fn show_user_guide<'a>(ui: &Ui<'a>) {

View File

@ -9,6 +9,6 @@ license = "MIT/Apache-2.0"
categories = ["gui", "rendering"]
[dependencies]
glium = { version = "0.16", default-features = false }
glium = { version = "0.17", default-features = false }
imgui = { version = "0.0.15-pre", path = "../" }
imgui-sys = { version = "0.0.15-pre", path = "../imgui-sys", features = ["glium"] }

View File

@ -11,7 +11,7 @@ build = "build.rs"
[dependencies]
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 }
[build-dependencies]

View File

@ -22,13 +22,26 @@ impl Vertex for ImDrawVert {
fn build_bindings() -> VertexFormat {
unsafe {
let dummy: &ImDrawVert = mem::transmute(0usize);
Cow::Owned(vec![("pos".into(),
mem::transmute(&dummy.pos),
<ImVec2 as Attribute>::get_type()),
("uv".into(),
mem::transmute(&dummy.uv),
<ImVec2 as Attribute>::get_type()),
("col".into(), mem::transmute(&dummy.col), AttributeType::U8U8U8U8)])
Cow::Owned(vec![
(
"pos".into(),
mem::transmute(&dummy.pos),
<ImVec2 as Attribute>::get_type(),
false
),
(
"uv".into(),
mem::transmute(&dummy.uv),
<ImVec2 as Attribute>::get_type(),
false
),
(
"col".into(),
mem::transmute(&dummy.col),
AttributeType::U8U8U8U8,
false
),
])
}
}
}