From cd51c78619e044fc58c57f26e0de21046fc9e6e0 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 11 Nov 2004 23:52:43 +0000 Subject: [PATCH] Added support for using sprites that are fixed in relation to a potentially scrolling view. Also added a means for non-sprites and animations to hear about view scrolls. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3209 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/media/AbstractMedia.java | 44 +++++++++++++------ .../media/AbstractMediaManager.java | 17 ++++++- .../com/threerings/media/ViewTracker.java | 18 ++++++++ .../threerings/media/VirtualMediaPanel.java | 30 ++++++++++++- 4 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 src/java/com/threerings/media/ViewTracker.java diff --git a/src/java/com/threerings/media/AbstractMedia.java b/src/java/com/threerings/media/AbstractMedia.java index d04f80d9e..00f321cf9 100644 --- a/src/java/com/threerings/media/AbstractMedia.java +++ b/src/java/com/threerings/media/AbstractMedia.java @@ -1,5 +1,5 @@ // -// $Id: AbstractMedia.java,v 1.12 2004/08/27 02:12:37 mdb Exp $ +// $Id: AbstractMedia.java,v 1.13 2004/11/11 23:52:43 mdb Exp $ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -39,6 +39,11 @@ import com.samskivert.util.StringUtil; public abstract class AbstractMedia implements Shape { + /** A {@link #_renderOrder} value at or above which, indicates that this + * media is in the HUD (heads up display) and should not scroll when the + * view scrolls. */ + public static final int HUD_LAYER = 65536; + /** * Instantiate an abstract media object. */ @@ -173,6 +178,8 @@ public abstract class AbstractMedia * appropriate manager and must not change while the manager is * managing it. If you wish to change the render order, remove the * media from the manager, change the order and add it back again. + * + * @see #HUD_LAYER */ public void setRenderOrder (int renderOrder) { @@ -203,6 +210,29 @@ public abstract class AbstractMedia } } + /** + * Called by the {@link AbstractMediaManager} when we are in a + * {@link VirtualMediaPanel} that just scrolled. + */ + public void viewLocationDidChange (int dx, int dy) + { + if (_renderOrder >= HUD_LAYER) { + setLocation(_bounds.x + dx, _bounds.y + dy); + } + } + + /** + * Dumps this media to a String object. + */ + public String toString () + { + StringBuffer buf = new StringBuffer(); + buf.append(StringUtil.shortClassName(this)); + buf.append("["); + toString(buf); + return buf.append("]").toString(); + } + /** * Initialize the media. */ @@ -269,18 +299,6 @@ public abstract class AbstractMedia } } - /** - * Dumps this media to a String object. - */ - public String toString () - { - StringBuffer buf = new StringBuffer(); - buf.append(StringUtil.shortClassName(this)); - buf.append("["); - toString(buf); - return buf.append("]").toString(); - } - /** * This should be overridden by derived classes (which should be sure * to call super.toString()) to append the derived class diff --git a/src/java/com/threerings/media/AbstractMediaManager.java b/src/java/com/threerings/media/AbstractMediaManager.java index 4569c845c..7b715c817 100644 --- a/src/java/com/threerings/media/AbstractMediaManager.java +++ b/src/java/com/threerings/media/AbstractMediaManager.java @@ -1,5 +1,5 @@ // -// $Id: AbstractMediaManager.java,v 1.13 2004/08/27 02:12:37 mdb Exp $ +// $Id: AbstractMediaManager.java,v 1.14 2004/11/11 23:52:43 mdb Exp $ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -139,6 +139,21 @@ public abstract class AbstractMediaManager return _media.contains(media); } + /** + * Called by a {@link VirtualMediaPanel} when the view that contains our + * media is scrolled. + * + * @param dx the scrolled distance in the x direction (in pixels). + * @param dy the scrolled distance in the y direction (in pixels). + */ + public void viewLocationDidChange (int dx, int dy) + { + // let our media know + for (int ii = 0, ll = _media.size(); ii < ll; ii++) { + ((AbstractMedia)_media.get(ii)).viewLocationDidChange(dx, dy); + } + } + /** * Calls {@link AbstractMedia#tick} on all media to give them a chance * to move about, change their look, generate dirty regions, and so diff --git a/src/java/com/threerings/media/ViewTracker.java b/src/java/com/threerings/media/ViewTracker.java new file mode 100644 index 000000000..4c63c2bdf --- /dev/null +++ b/src/java/com/threerings/media/ViewTracker.java @@ -0,0 +1,18 @@ +// +// $Id: ViewTracker.java,v 1.1 2004/11/11 23:52:43 mdb Exp $ + +package com.threerings.media; + +/** + * An interface used by entities that wish to respond to the scrolling of a + * {@link VirtualMediaPanel}. + * + * @see VirtualMediaPanel#addViewTracker + */ +public interface ViewTracker +{ + /** + * Called by a {@link VirtualMediaPanel} when it scrolls. + */ + public void viewLocationDidChange (int dx, int dy); +} diff --git a/src/java/com/threerings/media/VirtualMediaPanel.java b/src/java/com/threerings/media/VirtualMediaPanel.java index 66f272955..de888914b 100644 --- a/src/java/com/threerings/media/VirtualMediaPanel.java +++ b/src/java/com/threerings/media/VirtualMediaPanel.java @@ -1,5 +1,5 @@ // -// $Id: VirtualMediaPanel.java,v 1.28 2004/10/29 19:33:16 mdb Exp $ +// $Id: VirtualMediaPanel.java,v 1.29 2004/11/11 23:52:43 mdb Exp $ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -25,6 +25,7 @@ import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.MouseEvent; +import java.util.ArrayList; import javax.swing.SwingUtilities; @@ -101,6 +102,21 @@ public class VirtualMediaPanel extends MediaPanel return _vbounds; } + /** + * Adds an entity that will be informed when the view scrolls. + */ + public void addViewTracker (ViewTracker tracker) + { + _trackers.add(tracker); + } + + /** + * Removes an entity from the view trackers list. + */ + public void removeViewTracker (ViewTracker tracker) + { + _trackers.remove(tracker); + } /** * Instructs the view to follow the supplied pathable; ensuring that * the view's coordinates are adjusted according to the follow mode. @@ -256,6 +272,15 @@ public class VirtualMediaPanel extends MediaPanel sdirty.translate(-dx, -dy); dirtyScreenRect(sdirty); } + + // inform our view trackers + for (int ii = 0, ll = _trackers.size(); ii < ll; ii++) { + ((ViewTracker)_trackers.get(ii)).viewLocationDidChange(dx, dy); + } + + // let our sprites and animations know what's up + _animmgr.viewLocationDidChange(dx, dy); + _spritemgr.viewLocationDidChange(dx, dy); } /** @@ -417,4 +442,7 @@ public class VirtualMediaPanel extends MediaPanel /** We need to know our absolute coordinates in order to work around * the Windows copyArea() bug. */ protected Rectangle _abounds = new Rectangle(); + + /** A list of entities to be informed when the view scrolls. */ + protected ArrayList _trackers = new ArrayList(); }