Skip to content

Commit

Permalink
Pull font API and associated refactoring from 0.1-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Jun 29, 2019
1 parent f60c597 commit cebe02c
Show file tree
Hide file tree
Showing 24 changed files with 1,317 additions and 721 deletions.
16 changes: 0 additions & 16 deletions imgui-examples/examples/LICENSE.mplus

This file was deleted.

6 changes: 2 additions & 4 deletions imgui-examples/examples/color_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use imgui::*;

mod support;

const CLEAR_COLOR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];

struct State {
example: i32,
notify_text: &'static str,
Expand All @@ -25,15 +23,15 @@ impl Default for State {
}

fn main() {
let system = support::init(file!());
let mut state = State::default();
support::run("color_button.rs".to_owned(), CLEAR_COLOR, |ui, _, _| {
system.main_loop(|_, ui| {
example_selector(&mut state, ui);
match state.example {
1 => example_1(&mut state, ui),
2 => example_2(ui),
_ => (),
}
true
});
}

Expand Down
24 changes: 7 additions & 17 deletions imgui-examples/examples/custom_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use image::{jpeg::JPEGDecoder, ImageDecoder};
use imgui::*;

mod support;
use self::support::Textures;

const CLEAR_COLOR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];

#[derive(Default)]
struct CustomTexturesApp {
Expand All @@ -31,7 +28,7 @@ impl CustomTexturesApp {
fn register_textures<F>(
&mut self,
gl_ctx: &F,
textures: &mut Textures,
textures: &mut Textures<Rc<Texture2d>>,
) -> Result<(), Box<dyn Error>>
where
F: Facade,
Expand Down Expand Up @@ -89,7 +86,7 @@ impl CustomTexturesApp {
}

impl Lenna {
fn new<F>(gl_ctx: &F, textures: &mut Textures) -> Result<Self, Box<dyn Error>>
fn new<F>(gl_ctx: &F, textures: &mut Textures<Rc<Texture2d>>) -> Result<Self, Box<dyn Error>>
where
F: Facade,
{
Expand Down Expand Up @@ -121,16 +118,9 @@ impl Lenna {
fn main() {
let mut my_app = CustomTexturesApp::default();

support::run(
"custom_textures.rs".to_owned(),
CLEAR_COLOR,
|ui, gl_ctx, textures| {
if let Err(e) = my_app.register_textures(gl_ctx, textures) {
panic!("Failed to register textures! {}", e);
}
my_app.show_textures(ui);

true
},
);
let mut system = support::init(file!());
my_app
.register_textures(system.display.get_context(), system.renderer.textures())
.expect("Failed to register textures");
system.main_loop(|_, ui| my_app.show_textures(ui));
}
38 changes: 15 additions & 23 deletions imgui-examples/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@ use imgui::*;

mod support;

const CLEAR_COLOR: [f32; 4] = [1.0, 1.0, 1.0, 1.0];

fn main() {
support::run("hello_world.rs".to_owned(), CLEAR_COLOR, |ui, _, _| {
hello_world(ui)
let system = support::init(file!());
system.main_loop(|_, ui| {
ui.window(im_str!("Hello world"))
.size([300.0, 100.0], Condition::FirstUseEver)
.build(|| {
ui.text(im_str!("Hello world!"));
ui.text(im_str!("こんにちは世界!"));
ui.text(im_str!("This...is...imgui-rs!"));
ui.separator();
let mouse_pos = ui.io().mouse_pos;
ui.text(format!(
"Mouse Position: ({:.1},{:.1})",
mouse_pos[0], mouse_pos[1]
));
});
});
}

fn hello_world<'a>(ui: &Ui<'a>) -> bool {
ui.window(im_str!("Hello world"))
.size([300.0, 100.0], Condition::FirstUseEver)
.build(|| {
ui.text(im_str!("Hello world!"));
ui.text(im_str!("こんにちは世界!"));
ui.text(im_str!("This...is...imgui-rs!"));
ui.separator();
let mouse_pos = ui.io().mouse_pos;
ui.text(im_str!(
"Mouse Position: ({:.1},{:.1})",
mouse_pos[0],
mouse_pos[1]
));
});

true
}
167 changes: 88 additions & 79 deletions imgui-examples/examples/support/mod.rs
Original file line number Diff line number Diff line change
@@ -1,103 +1,112 @@
use glium::{
backend::{Context, Facade},
Texture2d,
};
use imgui::{self, FontGlyphRange, ImFontConfig, Ui};
use glium::glutin::{self, Event, WindowEvent};
use glium::{Display, Surface};
use imgui::{Context, FontConfig, FontGlyphRanges, FontSource, Ui};
use imgui_glium_renderer::GliumRenderer;
use imgui_winit_support::{HiDpiMode, WinitPlatform};
use std::rc::Rc;
use std::time::Instant;

pub type Textures = imgui::Textures<Rc<Texture2d>>;

pub fn run<F>(title: String, clear_color: [f32; 4], mut run_ui: F)
where
F: FnMut(&Ui, &Rc<Context>, &mut Textures) -> bool,
{
use glium::glutin;
use glium::{Display, Surface};
use imgui_glium_renderer::GliumRenderer;
pub struct System {
pub events_loop: glutin::EventsLoop,
pub display: glium::Display,
pub imgui: Context,
pub platform: WinitPlatform,
pub renderer: GliumRenderer,
pub font_size: f32,
}

let mut events_loop = glutin::EventsLoop::new();
pub fn init(title: &str) -> System {
let events_loop = glutin::EventsLoop::new();
let context = glutin::ContextBuilder::new().with_vsync(true);
let builder = glutin::WindowBuilder::new()
.with_title(title)
.with_title(title.to_owned())
.with_dimensions(glutin::dpi::LogicalSize::new(1024f64, 768f64));
let display = Display::new(builder, context, &events_loop).unwrap();
let gl_window = display.gl_window();
let window = gl_window.window();
let display =
Display::new(builder, context, &events_loop).expect("Failed to initialize display");

let mut imgui = imgui::Context::create();
let mut imgui = Context::create();
imgui.set_ini_filename(None);

let mut platform = WinitPlatform::init(&mut imgui);
platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Rounded);
{
let gl_window = display.gl_window();
let window = gl_window.window();
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(
ImFontConfig::new()
.oversample_h(1)
.pixel_snap_h(true)
.size_pixels(font_size),
);

imgui.fonts().add_font_with_config(
include_bytes!("../../../resources/mplus-1p-regular.ttf"),
ImFontConfig::new()
.merge_mode(true)
.oversample_h(1)
.pixel_snap_h(true)
.size_pixels(font_size)
.rasterizer_multiply(1.75),
&FontGlyphRange::japanese(),
);
imgui.fonts().add_font(&[
FontSource::DefaultFontData {
config: Some(FontConfig {
size_pixels: font_size,
..FontConfig::default()
}),
},
FontSource::TtfData {
data: include_bytes!("../../../resources/mplus-1p-regular.ttf"),
size_pixels: font_size,
config: Some(FontConfig {
rasterizer_multiply: 1.75,
glyph_ranges: FontGlyphRanges::japanese(),
..FontConfig::default()
}),
},
]);

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

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

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

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

platform.handle_event(imgui.io_mut(), &window, &event);
System {
events_loop,
display,
imgui,
platform,
renderer,
font_size,
}
}

if let Event::WindowEvent { event, .. } = event {
match event {
CloseRequested => quit = true,
_ => (),
impl System {
pub fn main_loop<F: FnMut(&mut bool, &mut Ui)>(self, mut run_ui: F) {
let System {
mut events_loop,
display,
mut imgui,
mut platform,
mut renderer,
..
} = self;
let gl_window = display.gl_window();
let window = gl_window.window();
let mut last_frame = Instant::now();
let mut run = true;

while run {
events_loop.poll_events(|event| {
platform.handle_event(imgui.io_mut(), &window, &event);

if let Event::WindowEvent { event, .. } = event {
if let WindowEvent::CloseRequested = event {
run = false;
}
}
}
});

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;
}

let mut target = display.draw();
target.clear_color(
clear_color[0],
clear_color[1],
clear_color[2],
clear_color[3],
);
platform.prepare_render(&ui, &window);
renderer.render(&mut target, ui).expect("Rendering failed");
target.finish().unwrap();

if quit {
break;
});

let io = imgui.io_mut();
platform
.prepare_frame(io, &window)
.expect("Failed to start frame");
last_frame = io.update_delta_time(last_frame);
let mut ui = imgui.frame();
run_ui(&mut run, &mut ui);

let mut target = display.draw();
target.clear_color_srgb(1.0, 1.0, 1.0, 1.0);
platform.prepare_render(&ui, &window);
renderer.render(&mut target, ui).expect("Rendering failed");
target.finish().expect("Failed to swap buffers");
}
}
}
Loading

0 comments on commit cebe02c

Please sign in to comment.