Hold on to your seats kids, it's another installment of "A Boy and His
Blowtorch". Refactored GameConfig and the EZ game framework, cleaning up some old cruft from GameConfig (which will break other projects and which I'll fix ASAP) and moved the XML based configuration system from ToyBox into EZGame. git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@286 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
@@ -27,179 +27,117 @@ import mx.controls.ComboBox;
|
||||
import mx.controls.HSlider;
|
||||
import mx.controls.Label;
|
||||
|
||||
import com.threerings.util.StreamableHashMap;
|
||||
import com.threerings.util.StringUtil;
|
||||
|
||||
import com.threerings.flex.LabeledSlider;
|
||||
|
||||
import com.threerings.parlor.game.client.FlexGameConfigurator;
|
||||
|
||||
import com.threerings.ezgame.data.ChoiceParameter;
|
||||
import com.threerings.ezgame.data.EZGameConfig;
|
||||
import com.threerings.ezgame.data.Parameter;
|
||||
import com.threerings.ezgame.data.RangeParameter;
|
||||
import com.threerings.ezgame.data.ToggleParameter;
|
||||
|
||||
/**
|
||||
* Adds custom configuration of options specified in XML.
|
||||
*/
|
||||
public class EZGameConfigurator extends FlexGameConfigurator
|
||||
{
|
||||
/**
|
||||
* Set XML game configuration options.
|
||||
*/
|
||||
public function setXMLConfig (config :XML) :void
|
||||
// from GameConfigurator
|
||||
override protected function gotGameConfig () :void
|
||||
{
|
||||
var log :Log = Log.getLog(this);
|
||||
super.gotGameConfig();
|
||||
|
||||
for each (var param :XML in config..params.children()) {
|
||||
if (StringUtil.isBlank(param.@ident)) {
|
||||
log.warning("Bad configuration option: no 'ident' in " +
|
||||
param + ", ignoring.");
|
||||
continue;
|
||||
}
|
||||
switch (param.localName().toLowerCase()) {
|
||||
case "range":
|
||||
addRangeOption(param, log);
|
||||
break;
|
||||
var params :Array = (_config as EZGameConfig).getGameDefinition().params;
|
||||
if (params == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
case "choice":
|
||||
addChoiceOption(param, log);
|
||||
break;
|
||||
for each (var param :Parameter in params) {
|
||||
if (param is RangeParameter) {
|
||||
var range :RangeParameter = (param as RangeParameter);
|
||||
var slider :HSlider = new HSlider();
|
||||
slider.minimum = range.minimum;
|
||||
slider.maximum = range.maximum;
|
||||
slider.value = range.start;
|
||||
slider.liveDragging = true;
|
||||
slider.snapInterval = 1;
|
||||
addLabeledControl(param, new LabeledSlider(slider));
|
||||
|
||||
case "toggle":
|
||||
addToggleOption(param, log);
|
||||
break;
|
||||
} else if (param is ChoiceParameter) {
|
||||
var choice :ChoiceParameter = (param as ChoiceParameter);
|
||||
var startDex :int = choice.choices.indexOf(choice.start);
|
||||
if (startDex == -1) {
|
||||
Log.getLog(this).warning(
|
||||
"Start value does not appear in list of choices [param=" + choice + "].");
|
||||
} else {
|
||||
var combo :ComboBox = new ComboBox();
|
||||
combo.dataProvider = choice.choices;
|
||||
combo.selectedIndex = startDex;
|
||||
addLabeledControl(param, combo);
|
||||
}
|
||||
|
||||
default:
|
||||
log.warning("Unknown XML configuration option: " + param +
|
||||
", ignoring.");
|
||||
break;
|
||||
} else if (param is ToggleParameter) {
|
||||
var check :CheckBox = new CheckBox();
|
||||
check.selected = (param as ToggleParameter).start;
|
||||
addLabeledControl(param, check);
|
||||
|
||||
} else {
|
||||
Log.getLog(this).warning("Unknown parameter in config [param=" + param + "].");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a control that came from parsing our custom option XML.
|
||||
*/
|
||||
protected function addXMLControl (spec :XML, control :UIComponent) :void
|
||||
{
|
||||
var ident :String = String(spec.@ident);
|
||||
var name :String = String(spec.@name);
|
||||
var tip :String = String(spec.@tip);
|
||||
if (StringUtil.isBlank(name)) {
|
||||
name = ident;
|
||||
}
|
||||
|
||||
var lbl :Label = new Label();
|
||||
lbl.text = name + ":";
|
||||
lbl.styleName = "lobbyLabel";
|
||||
lbl.toolTip = tip == "" ? name : tip;
|
||||
control.toolTip = tip == "" ? name : tip;
|
||||
|
||||
addControl(lbl, control);
|
||||
_customConfigs.push(ident, control);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a 'range' option specified in XML
|
||||
*/
|
||||
protected function addRangeOption (spec :XML, log :Log) :void
|
||||
{
|
||||
var min :Number = Number(spec.@minimum);
|
||||
var max :Number = Number(spec.@maximum);
|
||||
var start :Number = Number(spec.@start);
|
||||
if (isNaN(min) || isNaN(max) || isNaN(start)) {
|
||||
log.warning("Unable to parse range specification. " +
|
||||
"Required numeric values: minimum, maximum, start " +
|
||||
"[xml=" + spec + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
var slider :HSlider = new HSlider();
|
||||
slider.minimum = min;
|
||||
slider.maximum = max;
|
||||
slider.value = start;
|
||||
slider.liveDragging = true;
|
||||
slider.snapInterval = 1;
|
||||
|
||||
addXMLControl(spec, new LabeledSlider(slider));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a 'choice' option specified in XML
|
||||
*/
|
||||
protected function addChoiceOption (spec :XML, log :Log) :void
|
||||
{
|
||||
if (spec.@choices.length == 0 || spec.@start.length == 0) {
|
||||
log.warning("Unable to parse choice specification. " +
|
||||
"Required 'choices' or 'start' is missing. " +
|
||||
"[xml=" + spec + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
var choiceString :String = String(spec.@choices);
|
||||
var start :String = String(spec.@start);
|
||||
|
||||
var choices :Array = choiceString.split(",");
|
||||
var startDex :int = choices.indexOf(start);
|
||||
if (startDex == -1) {
|
||||
log.warning("Choice start value does not appear in list of choices "+
|
||||
"[xml=" + spec + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
var box :ComboBox = new ComboBox();
|
||||
box.dataProvider = choices;
|
||||
box.selectedIndex = startDex;
|
||||
|
||||
addXMLControl(spec, box);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a 'toggle' option specified in XML
|
||||
*/
|
||||
protected function addToggleOption (spec :XML, log :Log) :void
|
||||
{
|
||||
if (spec.@start.length == 0) {
|
||||
log.warning("Unable to parse toggle specification. " +
|
||||
"Required 'start' is missing. " +
|
||||
"[xml=" + spec + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
var startStr :String = String(spec.@start).toLowerCase();
|
||||
|
||||
var box :CheckBox = new CheckBox();
|
||||
box.selected = ("true" == startStr);
|
||||
|
||||
addXMLControl(spec, box);
|
||||
}
|
||||
|
||||
override protected function flushGameConfig () :void
|
||||
{
|
||||
super.flushGameConfig();
|
||||
|
||||
|
||||
// if there were any custom XML configs, flush those as well.
|
||||
if (_customConfigs.length > 0) {
|
||||
var options :Object = {};
|
||||
var params :StreamableHashMap = new StreamableHashMap();
|
||||
|
||||
for (var ii :int = 0; ii < _customConfigs.length; ii += 2) {
|
||||
var ident :String = String(_customConfigs[ii]);
|
||||
var control :UIComponent = (_customConfigs[ii + 1] as UIComponent);
|
||||
if (control is LabeledSlider) {
|
||||
options[ident] = (control as LabeledSlider).slider.value;
|
||||
params.put(ident, (control as LabeledSlider).slider.value);
|
||||
|
||||
} else if (control is CheckBox) {
|
||||
options[ident] = (control as CheckBox).selected;
|
||||
params.put(ident, (control as CheckBox).selected);
|
||||
|
||||
} else if (control is ComboBox) {
|
||||
options[ident] = (control as ComboBox).value;
|
||||
params.put(ident, (control as ComboBox).value);
|
||||
|
||||
} else {
|
||||
Log.getLog(this).warning("Unknow custom config type " + control);
|
||||
}
|
||||
}
|
||||
|
||||
(_config as EZGameConfig).customConfig = options;
|
||||
(_config as EZGameConfig).params = params;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a control that came from parsing our custom option XML.
|
||||
*/
|
||||
protected function addLabeledControl (param :Parameter, control :UIComponent) :void
|
||||
{
|
||||
if (StringUtil.isBlank(param.name)) {
|
||||
param.name = param.ident;
|
||||
}
|
||||
|
||||
var lbl :Label = new Label();
|
||||
lbl.text = param.name + ":";
|
||||
lbl.styleName = "lobbyLabel";
|
||||
lbl.toolTip = param.tip;
|
||||
control.toolTip = param.tip;
|
||||
|
||||
addControl(lbl, control);
|
||||
_customConfigs.push(param.ident, control);
|
||||
}
|
||||
|
||||
/** Contains pairs of identString, control, identString, control.. */
|
||||
protected var _customConfigs :Array = [];
|
||||
}
|
||||
|
||||
@@ -63,12 +63,11 @@ public class EZGamePanel extends Canvas
|
||||
_ezObj = (plobj as EZGameObject);
|
||||
backend = createBackend();
|
||||
|
||||
_gameView = new GameContainer(cfg.gameMedia);
|
||||
_gameView = new GameContainer(cfg.getGameDefinition().getMediaPath(cfg.getGameId()));
|
||||
_gameView.percentWidth = 100;
|
||||
_gameView.percentHeight = 100;
|
||||
backend.setSharedEvents(
|
||||
Loader(_gameView.getMediaContainer().getMedia()).
|
||||
contentLoaderInfo.sharedEvents);
|
||||
Loader(_gameView.getMediaContainer().getMedia()).contentLoaderInfo.sharedEvents);
|
||||
backend.setContainer(_gameView);
|
||||
addChild(_gameView);
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import com.threerings.util.Iterator;
|
||||
import com.threerings.util.MessageBundle;
|
||||
import com.threerings.util.Name;
|
||||
import com.threerings.util.StringUtil;
|
||||
import com.threerings.util.Wrapped;
|
||||
|
||||
import com.threerings.presents.client.ConfirmAdapter;
|
||||
import com.threerings.presents.client.ResultWrapper;
|
||||
@@ -176,7 +177,14 @@ public class GameControlBackend
|
||||
|
||||
// straight data
|
||||
o["gameData"] = _gameData;
|
||||
o["gameConfig"] = (_ctrl.getPlaceConfig() as EZGameConfig).customConfig;
|
||||
|
||||
// convert our game config from a HashMap to a Dictionary
|
||||
var gameConfig :Object = {};
|
||||
var cfg :EZGameConfig = (_ctrl.getPlaceConfig() as EZGameConfig);
|
||||
cfg.params.forEach(function (key :Object, value :Object) :void {
|
||||
gameConfig[key] = (value is Wrapped) ? Wrapped(value).unwrap() : value;
|
||||
});
|
||||
o["gameConfig"] = gameConfig;
|
||||
|
||||
// functions
|
||||
o["setProperty_v1"] = setProperty_v1;
|
||||
@@ -388,7 +396,7 @@ public class GameControlBackend
|
||||
var uc :UserCookie =
|
||||
(_ezObj.userCookies.get(playerId) as UserCookie);
|
||||
if (uc != null) {
|
||||
callback(uc.cookie);
|
||||
callback(EZObjectMarshaller.decode(uc.cookie));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -901,7 +909,7 @@ public class GameControlBackend
|
||||
delete _cookieCallbacks[cookie.playerId];
|
||||
for each (var fn :Function in arr) {
|
||||
try {
|
||||
fn(cookie.cookie);
|
||||
fn(EZObjectMarshaller.decode(cookie.cookie));
|
||||
} catch (err :Error) {
|
||||
// cope
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user