TrackManiaControl/application/core/Manialinks/IconManager.php

128 lines
2.8 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;
use ManiaControl\ManiaControl;
use ManiaControl\Players\PlayerManager;
2014-01-05 13:32:59 +01:00
/**
* Class managing Icons
*
* @author steeffeen & kremsy
*/
2013-12-31 17:09:29 +01:00
class IconManager implements CallbackListener {
/**
* 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
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_ONINIT, $this, 'handleOnInit');
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
* @param array $callback
*/
2013-12-31 17:11:53 +01:00
public function handleOnInit(array $callback) {
2013-12-31 17:09:29 +01:00
$this->preloadIcons();
}
/**
2014-01-05 13:32:59 +01:00
* Handle PlayerConnect Callback
*
2013-12-31 17:09:29 +01:00
* @param array $callback
*/
2013-12-31 17:11:53 +01:00
public function handlePlayerConnect(array $callback) {
2014-01-05 13:32:59 +01:00
$login = $callback[1];
$this->preloadIcons($login);
2013-12-31 17:09:29 +01:00
}
/**
* Preload Icons
2014-01-05 13:32:59 +01:00
*
* @param string $login
2013-12-31 17:09:29 +01:00
*/
2014-01-15 20:40:14 +01:00
public function preloadIcons($login = false) {
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
$manialinkText = $maniaLink->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $login);
}
2014-01-15 20:40:14 +01:00
}