From ff4f9d4a9cd6b724bfea2fedf850c835ee80c1c1 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 7 Feb 2005 03:37:39 +0000 Subject: [PATCH] A helper that makes it easy to wire up scrollbars or a scrollbox to a virtual media panel. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3334 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/VirtualRangeModel.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/java/com/threerings/media/VirtualRangeModel.java diff --git a/src/java/com/threerings/media/VirtualRangeModel.java b/src/java/com/threerings/media/VirtualRangeModel.java new file mode 100644 index 000000000..aab1676e9 --- /dev/null +++ b/src/java/com/threerings/media/VirtualRangeModel.java @@ -0,0 +1,96 @@ +// +// $Id$ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.media; + +import java.awt.Rectangle; + +import javax.swing.BoundedRangeModel; +import javax.swing.DefaultBoundedRangeModel; + +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +import com.threerings.media.util.MathUtil; + +/** + * Provides a {@link BoundedRangeModel} interface to a virtual media panel + * so that it can easily be wired up to scroll bars or other scrolling + * controls. + */ +public class VirtualRangeModel + implements ChangeListener +{ + /** + * Creates a virtual media panel range model that will interact with + * the supplied virtual media panel. + */ + public VirtualRangeModel (VirtualMediaPanel panel) + { + _panel = panel; + + // listen to our range models and scroll our badself + _horizRange.addChangeListener(this); + _vertRange.addChangeListener(this); + } + + /** + * Informs the virtual range model the extent of the area over which + * we can scroll. + */ + public void setScrollableArea (int x, int y, int width, int height) + { + Rectangle vbounds = _panel.getViewBounds(); + int hmax = x + width, vmax = y + height, value = + MathUtil.bound(x, _horizRange.getValue(), hmax - vbounds.width); + _horizRange.setRangeProperties(value, vbounds.width, x, hmax, false); + value = MathUtil.bound(y, _vertRange.getValue(), vmax - vbounds.height); + _vertRange.setRangeProperties(value, vbounds.height, y, vmax, false); + } + + /** + * Returns a range model that controls the scrollability of the scene + * in the horizontal direction. + */ + public BoundedRangeModel getHorizModel () + { + return _horizRange; + } + + /** + * Returns a range model that controls the scrollability of the scene + * in the vertical direction. + */ + public BoundedRangeModel getVertModel () + { + return _vertRange; + } + + // documentation inherited from interface ChangeListener + public void stateChanged (ChangeEvent e) + { + _panel.setViewLocation(_horizRange.getValue(), _vertRange.getValue()); + } + + protected VirtualMediaPanel _panel; + protected BoundedRangeModel _horizRange = new DefaultBoundedRangeModel(); + protected BoundedRangeModel _vertRange = new DefaultBoundedRangeModel(); +}