Ripeworks

Aug 14 FIVe3D custom Text Input component

Lately I have been doing nothing but creating 3D applications with Flash. I started with Papervision3D, and my current library of choice is FIVe3D (). Although it doesn’t have a camera object to pan and rotate your scene, it is a very light-weight and very fast library.

For one of my projects, it called for a textInput component. My first attempt was to add a textInput component to a Sprite3D object. Even thought the component displays properly, all of the animations that I had tied to that Sprite3D object were completely broken. After some quick Googling, I realized that I was on my own for this one, and needed to write my own component to handle this.

Enter DynamicText3DInput. I built a rather basic class which now makes it possible to input text in FIVe3D! Source and usage below:

Usage:

1
2
3
4
5
6
7
8
import five3D.component.DynamicText3DInput;
import five3D.typography.HelveticaBold;
...
var inputBox:DynamicText3DInput = new DynamicText3DInput(HelveticaBold);
inputBox.boxColor = 0xffffff;
inputBox.textColor = 0x000000;
inputBox.width = 200;
addChild(inputBox);

Source:

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
package five3D.component {
   
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import flash.events.TimerEvent;
    import flash.geom.Rectangle;
    import flash.utils.Timer;
   
    import five3D.display.DynamicText3D;
    import five3D.display.Sprite3D;
    import five3D.display.Shape3D;
   
    public class DynamicText3DInput extends Sprite3D {
       
        private var inputText:DynamicText3D;
       
        private var _width:int;
        private var _boxColor:uint;
        private var _boxPadding:int = 5;
        private var _bounds:Rectangle;
        private var _inputMask:Shape3D;
        private var _cursor:Shape3D;
        private var _cursorTimer:Timer;
       
        public function DynamicText3DInput(typography:Object):void {
            super();
           
            inputText = new DynamicText3D(typography);
           
            init();
        }
       
        override public function get width():Number {
            return _width;
        }
       
        override public function set width(value:Number):void {
            _width = value;
           
            adjustBox();
        }
       
        public function get boxColor():uint {
            return _boxColor;
        }
       
        public function set boxColor(value:uint):void {
            _boxColor = value;
            adjustBox();
        }
       
        public function get textColor():uint {
            return inputText.color;
        }
       
        public function set textColor(value:uint):void {
            inputText.color = value;
            drawCursor();
        }
       
        public function get size():int {
            return inputText.size;
        }
       
        public function set size(value:int):void {
            inputText.size = value;
            adjustBox();
        }
       
        public function get text():String {
            return inputText.text;
        }
       
        private function init():void {
            this.addChild(inputText);
            registerListeners();
            this.focusRect = false;
           
            _inputMask = new Shape3D();
            addChild(_inputMask);
            inputText.mask = _inputMask;
           
            _cursor = new Shape3D();
            addChild(_cursor);
           
            setupDefaults();
        }
       
        private function setupDefaults():void {
            _boxColor = 0xffffff;
            _width = 100;
            inputText.size = 10;
            inputText.color = 0x000000;
            inputText.x = _boxPadding;
            inputText.y = _boxPadding;
            _cursor.y = _boxPadding;
            _cursor.alpha = 0;
            drawCursor();
            _cursorTimer = new Timer(250);
            _cursorTimer.addEventListener(TimerEvent.TIMER, _adjustCursor, false, 0, true);
        }
       
        private function adjustBox():void {
            _bounds = new Rectangle(0, 0, _width + (_boxPadding * 2), inputText.size + (_boxPadding * 2));
            this.graphics3D.clear();
            this.graphics3D.beginFill(_boxColor, 1);
            this.graphics3D.drawRect(0, 0, _bounds.width, _bounds.height);
            this.graphics3D.endFill();
           
            _inputMask.graphics3D.clear();
            _inputMask.graphics3D.beginFill(_boxColor, 1);
            _inputMask.graphics3D.drawRect(0, 0, _bounds.width, _bounds.height);
            _inputMask.graphics3D.endFill();
           
            drawCursor();
        }
       
        private function drawCursor():void {
            _cursor.graphics3D.clear();
            _cursor.graphics3D.beginFill(inputText.color, 1);
            _cursor.graphics3D.drawRect(0, 0, 1, inputText.size);
            _cursor.graphics3D.endFill();
        }
       
        private function registerListeners():void {
            this.addEventListener(MouseEvent.CLICK, _boxClickHandler, false, 0, true);
            this.addEventListener(KeyboardEvent.KEY_DOWN, _keyPressHandler, false, 0, true);
        }
       
        private function _adjustCursor(e:TimerEvent):void {
            if( _cursor.alpha == 1)
                _cursor.alpha = 0;
            else
                _cursor.alpha = 1;
           
            if( this.stage.focus != this){
                _cursorTimer.stop();
                _cursor.alpha = 0;
            }
           
        }
       
        private function _boxClickHandler(e:MouseEvent):void {
            this.stage.focus = this;
           
            _cursorTimer.start();
        }
       
        private function _keyPressHandler(e:KeyboardEvent):void {
            if( this.stage.focus != this)
                return;
               
            switch(e.keyCode){
                case 8:
                    //delete the char beofre the cursor
                    inputText.text = inputText.text.substr(0, inputText.text.length - 1);
                    break;
                default:
                    inputText.text += String.fromCharCode(e.charCode);
                    break;
            }
           
            if( inputText.textWidth > _width ){
                inputText.x = (-inputText.textWidth) + _width + _boxPadding;
            }else{
                inputText.x = _boxPadding;
            }
           
            _cursor.x = inputText.x + inputText.textWidth + 1;
        }
    }
}

While this works, it would also be nice to adjust the cursor position, have selectable text, with copy+paste, etc.. But this works for now. Enjoy!

Comments (0)

Submit Comment