2013-11-28 17:36:39 +01:00
|
|
|
<?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;
|
2013-11-28 17:36:39 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 14:50:28 +01:00
|
|
|
* Class offering a Configurator for Script Settings
|
2013-11-28 17:36:39 +01:00
|
|
|
*
|
|
|
|
* @author steeffeen & kremsy
|
|
|
|
*/
|
2013-12-31 13:51:30 +01:00
|
|
|
class ScriptSettings implements ConfiguratorMenu, CallbackListener {
|
2013-11-28 17:36:39 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2013-12-12 19:41:37 +01:00
|
|
|
const ACTION_PREFIX_SETTING = 'ScriptSetting.';
|
2013-12-31 14:50:28 +01:00
|
|
|
const ACTION_SETTING_BOOL = 'ScriptSetting.ActionBoolSetting.';
|
|
|
|
const CB_SCRIPTSETTING_CHANGED = 'ScriptSettings.SettingChanged';
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-11-28 17:36:39 +01:00
|
|
|
/**
|
2013-12-31 14:50:28 +01:00
|
|
|
* Private Properties
|
2013-11-28 17:36:39 +01:00
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 14:50:28 +01:00
|
|
|
* Create a new Script Settings Instance
|
2013-11-28 17:36:39 +01:00
|
|
|
*
|
2013-12-31 13:51:30 +01:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-11-28 17:36:39 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2013-12-31 13:51:30 +01:00
|
|
|
|
|
|
|
// Register for callbacks
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this,
|
|
|
|
'handleManialinkPageAnswer');
|
2013-11-28 17:36:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
|
|
|
|
*/
|
|
|
|
public function getTitle() {
|
2013-12-12 19:41:37 +01:00
|
|
|
return 'Script Settings';
|
2013-11-28 17:36:39 +01:00
|
|
|
}
|
2013-12-12 19:41:37 +01:00
|
|
|
|
2013-11-28 17:36:39 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
|
|
|
|
*/
|
2013-12-31 14:50:28 +01:00
|
|
|
public function getMenu($width, $height, Script $script) {
|
2013-11-28 17:36:39 +01:00
|
|
|
$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;
|
|
|
|
}
|
2013-11-28 17:36:39 +01:00
|
|
|
$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;
|
2013-11-28 17:36:39 +01:00
|
|
|
|
|
|
|
// Pagers
|
|
|
|
$pagerPrev = new Quad_Icons64x64_1();
|
|
|
|
$frame->add($pagerPrev);
|
2013-12-04 00:40:37 +01:00
|
|
|
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2);
|
2013-11-28 17:36:39 +01:00
|
|
|
$pagerPrev->setSize($pagerSize, $pagerSize);
|
|
|
|
$pagerPrev->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowPrev);
|
|
|
|
|
|
|
|
$pagerNext = new Quad_Icons64x64_1();
|
|
|
|
$frame->add($pagerNext);
|
2013-12-04 00:40:37 +01:00
|
|
|
$pagerNext->setPosition($width * 0.45, $height * -0.44, 2);
|
2013-11-28 17:36:39 +01:00
|
|
|
$pagerNext->setSize($pagerSize, $pagerSize);
|
|
|
|
$pagerNext->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowNext);
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-31 11:05:14 +01:00
|
|
|
$script->addPager($pagerPrev, -1);
|
|
|
|
$script->addPager($pagerNext, 1);
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-11-28 17:36:39 +01:00
|
|
|
$pageCountLabel = new Label();
|
|
|
|
$frame->add($pageCountLabel);
|
|
|
|
$pageCountLabel->setHAlign(Control::RIGHT);
|
2013-12-04 00:40:37 +01:00
|
|
|
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
|
2013-11-28 17:36:39 +01:00
|
|
|
$pageCountLabel->setStyle('TextTitle1');
|
|
|
|
$pageCountLabel->setTextSize(2);
|
|
|
|
|
2013-12-31 13:51:30 +01:00
|
|
|
$script->addPageLabel($pageCountLabel);
|
|
|
|
|
2013-11-28 17:36:39 +01:00
|
|
|
// Setting pages
|
|
|
|
$pageFrames = array();
|
2013-12-12 19:41:37 +01:00
|
|
|
$y = 0.;
|
2013-11-28 17:36:39 +01:00
|
|
|
foreach ($scriptParams as $index => $scriptParam) {
|
|
|
|
$settingName = $scriptParam['Name'];
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-11-28 17:36:39 +01:00
|
|
|
if (!isset($scriptSettings[$settingName])) continue;
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-11-28 17:36:39 +01:00
|
|
|
if (!isset($pageFrame)) {
|
|
|
|
$pageFrame = new Frame();
|
|
|
|
$frame->add($pageFrame);
|
2013-12-31 13:51:30 +01:00
|
|
|
if (!empty($pageFrames)) {
|
|
|
|
$pageFrame->setVisible(false);
|
|
|
|
}
|
2013-11-28 17:36:39 +01:00
|
|
|
array_push($pageFrames, $pageFrame);
|
2013-12-09 09:07:55 +01:00
|
|
|
$y = $height * 0.41;
|
2013-12-31 13:51:30 +01:00
|
|
|
$script->addPage($pageFrame, count($pageFrames));
|
2013-11-28 17:36:39 +01:00
|
|
|
}
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-09 09:07:55 +01:00
|
|
|
$settingFrame = new Frame();
|
|
|
|
$pageFrame->add($settingFrame);
|
|
|
|
$settingFrame->setY($y);
|
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
$nameLabel = new Label_Text();
|
2013-12-09 09:07:55 +01:00
|
|
|
$settingFrame->add($nameLabel);
|
2013-11-28 17:36:39 +01:00
|
|
|
$nameLabel->setHAlign(Control::LEFT);
|
2013-12-09 09:07:55 +01:00
|
|
|
$nameLabel->setX($width * -0.46);
|
2013-11-28 17:36:39 +01:00
|
|
|
$nameLabel->setSize($width * 0.4, $settingHeight);
|
2013-12-12 19:41:37 +01:00
|
|
|
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
|
2013-11-28 17:36:39 +01:00
|
|
|
$nameLabel->setTextSize($labelTextSize);
|
|
|
|
$nameLabel->setText($settingName);
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-09 09:07:55 +01:00
|
|
|
$settingValue = $scriptSettings[$settingName];
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-30 20:12:53 +01:00
|
|
|
$substyle = '';
|
2013-12-31 13:51:30 +01:00
|
|
|
if ($settingValue === false) {
|
2013-12-30 20:12:53 +01:00
|
|
|
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlRed;
|
2013-12-31 13:51:30 +01:00
|
|
|
}
|
|
|
|
else if ($settingValue === true) {
|
2013-12-30 20:12:53 +01:00
|
|
|
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlGreen;
|
|
|
|
}
|
2013-12-31 13:51:30 +01:00
|
|
|
|
|
|
|
if ($substyle != '') {
|
2013-12-30 20:12:53 +01:00
|
|
|
$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);
|
2013-12-31 14:50:28 +01:00
|
|
|
$quad->setAction(self::ACTION_SETTING_BOOL . $settingName);
|
2013-12-31 13:51:30 +01:00
|
|
|
}
|
|
|
|
else {
|
2013-12-30 20:12:53 +01:00
|
|
|
$entry = new Entry();
|
|
|
|
$settingFrame->add($entry);
|
|
|
|
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
|
|
|
|
$entry->setHAlign(Control::CENTER);
|
2013-12-31 13:51:30 +01:00
|
|
|
$entry->setX($width / 2 * 0.55);
|
2013-12-30 20:12:53 +01:00
|
|
|
$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-12 19:41:37 +01:00
|
|
|
|
2013-12-09 13:45:58 +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);
|
2013-12-09 13:45:58 +01:00
|
|
|
$descriptionLabel->setTranslate(true);
|
2013-12-31 13:51:30 +01:00
|
|
|
// $descriptionLabel->setTextPrefix('Desc: ');
|
2013-12-09 13:45:58 +01:00
|
|
|
$descriptionLabel->setText($scriptParam['Desc']);
|
2013-12-31 10:40:03 +01:00
|
|
|
$script->addTooltip($nameLabel, $descriptionLabel);
|
2013-11-28 17:36:39 +01:00
|
|
|
|
|
|
|
$y -= $settingHeight;
|
|
|
|
if ($index % $pageMaxCount == $pageMaxCount - 1) {
|
2013-12-31 11:05:14 +01:00
|
|
|
unset($pageFrame);
|
2013-11-28 17:36:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-31 14:50:28 +01:00
|
|
|
|
2013-12-30 19:09:42 +01:00
|
|
|
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
|
2013-12-31 13:51:30 +01:00
|
|
|
|
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-31 13:51:30 +01:00
|
|
|
|
2013-12-30 19:09:42 +01:00
|
|
|
$settingName = substr($setting['Name'], $prefixLength);
|
2013-12-31 14:50:28 +01:00
|
|
|
if (!isset($scriptSettings[$settingName])) {
|
|
|
|
var_dump('no setting ' . $settingName);
|
|
|
|
continue;
|
|
|
|
}
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-31 14:50:28 +01:00
|
|
|
if ($setting['Value'] == $scriptSettings[$settingName]) {
|
|
|
|
// Not changed
|
|
|
|
continue;
|
2013-12-30 18:56:42 +01:00
|
|
|
}
|
2013-12-31 14:50:28 +01:00
|
|
|
|
|
|
|
$newSettings[$settingName] = $setting['Value'];
|
|
|
|
settype($newSettings[$settingName], gettype($scriptSettings[$settingName]));
|
2013-12-30 19:09:42 +01:00
|
|
|
}
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-31 14:50:28 +01:00
|
|
|
$this->applyNewScriptSettings($newSettings, $player);
|
2013-12-30 20:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-31 14:50:28 +01:00
|
|
|
* Handle ManialinkPageAnswer Callback
|
2013-12-31 13:51:30 +01:00
|
|
|
*
|
2013-12-30 20:12:53 +01:00
|
|
|
* @param array $callback
|
|
|
|
*/
|
2013-12-31 13:51:30 +01:00
|
|
|
public function handleManialinkPageAnswer(array $callback) {
|
2013-12-30 20:12:53 +01:00
|
|
|
$actionId = $callback[1][2];
|
|
|
|
$boolSetting = (strpos($actionId, self::ACTION_SETTING_BOOL) === 0);
|
2013-12-31 13:51:30 +01:00
|
|
|
if (!$boolSetting) return;
|
|
|
|
|
2013-12-30 20:12:53 +01:00
|
|
|
$actionArray = explode(".", $actionId);
|
2013-12-31 14:50:28 +01:00
|
|
|
$setting = $actionArray[2];
|
|
|
|
|
|
|
|
$login = $callback[1][1];
|
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-31 14:50:28 +01:00
|
|
|
$this->toggleBooleanSetting($setting, $player);
|
2013-12-30 20:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-31 14:50:28 +01:00
|
|
|
* Toogle a Boolean Setting
|
2013-12-31 13:51:30 +01:00
|
|
|
*
|
2013-12-30 20:12:53 +01:00
|
|
|
* @param Player $player
|
2013-12-31 13:51:30 +01:00
|
|
|
* @param $setting
|
2013-12-30 20:12:53 +01:00
|
|
|
*/
|
2013-12-31 14:50:28 +01:00
|
|
|
public function toggleBooleanSetting($setting, Player $player) {
|
2013-12-30 20:12:53 +01:00
|
|
|
$this->maniaControl->client->query('GetModeScriptSettings');
|
|
|
|
$scriptSettings = $this->maniaControl->client->getResponse();
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-31 14:50:28 +01:00
|
|
|
if (!isset($scriptSettings[$setting])) {
|
|
|
|
var_dump('no setting ' . $setting);
|
|
|
|
return;
|
2013-12-30 20:12:53 +01:00
|
|
|
}
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-31 14:50:28 +01:00
|
|
|
$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);
|
2013-12-30 20:12:53 +01:00
|
|
|
if (!$success) {
|
|
|
|
$this->maniaControl->chat->sendError('Error occurred: ' . $this->maniaControl->getClientErrorText(), $player->login);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-31 13:51:30 +01:00
|
|
|
|
2013-12-31 14:50:28 +01:00
|
|
|
// 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) .
|
|
|
|
'$>!';
|
|
|
|
|
|
|
|
$this->maniaControl->chat->sendInformation($chatMessage);
|
|
|
|
$this->maniaControl->log(Formatter::stripCodes($chatMessage));
|
|
|
|
|
|
|
|
// Trigger own callback
|
|
|
|
$this->maniaControl->callbackManager->triggerCallback(self::CB_SCRIPTSETTING_CHANGED,
|
|
|
|
array(self::CB_SCRIPTSETTING_CHANGED, $setting, $value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2013-11-28 17:36:39 +01:00
|
|
|
}
|
|
|
|
}
|