-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFlxInputText.as
executable file
·199 lines (161 loc) · 6.18 KB
/
FlxInputText.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
*** FlxInputText v0.9, Input text field extension for Flixel ***
IMPORTANT: You need to modify the FlxText class so that the _tf member is protected instead of private.
New members:
getText()
setMaxLength()
backgroundColor
borderColor
backgroundVisible
borderVisible
forceUpperCase
filterMode
customFilterPattern
Copyright (c) 2009 Martin Sebastian Wain
License: Creative Commons Attribution 3.0 United States
(http://creativecommons.org/licenses/by/3.0/us/)
(A tiny "single line comment" reference in the source code is more than sufficient as attribution :)
*/
package {
import org.flixel.*;
import flash.events.Event;
import flash.text.TextFieldType;
//@desc Input field class that "fits in" with Flixel's workflow
public class FlxInputText extends FlxText {
static public const NO_FILTER:uint = 0;
static public const ONLY_ALPHA:uint = 1;
static public const ONLY_NUMERIC:uint = 2;
static public const ONLY_ALPHANUMERIC:uint = 3;
static public const CUSTOM_FILTER:uint = 4;
//@desc Defines what text to filter. It can be NO_FILTER, ONLY_ALPHA, ONLY_NUMERIC, ONLY_ALPHA_NUMERIC or CUSTOM_FILTER
// (Remember to append "FlxInputText." as a prefix to those constants)
public var filterMode:uint = NO_FILTER;
//@desc This regular expression will filter out (remove) everything that matches. This is activated by setting filterMode = FlxInputText.CUSTOM_FILTER.
public var customFilterPattern:RegExp = /[]*/g;
//@desc If this is set to true, text typed is forced to be uppercase
public var forceUpperCase:Boolean = false;
protected var _fixedheight:Boolean = false;
protected var initialHeight:Number;
//@desc Same parameters as FlxText
public function FlxInputText(X:Number, Y:Number, Width:uint, Height:uint, Text:String,
Color:uint = 0x000000, Font:String = null, Size:uint = 8,
Justification:String = null, multiline:Boolean = false,
changeListener:Function = null) {
super(X, Y, Width, Text);
setFormat(Font, Size, Color, Justification);
_tf.height = initialHeight = Height;
multiline = true;
_tf.selectable = true;
_tf.type = TextFieldType.INPUT;
_tf.background = true;
_tf.backgroundColor = (~Color) ^ 0x888888;
_tf.alwaysShowSelection = true;
_tf.border = true;
_tf.borderColor = Color;
_tf.x = x;
_tf.y = y;
//_tf.visible = false;
calcFrame();
//_tf.addEventListener(Event.ENTER_FRAME, onEnterFrame);
_tf.addEventListener(Event.REMOVED_FROM_STAGE, onInputFieldRemoved);
_tf.addEventListener(Event.CHANGE, onTextChange);
FlxG.state.addChild(_tf);
this.changeListener = changeListener;
}
//@desc Boolean flag in case render() is called BEFORE onEnterFrame() (_tf would be always hidden)
// NOTE: I don't think it's necessary, but I'll leave it just in case.
//@param Direction True is Right, False is Left (see static const members RIGHT and LEFT)
//private var nextFrameHide:Boolean = false;
/*override public function render():void {
_tf.x=x;
_tf.y=y;
_tf.visible = true;
//nextFrameHide = false;
} */
private function onInputFieldRemoved(event:Event):void
{
//Clean up after ourselves
//_tf.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
_tf.removeEventListener(Event.REMOVED, onInputFieldRemoved);
_tf.removeEventListener(Event.CHANGE, onTextChange);
}
protected var frameListener:Function;
/*private function onEnterFrame(event:Event):void
{
if(nextFrameHide)
_tf.visible=false;
nextFrameHide = true;
} */
protected var changeListener:Function;
private function onTextChange(event:Event):void
{
if (_fixedheight)
while (_tf.textHeight >= initialHeight)
_tf.text = _tf.text.slice(0, _tf.text.length - 1);
if(forceUpperCase)
_tf.text = _tf.text.toUpperCase();
if(filterMode != NO_FILTER) {
var pattern:RegExp;
switch(filterMode) {
case ONLY_ALPHA: pattern = /[^a-zA-Z]*/g; break;
case ONLY_NUMERIC: pattern = /[^0-9]*/g; break;
case ONLY_ALPHANUMERIC: pattern = /[^a-zA-Z0-9]*/g; break;
case CUSTOM_FILTER: pattern = customFilterPattern; break;
default:
throw new Error("FlxInputText: Unknown filterMode ("+filterMode+")");
}
_tf.text = _tf.text.replace(pattern, "");
}
if (changeListener != null) {
changeListener(event);
}
}
//@desc Text field background color
public function set backgroundColor(Color:uint):void { _tf.backgroundColor = Color; }
//@desc Text field border color
public function set borderColor(Color:uint):void { _tf.borderColor = Color; }
//@desc Shows/hides background
public function set backgroundVisible(Enabled:Boolean):void { _tf.background = Enabled; }
//@desc Shows/hides border
public function set borderVisible(Enabled:Boolean):void { _tf.border = Enabled; }
//@desc Text field background color
public function get backgroundColor():uint { return _tf.backgroundColor; }
//@desc Text field border color
public function get borderColor():uint { return _tf.borderColor; }
//@desc Shows/hides background
public function get backgroundVisible():Boolean { return _tf.background; }
//@desc Shows/hides border
public function get borderVisible():Boolean { return _tf.border; }
//@desc Set the maximum length for the field (e.g. "3" for Arcade type hi-score initials)
//@param Length The maximum length. 0 means unlimited.
public function setMaxLength(Length:uint):void
{
_tf.maxChars = Length;
}
public function fixedHeight():void {
_fixedheight = !_fixedheight;
}
//@desc Get the text the user has typed
public function getText():String
{
return _tf.text;
}
public function set multiline(value:Boolean):void
{
_tf.multiline = value;
_tf.wordWrap = value;
}
public function get multiline():Boolean
{
return _tf.multiline;
}
/*public function toggleSelect():void {
_tf.backgroundColor ^= 0xffffff;
//color ^= 0xffffffff;
color = (color ? 0x000000 : 0xffffff)
} */
public function selectAll():void {
_tf.setSelection(0, _tf.text.length);
}
}
}