Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
RinatNamazov committed Jan 19, 2024
1 parent 8a120a5 commit 17ac723
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 111 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ extern crate winres;
fn main() {
let res = winres::WindowsResource::new();
res.compile().unwrap();
}
}
2 changes: 1 addition & 1 deletion src/cmd_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ pub fn cmd_with_prefix(command: &str) -> String {
str.push_str(CMD_PREFIX);
str.push_str(command);
str
}
}
5 changes: 4 additions & 1 deletion src/cppstd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ impl StdString {
pub fn to_string(&self) -> String {
unsafe {
if self.size < 16 {
CStr::from_bytes_until_nul(&self.str.buf).unwrap().to_string_lossy().to_string()
CStr::from_bytes_until_nul(&self.str.buf)
.unwrap()
.to_string_lossy()
.to_string()
} else {
CStr::from_ptr(self.str.ptr).to_string_lossy().to_string()
}
Expand Down
10 changes: 7 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::WinApiError(e) => write!(f, "WinAPI: {}", e),
Error::FunctionNotFound(symbol) => write!(f, "GetProcAddress failed for symbol: {}", symbol),
Error::MaybeInvalidGameOrPluginConflicting => write!(f, "Maybe invalid game or conflicting plugin"),
Error::FunctionNotFound(symbol) => {
write!(f, "GetProcAddress failed for symbol: {}", symbol)
}
Error::MaybeInvalidGameOrPluginConflicting => {
write!(f, "Maybe invalid game or conflicting plugin")
}
Error::SampNotLoaded(e) => write!(f, "Library 'samp.dll' not found. WinAPI: {}", e),
Error::IncompatibleSampVersion => write!(f, "Incompatible SA-MP version"),
}
Expand All @@ -39,4 +43,4 @@ impl From<WindowsError> for Error {
fn from(e: WindowsError) -> Self {
Error::WinApiError(e)
}
}
}
11 changes: 3 additions & 8 deletions src/gta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@
*****************************************************************************/

use std::ffi::c_void;
use windows::Win32::{
Foundation::HWND,
Graphics::Direct3D9::IDirect3DDevice9,
};
use windows::Win32::{Foundation::HWND, Graphics::Direct3D9::IDirect3DDevice9};

pub fn get_window_handle() -> HWND {
unsafe { **(0xC17054 as *const *const HWND) }
}

pub fn get_d3d9_device() -> IDirect3DDevice9 {
unsafe {
windows::core::Interface::from_raw(*(0xC97C28 as *const *mut c_void))
}
unsafe { windows::core::Interface::from_raw(*(0xC97C28 as *const *mut c_void)) }
}

pub fn is_gta_menu_active() -> bool {
unsafe { *(0xBA67A4 as *const bool) }
}
}
115 changes: 65 additions & 50 deletions src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::cmd_storage::CMD_PREFIX;
use std::ffi::CStr;
use egui::{Color32, FontData, FontDefinitions, FontFamily, FontId, FontTweak, Key, Label, RichText, Rounding, Sense, TextStyle};
use egui::epaint::Shadow;
use crate::{gta, samp};
use crate::plugin::Plugin;
use crate::{gta, samp};
use egui::{
epaint::Shadow, Color32, FontData, FontDefinitions, FontFamily, FontId, FontTweak, Key, Label,
RichText, Rounding, Sense, TextStyle,
};
use std::ffi::CStr;

pub struct Ui {}

