Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
add gruvbox theme + fix some ui code
Browse files Browse the repository at this point in the history
  • Loading branch information
Prevter committed Mar 26, 2024
1 parent aa26608 commit e2c53ee
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/shared/gui/themes/classic/classic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ namespace openhack::gui {
config::set("menu.color.border", Color(0.0f, 0.0f, 0.0f, 1.0f)); // Border color
config::set("menu.color.hovered", Color(0.12f, 0.43f, 0.65f, 1.00f)); // Item hovered
config::set("menu.color.clicked", Color(0.11f, 0.36f, 0.55f, 1.00f)); // Item clicked

config::set("menu.windowRounding", 0.0f);
config::set("menu.frameRounding", 0.0f);
}

}
250 changes: 250 additions & 0 deletions src/shared/gui/themes/gruvbox/gruvbox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
#include "gruvbox.hpp"

namespace openhack::gui {

void GruvboxTheme::setStyles() {
auto &style = ImGui::GetStyle();
style.WindowPadding = ImVec2(4, 4);
style.WindowRounding = config::get<float>("menu.windowRounding");
style.FramePadding = ImVec2(4, 2);
style.FrameRounding = config::get<float>("menu.frameRounding");
style.PopupRounding = config::get<float>("menu.frameRounding");
style.ItemSpacing = ImVec2(12, 2);
style.ItemInnerSpacing = ImVec2(8, 6);
style.WindowTitleAlign = ImVec2(0.5f, 0.5f);
style.IndentSpacing = 25.0f;
style.ScrollbarSize = 15.0f;
style.ScrollbarRounding = 9.0f;
style.GrabMinSize = 5.0f;
style.GrabRounding = 3.0f;
style.WindowBorderSize = config::get<float>("menu.borderSize");
style.WindowMinSize = ImVec2(32, 32);
style.DisplayWindowPadding = ImVec2(0, 0);
style.ScaleAllSizes(config::getGlobal<float>("UIScale"));
style.WindowMenuButtonPosition = ImGuiDir_Right;

auto &colors = style.Colors;
colors[ImGuiCol_Text] = config::get<Color>("menu.color.text");
colors[ImGuiCol_TextDisabled] = config::get<Color>("menu.color.textDisabled");

colors[ImGuiCol_WindowBg] = config::get<Color>("menu.color.background");
colors[ImGuiCol_FrameBg] = config::get<Color>("menu.color.secondary");
colors[ImGuiCol_TitleBg] =
colors[ImGuiCol_TitleBgActive] =
colors[ImGuiCol_TitleBgCollapsed] =
config::get<Color>(config::get<bool>("menu.rainbow.enabled", false) ? "menu.color.accent"
: "menu.color.background");

colors[ImGuiCol_Button] = config::get<Color>("menu.color.primary");
colors[ImGuiCol_ButtonHovered] = config::get<Color>("menu.color.hovered");
colors[ImGuiCol_ButtonActive] = config::get<Color>("menu.color.clicked");

colors[ImGuiCol_FrameBgHovered] = config::get<Color>("menu.color.secondary");

colors[ImGuiCol_ScrollbarBg] = config::get<Color>("menu.color.secondary");
colors[ImGuiCol_ScrollbarGrab] = config::get<Color>("menu.color.primary");
colors[ImGuiCol_ScrollbarGrabHovered] = config::get<Color>("menu.color.hovered");
colors[ImGuiCol_ScrollbarGrabActive] = config::get<Color>("menu.color.clicked");

colors[ImGuiCol_CheckMark] = config::get<Color>("menu.color.primary");
colors[ImGuiCol_SliderGrab] = config::get<Color>("menu.color.primary");
colors[ImGuiCol_SliderGrabActive] = config::get<Color>("menu.color.clicked");

colors[ImGuiCol_Border] = config::get<Color>("menu.color.border");
colors[ImGuiCol_BorderShadow] = config::get<Color>("menu.color.border");

colors[ImGuiCol_PopupBg] = config::get<Color>("menu.color.background");
colors[ImGuiCol_Header] = config::get<Color>("menu.color.primary");
colors[ImGuiCol_HeaderHovered] = config::get<Color>("menu.color.hovered");
colors[ImGuiCol_HeaderActive] = config::get<Color>("menu.color.clicked");
}

void GruvboxTheme::loadPalette() {
config::set("menu.color.text", Color(0.92f, 0.86f, 0.7f, 1.0f)); // Text color
config::set("menu.color.textDisabled", Color(0.51f, 0.65f, 0.6f, 0.95f)); // Disabled text color
config::set("menu.color.background", Color(0.24f, 0.22f, 0.21f, 0.999f)); // Window background
config::set("menu.color.accent", Color(0.4f, 0.62f, 0.41f, 1.0f)); // Title background
config::set("menu.color.primary", Color(0.98f, 0.29f, 0.20f, 1.0f)); // Button color
config::set("menu.color.secondary", Color(0.18f, 0.16f, 0.15f, 0.95f)); // Frame background
config::set("menu.color.border", Color(0.66f, 0.6f, 0.52f, 1.0f)); // Border color
config::set("menu.color.hovered", Color(0.961f, 0.357f, 0.282f, 1.0f)); // Hovered color
config::set("menu.color.clicked", Color(0.95f, 0.42f, 0.36f, 1.0f)); // Clicked color

config::set("menu.windowRounding", 0.0f);
config::set("menu.frameRounding", 7.0f);
config::set("menu.borderSize", 1.0f);
}

bool GruvboxCheckbox(const char *label, bool value, float width = -1.0f) {
auto *drawList = ImGui::GetWindowDrawList();
auto pos = ImGui::GetCursorScreenPos();
auto scale = config::getGlobal<float>("UIScale");
auto padding = scale * 3;
auto size = ImGui::GetFrameHeight() - padding * 2;
auto frameColor = ImGui::GetColorU32(ImGuiCol_FrameBg);

auto start = ImVec2(pos.x + padding, pos.y + padding);
auto end = ImVec2(start.x + size, start.y + size);

drawList->AddRectFilled(start, end, frameColor, scale * 6);
if (value) {
auto checkColor = ImGui::GetColorU32(ImGuiCol_CheckMark);
auto checkStart = ImVec2(start.x + 3, start.y + 3);
auto checkEnd = ImVec2(end.x - 3, end.y - 3);
drawList->AddRectFilled(checkStart, checkEnd, checkColor, scale * 3);
}

auto leftMargin = size + padding * 2;
auto btnWidth = width > 0 ? width : ImGui::GetContentRegionAvail().x;
bool clicked = ImGui::Button(fmt::format("##{}", label).c_str(), ImVec2(btnWidth, 0));
auto buttonPos = ImGui::GetItemRectMin();
// get the text before ## if it exists
auto text = std::string(label);
auto hashPos = text.find("##");
if (hashPos != std::string::npos)
text = text.substr(0, hashPos);
drawList->AddText(ImVec2(buttonPos.x + leftMargin, buttonPos.y + padding / 2),
ImGui::GetColorU32(ImGuiCol_Text), text.c_str());

return clicked;
}

bool GruvboxTheme::checkbox(const char *label, bool *value) {
ImGui::PushItemWidth(-1.0f);
auto textColor = *value ? config::get<Color>("menu.color.accent") : config::get<Color>(
"menu.color.textDisabled");

if (utils::isSearching()) {
if (utils::isSearched(label)) {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1, 1, 1, 1));
ImGui::PushStyleColor(ImGuiCol_CheckMark, (ImVec4) config::get<Color>("menu.color.primary"));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0.1f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0.2f));
} else {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 0.5f, 0.5f, 0.5f));
ImGui::PushStyleColor(ImGuiCol_CheckMark, ImVec4(0.5f, 0.5f, 0.5f, 0.5f));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));
}
} else {
ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4) textColor);
ImGui::PushStyleColor(ImGuiCol_CheckMark, (ImVec4) textColor);
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0.1f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0.2f));
}

