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

139 lines
3.5 KiB
PHP
Raw Normal View History

2014-04-27 14:44:40 +02:00
<?php
namespace FML\Script\Features;
use FML\Controls\Control;
use FML\Script\Script;
use FML\Script\ScriptLabel;
2014-05-14 23:24:00 +02:00
use FML\Types\Scriptable;
2014-04-27 14:44:40 +02:00
/**
* Script Feature for toggling Controls
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
2014-04-27 14:44: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-04-27 14:44:40 +02:00
*/
class Toggle extends ScriptFeature {
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-04-27 14:44:40 +02:00
*/
2014-05-14 23:24:00 +02:00
/** @var Control $togglingControl */
2014-04-27 14:44:40 +02:00
protected $togglingControl = null;
2014-05-14 23:24:00 +02:00
/** @var Control $toggledControl */
2014-04-27 14:44:40 +02:00
protected $toggledControl = null;
protected $labelName = null;
protected $onlyShow = null;
protected $onlyHide = null;
/**
* Construct a new Toggle Feature
*
* @param Control $togglingControl (optional) Toggling Control
2014-05-14 23:24:00 +02:00
* @param Control $toggledControl (optional) Toggled Control
2014-06-21 03:18:21 +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
*/
2014-06-21 03:18:21 +02:00
public function __construct(Control $togglingControl = null, Control $toggledControl = null, $labelName = ScriptLabel::MOUSECLICK,
$onlyShow = false, $onlyHide = false) {
2014-08-11 23:15:53 +02:00
if ($togglingControl !== null) {
2014-06-21 03:18:21 +02:00
$this->setTogglingControl($togglingControl);
}
2014-08-11 23:15:53 +02:00
if ($toggledControl !== null) {
2014-06-21 03:18:21 +02:00
$this->setToggledControl($toggledControl);
}
2014-04-27 14:44:40 +02:00
$this->setLabelName($labelName);
$this->setOnlyShow($onlyShow);
$this->setOnlyHide($onlyHide);
}
/**
2014-06-21 03:18:21 +02:00
* Set the toggling Control
2014-04-27 14:44:40 +02:00
*
* @param Control $control Toggling Control
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setTogglingControl(Control $control) {
$control->checkId();
2014-05-14 23:24:00 +02:00
if ($control instanceof Scriptable) {
$control->setScriptEvents(true);
}
2014-04-27 14:44:40 +02:00
$this->togglingControl = $control;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the toggled Control
2014-04-27 14:44:40 +02:00
*
* @param Control $control Toggling Control
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setToggledControl(Control $control) {
2014-06-21 03:18:21 +02:00
$this->toggledControl = $control->checkId();
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the label name
2014-04-27 14:44:40 +02:00
*
* @param string $labelName Script Label Name
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setLabelName($labelName) {
2014-05-14 23:24:00 +02:00
$this->labelName = (string)$labelName;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set to only show
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param bool $onlyShow Whether it should only show the Control but not toggle
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setOnlyShow($onlyShow) {
2014-05-14 23:24:00 +02:00
$this->onlyShow = (bool)$onlyShow;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set to only hide
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param bool $onlyHide Whether it should only hide the Control but not toggle
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setOnlyHide($onlyHide) {
2014-05-14 23:24:00 +02:00
$this->onlyHide = (bool)$onlyHide;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
$script->appendGenericScriptLabel($this->labelName, $this->getScriptText());
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Get the script text
2014-04-27 14:44:40 +02:00
*
* @return string
*/
protected function getScriptText() {
2014-06-21 03:18:21 +02:00
$togglingControlId = $this->togglingControl->getId(true, true);
$toggledControlId = $this->toggledControl->getId(true, true);
2014-05-14 23:24:00 +02:00
$visibility = '!ToggleControl.Visible';
2014-04-27 14:44:40 +02:00
if ($this->onlyShow) {
$visibility = 'True';
2014-05-14 23:24:00 +02:00
} else if ($this->onlyHide) {
2014-04-27 14:44:40 +02:00
$visibility = 'False';
}
2014-06-21 03:18:21 +02:00
return "
if (Event.Control.ControlId == {$togglingControlId}) {
declare ToggleControl = Page.GetFirstChild({$toggledControlId});
2014-04-27 14:44:40 +02:00
ToggleControl.Visible = {$visibility};
}";
}
}