From 9f701cddea7be61fd801b1888f0bf2b542abdab9 Mon Sep 17 00:00:00 2001 From: Jocy Date: Thu, 11 May 2017 22:35:16 +0200 Subject: [PATCH] Added Interface for SidebarMenuManager, improved its functionality --- core/Admin/ActionsMenu.php | 14 +++- .../Manialinks/SidebarMenuEntryRenderable.php | 16 ++++- core/Manialinks/SidebarMenuManager.php | 68 +++++++++++++++---- core/Maps/MapCommands.php | 2 +- plugins/MCTeam/CustomVotesPlugin.php | 13 +++- plugins/MCTeam/DonationPlugin.php | 10 ++- 6 files changed, 100 insertions(+), 23 deletions(-) diff --git a/core/Admin/ActionsMenu.php b/core/Admin/ActionsMenu.php index 7a9b4f4e..0208bacf 100644 --- a/core/Admin/ActionsMenu.php +++ b/core/Admin/ActionsMenu.php @@ -15,6 +15,7 @@ use ManiaControl\General\UsageInformationTrait; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkPageAnswerListener; +use ManiaControl\Manialinks\SidebarMenuEntryRenderable; use ManiaControl\Manialinks\SidebarMenuManager; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; @@ -29,7 +30,7 @@ use ManiaControl\Settings\SettingManager; * @copyright 2014-2017 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class ActionsMenu implements CallbackListener, ManialinkPageAnswerListener, UsageInformationAble { +class ActionsMenu implements SidebarMenuEntryRenderable, CallbackListener, ManialinkPageAnswerListener, UsageInformationAble { use UsageInformationTrait; /* @@ -74,8 +75,8 @@ class ActionsMenu implements CallbackListener, ManialinkPageAnswerListener, Usag $this->maniaControl->getCallbackManager()->registerCallbackListener(AuthenticationManager::CB_AUTH_LEVEL_CHANGED, $this, 'handlePlayerJoined'); $this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChanged'); - $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry(SidebarMenuManager::ORDER_ADMIN_MENU, self::ADMIN_MENU_ID); - $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry(SidebarMenuManager::ORDER_PLAYER_MENU, self::PLAYER_MENU_ID); + $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry($this,SidebarMenuManager::ORDER_ADMIN_MENU, self::ADMIN_MENU_ID); + $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry($this,SidebarMenuManager::ORDER_PLAYER_MENU, self::PLAYER_MENU_ID); } /** @@ -356,4 +357,11 @@ class ActionsMenu implements CallbackListener, ManialinkPageAnswerListener, Usag $this->rebuildAndShowMenu(); } + + /** + * Call here the function which updates the MenuIcon Manialink + */ + public function renderMenuIcon() { + $this->rebuildAndShowMenu(); + } } diff --git a/core/Manialinks/SidebarMenuEntryRenderable.php b/core/Manialinks/SidebarMenuEntryRenderable.php index 159bca69..4e04c583 100644 --- a/core/Manialinks/SidebarMenuEntryRenderable.php +++ b/core/Manialinks/SidebarMenuEntryRenderable.php @@ -3,7 +3,19 @@ namespace ManiaControl\Manialinks; +/** + * Interface for the Sidebar managing + * + * @api + * @author ManiaControl Team + * @copyright 2014-2017 ManiaControl Team + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ -interface SidebarMenuEntryable { - public function reloadMenuIcon(); +interface SidebarMenuEntryRenderable { + + /** + * Call here the function which updates the MenuIcon Manialink + */ + public function renderMenuIcon(); } \ No newline at end of file diff --git a/core/Manialinks/SidebarMenuManager.php b/core/Manialinks/SidebarMenuManager.php index a7254ffb..742f9da6 100644 --- a/core/Manialinks/SidebarMenuManager.php +++ b/core/Manialinks/SidebarMenuManager.php @@ -7,7 +7,14 @@ use ManiaControl\General\UsageInformationAble; use ManiaControl\General\UsageInformationTrait; use ManiaControl\ManiaControl; - +/** + * Class managing the Sidebar icons + * + * @api + * @author ManiaControl Team + * @copyright 2014-2017 ManiaControl Team + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ class SidebarMenuManager implements UsageInformationAble, CallbackListener { use UsageInformationTrait; @@ -22,10 +29,16 @@ class SidebarMenuManager implements UsageInformationAble, CallbackListener { /* @var $maniaControl ManiaControl */ private $maniaControl; - private $menuEntries = array(); - private $yPositions = array(); + private $menuEntries = array(); + private $yPositions = array(); + private $registeredClasses = array(); - function __construct(ManiaControl $maniaControl) { + /** + * SidebarMenuManager constructor. + * + * @param \ManiaControl\ManiaControl $maniaControl + */ + public function __construct(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SIDEBAR_POSX, 156); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SIDEBAR_POSY_SHOOTMANIA, -37); @@ -56,6 +69,7 @@ class SidebarMenuManager implements UsageInformationAble, CallbackListener { * * @param string $id * @return array|null + * @api */ public function getEntryPosition($id) { $itemSize = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MENU_ITEMSIZE); @@ -79,38 +93,66 @@ class SidebarMenuManager implements UsageInformationAble, CallbackListener { return null; } + /** * Registers an Entry to the SidebarMenu * Get the associated position with getEntryPosition($id) * - * @param int $order - * @param string $id + * @param SidebarMenuEntryRenderable $render + * @param $order + * @param $id + * @return bool * @api */ - public function addMenuEntry($order, $id) { + public function addMenuEntry(SidebarMenuEntryRenderable $render, $order, $id) { if (isset($this->menuEntries[$order])) { if ($this->menuEntries[$order] != $id) { - $this->addMenuEntry($order + 1, $id); + return $this->addMenuEntry($render, $order + 1, $id); } } $this->menuEntries[$order] = $id; + $this->yPositions = array(); ksort($this->menuEntries); - $this->yPositions = array(); + + + $registered = false; + foreach ($this->registeredClasses as $class) { + $class->renderMenuIcon(); + if ($class == $render) { + $registered = true; + } + } + + if (!$registered) { + array_push($this->registeredClasses, $render); + $render->renderMenuIcon(); + } + + return true; } /** - * Deletes an Entry from the SidebarMenu - * - * @param string $id + * @param \ManiaControl\Manialinks\SidebarMenuEntryRenderable $render + * @param $id + * @param bool $unregisterClass + * @api */ - public function deleteMenuEntry($id) { + public function deleteMenuEntry(SidebarMenuEntryRenderable $render, $id, $unregisterClass = false) { foreach ($this->menuEntries as $k => $entry) { if ($entry == $id) { array_splice($this->menuEntries, $k, 1); $this->yPositions = array(); } } + + if($unregisterClass){ + foreach($this->registeredClasses as $k => $class){ + if($class == $render){ + array_splice($this->registeredClasses, $k, 1); + } + } + } } } \ No newline at end of file diff --git a/core/Maps/MapCommands.php b/core/Maps/MapCommands.php index d3964e1b..5465ba85 100644 --- a/core/Maps/MapCommands.php +++ b/core/Maps/MapCommands.php @@ -428,9 +428,9 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb private function showMapListKarma($best, Player $player) { /** @var \MCTeam\KarmaPlugin $karmaPlugin */ $karmaPlugin = $this->maniaControl->getPluginManager()->getPlugin(MapList::DEFAULT_KARMA_PLUGIN); - $displayMxKarma = $this->maniaControl->getSettingManager()->getSettingValue($karmaPlugin, $karmaPlugin::SETTING_WIDGET_DISPLAY_MX); if ($karmaPlugin) { + $displayMxKarma = $this->maniaControl->getSettingManager()->getSettingValue($karmaPlugin, $karmaPlugin::SETTING_WIDGET_DISPLAY_MX); //Sort by Mx Karma in Maplist if ($displayMxKarma) { //TODO diff --git a/plugins/MCTeam/CustomVotesPlugin.php b/plugins/MCTeam/CustomVotesPlugin.php index 2e0c7fc5..201ebe30 100644 --- a/plugins/MCTeam/CustomVotesPlugin.php +++ b/plugins/MCTeam/CustomVotesPlugin.php @@ -24,6 +24,7 @@ use ManiaControl\Commands\CommandListener; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkPageAnswerListener; +use ManiaControl\Manialinks\SidebarMenuEntryRenderable; use ManiaControl\Manialinks\SidebarMenuManager; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; @@ -46,7 +47,7 @@ use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException; * @copyright 2014-2017 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkPageAnswerListener, TimerListener, Plugin { +class CustomVotesPlugin implements SidebarMenuEntryRenderable, CommandListener, CallbackListener, ManialinkPageAnswerListener, TimerListener, Plugin { /* * Constants */ @@ -161,7 +162,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SPECTATOR_ALLOW_START_VOTE, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_VOTE_TIME, 40); - $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry(SidebarMenuManager::ORDER_PLAYER_MENU + 5, self::CUSTOMVOTES_MENU_ID); + $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry($this,SidebarMenuManager::ORDER_PLAYER_MENU + 5, self::CUSTOMVOTES_MENU_ID); //Define Votes $this->defineVote("teambalance", "Vote for Team Balance"); @@ -371,6 +372,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP $this->destroyVote(); $this->maniaControl->getManialinkManager()->hideManialink(self::MLID_ICON); + $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->deleteMenuEntry($this,self::CUSTOMVOTES_MENU_ID,true); } /** @@ -796,6 +798,13 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP $this->constructMenu(); } + + /** + * Call here the function which updates the MenuIcon Manialink + */ + public function renderMenuIcon() { + $this->showIcon(); + } } /** diff --git a/plugins/MCTeam/DonationPlugin.php b/plugins/MCTeam/DonationPlugin.php index 035ae6ef..e8545585 100644 --- a/plugins/MCTeam/DonationPlugin.php +++ b/plugins/MCTeam/DonationPlugin.php @@ -19,6 +19,7 @@ use ManiaControl\Commands\CommandListener; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\LabelLine; use ManiaControl\Manialinks\ManialinkManager; +use ManiaControl\Manialinks\SidebarMenuEntryRenderable; use ManiaControl\Manialinks\SidebarMenuManager; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; @@ -31,7 +32,7 @@ use ManiaControl\Plugins\Plugin; * @copyright 2014-2017 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class DonationPlugin implements CallbackListener, CommandListener, Plugin { +class DonationPlugin implements CallbackListener, CommandListener, Plugin, SidebarMenuEntryRenderable { /* * Constants */ @@ -116,7 +117,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { // Define player stats $this->maniaControl->getStatisticManager()->defineStatMetaData(self::STAT_PLAYER_DONATIONS); - $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry(SidebarMenuManager::ORDER_PLAYER_MENU + 10, self::DONATIONPLUGIN_MENU_ID); + $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->addMenuEntry($this,SidebarMenuManager::ORDER_PLAYER_MENU, self::DONATIONPLUGIN_MENU_ID); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED, true); $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_DONATION_VALUES, "20,50,100,500,1000,2000"); @@ -233,6 +234,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { */ public function unload() { $this->maniaControl->getManialinkManager()->hideManialink(self::MLID_DONATE_WIDGET); + $this->maniaControl->getManialinkManager()->getSidebarMenuManager()->deleteMenuEntry($this,self::DONATIONPLUGIN_MENU_ID,true); } /** @@ -529,4 +531,8 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin { // Render and display xml $this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $player, 'TopDons'); } + + public function renderMenuIcon() { + $this->displayDonateWidget(); + } }