Expand All @@ -21,10 +23,9 @@ impl Ui {
fn add_font(fonts: &mut FontDefinitions, name: &str, font: &'static [u8]) {
let name = name.to_string();
let tweak = FontTweak::default();
fonts.font_data.insert(
name.clone(),
FontData::from_static(font).tweak(tweak),
);
fonts
.font_data
.insert(name.clone(), FontData::from_static(font).tweak(tweak));
fonts
.families
.get_mut(&FontFamily::Proportional)
Expand All @@ -39,7 +40,11 @@ impl Ui {

fn setup_custom_fonts(ctx: &egui::Context) {
let mut fonts = FontDefinitions::default();
Self::add_font(&mut fonts, "Segoe UI Bold", include_bytes!("C:\\Windows\\Fonts\\segoeuib.ttf"));
Self::add_font(
&mut fonts,
"Segoe UI Bold",
include_bytes!("C:\\Windows\\Fonts\\segoeuib.ttf"),
);
ctx.set_fonts(fonts);
}

Expand All @@ -53,7 +58,8 @@ impl Ui {
(TextStyle::Monospace, FontId::new(16.0, Monospace)),
(TextStyle::Button, FontId::new(16.5, Proportional)),
(TextStyle::Small, FontId::new(8.0, Proportional)),
].into();
]
.into();
ctx.set_style(style);
}

Expand Down Expand Up @@ -90,15 +96,24 @@ impl Ui {
let chat_contains_cmd = chat_input.starts_with(CMD_PREFIX);

// Don't draw empty list.
if (samp_input.total_recall == 0 && !chat_contains_cmd) || (chat_contains_cmd && Plugin::get().commands().is_empty()) {
if (samp_input.total_recall == 0 && !chat_contains_cmd)
|| (chat_contains_cmd && Plugin::get().commands().is_empty())
{
return;
}

let pos = samp_input.edit_box().position;
let pos = [pos[0] as f32, (pos[1] + samp_input.edit_box().height + 5) as f32];
let pos = [
pos[0] as f32,
(pos[1] + samp_input.edit_box().height + 5) as f32,
];

// So that each window has its own size.
let key = if chat_contains_cmd { "#Commands" } else { "#Recalls" };
let key = if chat_contains_cmd {
"#Commands"
} else {
"#Recalls"
};
egui::containers::Window::new(key)
.fixed_pos(pos)
.title_bar(false)
Expand All @@ -115,14 +130,12 @@ impl Ui {
}

fn draw_commands(&self, ui: &mut egui::Ui, chat_input: &String, samp_input: &mut samp::Input) {
egui::Grid::new("cmds")
.min_col_width(200.0)
.show(ui, |ui| {
self.draw_cmds_header(ui);
ui.end_row();
self.draw_cmds_body(ui, &chat_input, samp_input);
ui.end_row();
});
egui::Grid::new("cmds").min_col_width(200.0).show(ui, |ui| {
self.draw_cmds_header(ui);
ui.end_row();
self.draw_cmds_body(ui, &chat_input, samp_input);
ui.end_row();
});
}

fn draw_cmds_header(&self, ui: &mut egui::Ui) {
Expand All @@ -143,25 +156,27 @@ impl Ui {

ui.vertical(|ui| {
for (name, commands) in category.modules.iter() {
egui::CollapsingHeader::new(name).default_open(true).show(ui, |ui| {
for (cmd, description) in commands.iter() {
let text = if chat_input.is_empty() || cmd.starts_with(chat_input) {
RichText::new(cmd)
} else {
RichText::new(cmd).weak()
};

let label = ui.add(Label::new(text).sense(Sense::click()));

if label.clicked() {
input.edit_box().set_text(cmd.as_str());
}

if !description.is_empty() {
label.on_hover_text(description);
egui::CollapsingHeader::new(name)
.default_open(true)
.show(ui, |ui| {
for (cmd, description) in commands.iter() {
let text = if chat_input.is_empty() || cmd.starts_with(chat_input) {
RichText::new(cmd)
} else {
RichText::new(cmd).weak()
};

let label = ui.add(Label::new(text).sense(Sense::click()));

if label.clicked() {
input.edit_box().set_text(cmd.as_str());
}

if !description.is_empty() {
label.on_hover_text(description);
}
}
}
});
});
}
});
}
Expand All @@ -170,11 +185,10 @@ impl Ui {
fn draw_copyright(&self, ui: &mut egui::Ui) {
ui.separator();
ui.vertical_centered(|ui| {
ui.strong("Copyright © Rinat Namazov")
.on_hover_ui(|ui| {
ui.label(concat!("SA-MP Command Helper v", env!("CARGO_PKG_VERSION")));
ui.label("https://rinwares.com");
});
ui.strong("Copyright © Rinat Namazov").on_hover_ui(|ui| {
ui.label(concat!("SA-MP Command Helper v", env!("CARGO_PKG_VERSION")));
ui.label("https://rinwares.com");
});
});
}

Expand All @@ -187,11 +201,12 @@ impl Ui {
for i in 0..input.total_recall as usize {
if let Ok(recall) = CStr::from_bytes_until_nul(&input.recall_buffer[i]) {
if let Ok(text) = recall.to_str() {
let text = if input.current_recall == -1 || i == input.current_recall as usize {
RichText::new(text)
} else {
RichText::new(text).weak()
};
let text =
if input.current_recall == -1 || i == input.current_recall as usize {
RichText::new(text)
} else {
RichText::new(text).weak()
};

let label = ui.add(Label::new(text).sense(Sense::click()));

Expand All @@ -204,4 +219,4 @@ impl Ui {
}
});
}
}
}
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
*
*****************************************************************************/

#[cfg(debug_assertions)]
use windows::Win32::System::Console::AllocConsole;
use windows::Win32::{
Foundation::{BOOL, FALSE, HMODULE, TRUE},
System::{LibraryLoader::DisableThreadLibraryCalls, SystemServices::DLL_PROCESS_ATTACH},
};
#[cfg(debug_assertions)]
use windows::Win32::System::Console::AllocConsole;

mod cmd_storage;
mod cppstd;
mod errors;
mod gta;
mod gui;
mod plugin;
mod samp;
mod sampfuncs;
mod errors;
mod gta;
mod utils;
mod cppstd;
mod gui;
mod cmd_storage;

#[no_mangle]
extern "stdcall" fn DllMain(instance: HMODULE, reason: u32, _reserved: *mut ()) -> BOOL {
Expand All @@ -46,4 +46,4 @@ extern "stdcall" fn DllMain(instance: HMODULE, reason: u32, _reserved: *mut ())
}
}
TRUE
}
}
Loading

0 comments on commit 17ac723

Please sign in to comment.