diff --git a/core/ManiaControl.php b/core/ManiaControl.php index e371f33b..d3f1cd84 100644 --- a/core/ManiaControl.php +++ b/core/ManiaControl.php @@ -26,6 +26,7 @@ use ManiaControl\Files\FileUtil; use ManiaControl\General\UsageInformationAble; use ManiaControl\General\UsageInformationTrait; use ManiaControl\Manialinks\ManialinkManager; +use ManiaControl\Manialinks\ToggleInterfaceManager; use ManiaControl\Maps\MapManager; use ManiaControl\Players\Player; use ManiaControl\Players\PlayerManager; @@ -127,6 +128,10 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, * @see getManialinkManager */ private $manialinkManager = null; + /** @var ToggleInterfaceManager $toggleInterfaceManager + * @see getToggleInterfaceManager + */ + private $toggleInterfaceManager = null; /** @var MapManager $mapManager * @see getMapManager() */ @@ -206,6 +211,7 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, $this->settingManager = new SettingManager($this); $this->statisticManager = new StatisticManager($this); $this->manialinkManager = new ManialinkManager($this); + $this->toggleInterfaceManager = new ToggleInterfaceManager($this); $this->actionsMenu = new ActionsMenu($this); $this->chat = new Chat($this); $this->commandManager = new CommandManager($this); @@ -455,6 +461,15 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener, return $this->manialinkManager; } + /** + * Return the ToggleInterface manager + * + * @return ToggleInterfaceManager + */ + public function getToggleInterfaceManager() { + return $this->toggleInterfaceManager; + } + /** * Return the map manager * diff --git a/core/Manialinks/ManialinkManager.php b/core/Manialinks/ManialinkManager.php index 85cafef0..a8d61d49 100644 --- a/core/Manialinks/ManialinkManager.php +++ b/core/Manialinks/ManialinkManager.php @@ -248,13 +248,12 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener, * @param bool $hideOnClick * @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 - if($manialinkText instanceof ManiaLink){ - /*$toggleInterfaceF9 = new \FML\Script\Features\ToggleInterface("F9"); - $manialinkText->getScript() - ->addFeature($toggleInterfaceF9); (not working yet) */ - } + if($ToggleUIFeature && $manialinkText instanceof ManiaLink){ + $toggleInterface = new \FML\Script\Features\ToggleInterface; + $script = $manialinkText->getScript()->addFeature($toggleInterface); + } $manialinkText = (string) $manialinkText; diff --git a/core/Manialinks/ToggleInterfaceManager.php b/core/Manialinks/ToggleInterfaceManager.php new file mode 100644 index 00000000..0b4d748f --- /dev/null +++ b/core/Manialinks/ToggleInterfaceManager.php @@ -0,0 +1,81 @@ + + * @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; + } +} diff --git a/libs/FML/Script/Features/ToggleInterface.php b/libs/FML/Script/Features/ToggleInterface.php index 3912806c..5e41523a 100644 --- a/libs/FML/Script/Features/ToggleInterface.php +++ b/libs/FML/Script/Features/ToggleInterface.php @@ -19,7 +19,8 @@ class ToggleInterface extends ScriptFeature /* * Constants */ - const VAR_STATE = "FML_ToggleInterface_State"; + const VAR_ISVISIBLE = "FML_ToggleInterface_IsVisible"; + const VAR_WASVISIBLE = "FML_ToggleInterface_WasVisible"; /** * @var string $keyName Key name @@ -31,26 +32,19 @@ class ToggleInterface extends ScriptFeature */ protected $keyCode = null; - /** - * @var bool $rememberState Remember the current state - */ - protected $rememberState = true; - /** * Construct a new ToggleInterface * * @api * @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)) { $this->setKeyName($keyNameOrCode); } else if (is_int($keyNameOrCode)) { $this->setKeyCode($keyNameOrCode); } - $this->setRememberState($rememberState); } /** @@ -103,38 +97,16 @@ class ToggleInterface extends ScriptFeature 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() */ public function prepare(Script $script) { - $script->appendGenericScriptLabel(ScriptLabel::KEYPRESS, $this->getKeyPressScriptText()); - if ($this->rememberState) { - $script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText()); + $script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText()); + if ($this->keyCode != null || $this->keyName != null) { + $script->appendGenericScriptLabel(ScriptLabel::KEYPRESS, $this->getKeyPressScriptText()); + } else { + $script->appendGenericScriptLabel(ScriptLabel::TICK, $this->getTickScriptText()); } return $this; } @@ -146,10 +118,11 @@ class ToggleInterface extends ScriptFeature */ protected function getOnInitScriptText() { - $stateVariableName = $this::VAR_STATE; + $VarIsVisible = $this::VAR_ISVISIBLE; + $VarWasVisible = $this::VAR_WASVISIBLE; return " -declare persistent {$stateVariableName} as CurrentState for LocalUser = True; -Page.MainFrame.Visible = CurrentState; +declare Boolean {$VarIsVisible} for UI = True; +declare Boolean Last_IsVisible = True; "; } @@ -169,19 +142,38 @@ Page.MainFrame.Visible = CurrentState; $keyProperty = "KeyCode"; $keyValue = Builder::getInteger($this->keyCode); } + $VarIsVisible = $this::VAR_ISVISIBLE; + $VarWasVisible = $this::VAR_WASVISIBLE; $scriptText = " if (Event.{$keyProperty} == {$keyValue}) { - Page.MainFrame.Visible = !Page.MainFrame.Visible; -"; - if ($this->rememberState) { - $stateVariableName = $this::VAR_STATE; - $scriptText .= " - declare persistent {$stateVariableName} as CurrentState for LocalUser = True; - CurrentState = Page.MainFrame.Visible; -"; - } - return $scriptText . " + {$VarIsVisible} = !{$VarIsVisible}; }"; + 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; + } + } } +"; + } +} \ No newline at end of file