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-11 20:02:09 +02:00
|
|
|
/* @var $maniaControl ManiaControl */
|
|
|
|
private $maniaControl;
|
2017-05-11 22:35:16 +02:00
|
|
|
private $menuEntries = array();
|
|
|
|
private $yPositions = array();
|
|
|
|
private $registeredClasses = 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-11 20:02:28 +02:00
|
|
|
|
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-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
|
|
|
*
|
|
|
|
* @param string $id
|
2017-05-12 10:27:26 +02:00
|
|
|
* @return Position|null
|
2017-05-11 22:35:16 +02:00
|
|
|
* @api
|
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
|
|
|
|
|
|
|
if (isset($this->yPositions[$id])) {
|
2017-05-12 10:27:26 +02:00
|
|
|
$pos->setY($this->yPositions[$id]);
|
|
|
|
return $pos;
|
2017-05-11 20:02:28 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 20:02:09 +02:00
|
|
|
foreach ($this->menuEntries as $entry) {
|
2017-05-11 20:02:28 +02:00
|
|
|
if ($entry == $id) {
|
2017-05-12 10:27:26 +02:00
|
|
|
$this->yPositions[$id] = $pos->getY();
|
|
|
|
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-12 10:27:26 +02:00
|
|
|
* @param SidebarMenuEntryRenderable $render
|
2017-05-11 22:35:16 +02:00
|
|
|
* @param $order
|
|
|
|
* @param $id
|
|
|
|
* @return bool
|
2017-05-11 20:02:28 +02:00
|
|
|
* @api
|
|
|
|
*/
|
2017-05-11 22:35:16 +02:00
|
|
|
public function addMenuEntry(SidebarMenuEntryRenderable $render, $order, $id) {
|
2017-05-11 20:02:28 +02:00
|
|
|
if (isset($this->menuEntries[$order])) {
|
|
|
|
if ($this->menuEntries[$order] != $id) {
|
2017-05-11 22:35:16 +02:00
|
|
|
return $this->addMenuEntry($render, $order + 1, $id);
|
2017-05-11 20:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->menuEntries[$order] = $id;
|
2017-05-11 22:35:16 +02:00
|
|
|
$this->yPositions = array();
|
2017-05-11 20:02:28 +02:00
|
|
|
ksort($this->menuEntries);
|
2017-05-11 22:35:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
$registered = false;
|
|
|
|
foreach ($this->registeredClasses as $class) {
|
2017-05-11 22:37:17 +02:00
|
|
|
$class->renderMenuEntry();
|
2017-05-11 22:35:16 +02:00
|
|
|
if ($class == $render) {
|
|
|
|
$registered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$registered) {
|
|
|
|
array_push($this->registeredClasses, $render);
|
2017-05-11 22:37:17 +02:00
|
|
|
$render->renderMenuEntry();
|
2017-05-11 22:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-05-11 20:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-12 10:27:26 +02:00
|
|
|
* @param SidebarMenuEntryRenderable $render
|
2017-05-11 22:35:16 +02:00
|
|
|
* @param $id
|
|
|
|
* @param bool $unregisterClass
|
|
|
|
* @api
|
2017-05-11 20:02:28 +02:00
|
|
|
*/
|
2017-05-11 22:35:16 +02:00
|
|
|
public function deleteMenuEntry(SidebarMenuEntryRenderable $render, $id, $unregisterClass = false) {
|
2017-05-11 20:02:28 +02:00
|
|
|
foreach ($this->menuEntries as $k => $entry) {
|
|
|
|
if ($entry == $id) {
|
2017-05-12 20:04:50 +02:00
|
|
|
unset($this->menuEntries[$k]);
|
2017-05-11 20:02:28 +02:00
|
|
|
$this->yPositions = array();
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 22:35:16 +02:00
|
|
|
|
2017-05-12 10:27:26 +02:00
|
|
|
foreach ($this->registeredClasses as $k => $class) {
|
|
|
|
if ($class == $render && $unregisterClass) {
|
2017-05-12 20:04:50 +02:00
|
|
|
unset($this->registeredClasses[$k]);
|
2017-05-12 10:27:26 +02:00
|
|
|
}else{
|
|
|
|
$class->renderMenuEntry();
|
2017-05-11 22:35:16 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-12 10:27:26 +02:00
|
|
|
|
|
|
|
|
2017-05-11 20:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|