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

224 lines
5.3 KiB
PHP
Raw Normal View History

2017-07-02 17:45:32 +02:00
<?php
namespace FML\Script\Features;
use FML\Script\Builder;
use FML\Script\Script;
use FML\Script\ScriptLabel;
/**
* Script Feature for toggling the complete ManiaLink via Key Press
*
* @author steeffeen <mail@steeffeen.com>
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ToggleInterface extends ScriptFeature
{
/*
* Constants
*/
2022-03-03 18:00:30 +01:00
const VAR_ISVISIBLE = "FML_ToggleInterface_IsVisible";
const VAR_WASVISIBLE = "FML_ToggleInterface_WasVisible";
2017-07-02 17:45:32 +02:00
/**
* @var string $keyName Key name
*/
protected $keyName = null;
/**
* @var int $keyCode Key code
*/
protected $keyCode = null;
2022-03-12 11:26:56 +01:00
/**
* @var int $defaultVisible if is visible by default
*/
protected $defaultVisible = true;
2017-07-02 17:45:32 +02:00
/**
* Construct a new ToggleInterface
*
* @api
* @param string|int $keyNameOrCode (optional) Key name or code
*/
2022-03-12 11:26:56 +01:00
public function __construct($keyNameOrCode = null, $defaultVisible = true)
2017-07-02 17:45:32 +02:00
{
if (is_string($keyNameOrCode)) {
$this->setKeyName($keyNameOrCode);
} else if (is_int($keyNameOrCode)) {
$this->setKeyCode($keyNameOrCode);
}
2022-03-12 11:26:56 +01:00
$this->setDefaultVisible($defaultVisible);
2017-07-02 17:45:32 +02:00
}
/**
* Get the key name
*
* @api
* @return string
*/
public function getKeyName()
{
return $this->keyName;
}
/**
* Set the key name
*
* @api
* @param string $keyName Key name
* @return static
*/
public function setKeyName($keyName)
{
$this->keyName = (string)$keyName;
$this->keyCode = null;
return $this;
}
/**
* Get the key code
*
* @api
* @return int
*/
public function getKeyCode()
{
return $this->keyCode;
}
/**
* Set the key code
*
* @api
* @param int $keyCode Key code
* @return static
*/
public function setKeyCode($keyCode)
{
$this->keyCode = (int)$keyCode;
$this->keyName = null;
return $this;
}
2022-03-12 11:26:56 +01:00
/**
* Get the Default visibility property
*
* @api
* @return boolean
*/
public function getDefaultVisible()
{
return $this->defaultVisible;
}
/**
* Set the Default visibility property
*
* @api
* @param boolean $defaultVisible if is visible by default
* @return static
*/
public function setDefaultVisible($defaultVisible)
{
$this->defaultVisible = $defaultVisible;
return $this;
}
2017-07-02 17:45:32 +02:00
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
2022-03-03 18:00:30 +01:00
if ($this->keyCode != null || $this->keyName != null) {
2022-03-12 11:26:56 +01:00
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText(true));
2022-03-03 18:00:30 +01:00
$script->appendGenericScriptLabel(ScriptLabel::KEYPRESS, $this->getKeyPressScriptText());
} else {
2022-03-12 11:26:56 +01:00
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText());
$script->appendGenericScriptLabel(ScriptLabel::LOOP, $this->getLoopScriptText());
2017-07-02 17:45:32 +02:00
}
return $this;
}
/**
* Get the on init script text
*
* @return string
*/
2022-03-12 11:26:56 +01:00
protected function getOnInitScriptText($isToggleScript = false)
2017-07-02 17:45:32 +02:00
{
2022-03-03 18:00:30 +01:00
$VarIsVisible = $this::VAR_ISVISIBLE;
2022-03-12 11:26:56 +01:00
$maniascript = "
2022-03-03 18:00:30 +01:00
declare Boolean {$VarIsVisible} for UI = True;
declare Boolean Last_IsVisible = True;
2022-03-12 11:26:56 +01:00
";
if ($isToggleScript) {
if ($this->getDefaultVisible()) {
$defaultVisible = "True";
} else {
$defaultVisible = "False";
}
$maniascript .= "
{$VarIsVisible} = {$defaultVisible};
";
}
// Hide as start of the script
$maniascript .= $this->getLoopScriptText();
2022-03-12 11:26:56 +01:00
return $maniascript;
2017-07-02 17:45:32 +02:00
}
/**
* Get the key press script text
*
* @return string
*/
protected function getKeyPressScriptText()
{
$keyProperty = null;
$keyValue = null;
if ($this->keyName) {
$keyProperty = "KeyName";
$keyValue = Builder::getText($this->keyName);
} else if ($this->keyCode) {
$keyProperty = "KeyCode";
$keyValue = Builder::getInteger($this->keyCode);
}
2022-03-03 18:00:30 +01:00
$VarIsVisible = $this::VAR_ISVISIBLE;
2017-07-02 17:45:32 +02:00
$scriptText = "
if (Event.{$keyProperty} == {$keyValue}) {
2022-03-03 18:00:30 +01:00
{$VarIsVisible} = !{$VarIsVisible};
2017-07-02 17:45:32 +02:00
}";
2022-03-03 18:00:30 +01:00
return $scriptText ;
2017-07-02 17:45:32 +02:00
}
2022-03-03 18:00:30 +01:00
/**
* Get the key press script text
*
* @return string
*/
protected function getLoopScriptText()
2022-03-03 18:00:30 +01:00
{
$VarIsVisible = $this::VAR_ISVISIBLE;
$VarWasVisible = $this::VAR_WASVISIBLE;
return "
if (Last_IsVisible != {$VarIsVisible}) {
Last_IsVisible = {$VarIsVisible};
foreach (Control in Page.MainFrame.Controls) {
declare Boolean {$VarWasVisible} for Control = False;
if (Last_IsVisible && {$VarWasVisible}) {
Control.Visible = True;
{$VarWasVisible} = False;
} else if (!Last_IsVisible){
{$VarWasVisible} = Control.Visible;
Control.Visible = False;
}
}
2017-07-02 17:45:32 +02:00
}
2022-03-03 18:00:30 +01:00
";
}
}