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:
@@ -0,0 +1,151 @@
|
||||
//
|
||||
// $Id: BuilderModel.java 4145 2006-05-24 01:24:24Z ray $
|
||||
//
|
||||
// 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.cast.builder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import com.samskivert.util.CollectionUtil;
|
||||
|
||||
import com.threerings.cast.ComponentRepository;
|
||||
import com.threerings.cast.ComponentClass;
|
||||
|
||||
/**
|
||||
* The builder model represents the current state of the character the
|
||||
* user is building.
|
||||
*/
|
||||
public class BuilderModel
|
||||
{
|
||||
/**
|
||||
* Constructs a builder model.
|
||||
*/
|
||||
public BuilderModel (ComponentRepository crepo)
|
||||
{
|
||||
gatherComponentInfo(crepo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a builder model listener.
|
||||
*
|
||||
* @param l the listener.
|
||||
*/
|
||||
public void addListener (BuilderModelListener l)
|
||||
{
|
||||
if (!_listeners.contains(l)) {
|
||||
_listeners.add(l);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies all model listeners that the builder model has changed.
|
||||
*/
|
||||
protected void notifyListeners (int event)
|
||||
{
|
||||
int size = _listeners.size();
|
||||
for (int ii = 0; ii < size; ii++) {
|
||||
((BuilderModelListener)_listeners.get(ii)).modelChanged(event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the available component classes.
|
||||
*/
|
||||
public List getComponentClasses ()
|
||||
{
|
||||
return Collections.unmodifiableList(_classes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of components available in the specified class.
|
||||
*/
|
||||
public List getComponents (ComponentClass cclass)
|
||||
{
|
||||
List list = (List)_components.get(cclass);
|
||||
if (list == null) {
|
||||
list = new ArrayList();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the selected components in an array.
|
||||
*/
|
||||
public int[] getSelectedComponents ()
|
||||
{
|
||||
int[] values = new int[_selected.size()];
|
||||
Iterator iter = _selected.values().iterator();
|
||||
for (int i = 0; iter.hasNext(); i++) {
|
||||
values[i] = ((Integer)iter.next()).intValue();
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the selected component for the given component class.
|
||||
*/
|
||||
public void setSelectedComponent (ComponentClass cclass, int cid)
|
||||
{
|
||||
_selected.put(cclass, Integer.valueOf(cid));
|
||||
notifyListeners(BuilderModelListener.COMPONENT_CHANGED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gathers component class and component information from the
|
||||
* character manager for later reference by others.
|
||||
*/
|
||||
protected void gatherComponentInfo (ComponentRepository crepo)
|
||||
{
|
||||
// get the list of all component classes
|
||||
CollectionUtil.addAll(_classes, crepo.enumerateComponentClasses());
|
||||
|
||||
for (int ii = 0; ii < _classes.size(); ii++) {
|
||||
// get the list of components available for this class
|
||||
ComponentClass cclass = (ComponentClass)_classes.get(ii);
|
||||
Iterator iter = crepo.enumerateComponentIds(cclass);
|
||||
|
||||
while (iter.hasNext()) {
|
||||
Integer cid = (Integer)iter.next();
|
||||
ArrayList clist = (ArrayList)_components.get(cclass);
|
||||
if (clist == null) {
|
||||
_components.put(cclass, clist = new ArrayList());
|
||||
}
|
||||
|
||||
clist.add(cid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The currently selected character components. */
|
||||
protected HashMap _selected = new HashMap();
|
||||
|
||||
/** The hashtable of available component ids for each class. */
|
||||
protected HashMap _components = new HashMap();
|
||||
|
||||
/** The list of all available component classes. */
|
||||
protected ArrayList _classes = new ArrayList();
|
||||
|
||||
/** The model listeners. */
|
||||
protected ArrayList _listeners = new ArrayList();
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// $Id: BuilderModelListener.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// 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.cast.builder;
|
||||
|
||||
/**
|
||||
* The builder model listener interface should be implemented by
|
||||
* classes that would like to be notified when the builder model is
|
||||
* changed.
|
||||
*
|
||||
* @see BuilderModel
|
||||
*/
|
||||
public interface BuilderModelListener
|
||||
{
|
||||
/**
|
||||
* Called by the {@link BuilderModel} when the model is changed.
|
||||
*/
|
||||
public void modelChanged (int event);
|
||||
|
||||
/** Notification event constants. */
|
||||
public static final int COMPONENT_CHANGED = 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// $Id: BuilderPanel.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// 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.cast.builder;
|
||||
|
||||
import javax.swing.*;
|
||||
import com.samskivert.swing.*;
|
||||
|
||||
import com.threerings.cast.CharacterManager;
|
||||
import com.threerings.cast.ComponentRepository;
|
||||
|
||||
/**
|
||||
* The builder panel presents the user with an overview of a composited
|
||||
* character and facilities for altering the individual components that
|
||||
* comprise the character's display image.
|
||||
*/
|
||||
public class BuilderPanel extends JPanel
|
||||
{
|
||||
/**
|
||||
* Constructs the builder panel.
|
||||
*/
|
||||
public BuilderPanel (CharacterManager charmgr,
|
||||
ComponentRepository crepo, String cprefix)
|
||||
{
|
||||
setLayout(new VGroupLayout());
|
||||
|
||||
// give ourselves a wee bit of a border
|
||||
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||
|
||||
GroupLayout gl = new HGroupLayout(GroupLayout.STRETCH);
|
||||
gl.setOffAxisPolicy(GroupLayout.STRETCH);
|
||||
|
||||
// create the builder model
|
||||
BuilderModel model = new BuilderModel(crepo);
|
||||
|
||||
// create the component selection and sprite display panels
|
||||
JPanel sub = new JPanel(gl);
|
||||
sub.add(new ComponentPanel(model, cprefix));
|
||||
sub.add(new SpritePanel(charmgr, model));
|
||||
add(sub);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// $Id: ClassEditor.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// 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.cast.builder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import com.samskivert.swing.*;
|
||||
|
||||
import com.threerings.cast.ComponentClass;
|
||||
|
||||
/**
|
||||
* The class editor displays a label and a slider that allow the user to
|
||||
* select the desired component for a given component class.
|
||||
*/
|
||||
public class ClassEditor extends JPanel implements ChangeListener
|
||||
{
|
||||
/**
|
||||
* Constructs a class editor.
|
||||
*/
|
||||
public ClassEditor (
|
||||
BuilderModel model, ComponentClass cclass, List components)
|
||||
{
|
||||
_model = model;
|
||||
_components = components;
|
||||
_cclass = cclass;
|
||||
|
||||
GroupLayout gl = new VGroupLayout(GroupLayout.STRETCH);
|
||||
gl.setOffAxisPolicy(GroupLayout.STRETCH);
|
||||
setLayout(gl);
|
||||
|
||||
gl = new HGroupLayout();
|
||||
gl.setJustification(GroupLayout.LEFT);
|
||||
JPanel sub = new JPanel(gl);
|
||||
|
||||
sub.add(new JLabel(cclass.name + ": "));
|
||||
sub.add(_clabel = new JLabel("0"));
|
||||
add(sub);
|
||||
|
||||
// create the slider allowing selection of available components
|
||||
int max = components.size() - 1;
|
||||
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, max, 0);
|
||||
slider.setSnapToTicks(true);
|
||||
slider.addChangeListener(this);
|
||||
add(slider);
|
||||
|
||||
// set the starting component for this class
|
||||
setSelectedComponent(0);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void stateChanged (ChangeEvent e)
|
||||
{
|
||||
JSlider source = (JSlider)e.getSource();
|
||||
if (!source.getValueIsAdjusting()) {
|
||||
int val = source.getValue();
|
||||
// update the model with the newly selected component
|
||||
setSelectedComponent(val);
|
||||
// update the label with the new value
|
||||
_clabel.setText(Integer.toString(val));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the selected component in the builder model for the
|
||||
* component class associated with this editor to the component at
|
||||
* the given index in this editor's list of available components.
|
||||
*/
|
||||
protected void setSelectedComponent (int idx)
|
||||
{
|
||||
int cid = ((Integer)_components.get(idx)).intValue();
|
||||
_model.setSelectedComponent(_cclass, cid);
|
||||
}
|
||||
|
||||
/** The component class associated with this editor. */
|
||||
protected ComponentClass _cclass;
|
||||
|
||||
/** The components selectable via this editor. */
|
||||
protected List _components;
|
||||
|
||||
/** The label denoting the currently selected component index. */
|
||||
protected JLabel _clabel;
|
||||
|
||||
/** The builder model. */
|
||||
protected BuilderModel _model;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
//
|
||||
// $Id: ComponentPanel.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// 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.cast.builder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import com.samskivert.swing.*;
|
||||
|
||||
import com.threerings.cast.Log;
|
||||
import com.threerings.cast.*;
|
||||
|
||||
/**
|
||||
* The component panel displays the available components for all
|
||||
* component classes and allows the user to choose a set of components
|
||||
* for compositing into a character image.
|
||||
*/
|
||||
public class ComponentPanel extends JPanel
|
||||
{
|
||||
/**
|
||||
* Constructs the component panel.
|
||||
*/
|
||||
public ComponentPanel (BuilderModel model, String cprefix)
|
||||
{
|
||||
setLayout(new VGroupLayout(GroupLayout.STRETCH));
|
||||
// set up a border
|
||||
setBorder(BorderFactory.createEtchedBorder());
|
||||
// add the component editors to the panel
|
||||
addClassEditors(model, cprefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds editor user interface elements for each component class to
|
||||
* allow the user to select the desired component.
|
||||
*/
|
||||
protected void addClassEditors (BuilderModel model, String cprefix)
|
||||
{
|
||||
List classes = model.getComponentClasses();
|
||||
int size = classes.size();
|
||||
for (int ii = 0; ii < size; ii++) {
|
||||
ComponentClass cclass = (ComponentClass)classes.get(ii);
|
||||
if (!cclass.name.startsWith(cprefix)) {
|
||||
continue;
|
||||
}
|
||||
List ccomps = model.getComponents(cclass);
|
||||
if (ccomps.size() > 0) {
|
||||
add(new ClassEditor(model, cclass, ccomps));
|
||||
} else {
|
||||
Log.info("Not creating editor for empty class " +
|
||||
"[class=" + cclass + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
//
|
||||
// $Id: SpritePanel.java 4191 2006-06-13 22:42:20Z ray $
|
||||
//
|
||||
// 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.cast.builder;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import com.threerings.util.DirectionCodes;
|
||||
|
||||
import com.threerings.cast.CharacterDescriptor;
|
||||
import com.threerings.cast.CharacterManager;
|
||||
import com.threerings.cast.CharacterSprite;
|
||||
import com.threerings.cast.StandardActions;
|
||||
|
||||
/**
|
||||
* The sprite panel displays a character sprite centered in the panel
|
||||
* suitable for user perusal.
|
||||
*/
|
||||
public class SpritePanel extends JPanel
|
||||
implements DirectionCodes, BuilderModelListener
|
||||
{
|
||||
/**
|
||||
* Constructs the sprite panel.
|
||||
*/
|
||||
public SpritePanel (CharacterManager charmgr, BuilderModel model)
|
||||
{
|
||||
// save off references
|
||||
_charmgr = charmgr;
|
||||
_model = model;
|
||||
|
||||
// listen to the builder model so that we can update the
|
||||
// sprite when a new component is selected
|
||||
_model.addListener(this);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void paintComponent (Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
Graphics2D gfx = (Graphics2D)g;
|
||||
|
||||
if (_sprite != null) {
|
||||
// render the sprite
|
||||
_sprite.paint(gfx);
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void doLayout ()
|
||||
{
|
||||
super.doLayout();
|
||||
generateSprite();
|
||||
centerSprite();
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void modelChanged (int event)
|
||||
{
|
||||
if (event == COMPONENT_CHANGED) {
|
||||
generateSprite();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new character sprite for display to reflect the
|
||||
* currently selected character components.
|
||||
*/
|
||||
protected void generateSprite ()
|
||||
{
|
||||
int components[] = _model.getSelectedComponents();
|
||||
CharacterDescriptor desc = new CharacterDescriptor(components, null);
|
||||
CharacterSprite sprite = _charmgr.getCharacter(desc);
|
||||
setSprite(sprite);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the sprite to be displayed.
|
||||
*/
|
||||
protected void setSprite (CharacterSprite sprite)
|
||||
{
|
||||
sprite.setActionSequence(StandardActions.STANDING);
|
||||
sprite.setOrientation(WEST);
|
||||
_sprite = sprite;
|
||||
centerSprite();
|
||||
repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the sprite's location to render it centered within the panel.
|
||||
*/
|
||||
protected void centerSprite ()
|
||||
{
|
||||
if (_sprite != null) {
|
||||
Dimension d = getSize();
|
||||
int swid = _sprite.getWidth(), shei = _sprite.getHeight();
|
||||
int x = d.width / 2, y = (d.height + shei) / 2;
|
||||
_sprite.setLocation(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
/** The sprite displayed by the panel. */
|
||||
protected CharacterSprite _sprite;
|
||||
|
||||
/** The character manager. */
|
||||
protected CharacterManager _charmgr;
|
||||
|
||||
/** The builder model. */
|
||||
protected BuilderModel _model;
|
||||
}
|
||||
Reference in New Issue
Block a user