first commit

This commit is contained in:
st committed 2026-10-12 18:24:53 -05:00
commit fafeee709f
17 files changed
+1312

No files matched your search

+340
View File
@@ -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;
}
+125
View File
@@ -0,0 +1,125 @@
#pragma once
#include <utility>
#include "imgui.h"
namespace theme {
inline float clampf(float v, float lo, float hi) { return v < lo ? lo : (v > hi ? hi : v); }
inline ImVec4 hex(unsigned int rgb, float a = 1.f) {
return ImVec4(((rgb >> 16) & 0xFF) / 255.f, ((rgb >> 8) & 0xFF) / 255.f, (rgb & 0xFF) / 255.f, a);
}
// every color goes through the style alpha, so the whole menu fades together
inline ImU32 u32(const ImVec4& c) {
ImVec4 v = c;
v.w *= ImGui::GetStyle().Alpha;
return ImGui::ColorConvertFloat4ToU32(v);
}
inline ImVec4 with_alpha(ImVec4 c, float a) { c.w = a; return c; }
inline ImVec4 mix(const ImVec4& a, const ImVec4& b, float t) {
return ImVec4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t);
}
inline ImVec4 shade(const ImVec4& c, float f) {
return ImVec4(clampf(c.x * f, 0.f, 1.f), clampf(c.y * f, 0.f, 1.f), clampf(c.z * f, 0.f, 1.f), c.w);
}
inline float approach(float v, float target, float rate) {
v += (target - v) * clampf(rate, 0.f, 1.f);
return (v - target < 0.001f && target - v < 0.001f) ? target : v;
}
// ---- "orphan" palette ----
inline ImVec4 bg_dark() { return hex(0x17141c); }
inline ImVec4 bg() { return hex(0x2b2632); }
inline ImVec4 bg_raised() { return hex(0x38313c); }
inline ImVec4 fg() { return hex(0xc9b5af); }
inline ImVec4 fg_muted() { return hex(0x8372a0); }
inline ImVec4 fg_dim() { return hex(0x625577); }
inline ImVec4 accent() { return hex(0x8c79a5); }
inline ImVec4 accent_dark() { return hex(0x74608e); }
inline ImVec4 accent_bright() { return hex(0xbfa5ea); }
inline ImVec4 border() { return hex(0x433a4f); }
// Minecraft-button look: flat fill, light top/left edge, dark bottom/right edge
inline void bevel_box(ImDrawList* dl, ImVec2 a, ImVec2 b, const ImVec4& base, bool pressed = false) {
const float t = 2.f;
ImVec4 hi = shade(base, 1.35f), lo = shade(base, 0.6f);
if (pressed) std::swap(hi, lo);
dl->AddRectFilled(a, b, u32(base));
dl->AddRectFilled(a, ImVec2(b.x, a.y + t), u32(hi));
dl->AddRectFilled(a, ImVec2(a.x + t, b.y), u32(hi));
dl->AddRectFilled(ImVec2(a.x, b.y - t), b, u32(lo));
dl->AddRectFilled(ImVec2(b.x - t, a.y), b, u32(lo));
}
// Minecraft-style drop shadow
inline void shadow_text(ImDrawList* dl, ImVec2 pos, const ImVec4& col, const char* text) {
dl->AddText(ImVec2(pos.x + 2.f, pos.y + 2.f), u32(with_alpha(bg_dark(), col.w)), text);
dl->AddText(pos, u32(col), text);
}
inline void apply() {
ImGuiStyle& s = ImGui::GetStyle();
s.WindowRounding = s.ChildRounding = s.FrameRounding = s.PopupRounding = 0.f;
s.ScrollbarRounding = s.GrabRounding = s.TabRounding = 0.f;
s.WindowBorderSize = 2.f;
s.ChildBorderSize = 1.f;
s.PopupBorderSize = 2.f;
s.FrameBorderSize = 1.f;
s.WindowPadding = ImVec2(6.f, 6.f);
s.FramePadding = ImVec2(8.f, 5.f);
s.ItemSpacing = ImVec2(6.f, 5.f);
s.WindowTitleAlign = ImVec2(0.5f, 0.5f);
s.GrabMinSize = 12.f;
s.ScrollbarSize = 10.f;
s.AntiAliasedLines = false; // crisp, blocky edges
s.AntiAliasedFill = false;
ImVec4* c = s.Colors;
const ImVec4 clear(0.f, 0.f, 0.f, 0.f);
c[ImGuiCol_Text] = fg();
c[ImGuiCol_TextDisabled] = fg_dim();
c[ImGuiCol_WindowBg] = with_alpha(bg(), 0.96f);
c[ImGuiCol_ChildBg] = clear;
c[ImGuiCol_PopupBg] = with_alpha(bg_dark(), 0.98f);
c[ImGuiCol_Border] = bg_dark();
c[ImGuiCol_BorderShadow] = clear;
c[ImGuiCol_FrameBg] = bg_raised();
c[ImGuiCol_FrameBgHovered] = shade(bg_raised(), 1.25f);
c[ImGuiCol_FrameBgActive] = shade(bg_raised(), 1.45f);
c[ImGuiCol_TitleBg] = shade(accent_dark(), 0.75f);
c[ImGuiCol_TitleBgActive] = accent_dark();
c[ImGuiCol_TitleBgCollapsed] = shade(accent_dark(), 0.75f);
c[ImGuiCol_ScrollbarBg] = clear;
c[ImGuiCol_ScrollbarGrab] = border();
c[ImGuiCol_ScrollbarGrabHovered] = fg_dim();
c[ImGuiCol_ScrollbarGrabActive] = fg_muted();
c[ImGuiCol_CheckMark] = accent_bright();
c[ImGuiCol_SliderGrab] = accent();
c[ImGuiCol_SliderGrabActive] = accent_bright();
c[ImGuiCol_Button] = bg_raised();
c[ImGuiCol_ButtonHovered] = shade(bg_raised(), 1.3f);
c[ImGuiCol_ButtonActive] = accent_dark();
c[ImGuiCol_Header] = accent_dark();
c[ImGuiCol_HeaderHovered] = accent();
c[ImGuiCol_HeaderActive] = accent();
c[ImGuiCol_Separator] = border();
}
} // namespace theme