Ignore:
Timestamp:
07/03/08 16:48:09 (16 years ago)
Author:
bjn8
Message:

Added invalidation for graphics... biiiig commit. LOts of effeciency improvements - now can animate

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/Item.java

    r116 r121  
    55import java.awt.Cursor;
    66import java.awt.Dimension;
     7import java.awt.GradientPaint;
    78import java.awt.Graphics2D;
    89import java.awt.Point;
    910import java.awt.Polygon;
    1011import java.awt.Rectangle;
     12import java.awt.Shape;
    1113import java.awt.Stroke;
    1214import java.awt.geom.AffineTransform;
     
    2931import org.expeditee.gui.FrameIO;
    3032import org.expeditee.gui.FrameUtils;
     33import org.expeditee.gui.MessageBay;
    3134import org.expeditee.gui.Overlay;
    3235import org.expeditee.gui.Vector;
     
    185188                if (parent != null) {
    186189                        dest.setID(source.getParentOrCurrentFrame().getNextItemID());
    187                 }
     190                }
     191//              else {
     192//                      dest.setID(source.getID());
     193//              }
    188194        }
    189195
     
    254260        private Color _color = null;
    255261
    256         private Color _highlightColor = DEFAULT_HIGHLIGHT;
     262        protected Color _highlightColor = DEFAULT_HIGHLIGHT;
    257263
    258264        private Color _colorBackground = null;
     
    525531        }
    526532
    527         protected Polygon getCircle() {
     533        protected Polygon getLinkPoly() {
    528534                if (_circle == null) {
    529535                        int points = 16;
     
    552558                        _circleCross = new Polygon();
    553559
    554                         Rectangle bounds = getCircle().getBounds();
     560                        Rectangle bounds = getLinkPoly().getBounds();
    555561                        int x1 = (int) bounds.getMinX();
    556562                        int x2 = (int) bounds.getMaxX();
     
    943949                        g.setColor(fillColor);
    944950                        // The painting is not efficient enough for gradients...
    945                         // Shape s = getEnclosedShape();
    946                         // Rectangle b = s.getBounds();
    947                         // GradientPaint gp = new GradientPaint((int) (b.x + b.width * 0.3),
    948                         // b.y, getFillColor(), (int) (b.x + b.width * 1.3), b.y,
    949                         // Color.white);
    950                         // g.setPaint(gp);
    951                         // g.fill(s);
     951//                       Polygon s = getEnclosedShape();
     952//                      Rectangle b = s.getBounds();
     953//                      GradientPaint gp = new GradientPaint((int) (b.x + b.width * 0.3),
     954//                      b.y, getFillColor(), (int) (b.x + b.width * 1.3), b.y,
     955//                      Color.white);
     956//                      g.setPaint(gp);
     957//                       g.fillPolygon(s);
    952958                        g.fillPolygon(getEnclosedShape());
    953959                }
     
    9941000         */
    9951001        public void removeAllLines() {
     1002                for (Line l : _lines) {
     1003                        l.invalidateAll();
     1004                }
    9961005                _lines.clear();
    9971006        }
     
    10161025         */
    10171026        public void removeLine(Line line) {
    1018                 _lines.remove(line);
     1027                if (_lines.remove(line))
     1028                        line.invalidateAll();
    10191029        }
    10201030
     
    10241034                        Simple.RunFrameAndReportError(this, new Context());
    10251035                        Simple.ProgramFinished();
    1026                         FrameGraphics.DisplayMessage(AgentStats.getStats(), GREEN);
     1036                        MessageBay.displayMessage(AgentStats.getStats(), GREEN);
    10271037                } catch (ConcurrentModificationException ce) {
    10281038                        Simple.ProgramFinished();
    10291039                        ce.printStackTrace();
    10301040                } catch (Exception e) {
    1031                         FrameGraphics.LinkedErrorMessage(e.getMessage());
     1041                        MessageBay.linkedErrorMessage(e.getMessage());
    10321042                        Simple.ProgramFinished();
    10331043                }
     
    11421152
    11431153        public void setBackgroundColor(Color c) {
    1144                 _colorBackground = c;
     1154                if (c != _colorBackground) {
     1155                        _colorBackground = c;
     1156                        invalidateCommonTrait(ItemAppearence.BackgroundColorChanged);
     1157                }
    11451158        }
    11461159
     
    11631176         */
    11641177        public void setColor(Color c) {
    1165                 _color = c;
     1178                if (c != _color) {
     1179                        _color = c;
     1180                        invalidateCommonTrait(ItemAppearence.ForegroundColorChanged);
     1181                }
    11661182        }
    11671183
     
    11911207
    11921208        public void setFillColor(Color c) {
     1209
    11931210                _colorFill = c;
    11941211
     
    11981215                                other.setFillColor(c);
    11991216                }
     1217               
     1218                invalidateCommonTrait(ItemAppearence.FillColor);
     1219                invalidateFill();
    12001220        }
    12011221
    12021222        public void setFillPattern(String patternLink) {
    12031223                _fillPattern = patternLink;
     1224                invalidateCommonTrait(ItemAppearence.FillPattern);
     1225                invalidateFill();
    12041226        }
    12051227
     
    12441266                for (Line line : lines)
    12451267                        line.setLinePattern(getLinePattern());
     1268               
     1269               
    12461270        }
    12471271
     
    12541278         */
    12551279        public void setLink(String frameName) {
    1256                 if (FrameIO.isValidLink(frameName))
     1280                if (FrameIO.isValidLink(frameName)) 
    12571281                        _link = frameName;
    12581282                else
    1259                         FrameGraphics.ErrorMessage("[" + frameName
     1283                        MessageBay.errorMessage("[" + frameName
    12601284                                        + "] is not a valid frame name");
    12611285                // TODO make this throw exceptions etc...
     1286               
     1287                invalidate(getLinkDrawArea());
    12621288        }
    12631289
     
    12661292                        _link_frameset = frameset;
    12671293                else
    1268                         FrameGraphics.ErrorMessage("[" + frameset
     1294                        MessageBay.errorMessage("[" + frameset
    12691295                                        + "] is not a valid frameset name");
    12701296                // TODO make this throw exceptions etc...
     
    12801306                        _link_template = template;
    12811307                else
    1282                         FrameGraphics.ErrorMessage("[" + template
     1308                        MessageBay.errorMessage("[" + template
    12831309                                        + "] is not a valid frame name");
    12841310                // TODO make this throw exceptions etc...
     
    13161342
    13171343        /**
     1344         * Invalidates this, connected lines and fill
     1345         * @param trait
     1346         */
     1347        private void invalidateCommonTraitForAll(ItemAppearence trait) {
     1348                invalidateCommonTrait(trait);
     1349                for (Line line : getLines())
     1350                        line.invalidateCommonTrait(trait);
     1351                if (_colorFill != null) {
     1352                        invalidateFill(); // only invalidates if has fill
     1353                }
     1354                for(XRayable x: getEnclosures()){
     1355                        x.invalidateCommonTrait(trait);
     1356                }
     1357
     1358        }
     1359       
     1360       
     1361        /**
    13181362         * Sets the position of this item on the screen
    13191363         *
     
    13261370                float deltaX = x - _x;
    13271371                float deltaY = y - _y;
     1372               
     1373                if (deltaX == 0 && deltaY == 0) return;
     1374               
     1375                invalidateCommonTraitForAll(ItemAppearence.PreMoved);
    13281376
    13291377                _x = x;
     
    14131461                }
    14141462
    1415                 for (Line line : getLines())
    1416                         line.updatePolygon();
     1463                for (Line line : getLines()) {
     1464                        line.updatePolygon();   
     1465                }
     1466               
     1467                invalidateCommonTraitForAll(ItemAppearence.PostMoved);
     1468               
    14171469        }
    14181470
     
    14631515         */
    14641516        public void setThickness(float thick, boolean setConnectedThickness) {
     1517               
     1518                if (thick == _thickness) return;
     1519                boolean bigger = thick > _thickness;
     1520               
     1521                if (!bigger) invalidateCommonTrait(ItemAppearence.Thickness);
     1522               
    14651523                _thickness = thick;
    14661524                // update the size of any lines
    14671525                for (Line line : getLines())
    14681526                        line.setThickness(thick, setConnectedThickness);
     1527
     1528                if (bigger) invalidateCommonTrait(ItemAppearence.Thickness);
    14691529        }
    14701530
     
    15341594
    15351595        public int setHighlightColor(Color c) {
     1596               
    15361597                _highlightThickness = DEFAULT_HIGHLIGHT_THICKNESS;
    1537                 if (c != null)
    1538                         _highlightColor = c;
    1539                 else
    1540                         _highlightColor = DEFAULT_HIGHLIGHT;
     1598               
     1599                Color selColor = (c != null) ? c : DEFAULT_HIGHLIGHT;
     1600                if (_highlightColor != c) {
     1601                        _highlightColor = selColor;
     1602                        this.invalidateCommonTrait(ItemAppearence.HighlightColorChanged);
     1603                }
    15411604
    15421605                return Item.UNCHANGED_CURSOR;
     1606               
    15431607        }
    15441608
     
    15891653        public void setHighlightMode(HighlightMode mode, Color color) {
    15901654                setHighlightColor(color);
    1591                 _mode = mode;
     1655                if (_mode != mode) {
     1656                        _mode = mode;
     1657                        this.invalidateCommonTrait(ItemAppearence.HighlightModeChanged);
     1658                }
    15921659        }
    15931660
     
    16551722         */
    16561723        public void setEnclosedList(Collection<Item> enclosed) {
     1724               
     1725                boolean changed = (_enclosure == null && enclosed != null);
     1726               
     1727                if (_enclosure != null && enclosed == null) {
     1728                        invalidateFill();
     1729                }
     1730               
    16571731                _enclosure = enclosed;
     1732               
     1733                if (changed) {
     1734                        invalidateFill();;
     1735                }
    16581736        }
    16591737
     
    17731851                if (_actions == null || _actions.size() == 0) {
    17741852                        _poly = null;
     1853                        invalidateCommonTrait(ItemAppearence.LinkChanged);
    17751854                }
    17761855                _actions = new LinkedList<String>();
     
    17801859        protected int getLinkYOffset() {
    17811860                return 0;
     1861        }
     1862       
     1863        protected Rectangle getLinkDrawArea() { // TODO: Revise
     1864                return new Rectangle(getX() - LEFT_MARGIN - 3, getY() + getLinkYOffset() - 3, 6, 6);
    17821865        }
    17831866
     
    18041887
    18051888                        if (getLinkMark() && getLink() != null) {
    1806                                 g.drawPolygon(getCircle());
     1889                                g.drawPolygon(getLinkPoly());
    18071890
    18081891                                // if the link is not valid, cross out the circle
     
    18121895
    18131896                        if (getActionMark() && getAction() != null) {
    1814                                 g.drawPolygon(getCircle());
    1815                                 g.fillPolygon(getCircle());
     1897                                g.drawPolygon(getLinkPoly());
     1898                                g.fillPolygon(getLinkPoly());
    18161899
    18171900                                // if the link is not valid, cross out the circle
     
    19732056         */
    19742057        public void translate(Point2D origin, double ratio) {
     2058               
     2059                invalidateCommonTraitForAll(ItemAppearence.PreMoved);
     2060               
    19752061                _x = (float) (origin.getX() + ratio * (_x - origin.getX()));
    19762062                _y = (float) (origin.getY() + ratio * (_y - origin.getY()));
     
    19782064                for (Line line : getLines())
    19792065                        line.updatePolygon();
     2066               
     2067                invalidateCommonTraitForAll(ItemAppearence.PostMoved);
     2068               
    19802069        }
    19812070
     
    20052094                                        setLinePattern(new int[] { LinePatterns[i], LinePatterns[i] });
    20062095                                }
     2096                               
     2097                                invalidateCommonTrait(ItemAppearence.ToggleDashed);
    20072098                                return;
    20082099                        }
    20092100                }
     2101               
     2102               
    20102103        }
    20112104
     
    20372130        public boolean isDeleted() {
    20382131                return _deleted;
     2132        }
     2133       
     2134        /**
     2135         * @return The full canvas that this item draws to. Must include highlighting bounds
     2136         */
     2137        public Rectangle[] getDrawingArea() {
     2138               
     2139                return new Rectangle[] {
     2140                                ItemUtils.expandRectangle(getPolygon().getBounds(),
     2141                                _highlightThickness)
     2142                        };
     2143               
     2144        }
     2145       
     2146        /**
     2147         *
     2148         * @param area
     2149         * @return True if area intersects with this items drawing area.
     2150         */
     2151        public final boolean isInDrawingArea(Area area) {
     2152                for (Rectangle r : getDrawingArea()) {
     2153                        if (area.intersects(r)) return true;
     2154                }
     2155                return false;
     2156        }
     2157       
     2158        /**
     2159         * Completetly invalidates the item - so that it should be redrawed.
     2160         * Note: This is handled internally, it should be reare to invoke this
     2161         * externally
     2162         */
     2163        public final void invalidateAll() {
     2164                invalidate(getDrawingArea());
     2165        }
     2166       
     2167        /**
     2168         * Invalidates areas on the parent frame.
     2169         * Purpose: to be called on specific areas of the item that needs redrawing.
     2170         * @param damagedAreas
     2171         */
     2172        protected final void invalidate(Rectangle[] damagedAreas) {
     2173                for (Rectangle r : damagedAreas) invalidate(r);
     2174        }
     2175       
     2176        /**
     2177         * Invalidates areas on the parent frame.
     2178         * Purpose: to be called on specific areas of the item that needs redrawing.
     2179         * @param damagedAreas
     2180         */
     2181        protected final void invalidate(Rectangle damagedArea) {
     2182                FrameGraphics.invalidateItem(this, damagedArea);
     2183        }
     2184       
     2185        /**
     2186         * Used to invalidate visual traits commonly shared by all items.
     2187         * @param trait
     2188         */
     2189        public final void invalidateCommonTrait(ItemAppearence trait) {
     2190                invalidate(getDamagedArea(trait));
     2191               
     2192                if (_colorFill != null && (
     2193                                trait == ItemAppearence.Added
     2194                                || trait == ItemAppearence.Removed)
     2195                                ) {
     2196                        invalidateFill();
     2197                }
     2198        }
     2199       
     2200        /**
     2201         * Invalidates fill if has one, even if no color is set.
     2202         */
     2203        public void invalidateFill() {
     2204                if (isLineEnd() && _enclosure != null) {
     2205                        invalidate(getEnclosedShape().getBounds());
     2206                }
     2207        }
     2208       
     2209        /**
     2210         * Default implementation always uses drawing area except for links,
     2211         * where the link drawing area is used. Override to make item
     2212         * drawing more efficient - defining only parts of the item that needs
     2213         * redrawing.
     2214         *
     2215         * @see Item.getDrawingArea
     2216         *
     2217         * @param trait The visual trait that has changed.
     2218         *
     2219         * @return The damaged area according to the visual trait that has changed.
     2220         */
     2221        protected Rectangle[] getDamagedArea(ItemAppearence trait) {
     2222               
     2223                if (trait == ItemAppearence.LinkChanged)
     2224                        return new Rectangle[] {getLinkDrawArea()}; // Invalidate area where link is drawn
     2225                       
     2226                return getDrawingArea();
     2227
    20392228        }
    20402229
     
    20562245                _overlay = overlay;
    20572246        }
     2247
    20582248}
Note: See TracChangeset for help on using the changeset viewer.