Skip to content

Commit

Permalink
Pull input+winit stuff from 0.1-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Jun 27, 2019
1 parent 0825a8c commit 721bf46
Show file tree
Hide file tree
Showing 15 changed files with 765 additions and 477 deletions.
35 changes: 11 additions & 24 deletions imgui-examples/examples/support_gfx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use imgui::{FontGlyphRange, ImFontConfig, Context, Ui};
use imgui_gfx_renderer::{Renderer, Shaders};
use imgui_winit_support;
use imgui_winit_support::{WinitPlatform, HiDpiMode};
use std::time::Instant;

#[cfg(feature = "opengl")]
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;
use gfx::{Device};

type ColorFormat = gfx::format::Rgba8;
type DepthFormat = gfx::format::DepthStencil;
Expand Down Expand Up @@ -60,10 +58,10 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
}
imgui.set_ini_filename(None);

// In the examples we only use integer DPI factors, because the UI can get very blurry
// otherwise. This might or might not be what you want in a real application.
let hidpi_factor = window.get_hidpi_factor().round();
let mut platform = WinitPlatform::init(&mut imgui);
platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Rounded);

let hidpi_factor = platform.hidpi_factor();
let font_size = (13.0 * hidpi_factor) as f32;

imgui.fonts().add_default_font_with_config(
Expand All @@ -89,8 +87,6 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
let mut renderer = Renderer::init(&mut imgui, &mut factory, shaders, main_color.clone())
.expect("Failed to initialize renderer");

imgui_winit_support::configure_keys(&mut imgui);

let mut last_frame = Instant::now();
let mut quit = false;

Expand All @@ -101,12 +97,7 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
WindowEvent::{CloseRequested, Resized},
};

imgui_winit_support::handle_event(
&mut imgui,
&event,
window.get_hidpi_factor(),
hidpi_factor,
);
platform.handle_event(imgui.io_mut(), &window, &event);

if let Event::WindowEvent { event, .. } = event {
match event {
Expand All @@ -123,16 +114,11 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
break;
}

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;

imgui_winit_support::update_mouse_cursor(&imgui, &window);
let io = imgui.io_mut();
platform.prepare_frame(io, &window).expect("Failed to start frame");
last_frame = io.update_delta_time(last_frame);

let frame_size = imgui_winit_support::get_frame_size(&window, hidpi_factor).unwrap();

let ui = imgui.frame(frame_size, delta_s);
let ui = imgui.frame();
if !run_ui(&ui) {
break;
}
Expand Down Expand Up @@ -266,6 +252,7 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
}

encoder.clear(&main_color, clear_color);
platform.prepare_render(&ui, &window);
renderer
.render(ui, &mut factory, &mut encoder)
.expect("Rendering failed");
Expand Down
8 changes: 3 additions & 5 deletions imgui-gfx-renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use gfx::pso::{PipelineData, PipelineState};
use gfx::texture::{FilterMethod, SamplerInfo, WrapMode};
use gfx::traits::FactoryExt;
use gfx::{CommandBuffer, Encoder, Factory, IntoIndexBuffer, Rect, Resources, Slice};
use imgui::{Context, DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImTexture, Textures, Ui};
use imgui::{Context, DrawList, ImDrawIdx, ImDrawVert, ImTexture, Textures, Ui};

pub type RendererResult<T> = Result<T, RendererError>;

