TrackManiaControl/application/core/Admin/ActionsMenu.php

268 lines
8.3 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Admin;
use FML\Controls\Control;
use FML\Controls\Frame;
use FML\Controls\Quad;
2014-01-02 17:21:01 +01:00
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\ManiaLink;
use FML\Script\Script;
use ManiaControl\Callbacks\CallbackListener;
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
*/
2013-12-31 12:42:07 +01:00
class ActionsMenu implements CallbackListener, ManialinkPageAnswerListener {
/**
* Constants
*/
2014-01-02 17:21:01 +01: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-01-02 17:21:01 +01: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();
/**
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-01-02 17:21:01 +01:00
// Init settings
2013-12-29 19:21:29 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_POSX, 156.);
2013-12-31 12:24:54 +01:00
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_POSY, -37.);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_MENU_ITEMSIZE, 6.);
2014-01-02 17:21:01 +01:00
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_ONINIT, $this, 'handleOnInit');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerJoined');
2014-01-02 17:21:01 +01:00
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_ADMIN_MENU, $this, 'openAdminMenu');
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_OPEN_PLAYER_MENU, $this, 'openPlayerMenu');
}
/**
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-01-02 17:21:01 +01:00
* @param bool $playerAction
* @param int $order
*/
2013-12-31 12:42:07 +01:00
public function addMenuItem(Control $control, $playerAction = true, $order = 0) {
2014-01-02 17:21:01 +01:00
if($playerAction) {
2013-12-31 12:42:07 +01:00
$this->addPlayerMenuItem($control, $order);
2014-01-02 17:21:01 +01:00
} else {
2013-12-31 12:42:07 +01:00
$this->addAdminMenuItem($control, $order);
}
}
/**
* Add a new Player Menu Item
*
* @param Control $control
2014-01-02 17:21:01 +01:00
* @param int $order
2013-12-31 12:42:07 +01:00
*/
public function addPlayerMenuItem(Control $control, $order = 0) {
2014-01-02 17:21:01 +01:00
if(!isset($this->playerMenuItems[$order])) {
2013-12-31 12:42:07 +01:00
$this->playerMenuItems[$order] = array();
}
array_push($this->playerMenuItems[$order], $control);
}
/**
* Add a new Admin Menu Item
*
* @param Control $control
2014-01-02 17:21:01 +01:00
* @param int $order
2013-12-31 12:42:07 +01:00
*/
public function addAdminMenuItem(Control $control, $order = 0) {
2014-01-02 17:21:01 +01:00
if(!isset($this->adminMenuItems[$order])) {
2013-12-31 12:42:07 +01:00
$this->adminMenuItems[$order] = array();
}
array_push($this->adminMenuItems[$order], $control);
}
/**
* Handle ManiaControl OnInit callback
*
2013-12-31 12:24:54 +01:00
* @param array $callback
*/
2014-01-04 18:49:33 +01:00
public function handleOnInit(array $callback) { //TODO render only once, but howtodo admin check then?
//$manialinkText = $this->buildMenuIconsManialink()->render()->saveXML();
$players = $this->maniaControl->playerManager->getPlayers();
2014-01-02 17:21:01 +01:00
foreach($players as $player) {
2014-01-04 18:49:33 +01:00
$manialinkText = $this->buildMenuIconsManialink($player)->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login);
}
}
/**
* Handle PlayerJoined callback
*
2013-12-31 12:24:54 +01:00
* @param array $callback
*/
2013-12-31 13:32:23 +01:00
public function handlePlayerJoined(array $callback) {
2014-01-02 17:21:01 +01:00
$player = $callback[1];
2014-01-04 18:49:33 +01:00
$manialinkText = $this->buildMenuIconsManialink($player)->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $player->login);
}
2013-12-29 15:52:01 +01:00
/**
2013-12-31 12:42:07 +01:00
* Handle OpenAdminMenu Action
*
2013-12-29 15:52:01 +01:00
* @param array $callback
*/
public function openAdminMenu(array $callback, Player $player) {
}
2013-12-31 12:24:54 +01:00
2013-12-31 12:42:07 +01:00
/**
* Handle OpenPlayerMenu Action
*
* @param array $callback
*/
public function openPlayerMenu(array $callback, Player $player) {
}
2014-01-04 18:49:33 +01:00
/**
* Builds the Manialink
*
* @param Player $player
* @return ManiaLink
*/
2014-01-04 20:50:56 +01:00
private function buildMenuIconsManialink(Player $player) { //TODO Description Labels, close tooltip on click
2014-01-02 17:21:01 +01: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);
$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-01-02 17:21:01 +01:00
2013-12-29 15:52:01 +01:00
$manialink = new ManiaLink(self::MLID_MENU);
2014-01-02 17:21:01 +01:00
2013-12-31 13:34:10 +01:00
// Admin Menu Icon Frame
2013-12-29 15:52:01 +01:00
$frame = new Frame();
$manialink->add($frame);
$frame->setPosition($posX, $posY);
2014-01-02 17:21:01 +01:00
2013-12-29 15:52:01 +01:00
$backgroundQuad = new Quad();
$frame->add($backgroundQuad);
$backgroundQuad->setSize($itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
2014-01-01 21:05:45 +01:00
2013-12-29 15:52:01 +01:00
$iconFrame = new Frame();
$frame->add($iconFrame);
2014-01-02 17:21:01 +01:00
// Create script and features
$script = new Script();
$manialink->setScript($script);
2013-12-29 15:52:01 +01:00
$iconFrame->setSize($itemSize, $itemSize);
$itemQuad = new Quad_Icons64x64_1();
2013-12-31 13:34:10 +01:00
$itemQuad->setSubStyle($itemQuad::SUBSTYLE_IconServers);
2013-12-29 15:52:01 +01:00
$itemQuad->setSize($itemSize, $itemSize);
$iconFrame->add($itemQuad);
2014-01-04 18:18:46 +01:00
2014-01-04 18:49:33 +01:00
if($this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MODERATOR)) {
//Admin Menu
$popoutFrame = new Frame();
$manialink->add($popoutFrame);
$popoutFrame->setPosition($posX - $itemSize * 0.5, $posY);
$popoutFrame->setHAlign(Control::RIGHT);
$popoutFrame->setSize(4 * $itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
$quad = new Quad();
$popoutFrame->add($quad);
$quad->setHAlign(Control::RIGHT);
$quad->setStyles($quadStyle, $quadSubstyle);
2014-01-04 20:50:56 +01:00
$quad->setSize(count($this->adminMenuItems) * $itemSize * 1.15 + 2, $itemSize * $itemMarginFactorY);
2014-01-04 18:49:33 +01:00
$popoutFrame->add($quad);
$script->addTooltip($itemQuad, $popoutFrame, Script::OPTION_TOOLTIP_STAYONCLICK);
// Add items
$x = -1;
foreach(array_reverse($this->adminMenuItems) as $menuItems) {
foreach($menuItems as $menuItem) {
/** @var Quad $menuItem */
$menuItem->setSize($itemSize, $itemSize);
$popoutFrame->add($menuItem);
$menuItem->setX($x);
$menuItem->setHAlign(Control::RIGHT);
$x -= $itemSize * 1.05;
}
2014-01-04 18:18:46 +01:00
}
}
2013-12-31 13:34:10 +01:00
// Player Menu Icon Frame
2013-12-29 15:57:11 +01:00
$frame = new Frame();
$manialink->add($frame);
$frame->setPosition($posX, $posY - $itemSize * $itemMarginFactorY);
2014-01-02 17:21:01 +01:00
2013-12-29 15:57:11 +01:00
$backgroundQuad = new Quad();
$frame->add($backgroundQuad);
$backgroundQuad->setSize($itemSize * $itemMarginFactorX, $itemSize * $itemMarginFactorY);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
2014-01-02 17:21:01 +01:00
2013-12-29 15:57:11 +01:00
$iconFrame = new Frame();
$frame->add($iconFrame);
2014-01-02 17:21:01 +01:00
2013-12-29 15:57:11 +01:00
$iconFrame->setSize($itemSize, $itemSize);
$itemQuad = new Quad_Icons64x64_1();
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);
$iconFrame->add($itemQuad);
2014-01-02 17:21:01 +01:00
2014-01-04 18:18:46 +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-02 17:21:01 +01:00
2014-01-04 18:18:46 +01:00
$quad = new Quad();
$popoutFrame->add($quad);
$quad->setHAlign(Control::RIGHT);
$quad->setStyles($quadStyle, $quadSubstyle);
2014-01-04 20:50:56 +01:00
$quad->setSize(count($this->playerMenuItems) * $itemSize * 1.15 + 2, $itemSize * $itemMarginFactorY);
2014-01-02 17:21:01 +01:00
2014-01-04 18:18:46 +01:00
$popoutFrame->add($quad);
2014-01-04 17:55:42 +01:00
$script->addTooltip($itemQuad, $popoutFrame, Script::OPTION_TOOLTIP_STAYONCLICK);
// Add items
2014-01-04 18:18:46 +01:00
$x = -1;
2014-01-04 18:49:33 +01:00
foreach(array_reverse($this->playerMenuItems) as $menuItems) {
2014-01-04 17:55:42 +01:00
foreach($menuItems as $menuItem) {
/** @var Quad $menuItem */
$menuItem->setSize($itemSize, $itemSize);
$popoutFrame->add($menuItem);
$menuItem->setX($x);
$menuItem->setHAlign(Control::RIGHT);
$x -= $itemSize * 1.05;
}
}
2014-01-02 17:21:01 +01:00
return $manialink;
2013-12-29 15:52:01 +01:00
}
}