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

184 lines
5.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\Script\Builder;
2014-04-27 14:44:40 +02:00
use FML\Script\Script;
use FML\Script\ScriptLabel;
/**
2014-06-21 03:18:21 +02:00
* Script Feature realising a Menu showing specific Controls for the different items
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 Menu extends ScriptFeature
{
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/*
* Constants
*/
const FUNCTION_UPDATE_MENU = "FML_UpdateMenu";
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var MenuElement[] $elements Menu Elements
*/
protected $elements = array();
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* @var MenuElement $startElement Start Element
*/
protected $startElement = null;
/**
* Construct a new Menu
*
* @api
* @param Control $item (optional) Item Control in the Menu bar
* @param Control $control (optional) Toggled Menu Control
* @param bool $isStartElement (optional) Whether the Menu should start with the given Element
*/
public function __construct(Control $item = null, Control $control = null, $isStartElement = true)
{
if ($item && $control) {
$this->addItem($item, $control, $isStartElement);
}
}
/**
* Get the Menu Elements
*
* @api
* @return MenuElement[]
*/
public function getElements()
{
return $this->elements;
}
/**
* Add a Menu item
*
* @api
* @param Control $item Item Control in the Menu bar
* @param Control $control Toggled Menu Control
* @param bool $isStartElement (optional) Whether the Menu should start with this Element
* @return static
*/
public function addItem(Control $item, Control $control, $isStartElement = false)
{
$menuElement = new MenuElement($item, $control);
$this->addElement($menuElement, $isStartElement);
return $this;
}
2014-04-27 14:44:40 +02:00
2017-03-25 18:40:15 +01:00
/**
* Add a Menu Element
*
* @api
* @param MenuElement $menuElement Menu Element
* @param bool $isStartElement (optional) Whether the Menu should start with this Element
* @return static
*/
public function addElement(MenuElement $menuElement, $isStartElement = false)
{
if (!in_array($menuElement, $this->elements, true)) {
array_push($this->elements, $menuElement);
if ($isStartElement) {
// new start element
$this->setStartElement($menuElement);
} else {
// additional element - set invisible
$menuElement->getControl()
->setVisible(false);
}
}
return $this;
}
/**
* Get the Element to start with
*
* @api
* @return MenuElement
*/
public function getStartElement()
{
return $this->startElement;
}
/**
* Set the Element to start with
*
* @api
* @param MenuElement $startElement Start Element
* @return static
*/
public function setStartElement(MenuElement $startElement = null)
{
$this->startElement = $startElement;
if ($startElement && !in_array($startElement, $this->elements, true)) {
array_push($this->elements, $startElement);
}
return $this;
}
/**
* @see ScriptFeature::prepare()
*/
public function prepare(Script $script)
{
$updateFunctionName = self::FUNCTION_UPDATE_MENU;
$elementsArrayText = $this->getElementsArrayText();
// OnInit
if ($this->startElement) {
$startControlId = Builder::escapeText($this->startElement->getControl()->getId());
$initScriptText = "
2014-06-21 03:18:21 +02:00
{$updateFunctionName}({$elementsArrayText}, {$startControlId});";
2017-03-25 18:40:15 +01:00
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $initScriptText, true);
}
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
// MouseClick
$scriptText = "
2014-04-27 14:44:40 +02:00
declare MenuElements = {$elementsArrayText};
if (MenuElements.existskey(Event.Control.ControlId)) {
declare ShownControlId = MenuElements[Event.Control.ControlId];
{$updateFunctionName}(MenuElements, ShownControlId);
}";
2017-03-25 18:40:15 +01:00
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $scriptText, true);
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
// Update menu function
$updateFunctionText = "
2014-04-27 14:44:40 +02:00
Void {$updateFunctionName}(Text[Text] _Elements, Text _ShownControlId) {
foreach (ItemId => ControlId in _Elements) {
declare Control <=> (Page.GetFirstChild(ControlId));
Control.Visible = (ControlId == _ShownControlId);
}
}";
2017-03-25 18:40:15 +01:00
$script->addScriptFunction($updateFunctionName, $updateFunctionText);
2014-05-14 23:24:00 +02:00
2017-03-25 18:40:15 +01:00
return $this;
}
/**
* Build the array text for the Elements
*
* @return string
*/
protected function getElementsArrayText()
{
$elements = array();
foreach ($this->elements as $element) {
$elementId = $element->getItem()
->getId();
$elements[$elementId] = $element->getControl()
->getId();
}
return Builder::getArray($elements, true);
}
2014-04-27 14:44:40 +02:00
}