A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [RESOLVED] Extending classes

  1. #1
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124

    resolved [RESOLVED] Extending classes

    How do I go about doing this in general?

    I want to extend the List class to change just one function. how do I do it? Thanks.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Create a class which extends List. Write code for your new function in that class. If it's entirely new functionality, you're done. If it's changing existing functionality, you'll need to use the override keyword.

    What specifically are you having a problem with?

  3. #3
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Basically I want to take the drawList function inside the List.as class that comes with flash, and in my class extending it I want to rewrite it.

    Below is what I have in my class file. How would I implement it to overwrite the current function within the list class? And am I going about this correctly?

    Thanks.
    Code:
    package com.foldersource.newclasses{
    	import fl.controls.List;
    	public class MyListClass extends List {
    		public function MyListClass() {
    			super();
    		}
    		override protected  function drawList():void {
    			// List is very environmentally friendly, it reuses existing 
    			// renderers for old data, and recycles old renderers for new data.
    
    			// set horizontal scroll:
    			listHolder.x=listHolder.y=contentPadding;
    
    			var rect:Rectangle=listHolder.scrollRect;
    			rect.x=_horizontalScrollPosition;
    
    			// set pixel scroll:
    			rect.y=Math.floor(_verticalScrollPosition) % rowHeight;
    			listHolder.scrollRect=rect;
    
    			listHolder.cacheAsBitmap=useBitmapScrolling;
    
    			// figure out what we have to render:
    			var startIndex:uint=Math.floor(_verticalScrollPosition / rowHeight);
    			var endIndex:uint=Math.min(length,startIndex + rowCount + 25);
    
    
    			// these vars get reused in different loops:
    			var i:uint;
    			var item:Object;
    			var renderer:ICellRenderer;
    
    			// create a dictionary for looking up the new "displayed" items:
    			var itemHash:Dictionary=renderedItems=new Dictionary(true);
    			for (i=startIndex; i < endIndex; i++) {
    				itemHash[_dataProvider.getItemAt(i)]=true;
    			}
    			// find cell renderers that are still active, and make those that aren't active available:
    			var itemToRendererHash:Dictionary=new Dictionary(true);
    			while (activeCellRenderers.length > 0) {
    				renderer=activeCellRenderers.pop()  as  ICellRenderer;
    				item=renderer.data;
    				if (itemHash[item] == null || invalidItems[item] == true) {
    					availableCellRenderers.push(renderer);
    				} else {
    					itemToRendererHash[item]=renderer;
    					// prevent problems with duplicate objects:
    					invalidItems[item]=true;
    				}
    				list.removeChild(renderer  as  DisplayObject);
    			}
    			invalidItems=new Dictionary(true);
    
    			// draw cell renderers:
    			for (i=startIndex; i < endIndex; i++) {
    				var reused:Boolean=false;
    				item=_dataProvider.getItemAt(i);
    				if (itemToRendererHash[item] != null) {
    					// existing renderer for this item we can reuse:
    
    					reused=true;
    					renderer=itemToRendererHash[item];
    					delete itemToRendererHash[item];
    				} else if (availableCellRenderers.length > 0) {
    
    					// recycle an old renderer:
    					renderer=availableCellRenderers.pop()  as  ICellRenderer;
    				} else {
    
    					// out of renderers, create a new one:
    					renderer=getDisplayObjectInstance(getStyleValue("cellRenderer"))  as  ICellRenderer;
    					var rendererSprite:Sprite=renderer  as  Sprite;
    					if (rendererSprite != null) {
    						rendererSprite.addEventListener(MouseEvent.CLICK,handleCellRendererClick,false,0,true);
    						rendererSprite.addEventListener(MouseEvent.ROLL_OVER,handleCellRendererMouseEvent,false,0,true);
    						rendererSprite.addEventListener(MouseEvent.ROLL_OUT,handleCellRendererMouseEvent,false,0,true);
    						rendererSprite.addEventListener(Event.CHANGE,handleCellRendererChange,false,0,true);
    						rendererSprite.doubleClickEnabled=true;
    						rendererSprite.addEventListener(MouseEvent.DOUBLE_CLICK,handleCellRendererDoubleClick,false,0,true);
    
    						if (rendererSprite["setStyle"] != null) {
    							for (var n:String in rendererStyles) {
    								rendererSprite["setStyle"](n,rendererStyles[n]);
    							}
    						}
    					}
    				}
    				list.addChild(renderer  as  Sprite);
    				activeCellRenderers.push(renderer);
    
    				renderer.y=rowHeight * i - startIndex;
    				renderer.setSize(availableWidth + _maxHorizontalScrollPosition,rowHeight);
    
    				var label:String=itemToLabel(item);
    
    				var icon:Object=null;
    				if (_iconFunction != null) {
    					icon=_iconFunction(item);
    				} else if (_iconField != null) {
    					icon=item[_iconField];
    				}
    				if (! reused) {
    					renderer.data=item;
    				}
    				renderer.listData=new ListData(label,icon,this,i,i,0);
    				renderer.selected=_selectedIndices.indexOf(i) != -1;
    
    				// force an immediate draw (because render event will not be called on the renderer):
    				if (renderer is UIComponent) {
    					renderer  as  UIComponent.drawNow();
    				}
    			}
    		}
    	}
    }

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Something very much like that, yes. I'm not sure whether it matters if you use "protected override" vs "override protected".

    I don't see any reference to the new indexes property that you added to MyListData in the other thread, so I'm not sure what you're trying to accomplish with your new drawList method. To be honest, I just skimmed through that rather than trying to understand what it was doing. It looks different than your other code, so I'm guessing you copied it from somewhere?

  5. #5
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Yeah the mylistdata was an experiement that I later found out was a flop, but the list extension is my new solution as I know it will work, as I edited the original class and it worked, but the thing is I need to place my end files on a server, and I can't edit everyones class file obviously, LOL.

    So what I did was open up the List.as file and copied the drawlist function and pasted it into the new class. Then I just changed a couple values around to fit what I need.

    So how would I implement this?

    Thanks for all your help?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Looks like you already have implemented it. Just use MyListClass instead of List wherever you want your new behavior. Since any instance of MyListClass IS a List, you can use it anywhere you would use a List.

  7. #7
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Okay, this is what I've got:

    I have a file with a combo box in it, and inside that combobox is of course the list component which inherits the list class automatically. So would I just import the class and then define a new variable? Or would I just have to import the class at the top of the document?

    I.E. =

    import com.holderfolder.newclasses.MyListClass;

    or

    import com.holderfolder.newclasses.MyListClass;

    var listvarnew:MyListClass = new MyListClass();

    Thanks.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm not familiar with the combobox class. Looking through the livedocs, it seems that it has a dropdown property which is unfortunately read only. To get a combobox whose list is an instance of your new list class, you'd have to extend Combobox as well, and minimally provide a method to set the internal dropdown list variable (which you could then use to give it an instance of your new List type). There's probably a reason it was made private in the first place, so proceed with caution.

  9. #9
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Okay, thanks, I rappreciate the help, and will update when I come to a conclusion.

  10. #10
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    SUCCESS!!!!!!!


    I fixed my problem:

    Basically I had to create new class for the combobox, a new class for the list component, and then imported the list class into the combobox class, and everwhere the combobox class called for the List class, I called for the MyListClass, and then replaced all the combobox calls with MyComboBox int he main class application.

    If anyone reads this and needs an example, believe me I understand and have been through the headaches, but in doing so I've gained quite a knowledge of the combobox, list, listdata, and cellrenderer class now, and have implemented them all. Should you need an example, post a request here, or email me at: [email protected] and I'll be more than happy to send you an example and help in any way that I can.

    Thanks again for all your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center