TrackManiaControl/application/core/Libs/FML/Controls/Control.php

471 lines
12 KiB
PHP
Raw Normal View History

2014-01-03 17:18:14 +01:00
<?php
namespace FML\Controls;
2014-05-14 23:24:00 +02:00
use FML\Script\Builder;
2014-04-27 14:44:40 +02:00
use FML\Script\Features\ActionTrigger;
2014-05-14 23:24:00 +02:00
use FML\Script\Features\ControlScript;
2014-04-27 14:44:40 +02:00
use FML\Script\Features\MapInfo;
use FML\Script\Features\PlayerProfile;
2014-05-14 23:24:00 +02:00
use FML\Script\Features\ScriptFeature;
2014-04-27 14:44:40 +02:00
use FML\Script\Features\Toggle;
use FML\Script\Features\Tooltip;
2014-05-14 23:24:00 +02:00
use FML\Script\Features\UISound;
use FML\Script\ScriptLabel;
use FML\Types\Renderable;
use FML\Types\ScriptFeatureable;
2014-01-03 17:18:14 +01:00
/**
2014-01-19 19:30:21 +01:00
* Base Control
2014-01-12 00:51:46 +01:00
* (CMlControl)
2014-01-03 17:18:14 +01:00
*
2014-05-14 23:24:00 +02:00
* @author steeffeen
2014-04-13 18:21:40 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-01-03 17:18:14 +01:00
*/
2014-04-27 14:44:40 +02:00
abstract class Control implements Renderable, ScriptFeatureable {
2014-01-21 20:30:40 +01:00
/*
2014-01-03 17:18:14 +01:00
* Constants
*/
2014-05-14 23:24:00 +02:00
const CENTER = 'center';
2014-01-03 17:18:14 +01:00
const CENTER2 = 'center2';
2014-05-14 23:24:00 +02:00
const TOP = 'top';
const RIGHT = 'right';
const BOTTOM = 'bottom';
const LEFT = 'left';
2014-04-13 18:21:40 +02:00
/*
* Static Properties
*/
protected static $currentIndex = 0;
2014-05-14 23:24:00 +02:00
2014-01-21 20:30:40 +01:00
/*
2014-01-03 17:18:14 +01:00
* Protected Properties
*/
protected $tagName = 'control';
protected $id = '';
protected $x = 0.;
protected $y = 0.;
protected $z = 0.;
protected $width = -1.;
protected $height = -1.;
protected $hAlign = self::CENTER;
protected $vAlign = self::CENTER2;
protected $scale = 1.;
protected $hidden = 0;
protected $classes = array();
2014-04-27 14:44:40 +02:00
protected $scriptFeatures = array();
2014-01-03 17:18:14 +01:00
/**
* Construct a new Control
*
2014-01-12 00:51:46 +01:00
* @param string $id (optional) Control Id
2014-01-03 17:18:14 +01:00
*/
public function __construct($id = null) {
2014-01-19 19:30:21 +01:00
if ($id !== null) {
$this->setId($id);
}
2014-01-03 17:18:14 +01:00
}
/**
* Get Control Id
*
2014-04-27 14:44:40 +02:00
* @param bool $escaped (optional) Whether the Id should be escaped for ManiaScript
2014-01-03 17:18:14 +01:00
* @return string
*/
2014-04-27 14:44:40 +02:00
public function getId($escaped = false) {
if ($escaped) {
return Builder::escapeText($this->id);
}
2014-01-03 17:18:14 +01:00
return $this->id;
}
/**
* Set Control Id
*
* @param string $id Control Id
* @return \FML\Controls\Control
*/
public function setId($id) {
2014-05-14 23:24:00 +02:00
$this->id = (string)$id;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
2014-04-13 18:21:40 +02:00
* Check Id for dangerous Characters and assign a unique Id if necessary
2014-01-03 17:18:14 +01:00
*
2014-05-14 23:24:00 +02:00
* @param bool $forceNewId Whether to force setting a newly generated Id
2014-01-03 17:18:14 +01:00
* @return \FML\Controls\Control
*/
2014-05-14 23:24:00 +02:00
public function checkId($forceNewId = false) {
if ($forceNewId || !$this->getId()) {
2014-04-13 18:21:40 +02:00
$this->setId('FML_ID_' . self::$currentIndex);
self::$currentIndex++;
2014-01-03 17:18:14 +01:00
return $this;
}
$dangerousCharacters = array(' ', ' ', '.', '|', '-', PHP_EOL);
2014-05-14 23:24:00 +02:00
$idCharacters = str_split($this->getId());
$danger = false;
2014-01-03 17:18:14 +01:00
foreach ($idCharacters as $character) {
2014-05-14 23:24:00 +02:00
if (!in_array($character, $dangerousCharacters)) {
continue;
}
2014-01-03 17:18:14 +01:00
$danger = true;
break;
}
if ($danger) {
trigger_error("Please don't use special Characters in Ids, they might cause Problems! (I stripped them for You.)");
$id = str_ireplace($dangerousCharacters, '', $this->getId());
$this->setId($id);
}
return $this;
}
/**
* Set X Position
*
* @param float $x Horizontal Position
* @return \FML\Controls\Control
*/
public function setX($x) {
2014-05-14 23:24:00 +02:00
$this->x = (float)$x;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Y Position
*
* @param float $y Vertical Position
* @return \FML\Controls\Control
*/
public function setY($y) {
2014-05-14 23:24:00 +02:00
$this->y = (float)$y;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Z Position
*
* @param float $z Depth
* @return \FML\Controls\Control
*/
public function setZ($z) {
2014-05-14 23:24:00 +02:00
$this->z = (float)$z;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Control Position
*
* @param float $x Horizontal Position
* @param float $y Vertical Position
2014-01-12 00:51:46 +01:00
* @param float $z (optional) Depth
2014-01-03 17:18:14 +01:00
* @return \FML\Controls\Control
*/
public function setPosition($x, $y, $z = null) {
$this->setX($x);
$this->setY($y);
if ($z !== null) {
$this->setZ($z);
}
return $this;
}
/**
* Set Control Width
*
* @param float $width Control Width
* @return \FML\Controls\Control
*/
public function setWidth($width) {
2014-05-14 23:24:00 +02:00
$this->width = (float)$width;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Control Height
*
* @param float $height Control Height
* @return \FML\Controls\Control
*/
public function setHeight($height) {
2014-05-14 23:24:00 +02:00
$this->height = (float)$height;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Control Size
*
2014-05-14 23:24:00 +02:00
* @param float $width Control Width
2014-01-03 17:18:14 +01:00
* @param float $height Control Height
* @return \FML\Controls\Control
*/
public function setSize($width, $height) {
$this->setWidth($width);
$this->setHeight($height);
return $this;
}
/**
* Set Horizontal Alignment
*
* @param string $hAlign Horizontal Alignment
* @return \FML\Controls\Control
*/
public function setHAlign($hAlign) {
2014-05-14 23:24:00 +02:00
$this->hAlign = (string)$hAlign;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Vertical Alignment
*
* @param string $vAlign Vertical Alignment
* @return \FML\Controls\Control
*/
public function setVAlign($vAlign) {
2014-05-14 23:24:00 +02:00
$this->vAlign = (string)$vAlign;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Horizontal and Vertical Alignment
*
* @param string $hAlign Horizontal Alignment
* @param string $vAlign Vertical Alignment
* @return \FML\Controls\Control
*/
public function setAlign($hAlign, $vAlign) {
$this->setHAlign($hAlign);
$this->setVAlign($vAlign);
return $this;
}
2014-05-14 23:24:00 +02:00
/**
* Center Alignment
*
* @return \FML\Controls\Control
*/
public function centerAlign() {
$this->setAlign(self::CENTER, self::CENTER2);
return $this;
}
/**
* Reset Alignment
*
* @return \FML\Controls\Control
*/
public function resetAlign() {
$this->setAlign(null, null);
return $this;
}
2014-01-03 17:18:14 +01:00
/**
* Set Control Scale
*
* @param float $scale Control Scale
* @return \FML\Controls\Control
*/
public function setScale($scale) {
2014-05-14 23:24:00 +02:00
$this->scale = (float)$scale;
2014-01-03 17:18:14 +01:00
return $this;
}
/**
* Set Visibility
*
2014-01-12 00:51:46 +01:00
* @param bool $visible Whether Control should be visible
2014-01-03 17:18:14 +01:00
* @return \FML\Controls\Control
*/
2014-05-14 23:24:00 +02:00
public function setVisible($visible = true) {
2014-01-03 17:18:14 +01:00
$this->hidden = ($visible ? 0 : 1);
return $this;
}
/**
* Add new Class Name
*
* @param string $class Class Name
* @return \FML\Controls\Control
*/
public function addClass($class) {
2014-05-14 23:24:00 +02:00
$class = (string)$class;
2014-01-03 17:18:14 +01:00
if (!in_array($class, $this->classes)) {
array_push($this->classes, $class);
}
return $this;
}
2014-04-27 14:44:40 +02:00
/**
* Add a dynamic Action Trigger
*
* @param string $actionName Action to trigger
* @param string $eventLabel (optional) Event on which the Action is triggered
* @return \FML\Controls\Control
*/
public function addActionTriggerFeature($actionName, $eventLabel = ScriptLabel::MOUSECLICK) {
$actionTrigger = new ActionTrigger($actionName, $this, $eventLabel);
2014-05-14 23:24:00 +02:00
$this->addScriptFeature($actionTrigger);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Add a dynamic Feature opening the current Map Info
*
* @param string $eventLabel (optional) Event on which the Map Info will be opened
* @return \FML\Controls\Control
*/
public function addMapInfoFeature($eventLabel = ScriptLabel::MOUSECLICK) {
$mapInfo = new MapInfo($this, $eventLabel);
2014-05-14 23:24:00 +02:00
$this->addScriptFeature($mapInfo);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Add a dynamic Feature to open a specific Player Profile
*
2014-05-14 23:24:00 +02:00
* @param string $login The Login of the Player
2014-04-27 14:44:40 +02:00
* @param string $eventLabel (optional) Event on which the Player Profile will be opened
* @return \FML\Controls\Control
*/
public function addPlayerProfileFeature($login, $eventLabel = ScriptLabel::MOUSECLICK) {
$playerProfile = new PlayerProfile($login, $this, $eventLabel);
2014-05-14 23:24:00 +02:00
$this->addScriptFeature($playerProfile);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Add a dynamic Feature playing an UISound
*
2014-05-14 23:24:00 +02:00
* @param string $soundName UISound Name
* @param int $variant (optional) Sound Variant
2014-04-27 14:44:40 +02:00
* @param string $eventLabel (optional) Event on which the Sound will be played
* @return \FML\Controls\Control
*/
public function addUISoundFeature($soundName, $variant = 0, $eventLabel = ScriptLabel::MOUSECLICK) {
$uiSound = new UISound($soundName, $this, $variant, $eventLabel);
2014-05-14 23:24:00 +02:00
$this->addScriptFeature($uiSound);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Add a dynamic Feature toggling another Control
*
* @param Control $toggledControl Toggled Control
2014-05-14 23:24:00 +02:00
* @param string $labelName (optional) Script Label Name
* @param bool $onlyShow (optional) Whether it should only Show the Control but not toggle
* @param bool $onlyHide (optional) Whether it should only Hide the Control but not toggle
2014-04-27 14:44:40 +02:00
* @return \FML\Controls\Control
*/
public function addToggleFeature(Control $toggledControl, $labelName = Scriptlabel::MOUSECLICK, $onlyShow = false, $onlyHide = false) {
$toggle = new Toggle($this, $toggledControl, $labelName, $onlyShow, $onlyHide);
2014-05-14 23:24:00 +02:00
$this->addScriptFeature($toggle);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Add a dynamic Feature showing a Tooltip on hovering
*
* @param Control $tooltipControl Tooltip Control
2014-05-14 23:24:00 +02:00
* @param bool $stayOnClick (optional) Whether the Tooltip should stay on Click
* @param bool $invert (optional) Whether the Visibility Toggling should be inverted
2014-04-27 14:44:40 +02:00
* @return \FML\Controls\Control
*/
public function addTooltipFeature(Control $tooltipControl, $stayOnClick = false, $invert = false) {
$tooltip = new Tooltip($this, $tooltipControl, $stayOnClick, $invert);
2014-05-14 23:24:00 +02:00
$this->addScriptFeature($tooltip);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Add a dynamic Feature showing a Tooltip on hovering
*
2014-05-14 23:24:00 +02:00
* @param Label $tooltipControl Tooltip Control
* @param string $text The Text to display on the Tooltip Label
* @param bool $stayOnClick (optional) Whether the Tooltip should stay on Click
* @param bool $invert (optional) Whether the Visibility Toggling should be inverted
2014-04-27 14:44:40 +02:00
* @return \FML\Controls\Control
*/
public function addTooltipLabelFeature(Label $tooltipControl, $text, $stayOnClick = false, $invert = false) {
$tooltip = new Tooltip($this, $tooltipControl, $stayOnClick, $invert, $text);
2014-05-14 23:24:00 +02:00
$this->addScriptFeature($tooltip);
return $this;
}
/**
* Add a Custom Control Script Text Part
*
* @param string $scriptText Script Text
* @param string $label (optional) Script Label Name
* @param bool $isolated (optional) Whether to isolate the Script Text
* @return \FML\Controls\Control
*/
public function addScriptText($scriptText, $label = ScriptLabel::MOUSECLICK, $isolated = true) {
$customText = new ControlScript($this, $scriptText, $label, $isolated);
$this->addScriptFeature($customText);
return $this;
}
/**
* Add a Script Feature
*
* @param ScriptFeature $scriptFeature Script Feature
* @return \FML\Controls\Control
*/
public function addScriptFeature(ScriptFeature $scriptFeature) {
array_push($this->scriptFeatures, $scriptFeature);
2014-04-27 14:44:40 +02:00
return $this;
}
2014-04-27 19:22:20 +02:00
/**
* Remove all Script Features
2014-05-14 23:24:00 +02:00
*
2014-04-27 19:22:20 +02:00
* @return \FML\Controls\Control
*/
public function removeScriptFeatures() {
$this->scriptFeatures = array();
return $this;
}
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
/**
* @see \FML\Types\ScriptFeatureable::getScriptFeatures()
*/
public function getScriptFeatures() {
return $this->scriptFeatures;
}
2014-01-03 17:18:14 +01:00
/**
* @see \FML\Types\Renderable::render()
*/
public function render(\DOMDocument $domDocument) {
2014-01-12 00:51:46 +01:00
$xmlElement = $domDocument->createElement($this->tagName);
2014-01-03 17:18:14 +01:00
if ($this->id) {
2014-01-12 00:51:46 +01:00
$xmlElement->setAttribute('id', $this->id);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if ($this->x != 0. || $this->y != 0. || $this->z != 0.) {
$xmlElement->setAttribute('posn', "{$this->x} {$this->y} {$this->z}");
2014-01-03 17:18:14 +01:00
}
if ($this->width >= 0. || $this->height >= 0.) {
2014-01-12 00:51:46 +01:00
$xmlElement->setAttribute('sizen', "{$this->width} {$this->height}");
2014-01-03 17:18:14 +01:00
}
if ($this->hAlign) {
2014-01-12 00:51:46 +01:00
$xmlElement->setAttribute('halign', $this->hAlign);
2014-01-03 17:18:14 +01:00
}
if ($this->vAlign) {
2014-01-12 00:51:46 +01:00
$xmlElement->setAttribute('valign', $this->vAlign);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if ($this->scale != 1.) {
$xmlElement->setAttribute('scale', $this->scale);
2014-01-03 17:18:14 +01:00
}
if ($this->hidden) {
2014-01-12 00:51:46 +01:00
$xmlElement->setAttribute('hidden', $this->hidden);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
if (!empty($this->classes)) {
2014-04-27 14:44:40 +02:00
$classes = implode(' ', $this->classes);
2014-01-12 00:51:46 +01:00
$xmlElement->setAttribute('class', $classes);
2014-01-03 17:18:14 +01:00
}
2014-01-12 00:51:46 +01:00
return $xmlElement;
2014-01-03 17:18:14 +01:00
}
}