TrackManiaControl/core/Manialinks/IconManager.php

129 lines
3.0 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\Callbacks\Callbacks;
2013-12-31 17:09:29 +01:00
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
2014-05-02 17:50:30 +02:00
use ManiaControl\Players\PlayerManager;
2013-12-31 17:09:29 +01:00
2014-01-05 13:32:59 +01:00
/**
* Class managing Icons
*
2014-05-02 17:50:30 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @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';
/*
* Default icons
2013-12-31 17:09:29 +01:00
*/
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';
/*
* Private properties
2013-12-31 17:09:29 +01:00
*/
/** @var ManiaControl $maniaControl */
2013-12-31 17:09:29 +01:00
private $maniaControl = null;
private $icons = array();
/**
* Construct a new icon manager instance
2013-12-31 17:09:29 +01:00
*
* @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
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
$this->maniaControl->getCallbackManager()->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
2014-01-05 13:32:59 +01:00
*/
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-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
}
/**
* Get an Icon by its Name
2013-12-31 17:11:53 +01:00
*
* @param string $iconName
2013-12-31 17:09:29 +01:00
* @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();
}
/**
* 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
2014-05-02 17:50:30 +02: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-08-13 11:05:52 +02:00
$this->maniaControl->getManialinkManager()->sendManialink($maniaLink, $player);
2013-12-31 17:09:29 +01:00
}
2014-05-02 17:50:30 +02:00
/**
* Handle PlayerConnect Callback
*
* @param Player $player
*/
public function handlePlayerConnect(Player $player) {
$this->preloadIcons($player);
}
}