TrackManiaControl/libs/FML/Script/Features/ControlScript.php

206 lines
4.5 KiB
PHP
Raw Permalink Normal View History

2014-05-14 23:24:00 +02:00
<?php
namespace FML\Script\Features;
use FML\Controls\Control;
2017-03-25 18:40:15 +01:00
use FML\Script\Builder;
2014-05-14 23:24:00 +02:00
use FML\Script\Script;
use FML\Script\ScriptLabel;
use FML\Types\Scriptable;
/**
2014-06-21 03:18:21 +02:00
* Script Feature for a Control related script
2014-05-14 23:24:00 +02:00
*
* @author steeffeen
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2017-03-25 18:40:15 +01:00
class ControlScript extends ScriptFeature
{
/**
* @var Control $control Control
*/
protected $control = null;
/**
* @var string $labelName Script Label name
*/
protected $labelName = null;
/**
* @var string $scriptText Script text
*/
protected $scriptText = null;
/**
* Construct a new Control Script
*
* @api
* @param Control $control (optional) Control
* @param string $scriptText (optional) Script text
* @param string $labelName (optional) Script Label name
*/
public function __construct(Control $control = null, $scriptText = null, $labelName = ScriptLabel::MOUSECLICK)
{
if ($control) {
$this->setControl($control);
}
if ($scriptText) {
$this->setScriptText($scriptText);
}
if ($labelName) {
$this->setLabelName($labelName);
}
}
/**
* Get the Control
*
* @api
* @return Control
*/
public function getControl()
{
return $this->control;
}
/**
* Set the Control
*
* @api
* @param Control $control Control
* @return static
*/
public function setControl(Control $control)
{
$control->checkId();
2017-04-02 16:32:57 +02:00
$control->addScriptFeature($this);
2017-03-25 18:40:15 +01:00
$this->control = $control;
$this->updateScriptEvents();
return $this;
}
/**
* Get the script text
*
* @api
* @return string
*/
public function getScriptText()
{
return $this->scriptText;
}
/**
* Set the script text
*
* @api
* @param string $scriptText Script text
* @return static
*/
public function setScriptText($scriptText)
{
$this->scriptText = (string)$scriptText;
return $this;
}
2017-04-02 16:32:57 +02:00
/**
* Set the script text
*
* @api
* @param string $text Text
* @return static
* @deprecated Use setScriptText()
* @see ControlScript::setScriptText()
*/
public function setText($text)
{
return $this->setScriptText($text);
}
2017-03-25 18:40:15 +01:00
/**
* Get the Script Label name
*
* @api
* @return string
*/
public function getLabelName()
{
return $this->labelName;
}
/**
* Set the Script Label name
*
* @api
* @param string $labelName Script Label name
* @return static
*/
public function setLabelName($labelName)
{
$this->labelName = (string)$labelName;
$this->updateScriptEvents();
return $this;
}
/**
* Enable Script Events on the Control if needed
*
* @return static
*/
protected function updateScriptEvents()
{
if (!$this->control || !ScriptLabel::isEventLabel($this->labelName)) {
return $this;
}
if ($this->control instanceof Scriptable) {
$this->control->setScriptEvents(true);
}
return $this;
}
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
$isolated = !ScriptLabel::isEventLabel($this->labelName);
$script->appendGenericScriptLabel($this->labelName, $this->buildScriptText(), $isolated);
return $this;
}
/**
* Build the script text for the Control
*
* @return string
*/
protected function buildScriptText()
{
$controlId = Builder::escapeText($this->control->getId());
$scriptText = '';
$closeBlock = false;
if (ScriptLabel::isEventLabel($this->labelName)) {
$scriptText .= "
if (Event.ControlId == {$controlId}) {
declare Control <=> Event.Control;";
$closeBlock = true;
} else {
$scriptText .= "
declare Control <=> Page.GetFirstChild({$controlId});";
}
2017-04-02 16:32:57 +02:00
$class = $this->control->getManiaScriptClass();
$name = preg_replace('/^CMl/', '', $class, 1);
2017-03-25 18:40:15 +01:00
$scriptText .= "
declare {$name} <=> (Control as {$class});
";
$scriptText .= $this->scriptText . "
";
if ($closeBlock) {
$scriptText .= "}";
}
return $scriptText;
}
2014-05-14 23:24:00 +02:00
}