Behold, Nenya, Ring of Water and repository for our media and animation related

goodies, both Java 2D and LWJGL/JME 3D.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@1 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2006-06-23 18:07:28 +00:00
commit c2117ee86d
570 changed files with 61913 additions and 0 deletions
@@ -0,0 +1,191 @@
//
// $Id: ViewerApp.java 4181 2006-06-07 21:54:12Z mdb $
//
// 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.miso.viewer;
import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.resource.ResourceManager;
import com.threerings.media.FrameManager;
import com.threerings.media.image.ImageManager;
import com.threerings.media.tile.bundle.BundledTileSetRepository;
import com.threerings.cast.CharacterManager;
import com.threerings.cast.bundle.BundledComponentRepository;
import com.threerings.miso.Log;
import com.threerings.miso.data.SimpleMisoSceneModel;
import com.threerings.miso.tile.MisoTileManager;
import com.threerings.miso.tools.xml.SimpleMisoSceneParser;
import com.threerings.miso.util.MisoContext;
/**
* The ViewerApp is a scene viewing application that allows for trying
* out game scenes in a pseudo-runtime environment.
*/
public class ViewerApp
{
/**
* Construct and initialize the ViewerApp object.
*/
public ViewerApp (String[] args)
throws IOException
{
if (args.length < 1) {
System.err.println("Usage: ViewerApp scene_file.xml");
System.exit(-1);
}
// get the graphics environment
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
// get the target graphics device
GraphicsDevice gd = env.getDefaultScreenDevice();
Log.info("Graphics device [dev=" + gd +
", mem=" + gd.getAvailableAcceleratedMemory() +
", displayChange=" + gd.isDisplayChangeSupported() +
", fullScreen=" + gd.isFullScreenSupported() + "].");
// get the graphics configuration and display mode information
GraphicsConfiguration gc = gd.getDefaultConfiguration();
DisplayMode dm = gd.getDisplayMode();
Log.info("Display mode [bits=" + dm.getBitDepth() +
", wid=" + dm.getWidth() + ", hei=" + dm.getHeight() +
", refresh=" + dm.getRefreshRate() + "].");
// create the window
_frame = new ViewerFrame(gc);
_framemgr = FrameManager.newInstance(_frame, _frame);
// we don't need to configure anything
ResourceManager rmgr = new ResourceManager("rsrc");
rmgr.initBundles(
null, "config/resource/manager.properties", null);
ImageManager imgr = new ImageManager(rmgr, _frame);
_tilemgr = new MisoTileManager(rmgr, imgr);
_tilemgr.setTileSetRepository(
new BundledTileSetRepository(rmgr, imgr, "tilesets"));
// create the context object
MisoContext ctx = new ContextImpl();
// create the various managers
BundledComponentRepository crepo =
new BundledComponentRepository(rmgr, imgr, "components");
CharacterManager charmgr = new CharacterManager(imgr, crepo);
// create our scene view panel
_panel = new ViewerSceneViewPanel(ctx, charmgr, crepo);
_frame.setPanel(_panel);
// load up the scene specified by the user
try {
SimpleMisoSceneParser parser = new SimpleMisoSceneParser("");
SimpleMisoSceneModel model = parser.parseScene(args[0]);
if (model == null) {
Log.warning("No miso scene found in scene file " +
"[path=" + args[0] + "].");
System.exit(-1);
}
_panel.setSceneModel(model);
} catch (Exception e) {
Log.warning("Unable to parse scene [path=" + args[0] + "].");
Log.logStackTrace(e);
System.exit(-1);
}
// size and position the window, entering full-screen exclusive
// mode if available
if (gd.isFullScreenSupported()) {
Log.info("Entering full-screen exclusive mode.");
gd.setFullScreenWindow(_frame);
} else {
Log.warning("Full-screen exclusive mode not available.");
// _frame.pack();
_frame.setSize(600, 400);
SwingUtil.centerWindow(_frame);
}
}
/**
* The implementation of the MisoContext interface that provides
* handles to the config and manager objects that offer commonly used
* services.
*/
protected class ContextImpl implements MisoContext
{
public MisoTileManager getTileManager ()
{
return _tilemgr;
}
public FrameManager getFrameManager ()
{
return _framemgr;
}
}
/**
* Run the application.
*/
public void run ()
{
// show the window
_frame.show();
_framemgr.start();
}
/**
* Instantiate the application object and start it running.
*/
public static void main (String[] args)
{
try {
ViewerApp app = new ViewerApp(args);
app.run();
} catch (IOException ioe) {
System.err.println("Error initializing viewer app.");
ioe.printStackTrace();
}
}
/** The tile manager object. */
protected MisoTileManager _tilemgr;
/** The frame manager. */
protected FrameManager _framemgr;
/** The main application window. */
protected ViewerFrame _frame;
/** The main panel. */
protected ViewerSceneViewPanel _panel;
}
@@ -0,0 +1,94 @@
//
// $Id: ViewerFrame.java 3099 2004-08-27 02:21:06Z mdb $
//
// 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.miso.viewer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import com.samskivert.swing.util.MenuUtil;
/**
* The viewer frame is the main application window.
*/
public class ViewerFrame extends JFrame
{
/**
* Creates a frame in which the viewer application can operate.
*/
public ViewerFrame (GraphicsConfiguration gc)
{
super(gc);
// set up the frame options
setTitle("Scene Viewer");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// set the frame and content panel background to black
setBackground(Color.black);
getContentPane().setBackground(Color.black);
// create the "Settings" menu
JMenu menuSettings = new JMenu("Settings");
MenuUtil.addMenuItem(
menuSettings, "Preferences", this, "handlePreferences");
// create the menu bar
JMenuBar bar = new JMenuBar();
bar.add(menuSettings);
// add the menu bar to the frame
setJMenuBar(bar);
}
/**
* Sets the panel displayed by this frame.
*/
public void setPanel (Component panel)
{
// if we had an old panel, remove it
if (_panel != null) {
getContentPane().remove(_panel);
}
// now add the new one
_panel = panel;
getContentPane().add(_panel, BorderLayout.CENTER);
}
/**
* Dummy callback method.
*/
public void handlePreferences (ActionEvent event)
{
System.err.println("Nothing doing!");
}
protected Component _panel;
}
@@ -0,0 +1,237 @@
//
// $Id: ViewerSceneViewPanel.java 4188 2006-06-13 18:03:48Z mdb $
//
// 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.miso.viewer;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import com.samskivert.util.RandomUtil;
import com.threerings.cast.CharacterDescriptor;
import com.threerings.cast.CharacterManager;
import com.threerings.cast.CharacterSprite;
import com.threerings.cast.ComponentRepository;
import com.threerings.cast.util.CastUtil;
import com.threerings.media.sprite.PathObserver;
import com.threerings.media.sprite.Sprite;
import com.threerings.media.sprite.SpriteManager;
import com.threerings.media.util.LineSegmentPath;
import com.threerings.media.util.Path;
import com.threerings.media.util.PerformanceMonitor;
import com.threerings.media.util.PerformanceObserver;
import com.threerings.miso.Log;
import com.threerings.miso.MisoConfig;
import com.threerings.miso.client.MisoScenePanel;
import com.threerings.miso.data.MisoSceneModel;
import com.threerings.miso.util.MisoContext;
public class ViewerSceneViewPanel extends MisoScenePanel
implements PerformanceObserver, PathObserver
{
/**
* Construct the panel and initialize it with a context.
*/
public ViewerSceneViewPanel (MisoContext ctx,
CharacterManager charmgr,
ComponentRepository crepo)
{
super(ctx, MisoConfig.getSceneMetrics());
// create the character descriptors
_descUser = CastUtil.getRandomDescriptor("female", crepo);
_descDecoy = CastUtil.getRandomDescriptor("male", crepo);
// create the manipulable sprite
_sprite = createSprite(_spritemgr, charmgr, _descUser);
setFollowsPathable(_sprite, CENTER_ON_PATHABLE);
// create the decoy sprites
createDecoys(_spritemgr, charmgr);
PerformanceMonitor.register(this, "paint", 1000);
}
// documentation inherited
public void setSceneModel (MisoSceneModel model)
{
super.setSceneModel(model);
Log.info("Using " + model + ".");
}
// documentation inherited
public void doLayout ()
{
super.doLayout();
// now that we have a scene, we can create valid paths for our
// decoy sprites
createDecoyPaths();
}
/**
* Creates a new sprite.
*/
protected CharacterSprite createSprite (
SpriteManager spritemgr, CharacterManager charmgr,
CharacterDescriptor desc)
{
CharacterSprite s = charmgr.getCharacter(desc);
if (s != null) {
// start 'em out standing
s.setActionSequence(CharacterSprite.STANDING);
s.setLocation(300, 300);
s.addSpriteObserver(this);
spritemgr.addSprite(s);
}
return s;
}
/**
* Creates the decoy sprites.
*/
protected void createDecoys (
SpriteManager spritemgr, CharacterManager charmgr)
{
_decoys = new CharacterSprite[NUM_DECOYS];
for (int ii = 0; ii < NUM_DECOYS; ii++) {
_decoys[ii] = createSprite(spritemgr, charmgr, _descDecoy);
}
}
/**
* Creates paths for the decoy sprites.
*/
protected void createDecoyPaths ()
{
for (int ii = 0; ii < NUM_DECOYS; ii++) {
if (_decoys[ii] != null) {
createRandomPath(_decoys[ii]);
}
}
}
// documentation inherited
public void paint (Graphics g)
{
super.paint(g);
PerformanceMonitor.tick(this, "paint");
}
// documentation inherited
public void checkpoint (String name, int ticks)
{
Log.info(name + " [ticks=" + ticks + "].");
}
// documentation inherited
public void mousePressed (MouseEvent e)
{
super.mousePressed(e);
int x = e.getX(), y = e.getY();
Log.info("Mouse pressed +" + x + "+" + y);
switch (e.getModifiers()) {
case MouseEvent.BUTTON1_MASK:
createPath(_sprite, x, y);
break;
case MouseEvent.BUTTON2_MASK:
for (int ii = 0; ii < NUM_DECOYS; ii++) {
createPath(_decoys[ii], x, y);
}
break;
}
}
/**
* Assigns the sprite a path leading to the given destination
* screen coordinates. Returns whether a path was successfully
* assigned.
*/
protected boolean createPath (CharacterSprite s, int x, int y)
{
// get the path from here to there
LineSegmentPath path = (LineSegmentPath)getPath(s, x, y, true);
if (path == null) {
s.cancelMove();
return false;
}
// start the sprite moving along the path
path.setVelocity(100f/1000f);
s.move(path);
return true;
}
/**
* Assigns a new random path to the given sprite.
*/
protected void createRandomPath (CharacterSprite s)
{
Dimension d = _vbounds.getSize();
if (d.width <= 0 || d.height <= 0) {
return;
}
int x, y;
do {
x = RandomUtil.getInt(d.width);
y = RandomUtil.getInt(d.height);
} while (!createPath(s, x, y));
}
// documentation inherited
public void pathCompleted (Sprite sprite, Path path, long when)
{
CharacterSprite s = (CharacterSprite)sprite;
if (s != _sprite) {
// move the sprite to a new random location
createRandomPath(s);
}
}
// documentation inherited
public void pathCancelled (Sprite sprite, Path path)
{
// nothing doing
}
/** The number of decoy characters milling about. */
protected static final int NUM_DECOYS = 5;
/** The character descriptor for the user character. */
protected CharacterDescriptor _descUser;
/** The character descriptor for the decoy characters. */
protected CharacterDescriptor _descDecoy;
/** The sprite we're manipulating within the view. */
protected CharacterSprite _sprite;
/** The test sprites that meander about aimlessly. */
protected CharacterSprite _decoys[];
}