Expand Down Expand Up @@ -261,10 +261,8 @@ impl<R: Resources> Renderer<R> {
factory: &mut F,
encoder: &mut Encoder<R, C>,
) -> RendererResult<()> {
let FrameSize {
logical_size: (width, height),
hidpi_factor,
} = ui.frame_size();
let [width, height] = ui.io().display_size;
let hidpi_factor = ui.io().display_framebuffer_scale[0];

if !(width > 0.0 && height > 0.0) {
return Ok(());
Expand Down
36 changes: 13 additions & 23 deletions imgui-glium-examples/examples/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use glium::{
Texture2d,
};
use imgui::{FontGlyphRange, ImFontConfig, self, Ui};
use imgui_winit_support;
use imgui_winit_support::{HiDpiMode, WinitPlatform};
use std::rc::Rc;
use std::time::Instant;

Expand All @@ -29,10 +29,10 @@ where
let mut imgui = imgui::Context::create();
imgui.set_ini_filename(None);

// In the examples we only use integer DPI factors, because the UI can get very blurry
// otherwise. This might or might not be what you want in a real application.
let hidpi_factor = window.get_hidpi_factor().round();
let mut platform = WinitPlatform::init(&mut imgui);
platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Rounded);

let hidpi_factor = platform.hidpi_factor();
let font_size = (13.0 * hidpi_factor) as f32;

imgui.fonts().add_default_font_with_config(
Expand All @@ -53,25 +53,18 @@ where
&FontGlyphRange::japanese(),
);

imgui.set_font_global_scale((1.0 / hidpi_factor) as f32);
imgui.io_mut().font_global_scale = (1.0 / hidpi_factor) as f32;

let mut renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");

imgui_winit_support::configure_keys(&mut imgui);

let mut last_frame = Instant::now();
let mut quit = false;

loop {
events_loop.poll_events(|event| {
use glium::glutin::{Event, WindowEvent::CloseRequested};

imgui_winit_support::handle_event(
&mut imgui,
&event,
window.get_hidpi_factor(),
hidpi_factor,
);
platform.handle_event(imgui.io_mut(), &window, &event);

if let Event::WindowEvent { event, .. } = event {
match event {
Expand All @@ -81,16 +74,12 @@ where
}
});

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;

imgui_winit_support::update_mouse_cursor(&imgui, &window);

let frame_size = imgui_winit_support::get_frame_size(&window, hidpi_factor).unwrap();

let ui = imgui.frame(frame_size, delta_s);
let io = imgui.io_mut();
platform
.prepare_frame(io, &window)
.expect("Failed to start frame");
last_frame = io.update_delta_time(last_frame);
let ui = imgui.frame();
if !run_ui(&ui, display.get_context(), renderer.textures()) {
break;
}
Expand All @@ -102,6 +91,7 @@ where
clear_color[2],
clear_color[3],
);
platform.prepare_render(&ui, &window);
renderer.render(&mut target, ui).expect("Rendering failed");
target.finish().unwrap();

Expand Down
6 changes: 3 additions & 3 deletions imgui-glium-examples/examples/test_window_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1051,17 +1051,17 @@ fn show_example_app_custom_rendering(ui: &Ui, state: &mut CustomRenderingState,
if state.adding_line {
adding_preview = true;
state.points.push(mouse_pos_in_canvas);
if !ui.imgui().is_mouse_down(ImMouseButton::Left) {
if !ui.imgui().is_mouse_down(MouseButton::Left) {
state.adding_line = false;
adding_preview = false;
}
}
if ui.is_item_hovered() {
if !state.adding_line && ui.imgui().is_mouse_clicked(ImMouseButton::Left) {
if !state.adding_line && ui.imgui().is_mouse_clicked(MouseButton::Left) {
state.points.push(mouse_pos_in_canvas);
state.adding_line = true;
}
if ui.imgui().is_mouse_clicked(ImMouseButton::Right) && !state.points.is_empty() {
if ui.imgui().is_mouse_clicked(MouseButton::Right) && !state.points.is_empty() {
state.adding_line = false;
adding_preview = false;
state.points.pop();
Expand Down
8 changes: 3 additions & 5 deletions imgui-glium-renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use glium::program;
use glium::texture;
use glium::vertex;
use glium::{uniform, DrawError, IndexBuffer, Program, Surface, Texture2d, VertexBuffer};
use imgui::{self, DrawList, FrameSize, ImTexture, Textures, Ui};
use imgui::{self, DrawList, ImTexture, Textures, Ui};
use std::borrow::Cow;
use std::fmt;
use std::rc::Rc;
Expand Down Expand Up @@ -85,10 +85,8 @@ impl Renderer {

pub fn render<'a, S: Surface>(&mut self, surface: &mut S, ui: Ui<'a>) -> RendererResult<()> {
let _ = self.ctx.insert_debug_marker("imgui-rs: starting rendering");
let FrameSize {
logical_size: (width, height),
hidpi_factor,
} = ui.frame_size();
let [width, height] = ui.io().display_size;
let hidpi_factor = ui.io().display_framebuffer_scale[0];
if !(width > 0.0 && height > 0.0) {
return Ok(());
}
Expand Down
99 changes: 0 additions & 99 deletions imgui-sys/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,102 +26,3 @@ impl ImGuiDataType {
ImGuiDataType::Double,
];
}

/// A key identifier (ImGui-side enum)
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ImGuiKey {
Tab,
LeftArrow,
RightArrow,
UpArrow,
DownArrow,
PageUp,
PageDown,
Home,
End,
Insert,
Delete,
Backspace,
Space,
Enter,
Escape,
/// for text edit CTRL+A: select all
A,
/// for text edit CTRL+C: copy
C,
/// for text edit CTRL+V: paste
V,
/// for text edit CTRL+X: cut
X,
/// for text edit CTRL+Y: redo
Y,
/// for text edit CTRL+Z: undo
Z,
}
impl ImGuiKey {
/// All possible `ImGuiKey` variants
pub const VARIANTS: [ImGuiKey; 21] = [
ImGuiKey::Tab,
ImGuiKey::LeftArrow,
ImGuiKey::RightArrow,
ImGuiKey::UpArrow,
ImGuiKey::DownArrow,
ImGuiKey::PageUp,
ImGuiKey::PageDown,
ImGuiKey::Home,
ImGuiKey::End,
ImGuiKey::Insert,
ImGuiKey::Delete,
ImGuiKey::Backspace,
ImGuiKey::Space,
ImGuiKey::Enter,
ImGuiKey::Escape,
ImGuiKey::A,
ImGuiKey::C,
ImGuiKey::V,
ImGuiKey::X,
ImGuiKey::Y,
ImGuiKey::Z,
];
pub const COUNT: usize = 21;
}

/// A mouse cursor identifier
///
/// User code may request binding to display given cursor, which is why we have some cursors that
/// are marked unused here
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ImGuiMouseCursor {
None = -1,
Arrow = 0,
/// When hovering over InputText, etc.
TextInput,
/// (Unused by imgui functions)
ResizeAll,
/// When hovering over an horizontal border
ResizeNS,
/// When hovering over a vertical border or a column
ResizeEW,
/// When hovering over the bottom-left corner of a window
ResizeNESW,
/// When hovering over the bottom-right corner of a window
ResizeNWSE,
/// (Unused by imgui functions. Use for e.g. hyperlinks)
Hand,
}
impl ImGuiMouseCursor {
/// All possible `ImGuiMouseCursor` variants, except None
pub const VARIANTS: [ImGuiMouseCursor; 8] = [
// None variant intentionally skipped
ImGuiMouseCursor::Arrow,
ImGuiMouseCursor::TextInput,
ImGuiMouseCursor::ResizeAll,
ImGuiMouseCursor::ResizeNS,
ImGuiMouseCursor::ResizeEW,
ImGuiMouseCursor::ResizeNESW,
ImGuiMouseCursor::ResizeNWSE,
ImGuiMouseCursor::Hand,
];
}
25 changes: 16 additions & 9 deletions imgui-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ pub use bindings::{
ImGuiIO_ClearInputCharacters, ImGuiInputTextCallback, ImGuiInputTextCallbackData,
ImGuiInputTextCallbackData_DeleteChars, ImGuiInputTextCallbackData_HasSelection,
ImGuiInputTextCallbackData_ImGuiInputTextCallbackData, ImGuiInputTextCallbackData_InsertChars,
ImGuiInputTextCallbackData_destroy, ImGuiKey_COUNT, ImGuiListClipper, ImGuiNavInput_,
ImGuiNavInput_Activate, ImGuiNavInput_COUNT, ImGuiNavInput_Cancel, ImGuiNavInput_DpadDown,
ImGuiNavInput_DpadLeft, ImGuiNavInput_DpadRight, ImGuiNavInput_DpadUp, ImGuiNavInput_FocusNext,
ImGuiNavInput_FocusPrev, ImGuiNavInput_Input, ImGuiNavInput_InternalStart_,
ImGuiNavInput_KeyDown_, ImGuiNavInput_KeyLeft_, ImGuiNavInput_KeyMenu_,
ImGuiNavInput_KeyRight_, ImGuiNavInput_KeyTab_, ImGuiNavInput_KeyUp_, ImGuiNavInput_LStickDown,
ImGuiNavInput_LStickLeft, ImGuiNavInput_LStickRight, ImGuiNavInput_LStickUp,
ImGuiNavInput_Menu, ImGuiNavInput_TweakFast, ImGuiNavInput_TweakSlow, ImGuiPayload,
ImGuiSizeCallback, ImGuiStorage, ImGuiStyle, ImGuiStyleVar, ImGuiStyleVar_,
ImGuiInputTextCallbackData_destroy, ImGuiKey, ImGuiKey_, ImGuiKey_A, ImGuiKey_Backspace,
ImGuiKey_C, ImGuiKey_COUNT, ImGuiKey_Delete, ImGuiKey_DownArrow, ImGuiKey_End, ImGuiKey_Enter,
ImGuiKey_Escape, ImGuiKey_Home, ImGuiKey_Insert, ImGuiKey_LeftArrow, ImGuiKey_PageDown,
ImGuiKey_PageUp, ImGuiKey_RightArrow, ImGuiKey_Space, ImGuiKey_Tab, ImGuiKey_UpArrow,
ImGuiKey_V, ImGuiKey_X, ImGuiKey_Y, ImGuiKey_Z, ImGuiListClipper, ImGuiMouseCursor,
ImGuiMouseCursor_, ImGuiMouseCursor_Arrow, ImGuiMouseCursor_COUNT, ImGuiMouseCursor_Hand,
ImGuiMouseCursor_None, ImGuiMouseCursor_ResizeAll, ImGuiMouseCursor_ResizeEW,
ImGuiMouseCursor_ResizeNESW, ImGuiMouseCursor_ResizeNS, ImGuiMouseCursor_ResizeNWSE,
ImGuiMouseCursor_TextInput, ImGuiNavInput_, ImGuiNavInput_Activate, ImGuiNavInput_COUNT,
ImGuiNavInput_Cancel, ImGuiNavInput_DpadDown, ImGuiNavInput_DpadLeft, ImGuiNavInput_DpadRight,
ImGuiNavInput_DpadUp, ImGuiNavInput_FocusNext, ImGuiNavInput_FocusPrev, ImGuiNavInput_Input,
ImGuiNavInput_InternalStart_, ImGuiNavInput_KeyDown_, ImGuiNavInput_KeyLeft_,
ImGuiNavInput_KeyMenu_, ImGuiNavInput_KeyRight_, ImGuiNavInput_KeyTab_, ImGuiNavInput_KeyUp_,
ImGuiNavInput_LStickDown, ImGuiNavInput_LStickLeft, ImGuiNavInput_LStickRight,
ImGuiNavInput_LStickUp, ImGuiNavInput_Menu, ImGuiNavInput_TweakFast, ImGuiNavInput_TweakSlow,
ImGuiPayload, ImGuiSizeCallback, ImGuiStorage, ImGuiStyle, ImGuiStyleVar, ImGuiStyleVar_,
ImGuiStyleVar_Alpha, ImGuiStyleVar_ButtonTextAlign, ImGuiStyleVar_COUNT,
ImGuiStyleVar_ChildBorderSize, ImGuiStyleVar_ChildRounding, ImGuiStyleVar_FrameBorderSize,
ImGuiStyleVar_FramePadding, ImGuiStyleVar_FrameRounding, ImGuiStyleVar_GrabMinSize,
Expand Down
Loading

0 comments on commit 721bf46

Please sign in to comment.