ImGui: Improve integration, use scalable font, Control Options menu

This commit is contained in:
Daniel Gibson 2024-04-19 12:30:37 +02:00
parent 86be2eb513
commit c9a8901dbd
8 changed files with 4426 additions and 54 deletions

View file

@ -173,5 +173,6 @@ This can be configured with the following CVars:
- `r_useCarmacksReverse` Use Z-Fail ("Carmack's Reverse") when rendering shadows (default `1`)
- `r_useStencilOpSeparate` Use glStencilOpSeparate() (if available) when rendering shadow (default `1`)
- `r_scaleMenusTo43` Render full-screen menus in 4:3 by adding black bars on the left/right if necessary (default `1`)
- `s_alReverbGain` reduce strength of OpenAL (EAX-like) EFX reverb effects, `0.0` - `1.0` (default `0.5`)

View file

@ -71,6 +71,10 @@ if(NOT MSVC) # GCC/clang or compatible, hopefully
endif()
endif()
# we need C++11 for ImGui
# TODO: if we allow disabling ImGui, we could disable setting the c++ standard - or maybe just embrace C++11?
set (CMAKE_CXX_STANDARD 11)
if(NOT CMAKE_SYSTEM_PROCESSOR)
message(FATAL_ERROR "No target CPU architecture set")
endif()

View file

