6 Replies Latest reply on Sep 3, 2006 1:39 PM by cristian.rotundu

    Absolute coordinates of Component inside a Grid Parent

    ddanone Level 1
      I have a TextInput component inside a Grid component and it inside another component...
      How can i know the TextInput absolute x,y coordinates?

      <mx:TabNavigator ......>
      <mx:Canvas ...>
      <mx:Grid ...>
      <mx:GridRow>
      <mx:GridItem>
      <mx:TextInput id="AAA" />
      .....

      Thanks in advance
        • 1. Re: Absolute coordinates of Component inside a Grid Parent
          peterent Level 2
          What do you mean by "absolute" coordinates? Coordinates of the TextInput relative to the screen or relative to its parent?

          For the latter, AA.x and AA.y yield the relative to parent coordinates.

          For the screen, you need to translate them to global space:

          var point:Point = new Point( AA.x, AA.y );
          AA.parent.localToGlobal(point); // ie, the GridItem - AA's coordinates are in its coordinate space

          Now point is relative to the upper-left corner of the application screen space.

          • 2. Re: Absolute coordinates of Component inside a Grid Parent
            ddanone Level 1
            Hi Peter,
            that does not works because the parent (grid) is a child of a Canvas and the canvas is inside a TabNavigator.

            I refer to screen coordinates, the final goal is to place a ToolTip nex to the TextArea like Validators does.


            • 3. Re: Absolute coordinates of Component inside a Grid Parent
              ddanone Level 1
              I've translated this snipet from Javascript to Actionscript and WORKS!!

              public function getAbsPosition(o:Object):Object{
              var oL:Number=0;
              var oT:Number=0;
              var oH:Number=0;
              var oW:Number=0;
              oH=o.height;
              oW=o.width;
              while (o){
              oL+=o.x;
              oT+=o.y;
              o=o.parent;
              }
              Alert.show('x= '+oL+'\ny= '+oT+'\nw= '+oW+'\nh= '+oH);
              return {x:oL,y:oT,w:oW, h:oH}
              }


              To test it:

              var p:Object=getAbsPosition( the_object_you_want_to_calculate_coords );
              Alert.show('x= '+p.x+'\ny= '+p.y+'\nw= '+p.w+'\nh= '+p.h);

              Now "p" is an object containing the coordinates, width and height.



              • 4. Re: Absolute coordinates of Component inside a Grid Parent
                peterent Level 2
                My bad. I should have tried it before giving you an answer. This works nicely:

                <mx:TabNavigator ... />
                <mx:Canvas ... />
                <mx:Grid ... />
                <mx:GridItem>
                <mx:TextInput rollOver="showInfo(event)" rollOut="hideInfo()" />
                <mx:GridItem>
                ...
                </mx:Grid>
                </mx:Canvas>
                </mx:TabNavigator>

                import mx.managers.PopUpManager;

                private function showInfo( event:flash.events.MouseEvent ) : void
                {
                var p:Point = new Point(event.target.x, event.target.y);
                var pt:DisplayObject = DisplayObject(event.target);
                p = pt.localToGlobal(p);
                tip = PopUpManager.createPopUp( this, <<your class here>>, false ) as <<your class here>>;
                tip.move( p.x+pt.width+5, p.y );
                }

                When the mouse rolls over the TextInput (or whatever control you want), showInfo is called. The showInfo function converts the position of the TextInput (that's event.target) to global coordinates. Then the pop-up is created and moved into position, just to the right of the TextInput.

                I have working code if you want it.
                • 5. Re: Absolute coordinates of Component inside a Grid Parent
                  ddanone Level 1
                  Thanks again Peter, that WORKS fine too!!

                  I have many many Javascript code that it's easy to port to Actionscript and works fine.

                  I like Flex!!! 8D
                  • 6. Re: Absolute coordinates of Component inside a Grid Parent
                    cristian.rotundu
                    Hello
                    I have this problem using the global coordinates.
                    I have a component (component X) that's nested into another, and the latter as well into another one.
                    i generate these "component X" so i can have one or three or how many i want on the same level. I generate them at runtime.
                    Everything works perfect so far, but when i generate a "component X" when the main app is in scroll mode, the global coordinates transformation seems to not work anymore. From what I noticed the (0,0) is not the Application's upper left corner, but the visible screen upper left corner at that given moment. This is really messing my app, because if i scroll back to the top, the coords system moves as well and elements are literally "all over the place".
                    It makes no sense.
                    What's really going on with the global coordinates system when the main app scrolls? Can i truly have one fixed reference point?

                    Cheers!
                    Cristian