ScriptField

A multi-line text field with optional syntax highlighting for scripts written in the ActionScript 3/haxe, xml/flex/degrafa/svg/xaml or ruby/hotruby.  Lua and io languages are commented out.  It uses a vertical scrollbar and word wrap to display scripts that are larger than the viewable area.

The performance of the current syntax highlighting implementation limits it to be usable by smaller scripts and code snippets.  Syntax highlighting can be turned off when displaying larger scripts.

Summary
ScriptFieldA multi-line text field with optional syntax highlighting for scripts written in the ActionScript 3/haxe, xml/flex/degrafa/svg/xaml or ruby/hotruby.
Public constants
DEFAULT_WIDTHDefault width of a script window.
DEFAULT_HEIGHTDefault height of a script window.
defaultColorDefault base theme color of text and scrollbar in a script window.
defaultFontSizeDefault size of the text font used in a script window.
Private display objects
inputInternal text field used for display and edit of scripts.
scrollbarVertical scrollbar to allow browsing of larger scripts.
Private callback functions
changeCallbackOptional callback triggered when the text in the scriptField has been changed.
keyDownCallbackOptional callback triggered when a keyboard button is pressed down while focus is in the scriptField.
Private data members
scriptEngineNameName of the script engine language currently used for syntax highlighting.
syntaxA dynamic object holding syntax properties of the current script language.
Public property
enableSyntaxHighlightProperty indicating if syntax highlighting is on or off.
Constructor
ScriptFieldCreates a multi-line script field with word wrap and a vertical scrollbar.
Public properties
textRetrieve the current script text.
textChange the script text displayed in the field.
changeHandlerChange the callback function called when the script is changed.
keyDownHandlerChange the callback function called when a keyboard button is pressed.
Public methods
homeDisplays the top of the script that is the home position.
Script hightligthing
languageSets the scripting language for the text field.
syntaxHighlightSyntax highlights by setting text formats on the script field text.
Event handlers
inputChangeHandlerThe text in the input field has been changed
inputKeyDownHandlerA keyboard key has been pressed in the input field.
scrollHandlerThe user has scrolled the text in the input field.
scrollbarHandlerThe user has manipulated the scrollbar position.
WidgetInterface
activeSets the state of the widget to active or inactive.
activateActivates the widget, adds its event listeners
disactivateDisactivates the widget, removes its event listeners
disposeDispose of all resources used or referenced in the instance.
destructorRemoves used resources, references and graphics

Public constants

DEFAULT_WIDTH

public static const DEFAULT_WIDTH: uint

Default width of a script window.

DEFAULT_HEIGHT

public static const DEFAULT_HEIGHT: uint

Default height of a script window.

defaultColor

public static const defaultColor: int

Default base theme color of text and scrollbar in a script window.

defaultFontSize

public static const defaultFontSize: int

Default size of the text font used in a script window.

Private display objects

input

private var input: TextField

Internal text field used for display and edit of scripts.

scrollbar

private var scrollbar: Scrollbar

Vertical scrollbar to allow browsing of larger scripts.

Private callback functions

changeCallback

private var changeCallback: Function

Optional callback triggered when the text in the scriptField has been changed.

keyDownCallback

private var keyDownCallback: Function

Optional callback triggered when a keyboard button is pressed down while focus is in the scriptField.

Private data members

scriptEngineName

private var scriptEngineName: String

Name of the script engine language currently used for syntax highlighting.

syntax

private var syntax: Object

A dynamic object holding syntax properties of the current script language.  The properties are strings with regular expressions for matching lexical tokens.

Properties

multiLineCommentBeginbegin of a comment block; for lua “--[[“
multiLineCommentEndend of a comment block; for lua “]]”
singleLineCommentcomment to end of line; for lua “--”
keywordswords that are highlighted as keywords; for lua “and|break|...”
operatorstokens that are highlighted as operators; for lua “+|-|*|/|...”
hexPrefixprefix used for hex numbers; for lua null, for as3 “0x”

See also

language

Public property

enableSyntaxHighlight

public var enableSyntaxHighlight: Boolean

Property indicating if syntax highlighting is on or off.

Changes to this property takes effect when the text property is changed, when the user changes the script or when the script language is changed the next time.

Constructor

ScriptField

public function ScriptField(owner: String,  
inputText: String = "",
fieldWidth: uint = DEFAULT_WIDTH,
fieldHeight: uint = DEFAULT_HEIGHT,
color: uint = defaultColor,
fontSize: uint = defaultFontSize)

Creates a multi-line script field with word wrap and a vertical scrollbar.  No script language is selected.

Parameters

ownername of registered owner; see WidgetSprite.register
inputTextinitial script text displayed in the field
fieldWidthwidth of the field (default 200 pixels)
fieldHeightheight of the field (default 200 pixels)
colorbase theme color for the script text and scrollbar (default MediumGrey)
fontSizesize of the font used (default 10)

Public properties

text

public function get text():String

Retrieve the current script text.

text

public function set text(value: String):void

Change the script text displayed in the field.  The text is syntax highlighted if enabled.

Parameters

valuea new script text

changeHandler

