Created listener interfaces that will automatically be registered

if implemented by something on the display hierarchy.
You may also implement them in utility classes and register and unregister
event listening by hand.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@55 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Ray Greenwell
2006-08-24 00:15:49 +00:00
parent b118e302a3
commit a465884a84
11 changed files with 185 additions and 18 deletions
+27
View File
@@ -0,0 +1,27 @@
package com.threerings.ezgame {
import flash.events.Event;
// TODO: there may not be much point in using the standard flash event
// architecture. I'm about thiiiiiiis close to not.
//
public /*abstract*/ class EZEvent extends Event
{
/**
* Access the game object to which this event applies.
*/
public function get gameObject () :EZGame
{
return _ezgame;
}
public function EZEvent (type :String, ezgame :EZGame)
{
super(type);
_ezgame = ezgame;
}
/** The game object for this event. */
protected var _ezgame :EZGame;
}
}
+13
View File
@@ -23,6 +23,19 @@ public interface EZGame
*/ */
function set (propName :String, value :Object, index :int = -1) :void; function set (propName :String, value :Object, index :int = -1) :void;
/**
* Register an object to receive whatever events it should receive,
* based on which event listeners it implements. Note that it is not
* necessary to register any objects which appear on the display list,
* as they'll be registered automatically.
*/
function registerListener (obj :Object) :void;
/**
* Unregister the specified object from receiving events.
*/
function unregisterListener (obj :Object) :void;
/** /**
* Set the specified collection to contain the specified values, * Set the specified collection to contain the specified values,
* clearing any previous values. * clearing any previous values.
@@ -2,7 +2,7 @@ package com.threerings.ezgame {
import flash.events.Event; import flash.events.Event;
public class MessageReceivedEvent extends Event public class MessageReceivedEvent extends EZEvent
{ {
/** The type of all MessageReceivedEvents. */ /** The type of all MessageReceivedEvents. */
public static const TYPE :String = "msgReceived"; public static const TYPE :String = "msgReceived";
@@ -23,9 +23,10 @@ public class MessageReceivedEvent extends Event
return _value; return _value;
} }
public function MessageReceivedEvent (messageName :String, value :Object) public function MessageReceivedEvent (
ezgame :EZGame, messageName :String, value :Object)
{ {
super(TYPE); super(TYPE, ezgame);
_name = messageName; _name = messageName;
_value = value; _value = value;
} }
@@ -38,7 +39,7 @@ public class MessageReceivedEvent extends Event
override public function clone () :Event override public function clone () :Event
{ {
return new MessageReceivedEvent(_name, _value); return new MessageReceivedEvent(_ezgame, _name, _value);
} }
protected var _name :String; protected var _name :String;
@@ -0,0 +1,14 @@
package com.threerings.ezgame {
/**
* Implement this interface to automagically be registered to received
* MessageReceivedEvents.
*/
public interface MessageReceivedListener
{
/**
* Handle a MessageReceivedEvent.
*/
function messageReceived (event :MessageReceivedEvent) :void;
}
}
@@ -6,7 +6,7 @@ import flash.events.Event;
* Property change events are dispatched after the property change was * Property change events are dispatched after the property change was
* validated on the server. * validated on the server.
*/ */
public class PropertyChangedEvent extends Event public class PropertyChangedEvent extends EZEvent
{ {
/** The type of a property change event. */ /** The type of a property change event. */
public static const TYPE :String = "PropChanged"; public static const TYPE :String = "PropChanged";
@@ -47,9 +47,10 @@ public class PropertyChangedEvent extends Event
* Constructor. * Constructor.
*/ */
public function PropertyChangedEvent ( public function PropertyChangedEvent (
propName :String, newValue :Object, oldValue :Object, index :int = -1) ezgame :EZGame, propName :String, newValue :Object,
oldValue :Object, index :int = -1)
{ {
super(TYPE); super(TYPE, ezgame);
_name = propName; _name = propName;
_newValue = newValue; _newValue = newValue;
_oldValue = oldValue; _oldValue = oldValue;
@@ -64,7 +65,8 @@ public class PropertyChangedEvent extends Event
override public function clone () :Event override public function clone () :Event
{ {
return new PropertyChangedEvent(_name, _newValue, _oldValue, _index); return new PropertyChangedEvent(
_ezgame, _name, _newValue, _oldValue, _index);
} }
/** Our implementation details. */ /** Our implementation details. */
@@ -0,0 +1,14 @@
package com.threerings.ezgame {
/**
* Implement this interface to automagically be registered to received
* PropertyChangedEvents.
*/
public interface PropertyChangedListener
{
/**
* Handle a PropertyChangedEvent.
*/
function propertyChanged (event :PropertyChangedEvent) :void;
}
}
@@ -5,7 +5,7 @@ import flash.events.Event;
/** /**
* Dispatched when the state of the game has changed. * Dispatched when the state of the game has changed.
*/ */
public class StateChangedEvent extends Event public class StateChangedEvent extends EZEvent
{ {
/** Indicates that the game has transitioned to a started state. */ /** Indicates that the game has transitioned to a started state. */
public static const GAME_STARTED :String = "GameStarted"; public static const GAME_STARTED :String = "GameStarted";
@@ -17,9 +17,9 @@ public class StateChangedEvent extends Event
// TODO: move to own event? // TODO: move to own event?
public static const TURN_CHANGED :String = "TurnChanged"; public static const TURN_CHANGED :String = "TurnChanged";
public function StateChangedEvent (type :String) public function StateChangedEvent (type :String, ezgame :EZGame)
{ {
super(type); super(type, ezgame);
} }
override public function toString () :String override public function toString () :String
@@ -29,7 +29,7 @@ public class StateChangedEvent extends Event
override public function clone () :Event override public function clone () :Event
{ {
return new StateChangedEvent(type); return new StateChangedEvent(type, _ezgame);
} }
} }
} }
@@ -0,0 +1,14 @@
package com.threerings.ezgame {
/**
* Implement this interface to automagically be registered to received
* StateChangedEvents.
*/
public interface StateChangedListener
{
/**
* Handle a StateChangedEvent.
*/
function stateChanged (event :StateChangedEvent) :void;
}
}
@@ -68,7 +68,7 @@ public class EZGameController extends GameController
public function turnDidChange (turnHolder :Name) :void public function turnDidChange (turnHolder :Name) :void
{ {
dispatchUserEvent( dispatchUserEvent(
new StateChangedEvent(StateChangedEvent.TURN_CHANGED)); new StateChangedEvent(StateChangedEvent.TURN_CHANGED, gameObjImpl));
} }
// from PropertySetListener // from PropertySetListener
@@ -76,8 +76,8 @@ public class EZGameController extends GameController
{ {
// notify the user game // notify the user game
dispatchUserEvent(new PropertyChangedEvent( dispatchUserEvent(new PropertyChangedEvent(
event.getName(), event.getValue(), event.getOldValue(), gameObjImpl, event.getName(), event.getValue(),
event.getIndex())); event.getOldValue(), event.getIndex()));
} }
// from MessageListener // from MessageListener
@@ -114,7 +114,7 @@ public class EZGameController extends GameController
protected function dispatchUserMessage (args :Array) :void protected function dispatchUserMessage (args :Array) :void
{ {
dispatchUserEvent(new MessageReceivedEvent( dispatchUserEvent(new MessageReceivedEvent(
(args[0] as String), gameObjImpl, (args[0] as String),
EZObjectMarshaller.decode(args[1]))); EZObjectMarshaller.decode(args[1])));
} }
@@ -127,14 +127,14 @@ public class EZGameController extends GameController
{ {
super.gameDidStart(); super.gameDidStart();
dispatchUserEvent( dispatchUserEvent(
new StateChangedEvent(StateChangedEvent.GAME_STARTED)); new StateChangedEvent(StateChangedEvent.GAME_STARTED, gameObjImpl));
} }
override protected function gameDidEnd () :void override protected function gameDidEnd () :void
{ {
super.gameDidEnd(); super.gameDidEnd();
dispatchUserEvent( dispatchUserEvent(
new StateChangedEvent(StateChangedEvent.GAME_ENDED)); new StateChangedEvent(StateChangedEvent.GAME_ENDED, gameObjImpl));
} }
protected function dispatchUserEvent (event :Event) :void protected function dispatchUserEvent (event :Event) :void
@@ -38,6 +38,7 @@ public class EZGamePanel extends VBox
// add a listener so that we hear about all new children // add a listener so that we hear about all new children
addEventListener(Event.ADDED, childAdded); addEventListener(Event.ADDED, childAdded);
addEventListener(Event.REMOVED, childRemoved);
var cfg :EZGameConfig = (ctrl.getPlaceConfig() as EZGameConfig); var cfg :EZGameConfig = (ctrl.getPlaceConfig() as EZGameConfig);
_gameView = new MediaContainer(cfg.configData); // TODO _gameView = new MediaContainer(cfg.configData); // TODO
@@ -64,6 +65,7 @@ public class EZGamePanel extends VBox
// from PlaceView // from PlaceView
public function didLeavePlace (plobj :PlaceObject) :void public function didLeavePlace (plobj :PlaceObject) :void
{ {
removeListeners(_gameView);
_ezObj = null; _ezObj = null;
} }
@@ -77,6 +79,17 @@ public class EZGamePanel extends VBox
} }
} }
/**
* Handle REMOVED events.
*/
protected function childRemoved (event :Event) :void
{
trace("Child removed: " + event.target);
if (_ezObj != null) {
removeListeners(event.target as DisplayObject);
}
}
/** /**
* Find any children of the specified object that implement * Find any children of the specified object that implement
* com.metasoy.game.Game and provide them with the GameObject. * com.metasoy.game.Game and provide them with the GameObject.
@@ -93,6 +106,17 @@ public class EZGamePanel extends VBox
_seenGames[disp] = true; _seenGames[disp] = true;
} }
} }
// always check to see if it's a listener
_ctrl.gameObjImpl.registerListener(disp);
});
}
protected function removeListeners (root :DisplayObject) :void
{
DisplayUtil.walkDisplayObjects(root,
function (disp :DisplayObject) :void
{
_ctrl.gameObjImpl.unregisterListener(disp);
}); });
} }
@@ -26,7 +26,12 @@ import com.threerings.ezgame.data.PropertySetEvent;
import com.threerings.ezgame.util.EZObjectMarshaller; import com.threerings.ezgame.util.EZObjectMarshaller;
import com.threerings.ezgame.EZGame; import com.threerings.ezgame.EZGame;
import com.threerings.ezgame.MessageReceivedEvent;
import com.threerings.ezgame.MessageReceivedListener;
import com.threerings.ezgame.PropertyChangedEvent; import com.threerings.ezgame.PropertyChangedEvent;
import com.threerings.ezgame.PropertyChangedListener;
import com.threerings.ezgame.StateChangedEvent;
import com.threerings.ezgame.StateChangedListener;
public class GameObjectImpl extends EventDispatcher public class GameObjectImpl extends EventDispatcher
implements EZGame implements EZGame
@@ -74,6 +79,59 @@ public class GameObjectImpl extends EventDispatcher
_ezObj.applyPropertySet(propName, value, index); _ezObj.applyPropertySet(propName, value, index);
} }
// from EZGame
public function registerListener (obj :Object) :void
{
if (obj is MessageReceivedListener) {
var mrl :MessageReceivedListener = (obj as MessageReceivedListener);
addEventListener(
MessageReceivedEvent.TYPE, mrl.messageReceived,
false, 0, true);
}
if (obj is PropertyChangedListener) {
var pcl :PropertyChangedListener = (obj as PropertyChangedListener);
addEventListener(
PropertyChangedEvent.TYPE, pcl.propertyChanged,
false, 0, true);
}
if (obj is StateChangedListener) {
var scl :StateChangedListener = (obj as StateChangedListener);
addEventListener(
StateChangedEvent.GAME_STARTED, scl.stateChanged,
false, 0, true);
addEventListener(
StateChangedEvent.TURN_CHANGED, scl.stateChanged,
false, 0, true);
addEventListener(
StateChangedEvent.GAME_ENDED, scl.stateChanged,
false, 0, true);
}
}
// from EZGame
public function unregisterListener (obj :Object) :void
{
if (obj is MessageReceivedListener) {
var mrl :MessageReceivedListener = (obj as MessageReceivedListener);
removeEventListener(
MessageReceivedEvent.TYPE, mrl.messageReceived);
}
if (obj is PropertyChangedListener) {
var pcl :PropertyChangedListener = (obj as PropertyChangedListener);
removeEventListener(
PropertyChangedEvent.TYPE, pcl.propertyChanged);
}
if (obj is StateChangedListener) {
var scl :StateChangedListener = (obj as StateChangedListener);
removeEventListener(
StateChangedEvent.GAME_STARTED, scl.stateChanged);
removeEventListener(
StateChangedEvent.TURN_CHANGED, scl.stateChanged);
removeEventListener(
StateChangedEvent.GAME_ENDED, scl.stateChanged);
}
}
// from EZGame // from EZGame
public function setCollection (collName :String, values :Array) :void public function setCollection (collName :String, values :Array) :void
{ {