Improved Script Settings and Configurator Menu

This commit is contained in:
Steffen Schröder 2013-12-31 14:50:28 +01:00
parent 5fb76e2428
commit 98f70cb70b
4 changed files with 95 additions and 106 deletions

View File

@ -79,7 +79,7 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this,
'handlePlayerDisconnect');
$this->maniaControl->callbackManager->registerCallbackListener(ScriptSettings::CB_SCRIPTSETTINGS_CHANGED, $this, 'reopenMenu');
$this->maniaControl->callbackManager->registerCallbackListener(ScriptSettings::CB_SCRIPTSETTING_CHANGED, $this, 'reopenMenu');
// Create script settings
$this->scriptSettings = new ScriptSettings($maniaControl);
@ -242,14 +242,6 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
$script = new Script();
$manialink->setScript($script);
$pages = null;
/*
* $pages = new Pages(); $script->addFeature($pages); $tooltips = new Tooltips(); $script->addFeature($tooltips); $menus = new
* Menus(); $script->addFeature($menus);
*/
// $script->addPager()
$menuRelationships = array();
$menuItemY = $menuHeight * 0.42;
foreach ($this->menus as $index => $menu) {
@ -262,20 +254,16 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
// $menuItemLabel->setText('$z' . $menu->getTitle() . '$z');
// Add menu
$menuControl = $menu->getMenu($subMenuWidth, $subMenuHeight, $pages, $script);
$menuControl = $menu->getMenu($subMenuWidth, $subMenuHeight, $script);
if ($index > 0) {
$menuControl->setVisible(false);
}
$menusFrame->add($menuControl);
// Add menu relationship
// array_push($menuRelationships, array($menuItemLabel, $menuControl));
$script->addMenu($menuItemLabel, $menuControl);
$menuItemY -= $menuItemHeight * 1.1;
}
// $menus->add($menuRelationships);
// Add Close Quad (X)
$closeQuad = new Quad_Icons64x64_1();
$frame->add($closeQuad);

View File

