veeenu / hudhook

A videogame overlay framework written in Rust, supporting DirectX and OpenGL
MIT License
207 stars 30 forks source link

Add support for add_text to pass font size as param #201

Closed luadebug closed 3 months ago

luadebug commented 3 months ago

Add a way to call add_text with specific Font Size, please.

    #[doc(alias = "AddText")]
    pub fn add_text(
        &self,
        pos: impl Into<MintVec2>,
        col: impl Into<ImColor32>,
        text: impl AsRef<str>,
    ) {
        use std::os::raw::c_char;

        let text = text.as_ref();
        unsafe {
            let start = text.as_ptr() as *const c_char;
            let end = (start as usize + text.len()) as *const c_char;
            sys::ImDrawList_AddText_Vec2(
                self.draw_list,
                pos.into().into(),
                col.into().into(),
                start,
                end,
            )
        }
    }

Push and pop for font seems to be working, but I lose a way to control font size itself.


                            let width = ui.io().display_size[0];
                            let font_id = FONTS_STORAGE2
                                .as_mut()
                                .map(|fonts| {
                                    if width > 2000. {
                                        fonts.big
                                    } else if width > 1200. {
                                        fonts.normal
                                    } else {
                                        fonts.small
                                    }
                                })
                                .unwrap();
                            let custom_font = ui.push_font(font_id);
                            background_draw_list.add_text(
                                [espleft, esptop - 40.0f32],
                                if LOCAL_PLAYER.team().unwrap() == entity.team().unwrap() {
                                    SETTINGS.deref().ally_name_text_color
                                } else {
                                    SETTINGS.deref().enemy_name_text_color
                                },
                                entity.name().unwrap(),
                            );
                            custom_font.pop();
veeenu commented 3 months ago

Could you please elaborate on your request? It's not super clear.

I'm not sure where such a feature would belong in the current API -- it seems like it has nothing to do with hudhook and it depends on imgui instead.

For the record, font size can be controlled already by the clients, see this, but again it is not strictly dependent on hudhook and it is a feature from imgui.

If you mean changing the font size across widgets in the same frame, I don't think that is supported by imgui outside of pushing/popping preinitialized fonts. Fonts are rasterized to a texture at initialization time, and that texture then gets sampled when drawing text.

luadebug commented 3 months ago

ImGui::GetBackgroundDrawList()->AddText(esp_text, 19, ImVec2(ESPLeft, ESPTop - 20.f), ImColor(var::enemyColor[0], var::enemyColor[1], var::enemyColor[2]), Enemy->Name); This C++ code does not use any push or either pop for font, and second parameter being passed is text size. I was assuming that asking here is much better idea, rather go to imgui-rs that is being abandoned for months. https://github.com/imgui-rs/imgui-rs/pull/787

luadebug commented 3 months ago
// 200 IQ conversion mechanism
pub fn f32_to_u8(value: f32) -> u8 {
    // Scale and clamp the value to the range [0, 255]
    let scaled = (value * 255.0).clamp(0.0, 255.0);
    scaled as u8
}

                        unsafe {
                                let custom_font = ui.push_font(font_id);
                                // Get the entity name and create a CString
                                let text = entity.name().unwrap();
                                let c_text = CString::new(text).unwrap();  // Convert to C-compatible string

                                let font_handle = igGetFont();

                                // Get the pointer to the C string
                                let start: *const c_char = c_text.as_ptr();  // Use CString to ensure null termination

                                // The end pointer is not necessary when using CString, as you can pass the string slice
                                let end: *const c_char = start.add(c_text.as_bytes().len());

                                // Call the function
                                sys::ImDrawList_AddText_FontPtr(
                                    sys::igGetBackgroundDrawList_Nil(),
                                    font_handle,
                                    SETTINGS.deref().name_text_thickness,
                                    ImVec2::from([espleft, esptop - 80.0f32]),
                                    if LOCAL_PLAYER.team().unwrap() == entity.team().unwrap() {
                                        ImColor32::from_rgba(f32_to_u8(SETTINGS.deref().ally_name_text_color[0]), f32_to_u8(SETTINGS.deref().ally_name_text_color[1]), f32_to_u8(SETTINGS.deref().ally_name_text_color[2]), f32_to_u8(SETTINGS.deref().ally_name_text_color[3])).to_bits()
                                    } else {
                                        ImColor32::from_rgba(f32_to_u8(SETTINGS.deref().enemy_name_text_color[0]), f32_to_u8(SETTINGS.deref().enemy_name_text_color[1]), f32_to_u8(SETTINGS.deref().enemy_name_text_color[2]), f32_to_u8(SETTINGS.deref().enemy_name_text_color[3])).to_bits()
                                    },
                                    start,
                                    end,
                                    0.0f32,
                                    std::ptr::null()  // Assuming you don't want to specify a clipping rectangle
                                );
                                custom_font.pop();
                            }

When I drag slider that is bound to name_text_thickness I am able to change text size during this function call. What resolves my problem.

luadebug commented 3 months ago

That seems to be part of imgui-rs.