Add Toggle Interface feature

This commit is contained in:
Beu 2022-03-03 18:00:30 +01:00
parent 42d9996f8f
commit e7903af87a
4 changed files with 142 additions and 55 deletions

View File

@ -26,6 +26,7 @@ use ManiaControl\Files\FileUtil;
use ManiaControl\General\UsageInformationAble; use ManiaControl\General\UsageInformationAble;
use ManiaControl\General\UsageInformationTrait; use ManiaControl\General\UsageInformationTrait;
use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Manialinks\ToggleInterfaceManager;
use ManiaControl\Maps\MapManager; use ManiaControl\Maps\MapManager;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
@ -127,6 +128,10 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener,
* @see getManialinkManager * @see getManialinkManager
*/ */
private $manialinkManager = null; private $manialinkManager = null;
/** @var ToggleInterfaceManager $toggleInterfaceManager
* @see getToggleInterfaceManager
*/
private $toggleInterfaceManager = null;
/** @var MapManager $mapManager /** @var MapManager $mapManager
* @see getMapManager() * @see getMapManager()
*/ */
@ -206,6 +211,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener,
$this->settingManager = new SettingManager($this); $this->settingManager = new SettingManager($this);
$this->statisticManager = new StatisticManager($this); $this->statisticManager = new StatisticManager($this);
$this->manialinkManager = new ManialinkManager($this); $this->manialinkManager = new ManialinkManager($this);
$this->toggleInterfaceManager = new ToggleInterfaceManager($this);
$this->actionsMenu = new ActionsMenu($this); $this->actionsMenu = new ActionsMenu($this);
$this->chat = new Chat($this); $this->chat = new Chat($this);
$this->commandManager = new CommandManager($this); $this->commandManager = new CommandManager($this);
@ -455,6 +461,15 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener,
return $this->manialinkManager; return $this->manialinkManager;
} }
/**
* Return the ToggleInterface manager
*
* @return ToggleInterfaceManager
*/
public function getToggleInterfaceManager() {
return $this->toggleInterfaceManager;
}
/** /**
* Return the map manager * Return the map manager
* *

View File

@ -248,12 +248,11 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener,
* @param bool $hideOnClick * @param bool $hideOnClick
* @return bool * @return bool
*/ */
public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) { public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false, $ToggleUIFeature = true) {
//Add Toggle Feature //Add Toggle Feature
if($manialinkText instanceof ManiaLink){ if($ToggleUIFeature && $manialinkText instanceof ManiaLink){
/*$toggleInterfaceF9 = new \FML\Script\Features\ToggleInterface("F9"); $toggleInterface = new \FML\Script\Features\ToggleInterface;
$manialinkText->getScript() $script = $manialinkText->getScript()->addFeature($toggleInterface);
->addFeature($toggleInterfaceF9); (not working yet) */
} }
$manialinkText = (string) $manialinkText; $manialinkText = (string) $manialinkText;

View File

@ -0,0 +1,81 @@
<?php
namespace ManiaControl\Manialinks;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager;
use FML\ManiaLink;
/**
* Class managing the Custom UI in ManiaPlanet
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ToggleInterfaceManager implements CallbackListener {
/*
* Constants
*/
const MLID = 'ToggleInterface.KeyListener';
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $manialink = "";
/**
* Create a custom UI manager instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->buildManiaLink();
// Callbacks
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'handleAfterInit');
$this->maniaControl->getCallbackManager()->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerJoined');
//$this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'handleSettingChanged'); // TODO Setting Change Key
}
/**
* Handle ManiaControl AfterInit callback
*
* @internal
*/
public function handleAfterInit() {
$players = $this->maniaControl->getPlayerManager()->getPlayers();
if (!empty($players)) {
$this->maniaControl->getManialinkManager()->sendManialink($this->manialink , $players, 0, false, false);
}
}
/**
* Handle PlayerJoined Callback
*
* @param Player $player
*/
public function handlePlayerJoined(Player $player) {
$this->maniaControl->getManialinkManager()->sendManialink($this->manialink , $player, 0, false, false);
}
/**
* Build the Manialink with only the Toggle Interface script feature
*/
private function buildManiaLink() {
$manialink = new ManiaLink(self::MLID);
$manialink->getScript()->addFeature(new \FML\Script\Features\ToggleInterface("F9"));
$this->manialink = (string) $manialink;
}
}

View File