@ -8,33 +8,31 @@ use FML\Script\Tooltips;
use ManiaControl\Players\Player;
/**
* Interface for configurator menus
* Interface for Configurator Menus
*
* @author steeffeen & kremsy
*/
interface ConfiguratorMenu {
/**
* Get the menu title
* Get the Menu Title
*
* @return string
*/
public function getTitle();
/**
* Get the configurator menu
* Get the Configurator Menu Frame
*
* @param float $width
* @param float $height
* @param $pages temp removed
* @param \FML\Script\Script $script
* @internal param \FML\Script\Tooltips $tooltips
* @return \FML\Controls\Control
* @param float $width
* @param float $height
* @param Script $script
* @return \FML\Controls\Frame
*/
public function getMenu($width, $height, $pages, Script $script);
public function getMenu($width, $height, Script $script);
/**
* Save the config data
* Save the Config Data
*
* @param array $configData
* @param Player $player

View File

@ -18,7 +18,7 @@ use FML\Controls\Entry;
use ManiaControl\Players\Player;
/**
* Class offering a configurator for current script settings
* Class offering a Configurator for Script Settings
*
* @author steeffeen & kremsy
*/
@ -27,16 +27,16 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
* Constants
*/
const ACTION_PREFIX_SETTING = 'ScriptSetting.';
const ACTION_SETTING_BOOL = 'ScriptSetting.ActionBoolSetting';
const CB_SCRIPTSETTINGS_CHANGED = 'ScriptSettings.SettingsChanged';
const ACTION_SETTING_BOOL = 'ScriptSetting.ActionBoolSetting.';
const CB_SCRIPTSETTING_CHANGED = 'ScriptSettings.SettingChanged';
/**
* Private properties
* Private Properties
*/
private $maniaControl = null;
/**
* Create a new script settings instance
* Create a new Script Settings Instance
*
* @param ManiaControl $maniaControl
*/
@ -60,7 +60,7 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
*
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
*/
public function getMenu($width, $height, $pages, Script $script) {
public function getMenu($width, $height, Script $script) {
$frame = new Frame();
$this->maniaControl->client->query('GetModeScriptInfo');
@ -158,7 +158,7 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$quad->setSubStyle($substyle);
$quad->setSize(4, 4);
$quad->setHAlign(Control::CENTER);
$quad->setAction(self::ACTION_SETTING_BOOL . "." . $settingName);
$quad->setAction(self::ACTION_SETTING_BOOL . $settingName);
}
else {
$entry = new Entry();
@ -185,7 +185,6 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
$y -= $settingHeight;
if ($index % $pageMaxCount == $pageMaxCount - 1) {
$script->addPage($pageFrame, $index, "test"); // TODO not working
unset($pageFrame);
}
}
@ -200,107 +199,111 @@ class ScriptSettings implements ConfiguratorMenu, CallbackListener {
public function saveConfigData(array $configData, Player $player) {
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
$chatMessage = '';
$newSettings = array();
foreach ($configData[3] as $setting) {
if (substr($setting['Name'], 0, $prefixLength) != self::ACTION_PREFIX_SETTING) continue;
$settingName = substr($setting['Name'], $prefixLength);
foreach ($scriptSettings as $key => $value) {
if ($key == $settingName) {
// Check if something has been changed
if ($setting["Value"] != $value) {
$chatMessage .= '$FFF' . $settingName . '$z$s$FF0 to $FFF' . $setting["Value"] . ', ';
}
// Setting found, cast type, break the inner loop
settype($setting["Value"], gettype($value));
break;
}
if (!isset($scriptSettings[$settingName])) {
var_dump('no setting ' . $settingName);
continue;
}
$newSettings[$settingName] = $setting["Value"];
if ($setting['Value'] == $scriptSettings[$settingName]) {
// Not changed
continue;
}
$newSettings[$settingName] = $setting['Value'];
settype($newSettings[$settingName], gettype($scriptSettings[$settingName]));
}
// Nothing has been changed
if ($chatMessage = '') {
return;
}
$success = $this->maniaControl->client->query('SetModeScriptSettings', $newSettings);
if (!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
return;
}
$chatMessage = substr($chatMessage, 0, strlen($chatMessage) - 2);
$chatMessage = str_replace("S_", "", $chatMessage);
$title = $this->maniaControl->authenticationManager->getAuthLevelName($player->authLevel);
$this->maniaControl->chat->sendInformation(
'$ff0' . $title . ' $<' . $player->nickname . '$> set Scriptsettings $<' . $chatMessage . '$>!');
// log console message
$this->maniaControl->log(Formatter::stripCodes($title . ' ' . $player->nickname . ' set Scriptsettings ' . $chatMessage . '!'));
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTINGS_CHANGED, array(self::CB_SCRIPTSETTINGS_CHANGED));
$this->applyNewScriptSettings($newSettings, $player);
}
/**
* Called on ManialinkPageAnswer
* Handle ManialinkPageAnswer Callback
*
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2];
$boolSetting = (strpos($actionId, self::ACTION_SETTING_BOOL) === 0);
if (!$boolSetting) return;
$actionArray = explode(".", $actionId);
$setting = $actionArray[2];
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1]);
$this->setCheckboxSetting($player, $actionArray[2]);
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
$this->toggleBooleanSetting($setting, $player);
}
/**
* Toogle a boolean value setting
* Toogle a Boolean Setting
*
* @param Player $player
* @param $setting
*/
public function setCheckboxSetting(Player $player, $setting) {
public function toggleBooleanSetting($setting, Player $player) {
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
$newSetting = array();
foreach ($scriptSettings as $key => $value) {
if ($key == $setting) { // Setting found
$newSetting[$key] = $value == true ? false : true; // toggle setting
break;
}
if (!isset($scriptSettings[$setting])) {
var_dump('no setting ' . $setting);
return;
}
$success = $this->maniaControl->client->query('SetModeScriptSettings', $newSetting);
$newSettings = array();
$newSettings[$setting] = ($scriptSettings[$setting] ? false : true);
$this->applyNewScriptSettings($newSettings, $player);
}
/**
* Apply the Array of new Script Settings
*
* @param array $newSettings
* @param Player $player
*/
private function applyNewScriptSettings(array $newSettings, Player $player) {
if (!$newSettings) return;
$success = $this->maniaControl->client->query('SetModeScriptSettings', $newSettings);
if (!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
return;
}
$valString = ($newSetting[$setting]) ? 'true' : 'false';
$chatMessage = '$FFF' . preg_replace('/^S_/', '', $setting) . '$z$s$FF0 to $FFF' . $valString;
// Notifications
foreach ($newSettings as $setting => $value) {
$title = $this->maniaControl->authenticationManager->getAuthLevelName($player->authLevel);
$chatMessage = '$ff0' . $title . ' $<' . $player->nickname . '$> set ScriptSetting ';
$chatMessage .= '$<' . '$fff' . preg_replace('/^S_/', '', $setting) . '$z$s$ff0 to $fff' . $this->parseSettingValue($value) .
'$>!';
$title = $this->maniaControl->authenticationManager->getAuthLevelName($player->authLevel);
$this->maniaControl->chat->sendInformation(
'$ff0' . $title . ' $<' . $player->nickname . '$> set ScriptSetting $<' . $chatMessage . '$>!');
$this->maniaControl->chat->sendInformation($chatMessage);
$this->maniaControl->log(Formatter::stripCodes($chatMessage));
// log console message
$this->maniaControl->log(Formatter::stripCodes($title . ' ' . $player->nickname . ' set Scriptsetting ' . $chatMessage . '!'));
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTING_CHANGED,
array(self::CB_SCRIPTSETTING_CHANGED, $setting, $value));
}
}
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTINGS_CHANGED, array(self::CB_SCRIPTSETTINGS_CHANGED));
/**
* Parse the Setting Value to a String Representation
*
* @param mixed $value
* @return string
*/
private function parseSettingValue($value) {
if (is_bool($value)) {
return ($value ? 'True' : 'False');
}
return (string) $value;
}
}

View File

@ -58,7 +58,7 @@ class PluginMenu implements CallbackListener, ConfiguratorMenu {
*
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
*/
public function getMenu($width, $height, $pages, Script $script) {
public function getMenu($width, $height, Script $script) {
$frame = new Frame();
$pluginClasses = $this->maniaControl->pluginManager->getPluginClasses();
@ -163,9 +163,9 @@ class PluginMenu implements CallbackListener, ConfiguratorMenu {
}
}
// $script->addPager($pageFrames, 1 , $pageCountLabel);
// $script->addPage(array(-1 => $pagerPrev, 1 => $pagerNext), $pageFrames, $pageCountLabel);
//$pages->add(array(-1 => $pagerPrev, 1 => $pagerNext), $pageFrames, $pageCountLabel);
// $script->addPager($pageFrames, 1 , $pageCountLabel);
// $script->addPage(array(-1 => $pagerPrev, 1 => $pagerNext), $pageFrames, $pageCountLabel);
// $pages->add(array(-1 => $pagerPrev, 1 => $pagerNext), $pageFrames, $pageCountLabel);
return $frame;
}