forked from imgui-rs/imgui-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull font API and associated refactoring from 0.1-dev
- Loading branch information
Showing
24 changed files
with
1,317 additions
and
721 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
Oops, something went wrong.