Skip to content

Commit

Permalink
Add Script Feature to Toggle the complete ManiaLink
Browse files Browse the repository at this point in the history
Resolve #45
  • Loading branch information
steeffeen-zz committed Jun 4, 2017
1 parent 90d1edc commit 8f30646
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 0 deletions.
187 changes: 187 additions & 0 deletions FML/Script/Features/ToggleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?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 <[email protected]>
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ToggleInterface extends ScriptFeature
{

/*
* Constants
*/
const VAR_STATE = "FML_ToggleInterface_State";

/**
* @var string $keyName Key name
*/
protected $keyName = null;

/**
* @var int $keyCode Key code
*/
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)
{
if (is_string($keyNameOrCode)) {
$this->setKeyName($keyNameOrCode);
} else if (is_int($keyNameOrCode)) {
$this->setKeyCode($keyNameOrCode);
}
$this->setRememberState($rememberState);
}

/**
* 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;
}

/**
* 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());
}
return $this;
}

/**
* Get the on init script text
*
* @return string
*/
protected function getOnInitScriptText()
{
$stateVariableName = $this::VAR_STATE;
return "
declare persistent {$stateVariableName} as CurrentState for Page = True;
Page.MainFrame.Visible = CurrentState;
";
}

/**
* 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);
}
$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 Page = True;
CurrentState = Page.MainFrame.Visible;
";
}
return $scriptText . "
}";
}

}
20 changes: 20 additions & 0 deletions examples/toggleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

// Include FML
require_once __DIR__ . '/../autoload.php';

// Create ManiaLink
$maniaLink = new \FML\ManiaLink();

// Add some content
$quad = new \FML\Controls\Quads\Quad_ManiaPlanetLogos();
$quad->setSubStyle($quad::SUBSTYLE_ManiaPlanetLogoWhite);
$maniaLink->addChild($quad);

// Add the toggle interface feature using key name F9
$toggleInterfaceF9 = new \FML\Script\Features\ToggleInterface("F9");
$maniaLink->getScript()
->addFeature($toggleInterfaceF9);

// Print xml
echo $maniaLink;
60 changes: 60 additions & 0 deletions tests/Script/Features/ToggleInterfaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use FML\Script\Features\ToggleInterface;

class ToggleInterfaceTest extends \PHPUnit_Framework_TestCase
{

public function testConstructWithKeyName()
{
$toggleInterface = new ToggleInterface("TestKey", false);

$this->assertEquals("TestKey", $toggleInterface->getKeyName());
$this->assertNull($toggleInterface->getKeyCode());
$this->assertFalse($toggleInterface->getRememberState());
}

public function testConstructWithKeyCode()
{
$toggleInterface = new ToggleInterface(1337, true);

$this->assertNull($toggleInterface->getKeyName());
$this->assertEquals(1337, $toggleInterface->getKeyCode());
$this->assertTrue($toggleInterface->getRememberState());
}

public function testKeyNameAndCode()
{
$toggleInterface = new ToggleInterface();

$this->assertNull($toggleInterface->getKeyName());
$this->assertNull($toggleInterface->getKeyCode());

$this->assertSame($toggleInterface, $toggleInterface->setKeyName("SomeKey"));

$this->assertEquals("SomeKey", $toggleInterface->getKeyName());
$this->assertNull($toggleInterface->getKeyCode());

$this->assertSame($toggleInterface, $toggleInterface->setKeyCode(42));

$this->assertNull($toggleInterface->getKeyName());
$this->assertEquals(42, $toggleInterface->getKeyCode());

$this->assertSame($toggleInterface, $toggleInterface->setKeyName("OtherKey"));

$this->assertEquals("OtherKey", $toggleInterface->getKeyName());
$this->assertNull($toggleInterface->getKeyCode());
}

public function testRememberState()
{
$toggleInterface = new ToggleInterface();

$this->assertTrue($toggleInterface->getRememberState());

$this->assertSame($toggleInterface, $toggleInterface->setRememberState(false));

$this->assertFalse($toggleInterface->getRememberState());
}

}

0 comments on commit 8f30646

Please sign in to comment.