Skip to content

Commit

Permalink
radiobutton fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TobyAdd committed Dec 2, 2024
1 parent f0c538e commit 9dde499
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 25 deletions.
64 changes: 42 additions & 22 deletions libs/imgui/imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,36 +1213,56 @@ bool ImGui::CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value)

bool ImGui::RadioButton(const char* label, bool active, float scale)
{
ImVec2 buttonPosition = ImGui::GetCursorScreenPos();
ImDrawList* drawList = ImGui::GetWindowDrawList();
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;

float buttonWidth = 22.0f * scale;
float buttonHeight = 22.0f * scale;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label);
const ImVec2 label_size = CalcTextSize(label, NULL, true);

ImGui::InvisibleButton(label, ImVec2(buttonWidth + ImGui::CalcTextSize(label).x + 4.f, buttonHeight));
const float square_sz = GetFrameHeight();
const ImVec2 pos = window->DC.CursorPos;
const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz));
const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f));
ItemSize(total_bb, style.FramePadding.y);
if (!ItemAdd(total_bb, id))
return false;

bool buttonClicked = false;
if (ImGui::IsItemClicked()) {
active = true;
buttonClicked = true;
}
ImVec2 center = check_bb.GetCenter();
center.x = IM_ROUND(center.x);
center.y = IM_ROUND(center.y);
const float radius = (square_sz - 1.0f) * 0.5f;

float interpolationFactor = active ? 1.0f : 0.0f;
ImGuiContext& imguiContext = *GImGui;
float animationSpeed = 0.15f;
if (imguiContext.LastActiveId == imguiContext.CurrentWindow->GetID(label)) {
float animationProgress = ImSaturate(imguiContext.LastActiveIdTimer / animationSpeed);
interpolationFactor = active ? animationProgress : 1.0f - animationProgress;
bool hovered, held;
bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
if (pressed)
MarkItemEdited(id);

RenderNavHighlight(total_bb, id);
const int num_segment = window->DrawList->_CalcCircleAutoSegmentCount(radius);
window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), num_segment);
if (active)
{
const float pad = ImMax(1.0f, IM_TRUNC(square_sz / 6.0f));
window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark));
}

const ImU32 backgroundColor = ImGui::GetColorU32(ImLerp(ImColor(67, 72, 78).Value, ImColor(165, 255, 190).Value, interpolationFactor));
const ImVec2 buttonEndPosition(buttonPosition.x + buttonWidth, buttonPosition.y + buttonHeight);
drawList->AddRectFilled(buttonPosition, buttonEndPosition, backgroundColor, buttonHeight * 0.5f);
if (style.FrameBorderSize > 0.0f)
{
window->DrawList->AddCircle(center + ImVec2(1, 1), radius, GetColorU32(ImGuiCol_BorderShadow), num_segment, style.FrameBorderSize);
window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), num_segment, style.FrameBorderSize);
}

const ImVec2 textPosition(buttonPosition.x + buttonWidth + 5.0f, buttonPosition.y + buttonHeight / 2.0f - ImGui::GetTextLineHeight() / 2.0f);
drawList->AddText(textPosition, ImGui::GetColorU32(ImGuiCol_Text), label);
ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y);
if (g.LogEnabled)
LogRenderedText(&label_pos, active ? "(x)" : "( )");
if (label_size.x > 0.0f)
RenderText(label_pos, label);

return buttonClicked;
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags);
return pressed;
}


Expand Down
9 changes: 6 additions & 3 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ void Gui::Render() {
config.set("gui_inverted", !inverted);
ApplyGuiColors(!inverted);
}

if (ImGui::IsItemHovered())
ImGui::SetTooltip("Invert theme (beta)");
}
else if (windowName == "Framerate") {
bool tps_enabled = config.get<bool>("tps_enabled", false);
Expand Down Expand Up @@ -219,12 +222,12 @@ void Gui::Render() {

int mode_ = (int)engine.mode;

if (ImGui::RadioButton("Disable", &mode_, 0, m_scale))
if (ImGuiH::RadioButton("Disable", &mode_, 0, m_scale))
engine.mode = state::disable;

ImGui::SameLine();

if (ImGui::RadioButton("Record", &mode_, 1, m_scale))
if (ImGuiH::RadioButton("Record", &mode_, 1, m_scale))
{
bool canRecord = config.get<bool>("tps_enabled", false);

Expand All @@ -244,7 +247,7 @@ void Gui::Render() {

ImGui::SameLine();

if (ImGui::RadioButton("Play", &mode_, 2, m_scale)) {
if (ImGuiH::RadioButton("Play", &mode_, 2, m_scale)) {
bool canPlay = config.get<bool>("tps_enabled", false);

if (canPlay) {
Expand Down
48 changes: 48 additions & 0 deletions src/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,52 @@ namespace ImGuiH {

return buttonClicked;
}

static bool RadioButton2(const char* label, bool active, float scale)
{
auto &gui = Gui::get();
auto &config = Config::get();

ImVec2 buttonPosition = ImGui::GetCursorScreenPos();
ImDrawList* drawList = ImGui::GetWindowDrawList();

float buttonWidth = 22.0f * scale;
float buttonHeight = 22.0f * scale;

ImGui::InvisibleButton(label, ImVec2(buttonWidth + ImGui::CalcTextSize(label).x + 4.f, buttonHeight));

bool buttonClicked = false;
if (ImGui::IsItemClicked()) {
active = true;
buttonClicked = true;
}

float interpolationFactor = active ? 1.0f : 0.0f;
ImGuiContext& imguiContext = *GImGui;
float animationSpeed = 0.15f;
if (imguiContext.LastActiveId == imguiContext.CurrentWindow->GetID(label)) {
float animationProgress = ImSaturate(imguiContext.LastActiveIdTimer / animationSpeed);
interpolationFactor = active ? animationProgress : 1.0f - animationProgress;
}

int color_index = config.get<int>("gui_color_index", 0);
auto theme = themes[color_index];

const ImU32 backgroundColor = ImGui::GetColorU32(ImLerp(ImColor(67, 72, 78).Value, ImColor(theme.color_bg.r, theme.color_bg.g, theme.color_bg.b).Value, interpolationFactor));
const ImVec2 buttonEndPosition(buttonPosition.x + buttonWidth, buttonPosition.y + buttonHeight);
drawList->AddRectFilled(buttonPosition, buttonEndPosition, backgroundColor, buttonHeight * 0.5f);

const ImVec2 textPosition(buttonPosition.x + buttonWidth + 5.0f, buttonPosition.y + buttonHeight / 2.0f - ImGui::GetTextLineHeight() / 2.0f);
drawList->AddText(textPosition, ImGui::GetColorU32(ImGuiCol_Text), label);

return buttonClicked;
}

static bool RadioButton(const char* label, int* v, int v_button, float scale)
{
const bool pressed = RadioButton2(label, *v == v_button, scale);
if (pressed)
*v = v_button;
return pressed;
}
}

0 comments on commit 9dde499

Please sign in to comment.