Skip to content

Commit

Permalink
add imgui custom color picker component
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Nov 16, 2023
1 parent 897474d commit 483eb8e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/GUI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/SideBar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SettingsPanel.cpp
)

add_subdirectory(Components)
7 changes: 7 additions & 0 deletions src/GUI/Components/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.27)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/CustomColorPicker.cpp
)
67 changes: 67 additions & 0 deletions src/GUI/Components/CustomColorPicker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "Imgui.hpp"
#include "imgui.h"

namespace Xash
{
namespace GUI
{
static bool colorLabelClickable(const std::string_view label, const ImVec4 &color)
{
bool isClicked = false;

ImGui::ColorButton("##ColorPreview", color, ImGuiColorEditFlags_NoPicker);
if (ImGui::IsItemClicked())
{
isClicked = true;
}
ImGui::SameLine();
ImGui::Text("%s", label.data());
if (ImGui::IsItemClicked())
{
isClicked = true;
}
return isClicked;
}

static bool drawPopup(const std::string_view label, ImVec4 &color)
{
if (ImGui::BeginPopup(label.data()))
{
ImGui::ColorPicker4(
"##ColorPicker", (float *)&color,
ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_NoInputs
| ImGuiColorEditFlags_NoSidePreview
);
// Slider for each color component
ImGui::SliderFloat("##Red", &color.x, 0.0f, 1.0f, "R: %.3f");
ImGui::SliderFloat("##Green", &color.y, 0.0f, 1.0f, "G: %.3f");
ImGui::SliderFloat("##Blue", &color.z, 0.0f, 1.0f, "B: %.3f");
ImGui::SliderFloat("##Alpha", &color.w, 0.0f, 1.0f, "A: %.3f");
return true;
}
return false;
}

void Imgui::CustomColorPicker(
const std::string_view label, std::function<Vector4()> getter,
std::function<void(const Vector4 &)> setter, ImGuiCol_ colorIndex
)
{
const auto &settingColor = getter();
ImVec4 color{settingColor.x, settingColor.y, settingColor.z, settingColor.w};
bool isClicked = colorLabelClickable(label, color);

if (isClicked)
{
ImGui::OpenPopup(label.data());
}
if (drawPopup(label, color))
{
ImGui::GetStyle().Colors[colorIndex] =
ImVec4(color.x, color.y, color.z, color.w);
setter({color.x, color.y, color.z, color.w});
ImGui::EndPopup();
}
}
} // namespace GUI
} // namespace Xash
8 changes: 8 additions & 0 deletions src/GUI/Imgui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#include <vector>
#include <string>
#include <utility>
#include <functional>
#include "ModInfos.hpp"
#include "MonoModule.hpp"
#include "imgui.h"
#include "StyleSettings.hpp"

namespace Xash
{
Expand All @@ -29,6 +31,12 @@ namespace Xash
void Draw();
void Render();

// Custom ImGui Widgets
static void CustomColorPicker(
const std::string_view label, std::function<Vector4()> getter,
std::function<void(const Vector4 &)> setter, ImGuiCol_ colorIndex
);

private:
enum class ActivePanel : uint8_t
{
Expand Down
14 changes: 14 additions & 0 deletions src/settings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.27)

target_include_directories(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/XashSettings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/StyleSettings.cpp
)

0 comments on commit 483eb8e

Please sign in to comment.