Skip to content

Commit

Permalink
API changes + additions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Aug 20, 2015
1 parent e261db7 commit 8bf34e8
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 48 deletions.
83 changes: 54 additions & 29 deletions examples/support/mod.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,80 @@
use glium::{DisplayBuild, Surface};
use glium::backend::glutin_backend::GlutinFacade;
use glium::glutin;
use glium::glutin::{ElementState, Event, MouseButton, VirtualKeyCode};
use imgui::{ImGui, Frame};
use imgui::glium_renderer::Renderer;
use time::SteadyTime;

pub fn main_with_frame<'a, F: Fn(&Frame<'a>)>(f: F) {
let display = glutin::WindowBuilder::new()
.build_glium()
.unwrap();
pub struct Support {
display: GlutinFacade,
imgui: ImGui,
renderer: Renderer,
last_frame: SteadyTime,
mouse_pos: (i32, i32),
mouse_pressed: (bool, bool, bool)
}

impl Support {
pub fn init() -> Support {
let display = glutin::WindowBuilder::new()
.build_glium()
.unwrap();

let mut imgui = ImGui::init();
let renderer = Renderer::init(&mut imgui, &display).unwrap();

let mut imgui = ImGui::init();
let mut renderer = Renderer::init(&mut imgui, &display).unwrap();
Support {
display: display,
imgui: imgui,
renderer: renderer,
last_frame: SteadyTime::now(),
mouse_pos: (0, 0),
mouse_pressed: (false, false, false)
}
}

let mut last_frame = SteadyTime::now();
let mut mouse_pos = (0, 0);
let mut mouse_pressed = (false, false, false);
pub fn update_mouse(&mut self) {
self.imgui.set_mouse_pos(self.mouse_pos.0 as f32, self.mouse_pos.1 as f32);
self.imgui.set_mouse_down(&[self.mouse_pressed.0, self.mouse_pressed.1, self.mouse_pressed.2, false, false]);
}

'main: loop {
pub fn render<'fr, 'a: 'fr , F: FnMut(&Frame<'fr>) -> bool>(
&'a mut self, clear_color: (f32, f32, f32, f32), mut f: F) -> bool {
let mut result;
let now = SteadyTime::now();
let delta = now - last_frame;
let delta = now - self.last_frame;
let delta_f = delta.num_nanoseconds().unwrap() as f32 / 1_000_000_000.0;
last_frame = now;
self.last_frame = now;

imgui.set_mouse_pos(mouse_pos.0 as f32, mouse_pos.1 as f32);
imgui.set_mouse_down(&[mouse_pressed.0, mouse_pressed.1, mouse_pressed.2, false, false]);
self.update_mouse();

let mut target = display.draw();
target.clear_color(1.0, 1.0, 1.0, 1.0);
let mut target = self.display.draw();
target.clear_color(clear_color.0, clear_color.1,
clear_color.2, clear_color.3);

let (width, height) = target.get_dimensions();
let frame = imgui.frame(width, height, delta_f);
f(&frame);
renderer.render(&mut target, frame).unwrap();
let frame = self.imgui.frame(width, height, delta_f);
result = f(&frame);
self.renderer.render(&mut target, frame).unwrap();

target.finish().unwrap();

for event in display.poll_events() {
for event in self.display.poll_events() {
match event {
Event::Closed |
Event::KeyboardInput(ElementState::Pressed, _, Some(VirtualKeyCode::Escape))
=> break 'main,
Event::MouseMoved(pos) => mouse_pos = pos,
Event::MouseInput(state, MouseButton::Left) =>
mouse_pressed.0 = state == ElementState::Pressed,
Event::MouseInput(state, MouseButton::Right) =>
mouse_pressed.1 = state == ElementState::Pressed,
Event::MouseInput(state, MouseButton::Middle) =>
mouse_pressed.2 = state == ElementState::Pressed,
_ => ()
=> result = false,
Event::MouseMoved(pos) => self.mouse_pos = pos,
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,
_ => ()
}
}
result
}
}
184 changes: 173 additions & 11 deletions examples/test_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,166 @@ extern crate glium;
extern crate imgui;
extern crate time;

use imgui::Frame;
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,
show_app_layout: bool,
show_app_long_text: bool,
show_app_auto_resize: bool,
show_app_fixed_overlay: bool,
show_app_custom_rendering: bool,
show_app_manipulating_window_title: bool,
show_app_about: bool,
no_titlebar: bool,
no_border: bool,
no_resize: bool,
no_move: bool,
no_scrollbar: bool,
no_collapse: bool,
no_menu: bool,
bg_alpha: f32,
file_menu: FileMenuState
}

