Updated to ManiaLink v3
This commit is contained in:
@ -13,150 +13,241 @@ use FML\Types\Scriptable;
|
||||
* Script Feature for showing Tooltips
|
||||
*
|
||||
* @author steeffeen <mail@steeffeen.com>
|
||||
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
|
||||
* @copyright FancyManiaLinks Copyright © 2017 Steffen Schröder
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class Tooltip extends ScriptFeature {
|
||||
/*
|
||||
* Protected properties
|
||||
*/
|
||||
/** @var Control $hoverControl */
|
||||
protected $hoverControl = null;
|
||||
/** @var Control $tooltipControl */
|
||||
protected $tooltipControl = null;
|
||||
protected $stayOnClick = null;
|
||||
protected $invert = null;
|
||||
protected $text = null;
|
||||
class Tooltip extends ScriptFeature
|
||||
{
|
||||
|
||||
/**
|
||||
* Construct a new Tooltip Feature
|
||||
*
|
||||
* @param Control $hoverControl (optional) Hover Control
|
||||
* @param Control $tooltipControl (optional) Tooltip Control
|
||||
* @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
|
||||
*/
|
||||
public function __construct(Control $hoverControl = null, Control $tooltipControl = null, $stayOnClick = false, $invert = false, $text = null) {
|
||||
if ($hoverControl !== null) {
|
||||
$this->setHoverControl($hoverControl);
|
||||
}
|
||||
if ($tooltipControl !== null) {
|
||||
$this->setTooltipControl($tooltipControl);
|
||||
}
|
||||
$this->setStayOnClick($stayOnClick);
|
||||
$this->setInvert($invert);
|
||||
if ($text !== null) {
|
||||
$this->setText($text);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @var Control $hoverControl Hover Control
|
||||
*/
|
||||
protected $hoverControl = null;
|
||||
|
||||
/**
|
||||
* Set the Hover Control
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
/**
|
||||
* @var Control $tooltipControl Tooltip Control
|
||||
*/
|
||||
protected $tooltipControl = null;
|
||||
|
||||
/**
|
||||
* Set the Tooltip Control
|
||||
*
|
||||
* @param Control $tooltipControl Tooltip Control
|
||||
* @return static
|
||||
*/
|
||||
public function setTooltipControl(Control $tooltipControl) {
|
||||
$this->tooltipControl = $tooltipControl->checkId()->setVisible(false);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var bool $stayOnClick Stay on click
|
||||
*/
|
||||
protected $stayOnClick = null;
|
||||
|
||||
/**
|
||||
* Set to only show
|
||||
*
|
||||
* @param bool $stayOnClick (optional) Whether the Tooltip should stay on click
|
||||
* @return static
|
||||
*/
|
||||
public function setStayOnClick($stayOnClick) {
|
||||
$this->stayOnClick = (bool)$stayOnClick;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var bool $invert Inverted visibility toggling
|
||||
*/
|
||||
protected $invert = null;
|
||||
|
||||
/**
|
||||
* Set to only hide
|
||||
*
|
||||
* @param bool $invert (optional) Whether the visibility toggling should be inverted
|
||||
* @return static
|
||||
*/
|
||||
public function setInvert($invert) {
|
||||
$this->invert = (bool)$invert;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @var string $text Tooltip Text
|
||||
*/
|
||||
protected $text = null;
|
||||
|
||||
/**
|
||||
* Set text
|
||||
*
|
||||
* @param string $text (optional) Text to display if the TooltipControl is a Label
|
||||
* @return static
|
||||
*/
|
||||
public function setText($text) {
|
||||
$this->text = (string)$text;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \FML\Script\Features\ScriptFeature::prepare()
|
||||
*/
|
||||
public function prepare(Script $script) {
|
||||
$hoverControlId = $this->hoverControl->getId(true, true);
|
||||
$tooltipControlId = $this->tooltipControl->getId(true, true);
|
||||
/**
|
||||
* Get the Hover Control
|
||||
*
|
||||
* @api
|
||||
* @return Control
|
||||
*/
|
||||
public function getHoverControl()
|
||||
{
|
||||
return $this->hoverControl;
|
||||
}
|
||||
|
||||
// MouseOver
|
||||
$visibility = ($this->invert ? 'False' : 'True');
|
||||
$scriptText = "
|
||||
/**
|
||||
* 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 = "
|
||||
if (Event.Control.ControlId == {$hoverControlId}) {
|
||||
declare TooltipControl = Page.GetFirstChild({$tooltipControlId});
|
||||
TooltipControl.Visible = {$visibility};";
|
||||
if (is_string($this->text) && ($this->tooltipControl instanceof Label)) {
|
||||
$tooltipText = Builder::escapeText($this->text, true);
|
||||
$scriptText .= "
|
||||
if (is_string($this->text) && ($this->tooltipControl instanceof Label)) {
|
||||
$tooltipText = Builder::escapeText($this->text);
|
||||
$scriptText .= "
|
||||
declare TooltipLabel = (TooltipControl as CMlLabel);
|
||||
TooltipLabel.Value = {$tooltipText};";
|
||||
}
|
||||
$scriptText .= "
|
||||
}
|
||||
$scriptText .= "
|
||||
}";
|
||||
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOVER, $scriptText);
|
||||
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOVER, $scriptText);
|
||||
|
||||
// MouseOut
|
||||
$visibility = ($this->invert ? 'True' : 'False');
|
||||
$scriptText = "
|
||||
// MouseOut
|
||||
$visibility = Builder::getBoolean($this->invert);
|
||||
$scriptText = "
|
||||
if (Event.Control.ControlId == {$hoverControlId}) {
|
||||
declare TooltipControl = Page.GetFirstChild({$tooltipControlId});";
|
||||
if ($this->stayOnClick) {
|
||||
$scriptText .= "
|
||||
if ($this->stayOnClick) {
|
||||
$scriptText .= "
|
||||
declare FML_Clicked for Event.Control = False;
|
||||
if (!FML_Clicked) ";
|
||||
}
|
||||
$scriptText .= "
|
||||
}
|
||||
$scriptText .= "
|
||||
TooltipControl.Visible = {$visibility};
|
||||
}";
|
||||
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOUT, $scriptText);
|
||||
$script->appendGenericScriptLabel(ScriptLabel::MOUSEOUT, $scriptText);
|
||||
|
||||
// MouseClick
|
||||
if ($this->stayOnClick) {
|
||||
$scriptText = "
|
||||
// MouseClick
|
||||
if ($this->stayOnClick) {
|
||||
$scriptText = "
|
||||
if (Event.Control.ControlId == {$hoverControlId}) {
|
||||
declare FML_Clicked for Event.Control = False;
|
||||
FML_Clicked = !FML_Clicked;
|
||||
}";
|
||||
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $scriptText);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $scriptText);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user