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

Commit

Permalink
Merge pull request #91 dev -> master, Added custom fonts support, GUI…
Browse files Browse the repository at this point in the history
… enchantments

dev -> master, Added custom fonts support, GUI enchantments
  • Loading branch information
kualta authored Oct 10, 2021
2 parents 52ff439 + 53de594 commit f135e57
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 25 deletions.
17 changes: 14 additions & 3 deletions include/core/GUI/GUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,32 @@ class GUI {
public:

static bool Button(const string& label = "Button", Vector2 size = { 0, 0 } );
static bool CheckBox(const string& label, bool* value);
static bool CheckBox(const string& label, bool& value);
static void Text(const string& text);
static void TextLabel(const string& text, const string& label);
static void Tooltip(const string& text);

static bool Box(bool& value);
static void EndBox();

template<typename T>
static bool Input(const string& label, T *value);

template<typename T>
static bool Slider(const string& label, T* value, float min, float max);

static bool IsHovered();
static bool IsActive();
static bool IsFocused();

protected:

template<typename T>
static bool InputMatrix(const string& label, T* mtx);

};


}
} // namespace core

#include "GUI.tpp"

Expand Down
25 changes: 22 additions & 3 deletions include/core/GUI/GUI.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace core {

template<typename T>
bool GUI::Input(const string& label, T* value) {
bool updated;
bool updated = false;
ImGui::PushID(value);

if constexpr(std::is_same<int, T>::value) { updated = ImGui::InputInt(label.c_str(), value); }
else if constexpr(std::is_same<float, T>::value) { updated = ImGui::InputFloat(label.c_str(), value); }
else if constexpr(std::is_same<double, T>::value) { updated = ImGui::InputDouble(label.c_str(), value); }
else if constexpr(std::is_same<float, T>::value) { updated = ImGui::InputFloat(label.c_str(), value, 0.1f, 0.1f); }
else if constexpr(std::is_same<double, T>::value) { updated = ImGui::InputDouble(label.c_str(), value, 0.1f, 0.1f); }
else if constexpr(std::is_same<string, T>::value) { updated = ImGui::InputText(label.c_str(), value); }
else if constexpr(std::is_same<Vector4, T>::value) { updated = ImGui::InputFloat4(label.c_str(), value->data()); }
else if constexpr(std::is_same<Vector3, T>::value) { updated = ImGui::InputFloat3(label.c_str(), value->data()); }
Expand All @@ -22,6 +22,25 @@ bool GUI::Input(const string& label, T* value) {
else if constexpr(std::is_same<Vector2i, T>::value) { updated = ImGui::InputInt2(label.c_str(), value->data()); }
else if constexpr(std::is_same<Matrix4, T>::value) { updated = GUI::InputMatrix(label, value); }
else if constexpr(std::is_same<Matrix3, T>::value) { updated = GUI::InputMatrix(label, value); }
else if constexpr(std::is_same<Color3, T>::value) { updated = ImGui::ColorEdit3(label.c_str(), value->data()); }
else if constexpr(std::is_same<Color4, T>::value) { updated = ImGui::ColorEdit4(label.c_str(), value->data(), ImGuiColorEditFlags_AlphaBar); }

ImGui::PopID();
return updated;
}
template<typename T>
bool GUI::Slider(const string &label, T* value, float min, float max) {
bool updated = false;
ImGui::PushID(value);

if constexpr(std::is_same<int, T>::value) { updated = ImGui::SliderInt(label.c_str(), value, min, max); }
else if constexpr(std::is_same<float, T>::value) { updated = ImGui::SliderFloat(label.c_str(), value, min, max); }
else if constexpr(std::is_same<Vector4, T>::value) { updated = ImGui::SliderFloat4(label.c_str(), value->data(), min, max); }
else if constexpr(std::is_same<Vector3, T>::value) { updated = ImGui::SliderFloat3(label.c_str(), value->data(), min, max); }
else if constexpr(std::is_same<Vector2, T>::value) { updated = ImGui::SliderFloat2(label.c_str(), value->data(), min, max); }
else if constexpr(std::is_same<Vector4i, T>::value) { updated = ImGui::SliderInt4(label.c_str(), value->data(), min, max); }
else if constexpr(std::is_same<Vector3i, T>::value) { updated = ImGui::SliderInt3(label.c_str(), value->data(), min, max); }
else if constexpr(std::is_same<Vector2i, T>::value) { updated = ImGui::SliderInt2(label.c_str(), value->data(), min, max); }

ImGui::PopID();
return updated;
Expand Down
4 changes: 2 additions & 2 deletions include/core/GUI/GUIModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class GUIModule : public IModule {
void SetStandardStyle();
void SetStandardFont(float ratio);
void SubscribeToEvents();
void SetDefaultConfig();

GUIContext gui { NoCreate };
GUIContext gui { NoCreate };
ApplicationModule* appModule;
InputModule* inputModule;

void SetDefaultConfig();
};

}
Expand Down
Binary file added lib/other/DejaVuSans-ExtraLight.ttf
Binary file not shown.
Binary file removed lib/other/DejaVuSans.ttf
Binary file not shown.
Binary file added lib/other/DejaVuSansMono.ttf
Binary file not shown.
41 changes: 33 additions & 8 deletions src/GUI/GUI.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
#include <core/GUI/GUI.h>
#include <Magnum/ImGuiIntegration/Integration.h>
#include <misc/cpp/imgui_stdlib.h>
#include <limits>

namespace core {

bool GUI::Button(const string &label, Vector2 size) {
// TODO: PushID
bool GUI::Button(const string& label, Vector2 size) {
return ImGui::Button(label.c_str(), ImVec2(size));
}
bool GUI::CheckBox(const string& label, bool* value) {
// TODO: PushID
return ImGui::Checkbox(label.c_str(), value);
bool GUI::CheckBox(const string& label, bool& value) {
ImGui::PushID(value);
bool out = ImGui::Checkbox(label.c_str(), &value);
ImGui::PopID();
return out;
}
void GUI::Text(const string &text) {
// TODO: PushID
ImGui::Text(text.c_str());
void GUI::Text(const string& text) {
return ImGui::Text("%s", text.c_str());
}
bool GUI::IsHovered() {
return ImGui::IsItemHovered();
}
bool GUI::IsActive() {
return ImGui::IsItemActive();
}
bool GUI::IsFocused() {
return ImGui::IsItemFocused();
}
void GUI::Tooltip(const string& text) {
ImGui::SetTooltip("%s", text.c_str());
}
void GUI::TextLabel(const string& text, const string& label) {
ImGui::LabelText(label.c_str(), text.c_str());
}
bool GUI::Box(bool& isOpen) {
ImGui::PushID(isOpen);
bool out = ImGui::BeginChild("Box", { 0, 0 }, false, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse );
return out;
}
void GUI::EndBox() {
ImGui::EndChild();
ImGui::PopID();
}

}
6 changes: 3 additions & 3 deletions src/GUI/GUIContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ void GUIContext::DrawFrame() {
drawData->ScaleClipRects(io.DisplayFramebufferScale);

const Matrix3 projection =
Matrix3::translation({-1.0f, 1.0f})
* Matrix3::scaling({2.0f/Vector2(io.DisplaySize)})
* Matrix3::scaling({1.0f, -1.0f});
Matrix3::translation( { -1.0f, 1.0f } )
* Matrix3::scaling( { 2.0f / Vector2(io.DisplaySize) } )
* Matrix3::scaling( { 1.0f, -1.0f } );
shader.setTransformationProjectionMatrix(projection);

for (std::int_fast32_t n = 0; n < drawData->CmdListsCount; ++n) {
Expand Down
23 changes: 17 additions & 6 deletions src/GUI/GUIModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ void GUIModule::Start() {
const Vector2i frameBufferSize = appModule->GetFrameBufferSize(0);
const float supersamplingRatio = frameBufferSize.x() / windowSize.x();

gui = GUIContext(Vector2(windowSize) / dpiScale, windowSize, frameBufferSize);
ImGui::CreateContext();
SetStandardFont(supersamplingRatio); // Order is important here, ImGui Context, Fonts, GUIContext
gui = GUIContext(*ImGui::GetCurrentContext(), Vector2(windowSize) / dpiScale, windowSize, frameBufferSize);

SubscribeToEvents();
SetStandardFont(supersamplingRatio);
SetDefaultConfig();
SetStandardStyle();
}
Expand Down Expand Up @@ -58,10 +60,19 @@ void GUIModule::DrawGUI() {
GL::Renderer::enable(GL::Renderer::Feature::FaceCulling);
}
void GUIModule::SetStandardFont(const float ratio) {
// FIXME: ummm? It just doesnt work for some bizarre reason!
// ImGui::GetIO().Fonts->Clear();
// ImGui::GetIO().Fonts->AddFontFromFileTTF("DejaVuSans.ttf", 16.0f*ratio);
// ImGui::GetIO().Fonts->Build();
string filePath = __FILE__;
string dirPath = filePath.substr(0, filePath.rfind("\\"));
string fontsPath = dirPath + "/../../lib/other/";

ImFontConfig thinFontConfig;
thinFontConfig.OversampleH = 3;
thinFontConfig.OversampleV = 1;
thinFontConfig.RasterizerMultiply = 2.0f;

ImGui::GetIO().Fonts->Clear();
ImGui::GetIO().Fonts->AddFontFromFileTTF((fontsPath + "DejaVuSans-ExtraLight.ttf").c_str(), 14.0f * ratio, &thinFontConfig);
ImGui::GetIO().Fonts->AddFontFromFileTTF((fontsPath + "DejaVuSansMono.ttf").c_str(), 14.0f * ratio);
ImGui::GetIO().Fonts->Build();
}
void GUIModule::SetStandardStyle() {
const int rounding = 4;
Expand Down

0 comments on commit f135e57

Please sign in to comment.