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

141 lines
3.9 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\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
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 Menu extends ScriptFeature {
/*
* Constants
*/
const FUNCTION_UPDATE_MENU = 'FML_UpdateMenu';
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-04-27 14:44:40 +02:00
*/
2014-06-21 03:18:21 +02:00
/** @var MenuElement[] $elements */
2014-04-27 14:44:40 +02:00
protected $elements = array();
2014-05-14 23:24:00 +02:00
/** @var MenuElement $startElement */
2014-04-27 14:44:40 +02:00
protected $startElement = null;
/**
* Construct a new Menu Feature
*
2014-06-21 03:18:21 +02:00
* @param Control $item (optional) Item Control in the Menu bar
2014-04-27 14:44:40 +02:00
* @param Control $control (optional) Toggled Menu Control
*/
public function __construct(Control $item = null, Control $control = null) {
if ($item && $control) {
2014-05-14 23:24:00 +02:00
$this->addElement($item, $control);
2014-04-27 14:44:40 +02:00
}
}
/**
* Add a new Element to the Menu
*
2014-06-21 03:18:21 +02:00
* @param Control $item Item Control in the Menu bar
2014-05-14 23:24:00 +02:00
* @param Control $control Toggled Menu Control
* @param bool $isStartElement (optional) Whether the Menu should start with this Element
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function addElement(Control $item, Control $control, $isStartElement = false) {
$menuElement = new MenuElement($item, $control);
$this->appendElement($menuElement, $isStartElement);
return $this;
}
/**
* Append an Element to the Menu
*
2014-05-14 23:24:00 +02:00
* @param MenuElement $menuElement Menu Element
* @param bool $isStartElement (optional) Whether the Menu should start with this Element
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function appendElement(MenuElement $menuElement, $isStartElement = false) {
2014-06-21 03:18:21 +02:00
if (!in_array($menuElement, $this->elements, true)) {
array_push($this->elements, $menuElement);
if ($isStartElement) {
$this->setStartElement($menuElement);
} else if (count($this->elements) > 1) {
$menuElement->getControl()->setVisible(false);
}
2014-04-27 14:44:40 +02:00
}
return $this;
}
/**
* Set the Element to start with
*
* @param MenuElement $startElement Starting Element
2014-07-03 22:34:47 +02:00
* @return static
2014-04-27 14:44:40 +02:00
*/
public function setStartElement(MenuElement $startElement) {
$this->startElement = $startElement;
if (!in_array($startElement, $this->elements, true)) {
array_push($this->elements, $startElement);
}
return $this;
}
/**
* @see \FML\Script\Features\ScriptFeature::prepare()
*/
public function prepare(Script $script) {
$updateFunctionName = self::FUNCTION_UPDATE_MENU;
2014-05-14 23:24:00 +02:00
$elementsArrayText = $this->getElementsArrayText();
2014-04-27 14:44:40 +02:00
// OnInit
if ($this->startElement) {
2014-06-21 03:18:21 +02:00
$startControlId = $this->startElement->getControl()->getId(true, true);
2014-04-27 14:44:40 +02:00
$initScriptText = "
2014-06-21 03:18:21 +02:00
{$updateFunctionName}({$elementsArrayText}, {$startControlId});";
2014-04-27 14:44:40 +02:00
$script->appendGenericScriptLabel(ScriptLabel::ONINIT, $initScriptText, true);
}
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
// MouseClick
$scriptText = "
declare MenuElements = {$elementsArrayText};
if (MenuElements.existskey(Event.Control.ControlId)) {
declare ShownControlId = MenuElements[Event.Control.ControlId];
{$updateFunctionName}(MenuElements, ShownControlId);
}";
$script->appendGenericScriptLabel(ScriptLabel::MOUSECLICK, $scriptText, true);
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
// Update menu function
$updateFunctionText = "
Void {$updateFunctionName}(Text[Text] _Elements, Text _ShownControlId) {
foreach (ItemId => ControlId in _Elements) {
declare Control <=> (Page.GetFirstChild(ControlId));
Control.Visible = (ControlId == _ShownControlId);
}
}";
$script->addScriptFunction($updateFunctionName, $updateFunctionText);
2014-05-14 23:24:00 +02:00
2014-04-27 14:44:40 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Build the array text for the Elements
2014-04-27 14:44:40 +02:00
*
* @return string
*/
protected function getElementsArrayText() {
$elements = array();
foreach ($this->elements as $element) {
2014-05-14 23:24:00 +02:00
$elementId = $element->getItem()->getId();
$elements[$elementId] = $element->getControl()->getId();
2014-04-27 14:44:40 +02:00
}
return Builder::getArray($elements, true);
}
}