bool clicked = GruvboxCheckbox(label, *value);
ImGui::PopStyleColor(5);

if (clicked) *value = !*value;
ImGui::PopItemWidth();
return clicked;
}

bool GruvboxTheme::toggleSetting(const char *label, bool *value, const std::function<void()> &popupDraw, ImVec2 size,
float minWidth) {
ImGui::PushItemWidth(-1.0f);
auto textColor = *value ? config::get<Color>("menu.color.accent") : config::get<Color>(
"menu.color.textDisabled");

if (utils::isSearching()) {
if (utils::isSearched(label)) {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1, 1, 1, 1));
ImGui::PushStyleColor(ImGuiCol_CheckMark, (ImVec4) config::get<Color>("menu.color.primary"));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0.1f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0.2f));
} else {
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.5f, 0.5f, 0.5f, 0.5f));
ImGui::PushStyleColor(ImGuiCol_CheckMark, ImVec4(0.5f, 0.5f, 0.5f, 0.5f));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));
}
} else {
ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4) textColor);
ImGui::PushStyleColor(ImGuiCol_CheckMark, (ImVec4) textColor);
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0.1f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0.2f));
}

auto availWidth = ImGui::GetContentRegionAvail().x;
auto buttonSize = ImVec2(availWidth * 0.88f, 0);
auto arrowSize = ImVec2(availWidth * 0.12f, 0);

