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

126 lines
2.9 KiB
PHP
Raw Normal View History

2014-05-14 23:24:00 +02:00
<?php
namespace FML\Script\Features;
use FML\Controls\Control;
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
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ControlScript extends ScriptFeature {
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-05-14 23:24:00 +02:00
*/
/** @var Control $control */
protected $control = null;
protected $labelName = null;
protected $text = null;
/**
2014-06-21 03:18:21 +02:00
* Construct a new Control Script
2014-05-14 23:24:00 +02:00
*
* @param Control $control Event Control
2014-06-21 03:18:21 +02:00
* @param string $text Script text
* @param string $labelName (optional) Script Label name
2014-05-14 23:24:00 +02:00
*/
2014-05-16 22:44:22 +02:00
public function __construct(Control $control, $text, $labelName = ScriptLabel::MOUSECLICK) {
2014-05-14 23:24:00 +02:00
$this->setControl($control);
$this->setText($text);
$this->setLabelName($labelName);
}
/**
* Set the Control
*
2014-06-21 03:18:21 +02:00
* @param Control $control Event Control
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
public function setControl(Control $control) {
2014-06-21 03:18:21 +02:00
$this->control = $control->checkId();
2014-05-20 15:44:45 +02:00
$this->updateScriptEvents();
2014-05-14 23:24:00 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the script text
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $text Script text
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
public function setText($text) {
$this->text = (string)$text;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the label name
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $labelName Script Label name
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
public function setLabelName($labelName) {
2014-06-21 03:18:21 +02:00
$this->labelName = (string)$labelName;
2014-05-20 15:44:45 +02:00
$this->updateScriptEvents();
2014-05-14 23:24:00 +02:00
return $this;
}
2014-05-20 15:44:45 +02:00
/**
* Enable Script Events on the Control if needed
*/
protected function updateScriptEvents() {
2014-06-21 03:18:21 +02:00
if (!$this->control || !ScriptLabel::isEventLabel($this->labelName)) {
2014-05-20 15:44:45 +02:00
return;
}
if ($this->control instanceof Scriptable) {
$this->control->setScriptEvents(true);
}
}
2014-05-14 23:24:00 +02:00
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
2014-05-20 15:44:45 +02:00
$isolated = !ScriptLabel::isEventLabel($this->labelName);
$script->appendGenericScriptLabel($this->labelName, $this->buildScriptText(), $isolated);
2014-05-14 23:24:00 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Build the script text for the Control
2014-05-14 23:24:00 +02:00
*
* @return string
*/
2014-05-16 22:44:22 +02:00
protected function buildScriptText() {
2014-05-14 23:24:00 +02:00
$controlId = $this->control->getId(true);
2014-05-16 22:44:22 +02:00
$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 . '");';
}
$class = $this->control->getManiaScriptClass();
$name = preg_replace('/^CMl/', '', $class, 1);
$scriptText .= '
declare ' . $name . ' <=> (Control as ' . $class . ');
';
$scriptText .= $this->text . '
';
if ($closeBlock) {
$scriptText .= '}';
}
2014-05-14 23:24:00 +02:00
return $scriptText;
}
}