TrackManiaControl/application/core/Manialinks/IconManager.php

128 lines
2.9 KiB
PHP
Raw Normal View History

2013-12-31 17:09:29 +01:00
<?php
namespace ManiaControl\Manialinks;
use FML\Controls\Frame;
use FML\Controls\Quad;
use FML\ManiaLink;
use ManiaControl\Callbacks\CallbackListener;
2014-03-01 11:11:50 +01:00
use ManiaControl\Callbacks\CallbackManager;
2013-12-31 17:09:29 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Players\Player;
2013-12-31 17:09:29 +01:00
2014-01-05 13:32:59 +01:00
/**
* Class managing Icons
*
* @author steeffeen & kremsy
* @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-05 13:32:59 +01:00
*/
2013-12-31 17:09:29 +01:00
class IconManager implements CallbackListener {
/*
2013-12-31 17:09:29 +01:00
* Constants
*/
2014-01-05 13:32:59 +01:00
const DEFAULT_IMG_URL = 'http://images.maniacontrol.com/icons/';
2014-01-09 17:26:59 +01:00
const PRELOAD_MLID = 'IconManager.Preload.MLID';
2013-12-31 17:09:29 +01:00
/**
* Some Default icons
*/
2014-01-09 17:26:59 +01:00
const MX_ICON = 'ManiaExchange.png';
2014-01-05 00:43:46 +01:00
const MX_ICON_MOVER = 'ManiaExchange_logo_press.png';
2014-01-09 17:26:59 +01:00
2014-01-14 18:19:29 +01:00
const MX_ICON_GREEN = 'ManiaExchangeGreen.png';
2014-01-12 00:28:06 +01:00
const MX_ICON_GREEN_MOVER = 'ManiaExchange_logo_pressGreen.png';
2013-12-31 17:09:29 +01:00
/**
* Private Properties
*/
private $maniaControl = null;
private $icons = array();
/**
* Create a new Icon Manager
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
2014-01-05 13:32:59 +01:00
$this->addDefaultIcons();
2014-01-09 17:26:59 +01:00
2013-12-31 17:09:29 +01: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, 'handlePlayerConnect');
2013-12-31 17:09:29 +01:00
}
2014-01-05 13:32:59 +01:00
/**
* Add the Set of default Icons
*/
private function addDefaultIcons() {
$this->addIcon(self::MX_ICON);
$this->addIcon(self::MX_ICON_MOVER);
2014-01-14 18:19:29 +01:00
$this->addIcon(self::MX_ICON_GREEN);
$this->addIcon(self::MX_ICON_GREEN_MOVER);
2014-01-12 00:28:06 +01:00
2014-01-05 13:32:59 +01:00
}
2013-12-31 17:09:29 +01:00
/**
2014-01-05 13:32:59 +01:00
* Add an Icon
2013-12-31 17:11:53 +01:00
*
2013-12-31 17:09:29 +01:00
* @param string $iconName
* @param string $iconLink
*/
2013-12-31 17:11:53 +01:00
public function addIcon($iconName, $iconLink = self::DEFAULT_IMG_URL) {
2014-01-05 13:32:59 +01:00
$this->icons[$iconName] = $iconLink . '/' . $iconName;
2013-12-31 17:09:29 +01:00
}
/**
2014-01-05 13:32:59 +01:00
* Get an Icon by its name
2013-12-31 17:11:53 +01:00
*
2013-12-31 17:09:29 +01:00
* @param $iconName
* @return string
*/
2013-12-31 17:11:53 +01:00
public function getIcon($iconName) {
2014-01-28 15:56:50 +01:00
if (!isset($this->icons[$iconName])) {
2014-01-05 13:32:59 +01:00
return null;
}
2013-12-31 17:09:29 +01:00
return $this->icons[$iconName];
}
/**
2014-01-05 13:32:59 +01:00
* Handle OnInit Callback
2013-12-31 17:09:29 +01:00
*/
2014-03-01 11:11:50 +01:00
public function handleAfterInit() {
2013-12-31 17:09:29 +01:00
$this->preloadIcons();
}
/**
2014-01-05 13:32:59 +01:00
* Handle PlayerConnect Callback
*
* @param Player $player
2013-12-31 17:09:29 +01:00
*/
public function handlePlayerConnect(Player $player) {
$this->preloadIcons($player);
2013-12-31 17:09:29 +01:00
}
/**
* Preload Icons
2014-01-05 13:32:59 +01:00
*
* @param Player $player
2013-12-31 17:09:29 +01:00
*/
public function preloadIcons($player = null) {
2014-01-05 13:32:59 +01:00
$maniaLink = new ManiaLink(self::PRELOAD_MLID);
2014-01-09 17:26:59 +01:00
$frame = new Frame();
2013-12-31 17:09:29 +01:00
$maniaLink->add($frame);
$frame->setPosition(500, 500);
2014-01-09 17:26:59 +01:00
foreach($this->icons as $iconUrl) {
2013-12-31 17:09:29 +01:00
$iconQuad = new Quad();
$iconQuad->setImage($iconUrl);
2014-01-05 13:32:59 +01:00
$iconQuad->setSize(1, 1);
2013-12-31 17:09:29 +01:00
$frame->add($iconQuad);
}
2014-01-09 17:26:59 +01:00
2013-12-31 17:09:29 +01:00
// Send manialink
2014-03-02 13:00:23 +01:00
$this->maniaControl->manialinkManager->sendManialink($maniaLink, $player);
2013-12-31 17:09:29 +01:00
}
2014-01-15 20:40:14 +01:00
}