if (size.x > 0) {
buttonSize.x *= size.x;
arrowSize.x *= size.x;
}

bool clicked = GruvboxCheckbox(label, *value, buttonSize.x);
ImGui::PopStyleColor(4);
ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4) config::get<Color>("menu.color.text"));
gui::callback();
ImGui::PopStyleColor();
ImGui::SameLine(0, 0);
ImGui::SetNextItemWidth(arrowSize.x);
ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4) config::get<Color>("menu.color.text"));
bool openPopup = ImGui::ArrowButton((std::string("##open_") + label).c_str(), ImGuiDir_Right);
ImGui::PopItemWidth();
ImGui::PopStyleColor(2);
ImGui::PopItemWidth();

if (clicked) *value = !*value;

std::string popupName = std::string("##") + label;
if (openPopup)
ImGui::OpenPopup(popupName.c_str());

auto scale = config::getGlobal<float>("UIScale");
ImGui::SetNextWindowSizeConstraints(ImVec2(minWidth * scale, 0), ImVec2(FLT_MAX, FLT_MAX));
if (ImGui::BeginPopup(popupName.c_str())) {
popupDraw();
ImGui::EndPopup();
}

return clicked;
}

void GruvboxTheme::popupSettings(const char *label, const std::function<void()> &content, ImVec2 size) {
ImGui::PushItemWidth(-1);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 2));
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.0f, 0.5f));

ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));

auto availWidth = ImGui::GetContentRegionAvail().x;
auto buttonSize = ImVec2(availWidth * 0.885f, 0);
auto arrowSize = ImVec2(availWidth * 0.115f, 0);

if (size.x > 0) {
buttonSize.x *= size.x;
arrowSize.x *= size.x;
}

ImGui::Button(label, buttonSize);
ImGui::SameLine(0, 0);

ImGui::PopStyleColor(3);
ImGui::PopStyleVar(2);
ImGui::SetNextItemWidth(arrowSize.x);
bool openPopup = ImGui::ArrowButton((std::string("##open_") + label).c_str(), ImGuiDir_Right);
ImGui::PopItemWidth();

std::string popupName = std::string("##") + label;
if (openPopup)
ImGui::OpenPopup(popupName.c_str());

if (ImGui::BeginPopup(popupName.c_str())) {
content();
ImGui::EndPopup();
}
}

}
20 changes: 20 additions & 0 deletions src/shared/gui/themes/gruvbox/gruvbox.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "../themes.hpp"

namespace openhack::gui {
class GruvboxTheme : public Theme {
public:
std::string getName() override { return "Gruvbox"; }

void setStyles() override;

void loadPalette() override;

bool checkbox(const char *label, bool *value) override;

void popupSettings(const char* label, const std::function<void()> &content, ImVec2 size) override;

bool toggleSetting(const char *label, bool *value, const std::function<void()> &popupDraw, ImVec2 size, float minWidth) override;
};
}
4 changes: 4 additions & 0 deletions src/shared/gui/themes/megahack/megahack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace openhack::gui {
config::set("menu.color.border", Color(0.0f, 0.0f, 0.0f, 1.0f)); // Border color
config::set("menu.color.hovered", Color(0.3f, 0.76f, 0.6f, 1.0f)); // Item hovered
config::set("menu.color.clicked", Color(0.22f, 0.55f, 0.44f, 1.0f)); // Item clicked

config::set("menu.windowRounding", 0.0f);
config::set("menu.frameRounding", 0.0f);
config::set("menu.borderSize", 0.0f);
}