impl Default for State {
fn default() -> Self {
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,
show_app_layout: false,
show_app_long_text: false,
show_app_auto_resize: false,
show_app_fixed_overlay: false,
show_app_custom_rendering: false,
show_app_manipulating_window_title: false,
show_app_about: false,
no_titlebar: false,
no_border: false,
no_resize: false,
no_move: false,
no_scrollbar: false,
no_collapse: false,
no_menu: false,
bg_alpha: 0.65,
file_menu: Default::default()
}
}
}

struct FileMenuState {
enabled: bool
}

impl Default for FileMenuState {
fn default() -> Self {
FileMenuState {
enabled: true
}
}
}

fn main() {
// let mut show_app_metrics = false;
let show_app_main_menu_bar = true;
let mut state = State {
.. Default::default()
};
let mut support = Support::init();

support::main_with_frame(|frame| {
// if show_app_metrics { show_metrics_window(&mut show_app_metrics) }
if show_app_main_menu_bar { show_example_app_main_menu_bar(frame) }
});
loop {
let active = support.render(state.clear_color, |frame| {
show_test_window(frame, &mut state)
});
if !active { break }
}
}

fn show_example_app_main_menu_bar<'a>(frame: &Frame<'a>) {
fn show_test_window<'a>(frame: &Frame<'a>, state: &mut State) -> bool {
if state.show_app_main_menu_bar { show_example_app_main_menu_bar(frame, state) }
if state.show_app_fixed_overlay {
state.show_app_fixed_overlay = show_example_app_fixed_overlay(frame);
}
if state.show_app_about {
state.show_app_about = frame.window()
.name(im_str!("About ImGui"))
.always_auto_resize(true)
.closable(true)
.build(|| {
frame.text(ImStr::from_str(&format!("ImGui {}", imgui::get_version())));
frame.separator();
frame.text(im_str!("By Omar Cornut and all github contributors."));
frame.text(im_str!("ImGui is licensed under the MIT License, see LICENSE for more information."));
})
}

frame.window().name(im_str!("ImGui Demo"))
.title_bar(!state.no_titlebar)
.show_borders(!state.no_border)
.resizable(!state.no_resize)
.movable(!state.no_move)
.scroll_bar(!state.no_scrollbar)
.collapsible(!state.no_collapse)
.menu_bar(!state.no_menu)
.bg_alpha(state.bg_alpha)
.size((550.0, 680.0), ImGuiSetCond_FirstUseEver)
.closable(true)
.build(|| {
frame.text(im_str!("ImGui says hello."));
frame.menu_bar(|| {
frame.menu(im_str!("Menu")).build(|| {
show_example_menu_file(frame, &mut state.file_menu);
});
frame.menu(im_str!("Examples")).build(|| {
if frame.menu_item(im_str!("Main menu bar")).build() {
state.show_app_main_menu_bar = !state.show_app_main_menu_bar;
}
if frame.menu_item(im_str!("Console")).build() {
state.show_app_console = !state.show_app_console;
}
if frame.menu_item(im_str!("Simple layout")).build() {
state.show_app_layout = !state.show_app_layout;
}
if frame.menu_item(im_str!("Long text display")).build() {
state.show_app_long_text = !state.show_app_long_text;
}
if frame.menu_item(im_str!("Auto-resizing window")).build() {
state.show_app_auto_resize = !state.show_app_auto_resize;
}
if frame.menu_item(im_str!("Simple overlay")).build() {
state.show_app_fixed_overlay = !state.show_app_fixed_overlay;
}
if frame.menu_item(im_str!("Manipulating window title")).build() {
state.show_app_manipulating_window_title =
!state.show_app_manipulating_window_title;
}
if frame.menu_item(im_str!("Custom rendering")).build() {
state.show_app_custom_rendering = !state.show_app_custom_rendering;
}
});
frame.menu(im_str!("Help")).build(|| {
if frame.menu_item(im_str!("Metrics")).build() {
state.show_app_metrics = !state.show_app_metrics;
}
if frame.menu_item(im_str!("About ImGui")).build() {
state.show_app_about = !state.show_app_about;
}
});
});
})
}

