Moved avoid animation tracking into a helper class.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3854 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,68 @@
|
|||||||
|
//
|
||||||
|
// $Id$
|
||||||
|
//
|
||||||
|
// Narya library - tools for developing networked games
|
||||||
|
// Copyright (C) 2002-2005 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.animation;
|
||||||
|
|
||||||
|
import java.awt.Rectangle;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import com.samskivert.swing.util.SwingUtil;
|
||||||
|
|
||||||
|
import com.threerings.media.Log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A utility class for positioning animations such that they don't overlap,
|
||||||
|
* as best as possible.
|
||||||
|
*/
|
||||||
|
public class AnimationArranger
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Position the specified animation so that it is not overlapping
|
||||||
|
* any still-running animations previously passed to this method.
|
||||||
|
*/
|
||||||
|
public void positionAvoidAnimation (Animation anim, Rectangle viewBounds)
|
||||||
|
{
|
||||||
|
Rectangle abounds = new Rectangle(anim.getBounds());
|
||||||
|
ArrayList avoidables = (ArrayList) _avoidAnims.clone();
|
||||||
|
// if we are able to place it somewhere, do so
|
||||||
|
if (SwingUtil.positionRect(abounds, viewBounds, avoidables)) {
|
||||||
|
anim.setLocation(abounds.x, abounds.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add the animation to the list of avoidables
|
||||||
|
_avoidAnims.add(anim);
|
||||||
|
// keep an eye on it so that we can remove it when it's finished
|
||||||
|
anim.addAnimationObserver(_avoidAnimObs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The animations that other animations may wish to avoid. */
|
||||||
|
protected ArrayList _avoidAnims = new ArrayList();
|
||||||
|
|
||||||
|
/** Automatically removes avoid animations when they're done. */
|
||||||
|
protected AnimationAdapter _avoidAnimObs = new AnimationAdapter() {
|
||||||
|
public void animationCompleted (Animation anim, long when) {
|
||||||
|
if (!_avoidAnims.remove(anim)) {
|
||||||
|
Log.warning("Couldn't remove avoid animation?! " + anim + ".");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -38,6 +38,7 @@ import com.samskivert.util.StringUtil;
|
|||||||
import com.threerings.media.VirtualMediaPanel;
|
import com.threerings.media.VirtualMediaPanel;
|
||||||
import com.threerings.media.animation.Animation;
|
import com.threerings.media.animation.Animation;
|
||||||
import com.threerings.media.animation.AnimationAdapter;
|
import com.threerings.media.animation.AnimationAdapter;
|
||||||
|
import com.threerings.media.animation.AnimationArranger;
|
||||||
import com.threerings.media.image.Mirage;
|
import com.threerings.media.image.Mirage;
|
||||||
import com.threerings.media.sprite.Sprite;
|
import com.threerings.media.sprite.Sprite;
|
||||||
|
|
||||||
@@ -292,17 +293,11 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel
|
|||||||
*/
|
*/
|
||||||
public void trackAvoidAnimation (Animation anim)
|
public void trackAvoidAnimation (Animation anim)
|
||||||
{
|
{
|
||||||
// reposition the animation as appropriate
|
// lazy init the arranger
|
||||||
Rectangle abounds = new Rectangle(anim.getBounds());
|
if (_avoidArranger == null) {
|
||||||
ArrayList avoidables = (ArrayList)_avoidAnims.clone();
|
_avoidArranger = new AnimationArranger();
|
||||||
if (SwingUtil.positionRect(abounds, _vbounds, avoidables)) {
|
|
||||||
anim.setLocation(abounds.x, abounds.y);
|
|
||||||
}
|
}
|
||||||
|
_avoidArranger.positionAvoidAnimation(anim, _vbounds);
|
||||||
// add the animation to the list of avoidables
|
|
||||||
_avoidAnims.add(anim);
|
|
||||||
// keep an eye on it so that we can remove it when it's finished
|
|
||||||
anim.addAnimationObserver(_avoidAnimObs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// documentation inherited
|
||||||
@@ -393,8 +388,8 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel
|
|||||||
/** The action sprites on the board. */
|
/** The action sprites on the board. */
|
||||||
protected ArrayList _actionSprites = new ArrayList();
|
protected ArrayList _actionSprites = new ArrayList();
|
||||||
|
|
||||||
/** The animations that other animations may wish to avoid. */
|
/** Prevents certain animations from overlapping others. */
|
||||||
protected ArrayList _avoidAnims = new ArrayList();
|
protected AnimationArranger _avoidArranger;
|
||||||
|
|
||||||
/** Our background image. */
|
/** Our background image. */
|
||||||
protected Mirage _background;
|
protected Mirage _background;
|
||||||
@@ -412,15 +407,6 @@ public abstract class PuzzleBoardView extends VirtualMediaPanel
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Automatically removes avoid animations when they're done. */
|
|
||||||
protected AnimationAdapter _avoidAnimObs = new AnimationAdapter() {
|
|
||||||
public void animationCompleted (Animation anim, long when) {
|
|
||||||
if (!_avoidAnims.remove(anim)) {
|
|
||||||
Log.warning("Couldn't remove avoid animation?! " + anim + ".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Temporary action debugging. */
|
/** Temporary action debugging. */
|
||||||
protected static boolean DEBUG_ACTION = false;
|
protected static boolean DEBUG_ACTION = false;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user