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

163 lines
4.4 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\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>
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 Tooltip extends ScriptFeature {
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-04-27 14:44:40 +02:00
*/
2014-05-14 23:24:00 +02:00
/** @var Control $hoverControl */
2014-04-27 14:44:40 +02:00
protected $hoverControl = null;
2014-05-14 23:24:00 +02:00
/** @var Control $tooltipControl */
2014-04-27 14:44:40 +02:00
protected $tooltipControl = null;
protected $stayOnClick = null;
protected $invert = null;
protected $text = null;
/**
* Construct a new Tooltip Feature
*
2014-05-14 23:24:00 +02:00
* @param Control $hoverControl (optional) Hover Control
2014-04-27 14:44:40 +02:00
* @param Control $tooltipControl (optional) Tooltip Control
2014-06-21 03:18:21 +02:00
* @param bool $stayOnClick (optional) Whether the Tooltip should stay on click
* @param bool $invert (optional) Whether the visibility toggling should be inverted
* @param string $text (optional) Text to display if the TooltipControl is a Label
2014-04-27 14:44:40 +02:00
*/
public function __construct(Control $hoverControl = null, Control $tooltipControl = null, $stayOnClick = false, $invert = false, $text = null) {
2014-08-11 23:15:53 +02:00
if ($hoverControl !== null) {
2014-06-21 03:18:21 +02:00
$this->setHoverControl($hoverControl);
}
2014-08-11 23:15:53 +02:00
if ($tooltipControl !== null) {
2014-06-21 03:18:21 +02:00
$this->setTooltipControl($tooltipControl);
}
2014-04-27 14:44:40 +02:00
$this->setStayOnClick($stayOnClick);
$this->setInvert($invert);
2014-08-11 23:15:53 +02:00
if ($text !== null) {
2014-06-21 03:18:21 +02:00
$this->setText($text);
}
2014-04-27 14:44:40 +02:00
}
/**
* Set the Hover Control
*
* @param Control $hoverControl Hover Control
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setHoverControl(Control $hoverControl) {
$hoverControl->checkId();
2014-05-14 23:24:00 +02:00
if ($hoverControl instanceof Scriptable) {
$hoverControl->setScriptEvents(true);
}
2014-04-27 14:44:40 +02:00
$this->hoverControl = $hoverControl;
return $this;
}
/**
* Set the Tooltip Control
*
* @param Control $tooltipControl Tooltip Control
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setTooltipControl(Control $tooltipControl) {
2014-06-21 03:18:21 +02:00
$this->tooltipControl = $tooltipControl->checkId()->setVisible(false);
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set to only show
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param bool $stayOnClick (optional) Whether the Tooltip should stay on click
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setStayOnClick($stayOnClick) {
2014-05-14 23:24:00 +02:00
$this->stayOnClick = (bool)$stayOnClick;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set to only hide
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param bool $invert (optional) Whether the visibility toggling should be inverted
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setInvert($invert) {
2014-05-14 23:24:00 +02:00
$this->invert = (bool)$invert;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set text
2014-04-27 14:44:40 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $text (optional) Text to display if the TooltipControl is a Label
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setText($text) {
2014-06-21 03:18:21 +02:00
$this->text = (string)$text;
2014-04-27 14:44:40 +02:00
return $this;
}
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
2014-06-21 03:18:21 +02:00
$hoverControlId = $this->hoverControl->getId(true, true);
$tooltipControlId = $this->tooltipControl->getId(true, true);
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
// MouseOver
$visibility = ($this->invert ? 'False' : 'True');
$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};";
if (is_string($this->text) && ($this->tooltipControl instanceof Label)) {
2014-06-21 03:18:21 +02:00
$tooltipText = Builder::escapeText($this->text, true);
2014-04-27 14:44:40 +02:00
$scriptText .= "
declare TooltipLabel = (TooltipControl as CMlLabel);
2014-06-21 03:18:21 +02:00
TooltipLabel.Value = {$tooltipText};";
2014-04-27 14:44:40 +02:00
}
$scriptText .= "
}";
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOVER, $scriptText);
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
// MouseOut
$visibility = ($this->invert ? 'True' : 'False');
$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
if ($this->stayOnClick) {
$scriptText .= "
declare FML_Clicked for Event.Control = False;
if (!FML_Clicked) ";
}
$scriptText .= "
TooltipControl.Visible = {$visibility};
}";
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOUT, $scriptText);
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02: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;
}";
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $scriptText);
}
return $this;
}
}