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

169 lines
4.1 KiB
PHP
Raw Normal View History

2014-04-27 14:44:40 +02:00
<?php
namespace FML\Script\Features;
use FML\Controls\Control;
2014-05-14 23:24:00 +02:00
use FML\Script\Builder;
2014-04-27 14:44:40 +02:00
use FML\Script\Script;
use FML\Script\ScriptLabel;
2014-05-14 23:24:00 +02:00
use FML\Types\Scriptable;
2014-04-27 14:44:40 +02:00
/**
2014-06-21 03:18:21 +02:00
* Script Feature for playing a UISound
2014-04-27 14:44:40 +02:00
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
2014-04-27 14:44:40 +02:00
* @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
2014-04-27 14:44:40 +02:00
*/
class UISound extends ScriptFeature {
/*
* Constants
*/
2014-05-14 23:24:00 +02:00
const Bonus = 'Bonus';
const Capture = 'Capture';
const Checkpoint = 'Checkpoint';
const Combo = 'Combo';
const Custom1 = 'Custom1';
const Custom2 = 'Custom2';
const Custom3 = 'Custom3';
const Custom4 = 'Custom4';
const Default_ = 'Default';
const EndMatch = 'EndMatch';
const EndRound = 'EndRound';
const Finish = 'Finish';
const FirstHit = 'FirstHit';
const Notice = 'Notice';
const PhaseChange = 'PhaseChange';
2014-04-27 14:44:40 +02:00
const PlayerEliminated = 'PlayerEliminated';
2014-05-14 23:24:00 +02:00
const PlayerHit = 'PlayerHit';
2014-04-27 14:44:40 +02:00
const PlayersRemaining = 'PlayersRemaining';
2014-05-14 23:24:00 +02:00
const RankChange = 'RankChange';
const Record = 'Record';
const ScoreProgress = 'ScoreProgress';
const Silence = 'Silence';
const StartMatch = 'StartMatch';
const StartRound = 'StartRound';
const TieBreakPoint = 'TieBreakPoint';
const TiePoint = 'TiePoint';
const TimeOut = 'TimeOut';
const VictoryPoint = 'VictoryPoint';
const Warning = 'Warning';
2014-04-27 14:44:40 +02:00
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-04-27 14:44:40 +02:00
*/
protected $soundName = null;
2014-05-14 23:24:00 +02:00
/** @var Control $control */
2014-04-27 14:44:40 +02:00
protected $control = null;
protected $variant = 0;
protected $volume = 1.;
protected $labelName = null;
/**
* Construct a new UISound Feature
*
2014-06-21 03:18:21 +02:00
* @param string $soundName (optional) Played sound
2014-05-14 23:24:00 +02:00
* @param Control $control (optional) Action Control
2014-06-21 03:18:21 +02:00
* @param int $variant (optional) Sound variant
* @param string $labelName (optional) Script Label name
2014-04-27 14:44:40 +02:00
*/
public function __construct($soundName = null, Control $control = null, $variant = 0, $labelName = ScriptLabel::MOUSECLICK) {
2014-08-11 23:15:53 +02:00
if ($soundName !== null) {
2014-06-21 03:18:21 +02:00
$this->setSoundName($soundName);
}
2014-08-11 23:15:53 +02:00
if ($control !== null) {
2014-06-21 03:18:21 +02:00
$this->setControl($control);
}
2014-04-27 14:44:40 +02:00
$this->setVariant($variant);
$this->setLabelName($labelName);
}
/**
2014-06-21 03:18:21 +02:00
* Set the sound to play
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $soundName Sound name
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setSoundName($soundName) {
2014-05-14 23:24:00 +02:00
$this->soundName = (string)$soundName;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* Set the Control
*
* @param Control $control Action Control
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setControl(Control $control) {
$control->checkId();
2014-05-14 23:24:00 +02:00
if ($control instanceof Scriptable) {
$control->setScriptEvents(true);
}
2014-04-27 14:44:40 +02:00
$this->control = $control;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the sound variant
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param int $variant Sound variant
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setVariant($variant) {
2014-05-14 23:24:00 +02:00
$this->variant = (int)$variant;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the volume
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param float $volume Sound volume
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setVolume($volume) {
2014-05-14 23:24:00 +02:00
$this->volume = (float)$volume;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the label name
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $labelName Script Label name
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setLabelName($labelName) {
2014-05-14 23:24:00 +02:00
$this->labelName = (string)$labelName;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
$script->appendGenericScriptLabel($this->labelName, $this->getScriptText());
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Get the script text
2014-04-27 14:44:40 +02:00
*
* @return string
*/
protected function getScriptText() {
if ($this->control) {
// Control event
2014-06-21 03:18:21 +02:00
$controlId = Builder::escapeText($this->control->getId(), true);
2014-04-27 14:44:40 +02:00
$scriptText = "
2014-06-21 03:18:21 +02:00
if (Event.Control.ControlId == {$controlId}) {
2014-04-27 14:44:40 +02:00
PlayUiSound(CMlScriptIngame::EUISound::{$this->soundName}, {$this->variant}, {$this->volume});
}";
2014-05-14 23:24:00 +02:00
} else {
2014-04-27 14:44:40 +02:00
// Other
$scriptText = "
PlayUiSound(CMlScriptIngame::EUISound::{$this->soundName}, {$this->variant}, {$this->volume});";
}
return $scriptText;
}
}