Behold Vilya, Ring of Air and repository for our game and virtual worldly

extensions to the distributed environment provided by Narya.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@1 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Michael Bayne
2006-06-23 17:58:11 +00:00
commit a4df87e52f
317 changed files with 45818 additions and 0 deletions
@@ -0,0 +1,336 @@
//
// $Id: ChatPanel.java 3741 2005-10-25 11:13:51Z elizabeth $
//
// 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.micasa.client;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import com.samskivert.swing.GroupLayout;
import com.samskivert.swing.HGroupLayout;
import com.samskivert.swing.VGroupLayout;
import com.samskivert.swing.event.AncestorAdapter;
import com.threerings.util.Name;
import com.threerings.crowd.chat.client.ChatDirector;
import com.threerings.crowd.chat.client.ChatDisplay;
import com.threerings.crowd.chat.data.ChatCodes;
import com.threerings.crowd.chat.data.ChatMessage;
import com.threerings.crowd.chat.data.SystemMessage;
import com.threerings.crowd.chat.data.UserMessage;
import com.threerings.crowd.client.OccupantObserver;
import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.data.OccupantInfo;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;
import com.threerings.micasa.Log;
public class ChatPanel
extends JPanel
implements ActionListener, ChatDisplay, OccupantObserver, PlaceView
{
public ChatPanel (CrowdContext ctx)
{
// keep this around for later
_ctx = ctx;
// create our chat director and register ourselves with it
_chatdtr = new ChatDirector(_ctx, null, null);
// XXX - the line above royally borks things because it sends
// null, null downstream.
_chatdtr.addChatDisplay(this);
// register as an occupant observer
_ctx.getOccupantDirector().addOccupantObserver(this);
GroupLayout gl = new VGroupLayout(GroupLayout.STRETCH);
gl.setOffAxisPolicy(GroupLayout.STRETCH);
setLayout(gl);
// create our scrolling chat text display
_text = new JTextPane();
_text.setEditable(false);
add(new JScrollPane(_text));
// create our styles and add those to the text pane
createStyles(_text);
// add a label for the text entry stuff
add(new JLabel("Type here to chat:"), GroupLayout.FIXED);
// create a horizontal group for the text entry bar
gl = new HGroupLayout(GroupLayout.STRETCH);
JPanel epanel = new JPanel(gl);
epanel.add(_entry = new JTextField());
_entry.setActionCommand("send");
_entry.addActionListener(this);
_entry.setEnabled(false);
_send = new JButton("Send");
_send.setEnabled(false);
_send.addActionListener(this);
_send.setActionCommand("send");
epanel.add(_send, GroupLayout.FIXED);
add(epanel, GroupLayout.FIXED);
// listen to ancestor events to request focus when added
addAncestorListener(new AncestorAdapter() {
public void ancestorAdded (AncestorEvent e) {
if (_focus) {
_entry.requestFocusInWindow();
}
}
});
}
/**
* For applications where the chat box has extremely limited space,
* the send button can be removed to leave more space for the text
* input box.
*/
public void removeSendButton ()
{
if (_send.isVisible()) {
// _send.getParent().remove(_send);
_send.setVisible(false);
}
}
/**
* Sets whether the chat box text entry field requests the keyboard
* focus when the panel receives {@link
* AncestorListener#ancestorAdded} or {@link PlaceView#willEnterPlace}
* events.
*/
public void setRequestFocus (boolean focus)
{
_focus = focus;
}
protected void createStyles (JTextPane text)
{
StyleContext sctx = StyleContext.getDefaultStyleContext();
Style defstyle = sctx.getStyle(StyleContext.DEFAULT_STYLE);
_nameStyle = text.addStyle("name", defstyle);
StyleConstants.setForeground(_nameStyle, Color.blue);
_msgStyle = text.addStyle("msg", defstyle);
StyleConstants.setForeground(_msgStyle, Color.black);
_errStyle = text.addStyle("err", defstyle);
StyleConstants.setForeground(_errStyle, Color.red);
_noticeStyle = text.addStyle("notice", defstyle);
StyleConstants.setForeground(_noticeStyle, Color.magenta);
}
// documentation inherited
public void actionPerformed (ActionEvent e)
{
String cmd = e.getActionCommand();
if (cmd.equals("send")) {
sendText();
} else {
System.out.println("Unknown action event: " + cmd);
}
}
// documentation inherited
public void occupantEntered (OccupantInfo info)
{
displayOccupantMessage("*** " + info.username + " entered.");
}
// documentation inherited
public void occupantLeft (OccupantInfo info)
{
displayOccupantMessage("*** " + info.username + " left.");
}
// documentation inherited
public void occupantUpdated (OccupantInfo oinfo, OccupantInfo info)
{
}
protected void displayOccupantMessage (String message)
{
append(message + "\n", _noticeStyle);
}
protected void sendText ()
{
String text = _entry.getText();
// if the message to send begins with /tell then parse it and
// generate a tell request rather than a speak request
if (text.startsWith("/tell")) {
StringTokenizer tok = new StringTokenizer(text);
// there should be at least three tokens: '/tell target word'
if (tok.countTokens() < 3) {
displayError("Usage: /tell username message");
return;
}
// skip the /tell and grab the username
tok.nextToken();
String username = tok.nextToken();
// now strip off everything up to the username to get the
// message
int uidx = text.indexOf(username);
String message = text.substring(uidx + username.length()).trim();
// request to send this text as a tell message
_chatdtr.requestTell(new Name(username), message, null);
} else if (text.startsWith("/clear")) {
// clear the chat box
_chatdtr.clearDisplays();
} else {
// request to send this text as a chat message
_chatdtr.requestSpeak(null, text, ChatCodes.DEFAULT_MODE);
}
// clear out the input because we sent a request
_entry.setText("");
}
// documentation inherited from interface ChatDisplay
public void clear ()
{
_text.setText("");
}
// documentation inherited from interface ChatDisplay
public void displayMessage (ChatMessage message)
{
if (message instanceof UserMessage) {
UserMessage msg = (UserMessage) message;
if (msg.localtype == ChatCodes.USER_CHAT_TYPE) {
append("[" + msg.speaker + " whispers] ", _nameStyle);
append(msg.message + "\n", _msgStyle);
} else {
append("<" + msg.speaker + "> ", _nameStyle);
append(msg.message + "\n", _msgStyle);
}
} else if (message instanceof SystemMessage) {
append(message.message + "\n", _noticeStyle);
} else {
Log.warning("Received unknown message type [message=" +
message + "].");
}
}
protected void displayError (String message)
{
append(message + "\n", _errStyle);
}
/**
* Append the specified text in the specified style.
*/
protected void append (String text, Style style)
{
Document doc = _text.getDocument();
try {
doc.insertString(doc.getLength(), text, style);
} catch (BadLocationException ble) {
Log.warning("Unable to insert text!? [error=" + ble + "].");
}
}
public void willEnterPlace (PlaceObject place)
{
// enable our chat input elements since we're now somewhere that
// we can chat
_entry.setEnabled(true);
_send.setEnabled(true);
if (_focus) {
_entry.requestFocusInWindow();
}
}
// documentation inherited
public void didLeavePlace (PlaceObject place)
{
// nothing doing
}
// documentation inherited
public Dimension getPreferredSize ()
{
Dimension size = super.getPreferredSize();
// always prefer a sensible but not overly large width. this also
// prevents us from inheriting a foolishly large preferred width
// from the JTextPane which sometimes decides it wants to be as
// wide as its widest line of text rather than wrap that line of
// text.
size.width = PREFERRED_WIDTH;
return size;
}
protected CrowdContext _ctx;
protected ChatDirector _chatdtr;
protected boolean _focus = true;
protected JComboBox _roombox;
protected JTextPane _text;
protected JButton _send;
protected JTextField _entry;
protected Style _nameStyle;
protected Style _msgStyle;
protected Style _errStyle;
protected Style _noticeStyle;
/** A width that isn't so skinny that the text is teeny. */
protected static final int PREFERRED_WIDTH = 200;
}
@@ -0,0 +1,140 @@
//
// $Id: ClientController.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.micasa.client;
import java.awt.event.ActionEvent;
import com.samskivert.swing.Controller;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.SessionObserver;
import com.threerings.crowd.data.BodyObject;
import com.threerings.micasa.Log;
import com.threerings.micasa.data.MiCasaBootstrapData;
import com.threerings.micasa.util.MiCasaContext;
/**
* Responsible for top-level control of the client user interface.
*/
public class ClientController extends Controller
implements SessionObserver
{
/**
* Creates a new client controller. The controller will set everything
* up in preparation for logging on.
*/
public ClientController (MiCasaContext ctx, MiCasaFrame frame)
{
// we'll want to keep these around
_ctx = ctx;
_frame = frame;
// we want to know about logon/logoff
_ctx.getClient().addClientObserver(this);
// create the logon panel and display it
_logonPanel = new LogonPanel(_ctx);
_frame.setPanel(_logonPanel);
}
// documentation inherited
public boolean handleAction (ActionEvent action)
{
String cmd = action.getActionCommand();
if (cmd.equals("logoff")) {
// request that we logoff
_ctx.getClient().logoff(true);
return true;
}
Log.info("Unhandled action: " + action);
return false;
}
// documentation inherited
public void clientDidLogon (Client client)
{
Log.info("Client did logon [client=" + client + "].");
// keep the body object around for stuff
_body = (BodyObject)client.getClientObject();
// figure out where to go
int moveOid = -1;
// hacky hack
String jumpOidStr = null;
try {
jumpOidStr = System.getProperty("jumpoid");
} catch (SecurityException se) {
Log.info("Not checking for jumpOid as we're in an applet.");
}
if (jumpOidStr != null) {
try {
moveOid = Integer.parseInt(jumpOidStr);
} catch (NumberFormatException nfe) {
Log.warning("Invalid jump oid [oid=" + jumpOidStr +
", err=" + nfe + "].");
}
} else if (_body.location != -1) {
// if we were already in a location, go there
moveOid = _body.location;
} else {
// otherwise head to the default lobby to start things off
MiCasaBootstrapData data = (MiCasaBootstrapData)
client.getBootstrapData();
moveOid = data.defLobbyOid;
}
if (moveOid > 0) {
_ctx.getLocationDirector().moveTo(moveOid);
}
}
// documentation inherited
public void clientObjectDidChange (Client client)
{
// regrab our body object
_body = (BodyObject)client.getClientObject();
}
// documentation inherited
public void clientDidLogoff (Client client)
{
Log.info("Client did logoff [client=" + client + "].");
// reinstate the logon panel
_frame.setPanel(_logonPanel);
}
protected MiCasaContext _ctx;
protected MiCasaFrame _frame;
protected BodyObject _body;
// our panels
protected LogonPanel _logonPanel;
}
@@ -0,0 +1,234 @@
//
// $Id: LogonPanel.java 4158 2006-05-30 22:12:15Z 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.micasa.client;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.samskivert.swing.GroupLayout;
import com.samskivert.swing.HGroupLayout;
import com.samskivert.swing.VGroupLayout;
import com.threerings.util.MessageBundle;
import com.threerings.util.Name;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.ClientObserver;
import com.threerings.presents.client.LogonException;
import com.threerings.presents.net.Credentials;
import com.threerings.presents.net.UsernamePasswordCreds;
import com.threerings.micasa.util.MiCasaContext;
public class LogonPanel extends JPanel
implements ActionListener, ClientObserver
{
public LogonPanel (MiCasaContext ctx)
{
// keep these around for later
_ctx = ctx;
_msgs = _ctx.getMessageManager().getBundle("micasa");
setLayout(new VGroupLayout());
// stick the logon components into a panel that will stretch them
// to a sensible width
JPanel box = new JPanel(
new VGroupLayout(VGroupLayout.NONE, VGroupLayout.STRETCH,
5, VGroupLayout.CENTER)) {
public Dimension getPreferredSize () {
Dimension psize = super.getPreferredSize();
psize.width = Math.max(psize.width, 300);
return psize;
}
};
add(box);
// try obtaining our title text from a system property
String tstr = null;
try {
tstr = System.getProperty("logon.title");
} catch (Throwable t) {
}
if (tstr == null) {
tstr = "Mi Casa!";
}
// create a big fat label
JLabel title = new JLabel(tstr, JLabel.CENTER);
title.setFont(new Font("Helvetica", Font.BOLD, 24));
box.add(title);
// create the username bar
JPanel bar = new JPanel(new HGroupLayout(GroupLayout.STRETCH));
bar.add(new JLabel(_msgs.get("m.username")), GroupLayout.FIXED);
_username = new JTextField();
_username.setActionCommand("skipToPassword");
_username.addActionListener(this);
bar.add(_username);
box.add(bar);
// create the password bar
bar = new JPanel(new HGroupLayout(GroupLayout.STRETCH));
bar.add(new JLabel(_msgs.get("m.password")), GroupLayout.FIXED);
_password = new JPasswordField();
_password.setActionCommand("logon");
_password.addActionListener(this);
bar.add(_password);
box.add(bar);
// create the logon button bar
HGroupLayout gl = new HGroupLayout(GroupLayout.NONE);
gl.setJustification(GroupLayout.RIGHT);
bar = new JPanel(gl);
_logon = new JButton(_msgs.get("m.logon"));
_logon.setActionCommand("logon");
_logon.addActionListener(this);
bar.add(_logon);
box.add(bar);
box.add(new JLabel(_msgs.get("m.status")));
_status = new JTextArea() {
public Dimension getPreferredScrollableViewportSize ()
{
return new Dimension(10, 100);
}
};
_status.setEditable(false);
JScrollPane scroller = new JScrollPane(_status);
box.add(scroller);
// we'll want to listen for logon failure
_ctx.getClient().addClientObserver(this);
// start with focus in the username field
_username.requestFocusInWindow();
}
public void actionPerformed (ActionEvent e)
{
String cmd = e.getActionCommand();
if (cmd.equals("skipToPassword")) {
_password.requestFocusInWindow();
} else if (cmd.equals("logon")) {
logon();
} else {
System.out.println("Unknown action event: " + cmd);
}
}
// documentation inherited from interface
public void clientDidLogon (Client client)
{
_status.append(_msgs.get("m.logon_success") + "\n");
}
// documentation inherited from interface
public void clientDidLogoff (Client client)
{
_status.append(_msgs.get("m.logged_off") + "\n");
setLogonEnabled(true);
}
// documentation inherited from interface
public void clientFailedToLogon (Client client, Exception cause)
{
String msg;
if (cause instanceof LogonException) {
msg = MessageBundle.compose("m.logon_failed", cause.getMessage());
} else {
msg = MessageBundle.tcompose("m.logon_failed", cause.getMessage());
}
_status.append(_msgs.xlate(msg) + "\n");
setLogonEnabled(true);
}
// documentation inherited from interface
public void clientObjectDidChange (Client client)
{
// nothing we can do here...
}
// documentation inherited from interface
public void clientConnectionFailed (Client client, Exception cause)
{
String msg = MessageBundle.tcompose("m.connection_failed",
cause.getMessage());
_status.append(_msgs.xlate(msg) + "\n");
setLogonEnabled(true);
}
// documentation inherited from interface
public boolean clientWillLogoff (Client client)
{
// no vetoing here
return true;
}
protected void logon ()
{
// disable further logon attempts until we hear back
setLogonEnabled(false);
Name username = new Name(_username.getText().trim());
String password = new String(_password.getPassword()).trim();
String server = _ctx.getClient().getHostname();
int port = _ctx.getClient().getPorts()[0];
String msg = MessageBundle.tcompose("m.logging_on",
server, String.valueOf(port));
_status.append(_msgs.xlate(msg) + "\n");
// configure the client with some credentials and logon
Credentials creds = new UsernamePasswordCreds(username, password);
Client client = _ctx.getClient();
client.setCredentials(creds);
client.logon();
}
protected void setLogonEnabled (boolean enabled)
{
_username.setEnabled(enabled);
_password.setEnabled(enabled);
_logon.setEnabled(enabled);
}
protected MiCasaContext _ctx;
protected MessageBundle _msgs;
protected JTextField _username;
protected JPasswordField _password;
protected JButton _logon;
protected JTextArea _status;
}
@@ -0,0 +1,115 @@
//
// $Id: MiCasaApp.java 4158 2006-05-30 22:12:15Z 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.micasa.client;
import java.io.IOException;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.util.Name;
import com.threerings.presents.client.Client;
import com.threerings.presents.net.UsernamePasswordCreds;
import com.threerings.micasa.Log;
/**
* The micasa app is the main point of entry for the MiCasa client
* application. It creates and initializes the myriad components of the
* client and sets all the proper wheels in motion.
*/
public class MiCasaApp
{
public void init ()
throws IOException
{
// create a frame
_frame = new MiCasaFrame();
// create our client instance
String cclass = null;
try {
cclass = System.getProperty("client");
} catch (Throwable t) {
// security manager in effect, no problem
}
if (cclass == null) {
cclass = MiCasaClient.class.getName();
}
try {
_client = (MiCasaClient)Class.forName(cclass).newInstance();
} catch (Exception e) {
Log.warning("Unable to instantiate client class " +
"[cclass=" + cclass + "].");
Log.logStackTrace(e);
}
// initialize our client instance
_client.init(_frame);
}
public void run (String server, String username, String password)
{
// position everything and show the frame
_frame.setSize(800, 600);
SwingUtil.centerWindow(_frame);
_frame.setVisible(true);
Client client = _client.getContext().getClient();
Log.info("Using [server=" + server + ".");
client.setServer(server, Client.DEFAULT_SERVER_PORTS);
// configure the client with some credentials and logon
if (username != null && password != null) {
// create and set our credentials
client.setCredentials(
new UsernamePasswordCreds(new Name(username), password));
client.logon();
}
}
public static void main (String[] args)
{
String server = "localhost";
if (args.length > 0) {
server = args[0];
}
String username = (args.length > 1) ? args[1] : null;
String password = (args.length > 2) ? args[2] : null;
MiCasaApp app = new MiCasaApp();
try {
// initialize the app
app.init();
} catch (IOException ioe) {
Log.warning("Error initializing application.");
Log.logStackTrace(ioe);
}
// and run it
app.run(server, username, password);
}
protected MiCasaClient _client;
protected MiCasaFrame _frame;
}
@@ -0,0 +1,124 @@
//
// $Id: MiCasaApplet.java 4158 2006-05-30 22:12:15Z 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.micasa.client;
import java.applet.Applet;
import java.io.IOException;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.util.Name;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.ClientAdapter;
import com.threerings.presents.net.UsernamePasswordCreds;
import com.threerings.micasa.Log;
/**
* The MiCasa applet is used to make MiCasa games available via the web
* browser.
*/
public class MiCasaApplet extends Applet
{
/**
* Create the client instance and set things up.
*/
public void init ()
{
try {
// create a frame
_frame = new MiCasaFrame();
// create our client instance
_client = new MiCasaClient();
_client.init(_frame);
Name username = new Name(requireParameter("username"));
String authkey = requireParameter("authkey");
String server = getCodeBase().getHost();
Client client = _client.getContext().getClient();
// indicate which server to which we should connect
client.setServer(server, Client.DEFAULT_SERVER_PORTS);
// create and set our credentials
client.setCredentials(
new UsernamePasswordCreds(username, authkey));
// we want to hide the client frame when we logoff
client.addClientObserver(new ClientAdapter() {
public void clientDidLogoff (Client c)
{
_frame.setVisible(false);
}
});
} catch (IOException ioe) {
Log.warning("Unable to create client.");
Log.logStackTrace(ioe);
}
}
protected String requireParameter (String name)
throws IOException
{
String value = getParameter(name);
if (value == null) {
throw new IOException("Applet missing '" + name + "' parameter.");
}
return value;
}
/**
* Display the client frame and really get things going.
*/
public void start ()
{
if (_client != null) {
// show the frame
_frame.setSize(800, 600);
SwingUtil.centerWindow(_frame);
_frame.setVisible(true);
// and log on
_client.getContext().getClient().logon();
}
}
/**
* Log off and shut on down.
*/
public void stop ()
{
if (_client != null) {
// hide the frame and log off
_frame.setVisible(false);
Client client = _client.getContext().getClient();
if (client.isLoggedOn()) {
client.logoff(false);
}
}
}
protected MiCasaClient _client;
protected MiCasaFrame _frame;
}
@@ -0,0 +1,232 @@
//
// $Id: MiCasaClient.java 4158 2006-05-30 22:12:15Z 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.micasa.client;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JPanel;
import com.samskivert.util.Config;
import com.samskivert.util.RunQueue;
import com.threerings.util.MessageManager;
import com.threerings.presents.client.Client;
import com.threerings.presents.dobj.DObjectManager;
import com.threerings.crowd.client.LocationDirector;
import com.threerings.crowd.client.OccupantDirector;
import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.chat.client.ChatDirector;
import com.threerings.parlor.client.ParlorDirector;
import com.threerings.micasa.util.MiCasaContext;
/**
* The MiCasa client takes care of instantiating all of the proper
* managers and loading up all of the necessary configuration and getting
* the client bootstrapped. It can be extended by games that require an
* extended context implementation.
*/
public class MiCasaClient
implements RunQueue
{
/**
* Initializes a new client and provides it with a frame in which to
* display everything.
*/
public void init (MiCasaFrame frame)
throws IOException
{
// create our context
_ctx = createContextImpl();
// create the directors/managers/etc. provided by the context
createContextServices();
// for test purposes, hardcode the server info
_client.setServer("localhost", Client.DEFAULT_SERVER_PORTS);
// keep this for later
_frame = frame;
// log off when they close the window
_frame.addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent evt) {
// if we're logged on, log off
if (_client.isLoggedOn()) {
_client.logoff(true);
} else {
// otherwise get the heck out
System.exit(0);
}
}
});
// create our client controller and stick it in the frame
_frame.setController(new ClientController(_ctx, _frame));
}
/**
* Returns a reference to the context in effect for this client. This
* reference is valid for the lifetime of the application.
*/
public MiCasaContext getContext ()
{
return _ctx;
}
/**
* Creates the {@link MiCasaContext} implementation that will be
* passed around to all of the client code. Derived classes may wish
* to override this and create some extended context implementation.
*/
protected MiCasaContext createContextImpl ()
{
return new MiCasaContextImpl();
}
/**
* Creates and initializes the various services that are provided by
* the context. Derived classes that provide an extended context
* should override this method and create their own extended
* services. They should be sure to call
* <code>super.createContextServices</code>.
*/
protected void createContextServices ()
throws IOException
{
// create the handles on our various services
_client = new Client(null, this);
// create our managers and directors
_locdir = new LocationDirector(_ctx);
_occdir = new OccupantDirector(_ctx);
_pardtr = new ParlorDirector(_ctx);
_msgmgr = new MessageManager(MESSAGE_MANAGER_PREFIX);
_chatdir = new ChatDirector(_ctx, _msgmgr, null);
}
// documentation inherited from interface RunQueue
public void postRunnable (Runnable run)
{
// queue it on up on the awt thread
EventQueue.invokeLater(run);
}
// documentation inherited from interface RunQueue
public boolean isDispatchThread ()
{
return EventQueue.isDispatchThread();
}
/**
* The context implementation. This provides access to all of the
* objects and services that are needed by the operating client.
*/
protected class MiCasaContextImpl implements MiCasaContext
{
/**
* Apparently the default constructor has default access, rather
* than protected access, even though this class is declared to be
* protected. Why, I don't know, but we need to be able to extend
* this class elsewhere, so we need this.
*/
protected MiCasaContextImpl ()
{
}
public Client getClient ()
{
return _client;
}
public DObjectManager getDObjectManager ()
{
return _client.getDObjectManager();
}
public Config getConfig ()
{
return _config;
}
public LocationDirector getLocationDirector ()
{
return _locdir;
}
public OccupantDirector getOccupantDirector ()
{
return _occdir;
}
public ChatDirector getChatDirector ()
{
return _chatdir;
}
public ParlorDirector getParlorDirector ()
{
return _pardtr;
}
public void setPlaceView (PlaceView view)
{
// stick the place view into our frame
_frame.setPanel((JPanel)view);
}
public void clearPlaceView (PlaceView view)
{
// we'll just let the next place view replace our old one
}
public MiCasaFrame getFrame ()
{
return _frame;
}
public MessageManager getMessageManager ()
{
return _msgmgr;
}
}
protected MiCasaContext _ctx;
protected MiCasaFrame _frame;
protected Config _config = new Config("micasa");
protected Client _client;
protected LocationDirector _locdir;
protected OccupantDirector _occdir;
protected ChatDirector _chatdir;
protected ParlorDirector _pardtr;
protected MessageManager _msgmgr;
/** The prefix prepended to localization bundle names before looking
* them up in the classpath. */
protected static final String MESSAGE_MANAGER_PREFIX = "rsrc.i18n";
}
@@ -0,0 +1,89 @@
//
// $Id: MiCasaFrame.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.micasa.client;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.samskivert.swing.Controller;
import com.samskivert.swing.ControllerProvider;
/**
* Contains the user interface for the MiCasa client application.
*/
public class MiCasaFrame
extends JFrame implements ControllerProvider
{
/**
* Constructs the top-level MiCasa client frame.
*/
public MiCasaFrame ()
{
this("MiCasa Client");
}
/**
* Constructs the top-level MiCasa client frame with the specified
* window title.
*/
public MiCasaFrame (String title)
{
super(title);
// we'll handle shutting things down ourselves
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
/**
* Sets the panel that makes up the entire client display.
*/
public void setPanel (JPanel panel)
{
// remove the old panel
getContentPane().removeAll();
// add the new one
getContentPane().add(panel, BorderLayout.CENTER);
// swing doesn't properly repaint after adding/removing children
panel.revalidate();
repaint();
}
/**
* Sets the controller for the outermost scope. This controller will
* handle all actions that aren't handled by controllers of tigher
* scope.
*/
public void setController (Controller controller)
{
_controller = controller;
}
// documentation inherited
public Controller getController ()
{
return _controller;
}
protected Controller _controller;
}
@@ -0,0 +1,101 @@
//
// $Id: OccupantList.java 3406 2005-03-15 02:12:03Z 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.micasa.client;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import com.threerings.crowd.client.OccupantObserver;
import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.data.OccupantInfo;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;
/**
* The occupant list displays the list of users that are in a particular
* place.
*/
public class OccupantList
extends JList implements PlaceView, OccupantObserver
{
/**
* Constructs an occupant list with the supplied context which it will
* use to register itself with the necessary managers.
*/
public OccupantList (CrowdContext ctx)
{
// set up our list model
_model = new DefaultListModel();
setModel(_model);
// keep our context around for later
_ctx = ctx;
// register ourselves as an occupant observer
_ctx.getOccupantDirector().addOccupantObserver(this);
}
// documentation inherited
public void willEnterPlace (PlaceObject plobj)
{
// add all of the occupants of the place to our list
Iterator users = plobj.occupantInfo.iterator();
while (users.hasNext()) {
OccupantInfo info = (OccupantInfo)users.next();
_model.addElement(info.username);
}
}
// documentation inherited
public void didLeavePlace (PlaceObject plobj)
{
// clear out our occupant entries
_model.clear();
}
// documentation inherited
public void occupantEntered (OccupantInfo info)
{
// simply add this user to our list
_model.addElement(info.username);
}
// documentation inherited
public void occupantLeft (OccupantInfo info)
{
// remove this occupant from our list
_model.removeElement(info.username);
}
// documentation inherited
public void occupantUpdated (OccupantInfo oinfo, OccupantInfo info)
{
// nothing doing
}
/** Our client context. */
protected CrowdContext _ctx;
/** A list model that provides a vector interface. */
protected DefaultListModel _model;
}