FML Improvements
This commit is contained in:
		| @@ -5,18 +5,18 @@ namespace FML\Script; | ||||
| /** | ||||
|  * Builder Class offering Methods to build ManiaScript | ||||
|  * | ||||
|  * @author steeffeen | ||||
|  * @author    steeffeen | ||||
|  * @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder | ||||
|  * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 | ||||
|  * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3 | ||||
|  */ | ||||
| abstract class Builder { | ||||
|  | ||||
| 	/** | ||||
| 	 * Build a Label Implementation Block | ||||
| 	 * | ||||
| 	 * @param string $labelName Name of the Label | ||||
| 	 * @param string $labelName          Name of the Label | ||||
| 	 * @param string $implementationCode Label Implementation Coding (without declaration) | ||||
| 	 * @param bool $isolate Whether the Code should be isolated in an own Block | ||||
| 	 * @param bool   $isolate            Whether the Code should be isolated in an own Block | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| 	public static function getLabelImplementationBlock($labelName, $implementationCode, $isolate = true) { | ||||
| @@ -30,14 +30,14 @@ abstract class Builder { | ||||
| 	/** | ||||
| 	 * Escape dangerous Characters in the given Text | ||||
| 	 * | ||||
| 	 * @param string $text Text to escape | ||||
| 	 * @param bool $addApostrophes (optional) Whether to add Apostrophes before and after the Text | ||||
| 	 * @param string $text           Text to escape | ||||
| 	 * @param bool   $addApostrophes (optional) Whether to add Apostrophes before and after the Text | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| 	public static function escapeText($text, $addApostrophes = false) { | ||||
| 		$dangers = array('\\', '"', "\n"); | ||||
| 		$dangers      = array('\\', '"', "\n"); | ||||
| 		$replacements = array('\\\\', '\\"', '\\n'); | ||||
| 		$escapedText = str_ireplace($dangers, $replacements, $text); | ||||
| 		$escapedText  = str_ireplace($dangers, $replacements, $text); | ||||
| 		if ($addApostrophes) { | ||||
| 			$escapedText = '"' . $escapedText . '"'; | ||||
| 		} | ||||
| @@ -51,8 +51,8 @@ abstract class Builder { | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| 	public static function getReal($value) { | ||||
| 		$value = (float) $value; | ||||
| 		$stringVal = (string) $value; | ||||
| 		$value     = (float)$value; | ||||
| 		$stringVal = (string)$value; | ||||
| 		if (!fmod($value, 1)) { | ||||
| 			$stringVal .= '.'; | ||||
| 		} | ||||
| @@ -66,7 +66,7 @@ abstract class Builder { | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| 	public static function getBoolean($value) { | ||||
| 		$bool = (bool) $value; | ||||
| 		$bool = (bool)$value; | ||||
| 		if ($bool) { | ||||
| 			return "True"; | ||||
| 		} | ||||
| @@ -76,28 +76,26 @@ abstract class Builder { | ||||
| 	/** | ||||
| 	 * Get the String-Representation of the given Array | ||||
| 	 * | ||||
| 	 * @param array $array Array to convert to a ManiaScript Array | ||||
| 	 * @param bool $associative (optional) Whether the Array should be associative | ||||
| 	 * @param array $array       Array to convert to a ManiaScript Array | ||||
| 	 * @param bool  $associative (optional) Whether the Array should be associative | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| 	public static function getArray(array $array, $associative = false) { | ||||
| 		$arrayText = '['; | ||||
| 		$index = 0; | ||||
| 		$count = count($array); | ||||
| 		$index     = 0; | ||||
| 		$count     = count($array); | ||||
| 		foreach ($array as $key => $value) { | ||||
| 			if ($associative) { | ||||
| 				if (is_string($key)) { | ||||
| 					$arrayText .= '"' . self::escapeText($key) . '"'; | ||||
| 				} | ||||
| 				else { | ||||
| 				} else { | ||||
| 					$arrayText .= $key; | ||||
| 				} | ||||
| 				$arrayText .= ' => '; | ||||
| 			} | ||||
| 			if (is_string($value)) { | ||||
| 				$arrayText .= '"' . self::escapeText($value) . '"'; | ||||
| 			} | ||||
| 			else { | ||||
| 			} else { | ||||
| 				$arrayText .= $value; | ||||
| 			} | ||||
| 			if ($index < $count - 1) { | ||||
| @@ -112,7 +110,7 @@ abstract class Builder { | ||||
| 	/** | ||||
| 	 * Get the Include Command for the given File and Namespace | ||||
| 	 * | ||||
| 	 * @param string $file Include File | ||||
| 	 * @param string $file      Include File | ||||
| 	 * @param string $namespace Include Namespace | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| @@ -124,7 +122,7 @@ abstract class Builder { | ||||
| 	/** | ||||
| 	 * Get the Constant Command for the given Name and Value | ||||
| 	 * | ||||
| 	 * @param string $name Constant Name | ||||
| 	 * @param string $name  Constant Name | ||||
| 	 * @param string $value Constant Value | ||||
| 	 * @return string | ||||
| 	 */ | ||||
|   | ||||
| @@ -89,6 +89,15 @@ class CheckBoxFeature extends ScriptFeature { | ||||
| 		return $this; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get the managed Entry | ||||
| 	 * | ||||
| 	 * @return \FML\Controls\Entry | ||||
| 	 */ | ||||
| 	public function getEntry() { | ||||
| 		return $this->entry; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Set the Enabled Design | ||||
| 	 * | ||||
| @@ -134,6 +143,7 @@ class CheckBoxFeature extends ScriptFeature { | ||||
| Void " . self::FUNCTION_UPDATE_QUAD_DESIGN . "(CMlQuad _Quad) { | ||||
| 	declare " . self::VAR_CHECKBOX_ENABLED . " as Enabled for _Quad = True; | ||||
| 	Enabled = !Enabled; | ||||
| 	_Quad.StyleSelected = Enabled; | ||||
| 	declare " . self::VAR_CHECKBOX_DESIGNS . " as Designs for _Quad = Text[Boolean]; | ||||
| 	declare Design = Designs[Enabled]; | ||||
| 	declare DesignParts = TextLib::Split(\"|\", Design); | ||||
|   | ||||
| @@ -22,7 +22,6 @@ class ControlScript extends ScriptFeature { | ||||
| 	protected $control = null; | ||||
| 	protected $labelName = null; | ||||
| 	protected $text = null; | ||||
| 	protected $isolated = null; | ||||
|  | ||||
| 	/** | ||||
| 	 * Construct a new Custom Script Text | ||||
| @@ -30,13 +29,11 @@ class ControlScript extends ScriptFeature { | ||||
| 	 * @param Control $control   Event Control | ||||
| 	 * @param string  $text      Script Text | ||||
| 	 * @param string  $labelName (optional) Script Label Name | ||||
| 	 * @param bool    $isolated  (optional) Whether to isolate the Script Text | ||||
| 	 */ | ||||
| 	public function __construct(Control $control, $text, $labelName = ScriptLabel::MOUSECLICK, $isolated = true) { | ||||
| 	public function __construct(Control $control, $text, $labelName = ScriptLabel::MOUSECLICK) { | ||||
| 		$this->setControl($control); | ||||
| 		$this->setText($text); | ||||
| 		$this->setLabelName($labelName); | ||||
| 		$this->setIsolated($isolated); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| @@ -76,36 +73,47 @@ class ControlScript extends ScriptFeature { | ||||
| 		return $this; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Set whether the Script should be isolated | ||||
| 	 * | ||||
| 	 * @param bool $isolated Whether to isolate the Script Text | ||||
| 	 * @return \FML\Script\Features\ControlScript | ||||
| 	 */ | ||||
| 	public function setIsolated($isolated = true) { | ||||
| 		$this->isolated = (bool)$isolated; | ||||
| 		return $this; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * @see \FML\Script\Features\ScriptFeature::prepare() | ||||
| 	 */ | ||||
| 	public function prepare(Script $script) { | ||||
| 		$script->appendGenericScriptLabel($this->labelName, $this->getEncapsulatedText(), $this->isolated); | ||||
| 		$script->appendGenericScriptLabel($this->labelName, $this->buildScriptText(), true); | ||||
| 		return $this; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get the Script Text encapsulated for the Control Event | ||||
| 	 * Build the Script Text for the Control | ||||
| 	 * | ||||
| 	 * @return string | ||||
| 	 */ | ||||
| 	protected function getEncapsulatedText() { | ||||
| 	protected function buildScriptText() { | ||||
| 		$controlId  = $this->control->getId(true); | ||||
| 		$scriptText = " | ||||
| if (Event.ControlId == \"{$controlId}\") { | ||||
| 	{$this->text} | ||||
| }"; | ||||
| 		$scriptText = ''; | ||||
| 		$closeBlock = false; | ||||
|  | ||||
| 		if (ScriptLabel::isEventLabel($this->labelName)) { | ||||
| 			$scriptText .= ' | ||||
| if (Event.ControlId == "' . $controlId . '") { | ||||
| declare Control <=> Event.Control;'; | ||||
| 			$closeBlock = true; | ||||
| 		} else { | ||||
| 			$scriptText .= ' | ||||
| declare Control <=> Page.GetFirstChild("' . $controlId . '");'; | ||||
| 		} | ||||
|  | ||||
| 		$class = $this->control->getManiaScriptClass(); | ||||
| 		$name  = preg_replace('/^CMl/', '', $class, 1); | ||||
| 		$scriptText .= ' | ||||
| declare ' . $name . ' <=> (Control as ' . $class . '); | ||||
| '; | ||||
|  | ||||
| 		$scriptText .= $this->text . ' | ||||
| '; | ||||
|  | ||||
| 		if ($closeBlock) { | ||||
| 			$scriptText .= '}'; | ||||
| 		} | ||||
|  | ||||
| 		return $scriptText; | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -3,16 +3,37 @@ | ||||
| namespace FML\Script\Features; | ||||
|  | ||||
| use FML\Script\Script; | ||||
| use FML\Types\ScriptFeatureable; | ||||
|  | ||||
| /** | ||||
|  * ManiaLink Script Feature Class | ||||
|  * | ||||
|  * @author steeffeen | ||||
|  * @author    steeffeen | ||||
|  * @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder | ||||
|  * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 | ||||
|  * @license   http://www.gnu.org/licenses/ GNU General Public License, Version 3 | ||||
|  */ | ||||
| abstract class ScriptFeature { | ||||
|  | ||||
| 	/** | ||||
| 	 * Collect the Script Features of the given Objects | ||||
| 	 * | ||||
| 	 * @param object $scriptFeatureable ScriptFeatureable Object | ||||
| 	 * @param object $_                 (optional) Various Amount of additional Objects | ||||
| 	 * @return array | ||||
| 	 */ | ||||
| 	public static function collect($scriptFeatureable, $_ = null) { | ||||
| 		$params         = func_get_args(); | ||||
| 		$scriptFeatures = array(); | ||||
| 		foreach ($params as $object) { | ||||
| 			if ($object instanceof ScriptFeatureable) { | ||||
| 				$scriptFeatures = array_merge($scriptFeatures, $object->getScriptFeatures()); | ||||
| 			} else if ($object instanceof ScriptFeature) { | ||||
| 				array_push($scriptFeatures, $object); | ||||
| 			} | ||||
| 		} | ||||
| 		return $scriptFeatures; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Prepare the given Script for Rendering by adding the needed Labels, etc. | ||||
| 	 * | ||||
|   | ||||
| @@ -75,6 +75,29 @@ class ScriptLabel { | ||||
| 		return $this; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Check if the given Label is an Event Label | ||||
| 	 * | ||||
| 	 * @param string $label Label Name | ||||
| 	 * @return bool | ||||
| 	 */ | ||||
| 	public static function isEventLabel($label) { | ||||
| 		$eventLabels = self::getEventLabels(); | ||||
| 		if (in_array($label, $eventLabels)) { | ||||
| 			return true; | ||||
| 		} | ||||
| 		return false; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Get the possible Event Label Names | ||||
| 	 * | ||||
| 	 * @return array | ||||
| 	 */ | ||||
| 	public static function getEventLabels() { | ||||
| 		return array(self::ENTRYSUBMIT, self::KEYPRESS, self::MOUSECLICK, self::MOUSEOUT, self::MOUSEOVER); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * Build the full Script Label Text | ||||
| 	 * | ||||
|   | ||||
		Reference in New Issue
	
	Block a user