mirror of
https://github.com/optiscaler/OptiScaler.git
synced 2026-11-22 13:47:11 +00:00
* Plugin API: ComboEx and ItemDeactivatedAfterEdit Two widget functions appended to the end of OptiPluginApi. The API version stays 1 and no field moves. - ComboEx: Combo with greyed out items, bit i of disabledItems disables items[i]. Lets a plugin keep an option in the list while it's not available instead of hiding it. Combo now calls it with no items disabled. - ItemDeactivatedAfterEdit: true on the frame the previous widget stopped being edited, a slider was released for example, so a plugin can apply a value once instead of on every frame of a drag. The sample uses both: Ultra quality is greyed out below 8 samples and the strength is logged once when its slider is released. A plain Combo picks the counter's log level, so the sample still calls every function. * Plugin API: 64 bit ComboEx mask, sample steps Ultra down ComboEx takes a uint64_t disabledItemsMask, so the first 64 items can be greyed out. In the sample, lowering Samples below 8 while Ultra is selected moves Quality to High.
247 lines
7.3 KiB
C++
247 lines
7.3 KiB
C++
// Sample OptiScaler plugin, uses every part of plugin_sdk/optiscaler_plugin.h
|
|
//
|
|
// Open SamplePlugin.vcxproj in Visual Studio, select Release | x64 and build it. Copy
|
|
// bin/Release/SamplePlugin.asi to the plugins folder next to OptiScaler and set LoadAsiPlugins=true
|
|
// in OptiScaler.ini. Two tabs show up under PLUGINS in the menu and
|
|
// the settings are kept in the [Plugin.SamplePlugin] section of OptiScaler.ini.
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <cstdio>
|
|
|
|
#include "../optiscaler_plugin.h"
|
|
|
|
namespace
|
|
{
|
|
const OptiPluginApi* api = nullptr;
|
|
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;
|
|
float strength = 0.5f;
|
|
int32_t samples = 4;
|
|
int32_t quality = 2;
|
|
char label[64] = "Hello from the sample plugin";
|
|
float tint[4] = { 0.3f, 0.7f, 1.0f, 1.0f };
|
|
};
|
|
|
|
Settings settings;
|
|
bool unsaved = false;
|
|
int32_t clickCount = 0;
|
|
int32_t clickLogLevel = OptiLog_Debug;
|
|
|
|
void LoadSettings()
|
|
{
|
|
const Settings defaults;
|
|
|
|
settings.enabled = api->GetBool(self, "Enabled", defaults.enabled);
|
|
settings.strength = api->GetFloat(self, "Strength", defaults.strength);
|
|
settings.samples = api->GetInt(self, "Samples", defaults.samples);
|
|
settings.quality = api->GetInt(self, "Quality", defaults.quality);
|
|
api->GetString(self, "Label", settings.label, sizeof(settings.label), defaults.label);
|
|
|
|
// Colors don't have their own type, they are kept as separate floats
|
|
const char* channelKeys[] = { "TintR", "TintG", "TintB", "TintA" };
|
|
for (int i = 0; i < 4; i++)
|
|
settings.tint[i] = api->GetFloat(self, channelKeys[i], defaults.tint[i]);
|
|
|
|
unsaved = false;
|
|
}
|
|
|
|
void StoreTint()
|
|
{
|
|
const char* channelKeys[] = { "TintR", "TintG", "TintB", "TintA" };
|
|
for (int i = 0; i < 4; i++)
|
|
api->SetFloat(self, channelKeys[i], settings.tint[i]);
|
|
}
|
|
|
|
void ResetSettings()
|
|
{
|
|
const char* keys[] = { "Enabled", "Strength", "Samples", "Quality", "Label", "TintR", "TintG", "TintB", "TintA" };
|
|
for (const char* key : keys)
|
|
api->ResetValue(self, key);
|
|
|
|
LoadSettings();
|
|
unsaved = true;
|
|
}
|
|
|
|
void DrawMainTab(void* /*userData*/)
|
|
{
|
|
api->Section("General");
|
|
|
|
// Set* only changes the value in memory, Save writes the file
|
|
if (api->Checkbox("Enabled", &settings.enabled))
|
|
{
|
|
api->SetBool(self, "Enabled", settings.enabled);
|
|
unsaved = true;
|
|
}
|
|
api->ItemTooltip("Turns the whole effect on or off");
|
|
|
|
// Everything below can't be used while the effect is off
|
|
api->Disabled(!settings.enabled);
|
|
|
|
if (api->SliderFloat("Strength", &settings.strength, 0.0f, 1.0f, "%.2f"))
|
|
{
|
|
api->SetFloat(self, "Strength", settings.strength);
|
|
unsaved = true;
|
|
}
|
|
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;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
api->Section("Appearance");
|
|
|
|
if (api->InputText("Label", settings.label, sizeof(settings.label)))
|
|
{
|
|
api->SetString(self, "Label", settings.label);
|
|
unsaved = true;
|
|
}
|
|
|
|
if (api->ColorEdit("Tint", settings.tint, true))
|
|
{
|
|
StoreTint();
|
|
unsaved = true;
|
|
}
|
|
|
|
api->TextColored(settings.tint[0], settings.tint[1], settings.tint[2], settings.tint[3], settings.label);
|
|
|
|
api->Disabled(false);
|
|
|
|
api->Section("Settings");
|
|
|
|
if (unsaved)
|
|
api->TextColored(1.0f, 0.8f, 0.0f, 1.0f, "There are unsaved changes");
|
|
else
|
|
api->TextDisabled("Everything is saved");
|
|
|
|
api->Spacing();
|
|
|
|
if (api->Button("Save"))
|
|
{
|
|
if (api->SaveSettings(self))
|
|
{
|
|
unsaved = false;
|
|
api->Log(self, OptiLog_Info, "Settings saved");
|
|
}
|
|
else
|
|
{
|
|
api->Log(self, OptiLog_Error, "Couldn't save the settings");
|
|
}
|
|
}
|
|
api->ItemTooltip("Writes OptiScaler.ini, the Save INI button of the menu does the same");
|
|
|
|
api->SameLine();
|
|
|
|
if (api->Button("Reload"))
|
|
LoadSettings();
|
|
api->ItemTooltip("Throws away the unsaved changes");
|
|
|
|
api->SameLine();
|
|
|
|
if (api->Button("Defaults"))
|
|
ResetSettings();
|
|
api->ItemTooltip("Removes the keys from the ini, save to make it permanent");
|
|
}
|
|
|
|
void DrawInfoTab(void* userData)
|
|
{
|
|
auto counter = static_cast<int32_t*>(userData);
|
|
|
|
api->Section("Plugin API");
|
|
|
|
char text[128];
|
|
snprintf(text, sizeof(text), "OptiScaler API version %u, table size %u bytes", api->version, api->size);
|
|
api->Text(text);
|
|
|
|
snprintf(text, sizeof(text), "Built against version %d, table size %zu bytes", OPTI_PLUGIN_API_VERSION,
|
|
sizeof(OptiPluginApi));
|
|
api->TextDisabled(text);
|
|
|
|
api->Separator();
|
|
|
|
snprintf(text, sizeof(text), "Menu scale: %.2f, menu open: %s", api->GetMenuScale(),
|
|
api->IsMenuOpen() ? "yes" : "no");
|
|
api->Text(text);
|
|
|
|
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, static_cast<OptiLogLevel>(clickLogLevel), text);
|
|
}
|
|
|
|
api->SameLine();
|
|
|
|
snprintf(text, sizeof(text), "Clicked %d times", *counter);
|
|
api->Text(text);
|
|
}
|
|
} // namespace
|
|
|
|
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, ItemDeactivatedAfterEdit))
|
|
return false;
|
|
|
|
api = optiApi;
|
|
self = plugin;
|
|
|
|
LoadSettings();
|
|
|
|
api->RegisterTab(self, "Sample", "Settings of the sample plugin", DrawMainTab, nullptr);
|
|
api->RegisterTab(self, "Sample Info", "What the plugin API reports", DrawInfoTab, &clickCount);
|
|
|
|
api->Log(self, OptiLog_Info, "Sample plugin initialized");
|
|
|
|
return true;
|
|
}
|
|
|
|
BOOL APIENTRY DllMain(HMODULE, DWORD, LPVOID) { return TRUE; }
|