From 049717d5ecbf86ae439704dd0a4a2bede57ba21b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 18 Dec 2001 12:43:12 +0000 Subject: [PATCH] Added mechanism for displaying nodemap centered on a particular node. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@835 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/nodemap/NodeMap.java | 19 +++++++++- .../com/threerings/nodemap/NodeMapPanel.java | 36 +++++++++++++++---- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/java/com/threerings/nodemap/NodeMap.java b/src/java/com/threerings/nodemap/NodeMap.java index 91b6d343b..306948c5c 100644 --- a/src/java/com/threerings/nodemap/NodeMap.java +++ b/src/java/com/threerings/nodemap/NodeMap.java @@ -1,5 +1,5 @@ // -// $Id: NodeMap.java,v 1.5 2001/10/11 00:41:27 shaper Exp $ +// $Id: NodeMap.java,v 1.6 2001/12/18 12:43:12 mdb Exp $ package com.threerings.nodemap; @@ -30,6 +30,14 @@ public class NodeMap _dirty = false; } + /** + * Returns an iterator over the nodes in this node map. + */ + public Iterator nodes () + { + return _nodes.iterator(); + } + /** * Force the nodes in the map to be re-laid out. */ @@ -55,6 +63,15 @@ public class NodeMap _dirty = false; } + /** + * Returns the coordinates of the supplied node, adjusted into + * absolute coordinates (where 0,0 is the upper left of the node map). + */ + public Point getNodeCoords (Node node) + { + return new Point(node.getX()-_minx, node.getY()-_miny); + } + /** * Calculate the bounding rectangle that wholly contains the node * map. diff --git a/src/java/com/threerings/nodemap/NodeMapPanel.java b/src/java/com/threerings/nodemap/NodeMapPanel.java index a2b555956..87047df52 100644 --- a/src/java/com/threerings/nodemap/NodeMapPanel.java +++ b/src/java/com/threerings/nodemap/NodeMapPanel.java @@ -1,5 +1,5 @@ // -// $Id: NodeMapPanel.java,v 1.7 2001/12/18 12:21:21 mdb Exp $ +// $Id: NodeMapPanel.java,v 1.8 2001/12/18 12:43:12 mdb Exp $ package com.threerings.nodemap; @@ -69,16 +69,37 @@ public class NodeMapPanel extends JPanel return _map; } + /** + * Instructs the panel to center the displayed node map on the + * specified node. If the node is null, the panel will revert back to + * centering on the entire map. + */ + public void centerOnNode (Node node) + { + _centerNode = node; + repaint(); + } + + // documentation inherited public void paintComponent (Graphics g) { super.paintComponent(g); if (_map != null) { - // compute the offset necessary to center the map in the - // display Dimension osize = getSize(); - Dimension msize = _map.getSize(); - int tx = (osize.width - msize.width)/2; - int ty = (osize.height - msize.height)/2; + int tx, ty; + + // compute the necessary centering offset + if (_centerNode != null) { + Point ncoords = _map.getNodeCoords(_centerNode); + tx = (osize.width - _centerNode.getWidth())/2 - ncoords.x; + ty = (osize.height - _centerNode.getHeight())/2 - ncoords.y; + + } else { + Dimension msize = _map.getSize(); + tx = (osize.width - msize.width)/2; + ty = (osize.height - msize.height)/2; + } + g.translate(tx, ty); _map.paint(g); g.translate(-tx, -ty); @@ -144,4 +165,7 @@ public class NodeMapPanel extends JPanel /** The node map. */ protected NodeMap _map; + + /** The node on which we're centering, or null. */ + protected Node _centerNode; }