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

125 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace FML\Script\Features;
2014-05-14 23:24:00 +02:00
use FML\Script\Builder;
use FML\Script\Script;
use FML\Script\ScriptLabel;
/**
2014-06-21 03:18:21 +02:00
* Script Feature for triggering a manialink page action on key press
2014-05-14 23:24:00 +02:00
*
* @author steeffeen
* @link http://destroflyer.mania-community.de/maniascript/keycharid_table.php
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
2014-05-14 23:24:00 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class KeyAction extends ScriptFeature {
/*
2014-06-21 03:18:21 +02:00
* Protected properties
*/
protected $actionName = null;
protected $keyName = null;
protected $keyCode = null;
protected $charPressed = null;
/**
* Construct a new Key Action Feature
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $actionName (optional) Triggered action
* @param string $keyName (optional) Key name
*/
2014-06-21 03:18:21 +02:00
public function __construct($actionName = null, $keyName = null) {
2014-08-11 23:15:53 +02:00
if ($actionName !== null) {
2014-06-21 03:18:21 +02:00
$this->setActionName($actionName);
}
2014-08-11 23:15:53 +02:00
if ($keyName !== null) {
2014-06-21 03:18:21 +02:00
$this->setKeyName($keyName);
}
}
/**
2014-06-21 03:18:21 +02:00
* Set the action to trigger
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $actionName Triggered action
2014-07-03 22:34:47 +02:00
* @return static
*/
public function setActionName($actionName) {
2014-05-14 23:24:00 +02:00
$this->actionName = (string)$actionName;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the key name for triggering the action
2014-05-14 23:24:00 +02:00
*
* @param string $keyName Key Name
2014-07-03 22:34:47 +02:00
* @return static
*/
public function setKeyName($keyName) {
2014-06-21 03:18:21 +02:00
$this->keyName = (string)$keyName;
$this->keyCode = null;
$this->charPressed = null;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the key code for triggering the action
2014-05-14 23:24:00 +02:00
*
* @param int $keyCode Key Code
2014-07-03 22:34:47 +02:00
* @return static
*/
public function setKeyCode($keyCode) {
2014-06-21 03:18:21 +02:00
$this->keyCode = (int)$keyCode;
$this->keyName = null;
$this->charPressed = null;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the char to press for triggering the action
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $charPressed Pressed char
2014-07-03 22:34:47 +02:00
* @return static
*/
public function setCharPressed($charPressed) {
2014-06-21 03:18:21 +02:00
$this->charPressed = (string)$charPressed;
$this->keyName = null;
$this->keyCode = null;
return $this;
}
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
$script->appendGenericScriptLabel(ScriptLabel::KEYPRESS, $this->getScriptText());
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Get the script text
2014-05-14 23:24:00 +02:00
*
* @return string
*/
protected function getScriptText() {
2014-06-21 03:18:21 +02:00
$actionName = Builder::escapeText($this->actionName, true);
$key = null;
$value = null;
2014-08-11 23:15:53 +02:00
if ($this->keyName !== null) {
2014-06-21 03:18:21 +02:00
$key = 'KeyName';
$value = $this->keyName;
2014-08-11 23:15:53 +02:00
} else if ($this->keyCode !== null) {
2014-05-14 23:24:00 +02:00
$key = 'KeyCode';
2014-06-21 03:18:21 +02:00
$value = $this->keyCode;
2014-08-11 23:15:53 +02:00
} else if ($this->charPressed !== null) {
2014-05-14 23:24:00 +02:00
$key = 'CharPressed';
2014-06-21 03:18:21 +02:00
$value = $this->charPressed;
}
2014-06-21 03:18:21 +02:00
$value = Builder::escapeText($value, true);
return "
if (Event.{$key} == {$value}) {
TriggerPageAction({$actionName});
}";
}
}