TrackManiaControl/core/Manialinks/SidebarMenuManager.php

187 lines
5.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace ManiaControl\Manialinks;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Structures\ShootMania\Models\Position;
use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait;
use ManiaControl\ManiaControl;
/**
* Class managing the Sidebar icons
*
* @api
* @author ManiaControl Team <mail@maniacontrol.com>
2020-01-22 10:39:35 +01:00
* @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class SidebarMenuManager implements UsageInformationAble, CallbackListener {
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';
const ORDER_ADMIN_MENU = 10;
const ORDER_PLAYER_MENU = 20;
2017-05-19 18:36:41 +02:00
/** @var ManiaControl $maniaControl */
private $maniaControl;
2017-05-19 18:36:41 +02:00
/** @var \ManiaControl\Manialinks\SidebarMenuEntry[] $menuEntries */
private $menuEntries = array();
/**
* SidebarMenuManager constructor.
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$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);
2022-04-10 22:21:58 +02:00
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SIDEBAR_POSY_TRACKMANIA, 16);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_MENU_ITEMSIZE, 6);
}
/**
* Returns Position of the Sidebar (PositionObject with x and y)
*
* @return Position
* @api
*/
public function getSidebarPosition() {
$posX = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SIDEBAR_POSX);
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);
}
$pos = new Position();
$pos->setX($posX);
$pos->setY($posY);
return $pos;
}
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() {
$count = 0;
2017-05-19 18:36:41 +02:00
foreach ($this->menuEntries as $k => $entry) {
if ($k < SidebarMenuManager::ORDER_ADMIN_MENU) {
$count++;
}
}
return $count;
}
/**
* Returns PositionObject of a menu item of the sidebar, or null if it's not found
*
2017-05-19 18:36:41 +02:00
* @api
* @param string $id
* @return Position|null
*/
public function getEntryPosition($id) {
$itemSize = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MENU_ITEMSIZE);
$itemMarginFactor = 1.2;
$pos = $this->getSidebarPosition();
2017-05-17 16:11:41 +02:00
$count = $this->getElementCountBeforeAdminMenu();
$pos->setY($pos->getY() + $itemSize * $itemMarginFactor * $count);
foreach ($this->menuEntries as $entry) {
2017-05-19 18:36:41 +02:00
if ($entry->getId() == $id) {
return $pos;
}
$pos->setY($pos->getY() - $itemSize * $itemMarginFactor);
}
$this->maniaControl->getErrorHandler()->triggerDebugNotice('SidebarMenuEntry id:' . $id . ' not found');
return null;
}
/**
* Registers an Entry to the SidebarMenu
* Get the associated position with getEntryPosition($id)
*
* @api
2017-05-19 18:36:41 +02:00
* @param $order
* @param $id
* @param \ManiaControl\Manialinks\SidebarMenuEntryListener $listener
* @param $renderMethod
* @return bool
*/
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;
}
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-19 18:36:41 +02:00
$entry = new SidebarMenuEntry($listener, $renderMethod);
$entry->setId($id);
2017-05-19 18:36:41 +02:00
$this->menuEntries[$order] = $entry;
ksort($this->menuEntries);
2017-05-19 18:36:41 +02:00
$this->updateMenuEntries();
return true;
}
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();
}
}
/**
* @api
2017-05-19 18:36:41 +02:00
* @param SidebarMenuEntryListener $listener
* @param $id
*/
2017-05-19 18:36:41 +02:00
public function deleteMenuEntry(SidebarMenuEntryListener $listener, $id) {
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-19 18:36:41 +02:00
$this->updateMenuEntries();
}
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-19 18:36:41 +02:00
}
2017-05-19 18:36:41 +02:00
$this->updateMenuEntries();
}
}