Skip to content

Commit

Permalink
Merge pull request #317 from CBATeam/cba_settings
Browse files Browse the repository at this point in the history
CBA Settings frame work
  • Loading branch information
Killswitch00 authored Jul 6, 2016
2 parents c7c9d2e + 2ff2cd0 commit b04c7c9
Show file tree
Hide file tree
Showing 50 changed files with 2,625 additions and 1 deletion.
1 change: 1 addition & 0 deletions addons/settings/$PBOPREFIX$
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x\cba\addons\settings
25 changes: 25 additions & 0 deletions addons/settings/Cfg3DEN.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

class Cfg3DEN {
class Mission {
class Scenario {
class AttributeCategories {
class Presentation { // any existing
class Attributes {
class BriefingName;
class Author; // needed, to put blank space at the end. for looks
class GVAR(hash) {
property = QGVAR(hash);
value = 0;
control = "Default"; // blank space. not editable by hand
displayName = "";
tooltip = "";
defaultValue = QUOTE(NULL_HASH);
expression = QUOTE(missionNamespace setVariable [ARR_3(QUOTE(QGVAR(hash)),_value,true)]);
wikiType = "[[Array]]";
};
};
};
};
};
};
};
27 changes: 27 additions & 0 deletions addons/settings/CfgEventHandlers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

class Extended_PreStart_EventHandlers {
class ADDON {
init = QUOTE(if (!IS_LINUX) then {call COMPILE_FILE(XEH_preStart)};);
};
};

class Extended_PreInit_EventHandlers {
class ADDON {
init = QUOTE(if (!IS_LINUX) then {call COMPILE_FILE(XEH_preInit)};);
};
};

class Extended_PostInit_EventHandlers {
class ADDON {
init = QUOTE(if (!IS_LINUX) then {call COMPILE_FILE(XEH_postInit)};);
};
};

class Extended_DisplayLoad_EventHandlers {
class RscDisplayGameOptions {
ADDON = QUOTE(if (IS_LINUX || {isNil 'ADDON'}) then {_this call COMPILE_FILE(gui_initDisplay_disabled)} else {_this call COMPILE_FILE(gui_initDisplay)};);
};
class Display3DEN {
ADDON = QUOTE(if (!IS_LINUX) then {_this call COMPILE_FILE(init3DEN)};);
};
};
10 changes: 10 additions & 0 deletions addons/settings/CfgFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#define F_FILEPATH(func) class func {\
file = QUOTE(PATHTOF(DOUBLES(fnc,func).sqf));\
}

class CfgFunctions {
class CBA {
class Settings {};
};
};
20 changes: 20 additions & 0 deletions addons/settings/Display3DEN.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class ctrlMenuStrip;

class Display3DEN {
class Controls {
class MenuStrip: ctrlMenuStrip {
class Items {
class Options {
items[] += {QUOTE(ADDON)};
};
class ADDON {
text = CSTRING(shortcut);
action = QUOTE(findDisplay 313 call COMPILE_FILE(openSettingsMenu));
data = QUOTE(ADDON);
shortcuts[] = {INPUT_CTRL_OFFSET + INPUT_ALT_OFFSET + DIK_S};
};
};
};
};
};
19 changes: 19 additions & 0 deletions addons/settings/XEH_PREP.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

PREP(init);
PREP(set);
PREP(get);
PREP(check);
PREP(parse);
PREP(import);
PREP(export);
PREP(clear);
PREP(saveTempData);
PREP(isForced);
PREP(isOverwritten);

PREP(gui_addonChanged);
PREP(gui_sourceChanged);
PREP(gui_configure);
PREP(gui_closeMenu);
PREP(gui_refresh);
PREP(gui_preset);
48 changes: 48 additions & 0 deletions addons/settings/XEH_postInit.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "script_component.hpp"

// --- refresh all settings after postInit to guarantee that events are added and settings are recieved from server
{
if (isNil QGVAR(serverSettings)) then {
diag_log text "[CBA] (settings): No server settings after postInit phase.";
};

GVAR(ready) = true;
{
[QGVAR(refreshSetting), _x] call CBA_fnc_localEvent;
} forEach GVAR(allSettings);
} call CBA_fnc_execNextFrame;

