TrackManiaControl/application/core/Configurators/ScriptSettings.php

302 lines
9.3 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Configurators;
2013-12-31 10:40:03 +01:00
use FML\Script\Script;
2013-12-30 20:12:53 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
2013-12-30 19:09:42 +01:00
use ManiaControl\Formatter;
use ManiaControl\ManiaControl;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Script\Pages;
use FML\Script\Tooltips;
use FML\Controls\Control;
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Entry;
use ManiaControl\Players\Player;
/**
* Class offering a configurator for current script settings
*
* @author steeffeen & kremsy
*/
2013-12-30 20:12:53 +01:00
class ScriptSettings implements ConfiguratorMenu,CallbackListener {
/**
* Constants
*/
const ACTION_PREFIX_SETTING = 'ScriptSetting.';
2013-12-30 20:12:53 +01:00
const ACTION_SETTING_BOOL = 'ScriptSetting.ActionBoolSetting';
const CB_SCRIPTSETTINGS_CHANGED = 'ScriptSettings.SettingsChanged';
/**
* Private properties
*/
private $maniaControl = null;
/**
* Create a new script settings instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2013-12-30 20:12:53 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
}
/**
*
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
*/
public function getTitle() {
return 'Script Settings';
}
/**
*
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
*/
2013-12-31 10:40:03 +01:00
public function getMenu($width, $height, $pages, Script $script) {
$frame = new Frame();
$this->maniaControl->client->query('GetModeScriptInfo');
$scriptInfo = $this->maniaControl->client->getResponse();
2013-12-14 23:31:19 +01:00
if (isset($scriptInfo['faultCode'])) {
// Not in script mode
$label = new Label();
$frame->add($label);
$label->setText($scriptInfo['faultString']);
return $frame;
}
$scriptParams = $scriptInfo['ParamDescs'];
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
// Config
$pagerSize = 9.;
$settingHeight = 5.;
$labelTextSize = 2;
2013-12-09 09:07:55 +01:00
$pageMaxCount = 13;
// Pagers
$pagerPrev = new Quad_Icons64x64_1();
$frame->add($pagerPrev);
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2);
$pagerPrev->setSize($pagerSize, $pagerSize);
$pagerPrev->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowPrev);
$pagerNext = new Quad_Icons64x64_1();
$frame->add($pagerNext);
$pagerNext->setPosition($width * 0.45, $height * -0.44, 2);
$pagerNext->setSize($pagerSize, $pagerSize);
$pagerNext->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowNext);
2013-12-31 11:05:14 +01:00
$script->addPager($pagerPrev, -1);
$script->addPager($pagerNext, 1);
$pageCountLabel = new Label();
$frame->add($pageCountLabel);
$pageCountLabel->setHAlign(Control::RIGHT);
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
$pageCountLabel->setStyle('TextTitle1');
$pageCountLabel->setTextSize(2);
// Setting pages
$pageFrames = array();
$y = 0.;
foreach ($scriptParams as $index => $scriptParam) {
$settingName = $scriptParam['Name'];
2013-12-30 20:12:53 +01:00
if (!isset($scriptSettings[$settingName])) continue;
2013-12-30 20:12:53 +01:00
if (!isset($pageFrame)) {
$pageFrame = new Frame();
$frame->add($pageFrame);
array_push($pageFrames, $pageFrame);
2013-12-09 09:07:55 +01:00
$y = $height * 0.41;
}
2013-12-30 20:12:53 +01:00
2013-12-09 09:07:55 +01:00
$settingFrame = new Frame();
$pageFrame->add($settingFrame);
$settingFrame->setY($y);
$nameLabel = new Label_Text();
2013-12-09 09:07:55 +01:00
$settingFrame->add($nameLabel);
$nameLabel->setHAlign(Control::LEFT);
2013-12-09 09:07:55 +01:00
$nameLabel->setX($width * -0.46);
$nameLabel->setSize($width * 0.4, $settingHeight);
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setTextSize($labelTextSize);
$nameLabel->setText($settingName);
2013-12-30 20:12:53 +01:00
2013-12-09 09:07:55 +01:00
$settingValue = $scriptSettings[$settingName];
2013-12-30 20:12:53 +01:00
$substyle = '';
if($settingValue === false){
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlRed;
}else if($settingValue === true){
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlGreen;
}
if($substyle != ''){
$quad = new Quad_Icons64x64_1();
$settingFrame->add($quad);
$quad->setX($width / 2 * 0.545);
$quad->setZ(-0.01);
$quad->setSubStyle($substyle);
$quad->setSize(4, 4);
$quad->setHAlign(Control::CENTER);
$quad->setAction(self::ACTION_SETTING_BOOL . "." . $settingName);
}else{
$entry = new Entry();
$settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setHAlign(Control::CENTER);
$entry->setX($width /2 * 0.55);
$entry->setTextSize(1);
$entry->setSize($width * 0.3, $settingHeight * 0.9);
$entry->setName(self::ACTION_PREFIX_SETTING . $settingName);
$entry->setDefault($settingValue);
2013-12-09 09:07:55 +01:00
}
2013-12-30 20:12:53 +01:00
$descriptionLabel = new Label();
$pageFrame->add($descriptionLabel);
$descriptionLabel->setHAlign(Control::LEFT);
$descriptionLabel->setPosition($width * -0.45, $height * -0.44);
$descriptionLabel->setSize($width * 0.7, $settingHeight);
2013-12-30 23:53:42 +01:00
$descriptionLabel->setTextSize($labelTextSize);
$descriptionLabel->setTranslate(true);
2013-12-30 23:53:42 +01:00
//$descriptionLabel->setTextPrefix('Desc: ');
$descriptionLabel->setText($scriptParam['Desc']);
2013-12-31 10:40:03 +01:00
$script->addTooltip($nameLabel, $descriptionLabel);
$y -= $settingHeight;
if ($index % $pageMaxCount == $pageMaxCount - 1) {
2013-12-31 10:40:03 +01:00
$script->addPage($pageFrame, $index, "test"); //TODO not working
2013-12-31 11:05:14 +01:00
unset($pageFrame);
}
}
2013-12-31 10:40:03 +01:00
//$pages->add(array(-1 => $pagerPrev, 1 => $pagerNext), $pageFrames, $pageCountLabel);
return $frame;
}
/**
*
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
*/
public function saveConfigData(array $configData, Player $player) {
$this->maniaControl->client->query('GetModeScriptSettings');
$scriptSettings = $this->maniaControl->client->getResponse();
2013-12-30 19:09:42 +01:00
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
2013-12-30 18:56:42 +01:00
2013-12-30 19:09:42 +01:00
$chatMessage = '';
2013-12-30 18:56:42 +01:00
$newSettings = array();
2013-12-30 19:09:42 +01:00
foreach ($configData[3] as $setting) {
if (substr($setting['Name'], 0, $prefixLength) != self::ACTION_PREFIX_SETTING) continue;
2013-12-30 18:56:42 +01:00
2013-12-30 19:09:42 +01:00
$settingName = substr($setting['Name'], $prefixLength);
2013-12-30 18:56:42 +01:00
foreach($scriptSettings as $key => $value){
if($key == $settingName){
2013-12-30 19:09:42 +01:00
//Check if something has been changed
if($setting["Value"] != $value){
$chatMessage .= '$FFF'.$settingName.'$z$s$FF0 to $FFF' . $setting["Value"].', ';
}
2013-12-30 18:56:42 +01:00
//Setting found, cast type, break the inner loop
2013-12-30 19:09:42 +01:00
settype($setting["Value"], gettype($value));
2013-12-30 18:56:42 +01:00
break;
}
}
2013-12-30 19:09:42 +01:00
$newSettings[$settingName] = $setting["Value"];
}
2013-12-31 10:05:38 +01:00
//Nothing has been changed
if($chatMessage = ''){
return;
}
2013-12-30 19:09:42 +01:00
$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);
2013-12-31 10:05:38 +01:00
$this->maniaControl->chat->sendInformation('$ff0' . $title . ' $<' . $player->nickname . '$> set Scriptsettings $<' . $chatMessage . '$>!');
2013-12-30 18:56:42 +01:00
2013-12-30 19:09:42 +01:00
// log console message
$this->maniaControl->log(Formatter::stripCodes($title . ' ' . $player->nickname . ' set Scriptsettings ' . $chatMessage . '!'));
2013-12-30 20:12:53 +01:00
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTINGS_CHANGED, array(self::CB_SCRIPTSETTINGS_CHANGED));
}
/**
* Called on ManialinkPageAnswer
* @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);
$player = $this->maniaControl->playerManager->getPlayer($callback[1][1]);
$this->setCheckboxSetting($player, $actionArray[2]);
}
/**
* Toogle a boolean value setting
* @param Player $player
* @param $setting
*/
public function setCheckboxSetting(Player $player, $setting){
$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;
}
}
$success = $this->maniaControl->client->query('SetModeScriptSettings', $newSetting);
if (!$success) {
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
return;
}
$valString = ($newSetting[$setting]) ? 'true' : 'false';
$chatMessage = '$FFF'.$setting.'$z$s$FF0 to $FFF' . $valString;
$chatMessage = str_replace("S_","",$chatMessage);
$title = $this->maniaControl->authenticationManager->getAuthLevelName($player->authLevel);
$this->maniaControl->chat->sendInformation('$ff0' . $title . ' $<' . $player->nickname . '$> set Scriptsetting $<' . $chatMessage . '$>!');
2013-12-30 20:16:37 +01:00
// log console message
$this->maniaControl->log(Formatter::stripCodes($title . ' ' . $player->nickname . ' set Scriptsetting ' . $chatMessage . '!'));
2013-12-30 20:12:53 +01:00
// Trigger own callback
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTINGS_CHANGED, array(self::CB_SCRIPTSETTINGS_CHANGED));
}
}