2020-11-05 16:44:34 +01:00
|
|
|
#include "pch.h"
|
|
|
|
#include "options.h"
|
2020-12-02 18:12:34 +01:00
|
|
|
|
|
|
|
#include "fullscrn.h"
|
|
|
|
#include "midi.h"
|
2022-08-31 14:18:22 +02:00
|
|
|
#include "pb.h"
|
2021-10-25 05:42:36 +02:00
|
|
|
#include "render.h"
|
2020-11-06 14:56:32 +01:00
|
|
|
#include "Sound.h"
|
2020-12-03 15:47:36 +01:00
|
|
|
#include "winmain.h"
|
2022-08-31 10:11:21 +02:00
|
|
|
#include "translations.h"
|
2020-11-05 16:44:34 +01:00
|
|
|
|
2021-10-01 08:05:38 +02:00
|
|
|
constexpr int options::MaxUps, options::MaxFps, options::MinUps, options::MinFps, options::DefUps, options::DefFps;
|
2021-10-09 16:28:30 +02:00
|
|
|
constexpr int options::MaxSoundChannels, options::MinSoundChannels, options::DefSoundChannels;
|
2022-01-12 15:17:38 +01:00
|
|
|
constexpr int options::MaxVolume, options::MinVolume, options::DefVolume;
|
2020-11-06 14:56:32 +01:00
|
|
|
|
2023-01-27 14:25:47 +01:00
|
|
|
std::unordered_map<std::string, std::string> options::settings{};
|
2021-10-01 08:05:38 +02:00
|
|
|
bool options::ShowDialog = false;
|
2021-10-17 17:18:29 +02:00
|
|
|
GameInput* options::ControlWaitingForInput = nullptr;
|
2023-01-03 14:42:34 +01:00
|
|
|
std::vector<OptionBase*> options::AllOptions{};
|
|
|
|
|
2023-01-27 14:25:47 +01:00
|
|
|
optionsStruct options::Options
|
2023-01-03 14:42:34 +01:00
|
|
|
{
|
|
|
|
{
|
2023-01-27 14:25:47 +01:00
|
|
|
{
|
|
|
|
"Left Flipper key",
|
2023-02-10 06:46:09 +01:00
|
|
|
Msg::KEYMAPPER_FlipperL,
|
2023-01-27 14:25:47 +01:00
|
|
|
{InputTypes::Keyboard, SDLK_z},
|
|
|
|
{InputTypes::Mouse, SDL_BUTTON_LEFT},
|
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_LEFTSHOULDER}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Right Flipper key",
|
2023-02-10 06:46:09 +01:00
|
|
|
Msg::KEYMAPPER_FlipperR,
|
2023-01-27 14:25:47 +01:00
|
|
|
{InputTypes::Keyboard, SDLK_SLASH},
|
|
|
|
{InputTypes::Mouse,SDL_BUTTON_RIGHT},
|
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Plunger key",
|
2023-02-10 06:46:09 +01:00
|
|
|
Msg::KEYMAPPER_Plunger,
|
2023-01-27 14:25:47 +01:00
|
|
|
{InputTypes::Keyboard, SDLK_SPACE},
|
|
|
|
{InputTypes::Mouse,SDL_BUTTON_MIDDLE},
|
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_A}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Left Table Bump key",
|
2023-02-10 06:46:09 +01:00
|
|
|
Msg::KEYMAPPER_BumpLeft,
|
2023-01-27 14:25:47 +01:00
|
|
|
{InputTypes::Keyboard, SDLK_x},
|
|
|
|
{InputTypes::Mouse,SDL_BUTTON_X1},
|
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_DPAD_LEFT}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Right Table Bump key",
|
2023-02-10 06:46:09 +01:00
|
|
|
Msg::KEYMAPPER_BumpRight,
|
2023-01-27 14:25:47 +01:00
|
|
|
{InputTypes::Keyboard, SDLK_PERIOD},
|
|
|
|
{InputTypes::Mouse,SDL_BUTTON_X2},
|
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_DPAD_RIGHT}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Bottom Table Bump key",
|
2023-02-10 06:46:09 +01:00
|
|
|
Msg::KEYMAPPER_BumpBottom,
|
2023-01-27 14:25:47 +01:00
|
|
|
{InputTypes::Keyboard, SDLK_UP},
|
|
|
|
{InputTypes::Mouse,SDL_BUTTON_X2 + 1},
|
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_DPAD_UP}
|
|
|
|
},
|
2023-02-11 11:18:29 +01:00
|
|
|
{
|
|
|
|
"New Game",
|
|
|
|
Msg::Menu1_New_Game,
|
|
|
|
{InputTypes::Keyboard, SDLK_F2},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
|
|
|
{}
|
2023-02-11 11:18:29 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Toggle Pause",
|
|
|
|
Msg::Menu1_Pause_Resume_Game,
|
|
|
|
{InputTypes::Keyboard, SDLK_F3},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
2023-02-11 11:18:29 +01:00
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_START}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Toggle FullScreen",
|
|
|
|
Msg::Menu1_Full_Screen,
|
|
|
|
{InputTypes::Keyboard, SDLK_F4},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
|
|
|
{}
|
2023-02-11 11:18:29 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Toggle Sounds",
|
|
|
|
Msg::Menu1_Sounds,
|
|
|
|
{InputTypes::Keyboard, SDLK_F5},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
|
|
|
{}
|
2023-02-11 11:18:29 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Toggle Music",
|
|
|
|
Msg::Menu1_Music,
|
|
|
|
{InputTypes::Keyboard, SDLK_F6},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
|
|
|
{}
|
2023-02-11 11:18:29 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Show Control Dialog",
|
|
|
|
Msg::Menu1_Player_Controls,
|
|
|
|
{InputTypes::Keyboard, SDLK_F8},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
|
|
|
{}
|
2023-02-11 11:18:29 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"Toggle Menu Display",
|
|
|
|
Msg::Menu1_ToggleShowMenu,
|
|
|
|
{InputTypes::Keyboard, SDLK_F9},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
|
|
|
{}
|
2023-02-11 11:18:29 +01:00
|
|
|
},
|
2023-02-12 06:43:08 +01:00
|
|
|
{
|
|
|
|
"Exit",
|
|
|
|
Msg::Menu1_Exit,
|
|
|
|
{InputTypes::Keyboard, SDLK_ESCAPE},
|
2023-02-17 11:35:18 +01:00
|
|
|
{},
|
2023-02-12 06:43:08 +01:00
|
|
|
{InputTypes::GameController, SDL_CONTROLLER_BUTTON_BACK}
|
|
|
|
},
|
2023-01-03 14:42:34 +01:00
|
|
|
},
|
|
|
|
{"Sounds", true},
|
2023-01-27 14:25:47 +01:00
|
|
|
{"Music", false},
|
|
|
|
{"FullScreen", false},
|
|
|
|
{"Players", 1},
|
2023-02-10 06:46:09 +01:00
|
|
|
{"Screen Resolution", -1},
|
2023-01-03 14:42:34 +01:00
|
|
|
{"UI Scale", 1.0f},
|
|
|
|
{"Uniform scaling", true},
|
2023-02-10 06:46:09 +01:00
|
|
|
{"Linear Filtering", true},
|
|
|
|
{"Frames Per Second", DefFps},
|
|
|
|
{"Updates Per Second", DefUps},
|
|
|
|
{"ShowMenu", true},
|
|
|
|
{"Uncapped Updates Per Second", false},
|
|
|
|
{"Sound Channels", DefSoundChannels},
|
|
|
|
{"HybridSleep", false},
|
|
|
|
{"Prefer 3DPB Game Data", false},
|
|
|
|
{"Integer Scaling", false},
|
|
|
|
{"Sound Volume", DefVolume},
|
|
|
|
{"Music Volume", DefVolume},
|
|
|
|
{"Stereo Sound Effects", false},
|
|
|
|
{"Debug Overlay", false},
|
|
|
|
{"Debug Overlay Grid", true},
|
|
|
|
{"Debug Overlay All Edges", true},
|
|
|
|
{"Debug Overlay Ball Position", true},
|
|
|
|
{"Debug Overlay Ball Edges", true},
|
|
|
|
{"Debug Overlay Collision Mask", true},
|
|
|
|
{"Debug Overlay Sprites", true},
|
|
|
|
{"Debug Overlay Sounds", true},
|
|
|
|
{"Debug Overlay Ball Depth Grid", true},
|
|
|
|
{"Debug Overlay AABB", true},
|
|
|
|
{"FontFileName", ""},
|
|
|
|
{"Language", translations::GetCurrentLanguage()->ShortName},
|
2023-07-11 14:33:09 +02:00
|
|
|
{"Hide Cursor", false},
|
2023-01-03 14:42:34 +01:00
|
|
|
};
|
2021-09-16 09:57:46 +02:00
|
|
|
|
2021-11-21 13:40:56 +01:00
|
|
|
void options::InitPrimary()
|
2020-11-06 14:56:32 +01:00
|
|
|
{
|
2021-09-16 09:57:46 +02:00
|
|
|
auto imContext = ImGui::GetCurrentContext();
|
|
|
|
ImGuiSettingsHandler ini_handler;
|
|
|
|
ini_handler.TypeName = "Pinball";
|
|
|
|
ini_handler.TypeHash = ImHashStr(ini_handler.TypeName);
|
|
|
|
ini_handler.ReadOpenFn = MyUserData_ReadOpen;
|
|
|
|
ini_handler.ReadLineFn = MyUserData_ReadLine;
|
|
|
|
ini_handler.WriteAllFn = MyUserData_WriteAll;
|
|
|
|
imContext->SettingsHandlers.push_back(ini_handler);
|
|
|
|
|
|
|
|
// Settings are loaded from disk on the first frame
|
|
|
|
if (!imContext->SettingsLoaded)
|
|
|
|
{
|
2022-11-17 13:38:59 +01:00
|
|
|
ImGui::LoadIniSettingsFromDisk(imContext->IO.IniFilename);
|
|
|
|
imContext->SettingsLoaded = true;
|
2021-09-16 09:57:46 +02:00
|
|
|
}
|
|
|
|
|
2023-02-10 06:46:09 +01:00
|
|
|
for (const auto opt : AllOptions)
|
2023-01-03 14:42:34 +01:00
|
|
|
opt->Load();
|
2023-02-17 11:35:18 +01:00
|
|
|
PostProcessOptions();
|
2021-11-21 13:40:56 +01:00
|
|
|
}
|
2021-09-28 07:14:18 +02:00
|
|
|
|
2021-11-21 13:40:56 +01:00
|
|
|
void options::InitSecondary()
|
|
|
|
{
|
2021-09-22 14:50:07 +02:00
|
|
|
auto maxRes = fullscrn::GetMaxResolution();
|
|
|
|
if (Options.Resolution >= 0 && Options.Resolution > maxRes)
|
|
|
|
Options.Resolution = maxRes;
|
|
|
|
fullscrn::SetResolution(Options.Resolution == -1 ? maxRes : Options.Resolution);
|
2020-11-06 14:56:32 +01:00
|
|
|
}
|
2020-11-05 16:44:34 +01:00
|
|
|
|
2020-12-04 16:35:47 +01:00
|
|
|
void options::uninit()
|
|
|
|
{
|
2023-01-27 14:25:47 +01:00
|
|
|
Options.Language.V = translations::GetCurrentLanguage()->ShortName;
|
2023-01-03 14:42:34 +01:00
|
|
|
for (const auto opt : AllOptions)
|
|
|
|
{
|
|
|
|
opt->Save();
|
|
|
|
}
|
2020-11-05 16:44:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
int options::get_int(LPCSTR lpValueName, int defaultValue)
|
2020-11-05 16:44:34 +01:00
|
|
|
{
|
2021-09-16 09:57:46 +02:00
|
|
|
auto value = GetSetting(lpValueName, std::to_string(defaultValue));
|
|
|
|
return std::stoi(value);
|
2020-11-05 16:44:34 +01:00
|
|
|
}
|
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
void options::set_int(LPCSTR lpValueName, int data)
|
2020-11-05 16:44:34 +01:00
|
|
|
{
|
2021-09-16 09:57:46 +02:00
|
|
|
SetSetting(lpValueName, std::to_string(data));
|
2020-11-05 16:44:34 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 14:50:07 +02:00
|
|
|
float options::get_float(LPCSTR lpValueName, float defaultValue)
|
|
|
|
{
|
|
|
|
auto value = GetSetting(lpValueName, std::to_string(defaultValue));
|
|
|
|
return std::stof(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void options::set_float(LPCSTR lpValueName, float data)
|
|
|
|
{
|
|
|
|
SetSetting(lpValueName, std::to_string(data));
|
|
|
|
}
|
|
|
|
|
2023-01-03 14:42:34 +01:00
|
|
|
void options::GetInput(const std::string& rowName, GameInput (&values)[3])
|
2021-10-17 17:18:29 +02:00
|
|
|
{
|
|
|
|
for (auto i = 0u; i <= 2; i++)
|
|
|
|
{
|
|
|
|
auto name = rowName + " " + std::to_string(i);
|
|
|
|
auto inputType = static_cast<InputTypes>(get_int((name + " type").c_str(), -1));
|
|
|
|
auto input = get_int((name + " input").c_str(), -1);
|
|
|
|
if (inputType <= InputTypes::GameController && input != -1)
|
2023-01-03 14:42:34 +01:00
|
|
|
values[i] = {inputType, input};
|
2021-10-17 17:18:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void options::SetInput(const std::string& rowName, GameInput (&values)[3])
|
|
|
|
{
|
|
|
|
for (auto i = 0u; i <= 2; i++)
|
|
|
|
{
|
|
|
|
auto input = values[i];
|
|
|
|
auto name = rowName + " " + std::to_string(i);
|
|
|
|
set_int((name + " type").c_str(), static_cast<int>(input.Type));
|
|
|
|
set_int((name + " input").c_str(), input.Value);
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 14:56:32 +01:00
|
|
|
|
2021-09-23 11:16:58 +02:00
|
|
|
void options::toggle(Menu1 uIDCheckItem)
|
2020-12-02 18:12:34 +01:00
|
|
|
{
|
|
|
|
switch (uIDCheckItem)
|
|
|
|
{
|
2021-09-23 11:16:58 +02:00
|
|
|
case Menu1::Sounds:
|
2021-10-09 16:28:30 +02:00
|
|
|
Options.Sounds ^= true;
|
|
|
|
Sound::Enable(Options.Sounds);
|
2020-12-02 18:12:34 +01:00
|
|
|
return;
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
case Menu1::SoundStereo:
|
|
|
|
Options.SoundStereo ^= true;
|
|
|
|
return;
|
2021-09-23 11:16:58 +02:00
|
|
|
case Menu1::Music:
|
2021-10-09 16:28:30 +02:00
|
|
|
Options.Music ^= true;
|
|
|
|
if (!Options.Music)
|
2020-12-02 18:12:34 +01:00
|
|
|
midi::music_stop();
|
|
|
|
else
|
2022-05-20 18:32:09 +02:00
|
|
|
midi::music_play();
|
2020-12-02 18:12:34 +01:00
|
|
|
return;
|
2021-09-29 02:21:21 +02:00
|
|
|
case Menu1::Show_Menu:
|
2023-01-03 14:42:34 +01:00
|
|
|
Options.ShowMenu ^= true;
|
2021-10-24 17:38:23 +02:00
|
|
|
fullscrn::window_size_changed();
|
2021-09-29 02:21:21 +02:00
|
|
|
return;
|
2021-09-23 11:16:58 +02:00
|
|
|
case Menu1::Full_Screen:
|
2021-10-09 16:28:30 +02:00
|
|
|
Options.FullScreen ^= true;
|
|
|
|
fullscrn::set_screen_mode(Options.FullScreen);
|
2020-12-02 18:12:34 +01:00
|
|
|
return;
|
2021-09-23 11:16:58 +02:00
|
|
|
case Menu1::OnePlayer:
|
|
|
|
case Menu1::TwoPlayers:
|
|
|
|
case Menu1::ThreePlayers:
|
|
|
|
case Menu1::FourPlayers:
|
|
|
|
Options.Players = static_cast<int>(uIDCheckItem) - static_cast<int>(Menu1::OnePlayer) + 1;
|
2021-02-06 14:53:47 +01:00
|
|
|
break;
|
2021-09-23 11:16:58 +02:00
|
|
|
case Menu1::MaximumResolution:
|
|
|
|
case Menu1::R640x480:
|
|
|
|
case Menu1::R800x600:
|
|
|
|
case Menu1::R1024x768:
|
2021-02-06 14:53:47 +01:00
|
|
|
{
|
2021-09-22 14:50:07 +02:00
|
|
|
auto restart = false;
|
2021-09-23 11:16:58 +02:00
|
|
|
int newResolution = static_cast<int>(uIDCheckItem) - static_cast<int>(Menu1::R640x480);
|
|
|
|
if (uIDCheckItem == Menu1::MaximumResolution)
|
2021-02-06 14:53:47 +01:00
|
|
|
{
|
2021-09-22 14:50:07 +02:00
|
|
|
restart = fullscrn::GetResolution() != fullscrn::GetMaxResolution();
|
2021-02-06 14:53:47 +01:00
|
|
|
Options.Resolution = -1;
|
|
|
|
}
|
2021-09-22 14:50:07 +02:00
|
|
|
else if (newResolution <= fullscrn::GetMaxResolution())
|
2021-02-06 14:53:47 +01:00
|
|
|
{
|
2021-09-22 14:50:07 +02:00
|
|
|
restart = newResolution != (Options.Resolution == -1
|
|
|
|
? fullscrn::GetMaxResolution()
|
|
|
|
: fullscrn::GetResolution());
|
2021-02-06 14:53:47 +01:00
|
|
|
Options.Resolution = newResolution;
|
|
|
|
}
|
2021-09-22 14:50:07 +02:00
|
|
|
|
|
|
|
if (restart)
|
|
|
|
winmain::Restart();
|
2021-02-06 14:53:47 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-09-23 11:16:58 +02:00
|
|
|
case Menu1::WindowUniformScale:
|
2021-02-09 16:09:44 +01:00
|
|
|
Options.UniformScaling ^= true;
|
|
|
|
fullscrn::window_size_changed();
|
|
|
|
break;
|
2021-09-23 11:16:58 +02:00
|
|
|
case Menu1::WindowLinearFilter:
|
|
|
|
Options.LinearFiltering ^= true;
|
2021-10-25 05:42:36 +02:00
|
|
|
render::recreate_screen_texture();
|
2021-09-23 11:16:58 +02:00
|
|
|
break;
|
2021-11-21 13:40:56 +01:00
|
|
|
case Menu1::Prefer3DPBGameData:
|
|
|
|
Options.Prefer3DPBGameData ^= true;
|
|
|
|
winmain::Restart();
|
|
|
|
break;
|
2021-11-22 07:32:17 +01:00
|
|
|
case Menu1::WindowIntegerScale:
|
|
|
|
Options.IntegerScaling ^= true;
|
|
|
|
fullscrn::window_size_changed();
|
|
|
|
break;
|
2021-02-06 14:53:47 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 17:18:29 +02:00
|
|
|
void options::InputDown(GameInput input)
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2021-10-17 17:18:29 +02:00
|
|
|
if (ControlWaitingForInput)
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2021-10-17 17:18:29 +02:00
|
|
|
*ControlWaitingForInput = input;
|
|
|
|
ControlWaitingForInput = nullptr;
|
2021-10-01 08:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void options::ShowControlDialog()
|
2020-12-02 18:12:34 +01:00
|
|
|
{
|
2021-10-01 08:05:38 +02:00
|
|
|
if (!ShowDialog)
|
|
|
|
{
|
2021-10-17 17:18:29 +02:00
|
|
|
ControlWaitingForInput = nullptr;
|
2021-10-01 08:05:38 +02:00
|
|
|
ShowDialog = true;
|
2023-01-27 14:25:47 +01:00
|
|
|
// Save previous controls in KVP storage.
|
2023-02-10 06:46:09 +01:00
|
|
|
for (const auto& control : Options.Key)
|
2023-01-27 14:25:47 +01:00
|
|
|
{
|
|
|
|
control.Save();
|
|
|
|
}
|
2021-10-01 08:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void options::RenderControlDialog()
|
|
|
|
{
|
|
|
|
if (!ShowDialog)
|
|
|
|
return;
|
|
|
|
|
2023-01-27 14:25:47 +01:00
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2{550, 550});
|
2022-08-31 14:18:22 +02:00
|
|
|
if (ImGui::Begin(pb::get_rc_string(Msg::KEYMAPPER_Caption), &ShowDialog))
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2023-02-11 11:18:29 +01:00
|
|
|
if (ImGui::TreeNode(pb::get_rc_string(Msg::KEYMAPPER_Groupbox2)))
|
|
|
|
{
|
|
|
|
ImGui::TextWrapped("%s", pb::get_rc_string(Msg::KEYMAPPER_Help1));
|
|
|
|
ImGui::TextWrapped("%s", pb::get_rc_string(Msg::KEYMAPPER_Help2));
|
|
|
|
ImGui::TreePop();
|
|
|
|
}
|
2021-10-01 08:05:38 +02:00
|
|
|
|
|
|
|
ImGui::Spacing();
|
2022-08-31 14:18:22 +02:00
|
|
|
ImGui::TextUnformatted(pb::get_rc_string(Msg::KEYMAPPER_Groupbox1));
|
2021-10-01 08:05:38 +02:00
|
|
|
|
|
|
|
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2{5, 10});
|
2023-02-10 06:46:09 +01:00
|
|
|
if (ImGui::BeginTable("Controls", 4,
|
|
|
|
ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_Borders |
|
|
|
|
ImGuiTableFlags_SizingStretchSame))
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2021-10-17 17:18:29 +02:00
|
|
|
ImGui::TableSetupColumn("Control");
|
|
|
|
ImGui::TableSetupColumn("Binding 1");
|
|
|
|
ImGui::TableSetupColumn("Binding 2");
|
|
|
|
ImGui::TableSetupColumn("Binding 3");
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
|
2023-01-27 14:25:47 +01:00
|
|
|
int rowHash = 0;
|
2023-02-10 06:46:09 +01:00
|
|
|
for (auto& option : Options.Key)
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
|
|
|
ImGui::TableNextColumn();
|
2021-10-17 17:18:29 +02:00
|
|
|
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{0.5, 0, 0, 1});
|
2023-02-10 06:46:09 +01:00
|
|
|
if (ImGui::Button(pb::get_rc_string(option.Description), ImVec2(-1, 0)))
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2023-01-27 14:25:47 +01:00
|
|
|
for (auto& input : option.Inputs)
|
|
|
|
input = {};
|
2021-10-17 17:18:29 +02:00
|
|
|
}
|
|
|
|
ImGui::PopStyleColor(1);
|
2021-10-01 08:05:38 +02:00
|
|
|
|
2023-01-27 14:25:47 +01:00
|
|
|
for (auto& input : option.Inputs)
|
2021-10-17 17:18:29 +02:00
|
|
|
{
|
2021-10-01 08:05:38 +02:00
|
|
|
ImGui::TableNextColumn();
|
2023-01-27 14:25:47 +01:00
|
|
|
if (ControlWaitingForInput == &input)
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2023-02-10 06:46:09 +01:00
|
|
|
if (ImGui::Button("Press the key", ImVec2(-1, 0)))
|
2023-01-27 14:25:47 +01:00
|
|
|
{
|
|
|
|
ControlWaitingForInput = &input;
|
|
|
|
}
|
2021-10-01 08:05:38 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-11 11:18:29 +01:00
|
|
|
auto inputDescription = input.GetFullInputDescription();
|
2023-01-27 14:25:47 +01:00
|
|
|
if (ImGui::Button((inputDescription + "##" + std::to_string(rowHash++)).c_str(),
|
2021-10-17 17:18:29 +02:00
|
|
|
ImVec2(-1, 0)))
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2023-01-27 14:25:47 +01:00
|
|
|
ControlWaitingForInput = &input;
|
2021-10-01 08:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
ImGui::Spacing();
|
|
|
|
|
2023-02-12 06:43:08 +01:00
|
|
|
if (ImGui::Button(pb::get_rc_string(Msg::GenericOk)))
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
|
|
|
ShowDialog = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
2023-02-12 06:43:08 +01:00
|
|
|
if (ImGui::Button(pb::get_rc_string(Msg::GenericCancel)))
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2023-01-27 14:25:47 +01:00
|
|
|
for (auto& control : Options.Key)
|
|
|
|
{
|
|
|
|
control.Load();
|
|
|
|
}
|
2021-10-01 08:05:38 +02:00
|
|
|
ShowDialog = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
2022-08-31 14:18:22 +02:00
|
|
|
if (ImGui::Button(pb::get_rc_string(Msg::KEYMAPPER_Default)))
|
2021-10-01 08:05:38 +02:00
|
|
|
{
|
2023-01-27 14:25:47 +01:00
|
|
|
for (auto& control : Options.Key)
|
|
|
|
{
|
|
|
|
control.Reset();
|
|
|
|
}
|
2021-10-17 17:18:29 +02:00
|
|
|
ControlWaitingForInput = nullptr;
|
2021-10-01 08:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
ImGui::PopStyleVar();
|
|
|
|
|
|
|
|
if (!ShowDialog)
|
2021-10-17 17:18:29 +02:00
|
|
|
ControlWaitingForInput = nullptr;
|
2020-12-02 18:12:34 +01:00
|
|
|
}
|
2021-09-16 09:57:46 +02:00
|
|
|
|
2023-01-27 14:25:47 +01:00
|
|
|
std::vector<GameBindings> options::MapGameInput(GameInput key)
|
|
|
|
{
|
|
|
|
std::vector<GameBindings> result;
|
|
|
|
for (auto inputId = GameBindings::Min; inputId < GameBindings::Max; inputId++)
|
|
|
|
{
|
|
|
|
for (auto& inputValue : Options.Key[~inputId].Inputs)
|
|
|
|
{
|
|
|
|
if (key == inputValue)
|
|
|
|
{
|
|
|
|
result.push_back(inputId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-02-17 11:35:18 +01:00
|
|
|
void options::ResetAllOptions()
|
|
|
|
{
|
|
|
|
for (const auto opt : AllOptions)
|
|
|
|
opt->Reset();
|
|
|
|
PostProcessOptions();
|
|
|
|
}
|
|
|
|
|
2021-09-16 09:57:46 +02:00
|
|
|
void options::MyUserData_ReadLine(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line)
|
|
|
|
{
|
2023-01-27 14:25:47 +01:00
|
|
|
auto& keyValueStore = *static_cast<std::unordered_map<std::string, std::string>*>(entry);
|
2021-09-16 09:57:46 +02:00
|
|
|
std::string keyValue = line;
|
|
|
|
auto separatorPos = keyValue.find('=');
|
|
|
|
if (separatorPos != std::string::npos)
|
|
|
|
{
|
|
|
|
auto key = keyValue.substr(0, separatorPos);
|
|
|
|
auto value = keyValue.substr(separatorPos + 1, keyValue.size());
|
|
|
|
keyValueStore[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* options::MyUserData_ReadOpen(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name)
|
|
|
|
{
|
|
|
|
// There is only one custom entry
|
|
|
|
return strcmp(name, "Settings") == 0 ? &settings : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void options::MyUserData_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf)
|
|
|
|
{
|
|
|
|
buf->appendf("[%s][%s]\n", handler->TypeName, "Settings");
|
|
|
|
for (const auto& setting : settings)
|
|
|
|
{
|
|
|
|
buf->appendf("%s=%s\n", setting.first.c_str(), setting.second.c_str());
|
|
|
|
}
|
|
|
|
buf->append("\n");
|
|
|
|
}
|
|
|
|
|
2023-02-17 11:35:18 +01:00
|
|
|
void options::PostProcessOptions()
|
|
|
|
{
|
|
|
|
winmain::ImIO->FontGlobalScale = Options.UIScale;
|
|
|
|
Options.FramesPerSecond = Clamp(Options.FramesPerSecond.V, MinFps, MaxFps);
|
|
|
|
Options.UpdatesPerSecond = Clamp(Options.UpdatesPerSecond.V, MinUps, MaxUps);
|
|
|
|
Options.UpdatesPerSecond = std::max(Options.UpdatesPerSecond.V, Options.FramesPerSecond.V);
|
|
|
|
Options.SoundChannels = Clamp(Options.SoundChannels.V, MinSoundChannels, MaxSoundChannels);
|
|
|
|
Options.SoundVolume = Clamp(Options.SoundVolume.V, MinVolume, MaxVolume);
|
|
|
|
Options.MusicVolume = Clamp(Options.MusicVolume.V, MinVolume, MaxVolume);
|
|
|
|
translations::SetCurrentLanguage(Options.Language.V.c_str());
|
|
|
|
winmain::UpdateFrameRate();
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:18:29 +01:00
|
|
|
std::string GameInput::GetFullInputDescription() const
|
|
|
|
{
|
|
|
|
std::string prefix;
|
|
|
|
switch (Type)
|
|
|
|
{
|
|
|
|
case InputTypes::Keyboard:
|
|
|
|
prefix = "Keyboard\n";
|
|
|
|
break;
|
|
|
|
case InputTypes::Mouse:
|
|
|
|
prefix = "Mouse\n";
|
|
|
|
break;
|
|
|
|
case InputTypes::GameController:
|
|
|
|
prefix = "Controller\n";
|
|
|
|
break;
|
|
|
|
case InputTypes::None:
|
|
|
|
default:
|
2023-02-17 11:35:18 +01:00
|
|
|
return "Unused";
|
2023-02-11 11:18:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return prefix + GetShortInputDescription();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GameInput::GetShortInputDescription() const
|
2023-01-27 14:25:47 +01:00
|
|
|
{
|
|
|
|
static LPCSTR mouseButtons[]
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
"Left",
|
|
|
|
"Middle",
|
|
|
|
"Right",
|
|
|
|
"X1",
|
|
|
|
"X2",
|
|
|
|
};
|
|
|
|
|
|
|
|
static LPCSTR controllerButtons[] =
|
|
|
|
{
|
|
|
|
"A",
|
|
|
|
"B",
|
|
|
|
"X",
|
|
|
|
"Y",
|
|
|
|
"Back",
|
|
|
|
"Guide",
|
|
|
|
"Start",
|
|
|
|
"LeftStick",
|
|
|
|
"RightStick",
|
|
|
|
"LeftShoulder",
|
|
|
|
"RightShoulder",
|
|
|
|
"DpUp",
|
|
|
|
"DpDown",
|
|
|
|
"DpLeft",
|
|
|
|
"DpRight",
|
|
|
|
"Misc1",
|
|
|
|
"Paddle1",
|
|
|
|
"Paddle2",
|
|
|
|
"Paddle3",
|
|
|
|
"Paddle4",
|
|
|
|
"Touchpad",
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string keyName;
|
|
|
|
switch (Type)
|
|
|
|
{
|
|
|
|
case InputTypes::Keyboard:
|
2023-02-11 11:18:29 +01:00
|
|
|
keyName = SDL_GetKeyName(Value);
|
2023-01-27 14:25:47 +01:00
|
|
|
break;
|
|
|
|
case InputTypes::Mouse:
|
|
|
|
if (Value >= SDL_BUTTON_LEFT && Value <= SDL_BUTTON_X2)
|
2023-02-11 11:18:29 +01:00
|
|
|
keyName = mouseButtons[Value];
|
2023-01-27 14:25:47 +01:00
|
|
|
else
|
2023-02-17 11:35:18 +01:00
|
|
|
keyName = "MButton" + std::to_string(Value);
|
2023-01-27 14:25:47 +01:00
|
|
|
break;
|
|
|
|
case InputTypes::GameController:
|
2023-02-12 07:01:20 +01:00
|
|
|
if (Value >= SDL_CONTROLLER_BUTTON_A && Value < std::min(static_cast<int>(SDL_CONTROLLER_BUTTON_MAX), 21))
|
2023-02-11 11:18:29 +01:00
|
|
|
keyName = controllerButtons[Value];
|
2023-01-27 14:25:47 +01:00
|
|
|
else
|
2023-02-17 11:35:18 +01:00
|
|
|
keyName = "CButton" + std::to_string(Value);
|
2023-01-27 14:25:47 +01:00
|
|
|
break;
|
|
|
|
case InputTypes::None:
|
|
|
|
default:
|
2023-02-11 11:18:29 +01:00
|
|
|
break;
|
2023-01-27 14:25:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return keyName;
|
|
|
|
}
|
|
|
|
|
2023-01-03 14:42:34 +01:00
|
|
|
const std::string& options::GetSetting(const std::string& key, const std::string& defaultValue)
|
2021-09-16 09:57:46 +02:00
|
|
|
{
|
|
|
|
auto setting = settings.find(key);
|
|
|
|
if (setting == settings.end())
|
|
|
|
{
|
2023-01-03 14:42:34 +01:00
|
|
|
settings[key] = defaultValue;
|
2021-09-16 09:57:46 +02:00
|
|
|
if (ImGui::GetCurrentContext())
|
|
|
|
ImGui::MarkIniSettingsDirty();
|
2023-01-03 14:42:34 +01:00
|
|
|
return defaultValue;
|
2021-09-16 09:57:46 +02:00
|
|
|
}
|
|
|
|
return setting->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void options::SetSetting(const std::string& key, const std::string& value)
|
|
|
|
{
|
|
|
|
settings[key] = value;
|
|
|
|
if (ImGui::GetCurrentContext())
|
|
|
|
ImGui::MarkIniSettingsDirty();
|
|
|
|
}
|
2023-01-03 14:42:34 +01:00
|
|
|
|
|
|
|
OptionBase::OptionBase(LPCSTR name): Name(name)
|
|
|
|
{
|
|
|
|
options::AllOptions.push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionBase::~OptionBase()
|
|
|
|
{
|
|
|
|
auto& vec = options::AllOptions;
|
|
|
|
auto position = std::find(vec.begin(), vec.end(), this);
|
|
|
|
if (position != vec.end())
|
|
|
|
vec.erase(position);
|
|
|
|
}
|
2023-02-11 11:18:29 +01:00
|
|
|
|
|
|
|
std::string ControlOption::GetShortcutDescription() const
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
for (const auto& input : Inputs)
|
|
|
|
{
|
|
|
|
if (input.Type != InputTypes::None)
|
|
|
|
{
|
|
|
|
result = input.GetShortInputDescription();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|