bool MegaHackTheme::button(const char *label, const ImVec2 &size) {
Expand Down
4 changes: 4 additions & 0 deletions src/shared/gui/themes/modern/modern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace openhack::gui {
config::set("menu.color.border", Color(0.0f, 0.0f, 0.0f, 1.0f)); // Border color
config::set("menu.color.hovered", Color(0.58f, 0.42f, 0.79f, 1.00f)); // Item hovered
config::set("menu.color.clicked", Color(0.44f, 0.31f, 0.58f, 1.00f)); // Item clicked

config::set("menu.windowRounding", 4.0f);
config::set("menu.frameRounding", 4.0f);
config::set("menu.borderSize", 0.0f);
}

bool ModernTheme::inputFloat(const char *label, float *value, float min, float max, const char *format) {
Expand Down
22 changes: 13 additions & 9 deletions src/shared/gui/themes/themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "classic/classic.hpp"
#include "megahack/megahack.hpp"
#include "modern/modern.hpp"
// #include "steam2000/steam2000.hpp"
#include "gruvbox/gruvbox.hpp"

namespace openhack::gui {
bool Theme::beginWindow(const char *name, bool *open, ImGuiWindowFlags flags) {
Expand Down Expand Up @@ -94,8 +94,8 @@ namespace openhack::gui {
}

void Theme::popupSettings(const char *label, const std::function<void()> &content, ImVec2 size) {
// 95% is taken by a transparent button with label
// 5% is taken by the small button with the arrow
// 88% is taken by a transparent button with label
// 12% is taken by the small button with the arrow
ImGui::PushItemWidth(-1);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 2));
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.0f, 0.5f));
Expand All @@ -105,8 +105,8 @@ namespace openhack::gui {
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));

auto availWidth = ImGui::GetContentRegionAvail().x;
auto buttonSize = ImVec2(availWidth * 0.9f, 0);
auto arrowSize = ImVec2(availWidth * 0.1f, 0);
auto buttonSize = ImVec2(availWidth * 0.885f, 0);
auto arrowSize = ImVec2(availWidth * 0.115f, 0);

if (size.x > 0) {
buttonSize.x *= size.x;
Expand Down Expand Up @@ -227,8 +227,8 @@ namespace openhack::gui {

bool Theme::toggleSetting(const char *label, bool *value, const std::function<void()> &popupDraw, ImVec2 size,
float minWidth) {
// 95% is taken by a transparent button with label
// 5% is taken by the small button with the arrow
// 88% is taken by a transparent button with label
// 12% is taken by the small button with the arrow
ImGui::PushItemWidth(-1);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 2));
ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, ImVec2(0.0f, 0.5f));
Expand All @@ -254,8 +254,8 @@ namespace openhack::gui {
}

auto availWidth = ImGui::GetContentRegionAvail().x;
auto buttonSize = ImVec2(availWidth * 0.9f, 0);
auto arrowSize = ImVec2(availWidth * 0.1f, 0);
auto buttonSize = ImVec2(availWidth * 0.885f, 0);
auto arrowSize = ImVec2(availWidth * 0.115f, 0);

if (size.x > 0) {
buttonSize.x *= size.x;
Expand Down Expand Up @@ -308,12 +308,16 @@ namespace openhack::gui {
case Themes::Classic:
setTheme(new ClassicTheme());
break;
default:
case Themes::Modern:
setTheme(new ModernTheme());
break;
case Themes::MegaHack:
setTheme(new MegaHackTheme());
break;
case Themes::Gruvbox:
setTheme(new GruvboxTheme());
break;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/shared/gui/themes/themes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,16 @@ namespace openhack::gui
MegaHack = 1,
/// @brief Modern UI theme.
Modern = 2,
/// @brief Gruvbox theme.
Gruvbox = 3,
};

/// @brief The names of the themes.
const char *const THEME_NAMES[] = {
"Classic",
"MegaHack",
"Modern",
"Gruvbox",
};

/// @brief The count of the themes.
Expand Down
Loading

0 comments on commit e2c53ee

Please sign in to comment.