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

161 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2014-04-27 14:44:40 +02:00
<?php
namespace FML\Script\Features;
use FML\Controls\Label;
2017-03-25 18:40:15 +01:00
use FML\Script\Builder;
2014-05-14 23:24:00 +02:00
use FML\Script\Script;
2014-04-27 14:44:40 +02:00
use FML\Script\ScriptInclude;
2014-05-14 23:24:00 +02:00
use FML\Script\ScriptLabel;
2014-04-27 14:44:40 +02:00
/**
2014-06-21 03:18:21 +02:00
* Script Feature showing the current time on a Label
2014-04-27 14:44:40 +02:00
*
2014-05-14 23:24:00 +02:00
* @author steeffeen
2017-03-25 18:40:15 +01:00
* @copyright FancyManiaLinks Copyright © 2017 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
*/
2017-03-25 18:40:15 +01:00
class Clock extends ScriptFeature
{
/**
* @var Label $label Clock Label
*/
protected $label = null;
/**
* @var bool $showSeconds Show the seconds
*/
protected $showSeconds = null;
/**
* @var bool $showFullDate Show the date
*/
protected $showFullDate = null;
/**
* Construct a new Clock
*
* @api
* @param Label $label (optional) Clock Label
* @param bool $showSeconds (optional) Show the seconds
* @param bool $showFullDate (optional) Show the date
*/
public function __construct(Label $label = null, $showSeconds = true, $showFullDate = false)
{
if ($label) {
$this->setLabel($label);
}
$this->setShowSeconds($showSeconds)
->setShowFullDate($showFullDate);
}
/**
* Get the Label
*
* @api
* @return Label
*/
public function getLabel()
{
return $this->label;
}
/**
* Set the Label
*
* @api
* @param Label $label Clock Label
* @return static
*/
public function setLabel(Label $label)
{
$label->checkId();
$this->label = $label;
return $this;
}
/**
* Get if seconds should be shown
*
* @api
* @return bool
*/
public function getShowSeconds()
{
return $this->showSeconds;
}
/**
* Set if seconds should be shown
*
* @api
* @param bool $showSeconds If seconds should be shown
* @return static
*/
public function setShowSeconds($showSeconds)
{
$this->showSeconds = (bool)$showSeconds;
return $this;
}
/**
* Get if the full date should be shown
*
* @api
* @return bool
*/
public function getShowFullDate()
{
return $this->showFullDate;
}
/**
* Set if the full date should be shown
*
* @api
* @param bool $showFullDate If the full date should be shown
* @return static
*/
public function setShowFullDate($showFullDate)
{
$this->showFullDate = (bool)$showFullDate;
return $this;
}
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
$script->setScriptInclude(ScriptInclude::TEXTLIB)
->appendGenericScriptLabel(ScriptLabel::TICK, $this->getScriptText(), true);
return $this;
}
/**
* Get the script text
*
* @return string
*/
protected function getScriptText()
{
$controlId = Builder::escapeText($this->label->getId());
$scriptText = "
2014-06-21 03:18:21 +02:00
declare ClockLabel <=> (Page.GetFirstChild({$controlId}) as CMlLabel);
2014-04-27 14:44:40 +02:00
declare TimeText = CurrentLocalDateText;";
2017-03-25 18:40:15 +01:00
if (!$this->showSeconds) {
$scriptText .= "
2014-04-27 14:44:40 +02:00
TimeText = TextLib::SubText(TimeText, 0, 16);";
2017-03-25 18:40:15 +01:00
}
if (!$this->showFullDate) {
$scriptText .= "
2014-04-27 14:44:40 +02:00
TimeText = TextLib::SubText(TimeText, 11, 9);";
2017-03-25 18:40:15 +01:00
}
$scriptText .= "
2014-04-27 14:44:40 +02:00
ClockLabel.Value = TimeText;";
2017-03-25 18:40:15 +01:00
return $scriptText;
}
2014-04-27 14:44:40 +02:00
}