[customwars] r753 committed - Refactor script related code from the GUI class into the BeanShell cla...

6 views
Skip to first unread message

codesite...@google.com

unread,
Mar 24, 2011, 11:01:09 AM3/24/11
to customwar...@googlegroups.com
Revision: 753
Author: ace.stef
Date: Thu Mar 24 08:00:20 2011
Log: Refactor script related code from the GUI class into the
BeanShell class.
http://code.google.com/p/customwars/source/detail?r=753

Added:
/trunk/v2/src/com/customwars/client/script/BeanShell.java
Modified:
/trunk/v2/src/com/customwars/client/Config.java
/trunk/v2/src/com/customwars/client/ui/GUI.java
/trunk/v2/src/com/customwars/client/ui/state/InGameState.java

=======================================
--- /dev/null
+++ /trunk/v2/src/com/customwars/client/script/BeanShell.java Thu Mar 24
08:00:20 2011
@@ -0,0 +1,109 @@
+package com.customwars.client.script;
+
+import bsh.EvalError;
+import bsh.Interpreter;
+import bsh.util.JConsole;
+import org.apache.log4j.Logger;
+
+import javax.swing.JFrame;
+
+/**
+ * The BeanShell class provides global access to a single interactive
Beanshell interpreter.
+ * Java objects can be placed into the script-engine scope by using the
{@link #set(String, Object)} method.
+ * A swing JFrame console can be retrieved by calling getConsole();
+ * This console allows direct access to the script objects.
+ * All checked exceptions are logged.
+ *
+ * @see Interpreter
+ * @see JConsole
+ */
+public class BeanShell {
+ private static final Logger logger = Logger.getLogger(BeanShell.class);
+ private static final BeanShell instance = new BeanShell();
+ private final Interpreter bsh;
+ private final JFrame consoleFrame;
+
+ /**
+ * This class is a singleton. Direct instantiation is not allowed.
+ *
+ * @see #get
+ */
+ private BeanShell() {
+ consoleFrame = new JFrame("Console");
+ consoleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ JConsole console = new JConsole();
+
+ consoleFrame.add(console);
+ consoleFrame.setBounds(0, 0, 400, 400);
+ bsh = new Interpreter(console);
+ new Thread(bsh).start();
+ }
+
+ /**
+ * @return The single instance of this class.
+ */
+ public static BeanShell get() {
+ return instance;
+ }
+
+ /**
+ * @see Interpreter#set(String, Object)
+ */
+ public void set(String name, Object obj) {
+ try {
+ bsh.set(name, obj);
+ } catch (EvalError ex) {
+ logger.warn("Could not add " + name, ex);
+ }
+ }
+
+ /**
+ * @see Interpreter#unset(String)
+ */
+ public void unset(String name) {
+ try {
+ bsh.unset(name);
+ } catch (EvalError ex) {
+ logger.warn("Could not unset " + name, ex);
+ }
+ }
+
+ /**
+ * Retrieves a scripted variable from beanshell.
+ *
+ * @throws IllegalArgumentException When there is no var assigned to the
given name.
+ * @see Interpreter#get(String)
+ */
+ public Object get(String name) throws IllegalArgumentException {
+ try {
+ return bsh.get(name);
+ } catch (EvalError ex) {
+ throw new IllegalArgumentException("Could not get " + name, ex);
+ }
+ }
+
+ /**
+ * @see Interpreter#eval(String)
+ */
+ public void eval(String expression) {
+ try {
+ bsh.eval(expression);
+ } catch (EvalError ex) {
+ logger.warn("Could not evaluate expression " + expression, ex);
+ }
+ }
+
+ /**
+ * @return A list of variable names added to beanshell using the set
method.
+ */
+ public String[] getAllVars() {
+ return bsh.getNameSpace().getVariableNames();
+ }
+
+ /**
+ * @return A Swing JFrame console attached to a beanshell Interpreter.
+ */
+ public JFrame getConsole() {
+ return consoleFrame;
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/Config.java Mon Mar 21 06:59:56 2011
+++ /trunk/v2/src/com/customwars/client/Config.java Thu Mar 24 08:00:20 2011
@@ -2,6 +2,7 @@

import com.customwars.client.io.ResourceManager;
import com.customwars.client.model.co.COFactory;
+import com.customwars.client.script.BeanShell;
import com.customwars.client.script.ScriptManager;
import com.customwars.client.tools.IOUtil;
import com.customwars.client.tools.StringUtil;
@@ -38,11 +39,11 @@

private final ResourceManager resources;
private static String resourcesPath;
- private final ScriptManager scriptManager;
+ private final ScriptManager coScriptManager;

public Config(ResourceManager resources) {
this.resources = resources;
- this.scriptManager = new ScriptManager();
+ this.coScriptManager = new ScriptManager();
}

/**
@@ -71,6 +72,7 @@
storePaths();
initHomeDir();
loadConfigFiles();
+ initBeanshellConsole();
}

private static void checkForResourcesInClassPath() {
@@ -186,8 +188,8 @@

private void initScripts(String pluginPath) {
String coDir = pluginPath + "/data/co/";
- scriptManager.init(coDir + MAIN_SCRIPT_FILE, coDir + CO_SCRIPT_FILE);
- COFactory.setScriptManager(scriptManager);
+ coScriptManager.init(coDir + MAIN_SCRIPT_FILE, coDir + CO_SCRIPT_FILE);
+ COFactory.setScriptManager(coScriptManager);
}

private static Properties loadProperties(String location) throws
IOException {
@@ -199,4 +201,11 @@
InputStream in = ResourceLoader.getResourceAsStream(location);
return IOUtil.loadProperties(in, defaults);
}
-}
+
+ private void initBeanshellConsole() {
+ BeanShell bsh = BeanShell.get();
+ bsh.set("coScripts", coScriptManager);
+ bsh.set("resources", resources);
+ bsh.eval("setAccessibility(true)");
+ }
+}
=======================================
--- /trunk/v2/src/com/customwars/client/ui/GUI.java Sun Jul 4 03:40:33 2010
+++ /trunk/v2/src/com/customwars/client/ui/GUI.java Thu Mar 24 08:00:20 2011
@@ -1,11 +1,9 @@
package com.customwars.client.ui;

-import bsh.EvalError;
-import bsh.Interpreter;
-import bsh.util.JConsole;
import com.customwars.client.App;
import com.customwars.client.model.game.Game;
import com.customwars.client.network.NetworkException;
+import com.customwars.client.script.BeanShell;
import com.customwars.client.tools.StringUtil;
import com.customwars.client.ui.hud.ModelEventScreen;
import org.apache.log4j.Logger;
@@ -21,14 +19,12 @@

/**
* Application wide gui's Contains a console and a game event viewer
window.
- * Live objects can be added to the console
*/
public class GUI {
public static int YES_OPTION = 0;
public static int NO_OPTION = 1;

private static final Logger logger = Logger.getLogger(GUI.class);
- private static Interpreter bsh;
private static JFrame eventFrame, consoleFrame;
private static ModelEventScreen modelEventScreen;
private static GUIContext guiContext;
@@ -40,27 +36,11 @@
GUI.guiContext = guiContext;

if (!inited) {
- initConsole();
+ consoleFrame = BeanShell.get().getConsole();
initEventScreen();
inited = true;
}
}
-
- private static void initConsole() {
- consoleFrame = new JFrame("Console");
- JConsole console = new JConsole();
- bsh = new Interpreter(console);
-
- try {
- bsh.eval("setAccessibility(true)");
- } catch (EvalError evalError) {
- throw new RuntimeException(evalError);
- }
-
- consoleFrame.add(console);
- consoleFrame.setBounds(0, 0, 400, 400);
- new Thread(bsh).start();
- }

private static void initEventScreen() {
eventFrame = new JFrame("Model Events");
@@ -115,40 +95,6 @@
public static void setGame(Game game) {
modelEventScreen.setGame(game);
}
-
- /**
- * Add a live object to the console
- *
- * @param objScriptName The name to reference the object
- * @param obj The object which methods should become accesible
from the console
- */
- public static void addLiveObjToConsole(String objScriptName, Object obj)
{
- try {
- bsh.set(objScriptName, obj);
- } catch (EvalError ex) {
- logger.warn("Could not add object " + objScriptName);
- }
- }
-
- /**
- * Remove a live object from the console
- *
- * @param objScriptName The name that an object has been previously
referenced to
- */
- public static void removeLiveObjFromConsole(String objScriptName) {
- try {
- bsh.unset(objScriptName);
- } catch (EvalError ex) {
- logger.warn("Could not remove object " + objScriptName);
- }
- }
-
- /**
- * @return a list of names for each live objects
- */
- public static String[] getAllLiveObjectNames() {
- return bsh.getNameSpace().getVariableNames();
- }

/**
* Can the given point fit to the screen, the 'screen' can be either the
guiContext or the camera
=======================================
--- /trunk/v2/src/com/customwars/client/ui/state/InGameState.java Mon Mar
14 06:42:50 2011
+++ /trunk/v2/src/com/customwars/client/ui/state/InGameState.java Thu Mar
24 08:00:20 2011
@@ -21,6 +21,7 @@
import com.customwars.client.model.map.path.MoveTraverse;
import com.customwars.client.network.MessageSender;
import com.customwars.client.network.MessageSenderFactory;
+import com.customwars.client.script.BeanShell;
import com.customwars.client.tools.ColorUtil;
import com.customwars.client.ui.Camera2D;
import com.customwars.client.ui.GUI;
@@ -134,6 +135,7 @@
private void initSubSystems(Game game) {
initGame(game);
initGameContext(game, guiContext);
+ initScriptObjects();
}

private void initGame(Game game) {
@@ -141,7 +143,6 @@
this.game = game;
this.map = game.getMap();
initCamera(map);
- initScriptObjects();
GUI.setGame(game);
center = GUI.getCenteredRenderPoint(map.getSize(), guiContext);
}
@@ -184,14 +185,14 @@
* Add objects to beanshell, accessible by their name
*/
private void initScriptObjects() {
+ BeanShell bsh = BeanShell.get();
for (Player p : game.getAllPlayers()) {
String colorName = ColorUtil.toString(p.getColor());
- GUI.addLiveObjToConsole("p_" + colorName, p);
+ bsh.set("p_" + colorName, p);
}

- GUI.addLiveObjToConsole("game", game);
- GUI.addLiveObjToConsole("map", map);
- GUI.addLiveObjToConsole("resources", resources);
+ bsh.set("game", game);
+ bsh.set("map", map);
}

private void initGameListener(Game game) {
@@ -226,7 +227,22 @@
super.leave(container, stateBasedGame);
cwInput.resetInputTransform();
cursorControl.removeListener(this);
- gameOver = false;
+
+ if (gameOver) {
+ removeScriptObjects();
+ gameOver = false;
+ }
+ }
+
+ private void removeScriptObjects() {
+ BeanShell bsh = BeanShell.get();
+ for (Player p : game.getAllPlayers()) {
+ String colorName = ColorUtil.toString(p.getColor());
+ bsh.unset("p_" + colorName);
+ }
+
+ bsh.unset("game");
+ bsh.unset("map");
}

@Override
@@ -319,9 +335,9 @@
@Override
public void mousePressed(int button, int x, int y) {
if (isInputAllowed()) {
- if (button == Input.MOUSE_LEFT_BUTTON) {
- Tile cursorLocation = gameRenderer.getCursorLocation();
-
+ Tile cursorLocation = gameRenderer.getCursorLocation();
+
+ if (button == Input.MOUSE_LEFT_BUTTON) {
if (hud.isPopupVisible()) {
if (!hud.isWithinPopupMenu(x, y)) {
inputHandler.undo();
@@ -340,7 +356,11 @@
input.consumeEvent();
}
} else if (button == Input.MOUSE_RIGHT_BUTTON) {
- inputHandler.undo();
+ if (inGameContext.canUndo()) {
+ inputHandler.undo();
+ } else {
+ inputHandler.handleB(cursorLocation);
+ }
}
}
}
@@ -374,7 +394,7 @@
/**
* Input is allowed when all animations and actions are finished
*
- * @return If the gui is ready to process input
+ * @return If any input is allowed.
*/
private boolean isInputAllowed() {
return entered && gameRenderer != null &&
gameRenderer.isDyingUnitAnimationCompleted() &&

Reply all
Reply to author
Forward
0 new messages