Add an option to hide UI by default

This commit is contained in:
Beu 2022-03-12 11:26:56 +01:00
parent 8197e3e307
commit b4228a19c8
2 changed files with 53 additions and 8 deletions

View File

@ -27,6 +27,7 @@ class ToggleInterfaceManager implements CallbackListener {
*/
const MLID = 'ToggleInterface.KeyListener';
const SETTING_KEYNAME = 'Key Name (or code) to toggle the ManiaControl UI';
const SETTING_DEFAULT_VISIBLE = 'Display by default the UI';
/*
* Private properties
@ -46,6 +47,7 @@ class ToggleInterfaceManager implements CallbackListener {
// Settings
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_KEYNAME, "F9");
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_DEFAULT_VISIBLE, True);
// Build Manialink
$this->buildManiaLink();
@ -97,7 +99,7 @@ class ToggleInterfaceManager implements CallbackListener {
*/
private function buildManiaLink() {
$manialink = new ManiaLink(self::MLID);
$manialink->getScript()->addFeature(new \FML\Script\Features\ToggleInterface($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_KEYNAME)));
$manialink->getScript()->addFeature(new \FML\Script\Features\ToggleInterface($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_KEYNAME), $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_DEFAULT_VISIBLE)));
$this->manialink = (string) $manialink;
}
}

View File

@ -32,19 +32,25 @@ class ToggleInterface extends ScriptFeature
*/
protected $keyCode = null;
/**
* @var int $defaultVisible if is visible by default
*/
protected $defaultVisible = true;
/**
* Construct a new ToggleInterface
*
* @api
* @param string|int $keyNameOrCode (optional) Key name or code
*/
public function __construct($keyNameOrCode = null)
public function __construct($keyNameOrCode = null, $defaultVisible = true)
{
if (is_string($keyNameOrCode)) {
$this->setKeyName($keyNameOrCode);
} else if (is_int($keyNameOrCode)) {
$this->setKeyCode($keyNameOrCode);
}
$this->setDefaultVisible($defaultVisible);
}
/**
@ -97,15 +103,40 @@ class ToggleInterface extends ScriptFeature
return $this;
}
/**
* 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;
}
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText());
if ($this->keyCode != null || $this->keyName != null) {
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText(true));
$script->appendGenericScriptLabel(ScriptLabel::KEYPRESS, $this->getKeyPressScriptText());
} else {
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $this->getOnInitScriptText());
$script->appendGenericScriptLabel(ScriptLabel::LOOP, $this->getLoopScriptText());
}
return $this;
@ -116,14 +147,27 @@ class ToggleInterface extends ScriptFeature
*
* @return string
*/
protected function getOnInitScriptText()
protected function getOnInitScriptText($isToggleScript = false)
{
$VarIsVisible = $this::VAR_ISVISIBLE;
$VarWasVisible = $this::VAR_WASVISIBLE;
return "
$maniascript = "
declare Boolean {$VarIsVisible} for UI = True;
declare Boolean Last_IsVisible = True;
";
";
if ($isToggleScript) {
if ($this->getDefaultVisible()) {
$defaultVisible = "True";
} else {
$defaultVisible = "False";
}
$maniascript .= "
{$VarIsVisible} = {$defaultVisible};
";
}
return $maniascript;
}
/**
@ -143,7 +187,6 @@ declare Boolean Last_IsVisible = True;
$keyValue = Builder::getInteger($this->keyCode);
}
$VarIsVisible = $this::VAR_ISVISIBLE;
$VarWasVisible = $this::VAR_WASVISIBLE;
$scriptText = "
if (Event.{$keyProperty} == {$keyValue}) {
{$VarIsVisible} = !{$VarIsVisible};