2017-05-11 20:02:09 +02:00
|
|
|
<?php
|
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
namespace ManiaControl\Manialinks;
|
2017-05-11 20:02:09 +02:00
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
2017-05-12 10:27:26 +02:00
|
|
|
use ManiaControl\Callbacks\Structures\ShootMania\Models\Position;
|
2017-05-11 20:02:09 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
2017-05-11 22:35:16 +02:00
|
|
|
/**
|
|
|
|
* Class managing the Sidebar icons
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
|
|
* @copyright 2014-2017 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2017-05-11 20:02:28 +02:00
|
|
|
class SidebarMenuManager implements UsageInformationAble, CallbackListener {
|
2017-05-11 20:02:09 +02:00
|
|
|
use UsageInformationTrait;
|
|
|
|
|
|
|
|
/* Settings */
|
|
|
|
const SETTING_SIDEBAR_POSX = 'Sidebar X Position';
|
|
|
|
const SETTING_SIDEBAR_POSY_SHOOTMANIA = 'Sidebar Y Position (Shootmania)';
|
|
|
|
const SETTING_SIDEBAR_POSY_TRACKMANIA = 'Sidebar Y Position (Trackmania)';
|
|
|
|
const SETTING_MENU_ITEMSIZE = 'Size of menu items';
|
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
const ORDER_ADMIN_MENU = 10;
|
|
|
|
const ORDER_PLAYER_MENU = 20;
|
|
|
|
|
2017-05-19 18:36:41 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2017-05-11 20:02:09 +02:00
|
|
|
private $maniaControl;
|
2017-05-19 18:36:41 +02:00
|
|
|
/** @var \ManiaControl\Manialinks\SidebarMenuEntry[] $menuEntries */
|
|
|
|
private $menuEntries = array();
|
2017-05-11 20:02:09 +02:00
|
|
|
|
2017-05-11 22:35:16 +02:00
|
|
|
/**
|
|
|
|
* SidebarMenuManager constructor.
|
|
|
|
*
|
|
|
|
* @param \ManiaControl\ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
2017-05-11 20:02:09 +02:00
|
|
|
$this->maniaControl = $maniaControl;
|
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SIDEBAR_POSX, 156);
|
2017-05-11 22:43:15 +02:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SIDEBAR_POSY_SHOOTMANIA, -17);
|
2017-05-11 20:02:09 +02:00
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SIDEBAR_POSY_TRACKMANIA, 17);
|
|
|
|
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MENU_ITEMSIZE, 6);
|
|
|
|
}
|
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
/**
|
2017-05-12 10:27:26 +02:00
|
|
|
* Returns Position of the Sidebar (PositionObject with x and y)
|
2017-05-11 20:02:28 +02:00
|
|
|
*
|
2017-05-12 10:27:26 +02:00
|
|
|
* @return Position
|
2017-05-11 20:02:28 +02:00
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
public function getSidebarPosition() {
|
|
|
|
$posX = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SIDEBAR_POSX);
|
2017-05-11 20:02:09 +02:00
|
|
|
|
|
|
|
if ($this->maniaControl->getMapManager()->getCurrentMap()->getGame() === 'sm') {
|
|
|
|
$posY = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SIDEBAR_POSY_SHOOTMANIA);
|
|
|
|
} else {
|
|
|
|
$posY = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SIDEBAR_POSY_TRACKMANIA);
|
|
|
|
}
|
2017-05-12 10:27:26 +02:00
|
|
|
$pos = new Position();
|
|
|
|
$pos->setX($posX);
|
|
|
|
$pos->setY($posY);
|
|
|
|
|
|
|
|
return $pos;
|
2017-05-11 20:02:28 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 16:11:41 +02:00
|
|
|
/**
|
|
|
|
* Returns the number of elements above the admin menu
|
|
|
|
* Used to make the y-value setting of the sidebar relative to the admin menu
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-05-19 18:36:41 +02:00
|
|
|
private function getElementCountBeforeAdminMenu() {
|
2017-05-17 16:08:51 +02:00
|
|
|
$count = 0;
|
2017-05-19 18:36:41 +02:00
|
|
|
foreach ($this->menuEntries as $k => $entry) {
|
|
|
|
if ($k < SidebarMenuManager::ORDER_ADMIN_MENU) {
|
2017-05-17 16:08:51 +02:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
/**
|
2017-05-12 10:27:26 +02:00
|
|
|
* Returns PositionObject of a menu item of the sidebar, or null if it's not found
|
2017-05-11 20:02:28 +02:00
|
|
|
*
|
2017-05-19 18:36:41 +02:00
|
|
|
* @api
|
2017-05-11 20:02:28 +02:00
|
|
|
* @param string $id
|
2017-05-12 10:27:26 +02:00
|
|
|
* @return Position|null
|
2017-05-11 20:02:28 +02:00
|
|
|
*/
|
|
|
|
public function getEntryPosition($id) {
|
2017-05-12 10:27:26 +02:00
|
|
|
$itemSize = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MENU_ITEMSIZE);
|
|
|
|
$itemMarginFactor = 1.2;
|
|
|
|
$pos = $this->getSidebarPosition();
|
2017-05-11 20:02:28 +02:00
|
|
|
|
2017-05-17 16:11:41 +02:00
|
|
|
$count = $this->getElementCountBeforeAdminMenu();
|
2017-05-17 16:08:51 +02:00
|
|
|
$pos->setY($pos->getY() + $itemSize * $itemMarginFactor * $count);
|
|
|
|
|
2017-05-11 20:02:09 +02:00
|
|
|
foreach ($this->menuEntries as $entry) {
|
2017-05-19 18:36:41 +02:00
|
|
|
if ($entry->getId() == $id) {
|
2017-05-12 10:27:26 +02:00
|
|
|
return $pos;
|
2017-05-11 20:02:09 +02:00
|
|
|
}
|
2017-05-12 10:27:26 +02:00
|
|
|
$pos->setY($pos->getY() - $itemSize * $itemMarginFactor);
|
2017-05-11 20:02:09 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
$this->maniaControl->getErrorHandler()->triggerDebugNotice('SidebarMenuEntry id:' . $id . ' not found');
|
|
|
|
return null;
|
|
|
|
}
|
2017-05-11 20:02:09 +02:00
|
|
|
|
2017-05-11 22:35:16 +02:00
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
/**
|
|
|
|
* Registers an Entry to the SidebarMenu
|
|
|
|
* Get the associated position with getEntryPosition($id)
|
|
|
|
*
|
2017-05-19 01:16:57 +02:00
|
|
|
* @api
|
2017-05-19 18:36:41 +02:00
|
|
|
* @param $order
|
|
|
|
* @param $id
|
|
|
|
* @param \ManiaControl\Manialinks\SidebarMenuEntryListener $listener
|
|
|
|
* @param $renderMethod
|
2017-05-11 22:35:16 +02:00
|
|
|
* @return bool
|
2017-05-11 20:02:28 +02:00
|
|
|
*/
|
2017-05-19 18:36:41 +02:00
|
|
|
public function addMenuEntry($order, $id, SidebarMenuEntryListener $listener, $renderMethod) {
|
|
|
|
if ((!is_string($renderMethod) || !method_exists($listener, $renderMethod)) && !is_callable($renderMethod)) {
|
|
|
|
trigger_error("Given Listener (" . get_class($listener) . ") can't handle Timer Callback (No Method '{$renderMethod}')!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
if (isset($this->menuEntries[$order])) {
|
2017-05-19 18:36:41 +02:00
|
|
|
if ($this->menuEntries[$order]->getId() != $id) {
|
|
|
|
return $this->addMenuEntry($order + 1, $id, $listener, $renderMethod);
|
2017-05-11 20:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-11 22:35:16 +02:00
|
|
|
|
2017-05-19 18:36:41 +02:00
|
|
|
$entry = new SidebarMenuEntry($listener, $renderMethod);
|
|
|
|
$entry->setId($id);
|
2017-05-11 22:35:16 +02:00
|
|
|
|
2017-05-19 18:36:41 +02:00
|
|
|
$this->menuEntries[$order] = $entry;
|
|
|
|
ksort($this->menuEntries);
|
2017-05-11 22:35:16 +02:00
|
|
|
|
2017-05-19 18:36:41 +02:00
|
|
|
$this->updateMenuEntries();
|
2017-05-11 22:35:16 +02:00
|
|
|
|
|
|
|
return true;
|
2017-05-11 20:02:28 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 18:36:41 +02:00
|
|
|
/**
|
|
|
|
* Call all user functions
|
|
|
|
*/
|
|
|
|
private function updateMenuEntries() {
|
|
|
|
foreach ($this->menuEntries as $listening) {
|
|
|
|
|
|
|
|
// Call the User Function
|
|
|
|
$listening->triggerCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 20:02:28 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-19 01:16:57 +02:00
|
|
|
* @api
|
2017-05-19 18:36:41 +02:00
|
|
|
* @param SidebarMenuEntryListener $listener
|
2017-05-11 22:35:16 +02:00
|
|
|
* @param $id
|
2017-05-11 20:02:28 +02:00
|
|
|
*/
|
2017-05-19 18:36:41 +02:00
|
|
|
public function deleteMenuEntry(SidebarMenuEntryListener $listener, $id) {
|
2017-05-11 20:02:28 +02:00
|
|
|
foreach ($this->menuEntries as $k => $entry) {
|
2017-05-19 18:36:41 +02:00
|
|
|
if ($entry->getId() == $id) {
|
2017-05-12 20:04:50 +02:00
|
|
|
unset($this->menuEntries[$k]);
|
2017-05-11 20:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-19 18:36:41 +02:00
|
|
|
$this->updateMenuEntries();
|
|
|
|
}
|
2017-05-11 22:35:16 +02:00
|
|
|
|
2017-05-19 18:36:41 +02:00
|
|
|
/**
|
|
|
|
* @api
|
|
|
|
* @param \ManiaControl\Manialinks\SidebarMenuEntryListener $listener
|
|
|
|
*/
|
|
|
|
public function deleteMenuEntries(SidebarMenuEntryListener $listener) {
|
|
|
|
foreach ($this->menuEntries as $k => $entry) {
|
|
|
|
if ($entry->listener == $listener) {
|
|
|
|
unset($this->menuEntries[$k]);
|
2017-05-11 22:35:16 +02:00
|
|
|
}
|
2017-05-19 18:36:41 +02:00
|
|
|
}
|
2017-05-12 10:27:26 +02:00
|
|
|
|
2017-05-19 18:36:41 +02:00
|
|
|
$this->updateMenuEntries();
|
2017-05-11 20:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|