added icon manager

This commit is contained in:
kremsy 2013-12-31 17:09:29 +01:00
parent c2fe0a32db
commit 1a1d957d69
3 changed files with 113 additions and 5 deletions

View File

@ -0,0 +1,103 @@
<?php
/**
* Class managing Icon
*
* @author steeffeen & kremsy
*/
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;
class IconManager implements CallbackListener {
/**
* Constants
*/
const DEFAULT_IMG_URL = "http://images.maniacontrol.com/icons/";
const PRELOAD_ML_ID = "IconManager.Preload";
/**
* Some Default icons
*/
const MX_ICON = 'ManiaExchange.png';
/**
* Private Properties
*/
private $maniaControl = null;
private $icons = array();
/**
* Create a new Icon Manager
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Register for callbacks
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_ONINIT, $this, 'handleOnInit');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERJOINED, $this, 'handlePlayerConnect');
}
/**
* Adds an Icon
* @param string $iconName
* @param string $iconLink
*/
public function addIcon($iconName, $iconLink = self::DEFAULT_IMG_URL){
$this->icons[$iconName] = $iconLink . "/" . $iconName;
}
/**
* Gets an Icon by its name
* @param $iconName
* @return string
*/
public function getIcon($iconName){
return $this->icons[$iconName];
}
/**
* @param array $callback
*/
public function handleOnInit(array $callback){
$this->preloadIcons();
}
/**
* @param array $callback
*/
public function handlePlayerConnect(array $callback){
$this->preloadIcons($callback[1]);
}
/**
* Preload Icons
*/
private function preloadIcons($login = false){
$maniaLink = new ManiaLink(self::PRELOAD_ML_ID);
$frame = new Frame();
$maniaLink->add($frame);
$frame->setPosition(500, 500);
foreach($this->icons as $iconUrl){
$iconQuad = new Quad();
$iconQuad->setImage($iconUrl);
$iconQuad->setSize(10, 10);
$frame->add($iconQuad);
}
// Send manialink
$manialinkText = $maniaLink->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $login);
}
}

View File

@ -13,6 +13,7 @@ use FML\Controls\Frame;
use FML\Controls\Labels\Label_Text;
require_once __DIR__ . '/StyleManager.php';
require_once __DIR__ . '/IconManager.php';
require_once __DIR__ . '/CustomUIManager.php';
require_once __DIR__ . '/../FML/autoload.php';
@ -35,6 +36,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
*/
public $styleManager = null;
public $customUIManager = null;
public $iconManager = null;
/**
* Private properties
@ -52,6 +54,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener
$this->maniaControl = $maniaControl;
$this->styleManager = new StyleManager($maniaControl);
$this->customUIManager = new CustomUIManager($maniaControl);
$this->iconManager = new IconManager($maniaControl);
// Register for callbacks
$this->registerManialinkPageAnswerListener(self::ACTION_CLOSEWIDGET, $this, 'closeWidgetCallback');

View File

@ -10,6 +10,7 @@ use FML\Script\Script;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\IconManager;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\Plugin;
@ -115,6 +116,7 @@ class WidgetPlugin implements CallbackListener, Plugin {
$this->maniaControl->settingManager->initSetting($this, self::SETTING_CLOCK_WIDGET_WIDTH, 10);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_CLOCK_WIDGET_HEIGHT, 5.5);
$this->maniaControl->manialinkManager->iconManager->addIcon(IconManager::MX_ICON);
return true;
}
@ -201,7 +203,7 @@ class WidgetPlugin implements CallbackListener, Plugin {
if (isset($map->mx->pageurl)) {
$quad = new Quad();
$frame->add($quad);
$quad->setImage("http://images.maniacontrol.com/icons/ManiaExchange.png");
$quad->setImage($this->maniaControl->manialinkManager->iconManager->getIcon(IconManager::MX_ICON));
$quad->setPosition(-$width / 2 + 4, -1.5, -0.5);
$quad->setSize(4, 4);
$quad->setHAlign(Control::CENTER);