Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove focus flag and ImguiRenderLoopFlags #123

Merged
merged 2 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Read up on the underlying architecture [here](https://veeenu.github.io/blog/seki
```rust
// src/lib.rs
use hudhook::hooks::dx11::ImguiDX11Hooks;
use hudhook::hooks::{ImguiRenderLoop, ImguiRenderLoopFlags};
use hudhook::hooks::ImguiRenderLoop;
use imgui::{Condition, Window};
struct Dx11HookExample;

Expand All @@ -35,7 +35,7 @@ impl Dx11HookExample {
}

impl ImguiRenderLoop for Dx11HookExample {
fn render(&mut self, ui: &mut imgui::Ui, _: &ImguiRenderLoopFlags) {
fn render(&mut self, ui: &mut imgui::Ui) {
ui.window("Hello world").size([300.0, 110.0], Condition::FirstUseEver).build(|| {
ui.text("Hello world!");
ui.text("こんにちは世界!");
Expand Down
4 changes: 2 additions & 2 deletions examples/dx11_hook.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(lazy_cell)]

use hudhook::hooks::dx11::ImguiDx11Hooks;
use hudhook::hooks::{ImguiRenderLoop, ImguiRenderLoopFlags};
use hudhook::hooks::ImguiRenderLoop;
use imgui::Condition;
use tracing::metadata::LevelFilter;
struct Dx11HookExample;
Expand All @@ -25,7 +25,7 @@ impl Dx11HookExample {
}

impl ImguiRenderLoop for Dx11HookExample {
fn render(&mut self, ui: &mut imgui::Ui, _: &ImguiRenderLoopFlags) {
fn render(&mut self, ui: &mut imgui::Ui) {
ui.window("Hello world").size([300.0, 110.0], Condition::FirstUseEver).build(|| {
ui.text("Hello world!");
ui.text("こんにちは世界!");
Expand Down
4 changes: 2 additions & 2 deletions examples/dx12_hook.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(lazy_cell)]

use hudhook::hooks::dx12::ImguiDx12Hooks;
use hudhook::hooks::{ImguiRenderLoop, ImguiRenderLoopFlags};
use hudhook::hooks::ImguiRenderLoop;
use imgui::Condition;
use tracing::metadata::LevelFilter;
struct Dx12HookExample;
Expand All @@ -25,7 +25,7 @@ impl Dx12HookExample {
}

impl ImguiRenderLoop for Dx12HookExample {
fn render(&mut self, ui: &mut imgui::Ui, _: &ImguiRenderLoopFlags) {
fn render(&mut self, ui: &mut imgui::Ui) {
ui.window("Hello world").size([300.0, 110.0], Condition::FirstUseEver).build(|| {
ui.text("Hello world!");
ui.text("こんにちは世界!");
Expand Down
4 changes: 2 additions & 2 deletions examples/dx9_hook.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(lazy_cell)]

use hudhook::hooks::dx9::ImguiDx9Hooks;
use hudhook::hooks::{ImguiRenderLoop, ImguiRenderLoopFlags};
use hudhook::hooks::ImguiRenderLoop;
use imgui::Condition;
use tracing::metadata::LevelFilter;
struct Dx9HookExample;
Expand All @@ -25,7 +25,7 @@ impl Dx9HookExample {
}

impl ImguiRenderLoop for Dx9HookExample {
fn render(&mut self, ui: &mut imgui::Ui, _: &ImguiRenderLoopFlags) {
fn render(&mut self, ui: &mut imgui::Ui) {
ui.window("Hello world").size([300.0, 110.0], Condition::FirstUseEver).build(|| {
ui.text("Hello world!");
ui.text("こんにちは世界!");
Expand Down
4 changes: 2 additions & 2 deletions examples/opengl3_hook.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(lazy_cell)]

use hudhook::hooks::opengl3::ImguiOpenGl3Hooks;
use hudhook::hooks::{ImguiRenderLoop, ImguiRenderLoopFlags};
use hudhook::hooks::ImguiRenderLoop;
use imgui::Condition;
use tracing::metadata::LevelFilter;
struct HookYou;
Expand All @@ -25,7 +25,7 @@ impl HookYou {
}

impl ImguiRenderLoop for HookYou {
fn render(&mut self, ui: &mut imgui::Ui, _: &ImguiRenderLoopFlags) {
fn render(&mut self, ui: &mut imgui::Ui) {
ui.window("Hello world").size([300.0, 110.0], Condition::FirstUseEver).build(|| {
ui.text("Hello world!");
ui.text("こんにちは世界!");
Expand Down
15 changes: 6 additions & 9 deletions hudbook/hello-hud/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::time::Instant;


use hudhook::hooks::{ImguiRenderLoop, ImguiRenderLoopFlags};
use hudhook::hooks::dx12::ImguiDx12Hooks;
use hudhook::hooks::ImguiRenderLoop;
use imgui::*;

struct HelloHud {
Expand All @@ -16,13 +15,11 @@ impl HelloHud {
}

impl ImguiRenderLoop for HelloHud {
fn render(&mut self, ui: &mut Ui, _flags: &ImguiRenderLoopFlags) {
ui.window("##hello")
.size([320., 200.], Condition::Always)
.build(|| {
ui.text("Hello, world!");
ui.text(format!("Elapsed: {:?}", self.start_time.elapsed()));
});
fn render(&mut self, ui: &mut Ui) {
ui.window("##hello").size([320., 200.], Condition::Always).build(|| {
ui.text("Hello, world!");
ui.text(format!("Elapsed: {:?}", self.start_time.elapsed()));
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions hudbook/src/creating-library/writing-dll.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ The trait consists of only one method, `render`. `hudhook` will supply the `imgu
need to use to render our UI, we are only tasked with actually implementing our rendering code.

```rust
use hudhook::hooks::{ImguiRenderLoop, ImguiRenderLoopFlags};
use hudhook::hooks::ImguiRenderLoop;
use imgui::*;


impl ImguiRenderLoop for HelloHud {
fn render(&mut self, ui: &mut Ui, _flags: &ImguiRenderLoopFlags) {
fn render(&mut self, ui: &mut Ui) {
ui.window("##hello")
.size([320., 200.], Condition::Always)
.build(|| {
Expand Down
3 changes: 0 additions & 3 deletions src/hooks/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ pub trait ImguiWindowsEventHandler {
fn io(&self) -> &imgui::Io;
fn io_mut(&mut self) -> &mut imgui::Io;

fn focus(&self) -> bool;
fn focus_mut(&mut self) -> &mut bool;

fn wnd_proc(&self) -> WndProcType;

fn setup_io(&mut self) {
Expand Down
6 changes: 1 addition & 5 deletions src/hooks/common/wnd_proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use windows::Win32::UI::Input::{
use windows::Win32::UI::WindowsAndMessaging::*;

use super::{ImguiRenderLoop, ImguiWindowsEventHandler};
use crate::hooks::{get_wheel_delta_wparam, hiword, loword};
use crate::hooks::{get_wheel_delta_wparam, hiword};

////////////////////////////////////////////////////////////////////////////////
// Raw input
Expand Down Expand Up @@ -286,10 +286,6 @@ where
io.mouse_wheel_h += (wheel_delta_wparam as i16 as f32) / wheel_delta;
},
WM_CHAR => io.add_input_character(wparam as u8 as char),
WM_ACTIVATE => {
*imgui_renderer.focus_mut() = loword(wparam as _) != WA_INACTIVE as u16;
return LRESULT(1);
},
_ => {},
};

Expand Down
17 changes: 3 additions & 14 deletions src/hooks/dx11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use windows::Win32::UI::WindowsAndMessaging::SetWindowLongPtrA;
use windows::Win32::UI::WindowsAndMessaging::*;

use crate::hooks::common::{imgui_wnd_proc_impl, DummyHwnd, ImguiWindowsEventHandler, WndProcType};
use crate::hooks::{Hooks, ImguiRenderLoop, ImguiRenderLoopFlags};
use crate::hooks::{Hooks, ImguiRenderLoop};
use crate::mh::{MhHook, MhHooks};
use crate::renderers::imgui_dx11;

Expand Down Expand Up @@ -146,7 +146,6 @@ struct ImguiRenderer {
ctx: Context,
engine: imgui_dx11::RenderEngine,
wnd_proc: WndProcType,
flags: ImguiRenderLoopFlags,
swap_chain: IDXGISwapChain,
}

Expand All @@ -159,8 +158,6 @@ impl ImguiRenderer {

IMGUI_RENDER_LOOP.get_mut().unwrap().initialize(&mut ctx);

let flags = ImguiRenderLoopFlags { focused: true };

trace!("Initializing renderer");

let dev: ID3D11Device = swap_chain.GetDevice().expect("GetDevice");
Expand All @@ -187,7 +184,7 @@ impl ImguiRenderer {
));

trace!("Renderer initialized");
let mut renderer = ImguiRenderer { ctx, engine, wnd_proc, flags, swap_chain };
let mut renderer = ImguiRenderer { ctx, engine, wnd_proc, swap_chain };

ImguiWindowsEventHandler::setup_io(&mut renderer);

Expand Down Expand Up @@ -224,7 +221,7 @@ impl ImguiRenderer {

let ctx = &mut self.ctx;
let ui = ctx.frame();
IMGUI_RENDER_LOOP.get_mut().unwrap().render(ui, &self.flags);
IMGUI_RENDER_LOOP.get_mut().unwrap().render(ui);
let draw_data = ctx.render();

if let Err(e) = self.engine.render_draw_data(draw_data) {
Expand Down Expand Up @@ -269,14 +266,6 @@ impl ImguiWindowsEventHandler for ImguiRenderer {
self.ctx_mut().io_mut()
}

fn focus(&self) -> bool {
self.flags.focused
}

fn focus_mut(&mut self) -> &mut bool {
&mut self.flags.focused
}

fn wnd_proc(&self) -> WndProcType {
self.wnd_proc
}
Expand Down
16 changes: 2 additions & 14 deletions src/hooks/dx12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use windows::Win32::UI::WindowsAndMessaging::SetWindowLongPtrA;
use windows::Win32::UI::WindowsAndMessaging::*;

use crate::hooks::common::{imgui_wnd_proc_impl, DummyHwnd, ImguiWindowsEventHandler, WndProcType};
use crate::hooks::{Hooks, ImguiRenderLoop, ImguiRenderLoopFlags};
use crate::hooks::{Hooks, ImguiRenderLoop};
use crate::mh::{MhHook, MhHooks};
use crate::renderers::imgui_dx12::RenderEngine;

Expand Down Expand Up @@ -330,7 +330,6 @@ struct ImguiRenderer {
ctx: Context,
engine: RenderEngine,
wnd_proc: WndProcType,
flags: ImguiRenderLoopFlags,
frame_contexts: Vec<FrameContext>,
_rtv_heap: ID3D12DescriptorHeap,
renderer_heap: ID3D12DescriptorHeap,
Expand Down Expand Up @@ -443,8 +442,6 @@ impl ImguiRenderer {

ctx.set_ini_filename(None);

let flags = ImguiRenderLoopFlags { focused: true };

IMGUI_RENDER_LOOP.get_mut().unwrap().initialize(&mut ctx);

debug!("Done init");
Expand All @@ -454,7 +451,6 @@ impl ImguiRenderer {
command_list,
engine,
wnd_proc,
flags,
_rtv_heap: rtv_heap,
renderer_heap,
frame_contexts,
Expand Down Expand Up @@ -521,7 +517,7 @@ impl ImguiRenderer {
self.engine.new_frame(&mut self.ctx);
let ctx = &mut self.ctx;
let ui = ctx.frame();
unsafe { IMGUI_RENDER_LOOP.get_mut() }.unwrap().render(ui, &self.flags);
unsafe { IMGUI_RENDER_LOOP.get_mut() }.unwrap().render(ui);
let draw_data = ctx.render();

let transition_barrier = ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER {
Expand Down Expand Up @@ -608,14 +604,6 @@ impl ImguiWindowsEventHandler for ImguiRenderer {
self.ctx.io_mut()
}

fn focus(&self) -> bool {
self.flags.focused
}

fn focus_mut(&mut self) -> &mut bool {
&mut self.flags.focused
}

fn wnd_proc(&self) -> WndProcType {
self.wnd_proc
}
Expand Down
20 changes: 3 additions & 17 deletions src/hooks/dx9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use windows::Win32::UI::WindowsAndMessaging::{
};

use crate::hooks::common::{imgui_wnd_proc_impl, DummyHwnd, ImguiWindowsEventHandler, WndProcType};
use crate::hooks::{Hooks, ImguiRenderLoop, ImguiRenderLoopFlags};
use crate::hooks::{Hooks, ImguiRenderLoop};
use crate::mh::{MhHook, MhHooks};
use crate::renderers::imgui_dx9;

Expand All @@ -49,12 +49,7 @@ unsafe fn draw(this: &IDirect3DDevice9) {
imgui_wnd_proc as usize as i32,
));

Mutex::new(Box::new(ImguiRenderer {
ctx: context,
renderer,
wnd_proc,
flags: ImguiRenderLoopFlags { focused: false },
}))
Mutex::new(Box::new(ImguiRenderer { ctx: context, renderer, wnd_proc }))
})
.lock();

Expand Down Expand Up @@ -164,7 +159,6 @@ struct ImguiRenderer {
ctx: Context,
renderer: imgui_dx9::Renderer,
wnd_proc: WndProcType,
flags: ImguiRenderLoopFlags,
}

impl ImguiRenderer {
Expand Down Expand Up @@ -195,7 +189,7 @@ impl ImguiRenderer {

let ui = self.ctx.frame();

IMGUI_RENDER_LOOP.get_mut().unwrap().render(ui, &self.flags);
IMGUI_RENDER_LOOP.get_mut().unwrap().render(ui);
let draw_data = self.ctx.render();
self.renderer.render(draw_data).unwrap();
}
Expand All @@ -218,14 +212,6 @@ impl ImguiWindowsEventHandler for ImguiRenderer {
self.ctx.io_mut()
}

fn focus(&self) -> bool {
self.flags.focused
}

fn focus_mut(&mut self) -> &mut bool {
&mut self.flags.focused
}

fn wnd_proc(&self) -> WndProcType {
self.wnd_proc
}
Expand Down
17 changes: 5 additions & 12 deletions src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,14 @@ pub mod dx9;
#[cfg(feature = "opengl3")]
pub mod opengl3;

/// Holds information useful to the render loop which can't be retrieved from
/// `imgui::Ui`.
pub struct ImguiRenderLoopFlags {
/// Whether the hooked program's window is currently focused.
pub focused: bool,
}

/// Implement your `imgui` rendering logic via this trait.
pub trait ImguiRenderLoop {
/// Called once at the first occurrence of the hook. Implement this to
/// initialize your data.
fn initialize(&mut self, _ctx: &mut Context) {}

/// Called every frame. Use the provided `ui` object to build your UI.
fn render(&mut self, ui: &mut Ui, flags: &ImguiRenderLoopFlags);
fn render(&mut self, ui: &mut Ui);

/// Called during the window procedure.
fn on_wnd_proc(&self, _hwnd: HWND, _umsg: u32, _wparam: WPARAM, _lparam: LPARAM) {}
Expand All @@ -54,10 +47,10 @@ pub trait ImguiRenderLoop {
}
}

#[inline]
fn loword(l: u32) -> u16 {
(l & 0xffff) as u16
}
// #[inline]
// fn loword(l: u32) -> u16 {
// (l & 0xffff) as u16
// }
#[inline]
fn hiword(l: u32) -> u16 {
((l >> 16) & 0xffff) as u16
Expand Down
Loading