// --- autosave mission and server presets
private _presetsHash = profileNamespace getVariable [QGVAR(presetsHash), NULL_HASH];
private _autosavedPresets = profileNamespace getVariable [QGVAR(autosavedPresets), [[],[]]];

if !(allVariables GVAR(missionSettings) isEqualTo []) then {
private _preset = "mission" call FUNC(export);
private _presetName = format ["Autosave: %1 (%2)", localize LSTRING(ButtonMission), missionName];

[_presetsHash, _presetName, _preset] call CBA_fnc_hashSet;

(_autosavedPresets select 0) pushBackUnique _presetName;

if (count (_autosavedPresets select 0) > 3) then {
private _presetToRemove = (_autosavedPresets select 0) deleteAt 0;
[_presetsHash, _presetToRemove] call CBA_fnc_hashRem;
};
};

if (serverName != "") then {
private _preset = "server" call FUNC(export);
private _presetName = format ["Autosave: %1 (%2)", localize LSTRING(ButtonServer), serverName];

[_presetsHash, _presetName, _preset] call CBA_fnc_hashSet;

(_autosavedPresets select 1) pushBackUnique _presetName;

if (count (_autosavedPresets select 1) > 3) then {
private _presetToRemove = (_autosavedPresets select 1) deleteAt 0;
[_presetsHash, _presetToRemove] call CBA_fnc_hashRem;
};
};

profileNamespace setVariable [QGVAR(presetsHash), _presetsHash];
profileNamespace setVariable [QGVAR(autosavedPresets), _autosavedPresets];
74 changes: 74 additions & 0 deletions addons/settings/XEH_preInit.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//#define DEBUG_MODE_FULL
#include "script_component.hpp"

ADDON = false;

#include "XEH_PREP.sqf"

#ifdef DEBUG_MODE_FULL
["Test_Setting_1", "CHECKBOX", ["-test checkbox-", "-tooltip-"], "My Category", true] call cba_settings_fnc_init;
["Test_Setting_2", "LIST", ["-test list-", "-tooltip-"], "My Category", [[1,0], ["enabled","disabled"], 1]] call cba_settings_fnc_init;
["Test_Setting_3", "SLIDER", ["-test slider-", "-tooltip-"], "My Category", [0, 10, 5, 0]] call cba_settings_fnc_init;
["Test_Setting_4", "COLOR", ["-test color-", "-tooltip-"], "My Category", [1,1,0]] call cba_settings_fnc_init;
#endif

// --- init settings namespaces
#include "initSettings.sqf"

// --- event to refresh missionNamespace value if setting has changed and call public event as well as execute setting script
[QGVAR(refreshSetting), {
params ["_setting"];
private _value = _setting call FUNC(get);

missionNamespace setVariable [_setting, _value];

if (isNil QGVAR(ready)) exitWith {};

private _script = NAMESPACE_GETVAR(GVAR(defaultSettings),_setting,[]) param [8, {}];
[_value, _script] call {
private ["_setting", "_value", "_script"]; // prevent these variables from being overwritten
(_this select 0) call (_this select 1);
};

["CBA_SettingChanged", [_setting, _value]] call CBA_fnc_localEvent;
}] call CBA_fnc_addEventHandler;

// event to refresh all settings at once - saves bandwith
[QGVAR(refreshAllSettings), {
{
[QGVAR(refreshSetting), _x] call CBA_fnc_localEvent;
} forEach GVAR(allSettings);
}] call CBA_fnc_addEventHandler;

// refresh all settings when loading a save game
addMissionEventHandler ["Loaded", {
QGVAR(refreshAllSettings) call CBA_fnc_localEvent;
}];

#ifdef DEBUG_MODE_FULL
["CBA_SettingChanged", {
params ["_setting", "_value"];

private _message = format ["[CBA] (settings): %1 = %2", _setting, _value];
systemChat _message;
diag_log text _message;
}] call CBA_fnc_addEventHandler;
#endif

