imgui-derive

This commit is contained in:
Piotr 2020-09-22 22:33:57 +02:00
parent 8e66183b63
commit a051b6d6ce
3 changed files with 129 additions and 27 deletions

77
Cargo.lock generated
View File

@ -181,6 +181,41 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
dependencies = [
"fnv",
"ident_case",
"proc-macro2 1.0.19",
"quote 1.0.7",
"strsim",
"syn 1.0.38",
]
[[package]]
name = "darling_macro"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
dependencies = [
"darling_core",
"quote 1.0.7",
"syn 1.0.38",
]
[[package]]
name = "deflate"
version = "0.7.20"
@ -200,6 +235,8 @@ dependencies = [
"human-panic",
"image",
"imgui-glfw-rs",
"imgui-inspect",
"imgui-inspect-derive",
"num",
"rand 0.7.3",
"tobj",
@ -211,6 +248,12 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd56b59865bce947ac5958779cfa508f6c3b9497cc762b7e24a12d11ccde2c4f"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
@ -323,6 +366,12 @@ dependencies = [
"uuid",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "image"
version = "0.22.5"
@ -362,6 +411,28 @@ dependencies = [
"imgui-opengl-renderer",
]
[[package]]
name = "imgui-inspect"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df7314937ce7b2ca5ec374bdb0bd9f1f398d7648d658ae298136b2b66f9b14d0"
dependencies = [
"imgui",
"imgui-inspect-derive",
]
[[package]]
name = "imgui-inspect-derive"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd0654249213b6df7aed82a4a1dd41d6e11533fa5a7a4b5ac0b282107d7cf73d"
dependencies = [
"darling",
"proc-macro2 1.0.19",
"quote 1.0.7",
"syn 1.0.38",
]
[[package]]
name = "imgui-opengl-renderer"
version = "0.8.0"
@ -967,6 +1038,12 @@ version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "syn"
version = "0.15.44"

View File

@ -16,6 +16,8 @@ cgmath = "0.17.0"
gl = "0.14.0"
image = "0.22.5"
imgui-glfw-rs = { git = "https://github.com/Leinnan/imgui-glfw-rs" }
imgui-inspect = "0.5.0"
imgui-inspect-derive = "0.5.0"
# only needed from chapter 3 on
tobj = "2.0.2"
num = "0.3.0"

View File

@ -5,6 +5,9 @@ extern crate image;
use imgui_glfw_rs::glfw;
// Use the reexported imgui crate to avoid version conflicts.
use imgui_glfw_rs::imgui;
use imgui_inspect_derive::Inspect;
use imgui_inspect::InspectArgsStruct;
use imgui_inspect::InspectArgsSlider;
use imgui_glfw_rs::ImguiGLFW;
use self::camera::*;
@ -13,12 +16,6 @@ use imgui_glfw_rs::glfw::{Action, Context, Key};
use cgmath::prelude::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3};
use human_panic::setup_panic;
use image::GenericImageView;
use std::ffi::CString;
use std::mem;
use std::os::raw::c_void;
use std::ptr;
use std::sync::mpsc::Receiver;
mod camera;
mod consts;
mod engine;
@ -27,18 +24,25 @@ mod shader;
mod model;
mod mesh;
const CUBES_POS: [Vector3<f32>; 10] = [
vec3(0.0, 0.0, 0.0),
vec3(2.0, 5.0, -15.0),
vec3(-1.5, -2.2, -2.5),
vec3(-3.8, -2.0, -12.3),
vec3(2.4, -0.4, -3.5),
vec3(-1.7, 3.0, -7.5),
vec3(1.3, -2.0, -2.5),
vec3(1.5, 2.0, -2.5),
vec3(1.5, 0.2, -1.5),
vec3(-1.3, 1.0, -1.5),
];
#[derive(Inspect)]
pub struct BgInfo {
#[inspect_slider(min_value = 0.0, max_value = 1.0)]
pub r : f32,
#[inspect_slider(min_value = 0.0, max_value = 1.0)]
pub g: f32,
#[inspect_slider(min_value = 0.0, max_value = 1.0)]
pub b: f32,
}
impl Default for BgInfo {
fn default() -> Self {
BgInfo{
r: 0.1,
g: 0.2,
b: 0.4
}
}
}
pub fn main() {
setup_panic!();
@ -117,7 +121,7 @@ pub fn main() {
// render loop
// -----------
let (mut r,mut g,mut b) = (0.188, 0.22, 0.235);
let mut bg = BgInfo::default();
while !window.should_close() {
// per-frame time logic
// --------------------
@ -127,12 +131,15 @@ pub fn main() {
// input
// -----
process_input(&mut window, delta_time, &mut camera);
let skip_input = imgui.io().want_capture_mouse || imgui.io().want_capture_keyboard;
if !skip_input {
process_input(&mut window, delta_time, &mut camera);
}
// render
// ------
unsafe {
gl::ClearColor(r, g, b, 1.0);
gl::ClearColor(bg.r, bg.g, bg.b, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
// don't forget to enable shader before setting uniforms
@ -169,12 +176,24 @@ pub fn main() {
"Mouse Position: ({:.1},{:.1})",
last_x, last_y
));
imgui::Slider::new(im_str!("r"), 0.0 ..= 1.0)
.build(&ui, &mut r);
imgui::Slider::new(im_str!("g"), 0.0 ..= 1.0)
.build(&ui, &mut g);
imgui::Slider::new(im_str!("b"), 0.0 ..= 1.0)
.build(&ui, &mut b);
let selected = vec![&bg];
<BgInfo as imgui_inspect::InspectRenderStruct<
BgInfo,
>>::render(
&selected,
"Example Struct - Read Only",
&ui,
&InspectArgsStruct::default(),
);
let mut selected_mut = vec![&mut bg];
<BgInfo as imgui_inspect::InspectRenderStruct<
BgInfo,
>>::render_mut(
&mut selected_mut,
"Example Struct - Writable",
&ui,
&InspectArgsStruct::default(),
);
});
}
@ -192,6 +211,7 @@ pub fn main() {
&mut last_x,
&mut last_y,
&mut camera,
skip_input
);
}
}
@ -203,6 +223,7 @@ pub fn process_events(
last_x: &mut f32,
last_y: &mut f32,
camera: &mut Camera,
skip_input: bool
) {
match event {
glfw::WindowEvent::FramebufferSize(width, height) => {
@ -211,6 +232,7 @@ pub fn process_events(
unsafe { gl::Viewport(0, 0, width, height) }
}
glfw::WindowEvent::CursorPos(xpos, ypos) => {
if skip_input { return; }
let (xpos, ypos) = (xpos as f32, ypos as f32);
if *first_mouse {
*last_x = xpos;
@ -227,6 +249,7 @@ pub fn process_events(
camera.process_mouse_movement(xoffset, yoffset, true);
}
glfw::WindowEvent::Scroll(_xoffset, yoffset) => {
if skip_input { return; }
camera.process_mouse_scroll(yoffset as f32);
}
_ => {}