fn show_example_app_main_menu_bar<'a>(frame: &Frame<'a>, state: &mut State) {
frame.main_menu_bar(|| {
frame.menu(im_str!("File")).build(|| {
show_example_menu_file(frame);
show_example_menu_file(frame, &mut state.file_menu);
});
frame.menu(im_str!("Edit")).build(|| {
if frame.menu_item(im_str!("Undo")).shortcut(im_str!("CTRL+Z")).build() { }
Expand All @@ -35,7 +177,7 @@ fn show_example_app_main_menu_bar<'a>(frame: &Frame<'a>) {
});
}

fn show_example_menu_file<'a>(frame: &Frame<'a>) {
fn show_example_menu_file<'a>(frame: &Frame<'a>, state: &mut FileMenuState) {
frame.menu_item(im_str!("(dummy menu)")).enabled(false).build();
if frame.menu_item(im_str!("New")).build() { }
if frame.menu_item(im_str!("Open")).shortcut(im_str!("Ctrl+O")).build() { }
Expand All @@ -47,14 +189,17 @@ fn show_example_menu_file<'a>(frame: &Frame<'a>) {
frame.menu_item(im_str!("Hello"));
frame.menu_item(im_str!("Sailor"));
frame.menu(im_str!("Recurse..")).build(|| {
show_example_menu_file(frame);
show_example_menu_file(frame, state);
});
});
});
if frame.menu_item(im_str!("Save")).shortcut(im_str!("Ctrl+S")).build() { }
if frame.menu_item(im_str!("Save As..")).build() { }
frame.separator();
frame.menu(im_str!("Options")).build(|| {
if frame.menu_item(im_str!("Enabled")).selected(state.enabled).build() {
state.enabled = !state.enabled;
}
// TODO
});
frame.menu(im_str!("Colors")).build(|| {
Expand All @@ -66,3 +211,20 @@ fn show_example_menu_file<'a>(frame: &Frame<'a>) {
if frame.menu_item(im_str!("Checked")).selected(true).build() { }
if frame.menu_item(im_str!("Quit")).shortcut(im_str!("Alt+F4")).build() { }
}

fn show_example_app_fixed_overlay<'a>(frame: &Frame<'a>) -> bool {
frame.window()
.name(im_str!("Example: Fixed Overlay"))
.closable(true)
.bg_alpha(0.3)
.title_bar(false)
.resizable(false)
.movable(false)
.save_settings(false)
.build(|| {
frame.text(im_str!("Simple overlay\non the top-left side of the screen."));
frame.separator();
let mouse_pos = frame.imgui().mouse_pos();
frame.text(im_str!("Mouse Position: ({:.1},{:.1})", mouse_pos.0, mouse_pos.1));
})
}
7 changes: 7 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ bitflags!(
}
);

impl ImGuiWindowFlags {
#[inline]
pub fn with(self, mask: ImGuiWindowFlags, value: bool) -> ImGuiWindowFlags {
if value { self | mask } else { self - mask }
}
}

bitflags!(
#[repr(C)]
flags ImGuiSetCond: c_int {
Expand Down
Loading

0 comments on commit 8bf34e8

Please sign in to comment.