TrackManiaControl/application/core/Configurators/ManiaControlSettings.php

299 lines
8.0 KiB
PHP
Raw Normal View History

2014-01-05 19:00:11 +01:00
<?php
/**
* Class offering a Configurator for Script Settings
*
* @author steeffeen & kremsy
*/
namespace ManiaControl\Configurators;
2014-01-05 19:20:54 +01:00
use FML\Controls\Control;
use FML\Controls\Entry;
2014-01-05 19:10:11 +01:00
use FML\Controls\Frame;
2014-01-05 19:20:54 +01:00
use FML\Controls\Label;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quads\Quad_Icons64x64_1;
2014-01-05 19:00:11 +01:00
use FML\Script\Script;
2014-01-05 22:51:21 +01:00
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
2014-01-05 19:00:11 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2014-01-05 22:51:21 +01:00
class ManiaControlSettings implements ConfiguratorMenu, CallbackListener {
2014-01-05 19:00:11 +01:00
/**
* Constants
*/
2014-01-05 22:51:21 +01:00
const TITLE = 'ManiaControl Settings';
const ACTION_PREFIX_SETTING = 'ManiaControlSettings';
const ACTION_SETTING_BOOL = 'ManiaControlSettings.ActionBoolSetting.';
2014-01-05 19:00:11 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
/**
* Create a new Script Settings Instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-05 22:51:21 +01:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
2014-01-05 19:00:11 +01:00
}
/**
* Get the Menu Title
*
* @return string
*/
public function getTitle() {
2014-01-05 19:20:54 +01:00
return self::TITLE;
2014-01-05 19:00:11 +01:00
}
/**
* Get the Configurator Menu Frame
*
* @param float $width
* @param float $height
* @param Script $script
* @return \FML\Controls\Frame
*/
public function getMenu($width, $height, Script $script) {
2014-01-05 19:20:54 +01:00
$pagesId = 'ManiaControlSettingsPages';
$frame = new Frame();
2014-01-05 19:10:11 +01:00
2014-01-05 19:20:54 +01:00
// Config
$pagerSize = 9.;
2014-01-05 19:20:54 +01:00
$settingHeight = 5.;
$labelTextSize = 2;
$pageMaxCount = 13;
2014-01-05 19:20:54 +01:00
//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);
$script->addPager($pagerPrev, -1, $pagesId);
$script->addPager($pagerNext, 1, $pagesId);
$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);
$script->addPageLabel($pageCountLabel, $pagesId);
2014-01-05 19:10:11 +01:00
/** @var ManiaControl/SettingManager $this->maniaControl->settingManager */
$settings = $this->maniaControl->settingManager->getSettings();
$pluginClasses = $this->maniaControl->pluginManager->getPluginClasses();
2014-01-05 19:20:54 +01:00
$pageFrames = array();
$y = 0;
$index = 1;
$prevClass = '';
foreach($settings as $id => $setting) {
//Don't display Plugin Settings
if(array_search($setting->class, $pluginClasses) !== FALSE) {
continue;
}
2014-01-05 19:20:54 +01:00
if(!isset($pageFrame)) {
2014-01-05 19:20:54 +01:00
$pageFrame = new Frame();
$frame->add($pageFrame);
if(!empty($pageFrames)) {
2014-01-05 19:20:54 +01:00
$pageFrame->setVisible(false);
}
array_push($pageFrames, $pageFrame);
$y = $height * 0.41;
$script->addPage($pageFrame, count($pageFrames), $pagesId);
}
$settingFrame = new Frame();
$pageFrame->add($settingFrame);
$settingFrame->setY($y);
//Headline Label
if($prevClass != $setting->class) {
$headLabel = new Label_Text();
$settingFrame->add($headLabel);
$headLabel->setHAlign(Control::LEFT);
$headLabel->setX($width * -0.46);
$headLabel->setSize($width * 0.6, $settingHeight);
$headLabel->setStyle($headLabel::STYLE_TextCardSmall);
$headLabel->setTextSize($labelTextSize);
$headLabel->setText($setting->class);
$headLabel->setTextColor("F00");
$y -= $settingHeight;
if($index % $pageMaxCount == $pageMaxCount - 1) {
$pageFrame = new Frame();
$frame->add($pageFrame);
if(!empty($pageFrames)) {
$pageFrame->setVisible(false);
}
array_push($pageFrames, $pageFrame);
$y = $height * 0.41;
$script->addPage($pageFrame, count($pageFrames), $pagesId);
}
$index++;
$settingFrame = new Frame();
$pageFrame->add($settingFrame);
$settingFrame->setY($y);
} //Headline Label finished
2014-01-05 19:20:54 +01:00
$nameLabel = new Label_Text();
$settingFrame->add($nameLabel);
$nameLabel->setHAlign(Control::LEFT);
$nameLabel->setX($width * -0.46);
$nameLabel->setSize($width * 0.6, $settingHeight);
2014-01-05 19:20:54 +01:00
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setTextSize($labelTextSize);
$nameLabel->setText($setting->setting);
$nameLabel->setTextColor("FFF");
$substyle = '';
$entry = new Entry();
$settingFrame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setHAlign(Control::CENTER);
$entry->setX($width / 2 * 0.65);
$entry->setTextSize(1);
$entry->setSize($width * 0.3, $settingHeight * 0.9);
$entry->setName(self::ACTION_PREFIX_SETTING . '.' . $setting->index);
$entry->setDefault($setting->value);
if($setting->type == "bool") {
if($setting->value == "0") {
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlRed;
} else if($setting->value == "1") {
$substyle = Quad_Icons64x64_1::SUBSTYLE_LvlGreen;
}
$quad = new Quad_Icons64x64_1();
$settingFrame->add($quad);
$quad->setX($width / 2 * 0.6);
$quad->setZ(-0.01);
$quad->setSubStyle($substyle);
$quad->setSize(4, 4);
$quad->setHAlign(Control::CENTER2);
$quad->setAction(self::ACTION_SETTING_BOOL . $setting->index);
$entry->setVisible(false);
}
2014-01-05 19:20:54 +01:00
$prevClass = $setting->class;
2014-01-05 19:10:11 +01:00
2014-01-05 19:20:54 +01:00
$y -= $settingHeight;
if($index % $pageMaxCount == $pageMaxCount - 1) {
2014-01-05 19:20:54 +01:00
unset($pageFrame);
}
2014-01-05 19:10:11 +01:00
2014-01-05 19:20:54 +01:00
$index++;
2014-01-05 19:10:11 +01:00
}
return $frame;
2014-01-05 19:00:11 +01:00
}
/**
* Save the Config Data
*
* @param array $configData
* @param Player $player
*/
public function saveConfigData(array $configData, Player $player) {
$prefix = explode(".", $configData[3][0]['Name']);
if($prefix[0] != self::ACTION_PREFIX_SETTING) {
return;
}
$maniaControlSettings = $this->maniaControl->settingManager->getSettings();
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
foreach($configData[3] as $setting) {
$settingName = substr($setting['Name'], $prefixLength + 1);
$oldSetting = $maniaControlSettings[$settingName];
if($setting['Value'] == $oldSetting->value || $oldSetting->type == 'bool') {
continue;
}
$this->maniaControl->settingManager->updateSetting($oldSetting->class, $oldSetting->setting, $setting['Value']);
}
//Reopen the Menu
$menuId = $this->maniaControl->configurator->getMenuId($this->getTitle());
2014-01-18 21:05:45 +01:00
$this->maniaControl->configurator->reopenMenu($player, $menuId);
2014-01-05 19:00:11 +01:00
}
2014-01-05 22:51:21 +01:00
/**
* 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];
$login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
// Toggle the Boolean Setting
$this->toggleBooleanSetting($setting, $player);
// Save all Changes
$this->saveConfigData($callback[1], $player);
}
/**
* Toggles a Boolean Value
*
* @param $setting
* @param Player $player
*/
public function toggleBooleanSetting($setting, Player $player) {
$oldSetting = $this->maniaControl->settingManager->getSettingByIndex($setting);
if(!isset($oldSetting)) {
var_dump('no setting ' . $setting);
return;
}
//Toggle value
if($oldSetting->value == "1") {
$this->maniaControl->settingManager->updateSetting($oldSetting->class, $oldSetting->setting, "0");
} else {
$this->maniaControl->settingManager->updateSetting($oldSetting->class, $oldSetting->setting, "1");
}
}
2014-01-05 19:00:11 +01:00
}