Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Add toggle for placement transform editing & clean up UI
Browse files Browse the repository at this point in the history
  • Loading branch information
PieKing1215 committed Sep 16, 2024
1 parent 3eb0c6a commit e729bfc
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 74 deletions.
27 changes: 20 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use windows::Win32::Foundation::HINSTANCE;
use windows::Win32::System::SystemServices::DLL_PROCESS_ATTACH;

#[allow(clippy::missing_safety_doc)]
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "stdcall" fn DllMain(hmodule: HINSTANCE, reason: u32, _: *mut std::ffi::c_void) {
if reason == DLL_PROCESS_ATTACH {
Expand Down Expand Up @@ -215,7 +216,11 @@ impl ImguiRenderLoop for MainHud {
style_padding.end();

for (tw, _) in &mut self.tweaks {
tw.constant_render(ui);

if let Err(e) = tw.constant_render(ui) {
self.errors.push(e);
self.show = true;
}
}

if !self.show {
Expand Down Expand Up @@ -296,26 +301,34 @@ impl ImguiRenderLoop for MainHud {
let categories = self
.tweaks
.iter_mut()
.map(|(tw, _)| (tw.category().clone(), tw))
.enumerate()
.map(|(i, (tw, _))| (tw.category().clone(), i))
.into_group_map();

for (category, tweaks) in categories.into_iter().sorted_by(|a, b| {
for (category, mut tweak_indices) in categories.into_iter().sorted_by(|a, b| {
if a.0.is_none() || b.0.is_none() {
b.0.cmp(&a.0)
} else {
a.0.cmp(&b.0)
}
}) {
let render = || {
for tw in tweaks {
if let Err(e) = tw.render(ui) {
let mut render = || {
for i in &mut tweak_indices {
if let Err(e) = self.tweaks[*i].0.render(ui) {
self.errors.push(e);
self.show = true;
}
}

for (tw, _) in &mut self.tweaks {
if let Err(e) = tw.render_category(ui, category.as_deref()) {
self.errors.push(e);
self.show = true;
}
}
};

if let Some(category) = category {
if let Some(category) = &category {
if ui.collapsing_header(category, TreeNodeFlags::empty()) {
ui.indent_by(8.0);
render();
Expand Down
11 changes: 6 additions & 5 deletions src/tweaks/editor_camera_speed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ const DEFAULT_WHEEL_MULTIPLIER: f32 = 1.1;

static SPEED: AtomicF32 = AtomicF32::new(1.0);

#[no_mangle]
static mut jmp_back_addr: usize = 0x0;

pub struct EditorCameraSpeedTweak {
base_speed: f32,
shift_multiplier: f32,
Expand Down Expand Up @@ -87,7 +84,7 @@ impl Tweak for EditorCameraSpeedTweak {
})
}

fn render(&mut self, ui: &hudhook::imgui::Ui) {
fn render(&mut self, ui: &hudhook::imgui::Ui) -> anyhow::Result<()> {
ui.set_next_item_width(100.0);
ui.slider("Camera Base Speed", 0.1, 4.0, &mut self.base_speed);
if ui.is_item_hovered() {
Expand Down Expand Up @@ -130,9 +127,11 @@ impl Tweak for EditorCameraSpeedTweak {

// ui.text(format!("{}", self.current_speed));
// ui.text(format!("{}", self.current_wheel_multiplier));

Ok(())
}

fn constant_render(&mut self, ui: &hudhook::imgui::Ui) {
fn constant_render(&mut self, ui: &hudhook::imgui::Ui) -> anyhow::Result<()> {
// if ui.io().mouse_wheel != 0.0 {
// self.current_wheel_multiplier *= self.wheel_multiplier.powf(ui.io().mouse_wheel);
// }
Expand All @@ -152,6 +151,8 @@ impl Tweak for EditorCameraSpeedTweak {
}

SPEED.store(self.current_speed, Ordering::Release);

Ok(())
}

fn reset_to_default(&mut self) {
Expand Down
22 changes: 17 additions & 5 deletions src/tweaks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ pub trait Tweak {
Ok(())
}

fn render(&mut self, _ui: &hudhook::imgui::Ui) {}
fn render(&mut self, _ui: &hudhook::imgui::Ui) -> anyhow::Result<()> {
Ok(())
}

fn constant_render(&mut self, _ui: &hudhook::imgui::Ui) {}
fn render_category(&mut self, _ui: &hudhook::imgui::Ui, _category: Option<&str>) -> anyhow::Result<()> {
Ok(())
}

fn constant_render(&mut self, _ui: &hudhook::imgui::Ui) -> anyhow::Result<()> {
Ok(())
}

fn reset_to_default(&mut self) {}
fn reset_to_vanilla(&mut self) {}
Expand Down Expand Up @@ -112,13 +120,17 @@ impl TweakWrapper {
for t in &mut self.settings {
t.render(ui)?;
}
self.inner.render(ui);
self.inner.render(ui)?;

Ok(())
}

pub fn constant_render(&mut self, ui: &hudhook::imgui::Ui) {
self.inner.constant_render(ui);
pub fn render_category(&mut self, ui: &hudhook::imgui::Ui, category: Option<&str>) -> anyhow::Result<()> {
self.inner.render_category(ui, category)
}

pub fn constant_render(&mut self, ui: &hudhook::imgui::Ui) -> anyhow::Result<()> {
self.inner.constant_render(ui)
}

pub fn reset_to_default(&mut self) -> anyhow::Result<()> {
Expand Down
15 changes: 15 additions & 0 deletions src/tweaks/settings/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl<'b, 'r> ToggleBuilder<'b, 'r> {
label: label.into(),
injections: vec![],
detours: vec![],
value_changed_listeners: vec![],
},
}
}
Expand Down Expand Up @@ -65,6 +66,15 @@ impl<'b, 'r> ToggleBuilder<'b, 'r> {
self
}

#[must_use]
pub fn on_value_changed(
mut self,
callback: impl FnMut(bool) + Send + Sync + 'static,
) -> Self {
self.toggle.value_changed_listeners.push(Box::new(callback));
self
}

pub fn build(self) -> anyhow::Result<()> {
self.tweak_builder
.add_setting(Setting::new(self.toggle, self.defaults, self.config_key))
Expand All @@ -76,6 +86,7 @@ pub struct Toggle {
tooltip: String,
injections: Vec<(Injection, bool)>,
detours: Vec<(Box<dyn DetourUntyped + Send + Sync>, bool)>,
value_changed_listeners: Vec<Box<dyn FnMut(bool) + Send + Sync>>,
}

impl SettingImpl<bool> for Toggle {
Expand Down Expand Up @@ -114,6 +125,10 @@ impl SettingImpl<bool> for Toggle {
}
}

for listener in &mut self.value_changed_listeners {
listener(value);
}

Ok(())
}

Expand Down
Loading

0 comments on commit e729bfc

Please sign in to comment.