// event to modify settings on a dedicated server as admin
if (isServer) then {
[QGVAR(setSettingServer), {
params ["_setting", "_value", "_forced"];
[_setting, _value, _forced, "server"] call FUNC(set);
}] call CBA_fnc_addEventHandler;
};

// import settings from file if filepatching is enabled
if (isFilePatchingEnabled) then {
[loadFile PATH_SETTINGS_FILE, "client"] call FUNC(import);
diag_log text "[CBA] (settings): Settings file loaded.";
} else {
diag_log text "[CBA] (settings): Cannot load settings file. File patching disabled. Use -filePatching flag.";
};

ADDON = true;
3 changes: 3 additions & 0 deletions addons/settings/XEH_preStart.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "script_component.hpp"

#include "XEH_PREP.sqf"
23 changes: 23 additions & 0 deletions addons/settings/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "script_component.hpp"

class CfgPatches {
class ADDON {
author = "$STR_CBA_Author";
name = CSTRING(component);
url = "$STR_CBA_URL";
units[] = {};
weapons[] = {};
requiredVersion = REQUIRED_VERSION;
requiredAddons[] = {"CBA_common", "A3_UI_F", "3DEN"};
version = VERSION;
authors[] = {"commy2"};
};
};

#include "CfgEventHandlers.hpp"
#include "CfgFunctions.hpp"

#include "Cfg3DEN.hpp"
#include "Display3DEN.hpp"

#include "gui.hpp"
41 changes: 41 additions & 0 deletions addons/settings/fnc_check.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_settings_fnc_check
Description:
Check if provided value is valid.
Parameters:
_setting - Name of the setting <STRING>
_value - Value of to test <ANY>
Returns:
_return - true: Tested value is accepted for setting, false: Wrong value for setting
Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_setting", "", [""]], "_value"];

if (isNil "_value") exitWith {false};

NAMESPACE_GETVAR(GVAR(defaultSettings),_setting,[]) params ["_defaultValue", "", ["_settingType", ""], "_settingData"];

switch (toUpper _settingType) do {
case ("CHECKBOX"): {
_value isEqualType false
};
case ("LIST"): {
_settingData params ["_values"];
_value in _values
};
case ("SLIDER"): {
_settingData params ["_min", "_max"];
_value isEqualType 0 && {_value >= _min} && {_value <= _max}
};
case ("COLOR"): {
_value isEqualType [] && {count _value == count _defaultValue} && {_value isEqualTypeAll 0} && {{_x < 0 || _x > 1} count _value == 0}
};
default {false};
};
50 changes: 50 additions & 0 deletions addons/settings/fnc_clear.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* ----------------------------------------------------------------------------
Internal Function: CBA_settings_fnc_clear
Description:
Clear all settings from profile or mission.
Parameters:
_source - Can be "client", "server" or "mission" (optional, default: "client") <STRING>
Returns:
None
Author:
commy2
---------------------------------------------------------------------------- */
#include "script_component.hpp"

params [["_source", "client", [""]]];

switch (toLower _source) do {
case ("client"): {
profileNamespace setVariable [QGVAR(hash), NULL_HASH];
GVAR(clientSettings) call CBA_fnc_deleteNamespace;
GVAR(clientSettings) = [] call CBA_fnc_createNamespace;
};
case ("server"): {
if (!isServer) exitWith {};

profileNamespace setVariable [QGVAR(hash), NULL_HASH];
GVAR(serverSettings) call CBA_fnc_deleteNamespace;
GVAR(serverSettings) = isMultiplayer call CBA_fnc_createNamespace;
publicVariable QGVAR(serverSettings);
};
case ("mission"): {
if (!is3DEN) exitWith {};

set3DENMissionAttributes [["Scenario", QGVAR(hash), NULL_HASH]];
GVAR(missionSettings) call CBA_fnc_deleteNamespace;
GVAR(missionSettings) = [] call CBA_fnc_createNamespace;
};
default {};
};

if (isServer) then {
QGVAR(refreshAllSettings) call CBA_fnc_globalEvent;
} else {
QGVAR(refreshAllSettings) call CBA_fnc_localEvent;
};

nil
Loading

0 comments on commit b04c7c9

Please sign in to comment.