diff --git a/OptiScaler/plugins/PluginApi.cpp b/OptiScaler/plugins/PluginApi.cpp index 4cc36a00..c4b825d7 100644 --- a/OptiScaler/plugins/PluginApi.cpp +++ b/OptiScaler/plugins/PluginApi.cpp @@ -268,7 +268,8 @@ bool Api_SliderInt(const char* label, int32_t* value, int32_t min, int32_t max, return ImGui::SliderInt(label, value, min, max, format != nullptr ? format : "%d", ImGuiSliderFlags_AlwaysClamp); } -bool Api_Combo(const char* label, int32_t* current, const char* const* items, int32_t itemCount) +bool Api_ComboEx(const char* label, int32_t* current, const char* const* items, int32_t itemCount, + uint64_t disabledItemsMask) { if (_draw == nullptr || label == nullptr || current == nullptr || (items == nullptr && itemCount > 0)) return false; @@ -283,6 +284,9 @@ bool Api_Combo(const char* label, int32_t* current, const char* const* items, in { ImGui::PushID(i); + const bool disabled = i < 64 && (disabledItemsMask & (1ull << i)) != 0; + ImGui::BeginDisabled(disabled); + const bool selected = *current == i; if (ImGui::Selectable(items[i] != nullptr ? items[i] : "", selected) && !selected) { @@ -293,6 +297,7 @@ bool Api_Combo(const char* label, int32_t* current, const char* const* items, in if (selected) ImGui::SetItemDefaultFocus(); + ImGui::EndDisabled(); ImGui::PopID(); } @@ -302,6 +307,13 @@ bool Api_Combo(const char* label, int32_t* current, const char* const* items, in return changed; } +bool Api_Combo(const char* label, int32_t* current, const char* const* items, int32_t itemCount) +{ + return Api_ComboEx(label, current, items, itemCount, 0); +} + +bool Api_ItemDeactivatedAfterEdit() { return _draw != nullptr && ImGui::IsItemDeactivatedAfterEdit(); } + bool Api_InputText(const char* label, char* buffer, size_t bufferSize) { if (_draw == nullptr || label == nullptr || buffer == nullptr || bufferSize == 0) @@ -463,6 +475,9 @@ const OptiPluginApi _api = { .SaveSettings = Api_SaveSettings, .Log = Api_Log, + + .ComboEx = Api_ComboEx, + .ItemDeactivatedAfterEdit = Api_ItemDeactivatedAfterEdit, }; } // namespace diff --git a/plugin_sdk/README.md b/plugin_sdk/README.md index 576989e9..bc12e08c 100644 --- a/plugin_sdk/README.md +++ b/plugin_sdk/README.md @@ -23,9 +23,10 @@ ASI plugins loaded by OptiScaler can add their own tabs to the OptiScaler menu a ## Drawing -Inside a draw callback you can use `Section`, `Text`, `Checkbox`, `SliderFloat`/`SliderInt`, `Combo`, `InputText`, `ColorEdit`, `Button`, `SameLine`, `Disabled` and `ItemTooltip`. +Inside a draw callback you can use `Section`, `Text`, `Checkbox`, `SliderFloat`/`SliderInt`, `Combo`/`ComboEx`, `InputText`, `ColorEdit`, `Button`, `SameLine`, `Disabled`, `ItemTooltip` and `ItemDeactivatedAfterEdit`. - `Section` starts a card like the ones in OptiScaler's own tabs. +- `ComboEx` is a combo whose items can be greyed out. `ItemDeactivatedAfterEdit` is true on the frame a slider is released, for values that should apply once instead of on every frame of a drag. - Cards, disabled blocks and IDs are closed by OptiScaler after the callback, so a plugin can't leave ImGui in a broken state. - Widget functions do nothing when called outside a draw callback or from another thread. - Draw callbacks run on the render thread while the menu is open. Keep them fast, don't call D3D/Vulkan from them, and protect any data you share with your own threads. diff --git a/plugin_sdk/optiscaler_plugin.h b/plugin_sdk/optiscaler_plugin.h index f46b3a1a..4460bf70 100644 --- a/plugin_sdk/optiscaler_plugin.h +++ b/plugin_sdk/optiscaler_plugin.h @@ -136,6 +136,16 @@ extern "C" // Writes to the OptiScaler log, prefixed with the plugin name void (*Log)(OptiPlugin* plugin, OptiLogLevel level, const char* message); + // More widgets, only inside a draw callback --------------------------------------------------------- + + // Combo with greyed out items, bit i of disabledItemsMask disables items[i] (the first 64 items). + // The selected item is shown even when it's disabled. + bool (*ComboEx)(const char* label, int32_t* current, const char* const* items, int32_t itemCount, + uint64_t disabledItemsMask); + // True on the frame the previous widget stopped being edited, a slider was released for example. + // Lets a plugin apply a value once instead of on every frame of a drag. + bool (*ItemDeactivatedAfterEdit)(void); + } OptiPluginApi; // True when the running OptiScaler has the given field of OptiPluginApi diff --git a/plugin_sdk/sample/SamplePlugin.cpp b/plugin_sdk/sample/SamplePlugin.cpp index a039d1d2..575d9f67 100644 --- a/plugin_sdk/sample/SamplePlugin.cpp +++ b/plugin_sdk/sample/SamplePlugin.cpp @@ -19,6 +19,13 @@ OptiPlugin* self = nullptr; const char* const qualityNames[] = { "Low", "Medium", "High", "Ultra" }; constexpr int32_t qualityCount = sizeof(qualityNames) / sizeof(qualityNames[0]); +// Ultra needs at least this many samples +constexpr int32_t ultraQuality = 3; +constexpr int32_t ultraMinSamples = 8; + +const char* const logLevelNames[] = { "Debug", "Info", "Warn", "Error" }; +constexpr int32_t logLevelCount = sizeof(logLevelNames) / sizeof(logLevelNames[0]); + struct Settings { bool enabled = true; @@ -32,6 +39,7 @@ struct Settings Settings settings; bool unsaved = false; int32_t clickCount = 0; +int32_t clickLogLevel = OptiLog_Debug; void LoadSettings() { @@ -90,13 +98,32 @@ void DrawMainTab(void* /*userData*/) } api->ItemTooltip("0.0 - no effect\n1.0 - full effect"); + // Logged once when the slider is released, not on every frame of the drag + if (api->ItemDeactivatedAfterEdit()) + { + char text[64]; + snprintf(text, sizeof(text), "Strength set to %.2f", settings.strength); + api->Log(self, OptiLog_Debug, text); + } + if (api->SliderInt("Samples", &settings.samples, 1, 16, nullptr)) { api->SetInt(self, "Samples", settings.samples); + + // Too few samples for Ultra, step down to High + if (settings.samples < ultraMinSamples && settings.quality == ultraQuality) + { + settings.quality = ultraQuality - 1; + api->SetInt(self, "Quality", settings.quality); + } + unsaved = true; } - if (api->Combo("Quality", &settings.quality, qualityNames, qualityCount)) + // With too few samples Ultra stays in the list but can't be picked + const uint64_t disabledQualities = settings.samples < ultraMinSamples ? 1ull << ultraQuality : 0; + + if (api->ComboEx("Quality", &settings.quality, qualityNames, qualityCount, disabledQualities)) { api->SetInt(self, "Quality", settings.quality); unsaved = true; @@ -178,13 +205,16 @@ void DrawInfoTab(void* userData) api->Section("Counter"); + // Not kept in the ini, only for this session + api->Combo("Log clicks as", &clickLogLevel, logLevelNames, logLevelCount); + // userData given to RegisterTab comes back here if (api->Button("Click me")) { (*counter)++; snprintf(text, sizeof(text), "Button clicked %d times", *counter); - api->Log(self, OptiLog_Debug, text); + api->Log(self, static_cast(clickLogLevel), text); } api->SameLine(); @@ -197,7 +227,7 @@ void DrawInfoTab(void* userData) extern "C" __declspec(dllexport) bool OptiPluginInit(const OptiPluginApi* optiApi, OptiPlugin* plugin) { // An older OptiScaler doesn't have everything this plugin uses - if (!OPTI_API_HAS(optiApi, Log)) + if (!OPTI_API_HAS(optiApi, ItemDeactivatedAfterEdit)) return false; api = optiApi;