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

254 lines
6.1 KiB
PHP
Raw Permalink 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\Controls\Label;
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 showing Tooltips
2014-04-27 14:44:40 +02:00
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
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 Tooltip extends ScriptFeature
{
/**
* @var Control $hoverControl Hover Control
*/
protected $hoverControl = null;
/**
* @var Control $tooltipControl Tooltip Control
*/
protected $tooltipControl = null;
/**
* @var bool $stayOnClick Stay on click
*/
protected $stayOnClick = null;
/**
* @var bool $invert Inverted visibility toggling
*/
protected $invert = null;
/**
* @var string $text Tooltip Text
*/
protected $text = null;
/**
* Construct a new Tooltip
*
* @api
* @param Control $hoverControl (optional) Hover Control
* @param Control $tooltipControl (optional) Tooltip Control
* @param bool $stayOnClick (optional) If the Tooltip should stay on click
* @param bool $invert (optional) If the visibility toggling should be inverted
* @param string $text (optional) Text to display if the TooltipControl is a Label
*/
public function __construct(Control $hoverControl = null, Control $tooltipControl = null, $stayOnClick = null, $invert = null, $text = null)
{
if ($hoverControl) {
$this->setHoverControl($hoverControl);
}
if ($tooltipControl) {
$this->setTooltipControl($tooltipControl);
}
if ($stayOnClick) {
$this->setStayOnClick($stayOnClick);
}
if ($invert) {
$this->setInvert($invert);
}
if ($text) {
$this->setText($text);
}
}
/**
* Get the Hover Control
*
* @api
* @return Control
*/
public function getHoverControl()
{
return $this->hoverControl;
}
/**
* Set the Hover Control
*
* @api
* @param Control $hoverControl Hover Control
* @return static
*/
public function setHoverControl(Control $hoverControl)
{
$hoverControl->checkId();
if ($hoverControl instanceof Scriptable) {
$hoverControl->setScriptEvents(true);
}
$this->hoverControl = $hoverControl;
return $this;
}
/**
* Get the Tooltip Control
*
* @api
* @return Control
*/
public function getTooltipControl()
{
return $this->tooltipControl;
}
/**
* Set the Tooltip Control
*
* @api
* @param Control $tooltipControl Tooltip Control
* @return static
*/
public function setTooltipControl(Control $tooltipControl)
{
$tooltipControl->checkId();
$tooltipControl->setVisible(false);
$this->tooltipControl = $tooltipControl;
return $this;
}
/**
* Get the staying on click
*
* @api
* @return bool
*/
public function getStayOnClick()
{
return $this->stayOnClick;
}
/**
* Set the staying on click
*
* @api
* @param bool $stayOnClick If the Tooltip should stay on click
* @return static
*/
public function setStayOnClick($stayOnClick)
{
$this->stayOnClick = (bool)$stayOnClick;
return $this;
}
/**
* Get inverting of the visibility
*
* @api
* @return bool
*/
public function getInvert()
{
return $this->invert;
}
/**
* Set inverting of the visibility
*
* @api
* @param bool $invert If the visibility toggling should be inverted
* @return static
*/
public function setInvert($invert)
{
$this->invert = (bool)$invert;
return $this;
}
/**
* Get the text
*
* @api
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* Set the text
*
* @api
* @param string $text Text to display if the TooltipControl is a Label
* @return static
*/
public function setText($text)
{
$this->text = (string)$text;
return $this;
}
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
$hoverControlId = Builder::escapeText($this->hoverControl->getId());
$tooltipControlId = Builder::escapeText($this->tooltipControl->getId());
// MouseOver
$visibility = Builder::getBoolean(!$this->invert);
$scriptText = "
2014-06-21 03:18:21 +02:00
if (Event.Control.ControlId == {$hoverControlId}) {
declare TooltipControl = Page.GetFirstChild({$tooltipControlId});
2014-04-27 14:44:40 +02:00
TooltipControl.Visible = {$visibility};";
2017-03-25 18:40:15 +01:00
if (is_string($this->text) && ($this->tooltipControl instanceof Label)) {
$tooltipText = Builder::escapeText($this->text);
$scriptText .= "
2014-04-27 14:44:40 +02:00
declare TooltipLabel = (TooltipControl as CMlLabel);
2014-06-21 03:18:21 +02:00
TooltipLabel.Value = {$tooltipText};";
2017-03-25 18:40:15 +01:00
}
$scriptText .= "
2014-04-27 14:44:40 +02:00
}";
2017-03-25 18:40:15 +01:00
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOVER, $scriptText);
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
// MouseOut
$visibility = Builder::getBoolean($this->invert);
$scriptText = "
2014-06-21 03:18:21 +02:00
if (Event.Control.ControlId == {$hoverControlId}) {
declare TooltipControl = Page.GetFirstChild({$tooltipControlId});";
2017-03-25 18:40:15 +01:00
if ($this->stayOnClick) {
$scriptText .= "
2014-04-27 14:44:40 +02:00
declare FML_Clicked for Event.Control = False;
if (!FML_Clicked) ";
2017-03-25 18:40:15 +01:00
}
$scriptText .= "
2014-04-27 14:44:40 +02:00
TooltipControl.Visible = {$visibility};
}";
2017-03-25 18:40:15 +01:00
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOUT, $scriptText);
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
// MouseClick
if ($this->stayOnClick) {
$scriptText = "
2014-06-21 03:18:21 +02:00
if (Event.Control.ControlId == {$hoverControlId}) {
2014-04-27 14:44:40 +02:00
declare FML_Clicked for Event.Control = False;
FML_Clicked = !FML_Clicked;
}";
2017-03-25 18:40:15 +01:00
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $scriptText);
}
return $this;
}
2014-04-27 14:44:40 +02:00
}