first commit
This commit is contained in:
commit
fafeee709f
17 files changed
+1312
No files matched your search
@@ -0,0 +1,340 @@
|
||||
#pragma once
|
||||
#include <cfloat>
|
||||
#include <string>
|
||||
#include "imgui.h"
|
||||
#include "theme.h"
|
||||
#include "../input.h"
|
||||
#include "../modules/module_manager.h"
|
||||
|
||||
#if __has_include("../font_data.h")
|
||||
#include "../font_data.h"
|
||||
#define AMNESIA_HAS_FONT 1
|
||||
#else
|
||||
#define AMNESIA_HAS_FONT 0
|
||||
#endif
|
||||
|
||||
inline std::string key_name(int vk) {
|
||||
if (vk == 0) return "None";
|
||||
|
||||
LONG lparam = (LONG)(MapVirtualKeyA((UINT)vk, MAPVK_VK_TO_VSC) << 16);
|
||||
switch (vk) { // extended keys need bit 24 set to get the right name
|
||||
case VK_INSERT: case VK_DELETE: case VK_HOME: case VK_END:
|
||||
case VK_PRIOR: case VK_NEXT:
|
||||
case VK_LEFT: case VK_RIGHT: case VK_UP: case VK_DOWN:
|
||||
case VK_RCONTROL: case VK_RMENU:
|
||||
lparam |= (1 << 24);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
char buf[64] = {};
|
||||
if (GetKeyNameTextA(lparam, buf, sizeof(buf)) > 0) return buf;
|
||||
return "Key " + std::to_string(vk);
|
||||
}
|
||||
|
||||
class clickgui {
|
||||
public:
|
||||
// after ImGui::CreateContext(), before the backends are initialized
|
||||
void init() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.IniFilename = nullptr; // never write imgui.ini into the game's folder
|
||||
io.LogFilename = nullptr;
|
||||
load_fonts();
|
||||
theme::apply();
|
||||
}
|
||||
|
||||
// once per frame, after ImGui::NewFrame()
|
||||
void update() {
|
||||
const bool menu_down = is_key_down(k_menu_key);
|
||||
if (menu_down && !menu_key_was_down_) set_open(!g_menu_open);
|
||||
menu_key_was_down_ = menu_down;
|
||||
|
||||
const bool esc_down = is_key_down(VK_ESCAPE);
|
||||
if (esc_down && !esc_was_down_ && g_menu_open && !any_binding()) set_open(false);
|
||||
esc_was_down_ = esc_down;
|
||||
|
||||
open_t_ = theme::approach(open_t_, g_menu_open ? 1.f : 0.f, ImGui::GetIO().DeltaTime * 14.f);
|
||||
}
|
||||
|
||||
// dim + title; call BEFORE modules().render() so the HUD stays above the dimming
|
||||
void draw_backdrop() {
|
||||
if (open_t_ <= 0.01f) return;
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImDrawList* dl = ImGui::GetBackgroundDrawList();
|
||||
|
||||
dl->AddRectFilled(ImVec2(0.f, 0.f), io.DisplaySize,
|
||||
theme::u32(theme::with_alpha(theme::bg_dark(), 0.6f * open_t_)));
|
||||
|
||||
const char* title = "AMNESIA";
|
||||
const float tw = ImGui::CalcTextSize(title).x * k_title_size / ImGui::GetFontSize();
|
||||
const ImVec2 tp(io.DisplaySize.x * 0.5f - tw * 0.5f, 18.f);
|
||||
dl->AddText(font_title_, k_title_size, ImVec2(tp.x + 3.f, tp.y + 3.f),
|
||||
theme::u32(theme::with_alpha(theme::bg_dark(), open_t_)), title);
|
||||
dl->AddText(font_title_, k_title_size, tp,
|
||||
theme::u32(theme::with_alpha(theme::accent_bright(), open_t_)), title);
|
||||
|
||||
const char* hint = "Right-click a module for settings | drag HUD elements to move them";
|
||||
const float hw = ImGui::CalcTextSize(hint).x;
|
||||
dl->AddText(ImVec2(io.DisplaySize.x * 0.5f - hw * 0.5f, tp.y + k_title_size + 8.f),
|
||||
theme::u32(theme::with_alpha(theme::fg_muted(), open_t_)), hint);
|
||||
}
|
||||
|
||||
// the panels and the HUD drag editor
|
||||
void draw() {
|
||||
if (open_t_ <= 0.01f) return;
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, open_t_);
|
||||
|
||||
float x = 40.f;
|
||||
for (category cat : k_categories) {
|
||||
if (!has_modules(cat)) continue;
|
||||
draw_panel(cat, x);
|
||||
x += k_panel_w + 20.f;
|
||||
}
|
||||
draw_hud_editor();
|
||||
|
||||
ImGui::PopStyleVar();
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr int k_menu_key = VK_RSHIFT;
|
||||
static constexpr float k_panel_w = 230.f;
|
||||
static constexpr float k_main_size = 18.f;
|
||||
static constexpr float k_title_size = 32.f;
|
||||
static constexpr category k_categories[] = { category::hud, category::visual, category::utility };
|
||||
|
||||
ImFont* font_main_ = nullptr;
|
||||
ImFont* font_title_ = nullptr;
|
||||
float open_t_ = 0.f;
|
||||
bool menu_key_was_down_ = false;
|
||||
bool esc_was_down_ = false;
|
||||
|
||||
void load_fonts() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
#if AMNESIA_HAS_FONT
|
||||
ImFontConfig cfg;
|
||||
cfg.FontDataOwnedByAtlas = false; // the data lives in our DLL, ImGui must not free it
|
||||
font_main_ = io.Fonts->AddFontFromMemoryTTF((void*)k_font_data, (int)k_font_size, k_main_size, &cfg);
|
||||
font_title_ = io.Fonts->AddFontFromMemoryTTF((void*)k_font_data, (int)k_font_size, k_title_size, &cfg);
|
||||
#endif
|
||||
if (!font_main_) { // no font_data.h (or it failed): fall back to ImGui's built-in
|
||||
font_main_ = io.Fonts->AddFontDefault();
|
||||
font_title_ = font_main_;
|
||||
}
|
||||
io.FontDefault = font_main_;
|
||||
}
|
||||
|
||||
void set_open(bool open) {
|
||||
if (open == g_menu_open) return;
|
||||
g_menu_open = open;
|
||||
ImGui::GetIO().MouseDrawCursor = open; // ImGui draws its own cursor while open
|
||||
|
||||
if (open) {
|
||||
input::release_cursor();
|
||||
}
|
||||
else {
|
||||
input::restore_cursor();
|
||||
for (auto& m : modules().all()) m->ui.binding = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool any_binding() const {
|
||||
for (auto& m : modules().all())
|
||||
if (m->ui.binding) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool has_modules(category cat) const {
|
||||
for (auto& m : modules().all())
|
||||
if (m->cat() == cat) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// ---------------- panels ----------------
|
||||
|
||||
void draw_panel(category cat, float default_x) {
|
||||
ImGui::SetNextWindowPos(ImVec2(default_x, 110.f), ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowSize(ImVec2(k_panel_w, 0.f), ImGuiCond_Always); // fixed width, auto height
|
||||
|
||||
ImGuiWindowFlags flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse |
|
||||
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse |
|
||||
ImGuiWindowFlags_NoSavedSettings;
|
||||
if (!g_menu_open) flags |= ImGuiWindowFlags_NoInputs; // fading out: no more clicks
|
||||
|
||||
if (ImGui::Begin(category_name(cat), nullptr, flags)) {
|
||||
for (auto& m : modules().all())
|
||||
if (m->cat() == cat) draw_module(*m);
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void draw_module(module& m) {
|
||||
ImGui::PushID(&m);
|
||||
|
||||
ImDrawList* dl = ImGui::GetWindowDrawList();
|
||||
const float w = ImGui::GetContentRegionAvail().x;
|
||||
const float h = ImGui::GetFontSize() + 14.f;
|
||||
const ImVec2 p = ImGui::GetCursorScreenPos();
|
||||
|
||||
ImGui::InvisibleButton("##row", ImVec2(w, h));
|
||||
const bool hovered = ImGui::IsItemHovered();
|
||||
const bool active = ImGui::IsItemActive();
|
||||
if (ImGui::IsItemClicked(ImGuiMouseButton_Left)) m.toggle();
|
||||
if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
|
||||
m.ui.expanded = !m.ui.expanded;
|
||||
m.ui.binding = false;
|
||||
}
|
||||
if (hovered)
|
||||
ImGui::SetTooltip("%s\n\nLMB: toggle | RMB: settings", m.description().c_str());
|
||||
|
||||
const float dt = ImGui::GetIO().DeltaTime;
|
||||
m.ui.hover_t = theme::approach(m.ui.hover_t, hovered ? 1.f : 0.f, dt * 16.f);
|
||||
m.ui.enable_t = theme::approach(m.ui.enable_t, m.enabled() ? 1.f : 0.f, dt * 12.f);
|
||||
m.ui.expand_t = theme::approach(m.ui.expand_t, m.ui.expanded ? 1.f : 0.f, dt * 12.f);
|
||||
|
||||
ImVec4 base = theme::mix(theme::bg_raised(), theme::accent(), m.ui.enable_t);
|
||||
base = theme::shade(base, 1.f + 0.15f * m.ui.hover_t);
|
||||
theme::bevel_box(dl, p, ImVec2(p.x + w, p.y + h), base, active);
|
||||
|
||||
const ImVec4 text_col = theme::mix(theme::fg(), theme::bg_dark(), m.ui.enable_t);
|
||||
const float text_y = p.y + (h - ImGui::GetFontSize()) * 0.5f;
|
||||
theme::shadow_text(dl, ImVec2(p.x + 8.f, text_y), text_col, m.name().c_str());
|
||||
|
||||
if (m.toggle_key()) {
|
||||
const std::string k = "[" + key_name(m.toggle_key()) + "]";
|
||||
const float kw = ImGui::CalcTextSize(k.c_str()).x;
|
||||
const ImVec4 kcol = theme::mix(theme::fg_muted(), theme::bg_dark(), m.ui.enable_t * 0.6f);
|
||||
dl->AddText(ImVec2(p.x + w - kw - 8.f, text_y), theme::u32(kcol), k.c_str());
|
||||
}
|
||||
|
||||
if (m.ui.expand_t > 0.01f) {
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * m.ui.expand_t);
|
||||
ImGui::Indent(6.f);
|
||||
draw_bind_button(m);
|
||||
for (auto& s : m.settings()) draw_setting(s);
|
||||
ImGui::Unindent(6.f);
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::Dummy(ImVec2(0.f, 2.f));
|
||||
}
|
||||
|
||||
ImGui::PopID();
|
||||
}
|
||||
|
||||
void draw_bind_button(module& m) {
|
||||
std::string label;
|
||||
if (m.ui.binding) label = "Bind: press a key...";
|
||||
else label = "Bind: [" + key_name(m.toggle_key()) + "]";
|
||||
|
||||
if (ImGui::Button(label.c_str(), ImVec2(-FLT_MIN, 0.f))) m.ui.binding = !m.ui.binding;
|
||||
|
||||
if (m.ui.binding) {
|
||||
for (int vk = 0x08; vk < 0xFF; ++vk) { // starts above the mouse buttons
|
||||
if (is_key_down(vk)) {
|
||||
m.set_toggle_key(vk == VK_ESCAPE ? 0 : vk); // Esc clears the bind
|
||||
m.ui.binding = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_setting(setting& s) {
|
||||
ImGui::PushID(&s);
|
||||
|
||||
switch (s.type) {
|
||||
case setting_type::toggle: {
|
||||
const std::string label = s.name + ": " + (s.bool_value ? "ON" : "OFF");
|
||||
ImGui::PushStyleColor(ImGuiCol_Text, s.bool_value ? theme::accent_bright() : theme::fg_muted());
|
||||
if (ImGui::Button(label.c_str(), ImVec2(-FLT_MIN, 0.f))) s.bool_value = !s.bool_value;
|
||||
ImGui::PopStyleColor();
|
||||
break;
|
||||
}
|
||||
case setting_type::slider_float: {
|
||||
const std::string fmt = s.name + ": %.2f";
|
||||
ImGui::SetNextItemWidth(-FLT_MIN);
|
||||
ImGui::SliderFloat("##v", &s.float_value, s.float_min, s.float_max, fmt.c_str());
|
||||
break;
|
||||
}
|
||||
case setting_type::slider_int: {
|
||||
const std::string fmt = s.name + ": %d";
|
||||
ImGui::SetNextItemWidth(-FLT_MIN);
|
||||
ImGui::SliderInt("##v", &s.int_value, s.int_min, s.int_max, fmt.c_str());
|
||||
break;
|
||||
}
|
||||
case setting_type::mode: {
|
||||
if (!s.modes.empty()) {
|
||||
if (s.int_value < 0 || s.int_value >= (int)s.modes.size()) s.int_value = 0;
|
||||
const std::string label = s.name + ": " + s.modes[s.int_value];
|
||||
if (ImGui::Button(label.c_str(), ImVec2(-FLT_MIN, 0.f)))
|
||||
s.int_value = (s.int_value + 1) % (int)s.modes.size();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case setting_type::color: {
|
||||
ImGui::ColorEdit4("##c", s.color,
|
||||
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel |
|
||||
ImGuiColorEditFlags_AlphaBar);
|
||||
ImGui::SameLine();
|
||||
ImGui::TextUnformatted(s.name.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::PopID();
|
||||
}
|
||||
|
||||
// ---------------- HUD drag editor ----------------
|
||||
|
||||
void draw_hud_editor() {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
for (auto& up : modules().all()) {
|
||||
module& m = *up;
|
||||
if (!m.enabled() || !m.is_hud()) continue;
|
||||
|
||||
const ImVec2 size = m.hud_size();
|
||||
if (size.x <= 0.f || size.y <= 0.f) continue;
|
||||
|
||||
// keep it on screen
|
||||
m.hud_pos.x = theme::clampf(m.hud_pos.x, 0.f, io.DisplaySize.x - size.x);
|
||||
m.hud_pos.y = theme::clampf(m.hud_pos.y, 0.f, io.DisplaySize.y - size.y);
|
||||
|
||||
ImGui::SetNextWindowPos(m.hud_pos, ImGuiCond_Always);
|
||||
ImGui::SetNextWindowSize(size, ImGuiCond_Always);
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.f, 0.f));
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f);
|
||||
|
||||
const std::string id = "##hud_" + m.name();
|
||||
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
|
||||
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar |
|
||||
ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoSavedSettings |
|
||||
ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoNav |
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus;
|
||||
if (!g_menu_open) flags |= ImGuiWindowFlags_NoInputs;
|
||||
|
||||
if (ImGui::Begin(id.c_str(), nullptr, flags)) {
|
||||
ImGui::InvisibleButton("##drag", size);
|
||||
const bool hovered = ImGui::IsItemHovered();
|
||||
const bool active = ImGui::IsItemActive();
|
||||
|
||||
if (active && ImGui::IsMouseDragging(ImGuiMouseButton_Left, 0.f)) {
|
||||
m.hud_pos.x += io.MouseDelta.x;
|
||||
m.hud_pos.y += io.MouseDelta.y;
|
||||
}
|
||||
|
||||
const ImVec4 col = (hovered || active) ? theme::accent_bright() : theme::accent_dark();
|
||||
ImGui::GetForegroundDrawList()->AddRect(
|
||||
m.hud_pos, ImVec2(m.hud_pos.x + size.x, m.hud_pos.y + size.y),
|
||||
theme::u32(col), 0.f, 0, 2.f);
|
||||
}
|
||||
ImGui::End();
|
||||
ImGui::PopStyleVar(2);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
inline clickgui& gui() {
|
||||
static clickgui instance;
|
||||
return instance;
|
||||
}
|
||||
Reference in new issue
Block a user