TrackManiaControl/application/core/Admin/ActionsMenu.php

319 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Admin;
use FML\Controls\Control;
use FML\Controls\Frame;
2014-01-05 01:29:49 +01:00
use FML\Controls\Label;
use FML\Controls\Quad;
2014-01-02 17:21:01 +01:00
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\ManiaLink;
use ManiaControl\Callbacks\CallbackListener;
2014-03-01 11:11:50 +01:00
use ManiaControl\Callbacks\CallbackManager;
2014-01-02 17:21:01 +01:00
use ManiaControl\ManiaControl;
2013-12-29 15:52:01 +01:00
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
/**
2013-12-31 12:42:07 +01:00
* Class managing Actions Menus
*
* @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2013-12-31 12:42:07 +01:00
class ActionsMenu implements CallbackListener, ManialinkPageAnswerListener {
/*
* Constants
*/
2014-04-27 18:59:21 +02:00
const MLID_MENU = 'ActionsMenu.MLID';
const SETTING_MENU_POSX = 'Menu Position: X';
const SETTING_MENU_POSY = 'Menu Position: Y';
const SETTING_MENU_ITEMSIZE = 'Menu Item Size';
const ACTION_OPEN_ADMIN_MENU = 'ActionsMenu.OpenAdminMenu';
2013-12-31 12:42:07 +01:00
const ACTION_OPEN_PLAYER_MENU = 'ActionsMenu.OpenPlayerMenu';
2014-04-27 18:59:21 +02:00
/*
2013-12-31 12:42:07 +01:00
* Private Properties
*/
private $maniaControl = null;
2013-12-31 12:42:07 +01:00
private $adminMenuItems = array();
private $playerMenuItems = array();
2014-01-05 00:43:46 +01:00
private $initCompleted = false;
2014-01-05 01:29:49 +01:00
/**
2013-12-31 12:42:07 +01:00
* Create a new Actions Menu
*
2013-12-31 12:24:54 +01:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-04-27 18:59:21 +02:00
// Init settings
2013-12-29 19:21:29 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_POSX, 156.);
2014-01-14 19:11:57 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_POSY, -17.);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_ITEMSIZE, 6.);
2014-04-27 18:59:21 +02:00
// Register for callbacks
2014-03-01 11:11:50 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit');
2014-02-19 10:43:37 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerJoined');
2014-01-09 18:19:37 +01:00
$this->maniaControl->callbackManager->registerCallbackListener(AuthenticationManager::CB_AUTH_LEVEL_CHANGED, $this, 'handlePlayerJoined');
}
/**
2013-12-31 12:42:07 +01:00
* Add a new Menu Item
*
2013-12-09 19:45:40 +01:00
* @param Control $control
2014-04-27 18:59:21 +02:00
* @param bool $playerAction
* @param int $order
* @param string $description
*/
2014-01-05 12:56:47 +01:00
public function addMenuItem(Control $control, $playerAction = true, $order = 0, $description = null) {
2014-04-27 18:59:21 +02:00
if ($playerAction) {
2014-01-04 21:54:40 +01:00
$this->addPlayerMenuItem($control, $order, $description);
2014-04-27 18:59:21 +02:00
}
else {
2014-01-05 00:43:46 +01:00
$this->addAdminMenuItem($control, $order, $description);
2013-12-31 12:42:07 +01:00
}
}
2014-01-18 20:05:50 +01:00
/**
* Removes a Menu Item
*
2014-04-27 18:59:21 +02:00
* @param $order
2014-01-18 20:05:50 +01:00
* @param bool $playerAction
*/
public function removeMenuItem($order, $playerAction = true) {
2014-04-27 18:59:21 +02:00
if ($playerAction) {
2014-01-18 20:05:50 +01:00
unset($this->playerMenuItems[$order]);
2014-04-27 18:59:21 +02:00
}
else {
2014-01-18 20:05:50 +01:00
unset($this->adminMenuItems[$order]);
}
$this->rebuildAndShowMenu();
}
2013-12-31 12:42:07 +01:00
/**
* Add a new Player Menu Item
*
* @param Control $control
2014-04-27 18:59:21 +02:00
* @param int $order
* @param string $description
2013-12-31 12:42:07 +01:00
*/
2014-01-05 12:56:47 +01:00
public function addPlayerMenuItem(Control $control, $order = 0, $description = null) {
2014-04-27 18:59:21 +02:00
if (!isset($this->playerMenuItems[$order])) {
2013-12-31 12:42:07 +01:00
$this->playerMenuItems[$order] = array();
}
2014-01-05 12:56:47 +01:00
array_push($this->playerMenuItems[$order], array($control, $description));
2014-01-05 13:44:04 +01:00
krsort($this->playerMenuItems);
2014-01-05 12:56:47 +01:00
$this->rebuildAndShowMenu();
2013-12-31 12:42:07 +01:00
}
/**
* Add a new Admin Menu Item
*
* @param Control $control
2014-04-27 18:59:21 +02:00
* @param int $order
* @param string $description
2013-12-31 12:42:07 +01:00
*/
2014-01-05 12:56:47 +01:00
public function addAdminMenuItem(Control $control, $order = 0, $description = null) {
2014-04-27 18:59:21 +02:00
if (!isset($this->adminMenuItems[$order])) {
2013-12-31 12:42:07 +01:00
$this->adminMenuItems[$order] = array();
}
2014-01-05 12:56:47 +01:00
array_push($this->adminMenuItems[$order], array($control, $description));
2014-01-05 13:44:04 +01:00
krsort($this->adminMenuItems);
2014-01-05 12:56:47 +01:00
$this->rebuildAndShowMenu();
}
/**
2014-03-01 11:11:50 +01:00
* Handle ManiaControl AfterInit callback
*/
2014-03-01 11:11:50 +01:00
public function handleAfterInit() {
2014-01-05 00:43:46 +01:00
$this->initCompleted = true;
2014-01-05 13:44:04 +01:00
$this->rebuildAndShowMenu();
2014-01-05 00:43:46 +01:00
}
/**
* Build and show the menus to everyone (if a menu get made after the init)
*/
public function rebuildAndShowMenu() {
2014-04-27 18:59:21 +02:00
if (!$this->initCompleted) {
2014-01-09 18:19:37 +01:00
return;
}
2014-01-05 12:56:47 +01:00
$players = $this->maniaControl->playerManager->getPlayers();
2014-04-27 18:59:21 +02:00
foreach ($players as $player) {
$manialink = $this->buildMenuIconsManialink($player);
2014-03-02 13:00:23 +01:00
$this->maniaControl->manialinkManager->sendManialink($manialink, $player->login);
2014-01-05 00:43:46 +01:00
}
}
/**
* Handle PlayerJoined callback
*
* @param Player $player
*/
public function handlePlayerJoined(Player $player) {
2014-03-02 13:00:23 +01:00
$maniaLink = $this->buildMenuIconsManialink($player);
$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player->login);
}
2014-01-04 18:49:33 +01:00
/**
* Builds the Manialink
*
* @param Player $player
* @return ManiaLink
*/
2014-01-05 12:56:47 +01:00
private function buildMenuIconsManialink(Player $player) {
2014-04-27 18:59:21 +02:00
$posX = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MENU_POSX);
$posY = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MENU_POSY);
$itemSize = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MENU_ITEMSIZE);
$shootManiaOffset = $this->maniaControl->manialinkManager->styleManager->getDefaultIconOffsetSM();
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle();
2013-12-29 15:52:01 +01:00
$itemMarginFactorX = 1.3;
$itemMarginFactorY = 1.2;
2014-04-27 18:59:21 +02:00
// If game is shootmania lower the icons position by 20
2014-03-12 13:07:02 +01:00
if ($this->maniaControl->mapManager->getCurrentMap()->getGame() == 'sm') {
2014-01-15 22:07:09 +01:00
$posY -= $shootManiaOffset;
}
2014-04-27 18:59:21 +02:00
2013-12-29 15:52:01 +01:00
$manialink = new ManiaLink(self::MLID_MENU);
2014-04-27 18:59:21 +02:00
$script = $manialink->getScript();
2014-01-05 12:56:47 +01:00
/*
* Admin Menu
*/
2014-04-27 18:59:21 +02:00
if ($this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
2014-01-05 12:56:47 +01:00
// Admin Menu Icon Frame
$iconFrame = new Frame();
$manialink->add($iconFrame);
$iconFrame->setPosition($posX, $posY);
2014-04-27 18:59:21 +02:00
2014-01-05 12:56:47 +01:00
$backgroundQuad = new Quad();
$iconFrame->add($backgroundQuad);
$backgroundQuad->setSize($itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
2014-04-27 18:59:21 +02:00
2014-01-05 12:56:47 +01:00
$itemQuad = new Quad_Icons64x64_1();
$iconFrame->add($itemQuad);
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_IconServers);
$itemQuad->setSize($itemSize, $itemSize);
2014-04-27 18:59:21 +02:00
2014-04-27 19:22:20 +02:00
// Admin Menu Description
2014-01-05 12:56:47 +01:00
$descriptionLabel = new Label();
2014-04-27 19:22:20 +02:00
$manialink->add($descriptionLabel);
$descriptionLabel->setPosition($posX - count($this->adminMenuItems) * $itemSize * 1.15 - 6, $posY);
2014-01-05 12:56:47 +01:00
$descriptionLabel->setAlign(Control::RIGHT, Control::TOP);
$descriptionLabel->setSize(40, 4);
$descriptionLabel->setTextSize(1.4);
$descriptionLabel->setTextColor('fff');
2014-04-27 18:59:21 +02:00
2014-01-05 12:56:47 +01:00
// Admin Menu
2014-01-04 18:49:33 +01:00
$popoutFrame = new Frame();
$manialink->add($popoutFrame);
$popoutFrame->setPosition($posX - $itemSize * 0.5, $posY);
$popoutFrame->setHAlign(Control::RIGHT);
$popoutFrame->setSize(4 * $itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
2014-01-12 01:59:23 +01:00
$popoutFrame->setVisible(false);
2014-04-27 18:59:21 +02:00
2014-01-05 12:56:47 +01:00
$backgroundQuad = new Quad();
$popoutFrame->add($backgroundQuad);
$backgroundQuad->setHAlign(Control::RIGHT);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$backgroundQuad->setSize(count($this->adminMenuItems) * $itemSize * 1.15 + 2, $itemSize * $itemMarginFactorY);
2014-04-27 18:59:21 +02:00
$itemQuad->addToggleFeature($popoutFrame);
2014-01-04 18:49:33 +01:00
// Add items
$x = -1;
2014-04-27 18:59:21 +02:00
foreach ($this->adminMenuItems as $menuItems) {
foreach ($menuItems as $menuItem) {
2014-01-05 12:56:47 +01:00
$menuQuad = $menuItem[0];
2014-04-27 19:22:20 +02:00
/**
* @var Quad $menuQuad
*/
2014-01-05 12:56:47 +01:00
$popoutFrame->add($menuQuad);
$menuQuad->setSize($itemSize, $itemSize);
$menuQuad->setX($x);
$menuQuad->setHAlign(Control::RIGHT);
$x -= $itemSize * 1.05;
2014-04-27 18:59:21 +02:00
if ($menuItem[1]) {
2014-04-27 19:22:20 +02:00
$menuQuad->removeScriptFeatures();
2014-01-05 12:56:47 +01:00
$description = '$s' . $menuItem[1];
2014-04-27 18:59:21 +02:00
$menuQuad->addTooltipLabelFeature($descriptionLabel, $description);
2014-01-05 12:56:47 +01:00
}
}
2014-01-04 18:18:46 +01:00
}
}
2014-04-27 18:59:21 +02:00
2014-01-05 12:56:47 +01:00
/*
* Player Menu
*/
2013-12-31 13:34:10 +01:00
// Player Menu Icon Frame
2014-01-05 12:56:47 +01:00
$iconFrame = new Frame();
$manialink->add($iconFrame);
$iconFrame->setPosition($posX, $posY - $itemSize * $itemMarginFactorY);
2014-04-27 18:59:21 +02:00
2013-12-29 15:57:11 +01:00
$backgroundQuad = new Quad();
2014-01-05 12:56:47 +01:00
$iconFrame->add($backgroundQuad);
2013-12-29 15:57:11 +01:00
$backgroundQuad->setSize($itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
2014-04-27 18:59:21 +02:00
$itemQuad = new Quad_Icons64x64_1();
2014-01-05 12:56:47 +01:00
$iconFrame->add($itemQuad);
2013-12-31 13:34:10 +01:00
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_IconPlayers);
2013-12-29 15:57:11 +01:00
$itemQuad->setSize($itemSize, $itemSize);
2014-04-27 18:59:21 +02:00
2014-04-27 19:22:20 +02:00
// Player Menu Description
2014-01-05 12:56:47 +01:00
$descriptionLabel = new Label();
2014-04-27 19:22:20 +02:00
$manialink->add($descriptionLabel);
$descriptionLabel->setPosition($posX - count($this->playerMenuItems) * $itemSize * 1.15 - 6, $posY - $itemSize * $itemMarginFactorY);
2014-01-05 12:56:47 +01:00
$descriptionLabel->setAlign(Control::RIGHT, Control::TOP);
$descriptionLabel->setSize(40, 4);
$descriptionLabel->setTextSize(1.4);
$descriptionLabel->setTextColor('fff');
2014-04-27 18:59:21 +02:00
2014-01-05 12:56:47 +01:00
// Player Menu
2014-01-04 17:55:42 +01:00
$popoutFrame = new Frame();
$manialink->add($popoutFrame);
$popoutFrame->setPosition($posX - $itemSize * 0.5, $posY - $itemSize * $itemMarginFactorY);
$popoutFrame->setHAlign(Control::RIGHT);
$popoutFrame->setSize(4 * $itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
2014-01-12 01:59:23 +01:00
$popoutFrame->setVisible(false);
2014-04-27 18:59:21 +02:00
2014-01-05 12:56:47 +01:00
$backgroundQuad = new Quad();
$popoutFrame->add($backgroundQuad);
$backgroundQuad->setHAlign(Control::RIGHT);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$backgroundQuad->setSize(count($this->playerMenuItems) * $itemSize * 1.15 + 2, $itemSize * $itemMarginFactorY);
2014-04-27 18:59:21 +02:00
$itemQuad->addToggleFeature($popoutFrame);
2014-01-04 17:55:42 +01:00
// Add items
2014-01-04 18:18:46 +01:00
$x = -1;
2014-04-27 18:59:21 +02:00
foreach ($this->playerMenuItems as $menuItems) {
foreach ($menuItems as $menuItem) {
2014-01-05 12:56:47 +01:00
$menuQuad = $menuItem[0];
2014-04-27 19:22:20 +02:00
/**
* @var Quad $menuQuad
*/
2014-01-05 12:56:47 +01:00
$popoutFrame->add($menuQuad);
$menuQuad->setSize($itemSize, $itemSize);
$menuQuad->setX($x);
$menuQuad->setHAlign(Control::RIGHT);
$x -= $itemSize * 1.05;
2014-04-27 18:59:21 +02:00
if ($menuItem[1]) {
2014-04-27 19:22:20 +02:00
$menuQuad->removeScriptFeatures();
2014-01-05 12:56:47 +01:00
$description = '$s' . $menuItem[1];
2014-04-27 18:59:21 +02:00
$menuQuad->addTooltipLabelFeature($descriptionLabel, $description);
2014-01-05 12:56:47 +01:00
}
}
2014-01-04 17:55:42 +01:00
}
2014-04-27 18:59:21 +02:00
return $manialink;
2013-12-29 15:52:01 +01:00
}
}