2013-12-04 00:40:37 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl\Plugins;
|
|
|
|
|
2014-01-05 20:42:16 +01:00
|
|
|
use FML\Controls\Control;
|
2013-12-04 00:40:37 +01:00
|
|
|
use FML\Controls\Frame;
|
2013-12-09 20:31:02 +01:00
|
|
|
use FML\Controls\Label;
|
2014-01-05 20:42:16 +01:00
|
|
|
use FML\Controls\Labels\Label_Button;
|
2013-12-09 20:31:02 +01:00
|
|
|
use FML\Controls\Labels\Label_Text;
|
2013-12-12 19:41:37 +01:00
|
|
|
use FML\Controls\Quads\Quad_Icons64x64_1;
|
2014-01-05 20:42:16 +01:00
|
|
|
use FML\Script\Pages;
|
|
|
|
use FML\Script\Script;
|
|
|
|
use FML\Script\Tooltips;
|
2013-12-12 19:41:37 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
2014-01-05 20:42:16 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
|
|
|
use ManiaControl\Configurators\ConfiguratorMenu;
|
2013-12-31 15:09:08 +01:00
|
|
|
use ManiaControl\Formatter;
|
2014-01-05 20:42:16 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
use ManiaControl\Players\Player;
|
2013-12-04 00:40:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Configurator for enabling and disabling plugins
|
|
|
|
*
|
|
|
|
* @author steeffeen
|
|
|
|
*/
|
2013-12-12 19:41:37 +01:00
|
|
|
class PluginMenu implements CallbackListener, ConfiguratorMenu {
|
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-01-05 20:42:16 +01:00
|
|
|
const ACTION_PREFIX_ENABLEPLUGIN = 'PluginMenu.Enable.';
|
2013-12-12 19:41:37 +01:00
|
|
|
const ACTION_PREFIX_DISABLEPLUGIN = 'PluginMenu.Disable.';
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-04 00:40:37 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
|
|
|
private $maniaControl = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new plugin menu instance
|
|
|
|
*
|
2013-12-31 14:50:28 +01:00
|
|
|
* @param ManiaControl $maniaControl
|
2013-12-04 00:40:37 +01:00
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2014-01-05 20:42:16 +01:00
|
|
|
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
|
2013-12-04 00:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
|
|
|
|
*/
|
|
|
|
public function getTitle() {
|
2013-12-12 19:41:37 +01:00
|
|
|
return 'Plugins';
|
2013-12-04 00:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
|
|
|
|
*/
|
2013-12-31 14:50:28 +01:00
|
|
|
public function getMenu($width, $height, Script $script) {
|
2013-12-31 15:09:08 +01:00
|
|
|
$pagesId = 'PluginPages';
|
2014-01-05 20:42:16 +01:00
|
|
|
$frame = new Frame();
|
|
|
|
|
2013-12-09 20:31:02 +01:00
|
|
|
$pluginClasses = $this->maniaControl->pluginManager->getPluginClasses();
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
// Config
|
2014-01-05 20:42:16 +01:00
|
|
|
$pagerSize = 9.;
|
|
|
|
$entryHeight = 5.;
|
2013-12-09 20:31:02 +01:00
|
|
|
$labelTextSize = 2;
|
2014-01-05 20:42:16 +01:00
|
|
|
$pageMaxCount = 10;
|
|
|
|
|
2013-12-12 19:41:37 +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);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
$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);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-31 15:09:08 +01:00
|
|
|
$script->addPager($pagerPrev, -1, $pagesId);
|
|
|
|
$script->addPager($pagerNext, 1, $pagesId);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
$pageCountLabel = new Label_Text();
|
|
|
|
$frame->add($pageCountLabel);
|
|
|
|
$pageCountLabel->setHAlign(Control::RIGHT);
|
|
|
|
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
|
|
|
|
$pageCountLabel->setStyle($pageCountLabel::STYLE_TextTitle1);
|
|
|
|
$pageCountLabel->setTextSize(2);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-31 15:09:08 +01:00
|
|
|
$script->addPageLabel($pageCountLabel, $pagesId);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
// Plugin pages
|
2013-12-09 20:31:02 +01:00
|
|
|
$pageFrames = array();
|
2014-01-05 20:42:16 +01:00
|
|
|
$y = 0.;
|
|
|
|
foreach($pluginClasses as $index => $pluginClass) {
|
|
|
|
if(!isset($pageFrame)) {
|
2013-12-09 20:31:02 +01:00
|
|
|
$pageFrame = new Frame();
|
|
|
|
$frame->add($pageFrame);
|
2014-01-05 20:42:16 +01:00
|
|
|
if(!empty($pageFrames)) {
|
2013-12-31 15:09:08 +01:00
|
|
|
$pageFrame->setVisible(false);
|
|
|
|
}
|
2013-12-09 20:31:02 +01:00
|
|
|
array_push($pageFrames, $pageFrame);
|
2013-12-31 15:09:08 +01:00
|
|
|
$script->addPage($pageFrame, count($pageFrames), $pagesId);
|
2013-12-09 20:31:02 +01:00
|
|
|
$y = $height * 0.41;
|
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-14 23:33:23 +01:00
|
|
|
$active = $this->maniaControl->pluginManager->isPluginActive($pluginClass);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
$pluginFrame = new Frame();
|
|
|
|
$pageFrame->add($pluginFrame);
|
|
|
|
$pluginFrame->setY($y);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
$activeQuad = new Quad_Icons64x64_1();
|
|
|
|
$pluginFrame->add($activeQuad);
|
|
|
|
$activeQuad->setPosition($width * -0.45, -0.1, 1);
|
|
|
|
$activeQuad->setSize($entryHeight * 0.9, $entryHeight * 0.9);
|
2014-01-05 20:42:16 +01:00
|
|
|
if($active) {
|
2013-12-12 19:41:37 +01:00
|
|
|
$activeQuad->setSubStyle($activeQuad::SUBSTYLE_LvlGreen);
|
2014-01-05 20:42:16 +01:00
|
|
|
} else {
|
2013-12-12 19:41:37 +01:00
|
|
|
$activeQuad->setSubStyle($activeQuad::SUBSTYLE_LvlRed);
|
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
$nameLabel = new Label_Text();
|
|
|
|
$pluginFrame->add($nameLabel);
|
2013-12-09 20:31:02 +01:00
|
|
|
$nameLabel->setHAlign(Control::LEFT);
|
|
|
|
$nameLabel->setX($width * -0.4);
|
2013-12-12 19:41:37 +01:00
|
|
|
$nameLabel->setSize($width * 0.5, $entryHeight);
|
|
|
|
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
|
2013-12-09 20:31:02 +01:00
|
|
|
$nameLabel->setTextSize($labelTextSize);
|
2013-12-12 19:41:37 +01:00
|
|
|
$nameLabel->setText($pluginClass::getName());
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-09 20:31:02 +01:00
|
|
|
$descriptionLabel = new Label();
|
|
|
|
$pageFrame->add($descriptionLabel);
|
2013-12-12 19:41:37 +01:00
|
|
|
$descriptionLabel->setAlign(Control::LEFT, Control::TOP);
|
|
|
|
$descriptionLabel->setPosition($width * -0.45, $height * -0.22);
|
|
|
|
$descriptionLabel->setSize($width * 0.7, $entryHeight);
|
|
|
|
$descriptionLabel->setTextSize(2);
|
2013-12-09 20:31:02 +01:00
|
|
|
$descriptionLabel->setTranslate(true);
|
2013-12-12 19:41:37 +01:00
|
|
|
$descriptionLabel->setVisible(false);
|
|
|
|
$descriptionLabel->setAutoNewLine(true);
|
|
|
|
$descriptionLabel->setMaxLines(5);
|
|
|
|
$description = "Author: {$pluginClass::getAuthor()}\nVersion: {$pluginClass::getVersion()}\nDesc: {$pluginClass::getDescription()}";
|
|
|
|
$descriptionLabel->setText($description);
|
2013-12-31 10:40:03 +01:00
|
|
|
$script->addTooltip($nameLabel, $descriptionLabel);
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-12 19:41:37 +01:00
|
|
|
$statusChangeButton = new Label_Button();
|
|
|
|
$pluginFrame->add($statusChangeButton);
|
|
|
|
$statusChangeButton->setHAlign(Control::RIGHT);
|
|
|
|
$statusChangeButton->setX($width * 0.45);
|
|
|
|
$statusChangeButton->setStyle($statusChangeButton::STYLE_CardButtonSmall);
|
2014-01-05 20:42:16 +01:00
|
|
|
if($active) {
|
2013-12-12 19:41:37 +01:00
|
|
|
$statusChangeButton->setTextPrefix('$f00');
|
|
|
|
$statusChangeButton->setText('Deactivate');
|
|
|
|
$statusChangeButton->setAction(self::ACTION_PREFIX_DISABLEPLUGIN . $pluginClass);
|
2014-01-05 20:42:16 +01:00
|
|
|
} else {
|
2013-12-12 19:41:37 +01:00
|
|
|
$statusChangeButton->setTextPrefix('a');
|
|
|
|
$statusChangeButton->setText('Activate');
|
|
|
|
$statusChangeButton->setAction(self::ACTION_PREFIX_ENABLEPLUGIN . $pluginClass);
|
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-09 20:31:02 +01:00
|
|
|
$y -= $entryHeight;
|
2014-01-05 20:42:16 +01:00
|
|
|
if($index % $pageMaxCount == $pageMaxCount - 1) {
|
2013-12-31 11:05:14 +01:00
|
|
|
unset($pageFrame);
|
2013-12-12 19:41:37 +01:00
|
|
|
}
|
2013-12-09 20:31:02 +01:00
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
|
2013-12-04 00:40:37 +01:00
|
|
|
return $frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
|
|
|
|
*/
|
|
|
|
public function saveConfigData(array $configData, Player $player) {
|
|
|
|
}
|
2013-12-12 19:41:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle PlayerManialinkPageAnswer callback
|
|
|
|
*
|
2013-12-31 14:50:28 +01:00
|
|
|
* @param array $callback
|
2013-12-12 19:41:37 +01:00
|
|
|
*/
|
|
|
|
public function handleManialinkPageAnswer(array $callback) {
|
|
|
|
$actionId = $callback[1][2];
|
2014-01-05 20:42:16 +01:00
|
|
|
$enable = (strpos($actionId, self::ACTION_PREFIX_ENABLEPLUGIN) === 0);
|
|
|
|
$disable = (strpos($actionId, self::ACTION_PREFIX_DISABLEPLUGIN) === 0);
|
|
|
|
if(!$enable && !$disable) {
|
2013-12-12 19:41:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
$login = $callback[1][1];
|
2013-12-12 19:41:37 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2014-01-05 20:42:16 +01:00
|
|
|
if(!$player) {
|
2013-12-12 19:41:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
if($enable) {
|
2013-12-12 19:41:37 +01:00
|
|
|
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_ENABLEPLUGIN));
|
2014-01-18 10:42:19 +01:00
|
|
|
$activated = $this->maniaControl->pluginManager->activatePlugin($pluginClass, $player->login);
|
2014-01-05 20:42:16 +01:00
|
|
|
if($activated) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->maniaControl->chat->sendSuccess($pluginClass::getName() . ' activated!', $player->login);
|
|
|
|
$this->maniaControl->configurator->showMenu($player);
|
2013-12-31 15:09:08 +01:00
|
|
|
$this->maniaControl->log(Formatter::stripCodes("{$player->login} activated '{$pluginClass}'!"));
|
2014-01-05 20:42:16 +01:00
|
|
|
} else {
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->maniaControl->chat->sendError('Error activating ' . $pluginClass::getName() . '!', $player->login);
|
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
} else {
|
2013-12-12 19:41:37 +01:00
|
|
|
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_DISABLEPLUGIN));
|
2013-12-14 23:33:23 +01:00
|
|
|
$deactivated = $this->maniaControl->pluginManager->deactivatePlugin($pluginClass);
|
2014-01-05 20:42:16 +01:00
|
|
|
if($deactivated) {
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->maniaControl->chat->sendSuccess($pluginClass::getName() . ' deactivated!', $player->login);
|
|
|
|
$this->maniaControl->configurator->showMenu($player);
|
2013-12-31 15:09:08 +01:00
|
|
|
$this->maniaControl->log(Formatter::stripCodes("{$player->login} deactivated '{$pluginClass}'!"));
|
2014-01-05 20:42:16 +01:00
|
|
|
} else {
|
2013-12-14 23:33:23 +01:00
|
|
|
$this->maniaControl->chat->sendError('Error deactivating ' . $pluginClass::getName() . '!', $player->login);
|
|
|
|
}
|
2013-12-12 19:41:37 +01:00
|
|
|
}
|
2014-01-05 20:42:16 +01:00
|
|
|
|
|
|
|
//Reopen the Menu
|
|
|
|
$menuId = $this->maniaControl->configurator->getMenuId($this->getTitle());
|
|
|
|
$this->maniaControl->configurator->reopenMenu($menuId);
|
2013-12-12 19:41:37 +01:00
|
|
|
}
|
2013-12-04 00:40:37 +01:00
|
|
|
}
|