working setting type SET in MC settings menu

This commit is contained in:
Steffen Schröder 2014-05-18 23:35:39 +02:00
parent cb5ad4e077
commit 5cf86b2025
4 changed files with 191 additions and 102 deletions

View File

@ -2,6 +2,7 @@
namespace ManiaControl\Configurators; namespace ManiaControl\Configurators;
use FML\Components\ValuePicker;
use FML\Controls\Control; use FML\Controls\Control;
use FML\Controls\Entry; use FML\Controls\Entry;
use FML\Controls\Frame; use FML\Controls\Frame;
@ -126,6 +127,8 @@ class ManiaControlSettings implements ConfiguratorMenu, CallbackListener {
$index = 0; $index = 0;
$y = 0; $y = 0;
foreach ($settings as $setting) { foreach ($settings as $setting) {
/** @var Setting $setting */
if (!$pageFrame) { if (!$pageFrame) {
$pageFrame = new Frame(); $pageFrame = new Frame();
$frame->add($pageFrame); $frame->add($pageFrame);
@ -162,22 +165,33 @@ class ManiaControlSettings implements ConfiguratorMenu, CallbackListener {
$nameLabel->setText($setting->setting); $nameLabel->setText($setting->setting);
$nameLabel->setTextColor("FFF"); $nameLabel->setTextColor("FFF");
$settingName = self::ACTION_PREFIX_SETTING . $setting->index;
if ($setting->type === Setting::TYPE_BOOL) { if ($setting->type === Setting::TYPE_BOOL) {
// TODO: implement fml checkbox
$quad = new Quad_Icons64x64_1(); $quad = new Quad_Icons64x64_1();
$settingFrame->add($quad); $settingFrame->add($quad);
$quad->setX($width / 2 * 0.6); $quad->setPosition($width * 0.33, 0, -0.01);
$quad->setZ(-0.01);
$quad->setSubStyle(($setting->value ? $quad::SUBSTYLE_LvlGreen : $quad::SUBSTYLE_LvlRed));
$quad->setSize(4, 4); $quad->setSize(4, 4);
$quad->setAction(self::ACTION_SETTING_BOOL . $setting->index); $quad->setSubStyle(($setting->value ? $quad::SUBSTYLE_LvlGreen : $quad::SUBSTYLE_LvlRed));
$quad->setAction($settingName);
} else if ($setting->type === Setting::TYPE_SET) {
// SET value picker
$label = new Label_Text();
$label->setX($width * 0.33);
$label->setSize($width * 0.3, $settingHeight * 0.9);
$label->setStyle($label::STYLE_TextValueSmall);
$label->setTextSize(1);
$valuePicker = new ValuePicker($settingName, $setting->set, $setting->value, $label);
$settingFrame->add($valuePicker);
} else { } else {
// Standard entry
$entry = new Entry(); $entry = new Entry();
$settingFrame->add($entry); $settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall); $entry->setX($width * 0.33);
$entry->setX($width / 2 * 0.65);
$entry->setTextSize(1);
$entry->setSize($width * 0.3, $settingHeight * 0.9); $entry->setSize($width * 0.3, $settingHeight * 0.9);
$entry->setName(self::ACTION_PREFIX_SETTING . $setting->index); $entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setTextSize(1);
$entry->setName($settingName);
$entry->setDefault($setting->value); $entry->setDefault($setting->value);
} }
@ -364,19 +378,17 @@ class ManiaControlSettings implements ConfiguratorMenu, CallbackListener {
return; return;
} }
$maniaControlSettings = $this->maniaControl->settingManager->getSettings();
$prefixLength = strlen(self::ACTION_PREFIX_SETTING); $prefixLength = strlen(self::ACTION_PREFIX_SETTING);
foreach ($configData[3] as $settingData) {
$settingIndex = substr($settingData['Name'], $prefixLength);
foreach ($configData[3] as $setting) { $setting = $this->maniaControl->settingManager->getSettingObjectByIndex($settingIndex);
$settingName = substr($setting['Name'], $prefixLength); if (!$setting || $settingData['Value'] == $setting->value || $setting->type === Setting::TYPE_BOOL) {
$oldSetting = $maniaControlSettings[$settingName];
if ($setting['Value'] == $oldSetting->value || $oldSetting->type == 'bool') {
continue; continue;
} }
$this->maniaControl->settingManager->setSetting($oldSetting->class, $oldSetting->setting, $setting['Value']); $setting->value = $settingData['Value'];
$this->maniaControl->settingManager->saveSetting($setting);
} }
$this->maniaControl->chat->sendSuccess('Settings saved!', $player); $this->maniaControl->chat->sendSuccess('Settings saved!', $player);

View File

@ -50,6 +50,29 @@ class ValuePickerFeature extends ScriptFeature {
$this->setDefault($default); $this->setDefault($default);
} }
/**
* Set the possible Values
*
* @param array $values Possible Values
* @return \FML\Script\Features\ValuePickerFeature
*/
public function setValues(array $values) {
$this->values = array();
foreach ($values as $value) {
array_push($this->values, (string)$value);
}
return $this;
}
/**
* Get the ValuePicker Label
*
* @return \FML\Controls\Label
*/
public function getLabel() {
return $this->label;
}
/** /**
* Set the ValuePicker Label * Set the ValuePicker Label
* *
@ -66,12 +89,12 @@ class ValuePickerFeature extends ScriptFeature {
} }
/** /**
* Get the ValuePicker Label * Get the hidden Entry
* *
* @return \FML\Controls\Label * @return \FML\Controls\Entry
*/ */
public function getLabel() { public function getEntry() {
return $this->label; return $this->entry;
} }
/** /**
@ -88,54 +111,6 @@ class ValuePickerFeature extends ScriptFeature {
return $this; return $this;
} }
/**
* Get the hidden Entry
*
* @return \FML\Controls\Entry
*/
public function getEntry() {
return $this->entry;
}
/**
* Set the possible Values
*
* @param array $values Possible Values
* @return \FML\Script\Features\ValuePickerFeature
*/
public function setValues(array $values) {
$this->values = array();
foreach ($values as $value) {
array_push($this->values, (string)$value);
}
return $this;
}
/**
* Set the default Value
*
* @param string $default Default Value
* @return \FML\Script\Features\ValuePickerFeature
*/
public function setDefault($default) {
$this->default = (string)$default;
}
/**
* Get the default Value
*
* @return string
*/
public function getDefault() {
if ($this->default) {
return $this->default;
}
if ($this->values) {
return reset($this->values);
}
return null;
}
/** /**
* @see \FML\Script\Features\ScriptFeature::prepare() * @see \FML\Script\Features\ScriptFeature::prepare()
*/ */
@ -144,7 +119,7 @@ class ValuePickerFeature extends ScriptFeature {
$script->setScriptInclude(ScriptInclude::TEXTLIB); $script->setScriptInclude(ScriptInclude::TEXTLIB);
$script->addScriptFunction(self::FUNCTION_UPDATE_PICKER_VALUE, $this->buildUpdatePickerValueFunction()); $script->addScriptFunction(self::FUNCTION_UPDATE_PICKER_VALUE, $this->buildUpdatePickerValueFunction());
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->buildInitScriptText(), true); $script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->buildInitScriptText(), true);
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->buildClickScriptText()); $script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $this->buildClickScriptText());
} }
return $this; return $this;
} }
@ -158,12 +133,14 @@ class ValuePickerFeature extends ScriptFeature {
$functionText = " $functionText = "
Void " . self::FUNCTION_UPDATE_PICKER_VALUE . "(CMlLabel _Label) { Void " . self::FUNCTION_UPDATE_PICKER_VALUE . "(CMlLabel _Label) {
declare " . self::VAR_PICKER_VALUES . " as Values for _Label = Text[]; declare " . self::VAR_PICKER_VALUES . " as Values for _Label = Text[];
declare NewValueIndex = 0; declare NewValueIndex = -1;
if (Values.exists(_Label.Value) { if (Values.exists(_Label.Value)) {
declare ValueIndex = _Label.keyof(_Label.Value); declare ValueIndex = Values.keyof(_Label.Value);
ValueIndex += 1; ValueIndex += 1;
if (Values.existskey(ValueIndex)) { if (Values.existskey(ValueIndex)) {
NewValueIndex = ValueIndex; NewValueIndex = ValueIndex;
} else {
NewValueIndex = 0;
} }
} }
declare NewValue = \"\"; declare NewValue = \"\";
@ -209,6 +186,31 @@ EntryId = \"{$entryId}\";
return $scriptText; return $scriptText;
} }
/**
* Get the default Value
*
* @return string
*/
public function getDefault() {
if ($this->default) {
return $this->default;
}
if ($this->values) {
return reset($this->values);
}
return null;
}
/**
* Set the default Value
*
* @param string $default Default Value
* @return \FML\Script\Features\ValuePickerFeature
*/
public function setDefault($default) {
$this->default = (string)$default;
}
/** /**
* Build the Script Text for Label Clicks * Build the Script Text for Label Clicks
* *

View File

@ -123,6 +123,15 @@ class Setting {
return null; return null;
} }
/**
* Get whether the Setting has been persisted at some point
*
* @return bool
*/
public function isPersisted() {
return ($this->index > 0);
}
/** /**
* Get the Formatted Value of the Setting * Get the Formatted Value of the Setting
* *

View File

@ -100,20 +100,49 @@ class SettingManager implements CallbackListener {
return false; return false;
} }
if ($result) { if ($result) {
$this->storedSettings = array(); $this->clearStorage();
return true; return true;
} }
return false; return false;
} }
/** /**
* Get a Setting by its Index * Clear the Settings Storage
*/
public function clearStorage() {
$this->storedSettings = array();
}
/**
* @deprecated
* @see SettingManager::getSettingValueByIndex()
*/
public function getSettingByIndex($settingIndex, $defaultValue = null) {
return $this->getSettingValueByIndex($settingIndex, $defaultValue);
}
/**
* Get a Setting Value by its Index
* *
* @param int $settingIndex * @param int $settingIndex
* @param mixed $defaultValue * @param mixed $defaultValue
* @return mixed
*/
public function getSettingValueByIndex($settingIndex, $defaultValue = null) {
$setting = $this->getSettingObjectByIndex($settingIndex);
if (!$setting) {
return $defaultValue;
}
return $setting->value;
}
/**
* Get a Setting Object by its Index
*
* @param int $settingIndex
* @return Setting * @return Setting
*/ */
public function getSettingByIndex($settingIndex, $defaultValue = null) { public function getSettingObjectByIndex($settingIndex) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "` $settingQuery = "SELECT * FROM `" . self::TABLE_SETTINGS . "`
WHERE `index` = {$settingIndex};"; WHERE `index` = {$settingIndex};";
@ -123,44 +152,58 @@ class SettingManager implements CallbackListener {
return null; return null;
} }
if ($result->num_rows <= 0) { if ($result->num_rows <= 0) {
$result->close(); $result->free();
return $defaultValue; return null;
} }
/** @var Setting $setting */ /** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null)); $setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
$result->close(); $result->free();
$this->storeSetting($setting);
return $setting; return $setting;
} }
/**
* Store the given Setting
*
* @param Setting $setting
*/
private function storeSetting(Setting $setting) {
$this->storedSettings[$setting->class . $setting->setting] = $setting;
}
/** /**
* Set a Setting for the given Object * Set a Setting for the given Object
* *
* @param mixed $object * @param mixed $object
* @param string $settingName * @param string $settingName
* @param mixed $value * @param mixed $value
* @return Setting * @return bool
*/ */
public function setSetting($object, $settingName, $value) { public function setSetting($object, $settingName, $value) {
$className = ClassUtil::getClass($object);
$setting = $this->getSettingObject($object, $settingName); $setting = $this->getSettingObject($object, $settingName);
if ($setting) { if ($setting) {
$setting->value = $value; $setting->value = $value;
if (!$this->saveSetting($setting)) { $saved = $this->saveSetting($setting);
return null; if (!$saved) {
return false;
} }
} else { } else {
$setting = $this->initSetting($object, $settingName, $value); $saved = $this->initSetting($object, $settingName, $value);
if (!$saved) {
return false;
}
$setting = $this->getSettingObject($object, $settingName, $value);
} }
$this->storedSettings[$className . $settingName] = $setting; $this->storeSetting($setting);
// Trigger Settings Changed Callback // Trigger Settings Changed Callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SETTING_CHANGED, $setting); $this->maniaControl->callbackManager->triggerCallback(self::CB_SETTING_CHANGED, $setting);
return $setting; return true;
} }
/** /**
@ -175,8 +218,9 @@ class SettingManager implements CallbackListener {
$settingClass = ClassUtil::getClass($object); $settingClass = ClassUtil::getClass($object);
// Retrieve from Storage if possible // Retrieve from Storage if possible
if (isset($this->storedSettings[$settingClass . $settingName])) { $storedSetting = $this->getStoredSetting($object, $settingName);
return $this->storedSettings[$settingClass . $settingName]; if ($storedSetting) {
return $storedSetting;
} }
// Fetch setting // Fetch setting
@ -190,48 +234,71 @@ class SettingManager implements CallbackListener {
return null; return null;
} }
if ($result->num_rows <= 0) { if ($result->num_rows <= 0) {
$result->free();
if ($default === null) { if ($default === null) {
return null; return null;
} }
$setting = $this->initSetting($object, $settingName, $default); $saved = $this->initSetting($object, $settingName, $default);
return $setting; if ($saved) {
return $this->getSettingObject($object, $settingName, $default);
} else {
return null;
}
} }
/** @var Setting $setting */ /** @var Setting $setting */
$setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null)); $setting = $result->fetch_object(Setting::CLASS_NAME, array(false, null, null));
$result->free(); $result->free();
// Store setting $this->storeSetting($setting);
$this->storedSettings[$settingClass . $settingName] = $setting;
return $setting; return $setting;
} }
/**
* Retrieve a stored Setting
*
* @param mixed $settingClass
* @param string $settingName
* @return Setting
*/
private function getStoredSetting($settingClass, $settingName) {
$settingClass = ClassUtil::getClass($settingClass);
if (isset($this->storedSettings[$settingClass . $settingName])) {
return $this->storedSettings[$settingClass . $settingName];
}
return null;
}
/** /**
* Initialize a Setting for the given Object * Initialize a Setting for the given Object
* *
* @param mixed $object * @param mixed $object
* @param string $settingName * @param string $settingName
* @param mixed $defaultValue * @param mixed $defaultValue
* @return Setting * @return bool
*/ */
public function initSetting($object, $settingName, $defaultValue) { public function initSetting($object, $settingName, $defaultValue) {
$setting = new Setting($object, $settingName, $defaultValue); $setting = new Setting($object, $settingName, $defaultValue);
$saved = $this->saveSetting($setting); return $this->saveSetting($setting, true);
if ($saved) {
return $setting;
}
return null;
} }
/** /**
* Save the given Setting in the Database * Save the given Setting in the Database
* *
* @param Setting $setting * @param Setting $setting
* @param bool $init
* @return bool * @return bool
*/ */
public function saveSetting(Setting &$setting) { public function saveSetting(Setting $setting, $init = false) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
if ($init) {
// Init - Keep old value if the default didn't change
$valueUpdateString = '`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`))';
} else {
// Set - Update value in any case
$valueUpdateString = '`value` = VALUES(`value`)';
}
$settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` ( $settingQuery = "INSERT INTO `" . self::TABLE_SETTINGS . "` (
`class`, `class`,
`setting`, `setting`,
@ -249,7 +316,7 @@ class SettingManager implements CallbackListener {
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`index` = LAST_INSERT_ID(`index`), `index` = LAST_INSERT_ID(`index`),
`type` = VALUES(`type`), `type` = VALUES(`type`),
`value` = IF(`default` = VALUES(`default`), `value`, VALUES(`default`)), {$valueUpdateString},
`default` = VALUES(`default`), `default` = VALUES(`default`),
`set` = VALUES(`set`), `set` = VALUES(`set`),
`changed` = NOW();"; `changed` = NOW();";
@ -268,7 +335,6 @@ class SettingManager implements CallbackListener {
$settingStatement->close(); $settingStatement->close();
return false; return false;
} }
$setting->index = $settingStatement->insert_id;
$settingStatement->close(); $settingStatement->close();
return true; return true;
} }