Исходник Modernized menu Tooltip // supremacy c+p

Начинающий
Начинающий
Статус
Оффлайн
Регистрация
15 Окт 2019
Сообщения
42
Реакции
6
USAGE:
You need to define m_tooltip in the element you want, example button.cpp
button.h:
Expand Collapse Copy
#pragma once
#define BUTTON_X_OFFSET      20
#define BUTTON_BOX_HEIGHT    20
#define BUTTON_ITEM_X_OFFSET 10

class Button : public Element {
    friend class GUI;

public:
    __forceinline Button() : m_label{} {
        m_flags = DRAW | CLICK;
        m_type = BUTTON;
        m_base_h = m_h = BUTTON_BOX_HEIGHT;
        m_use_label = false;
    }

    __forceinline void setup(const std::string& label, const std::string& tooltip) {
        m_label = label;
        m_tooltip = tooltip;

    }

protected:
    std::string m_label;
    std::string m_tooltip;

    void draw() override;
    void click() override;
};

button.cpp:
Expand Collapse Copy
void Button::draw() {
      // ... all ur shit code.....
    // tooltip logic.
    static Tooltip tooltip;
    if (is_hovered && !m_tooltip.empty()) {
        tooltip.show(this);
        tooltip.draw(this, { cx, y }, m_tooltip);
    }
    else {
        tooltip.hide(this);
    }
}

!!! as you added new setup condition you need to adapt menu.h accordingly !!!
menu.h:
Expand Collapse Copy
// button calls.
        save.setup(XOR("save"), XOR("save configuration")); // here you add tool tip
        save.SetCallback(callbacks::ConfigSave);
        RegisterElement(&save, 1);

        load.setup(XOR("load"), XOR("load configuration")); // or leave empty for no
        load.SetCallback(callbacks::ConfigLoad);
        RegisterElement(&load, 1);

AND FINALLY
tooltip.h:
Expand Collapse Copy
#pragma once

class Tooltip {
public:
    // draw the tooltip.
    void draw(const void* owner, const Point& pos, const std::string& text);

    // show the tooltip
    void show(const void* owner);

    // hide the tooltip.
    void hide(const void* owner);
};

tooltip.cpp:
Expand Collapse Copy
#include "../../../includes.h"

struct TooltipAnimState {
    float alpha;         // tooltip alpha [0..1].
    bool visible;        // is tooltip currently visible.
    bool initialized;    // has the state been initialized.

    TooltipAnimState() : alpha(0.f), visible(false), initialized(false) {}
};
static std::unordered_map<const void*, TooltipAnimState> g_tooltip_anims;

void Tooltip::draw(const void* owner, const Point& pos, const std::string& text) {
    TooltipAnimState& anim = g_tooltip_anims[owner];

    float speed = 14.0f * g_csgo.m_globals->m_frametime;
    if (!anim.initialized) {
        anim.alpha = 0.f;
        anim.visible = false;
        anim.initialized = true;
    }

    float target = anim.visible ? 1.f : 0.f;
    anim.alpha += (target - anim.alpha) * speed;
    if (std::abs(anim.alpha - target) < 0.01f)
        anim.alpha = target;

    if (anim.alpha <= 0.f)
        return;

    auto size = render::menu.size(text);
    int padding = 6;
    int w = size.m_width + padding * 2;
    int h = size.m_height + padding * 2;
    int x = pos.x;
    int y = pos.y - h - 8;

    int screen_w = g_cl.m_width;
    int screen_h = g_cl.m_height;
    if (x + w > screen_w)
        x = screen_w - w - 2;
    if (y < 0)
        y = pos.y + 18;

    int a = int(220 * anim.alpha);

    render::rect_filled(x, y, w, h, { 30, 30, 30, a });
    render::rect(x, y, w, h, { 0, 0, 0, a });
    render::menu.string(x + padding, y + padding, { 205, 205, 205, a }, text);
}

void Tooltip::show(const void* owner) {
    TooltipAnimState& anim = g_tooltip_anims[owner];
    anim.visible = true;
}

void Tooltip::hide(const void* owner) {
    TooltipAnimState& anim = g_tooltip_anims[owner];
    anim.visible = false;
}
 
Последнее редактирование:
Назад
Сверху Снизу