configurator coding improvements

This commit is contained in:
Steffen Schröder 2014-07-24 23:12:56 +02:00
parent b9c6e7cb85
commit f9d59c432d

View File

@ -3,7 +3,6 @@
namespace ManiaControl\Configurators; namespace ManiaControl\Configurators;
use FML\Controls\Frame; use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Labels\Label_Text; use FML\Controls\Labels\Label_Text;
use FML\Controls\Quad; use FML\Controls\Quad;
use FML\Controls\Quads\Quad_BgRaceScore2; use FML\Controls\Quads\Quad_BgRaceScore2;
@ -32,7 +31,7 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
*/ */
const ACTION_TOGGLEMENU = 'Configurator.ToggleMenuAction'; const ACTION_TOGGLEMENU = 'Configurator.ToggleMenuAction';
const ACTION_SAVECONFIG = 'Configurator.SaveConfigAction'; const ACTION_SAVECONFIG = 'Configurator.SaveConfigAction';
const ACTION_SELECTMENU = 'Configurator.SelectMenu'; const ACTION_SELECTMENU = 'Configurator.SelectMenu.';
const SETTING_MENU_POSX = 'Menu Widget Position: X'; const SETTING_MENU_POSX = 'Menu Widget Position: X';
const SETTING_MENU_POSY = 'Menu Widget Position: Y'; const SETTING_MENU_POSY = 'Menu Widget Position: Y';
const SETTING_MENU_WIDTH = 'Menu Widget Width'; const SETTING_MENU_WIDTH = 'Menu Widget Width';
@ -104,8 +103,8 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
*/ */
private function addActionsMenuItem() { private function addActionsMenuItem() {
$itemQuad = new Quad_UIConstruction_Buttons(); $itemQuad = new Quad_UIConstruction_Buttons();
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_Tools); $itemQuad->setSubStyle($itemQuad::SUBSTYLE_Tools)
$itemQuad->setAction(self::ACTION_TOGGLEMENU); ->setAction(self::ACTION_TOGGLEMENU);
$this->maniaControl->actionsMenu->addAdminMenuItem($itemQuad, 100, 'Settings'); $this->maniaControl->actionsMenu->addAdminMenuItem($itemQuad, 100, 'Settings');
} }
@ -137,14 +136,34 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
* Show the Menu to the Player * Show the Menu to the Player
* *
* @param Player $player * @param Player $player
* @param int $menuId * @param mixed $menuId
*/ */
public function showMenu(Player $player, $menuId = 0) { public function showMenu(Player $player, $menuId = 0) {
if ($menuId instanceof ConfiguratorMenu) {
$menuId = $this->getMenuId($menuId->getTitle());
}
$manialink = $this->buildManialink($menuId, $player); $manialink = $this->buildManialink($menuId, $player);
$this->maniaControl->manialinkManager->displayWidget($manialink, $player, self::MENU_NAME); $this->maniaControl->manialinkManager->displayWidget($manialink, $player, self::MENU_NAME);
$player->setCache($this, self::CACHE_MENU_SHOWN, true); $player->setCache($this, self::CACHE_MENU_SHOWN, true);
} }
/**
* Gets the Menu Id
*
* @param string $title
* @return int
*/
public function getMenuId($title) {
$index = 0;
foreach ($this->menus as $menu) {
if ($menu === $title || $menu->getTitle() === $title) {
return $index;
}
$index++;
}
return 0;
}
/** /**
* Build Menu ManiaLink if necessary * Build Menu ManiaLink if necessary
* *
@ -169,14 +188,13 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
$frame = new Frame(); $frame = new Frame();
$manialink->add($frame); $manialink->add($frame);
$frame->setPosition($menuPosX, $menuPosY); $frame->setPosition($menuPosX, $menuPosY, 10);
$frame->setZ(10);
$backgroundQuad = new Quad(); $backgroundQuad = new Quad();
$frame->add($backgroundQuad); $frame->add($backgroundQuad);
$backgroundQuad->setZ(-10); $backgroundQuad->setZ(-10)
$backgroundQuad->setSize($menuWidth, $menuHeight); ->setSize($menuWidth, $menuHeight)
$backgroundQuad->setStyles($quadStyle, $quadSubstyle); ->setStyles($quadStyle, $quadSubstyle);
$menuItemsFrame = new Frame(); $menuItemsFrame = new Frame();
$frame->add($menuItemsFrame); $frame->add($menuItemsFrame);
@ -185,8 +203,8 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
$itemsBackgroundQuad = new Quad(); $itemsBackgroundQuad = new Quad();
$menuItemsFrame->add($itemsBackgroundQuad); $menuItemsFrame->add($itemsBackgroundQuad);
$backgroundQuad->setZ(-9); $backgroundQuad->setZ(-9);
$itemsBackgroundQuad->setSize($menuListWidth, $menuHeight); $itemsBackgroundQuad->setSize($menuListWidth, $menuHeight)
$itemsBackgroundQuad->setStyles($quadStyle, $quadSubstyle); ->setStyles($quadStyle, $quadSubstyle);
$menusFrame = new Frame(); $menusFrame = new Frame();
$frame->add($menusFrame); $frame->add($menusFrame);
@ -199,15 +217,15 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
$menuId = 0; $menuId = 0;
foreach ($this->menus as $menu) { foreach ($this->menus as $menu) {
// Add title // Add title
$menuItemLabel = new Label(); $menuItemLabel = new Label_Text();
$menuItemsFrame->add($menuItemLabel); $menuItemsFrame->add($menuItemLabel);
$menuItemLabel->setY($menuItemY); $menuItemLabel->setY($menuItemY)
$menuItemLabel->setSize($menuListWidth * 0.9, $menuItemHeight * 0.9); ->setSize($menuListWidth * 0.9, $menuItemHeight * 0.9)
$menuItemLabel->setStyle(Label_Text::STYLE_TextCardRaceRank); ->setStyle($menuItemLabel::STYLE_TextCardRaceRank)
$menuItemLabel->setText('$z' . $menu->getTitle() . '$z'); ->setText($menu->getTitle())
$menuItemLabel->setAction(self::ACTION_SELECTMENU . '.' . $menuId); ->setAction(self::ACTION_SELECTMENU . $menuId);
//Show a Menu // Show the menu
if ($menuId === $menuIdShown) { if ($menuId === $menuIdShown) {
$menuControl = $menu->getMenu($subMenuWidth, $subMenuHeight, $script, $player); $menuControl = $menu->getMenu($subMenuWidth, $subMenuHeight, $script, $player);
$menusFrame->add($menuControl); $menusFrame->add($menuControl);
@ -220,32 +238,32 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
// Add Close Quad (X) // Add Close Quad (X)
$closeQuad = new Quad_Icons64x64_1(); $closeQuad = new Quad_Icons64x64_1();
$frame->add($closeQuad); $frame->add($closeQuad);
$closeQuad->setPosition($menuWidth * 0.483, $menuHeight * 0.467, 3); $closeQuad->setPosition($menuWidth * 0.483, $menuHeight * 0.467, 3)
$closeQuad->setSize(6, 6); ->setSize(6, 6)
$closeQuad->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_QuitRace); ->setSubStyle($closeQuad::SUBSTYLE_QuitRace)
$closeQuad->setAction(ManialinkManager::ACTION_CLOSEWIDGET); ->setAction(ManialinkManager::ACTION_CLOSEWIDGET);
// Add close button // Add close button
$closeButton = new Label(); $closeButton = new Label_Text();
$frame->add($closeButton); $frame->add($closeButton);
$closeButton->setPosition($menuWidth * -0.5 + $menuListWidth * 0.29, $menuHeight * -0.43); $closeButton->setPosition($menuWidth * -0.5 + $menuListWidth * 0.29, $menuHeight * -0.43)
$closeButton->setSize($menuListWidth * 0.3, $menuListWidth * 0.1); ->setSize($menuListWidth * 0.3, $menuListWidth * 0.1)
$closeButton->setStyle(Label_Text::STYLE_TextButtonNavBack); ->setStyle($closeButton::STYLE_TextButtonNavBack)
$closeButton->setTextPrefix('$999'); ->setTextPrefix('$999')
$closeButton->setTranslate(true); ->setTranslate(true)
$closeButton->setText('$zClose$z'); ->setText('Close')
$closeButton->setAction(self::ACTION_TOGGLEMENU); ->setAction(self::ACTION_TOGGLEMENU);
// Add save button // Add save button
$saveButton = new Label(); $saveButton = new Label_Text();
$frame->add($saveButton); $frame->add($saveButton);
$saveButton->setPosition($menuWidth * -0.5 + $menuListWidth * 0.71, $menuHeight * -0.43); $saveButton->setPosition($menuWidth * -0.5 + $menuListWidth * 0.71, $menuHeight * -0.43)
$saveButton->setSize($menuListWidth * 0.3, $menuListWidth * 0.1); ->setSize($menuListWidth * 0.3, $menuListWidth * 0.1)
$saveButton->setStyle(Label_Text::STYLE_TextButtonNavBack); ->setStyle($saveButton::STYLE_TextButtonNavBack)
$saveButton->setTextPrefix('$0f5'); ->setTextPrefix('$0f5')
$saveButton->setTranslate(true); ->setTranslate(true)
$saveButton->setText('$zSave$z'); ->setText('Save')
$saveButton->setAction(self::ACTION_SAVECONFIG); ->setAction(self::ACTION_SAVECONFIG);
return $manialink; return $manialink;
} }
@ -257,6 +275,7 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
* @param int $menuId * @param int $menuId
*/ */
public function reopenMenu(Player $player, $menuId = 0) { public function reopenMenu(Player $player, $menuId = 0) {
// TODO: improve "reopen" to not need the $menuId param by saving the last shown menu
$this->showMenu($player, $menuId); $this->showMenu($player, $menuId);
} }
@ -289,10 +308,19 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
* @param Player $player * @param Player $player
*/ */
public function hideMenu(Player $player) { public function hideMenu(Player $player) {
$player->destroyCache($this, self::CACHE_MENU_SHOWN); $this->closeWidget($player);
$this->maniaControl->manialinkManager->closeWidget($player); $this->maniaControl->manialinkManager->closeWidget($player);
} }
/**
* Handle widget being closed
*
* @param Player $player
*/
public function closeWidget(Player $player) {
$player->destroyCache($this, self::CACHE_MENU_SHOWN);
}
/** /**
* Save the config data received from the manialink * Save the config data received from the manialink
* *
@ -301,17 +329,15 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
*/ */
public function handleSaveConfigAction(array $callback, Player $player) { public function handleSaveConfigAction(array $callback, Player $player) {
foreach ($this->menus as $menu) { foreach ($this->menus as $menu) {
/** @var ConfiguratorMenu $menu */
$menu->saveConfigData($callback[1], $player); $menu->saveConfigData($callback[1], $player);
} }
} }
/** /**
* Unset the player if he opened another Main Widget * Unset the player if he opened another Main Widget
* *
* @param Player $player * @param Player $player
* @param $openedWidget * @param string $openedWidget
*/ */
public function handleWidgetOpened(Player $player, $openedWidget) { public function handleWidgetOpened(Player $player, $openedWidget) {
if ($openedWidget !== self::MENU_NAME) { if ($openedWidget !== self::MENU_NAME) {
@ -319,33 +345,6 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
} }
} }
/**
* Widget get closed -> unset player
*
* @param Player $player
*/
public function closeWidget(Player $player) {
$player->destroyCache($this, self::CACHE_MENU_SHOWN);
}
/**
* Gets the Menu Id
*
* @param string $title
* @return int
*/
public function getMenuId($title) {
$index = 0;
foreach ($this->menus as $menu) {
/** @var ConfiguratorMenu $menu */
if ($menu === $title || $menu->getTitle() === $title) {
return $index;
}
$index++;
}
return 0;
}
/** /**
* Handle ManialinkPageAnswer Callback * Handle ManialinkPageAnswer Callback
* *
@ -358,10 +357,12 @@ class Configurator implements CallbackListener, CommandListener, ManialinkPageAn
return; return;
} }
$login = $callback[1][1]; $login = $callback[1][1];
$actionArray = explode(".", $callback[1][2]);
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
$this->showMenu($player, intval($actionArray[2]));
if ($player) {
$actionArray = explode('.', $callback[1][2]);
$this->showMenu($player, intval($actionArray[2]));
}
} }
} }