Skip to content

Commit

Permalink
refactor: Config are now accessible via fw|ndbl::g_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
berdal84 committed May 22, 2024
1 parent 8008950 commit f698289
Show file tree
Hide file tree
Showing 28 changed files with 380 additions and 301 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ add_library(
src/fw/gui/App.h
src/fw/gui/AppView.cpp
src/fw/gui/AppView.h
src/fw/gui/Config.cpp
src/fw/gui/gui.cpp
src/fw/gui/Config.h
src/fw/gui/Event.h
src/fw/gui/EventManager.cpp
Expand Down Expand Up @@ -424,6 +424,8 @@ configure_file(
add_library(
nodable-gui
STATIC
src/nodable/gui/gui.cpp
src/nodable/gui/gui.h
src/nodable/gui/Config.cpp
src/nodable/gui/Config.h
src/nodable/gui/GraphView.cpp
Expand Down
6 changes: 3 additions & 3 deletions src/fw/gui-example/AppExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "AppExampleView.h"
#include "fw/gui/App.h"
#include "fw/gui/Config.h"
#include "fw/gui/gui.h"

namespace fw
{
Expand All @@ -24,15 +25,15 @@ namespace fw
{
if ( ImGui::BeginMenu( "File" ) )
{
if ( ImGui::MenuItem( "Show splashscreen" ) ) g_conf().splashscreen = true;
if ( ImGui::MenuItem( "Show splashscreen" ) ) g_conf->splashscreen = true;
if ( ImGui::MenuItem( "Quit" ) ) should_stop = true;
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}

// do not draw windows when splashscreen is visible
if ( g_conf().splashscreen )
if ( g_conf->splashscreen )
{
return;
};
Expand All @@ -55,7 +56,6 @@ namespace fw
}
ImGui::End();
}

bool on_init() override
{
LOG_MESSAGE( "Example", "My ON_INIT log!\n" );
Expand Down
2 changes: 1 addition & 1 deletion src/fw/gui-example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using namespace fw;
int main(int argc, char *argv[])
{
// Override config
fw::g_conf().app_window_label = "framework-example - (based on framework-gui library)";
fw::g_conf->app_window_label = "framework-example - (based on framework-gui library)";

// Instantiate the application using the predefined configuration
AppExample app;
Expand Down
57 changes: 32 additions & 25 deletions src/fw/gui/App.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "App.h"

#include <imgui/backends/imgui_impl_opengl3.h>
#include <imgui/backends/imgui_impl_sdl.h>
#include <lodepng/lodepng.h>
#include <nfd.h>
#include <gl3w.h>

#include "fw/core/system.h"
#include "AppView.h"
#include "Config.h"
#include "ImGuiEx.h"
#include "fw/core/system.h"
#include "gl3w/GL/gl3w.h"
#include "imgui/backends/imgui_impl_opengl3.h"
#include "imgui/backends/imgui_impl_sdl.h"
#include "lodepng/lodepng.h"
#include "nativefiledialog-extended/src/include/nfd.h"
#include "gui.h"

using namespace fw;

Expand Down Expand Up @@ -40,6 +41,7 @@ App::~App()

bool App::init()
{
before_init();
LOG_VERBOSE("fw::App", "init ...\n");

// Setup SDL
Expand All @@ -60,7 +62,7 @@ bool App::init()
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(0, &current);
m_sdl_window = SDL_CreateWindow( g_conf().app_window_label.c_str(),
m_sdl_window = SDL_CreateWindow( g_conf->app_window_label.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
Expand All @@ -72,7 +74,7 @@ bool App::init()
);

m_sdl_gl_context = SDL_GL_CreateContext(m_sdl_window);
SDL_GL_SetSwapInterval( g_conf().vsync ? 1 : 0);
SDL_GL_SetSwapInterval( g_conf->vsync ? 1 : 0);

LOG_VERBOSE("fw::App", "gl3w init ...\n");
gl3wInit();
Expand Down Expand Up @@ -120,9 +122,9 @@ bool App::init()
colors[ImGuiCol_CheckMark] = Vec4(0.31f, 0.23f, 0.14f, 1.00f);
colors[ImGuiCol_SliderGrab] = Vec4(0.71f, 0.46f, 0.22f, 0.63f);
colors[ImGuiCol_SliderGrabActive] = Vec4(0.71f, 0.46f, 0.22f, 1.00f);
colors[ImGuiCol_Button] = (ImVec4) g_conf().button_color;
colors[ImGuiCol_ButtonHovered] = (ImVec4) g_conf().button_hoveredColor;
colors[ImGuiCol_ButtonActive] = (ImVec4) g_conf().button_activeColor;
colors[ImGuiCol_Button] = (ImVec4) g_conf->button_color;
colors[ImGuiCol_ButtonHovered] = (ImVec4) g_conf->button_hoveredColor;
colors[ImGuiCol_ButtonActive] = (ImVec4) g_conf->button_activeColor;
colors[ImGuiCol_Header] = Vec4(0.70f, 0.70f, 0.70f, 1.00f);
colors[ImGuiCol_HeaderHovered] = Vec4(0.89f, 0.65f, 0.11f, 0.96f);
colors[ImGuiCol_HeaderActive] = Vec4(1.00f, 1.00f, 1.00f, 1.00f);
Expand Down Expand Up @@ -155,14 +157,14 @@ bool App::init()
colors[ImGuiCol_TableRowBg] = Vec4(0.20f, 0.20f, 0.20f, 0.40f);
colors[ImGuiCol_TableRowBgAlt] = Vec4(0.20f, 0.20f, 0.20f, 0.20f);

style.WindowBorderSize = g_conf().border_size;
style.FrameBorderSize = g_conf().border_size;
style.FrameRounding = g_conf().frame_rounding;
style.ChildRounding = g_conf().frame_rounding;
style.WindowRounding = g_conf().window_rounding;
style.AntiAliasedFill = g_conf().antialiased;
style.AntiAliasedLines = g_conf().antialiased;
style.WindowPadding = g_conf().padding;
style.WindowBorderSize = g_conf->border_size;
style.FrameBorderSize = g_conf->border_size;
style.FrameRounding = g_conf->frame_rounding;
style.ChildRounding = g_conf->frame_rounding;
style.WindowRounding = g_conf->window_rounding;
style.AntiAliasedFill = g_conf->antialiased;
style.AntiAliasedLines = g_conf->antialiased;
style.WindowPadding = g_conf->padding;

//style.ScaleAllSizes(1.25f);

Expand Down Expand Up @@ -251,7 +253,7 @@ void App::draw()
SDL_GL_MakeCurrent(m_sdl_window, m_sdl_gl_context);
ImGuiIO& io = ImGui::GetIO();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
Vec4& color = g_conf().background_color.value;
Vec4& color = g_conf->background_color.value;
glClearColor( color.x, color.y, color.z, color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
Expand Down Expand Up @@ -395,20 +397,20 @@ int App::main(int argc, char *argv[])
u32_t frame_time = SDL_GetTicks() - frame_start;

// limit frame rate
if ( g_conf().delta_time_limit && frame_time < g_conf().delta_time_min )
if ( g_conf->delta_time_limit && frame_time < g_conf->delta_time_min )
{
SDL_Delay( g_conf().delta_time_min - frame_time );
SDL_Delay( g_conf->delta_time_min - frame_time );
}

if( g_conf().show_fps)
if( g_conf->show_fps)
{
static u32_t dt = 1000 / 60;
u32_t all_time = SDL_GetTicks() - frame_start;
if( all_time <= 0 ) all_time = 1;
dt = u32_t(0.9f*float(dt) + 0.1f*float(all_time)); // Smooth value
u32_t fps = 1000 / dt;
char title[256];
snprintf( title, 256, "%s | %i fps (dt %d ms, frame %d ms)", g_conf().app_window_label.c_str(), fps, dt, frame_time );
snprintf( title, 256, "%s | %i fps (dt %d ms, frame %d ms)", g_conf->app_window_label.c_str(), fps, dt, frame_time );
title[255] = '\0';
SDL_SetWindowTitle( m_sdl_window, title );
}
Expand All @@ -428,3 +430,8 @@ int App::fps()
{
return (int)ImGui::GetIO().Framerate;
}

void App::show_splashscreen( bool b )
{
g_conf->splashscreen = b;
}
7 changes: 4 additions & 3 deletions src/fw/gui/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ namespace fw

// virtual methods user can override

virtual bool on_init() { return true; };
virtual bool on_shutdown() { return true; };
virtual void before_init() {};
virtual bool on_init() = 0;
virtual bool on_shutdown() = 0;
virtual void on_update() {};
virtual void on_draw() {};

Expand All @@ -55,7 +56,7 @@ namespace fw
bool is_fullscreen() const;
void set_fullscreen(bool b);
void save_screenshot(const char*); // Save current view as PNG file to a given path (relative or absolute)

void show_splashscreen(bool b);
static int fps(); // get the current frame per second (un-smoothed)
static std::filesystem::path asset_path(const std::filesystem::path&); // get asset's absolute path (relative path will be converted)
static std::filesystem::path asset_path(const char*); // get asset's absolute path (relative path will be converted)
Expand Down
21 changes: 11 additions & 10 deletions src/fw/gui/AppView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Config.h"
#include "EventManager.h"
#include "TextureManager.h"
#include "gui.h"

using namespace fw;

Expand Down Expand Up @@ -38,10 +39,10 @@ bool AppView::draw()

// Show/Hide ImGui Demo Window
{
if ( g_conf().imgui_demo)
if ( g_conf->imgui_demo)
{
ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
ImGui::ShowDemoWindow(&g_conf().imgui_demo);
ImGui::ShowDemoWindow(&g_conf->imgui_demo);
}
}

Expand Down Expand Up @@ -94,12 +95,12 @@ bool AppView::draw()
ImGui::DockBuilderSetNodeSize(m_dockspaces[Dockspace_ROOT] , viewport_size);

ImGui::DockBuilderSplitNode(m_dockspaces[Dockspace_ROOT] , ImGuiDir_Down , 0.5f, &m_dockspaces[Dockspace_BOTTOM], &m_dockspaces[Dockspace_CENTER]);
ImGui::DockBuilderSetNodeSize(m_dockspaces[Dockspace_BOTTOM] , ImVec2(viewport_size.x, g_conf().dockspace_bottom_size));
ImGui::DockBuilderSetNodeSize(m_dockspaces[Dockspace_BOTTOM] , ImVec2(viewport_size.x, g_conf->dockspace_bottom_size));

ImGui::DockBuilderSplitNode(m_dockspaces[Dockspace_CENTER] , ImGuiDir_Up , 0.5f, &m_dockspaces[Dockspace_TOP], &m_dockspaces[Dockspace_CENTER]);
ImGui::DockBuilderSetNodeSize(m_dockspaces[Dockspace_TOP] , ImVec2(viewport_size.x, g_conf().dockspace_top_size));
ImGui::DockBuilderSetNodeSize(m_dockspaces[Dockspace_TOP] , ImVec2(viewport_size.x, g_conf->dockspace_top_size));

ImGui::DockBuilderSplitNode(m_dockspaces[Dockspace_CENTER] , ImGuiDir_Right, g_conf().dockspace_right_ratio, &m_dockspaces[Dockspace_RIGHT], &m_dockspaces[Dockspace_CENTER]);
ImGui::DockBuilderSplitNode(m_dockspaces[Dockspace_CENTER] , ImGuiDir_Right, g_conf->dockspace_right_ratio, &m_dockspaces[Dockspace_RIGHT], &m_dockspaces[Dockspace_CENTER]);

// Configure dockspaces
ImGui::DockBuilderGetNode(m_dockspaces[Dockspace_CENTER])->HasCloseButton = false;
Expand Down Expand Up @@ -195,17 +196,17 @@ void AppView::draw_splashscreen()

bool AppView::begin_splashscreen()
{
if ( g_conf().splashscreen && !ImGui::IsPopupOpen( g_conf().splashscreen_window_label))
if ( g_conf->splashscreen && !ImGui::IsPopupOpen( g_conf->splashscreen_window_label))
{
ImGui::OpenPopup( g_conf().splashscreen_window_label);
ImGui::OpenPopup( g_conf->splashscreen_window_label);
}

ImGui::SetNextWindowSizeConstraints(ImVec2(550, 300), ImVec2(550, 50000));
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), 0, ImVec2(0.5f, 0.5f));

auto flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize;

return ImGui::BeginPopupModal( g_conf().splashscreen_window_label, &g_conf().splashscreen, flags);
return ImGui::BeginPopupModal( g_conf->splashscreen_window_label, &g_conf->splashscreen, flags);
}

void AppView::end_splashscreen()
Expand All @@ -220,11 +221,11 @@ void AppView::draw_status_window()
if (!log::get_messages().empty())
{
const std::deque<log::Message> &messages = log::get_messages();
auto it = messages.rend() - std::min( g_conf().log_tooltip_max_count, messages.size());
auto it = messages.rend() - std::min( g_conf->log_tooltip_max_count, messages.size());
while (it != messages.rend())
{
auto &each_message = *it;
ImGui::TextColored((ImVec4) g_conf().log_color[each_message.verbosity], "%s", each_message.text.c_str());
ImGui::TextColored((ImVec4) g_conf->log_color[each_message.verbosity], "%s", each_message.text.c_str());
++it;
}

Expand Down
7 changes: 0 additions & 7 deletions src/fw/gui/Config.cpp

This file was deleted.

2 changes: 0 additions & 2 deletions src/fw/gui/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,4 @@ namespace fw
float frame_rounding {3.f};
float border_size {1.f};
};

Config& g_conf(); // Global configuration for the framework
}
19 changes: 10 additions & 9 deletions src/fw/gui/FontManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "FontManager.h"
#include "App.h"
#include "Config.h"
#include "gui.h"

using namespace fw;

Expand All @@ -9,15 +10,15 @@ FontManager::~FontManager()

void FontManager::init()
{
for (const FontConfig& each_font : g_conf().font_manager.text)
for (const FontConfig& each_font : g_conf->font_manager.text)
{
load_font(each_font);
}

// Assign text_fonts (user might want to change it later, but we need defaults)
for( int each_slot = 0; each_slot < FontSlot_COUNT; ++each_slot )
{
if(auto font = g_conf().font_manager.defaults[each_slot] )
if(auto font = g_conf->font_manager.defaults[each_slot] )
{
m_fonts[each_slot] = get_font(font);
}
Expand All @@ -44,15 +45,15 @@ ImFont* FontManager::load_font(const FontConfig& text_font)
imfont_cfg.OversampleV = 3;
std::filesystem::path absolute_path = App::asset_path(text_font.path);
LOG_VERBOSE("NodableView", "Adding text_font from file ... %s\n", absolute_path.c_str())
font = io.Fonts->AddFontFromFileTTF(absolute_path.string().c_str(), text_font.size * g_conf().font_manager.subsamples, &imfont_cfg);
font = io.Fonts->AddFontFromFileTTF(absolute_path.string().c_str(), text_font.size * g_conf->font_manager.subsamples, &imfont_cfg);
}

// Add Icons my merging to previous text_font.
if (text_font.icons_enable )
{
if(strlen( g_conf().font_manager.icon.path) == 0)
if(strlen( g_conf->font_manager.icon.path) == 0)
{
LOG_WARNING("NodableView", "g_conf().font_manager.icon.path is empty, icons will be \"?\"\n");
LOG_WARNING("NodableView", "g_conf->font_manager.icon.path is empty, icons will be \"?\"\n");
return font;
}

Expand All @@ -65,13 +66,13 @@ ImFont* FontManager::load_font(const FontConfig& text_font)
imfont_cfg.OversampleH = 2;
imfont_cfg.OversampleV = 3;
//imfont_cfg.GlyphOffset.y = -(text_font.icons_size - text_font.size)/2.f;
imfont_cfg.GlyphMinAdvanceX = text_font.icons_size * g_conf().font_manager.subsamples; // monospace to fix text alignment in drop down menus.
std::filesystem::path absolute_path = App::asset_path( g_conf().font_manager.icon.path);
font = io.Fonts->AddFontFromFileTTF(absolute_path.string().c_str(), text_font.icons_size * g_conf().font_manager.subsamples, &imfont_cfg, icons_ranges);
imfont_cfg.GlyphMinAdvanceX = text_font.icons_size * g_conf->font_manager.subsamples; // monospace to fix text alignment in drop down menus.
std::filesystem::path absolute_path = App::asset_path( g_conf->font_manager.icon.path);
font = io.Fonts->AddFontFromFileTTF(absolute_path.string().c_str(), text_font.icons_size * g_conf->font_manager.subsamples, &imfont_cfg, icons_ranges);
LOG_VERBOSE("NodableView", "Merging icons font ...\n")
}

font->Scale = 1.0f / g_conf().font_manager.subsamples;
font->Scale = 1.0f / g_conf->font_manager.subsamples;

m_loaded_fonts.insert_or_assign(text_font.id, font);
LOG_MESSAGE("NodableView", "Font %s added: \"%s\"\n", text_font.id, text_font.path )
Expand Down
20 changes: 20 additions & 0 deletions src/fw/gui/gui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "gui.h"

fw::Config* fw::g_conf{nullptr};

void fw::init()
{
FW_EXPECT(fw::g_conf == nullptr, "fw::g_conf is already initialized\n")
fw::g_conf = new Config();

Pool::init();
};

void fw::shutdown()
{
FW_EXPECT(fw::g_conf != nullptr, "fw::g_conf was not initialized, did you call fw::shutdown() twice?\n")
delete fw::g_conf;
fw::g_conf = nullptr;

Pool::shutdown();
};
9 changes: 9 additions & 0 deletions src/fw/gui/gui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include "Config.h"

namespace fw
{
extern Config* g_conf; // Globally accessible configuration. Must be initialized with fw::init() before use;
void init(); // create a new g_conf
void shutdown(); // delete current g_conf
}
Loading

0 comments on commit f698289

Please sign in to comment.