File: Ingame_Options.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (109 lines) | stat: -rw-r--r-- 3,294 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "options/Ingame_Options.h"
#include "options/Ingame_Options_internal.h"
#include "options/manager/ingame_options_manager.h"

#include "options/OptionsManager.h"
#include "options/Option.h"

static std::unique_ptr<OptConfigurator> OCGR;

// A list of all options categories: the category name and a boolean value for whether or not the window is active
SCP_vector<std::pair<SCP_string, bool>> Option_categories;

const std::unique_ptr<OptConfigurator>& getOptConfigurator()
{
	if (OCGR == nullptr) {
		OCGR.reset(new OptConfigurator());
	}

	return OCGR;
}

void ingame_options_init()
{
	gr_set_clear_color(0, 0, 0);
	auto& optionsList = options::OptionsManager::instance()->getOptions();

	// Get a list of all option categories
	for (auto& option : optionsList) {
		const options::OptionBase* thisOpt = option.get();

		if (thisOpt->getFlags()[options::OptionFlags::RetailBuiltinOption]) {
			continue;
		}

		// Get the category and save it for the window list
		SCP_string cat = thisOpt->getCategory();

		bool found = false;
		for (size_t i = 0; i < Option_categories.size(); i++) {
			if (Option_categories[i].first == cat) {
				found = true;
				break;
			}
		}

		if (!found) {
			Option_categories.push_back(std::make_pair(cat, true));
		}

		auto val = thisOpt->getCurrentValueDescription();

		// If it's a binary option then setup the necessary boolean for imgui
		if (thisOpt->getType() == options::OptionType::Selection) {
			auto values = thisOpt->getValidValues();

			if ((values.size() == 2) && !(thisOpt->getFlags()[options::OptionFlags::ForceMultiValueSelection])) {

				// On/Off options
				if (getOptConfigurator()->get_binary_option_index(thisOpt->getTitle()) < 0) {
					std::pair<SCP_string, bool> thisPair = std::make_pair(thisOpt->getTitle(), false);

					// Options manager stores the boolean as a serialized string, so we need to set our own bool
					if (val.serialized == "true") {
						thisPair.second = true;
					}

					getOptConfigurator()->binary_options.push_back(thisPair);
				}
			}
		// If it's a range option then setup the necessary float or int variables for imgui
		} else if (thisOpt->getType() == options::OptionType::Range) {

			// OptionsManager stores the value as a serialized string, so we need to return it to a float
			float f_val;
			try {
				f_val = std::stof(val.serialized.c_str());
			} catch (...) {
				Warning(LOCATION, "Error getting value for option '%s'. Setting to 0.0f!", thisOpt->getTitle().c_str());
				f_val = 0.0f;
			}

			// Integer Ranges
			if (thisOpt->getFlags()[options::OptionFlags::RangeTypeInteger]) {
				if (getOptConfigurator()->get_int_range_option_index(thisOpt->getTitle()) < 0) {
					std::pair<SCP_string, int> thisPair = std::make_pair(thisOpt->getTitle().c_str(), static_cast<int>(f_val));
					getOptConfigurator()->range_int_options.push_back(thisPair);
				}

			// Float Ranges
			} else {
				if (getOptConfigurator()->get_float_range_option_index(thisOpt->getTitle()) < 0) {
					std::pair<SCP_string, float> thisPair = std::make_pair(thisOpt->getTitle().c_str(), f_val);
					getOptConfigurator()->range_float_options.push_back(thisPair);
				}
			}
		}

	}
}

void ingame_options_close()
{
	OCGR.reset();
}

void ingame_options_do_frame()
{
	getOptConfigurator()->onFrame();
}