Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added class TextArea and TextInputGroup, and other minor changes

  • Loading branch information...
commit 8796fa16dea42f616f252376916c8686ab7bc9d1 1 parent 394a734
Cyril Michel HANSEN authored February 15, 2012

Showing 1 changed file with 650 additions and 19 deletions. Show diff stats Hide diff stats

  1. 669  build/caat.js
669  build/caat.js
@@ -41,6 +41,7 @@ TIME: 20:35:01
41 41
  */
42 42
 var CAAT= CAAT || {};
43 43
 
  44
+
44 45
 /**
45 46
  * Common bind function. Allows to set an object's function as callback. Set for every function in the
46 47
  * javascript context.
@@ -65,6 +66,7 @@ Function.prototype.bind= Function.prototype.bind || function() {
65 66
  */
66 67
 function extend(subc, superc) {
67 68
     var subcp = subc.prototype;
  69
+CAAT.Color
68 70
 
69 71
     // Class pattern.
70 72
     var F = function() {
@@ -2519,7 +2521,7 @@ var cp1= proxy(
2519 2521
      *
2520 2522
      * <p>
2521 2523
      * For am exponential interpolation, the getPosition function would look like this:
2522  
-     * <code>function getPosition(time) { return { x:time, y: Math.pow(time,2) }Ê}</code>.
  2524
+     * <code>function getPosition(time) { return { x:time, y: Math.pow(time,2) }}</code>.
2523 2525
      * meaning that for time=0.5, a value of 0,5*0,5 should use instead.
2524 2526
      *
2525 2527
      * <p>
@@ -3862,7 +3864,7 @@ var cp1= proxy(
3862 3864
          * 
3863 3865
          * @param time the time to apply the scale for.
3864 3866
          * @param actor the target actor to Scale.
3865  
-         * @return {object} an object of the form <code>{ scaleX: {float}, scaleY: {float}Ê}</code>
  3867
+         * @return {object} an object of the form <code>{ scaleX: {float}, scaleY: {float}}</code>
3866 3868
          */
3867 3869
 		setForTime : function(time,actor) {
3868 3870
 
@@ -4183,7 +4185,7 @@ var cp1= proxy(
4183 4185
          * If autoRotate=true, the actor is rotated as well. The rotation anchor will (if set) always be ANCHOR_CENTER.
4184 4186
          * @param time an integer indicating the time the behavior is being applied at.
4185 4187
          * @param actor a CAAT.Actor instance to be translated.
4186  
-         * @return {object} an object of the form <code>{ x: {float}, y: {float}Ê}</code>.
  4188
+         * @return {object} an object of the form <code>{ x: {float}, y: {float}}</code>.
4187 4189
          */
4188 4190
 		setForTime : function(time,actor) {
4189 4191
 
@@ -4330,7 +4332,7 @@ var cp1= proxy(
4330 4332
          *
4331 4333
          * @param time the time to apply the scale for.
4332 4334
          * @param actor the target actor to Scale.
4333  
-         * @return {object} an object of the form <code>{ scaleX: {float}, scaleY: {float}Ê}</code>
  4335
+         * @return {object} an object of the form <code>{ scaleX: {float}, scaleY: {float}}</code>
4334 4336
          */
4335 4337
 		setForTime : function(time,actor) {
4336 4338
 
@@ -5318,6 +5320,21 @@ var cp1= proxy(
5318 5320
             this.parent= parent;
5319 5321
             return this;
5320 5322
         },
  5323
+
  5324
+        getScene : function() {
  5325
+            if ( typeof this.parent ==='undefined' || this.parent === null) {
  5326
+                return  null;
  5327
+            }
  5328
+
  5329
+            //console.log("this.parent.name " + this.parent.name);
  5330
+            if (this.parent.name === 'CAAT.Scene') {
  5331
+                return  this.parent;
  5332
+            }
  5333
+
  5334
+            return this.parent.getScene();
  5335
+        },
  5336
+
  5337
+
5321 5338
         /**
5322 5339
          * Set this actor's background image.
5323 5340
          * The need of a background image is to kept compatibility with the new CSSDirector class.
@@ -7156,7 +7173,7 @@ var cp1= proxy(
7156 7173
                 throw('adding to a container an element with parent.');
7157 7174
             }
7158 7175
 
7159  
-            child.parent= this;
  7176
+            child.setParent(this); // Pour surcharger / polymorphisme de TextArea
7160 7177
             this.childrenList.push(child);
7161 7178
             child.dirty= true;
7162 7179
 
@@ -9038,6 +9055,7 @@ var cp1= proxy(
9038 9055
         nDirtyRects         :   0,
9039 9056
 
9040 9057
         collidingActors     :   null,
  9058
+        name                : "CAAT.Director",
9041 9059
 
9042 9060
         solveCollissions : function() {
9043 9061
             if ( !this.collidingActors.length ) {
@@ -10309,7 +10327,7 @@ var cp1= proxy(
10309 10327
          *    will have their areas redrawn.
10310 10328
          *  + CAAT.Director.CLEAR_NONE. clears nothing.
10311 10329
          *
10312  
-         * @param clear {CAAT.Director.CLEAR_ALL |ÊCAAT.Director.CLEAR_NONE | CAAT.Director.CLEAR_DIRTY_RECTS}
  10330
+         * @param clear {CAAT.Director.CLEAR_ALL |CAAT.Director.CLEAR_NONE | CAAT.Director.CLEAR_DIRTY_RECTS}
10313 10331
          * @return this.
10314 10332
          */
10315 10333
         setClear : function(clear) {
@@ -11153,6 +11171,29 @@ CAAT.registerKeyListener= function(f) {
11153 11171
     CAAT.keyListeners.push(f);
11154 11172
 };
11155 11173
 
  11174
+CAAT.registerKeyListenerIfNeeded= function(f) {
  11175
+ for( var i=0; i<CAAT.keyListeners.length; i++ ) {
  11176
+        if ( f===CAAT.keyListeners[i] ) {            
  11177
+            return;
  11178
+        }
  11179
+    }
  11180
+
  11181
+    CAAT.keyListeners.push(f);
  11182
+};
  11183
+
  11184
+/**
  11185
+ * Unregister a key events notification function
  11186
+ * @param f {function}
  11187
+ */
  11188
+CAAT.unregisterKeyListener= function(f) {
  11189
+    for( var i=0; i<CAAT.windowResizeListeners.length; i++ ) {
  11190
+        if ( f===CAAT.keyListeners[i] ) {
  11191
+            CAAT.keyListeners.splice(i,1);
  11192
+            return;
  11193
+        }
  11194
+    }
  11195
+};
  11196
+
11156 11197
 CAAT.Keys = {
11157 11198
     ENTER:13,
11158 11199
     BACKSPACE:8,
@@ -11324,7 +11365,7 @@ CAAT.GlobalEnableEvents= function __GlobalEnableEvents() {
11324 11365
 
11325 11366
     this.GlobalEventsEnabled= true;
11326 11367
 
11327  
-    window.addEventListener('keydown',
  11368
+    document.addEventListener('keydown',
11328 11369
         function(evt) {
11329 11370
             var key = (evt.which) ? evt.which : evt.keyCode;
11330 11371
 
@@ -11350,7 +11391,7 @@ CAAT.GlobalEnableEvents= function __GlobalEnableEvents() {
11350 11391
         },
11351 11392
         false);
11352 11393
 
11353  
-    window.addEventListener('keyup',
  11394
+    document.addEventListener('keyup',
11354 11395
         function(evt) {
11355 11396
 
11356 11397
             var key = (evt.which) ? evt.which : evt.keyCode;
@@ -11375,9 +11416,29 @@ CAAT.GlobalEnableEvents= function __GlobalEnableEvents() {
11375 11416
                 }
11376 11417
             }
11377 11418
         },
11378  
-        false );
  11419
+        true );
  11420
+
  11421
+    document.addEventListener('keypress',
  11422
+        function(evt) {
  11423
+
  11424
+            var key = (evt.which) ? evt.which : evt.charCode;
  11425
+
  11426
+                for( var i=0; i<CAAT.keyListeners.length; i++ ) {
  11427
+                    CAAT.keyListeners[i]( new CAAT.KeyEvent(
  11428
+                        key,
  11429
+                        'press',
  11430
+                        {
  11431
+                            alt:        CAAT.KEY_MODIFIERS.alt,
  11432
+                            control:    CAAT.KEY_MODIFIERS.control,
  11433
+                            shift:      CAAT.KEY_MODIFIERS.shift
  11434
+                        },
  11435
+                        evt));
  11436
+                }
  11437
+        },
  11438
+        true );
11379 11439
 
11380  
-    window.addEventListener('resize',
  11440
+
  11441
+    document.addEventListener('resize',
11381 11442
         function(evt) {
11382 11443
             for( var i=0; i<CAAT.windowResizeListeners.length; i++ ) {
11383 11444
                 CAAT.windowResizeListeners[i].windowResized(
@@ -11385,7 +11446,7 @@ CAAT.GlobalEnableEvents= function __GlobalEnableEvents() {
11385 11446
                         window.innerHeight);
11386 11447
             }
11387 11448
         },
11388  
-        false);
  11449
+        true);
11389 11450
 };
11390 11451
 
11391 11452
 /**
@@ -12307,6 +12368,7 @@ CAAT.RegisterDirector= function __CAATGlobal_RegisterDirector(director) {
12307 12368
                   }
12308 12369
               }
12309 12370
         }
  12371
+        
12310 12372
 
12311 12373
         
12312 12374
     };
@@ -12538,6 +12600,7 @@ CAAT.RegisterDirector= function __CAATGlobal_RegisterDirector(director) {
12538 12600
         timerSequence:                  0,      // incremental CAAT.TimerTask id.
12539 12601
 
12540 12602
         paused:                         false,
  12603
+        name:                           "CAAT.Scene",
12541 12604
 
12542 12605
         isPaused :  function()  {
12543 12606
             return this.paused;
@@ -13657,7 +13720,7 @@ CAAT.modules.CircleManager = CAAT.modules.CircleManager || {};/**
13657 13720
          * Transparent means that every scan pixel is alpha=0.
13658 13721
          * @param image
13659 13722
          * @param threshold {integer} any value below or equal to this will be optimized.
13660  
-         * @param !areas { object{ top<boolean>, bottom<boolean>, left<boolean, right<boolean> }Ê}
  13723
+         * @param !areas { object{ top<boolean>, bottom<boolean>, left<boolean, right<boolean> }}
13661 13724
          */
13662 13725
     CAAT.modules.ImageUtil.optimize= function(image, threshold, areas ) {
13663 13726
         threshold>>=0;
@@ -13950,9 +14013,10 @@ CAAT.modules.CircleManager = CAAT.modules.CircleManager || {};/**
13950 14013
             var cchar;
13951 14014
 
13952 14015
             for( i=0; i<chars.length; i++ ) {
13953  
-                var cw= Math.max( 1, (ctx.measureText( chars.charAt(i) ).width>>0)+1 ) + 2 * padding ;
  14016
+               // var cw= Math.max( 1, (ctx.measureText( chars.charAt(i) ).width>>0)+1 ) + 2 * padding ;
  14017
+                var cw= Math.max( 1, (ctx.measureText( chars.charAt(i) ).width>>0)+1 );
13954 14018
                 charWidth.push(cw);
13955  
-                textWidth+= cw;
  14019
+                textWidth+= cw + 2 * padding;
13956 14020
             }
13957 14021
 
13958 14022
             canvas.width= textWidth;
@@ -13966,19 +14030,19 @@ CAAT.modules.CircleManager = CAAT.modules.CircleManager || {};/**
13966 14030
 
13967 14031
             this.charMap= {};
13968 14032
 
13969  
-            x=0;
  14033
+            x=padding; // x=0
13970 14034
             for( i=0; i<chars.length; i++ ) {
13971 14035
                 cchar= chars.charAt(i);
13972  
-                ctx.fillText( cchar, x+padding, 0 );
  14036
+                ctx.fillText( cchar, x, 0 ); // x + padding
13973 14037
                 if ( this.strokeStyle ) {
13974 14038
                     ctx.beginPath();
13975  
-                    ctx.strokeText( cchar, x+padding,  0 );
  14039
+                    ctx.strokeText( cchar, x,  0 ); // x + padding
13976 14040
                 }
13977 14041
                 this.charMap[cchar]= {
13978 14042
                     x:      x,
13979 14043
                     width:  charWidth[i]
13980 14044
                 };
13981  
-                x+= charWidth[i];
  14045
+                x+= charWidth[i]  + 2 * padding; // prochaine position
13982 14046
             }
13983 14047
 
13984 14048
             this.image= CAAT.modules.ImageUtil.optimize( canvas, 32, { top: true, bottom: true, left: false, right: false } );
@@ -14025,6 +14089,10 @@ CAAT.modules.CircleManager = CAAT.modules.CircleManager || {};/**
14025 14089
                 }
14026 14090
             }
14027 14091
         },
  14092
+        // Temporary hack, best solution should be to reconciliate drawing API
  14093
+        drawString : function( ctx, str, x, y ) {
  14094
+            this.drawText(str, ctx, x,y);
  14095
+            },
14028 14096
 
14029 14097
         save : function() {
14030 14098
             var str= "image/png";
@@ -17774,4 +17842,567 @@ function makeOrtho(left, right, bottom, top, znear, zfar) {
17774 17842
         }
17775 17843
     };
17776 17844
 
17777  
-})();
  17845
+})();
  17846
+
  17847
+(function() {
  17848
+
  17849
+    /**
  17850
+     * TextArea
  17851
+     *
  17852
+     * Single line editor, movable cursor, insertion mode, backspace and delete
  17853
+     * Requires a sprite sheet based font which provides stringWidth(str)
  17854
+
  17855
+     * @constructor
  17856
+     * @extends CAAT.ActorContainer
  17857
+     *
  17858
+     */
  17859
+    CAAT.TextArea = function() {
  17860
+        CAAT.TextArea.superclass.constructor.call(this);
  17861
+
  17862
+        // Setup default font sprite sheet
  17863
+        this.setDefaultFont();
  17864
+
  17865
+        this.lineHeight = this.font.height;
  17866
+        this.textAlign= "left";
  17867
+        this.textBaseline= "top";
  17868
+        this.outlineColor= "black";
  17869
+        this.clip= false;
  17870
+        this.enableEvents(true);
  17871
+        this.setDefaultCursor();
  17872
+
  17873
+        // initial state: no focus, no cursor
  17874
+        this.hideCursor();
  17875
+
  17876
+        return this;
  17877
+    };
  17878
+
  17879
+    CAAT.TextArea.prototype= {
  17880
+        font:               null,   // a sprite sheet based font object (CAAT.Font or SpriteImage)                                    
  17881
+        text:               null,   // a string with the text to draw.
  17882
+        textWidth:          0,      // an integer indicating text width in pixels.
  17883
+        textHeight:         0,      // an integer indicating text height in pixels.
  17884
+        cursorPos:          0,      // after last character of text
  17885
+        cursorActor:        null,
  17886
+        lineHeight:         0,
  17887
+
  17888
+    
  17889
+        setDefaultFont: function() {
  17890
+          this.font = new CAAT.Font().
  17891
+                        setFontSize(24).
  17892
+                        createDefault(2); // padding required to isolate letters from each others
  17893
+
  17894
+    },
  17895
+
  17896
+        setDefaultCursor: function() {
  17897
+            if (this.cursorActor != null) {
  17898
+                this.removeChild(this.cursorActor);
  17899
+            }
  17900
+
  17901
+                this.cursorActor= new CAAT.TextActor().
  17902
+                    setFont(this.font).
  17903
+                    setTextFillStyle("black").
  17904
+                    setOutline(true).
  17905
+                    enableEvents(false).
  17906
+                    setText('_').
  17907
+                    setLocation (0, 5);
  17908
+
  17909
+                this.setDefaultCursorBehavior();
  17910
+                this.addChild(this.cursorActor);
  17911
+        },
  17912
+
  17913
+        setDefaultCursorBehavior : function() {
  17914
+            this.cursorActor.emptyBehaviorList();
  17915
+            var cursorAlphaB = new CAAT.AlphaBehavior().
  17916
+                  setValues(1,0).
  17917
+                  setCycle(true).
  17918
+                  setPingPong().
  17919
+                  setFrameTime(0, 1500);
  17920
+
  17921
+            this.cursorActor.addBehavior(cursorAlphaB);
  17922
+        },
  17923
+
  17924
+        hideCursor : function() {
  17925
+             this.cursorActor.emptyBehaviorList();
  17926
+             this.cursorActor.setVisible(false);
  17927
+        },
  17928
+
  17929
+        showCursor : function() {
  17930
+             this.cursorActor.setVisible(true);
  17931
+             this.setDefaultCursorBehavior();
  17932
+        },
  17933
+
  17934
+       
  17935
+
  17936
+        onFocus: function() {
  17937
+          // Cursor becomes visible  
  17938
+          if (this.cursorActor != null) {
  17939
+            this.showCursor();
  17940
+            } else {
  17941
+                this.setDefaultCursor();
  17942
+            }
  17943
+
  17944
+        },
  17945
+
  17946
+        onFocusLost: function() {
  17947
+            if (this.cursorActor != null) {
  17948
+               // Make cursor invisible
  17949
+                this.hideCursor();
  17950
+            } 
  17951
+          
  17952
+        },
  17953
+
  17954
+       
  17955
+        moveLeft : function( ) {
  17956
+            if (null!==this.text && this.cursorPos > 0) {
  17957
+               this.setCursorPos(this.cursorPos -1);  
  17958
+            }
  17959
+        },
  17960
+        
  17961
+        moveRight : function( ) {
  17962
+            if (null!==this.text && this.cursorPos < this.text.length) {
  17963
+               this.setCursorPos(this.cursorPos +1);  
  17964
+            }
  17965
+        },
  17966
+
  17967
+        delete : function( ) {
  17968
+           if (null!==this.text && this.text.length > 0) {
  17969
+            var newText = this.text.substr(0 , this.cursorPos) +  this.text.substr( this.cursorPos +1, this.text.length  - this.cursorPos - 1);
  17970
+            this.setText(newText, true);
  17971
+            // cursorPos modified only if last character was deleted
  17972
+            if (this.cursorPos > this.text.length) {
  17973
+                this.setCursorPos(this.text.length);
  17974
+               // this.cursorPos = this.text.length;
  17975
+            }
  17976
+
  17977
+           }
  17978
+        },
  17979
+
  17980
+        backspace : function( ) {
  17981
+           if (this.cursorPos > 0) {
  17982
+            var newText = this.text.substr(0 , this.cursorPos -1) +  this.text.substr( this.cursorPos, this.text.length  - this.cursorPos);
  17983
+            this.setText(newText, true);
  17984
+            this.setCursorPos(this.cursorPos - 1);
  17985
+           }
  17986
+        },
  17987
+
  17988
+        insert : function( newChar ) {
  17989
+           if ( null!== this.text) {
  17990
+            var newText = this.text.substr(0 , this.cursorPos) + newChar +   this.text.substr( this.cursorPos, this.text.length  - this.cursorPos);
  17991
+            this.setText(newText, true);         
  17992
+           } else {
  17993
+               this.setText(newChar, true);
  17994
+           }
  17995
+           this.setCursorPos(this.cursorPos+1);
  17996
+        },
  17997
+
  17998
+        setCursorPos : function( pos ) {
  17999
+            console.log("setCursorPos : before " + this.cursorPos);
  18000
+              if ( null=== this.text) {
  18001
+             this.cursorPos = 0;
  18002
+           } else {
  18003
+                if (typeof(pos) === 'undefined'  || null ===pos) {
  18004
+                       this.cursorPos = this.text.length; 
  18005
+                } else { 
  18006
+            this.cursorPos = pos;
  18007
+              }
  18008
+              console.log("setCursorPos : after " + this.cursorPos);
  18009
+              cursorPosx = this.font.stringWidth(this.text.substr(0 , this.cursorPos));
  18010
+
  18011
+              if (this.cursorActor != null) {
  18012
+                this.cursorActor.setLocation( cursorPosx , 0);
  18013
+              }
  18014
+           }
  18015
+        },
  18016
+
  18017
+        
  18018
+        /**
  18019
+         * Set the text to be shown by the actor.
  18020
+         * @param sText a string with the text to be shown.
  18021
+         * @return this
  18022
+         */
  18023
+        setText : function( sText, keepCursor) {
  18024
+            this.text= sText;
  18025
+            if ( null===this.text || this.text==="" ) {
  18026
+                this.width= this.height= 0;
  18027
+            }
  18028
+            this.calcTextSize( CAAT.director[0] );
  18029
+            if (typeof(keepCursor) === 'undefined'  || !keepCursor) {
  18030
+            this.setCursorPos();
  18031
+            }
  18032
+            return this;
  18033
+        },
  18034
+        /**
  18035
+         * Sets the font to be applied for the text.
  18036
+         * @param font a string with a valid canvas rendering context font description.
  18037
+         * @return this
  18038
+         */
  18039
+        setFont : function(font) {
  18040
+
  18041
+            if ( !font ) {
  18042
+                this.setDefaultFont();
  18043
+            }
  18044
+
  18045
+            this.font= font;
  18046
+            this.calcTextSize( CAAT.director[0] );
  18047
+
  18048
+            // Update cursor to the same font
  18049
+            this.setDefaultCursor();
  18050
+
  18051
+            return this;
  18052
+        },
  18053
+        /**
  18054
+         * Calculates the text dimension in pixels and stores the values in textWidth and textHeight
  18055
+         * attributes.
  18056
+         * If Actor's width and height were not set, the Actor's dimension will be set to these values.
  18057
+         * @return this
  18058
+         */
  18059
+        calcTextSize : function() {
  18060
+
  18061
+            if ( typeof this.text==='undefined' || null===this.text || ""===this.text ) {
  18062
+                this.textWidth= 0;
  18063
+                this.textHeight= 0;
  18064
+                return this;
  18065
+            }
  18066
+
  18067
+            // The font provides stringWidth()
  18068
+
  18069
+            this.textWidth= this.font.stringWidth(this.text);
  18070
+            this.width= this.textWidth;
  18071
+
  18072
+
  18073
+            this.lineHeight = this.font.height;
  18074
+            this.textHeight= this.lineHeight;
  18075
+            this.height= this.textHeight;
  18076
+
  18077
+            return this;
  18078
+        },
  18079
+
  18080
+        /**
  18081
+         * Custom paint method for TextArea instances.
  18082
+         * If the path attribute is set, the text will be drawn traversing the path.
  18083
+         *
  18084
+         * @param director a valid CAAT.Director instance.
  18085
+         * @param time an integer with the Scene time the Actor is being drawn.
  18086
+         */
  18087
+        paint : function(director, time) {
  18088
+
  18089
+            CAAT.TextArea.superclass.paint.call(this, director, time );
  18090
+
  18091
+            // TextArea can be cached (but not editors)
  18092
+            if ( this.cached ) {
  18093
+                // cacheAsBitmap sets this actor's background image as a representation of itself.
  18094
+                // So if after drawing the background it was cached, we're done.
  18095
+                return;
  18096
+            }
  18097
+
  18098
+            if ( null===this.text) {
  18099
+                return; 
  18100
+            }
  18101
+
  18102
+            if ( this.textWidth===0 || this.textHeight===0 ) {
  18103
+                this.calcTextSize(director);
  18104
+            }
  18105
+
  18106
+            var ctx= director.ctx;
  18107
+            
  18108
+            // Is drawing on path useful here ?
  18109
+            if (typeof this.font === 'object') {
  18110
+                return this.drawSpriteText(director,time);
  18111
+            }        
  18112
+        },
  18113
+        
  18114
+        // Ou peut on conserver les méthodes héritées de TextActor?
  18115
+
  18116
+        /**
  18117
+         * Private.
  18118
+         * Draw the text using a sprited font instead of a canvas font.
  18119
+         * @param director a valid CAAT.Director instance.
  18120
+         * @param time an integer with the Scene time the Actor is being drawn.
  18121
+         */
  18122
+        drawSpriteText: function(director, time) {
  18123
+                this.font.drawString( director.ctx, this.text, 0, 0);
  18124
+        }
  18125
+    };
  18126
+
  18127
+    extend( CAAT.TextArea, CAAT.ActorContainer, null);
  18128
+})();
  18129
+
  18130
+
  18131
+(function() {
  18132
+
  18133
+    /**
  18134
+     * TextInputGroup
  18135
+     * Invisible Container managing the focus of a set of TextAreas
  18136
+     * childrenList order is used for navigation
  18137
+     * focus can also be set programatically
  18138
+
  18139
+     * @constructor
  18140
+     * @extends CAAT.ActorContainer
  18141
+     *
  18142
+     */
  18143
+    CAAT.TextInputGroup = function() {
  18144
+        CAAT.TextInputGroup.superclass.constructor.call(this);
  18145
+
  18146
+        // A singleton is created to store a map between scenes and input groups
  18147
+        if (typeof(CAAT.TextInputGroup.SceneFocusManager) === 'undefined' ) {
  18148
+             CAAT.TextInputGroup.SceneFocusManager = {};// dictionnaire Scene -> TextInputGroup 
  18149
+        }
  18150
+
  18151
+        return this;
  18152
+    };
  18153
+
  18154
+
  18155
+    CAAT.TextInputGroup.prototype= {
  18156
+        focusedChild:       null,
  18157
+        focusedChildIndex:    null, // position in childrenList of ActorContainer
  18158
+
  18159
+        setParent : function(parent) {
  18160
+            CAAT.TextInputGroup.superclass.setParent.call(this, parent);
  18161
+            return this;
  18162
+        },
  18163
+
  18164
+        deferredInit: function(director) {
  18165
+            var sceneIndex = director.getSceneIndex(this.getScene()); // A tester
  18166
+
  18167
+            // Associate Scene -> TextInputGroup
  18168
+            CAAT.TextInputGroup.SceneFocusManager[sceneIndex] = this;
  18169
+
  18170
+            // Key event callback must be defined only once, otherwise key events will be duplicated
  18171
+            CAAT.registerKeyListenerIfNeeded(this.___keyAction.bind(this));
  18172
+        },
  18173
+
  18174
+
  18175
+         ___keyAction: function(event) {
  18176
+            // Events are discarded if the scene is not current one
  18177
+            if (CAAT.director[0].currentScene != this.getScene()) { return; }
  18178
+
  18179
+                var inputText = this.getFocused();
  18180
+                if (typeof(inputText) === 'undefined'  || typeof(inputText) === 'undefined'  || null ===inputText) return;
  18181
+
  18182
+                if (event.getAction()=='down') {
  18183
+                // event.preventDefault();
  18184
+                 //console.log("text = " + CAAT.TextArea.focus.text);
  18185
+                 switch (event.getKeyCode()) {
  18186
+                     case CAAT.Keys['DELETE']:
  18187
+                      // The empty text case has already been excluded above
  18188
+                      inputText.delete();    
  18189
+                      event.preventDefault();              
  18190
+                     break;
  18191
+                    case CAAT.Keys['BACKSPACE']:
  18192
+                      // The empty text case has already been excluded above
  18193
+                      inputText.backspace();    
  18194
+                      event.preventDefault();              
  18195
+                     break;                 
  18196
+                    case CAAT.Keys['LEFT']:
  18197
+                    inputText.moveLeft();
  18198
+                    event.preventDefault();
  18199
+                    break;
  18200
+                      case CAAT.Keys['RIGHT']:
  18201
+                    inputText.moveRight();
  18202
+                    event.preventDefault();
  18203
+                    break;
  18204
+                      case CAAT.Keys['UP']:
  18205
+                        case CAAT.Keys['DOWN']: 
  18206
+                        // LATER ?
  18207
+                          event.preventDefault();
  18208
+                        break;
  18209
+                    case CAAT.Keys['TAB']:
  18210
+                        if (event.isShiftPressed()) {
  18211
+                            this.prevChild();
  18212
+                        } else {
  18213
+                            this.nextChild();
  18214
+                        }
  18215
+                          event.preventDefault();
  18216
+                        break;                    
  18217
+                     default:
  18218
+                     // other keys handled as keypress events
  18219
+                     // event.preventDefault(); is not called as it would cancel the keypress event
  18220
+                     break;
  18221
+                 }
  18222
+
  18223
+            }
  18224
+            if (event.getAction()=='press') {
  18225
+
  18226
+                switch (event.getKeyCode()) {
  18227
+                     case CAAT.Keys['DELETE']:
  18228
+                     case CAAT.Keys['BACKSPACE']:           
  18229
+                     break;
  18230
+                    case CAAT.Keys['LEFT']:
  18231
+
  18232
+                    break;
  18233
+                      case CAAT.Keys['RIGHT']:
  18234
+
  18235
+                    break;              
  18236
+                     default:
  18237
+                    var newChar = String.fromCharCode(event.getKeyCode());
  18238
+                        inputText.insert(newChar);
  18239
+                    
  18240
+                     break;
  18241
+                 }
  18242
+  
  18243
+            }
  18244
+
  18245
+        },
  18246
+
  18247
+        getFocused: function() {
  18248
+          return this.focusedChild;
  18249
+        },
  18250
+
  18251
+
  18252
+        prevChild: function() {
  18253
+            if (this.focusedChildIndex > 0) {
  18254
+
  18255
+                this.notifyFocusLost(this.focusedChild);  
  18256
+
  18257
+                this.focusedChildIndex--;
  18258
+                this.focusedChild = this.childrenList[this.focusedChildIndex];
  18259
+  
  18260
+                this.notifyFocus(this.focusedChild);
  18261
+            }
  18262
+        },
  18263
+
  18264
+        nextChild: function() {
  18265
+             var newFocusedChildIndex = null;
  18266
+
  18267
+                 if (this.focusedChildIndex < this.childrenList.length -1) {
  18268
+                    newFocusedChildIndex = this.focusedChildIndex+1;
  18269
+                    newFocusedChild = this.childrenList[this.focusedChildIndex++];
  18270
+      
  18271
+                } else {
  18272
+                if (this.focusedChildIndex === this.childrenList.length -1) {
  18273
+                     newFocusedChildIndex = 0;
  18274
+                     newFocusedChild = this.childrenList[newFocusedChildIndex];
  18275
+                 }
  18276
+             }
  18277
+
  18278
+                 if (newFocusedChildIndex !=null) {
  18279
+                     this.notifyFocusLost(this.focusedChild);
  18280
+
  18281
+                     this.focusedChildIndex = newFocusedChildIndex;
  18282
+                     this.focusedChild = this.childrenList[newFocusedChildIndex];
  18283
+
  18284
+                     this.notifyFocus(this.focusedChild);
  18285
+                 }
  18286
+        },        
  18287
+       /**
  18288
+         * Removes all children from this ActorContainer.
  18289
+         *
  18290
+         * @return this
  18291
+         */
  18292
+        emptyChildren : function() {
  18293
+            CAAT.TextInputGroup.superclass.emptyChildren.call(this);
  18294
+
  18295
+            // notify previously focused Child of lost of focus ?
  18296
+            // The actor will not be shown anymore ??
  18297
+            this.notifyFocusLost(this.focusedChild);
  18298
+
  18299
+            this.focusedChild= null;
  18300
+            this.focusedChildIndex = null;
  18301
+
  18302
+            return this;
  18303
+        },
  18304
+       /**
  18305
+         * Adds an Actor to this ActorContainer.
  18306
+
  18307
+         * @param child a CAAT.Actor object instance.
  18308
+         * @return this
  18309
+         */
  18310
+        addChild : function(child) {
  18311
+
  18312
+            CAAT.TextInputGroup.superclass.addChild.call(this, child);
  18313
+
  18314
+            // Focus it only if it is the first child
  18315
+            this.setFocusToFirstChildOnly(child);
  18316
+
  18317
+            return this;
  18318
+        },
  18319
+        
  18320
+         setFocusToFirstChildOnly : function(child) {
  18321
+            // set focus only if it is the first child added
  18322
+            if (this.focusedChild === null) {
  18323
+                this.focusedChildIndex = 0;
  18324
+                this.focusedChild = child;
  18325
+                this.notifyFocus(this.focusedChild);
  18326
+            }
  18327
+        },
  18328
+       
  18329
+        /**
  18330
+         * Adds an Actor to this ActorContainer.
  18331
+         *
  18332
+         * @param child a CAAT.Actor object instance.
  18333
+         *
  18334
+         * @return this
  18335
+         */
  18336
+        addChildAt : function(child, index) {
  18337
+
  18338
+            CAAT.TextInputGroup.superclass.addChildAt.call(this, child, index);
  18339
+
  18340
+            // No change in focus in the general case
  18341
+            this.setFocusToFirstChildOnly(child);
  18342
+
  18343
+            return this;
  18344
+        },
  18345
+       
  18346
+        /**
  18347
+         * Removed an Actor form this ActorContainer.
  18348
+         * If the Actor is not contained into this Container, nothing happends.
  18349
+         *
  18350
+         * @param child a CAAT.Actor object instance.
  18351
+         *
  18352
+         * @return this
  18353
+         */
  18354
+        removeChild : function(child) {
  18355
+
  18356
+            CAAT.TextInputGroup.superclass.removeChild.call(this, child);
  18357
+
  18358
+            // If the child had focus, update the state
  18359
+             if (this.focusedChild === child) {
  18360
+                this.focusedChild = null;
  18361
+                this.focusedChildIndex = null;
  18362
+                this.notifyFocusLost(child);
  18363
+            }
  18364
+
  18365
+            return this;
  18366
+        },
  18367
+
  18368
+        /**
  18369
+         * Sets the font to be applied for the text.
  18370
+         * @param font a string with a valid canvas rendering context font description.
  18371
+         * @return this
  18372
+         */
  18373
+        focusChild : function(child) {
  18374
+            var childIndex = this.childrenList.indexOf(child);
  18375
+
  18376
+            if (childIndex != -1) {
  18377
+
  18378
+                if (this.focusedChild != null) {
  18379
+                    this.notifyFocusLost(this.focusedChild);
  18380
+                }
  18381
+
  18382
+                this.focusedChild = child;
  18383
+                this.focusedChildIndex = childIndex;
  18384
+                this.notifyFocus(child);
  18385
+        }
  18386
+
  18387
+            return this;
  18388
+        },
  18389
+
  18390
+        notifyFocus : function(child) {
  18391
+            child.onFocus();
  18392
+        },
  18393
+
  18394
+
  18395
+        notifyFocusLost : function(child) {
  18396
+            if (child != null) {
  18397
+                child.onFocusLost();
  18398
+            }
  18399
+        }
  18400
+
  18401
+
  18402
+        
  18403
+        
  18404
+    };
  18405
+
  18406
+    extend( CAAT.TextInputGroup, CAAT.ActorContainer, null);
  18407
+})();
  18408
+

0 notes on commit 8796fa1

Please sign in to comment.
Something went wrong with that request. Please try again.