//
// $Id: DirtyItemList.java,v 1.1 2001/10/13 01:08:59 shaper Exp $

package com.threerings.media.sprite;

import java.util.*;

import com.threerings.media.sprite.Sprite;
import com.threerings.media.tile.ObjectTile;

import com.threerings.media.Log;

/**
 * The dirty item list keeps track of dirty sprites and object tiles
 * in a scene.  Since scenes can only ever have one of either item at
 * any given coordinate, dirty items are stored and checked for
 * equality based solely on their coordinate, and each coordinate can
 * exist in the list only once.
 */
public class DirtyItemList extends ArrayList
{
    /**
     * Append the dirty item at the given coordinates to the dirty
     * item list.
     */
    public boolean appendDirtyItem (Object item, int x, int y)
    {
        // only allow appending sprites and object tiles
        if (!(item instanceof Sprite) && !(item instanceof ObjectTile)) {
            return false;
        }

        // only add the item if there are no existing items
        if (!contains(x, y)) {
            add(new DirtyItem(item, x, y));
            return true;
        }

        return false;
    }

    /**
     * Sort the items in the list using the given comparator.
     */
    public void sort (Comparator comp)
    {
        Object[] items = new Object[size()];
        toArray(items);
        Arrays.sort(items, comp);
        clear();
        for (int ii = 0; ii < items.length; ii++) {
            add(items[ii]);
        }
    }

    /**
     * Returns whether the list contains a dirty item at the given
     * coordinates.
     */
    protected boolean contains (int x, int y)
    {
        int size = size();
        for (int ii = 0; ii < size; ii++) {
            Object o = get(ii);
            if (o instanceof DirtyItem) {
                DirtyItem di = (DirtyItem)o;
                return (x == di.x && y == di.y);
            }
        }

        return false;
    }

    /**
     * A wrapper class to hold the items inserted in the dirty list
     * along with their coordinates in the scene.
     */
    public class DirtyItem
    {
        public Object obj;
        public int x, y;

        public DirtyItem (Object obj, int x, int y)
        {
            this.obj = obj;
            this.x = x;
            this.y = y;
        }
    }
}

@@ -27,17 +27,17 @@
 ava,v 1.
-1
+2
  2001/10
@@ -42,18 +42,18 @@
 10/1
-3 01:08:59
+7 22:13:53
  sha
@@ -97,24 +97,75 @@
 ia.sprite;%0A%0A
+import java.awt.Graphics2D;%0Aimport java.awt.Shape;%0A
 import java.
@@ -677,16 +677,17 @@
 * Append
+s
  the dir
@@ -685,28 +685,30 @@
 s the dirty 
+spr
 ite
-m
  at the give
@@ -742,24 +742,126 @@
      * item 
+list if no item already exists at those coordinates.%0A     * Returns whether the item was added to the 
 list.%0A     *
@@ -896,25 +896,29 @@
 irty
-I
+Spri
 te
-m
  (
-Object 
+Sprite spr
 ite
-m
 , in
@@ -947,135 +947,119 @@
     
-// only allow appending sprites and object tiles%0A        if (!(item instanceof Sprite) && !(item instanceof ObjectTile)) %7B%0A    
+if (!contains(x, y)) %7B%0A            add(new DirtyItem(sprite, null, x, y));%0A            return true;%0A        %7D%0A%0A
     
@@ -1084,74 +1084,319 @@
     
+%7D%0A%0A
     
-%7D%0A
+/**
 %0A     
-   // only add the item if there are no existing items
+* Appends the dirty object tile at the given coordinates to the%0A     * dirty item list if no item already exists at those coordinates.%0A     * Returns whether the item was added to the list.%0A     */%0A    public boolean appendDirtyObject (%0A        ObjectTile tile, Shape bounds, int x, int y)%0A    %7B
 %0A   
@@ -1453,20 +1453,28 @@
 rtyItem(
-item
+tile, bounds
 , x, y))
@@ -1502,33 +1502,32 @@
 true;%0A        %7D%0A
-%0A
         return f
@@ -2517,24 +2517,53 @@
 Object obj;%0A
+        public Shape bounds;%0A
         publ
@@ -2613,16 +2613,30 @@
 ect obj,
+ Shape bounds,
  int x, 
@@ -2701,38 +2701,309 @@
 his.
-x = x;%0A            this.y = y;
+bounds = bounds;%0A            this.x = x;%0A            this.y = y;%0A        %7D%0A%0A        public void paint (Graphics2D gfx)%0A        %7B%0A            if (obj instanceof Sprite) %7B%0A                ((Sprite)obj).paint(gfx);%0A%0A            %7D else %7B%0A                ((ObjectTile)obj).paint(gfx, bounds);%0A            %7D
 %0A   

Add sprites and objects to the dirty item list separately since the latter have a bounds associated with them. Dirty items can now paint themselves to simplify scene-specific code.