@ -31,6 +31,7 @@ If you have questions concerning this license or the applicable additional terms
#include "framework/Game.h"
#include "framework/DeclEntityDef.h"
#include "framework/Session.h"
/*
=================
@ -124,7 +125,8 @@ bool idDeclEntityDef::Parse( const char *text, const int textLength ) {
// precache all referenced media
// do this as long as we arent in modview
if ( !( com_editors & (EDITOR_RADIANT|EDITOR_AAS) ) ) {
// DG: ... and only if we currently have a loaded/loading map
if ( !( com_editors & (EDITOR_RADIANT|EDITOR_AAS) ) && session->GetCurrentMapName()[0] ) {
game->CacheDictionaryMedia( &dict );
}

View file

@ -1,26 +1,196 @@
#include "Common.h"
#include "CVarSystem.h"
#include "sys/sys_imgui.h"
#ifndef IMGUI_DISABLE
static bool dhewm3MenuOpen = false;
namespace {
static void AddCVarOptionTooltips( const idCVar& cvar, const char* desc = nullptr )
{
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted( cvar.GetName() );
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
ImGui::SameLine();
ImGui::TextDisabled("(?)");
if (ImGui::BeginItemTooltip())
{
ImGui::PushTextWrapPos( ImGui::GetFontSize() * 35.0f );
ImGui::TextUnformatted( desc ? desc : cvar.GetDescription() );
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
enum OptionType {
OT_NONE,
OT_HEADING, // not an option, just a heading on the page
OT_BOOL,
OT_FLOAT,
OT_INT,
OT_CUSTOM, // using a callback in Draw()
};
struct CVarOption {
typedef void (*DrawCallback)( idCVar& cvar ); // for OT_CUSTOM
const char* name = nullptr;
idCVar* cvar = nullptr;
const char* label = nullptr;
DrawCallback drawCallback = nullptr;
OptionType type = OT_NONE;
// TODO: the following two could be a union, together with drawCallback and possibly others!
float minVal = 0.0f;
float maxVal = 0.0f;
CVarOption() = default;
CVarOption(const char* _name, const char* _label, OptionType _type, float _minVal = 0.0f, float _maxVal = 0.0f)
: name(_name), label(_label), type(_type), minVal(_minVal), maxVal(_maxVal)
{}
CVarOption(const char* _name, DrawCallback drawCB)
: name(_name), drawCallback(drawCB), type(OT_CUSTOM)
{}
CVarOption(const char* headingLabel) : label(headingLabel), type(OT_HEADING)
{}
void Init()
{
if (name != NULL) {
cvar = cvarSystem->Find(name);
printf("# Init() name = %s cvar = %p\n", name, cvar);
}
else printf("# Init() name = NULL label = %s\n", label);
}
void Draw()
{
if (type == OT_HEADING) {
if (label != NULL) {
ImGui::SeparatorText(label);
}
} else if (cvar != nullptr) {
switch(type) {
case OT_BOOL:
{
bool b = cvar->GetBool();
bool bOrig = b;
ImGui::Checkbox( label, &b );
AddCVarOptionTooltips( *cvar );
if (b != bOrig) {
cvar->SetBool(b);
}
break;
}
case OT_FLOAT:
{
float f = cvar->GetFloat();
float fOrig = f;
// TODO: make format configurable?
ImGui::SliderFloat(label, &f, minVal, maxVal, "%.2f", 0);
AddCVarOptionTooltips( *cvar );
if(f != fOrig) {
cvar->SetFloat(f);
}
break;
}
case OT_INT:
{
int i = cvar->GetInteger();
int iOrig = i;
ImGui::SliderInt(label, &i, minVal, maxVal);
AddCVarOptionTooltips( *cvar );
if (i != iOrig) {
cvar->SetInteger(i);
}
break;
}
case OT_CUSTOM:
if (drawCallback != nullptr) {
drawCallback(*cvar);
}
break;
}
}
}
};
static void InitOptions(CVarOption options[], size_t numOptions)
{
for( int i=0; i<numOptions; ++i ) {
options[i].Init();
}
}
static void DrawOptions(CVarOption options[], size_t numOptions)
{
for( int i=0; i<numOptions; ++i ) {
options[i].Draw();
}
}
static CVarOption controlOptions[] = {
CVarOption("Mouse Settings"),
CVarOption("sensitivity", "Sensitivity", OT_FLOAT, 1.0f, 30.0f),
CVarOption("m_smooth", "Smoothing Samples", OT_INT, 1, 8),
CVarOption("in_nograb", "Don't grab Mouse Cursor (for debugging/testing)", OT_BOOL),
CVarOption("Keyboard Settings"),
CVarOption("in_grabKeyboard", "Grab Keyboard", OT_BOOL),
CVarOption("in_ignoreConsoleKey", "Don't open console with key between Esc, Tab and 1", OT_BOOL),
CVarOption("Gamepad Settings"),
CVarOption("in_useGamepad", "Enable Gamepad Support", OT_BOOL),
CVarOption("joy_gamepadLayout", [](idCVar& cvar) {
int sel = cvar.GetInteger() + 1; // -1 .. 3 => 0 .. 4
int selOrig = sel;
// -1: auto (needs SDL 2.0.12 or newer), 0: XBox-style, 1: Nintendo-style, 2: PS4/5-style, 3: PS2/3-style
const char* items[] = { "Auto-Detect", "XBox Controller-like",
"Nintendo-style", "Playstation 4/5 Controller-like",
"Playstation 2/3 Controller-like" };
ImGui::Combo("Gamepad Layout", &sel, items, IM_ARRAYSIZE(items));
AddCVarOptionTooltips( cvar, "Button Layout of Gamepad (esp. for the displayed names of the 4 buttons on the right)" );
if(sel != selOrig) {
cvar.SetInteger(sel-1);
}
}),
CVarOption("joy_deadZone", "Axis Deadzone", OT_FLOAT, 0.0f, 0.99f),
CVarOption("joy_triggerThreshold", "Trigger Threshold/Deadzone", OT_FLOAT, 0.0f, 0.99f),
};
// TODO: r_scaleMenusTo43
} //anon namespace
// called from D3::ImGuiHooks::NewFrame() (if this window is enabled)
void Com_DrawDhewm3SettingsMenu()
{
bool showSettingsWindow = true;
ImGui::Begin("dhewm3 Settings", &showSettingsWindow);
static float scale = 1.0f;
ImGuiIO& io = ImGui::GetIO();
ImGui::SliderFloat("ImGui scale", &scale, 0.1f, 8.0f, "%.2f");
ImGui::SameLine();
if (ImGui::Button("Reset to Default")) {
scale = D3::ImGuiHooks::GetDefaultDPI() / 96.0f;
float scale = D3::ImGuiHooks::GetScale();
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.4f);
if ( ImGui::DragFloat("ImGui scale", &scale, 0.005f, 0.25f, 8.0f, "%.3f") ) {
D3::ImGuiHooks::SetScale( scale );
}
ImGui::SameLine();
if ( ImGui::Button("Reset") ) {
D3::ImGuiHooks::SetScale( -1.0f );
}
io.FontGlobalScale = scale;
if (ImGui::Button("Show ImGui Demo")) {
D3::ImGuiHooks::OpenWindow( D3::ImGuiHooks::D3_ImGuiWin_Demo );
@ -29,48 +199,58 @@ void Com_DrawDhewm3SettingsMenu()
ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None;
if (ImGui::BeginTabBar("SettingsTabBar", tab_bar_flags))
{
if (ImGui::BeginTabItem("Avocado"))
{
ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Broccoli"))
/*if (ImGui::BeginTabItem("Control Bindings"))
{
ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}*/
if (ImGui::BeginTabItem("Control Options"))
{
DrawOptions( controlOptions, IM_ARRAYSIZE(controlOptions) );
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Cucumber"))
if (ImGui::BeginTabItem("Game Options"))
{
ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Video Options"))
{
ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Audio Options"))
{
ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah");
ImGui::EndTabItem();
}
ImGui::EndTabBar();
}
ImGui::End();
if(!showSettingsWindow) {
if (!showSettingsWindow) {
D3::ImGuiHooks::CloseWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings );
dhewm3MenuOpen = false;
}
}
void Com_InitDhewm3SettingsMenu()
{
InitOptions( controlOptions, IM_ARRAYSIZE(controlOptions) );
}
void Com_Dhewm3Settings_f( const idCmdArgs &args )
{
if ( !dhewm3MenuOpen ) {
bool menuOpen = (D3::ImGuiHooks::GetOpenWindowsMask() & D3::ImGuiHooks::D3_ImGuiWin_Settings) != 0;
if ( !menuOpen ) {
// TODO: if in SP game, pause
Com_InitDhewm3SettingsMenu();
D3::ImGuiHooks::OpenWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings );
dhewm3MenuOpen = true;
} else {
D3::ImGuiHooks::CloseWindow( D3::ImGuiHooks::D3_ImGuiWin_Settings );
// TODO: if in SP game, unpause
dhewm3MenuOpen = false;
}
}

4146
neo/sys/proggyvector_font.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -19,15 +19,19 @@ typedef char* (*MY_XGETDEFAULTFUN)(Display*, const char*, const char*);
extern void Com_DrawDhewm3SettingsMenu(); // in framework/dhewm3SettingsMenu.cpp
static idCVar imgui_scale( "imgui_scale", "-1.0", CVAR_SYSTEM|CVAR_FLOAT|CVAR_ARCHIVE, "factor to scale ImGUI menus by (-1: auto)" ); // TODO: limit values?
namespace D3 {
namespace ImGuiHooks {
#include "proggyvector_font.h"
static SDL_Window* sdlWindow = NULL;
ImGuiContext* imguiCtx = NULL;
static bool haveNewFrame = false;
static int openImguiWindows = 0; // or-ed enum D3ImGuiWindow values
float GetDefaultDPI()
static float GetDefaultDPI()
{
SDL_Window* win = sdlWindow;
float dpi = -1.0f;
@ -67,13 +71,40 @@ float GetDefaultDPI()
return dpi;
}
static float GetDefaultScale()
{
float ret = GetDefaultDPI() / 96.0f;
ret = round(ret*2.0)*0.5; // round to .0 or .5
return ret;
}
float GetScale()
{
float ret = imgui_scale.GetFloat();
if (ret < 0.0f) {
ret = GetDefaultScale();
}
return ret;
}
void SetScale( float scale )
{
ImGuiIO& io = ImGui::GetIO();
float realScale = (scale < 0.0f) ? GetDefaultScale() : scale;
io.FontGlobalScale = realScale;
// TODO: could instead set fontsize to 18.0f * scale
// (io.Fonts->ClearFonts() and then add again with new size - but must be done before NewFrame() / after EndFrame())
imgui_scale.SetFloat( scale );
}
static bool imgui_initialized = false;
// using void* instead of SDL_Window and SDL_GLContext to avoid dragging SDL headers into sys_imgui.h
bool Init(void* _sdlWindow, void* sdlGlContext)
{
common->Printf( "Initializing ImGui\n" );
SDL_Window* win = (SDL_Window*)_sdlWindow;
sdlWindow = win;
sdlWindow = (SDL_Window*)_sdlWindow;
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
@ -104,24 +135,15 @@ bool Init(void* _sdlWindow, void* sdlGlContext)
colors[ImGuiCol_TitleBg] = ImVec4(0.28f, 0.36f, 0.48f, 0.88f);
colors[ImGuiCol_TabHovered] = ImVec4(0.42f, 0.69f, 1.00f, 0.80f);
colors[ImGuiCol_TabActive] = ImVec4(0.24f, 0.51f, 0.83f, 1.00f);
#if 0
float dpi = getFontDPI(win);
printf("XXX dpi = %f\n", dpi);
if (dpi != -1.0f) {
float fontScale = dpi / 96.0f; // TODO: is 96dpi not the default anywhere? macOS maybe?
fontScale = round(fontScale*2.0)*0.5; // round to .0 or .5
io.FontGlobalScale = fontScale;
printf("fontscale: %f\n", fontScale);
ImFontConfig fontCfg;
//fontCfg.OversampleH = fontCfg.OversampleV = 1;
fontCfg.PixelSnapH = true;
fontCfg.RasterizerDensity = fontScale;
io.Fonts->AddFontDefault(&fontCfg);
}
#endif
ImFontConfig fontCfg;
strcpy(fontCfg.Name, "ProggyVector");
ImFont* font = io.Fonts->AddFontFromMemoryCompressedTTF(ProggyVector_compressed_data, ProggyVector_compressed_size, 18.0f, nullptr);
SetScale( GetScale() );
// Setup Platform/Renderer backends
if ( ! ImGui_ImplSDL2_InitForOpenGL( win, sdlGlContext ) ) {
if ( ! ImGui_ImplSDL2_InitForOpenGL( sdlWindow, sdlGlContext ) ) {
ImGui::DestroyContext( imguiCtx );
imguiCtx = NULL;
common->Warning( "Failed to initialize ImGui SDL platform backend!\n" );
@ -152,16 +174,20 @@ bool Init(void* _sdlWindow, void* sdlGlContext)
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != nullptr);
imgui_initialized = true;
return true;
}
void Shutdown()
{
common->Printf( "Shutting down ImGui\n" );
ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext( imguiCtx );
if ( imgui_initialized ) {
common->Printf( "Shutting down ImGui\n" );
// TODO: only if init was successful!
ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext( imguiCtx );
imgui_initialized = false;
}
}
// NewFrame() is called once per D3 frame, after all events have been gotten
@ -194,7 +220,7 @@ void NewFrame()
bool ProcessEvent(const void* sdlEvent)
{
if (openImguiWindows == 0)
return false;
return false;
const SDL_Event* ev = (const SDL_Event*)sdlEvent;
// ImGui_ImplSDL2_ProcessEvent() doc says:
@ -284,4 +310,9 @@ void CloseWindow( D3ImGuiWindow win )
openImguiWindows &= ~win;
}
int GetOpenWindowsMask()
{
return openImguiWindows;
}
}} //namespace D3::ImGuiHooks

View file

@ -33,6 +33,10 @@ extern void OpenWindow( D3ImGuiWindow win );
extern void CloseWindow( D3ImGuiWindow win );
// enum D3ImGuiWindow values of all currently open imgui windows or-ed together
// (0 if none are open)
extern int GetOpenWindowsMask();
// called with every SDL event by Sys_GetEvent()
// returns true if ImGui has handled the event (so it shouldn't be handled by D3)
extern bool ProcessEvent(const void* sdlEvent);
@ -45,7 +49,8 @@ extern void NewFrame();
// renders ImGui menus then
extern void EndFrame();
extern float GetDefaultDPI();
extern float GetScale();
extern void SetScale( float scale );
#else // IMGUI_DISABLE - just stub out everything
@ -72,7 +77,10 @@ inline void OpenWindow( D3ImGuiWindow win ) {}
inline void CloseWindow( D3ImGuiWindow win ) {}
inline float GetDefaultDPI() { return 96.0f; }
inline int GetOpenWindowsMask() { return 0; }
inline float GetScale() { return 1.0f; }
inline void SetScale( float scale ) {}
#endif

View file

@ -56,7 +56,7 @@ typedef enum {
typedef enum {
SE_NONE, // evTime is still valid
SE_KEY, // evValue is a key code, evValue2 is the down flag
SE_CHAR, // evValue is an ascii char
SE_CHAR, // evValue is a "High ASCII" (ISO-8859-1) char
SE_MOUSE, // evValue and evValue2 are relative signed x / y moves
SE_MOUSE_ABS, // evValue and evValue2 are absolute x / y coordinates in the window
SE_JOYSTICK, // evValue is an axis number and evValue2 is the current state (-127 to 127)