@ -19,7 +19,8 @@ class ToggleInterface extends ScriptFeature
/* /*
* Constants * Constants
*/ */
const VAR_STATE = "FML_ToggleInterface_State"; const VAR_ISVISIBLE = "FML_ToggleInterface_IsVisible";
const VAR_WASVISIBLE = "FML_ToggleInterface_WasVisible";
/** /**
* @var string $keyName Key name * @var string $keyName Key name
@ -31,26 +32,19 @@ class ToggleInterface extends ScriptFeature
*/ */
protected $keyCode = null; protected $keyCode = null;
/**
* @var bool $rememberState Remember the current state
*/
protected $rememberState = true;
/** /**
* Construct a new ToggleInterface * Construct a new ToggleInterface
* *
* @api * @api
* @param string|int $keyNameOrCode (optional) Key name or code * @param string|int $keyNameOrCode (optional) Key name or code
* @param bool $rememberState (optional) Remember the current state
*/ */
public function __construct($keyNameOrCode = null, $rememberState = true) public function __construct($keyNameOrCode = null)
{ {
if (is_string($keyNameOrCode)) { if (is_string($keyNameOrCode)) {
$this->setKeyName($keyNameOrCode); $this->setKeyName($keyNameOrCode);
} else if (is_int($keyNameOrCode)) { } else if (is_int($keyNameOrCode)) {
$this->setKeyCode($keyNameOrCode); $this->setKeyCode($keyNameOrCode);
} }
$this->setRememberState($rememberState);
} }
/** /**
@ -103,38 +97,16 @@ class ToggleInterface extends ScriptFeature
return $this; return $this;
} }
/**
* Get if the state should get remembered
*
* @api
* @return bool
*/
public function getRememberState()
{
return $this->rememberState;
}
/**
* Set if the state should get remembered
*
* @api
* @param bool $rememberState Remember the current state
* @return static
*/
public function setRememberState($rememberState)
{
$this->rememberState = (bool)$rememberState;
return $this;
}
/** /**
* @see ScriptFeature::prepare() * @see ScriptFeature::prepare()
*/ */
public function prepare(Script $script) public function prepare(Script $script)
{ {
$script->appendGenericScriptLabel(ScriptLabel::KEYPRESS, $this->getKeyPressScriptText()); $script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText());
if ($this->rememberState) { if ($this->keyCode != null || $this->keyName != null) {
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText()); $script->appendGenericScriptLabel(ScriptLabel::KEYPRESS, $this->getKeyPressScriptText());
} else {
$script->appendGenericScriptLabel(ScriptLabel::TICK, $this->getTickScriptText());
} }
return $this; return $this;
} }
@ -146,10 +118,11 @@ class ToggleInterface extends ScriptFeature
*/ */
protected function getOnInitScriptText() protected function getOnInitScriptText()
{ {
$stateVariableName = $this::VAR_STATE; $VarIsVisible = $this::VAR_ISVISIBLE;
$VarWasVisible = $this::VAR_WASVISIBLE;
return " return "
declare persistent {$stateVariableName} as CurrentState for LocalUser = True; declare Boolean {$VarIsVisible} for UI = True;
Page.MainFrame.Visible = CurrentState; declare Boolean Last_IsVisible = True;
"; ";
} }
@ -169,19 +142,38 @@ Page.MainFrame.Visible = CurrentState;
$keyProperty = "KeyCode"; $keyProperty = "KeyCode";
$keyValue = Builder::getInteger($this->keyCode); $keyValue = Builder::getInteger($this->keyCode);
} }
$VarIsVisible = $this::VAR_ISVISIBLE;
$VarWasVisible = $this::VAR_WASVISIBLE;
$scriptText = " $scriptText = "
if (Event.{$keyProperty} == {$keyValue}) { if (Event.{$keyProperty} == {$keyValue}) {
Page.MainFrame.Visible = !Page.MainFrame.Visible; {$VarIsVisible} = !{$VarIsVisible};
";
if ($this->rememberState) {
$stateVariableName = $this::VAR_STATE;
$scriptText .= "
declare persistent {$stateVariableName} as CurrentState for LocalUser = True;
CurrentState = Page.MainFrame.Visible;
";
}
return $scriptText . "
}"; }";
return $scriptText ;
} }
/**
* Get the key press script text
*
* @return string
*/
protected function getTickScriptText()
{
$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;
}
}
}
";
}
} }