5 Replies Latest reply on Sep 24, 2008 2:56 PM by Newsgroup_User

    ItemRenderer

    SiHoop Level 1
      I'm just starting to understand how I can use different item renderers to add functionality to a datagrid. I can use the code below to a add colored circle to a cell, but what I want to do next is to add an animated object (i.e. sprite/ movie clip) to the datagrid. I will be using Tweener to animate the object (I don't know whether that makes a difference), but I cannot figure out which class to extend to get the level of interactivity I need in the object. I can add a button, but I don't think I'm going to be able to animate a button satisfactorily.
      Is it possible to extend a class to add a sprite or movie clip to a datagrid?
        • 1. Re: ItemRenderer
          Level 7

          "SiHoop" <webforumsuser@macromedia.com> wrote in message
          news:gbdi3u$n21$1@forums.macromedia.com...
          > I'm just starting to understand how I can use different item renderers to
          > add
          > functionality to a datagrid. I can use the code below to a add colored
          > circle
          > to a cell, but what I want to do next is to add an animated object (i.e.
          > sprite/ movie clip) to the datagrid. I will be using Tweener to animate
          > the
          > object (I don't know whether that makes a difference), but I cannot figure
          > out
          > which class to extend to get the level of interactivity I need in the
          > object. I
          > can add a button, but I don't think I'm going to be able to animate a
          > button
          > satisfactorily.
          > Is it possible to extend a class to add a sprite or movie clip to a
          > datagrid?

          In the example you posted, I'd just extend Canvas:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Canvas paddingLeft="20" xmlns:mx=" http://www.adobe.com/2006/mxml"
          width="50" >
          <mx:Script>
          <![CDATA[

          private var _data:Object;
          private var _dataChanged:Boolean;

          override public function set data(value:Object):void{
          //if the value has changed, set a flag so the object
          //gets drawn in the next pass at the display list
          _dataChanged=true;
          //don't forget to call the super, it does a lot for you!
          super.data=value;
          _data=value;
          }
          override public function get data():Object{
          return _data;
          }
          override protected function updateDisplayList(unscaledWidth:Number,
          unscaledHeight:Number):void{
          super.updateDisplayList(unscaledWidth, unscaledHeight);
          if (_dataChanged) {
          var myColor:uint=uint(_data.userColors);
          graphics.drawRoundRect(0, 8, 10, 10, 10, myColor,100);
          }
          }
          ]]>
          </mx:Script>
          </mx:Canvas>

          But to answer your question, inside your class you can add anything you
          could add to a normal Canvas, including, for example, a swfLoader. Just
          keep in mind that Canvas is a really heavy class, so you might want to learn
          to use UIComponent as the base.


          • 2. Re: ItemRenderer
            SiHoop Level 1
            Thank you, this example is extremely helpful. I'd like to ask a few follow up questions and will keep them as simple as possible
            1. When you say that Canvas is a really heavy class, do you mean that it increases the size of the resulting swf because it includes many properties and methods that must be transported to Flash player?
            2. You use graphics to draw the objects. I want the graphics to respond to mouse events so can I write an event handler for this, or do I need to load the graphics into a sprite and write an event handler for the sprite (or something like that)?
            3. You used updateDisplayList and a couple of times used calls to the super and noted that they do a lot of work-- I've played with this and found that I do not HAVE to use them (i.e. it still works without including super). I have not seen updateDisplayList before-- is this something I should learn about and use?

            Many thanks for your time-- I can't tell you what an eye-opener this has been for me.
            • 3. Re: ItemRenderer
              Level 7

              "SiHoop" <webforumsuser@macromedia.com> wrote in message
              news:gbe5l5$hg2$1@forums.macromedia.com...
              > Thank you, this example is extremely helpful. I'd like to ask a few follow
              > up
              > questions and will keep them as simple as possible
              > 1. When you say that Canvas is a really heavy class, do you mean that it
              > increases the size of the resulting swf because it includes many
              > properties and
              > methods that must be transported to Flash player?

              In part, yes. But the Cavass control is also running checks, for instance,
              to see if it needs scrollbars, has constraints, and what have you. All of
              these take CPU time and will increase the amount of time it takes your List
              to render. If you have a lot of records showing on screen, this can be
              signifcant.

              > 2. You use graphics to draw the objects. I want the graphics to respond to
              > mouse events so can I write an event handler for this, or do I need to
              > load the
              > graphics into a sprite and write an event handler for the sprite (or
              > something
              > like that)?

              I don't think UIComponent will accept mouse events to transparent areas.
              Conversely, I believe that anything that is on the display list (even
              objects drawn directly by the graphics functions) will react to mouse
              events. The difference is that you won't be able to tell different objects
              drawn by the graphics functions apart, so if this is something you need, by
              all means use Sprites or another UIComponent.

              > 3. You used updateDisplayList and a couple of times used calls to the
              > super
              > and noted that they do a lot of work-- I've played with this and found
              > that I
              > do not HAVE to use them (i.e. it still works without including super).

              The super for data makes sure that the collection is notified of any changes
              in the data that you make from the renderer. You're right, in your case you
              probably don't need to use it. But if you get in the habit of _not_ using
              it and then you run into problems with it, you probably will have a hard
              time tracking down why. So just get in the habit of using it :-).

              > I have
              > not seen updateDisplayList before-- is this something I should learn about
              > and
              > use?

              Yes!!!

              The reason itemRenderers are such a pain in the butt is because the
              engineers decided to choose performance over ease of use. So, in essence,
              the component life cycle "schedules" different tasks to happen in the most
              efficient order. What this means is that if you do things at the wrong
              time, best case is it will still work, but it will slow down the performance
              of the renderer, and if there are lots of renderers this may become
              noticeable. Worst case is that wonky things will happen that are hard to
              explain. So you should learn when the right time is for each thing you need
              to do. updateDisplayList is the right time to set sizes and colors.

              HTH;

              Amy


              • 4. Re: ItemRenderer
                SiHoop Level 1
                Amy,
                Thanks so much for your detailed reply. I know that my knowledge of how Flex works has grown tremendously as a result.
                Simon
                • 5. Re: ItemRenderer
                  Level 7

                  "SiHoop" <webforumsuser@macromedia.com> wrote in message
                  news:gbecsc$p71$1@forums.macromedia.com...
                  > Amy,
                  > Thanks so much for your detailed reply. I know that my knowledge of how
                  > Flex works has grown tremendously as a result.
                  > Simon

                  You're welcome.

                  -Amy