public function set changeHandler(changeHandlerFunction: Function):void

Change the callback function called when the script is changed.  Set to null to disable callbacks.

Parameters

changeHandlerFunctionfunction called when the script text changes

Callbacks

changeHandlerFunctionfunction onScriptFieldChange(script:String):void

keyDownHandler

public function set keyDownHandler(keyDownHandlerFunction: Function):void

Change the callback function called when a keyboard button is pressed.  Set to null to disable callbacks.

Parameters

changeHandlerFunctionfunction called when a button is pressed

Callbacks

changeHandlerFunctionfunction onScriptFieldKeyDown(event:KeyboardEvent):void

Public methods

home

public function home():void

Displays the top of the script that is the home position.

Script hightligthing

language

public function set language(scriptLanguageName: String):void

Sets the scripting language for the text field.  For syntax highlighting to be displayed the enableSyntaxHighlight property must be set to true.

The implementation could be changed to retrieve the language details from an external source (XML file or Soap server) to avoid code bloat if the number of languages increases.

Parameters

scriptLanguageNameEither “as3”, “haxe”, “xml”, “degrafa”, “svg”, “xaml”, “ruby”, “hotruby” or “” to disable

syntaxHighlight

private function syntaxHighlight():void

Syntax highlights by setting text formats on the script field text.

It uses a simple multiscan method which does not scale well to larger scripts.  Alternatives are only to color the visible script text or to implement a single pass scanner.

Event handlers

inputChangeHandler

private function inputChangeHandler(event: Event):void

The text in the input field has been changed

Parameters

eventan Event.CHANGE event

inputKeyDownHandler

private function inputKeyDownHandler(event: KeyboardEvent):void

A keyboard key has been pressed in the input field.  Calls the keydown callback if it has been set.

Parameters

eventa keyboard event

scrollHandler

private function scrollHandler(event: Event):void

The user has scrolled the text in the input field.  Updates the scrollbar to the new position.

Parameters

eventa scroll event

scrollbarHandler

private function scrollbarHandler(newPosition: Number):void

The user has manipulated the scrollbar position.  Updates the text field to display the new position.

Parameters

newPositiona numeric line number position

WidgetInterface

active

public function set active(state: Boolean):void

Sets the state of the widget to active or inactive.

Parameters

stateTrue to activate, false otherwise.

activate

private function activate():void

Activates the widget, adds its event listeners

disactivate

private function disactivate():void

Disactivates the widget, removes its event listeners

dispose

public function dispose():void

Dispose of all resources used or referenced in the instance.

destructor

private function destructor():void

Removes used resources, references and graphics

Widgets are required to implement this interface.
Base class for widgets to enable/disable event listeners and keep a global tab order of all widgets (provided they are created and registered in tab order.
public static const DEFAULT_WIDTH: uint
Default width of a script window.
public static const DEFAULT_HEIGHT: uint
Default height of a script window.
public static const defaultColor: int
Default base theme color of text and scrollbar in a script window.
public static const defaultFontSize: int
Default size of the text font used in a script window.
private var input: TextField
Internal text field used for display and edit of scripts.
private var scrollbar: Scrollbar
Vertical scrollbar to allow browsing of larger scripts.
private var changeCallback: Function
Optional callback triggered when the text in the scriptField has been changed.
private var keyDownCallback: Function
Optional callback triggered when a keyboard button is pressed down while focus is in the scriptField.
private var scriptEngineName: String
Name of the script engine language currently used for syntax highlighting.
private var syntax: Object
A dynamic object holding syntax properties of the current script language.
public var enableSyntaxHighlight: Boolean
Property indicating if syntax highlighting is on or off.
public function ScriptField(owner: String,  
inputText: String = "",
fieldWidth: uint = DEFAULT_WIDTH,
fieldHeight: uint = DEFAULT_HEIGHT,
color: uint = defaultColor,
fontSize: uint = defaultFontSize)
Creates a multi-line script field with word wrap and a vertical scrollbar.
public function get text():String
Retrieve the current script text.
public function set changeHandler(changeHandlerFunction: Function):void
Change the callback function called when the script is changed.
public function set keyDownHandler(keyDownHandlerFunction: Function):void
Change the callback function called when a keyboard button is pressed.
public function home():void
Displays the top of the script that is the home position.
public function set language(scriptLanguageName: String):void
Sets the scripting language for the text field.
private function syntaxHighlight():void
Syntax highlights by setting text formats on the script field text.
private function inputChangeHandler(event: Event):void
The text in the input field has been changed
private function inputKeyDownHandler(event: KeyboardEvent):void
A keyboard key has been pressed in the input field.
private function scrollHandler(event: Event):void
The user has scrolled the text in the input field.
private function scrollbarHandler(newPosition: Number):void
The user has manipulated the scrollbar position.
public function set active(state: Boolean):void
Sets the state of the widget to active or inactive.
private function activate():void
Activates the widget, adds its event listeners
private function disactivate():void
Disactivates the widget, removes its event listeners
public function dispose():void
Dispose of all resources used or referenced in the instance.
private function destructor():void
Removes used resources, references and graphics
public function register(owner: String):void
Register the owner of a widget.
Close