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

327 lines
7.7 KiB
PHP
Raw Permalink Normal View History

2014-05-14 23:24:00 +02:00
<?php
namespace FML\Script\Features;
2017-03-25 18:40:15 +01:00
use FML\Components\CheckBoxDesign;
2014-05-14 23:24:00 +02:00
use FML\Controls\Entry;
use FML\Controls\Quad;
use FML\Script\Builder;
use FML\Script\Script;
use FML\Script\ScriptInclude;
use FML\Script\ScriptLabel;
/**
2014-06-21 03:18:21 +02:00
* Script Feature for creating a CheckBox behavior
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 CheckBoxFeature extends ScriptFeature
{
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/*
* Constants
*/
const FUNCTION_UPDATE_QUAD_DESIGN = "FML_UpdateQuadDesign";
const VAR_CHECKBOX_ENABLED = "FML_CheckBox_Enabled";
const VAR_CHECKBOX_DESIGNS = "FML_CheckBox_Designs";
const VAR_CHECKBOX_ENTRY_ID = "FML_CheckBox_EntryId";
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var Quad $quad CheckBox Quad
*/
protected $quad = null;
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var Entry $entry Hidden Entry for submitting the value
*/
protected $entry = null;
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var bool $default Default value
*/
protected $default = null;
2014-05-16 22:44:22 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var CheckBoxDesign $enabledDesign Enabled Design
*/
protected $enabledDesign = null;
2014-05-18 19:45:50 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var CheckBoxDesign $disabledDesign Disabled Design
*/
protected $disabledDesign = null;
2014-05-14 23:24:00 +02:00
2023-05-25 13:03:31 +02:00
/**
* @var string $customScript Script executed when clicking on the checkbox
*/
protected $customScript = "";
2017-03-25 18:40:15 +01:00
/**
* Construct a new CheckBox Feature
*
* @api
* @param Quad $quad (optional) CheckBox Quad
* @param Entry $entry (optional) Hidden Entry
* @param bool $default (optional) Default value
*/
public function __construct(Quad $quad = null, Entry $entry = null, $default = null)
{
if ($quad) {
$this->setQuad($quad);
}
if ($entry) {
$this->setEntry($entry);
}
if ($default !== null) {
$this->setDefault($default);
}
$this->setEnabledDesign(CheckBoxDesign::defaultDesign());
$this->setDisabledDesign(CheckBoxDesign::defaultDesign());
}
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/**
* Get the CheckBox Quad
*
* @api
* @return Quad
*/
public function getQuad()
{
return $this->quad;
}
/**
* Set the CheckBox Quad
*
* @api
* @param Quad $quad CheckBox Quad
* @return static
*/
public function setQuad(Quad $quad)
{
$quad->checkId();
$quad->setScriptEvents(true);
$this->quad = $quad;
return $this;
}
/**
* Get the hidden Entry
*
* @api
* @return Entry
*/
public function getEntry()
{
return $this->entry;
}
/**
* Set the hidden Entry
*
* @api
* @param Entry $entry Hidden Entry
* @return static
*/
public function setEntry(Entry $entry)
{
$entry->checkId();
$this->entry = $entry;
return $this;
}
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/**
* Get the default value
*
* @api
* @return bool
*/
public function getDefault()
{
return $this->default;
}
/**
* Set the default value
*
* @api
* @param bool $default Default value
* @return static
*/
public function setDefault($default)
{
$this->default = (bool)$default;
return $this;
}
/**
* Get the enabled Design
*
* @api
* @return CheckBoxDesign
*/
public function getEnabledDesign()
{
return $this->enabledDesign;
}
/**
* Set the enabled Design
*
* @api
* @param CheckBoxDesign $checkBoxDesign Enabled CheckBox Design
* @return static
*/
public function setEnabledDesign(CheckBoxDesign $checkBoxDesign)
{
$this->enabledDesign = $checkBoxDesign;
return $this;
}
/**
* Get the disabled Design
*
* @api
* @return CheckBoxDesign
*/
public function getDisabledDesign()
{
return $this->disabledDesign;
}
/**
* Set the disabled Design
*
* @api
* @param CheckBoxDesign $checkBoxDesign Disabled CheckBox Design
* @return static
*/
public function setDisabledDesign(CheckBoxDesign $checkBoxDesign)
{
$this->disabledDesign = $checkBoxDesign;
return $this;
}
2023-05-25 13:03:31 +02:00
/**
* Get script launched when clicking on the checkbox
*
* @api
* @return string
*/
public function getCustomScript()
{
return $this->customScript;
}
/**
* Set script launched when clicking on the checkbox
*
* @api
* @param string $customScript script
* @return static
*/
public function setCustomScript(string $customScript)
{
$this->customScript = $customScript;
return $this;
}
2017-03-25 18:40:15 +01:00
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
if ($this->getQuad()) {
$script->setScriptInclude(ScriptInclude::TEXTLIB);
$script->addScriptFunction(self::FUNCTION_UPDATE_QUAD_DESIGN, $this->buildUpdateQuadDesignFunction());
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->buildInitScriptText(), true);
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $this->buildClickScriptText());
}
return $this;
}
/**
* Build the function text
*
* @return string
*/
protected function buildUpdateQuadDesignFunction()
{
return "
2014-05-14 23:24:00 +02:00
Void " . self::FUNCTION_UPDATE_QUAD_DESIGN . "(CMlQuad _Quad) {
declare " . self::VAR_CHECKBOX_ENABLED . " as Enabled for _Quad = True;
Enabled = !Enabled;
2014-05-16 22:44:22 +02:00
_Quad.StyleSelected = Enabled;
2014-05-14 23:24:00 +02:00
declare " . self::VAR_CHECKBOX_DESIGNS . " as Designs for _Quad = Text[Boolean];
declare Design = Designs[Enabled];
declare DesignParts = TextLib::Split(\"|\", Design);
2017-03-25 18:40:15 +01:00
if (DesignParts.count == 2) {
2014-05-14 23:24:00 +02:00
_Quad.Style = DesignParts[0];
_Quad.Substyle = DesignParts[1];
} else {
_Quad.ImageUrl = Design;
}
2017-03-25 18:40:15 +01:00
declare " . self::VAR_CHECKBOX_ENTRY_ID . " as EntryId for _Quad = \"\";
if (EntryId != \"\") {
2014-05-14 23:24:00 +02:00
declare Entry <=> (Page.GetFirstChild(EntryId) as CMlEntry);
2017-03-25 18:40:15 +01:00
if (Entry != Null) {
if (Enabled) {
Entry.Value = \"1\";
} else {
Entry.Value = \"0\";
}
}
2014-05-14 23:24:00 +02:00
}
}";
2017-03-25 18:40:15 +01:00
}
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/**
* Build the init script text
*
* @return string
*/
protected function buildInitScriptText()
{
$quadId = Builder::getId($this->getQuad());
$entryId = Builder::EMPTY_STRING;
if ($this->entry) {
$entryId = Builder::getId($this->getEntry());
}
$default = Builder::getBoolean($this->default);
$enabledDesignString = $this->enabledDesign->getDesignString();
$disabledDesignString = $this->disabledDesign->getDesignString();
return "
declare Quad_CheckBox <=> (Page.GetFirstChild(\"{$quadId}\") as CMlQuad);
2014-05-14 23:24:00 +02:00
declare Text[Boolean] " . self::VAR_CHECKBOX_DESIGNS . " as Designs for Quad_CheckBox;
2017-03-25 18:40:15 +01:00
Designs[True] = \"{$enabledDesignString}\";
Designs[False] = \"{$disabledDesignString}\";
2014-05-14 23:24:00 +02:00
declare Boolean " . self::VAR_CHECKBOX_ENABLED . " as Enabled for Quad_CheckBox;
Enabled = !{$default};
2014-05-14 23:24:00 +02:00
declare Text " . self::VAR_CHECKBOX_ENTRY_ID . " as EntryId for Quad_CheckBox;
2017-03-25 18:40:15 +01:00
EntryId = \"{$entryId}\";
2014-05-14 23:24:00 +02:00
" . self::FUNCTION_UPDATE_QUAD_DESIGN . "(Quad_CheckBox);
";
2017-03-25 18:40:15 +01:00
}
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
/**
* Build the script text for Quad clicks
*
* @return string
*/
protected function buildClickScriptText()
{
$quadId = Builder::getId($this->getQuad());
return "
if (Event.ControlId == \"{$quadId}\") {
2014-05-14 23:24:00 +02:00
declare Quad_CheckBox <=> (Event.Control as CMlQuad);
" . self::FUNCTION_UPDATE_QUAD_DESIGN . "(Quad_CheckBox);
2023-05-25 13:03:31 +02:00
" . $this->customScript . "
2014-05-14 23:24:00 +02:00
}";
2017-03-25 18:40:15 +01:00
}
2014-05-14 23:24:00 +02:00
}