PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/threerings/tudey/tools/TudeyTool.java

http://github.com/threerings/clyde
Java | 251 lines | 147 code | 41 blank | 63 comment | 8 complexity | 0012606eb6756c170bb81195e37737b6 MD5 | raw file
  1. //
  2. // $Id$
  3. //
  4. // Clyde library - tools for developing networked games
  5. // Copyright (C) 2005-2012 Three Rings Design, Inc.
  6. // http://code.google.com/p/clyde/
  7. //
  8. // Redistribution and use in source and binary forms, with or without modification, are permitted
  9. // provided that the following conditions are met:
  10. //
  11. // 1. Redistributions of source code must retain the above copyright notice, this list of
  12. // conditions and the following disclaimer.
  13. // 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  14. // conditions and the following disclaimer in the documentation and/or other materials provided
  15. // with the distribution.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19. // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  21. // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. package com.threerings.tudey.tools;
  26. import com.google.inject.Guice;
  27. import com.google.inject.Injector;
  28. import com.samskivert.util.Config;
  29. import com.threerings.presents.client.Client;
  30. import com.threerings.presents.dobj.DObjectManager;
  31. import com.threerings.util.Name;
  32. import com.threerings.crowd.chat.client.ChatDirector;
  33. import com.threerings.crowd.client.LocationDirector;
  34. import com.threerings.crowd.client.OccupantDirector;
  35. import com.threerings.crowd.client.PlaceView;
  36. import com.threerings.whirled.client.SceneDirector;
  37. import com.threerings.whirled.client.persist.SceneRepository;
  38. import com.threerings.whirled.data.SceneModel;
  39. import com.threerings.whirled.util.NoSuchSceneException;
  40. import com.threerings.opengl.GlCanvasTool;
  41. import com.threerings.opengl.GlView;
  42. import com.threerings.opengl.gui.Root;
  43. import com.threerings.opengl.renderer.Color4f;
  44. import com.threerings.tudey.util.TudeyContext;
  45. import com.threerings.tudey.util.TudeySceneFactory;
  46. /**
  47. * Base class for Tudey tools.
  48. */
  49. public abstract class TudeyTool extends GlCanvasTool
  50. implements TudeyContext
  51. {
  52. /**
  53. * Creates a new tool.
  54. */
  55. public TudeyTool (String msgs)
  56. {
  57. super(msgs);
  58. // create the various directors
  59. _locdir = new LocationDirector(this);
  60. _occdir = new OccupantDirector(this);
  61. _chatdir = new ChatDirector(this, "chat");
  62. // create a fake repository that stores nothing
  63. SceneRepository screp = new SceneRepository() {
  64. public SceneModel loadSceneModel (int sceneId) throws NoSuchSceneException {
  65. throw new NoSuchSceneException(sceneId);
  66. }
  67. public void storeSceneModel (SceneModel model) {
  68. // no-op
  69. }
  70. public void deleteSceneModel (int sceneId) {
  71. // no-op
  72. }
  73. };
  74. _scenedir = new SceneDirector(this, _locdir, screp, new TudeySceneFactory());
  75. // create the ui root
  76. _root = createRoot();
  77. _root.setModalShade(new Color4f(0f, 0f, 0f, 0.5f));
  78. }
  79. // documentation inherited from interface PresentsContext
  80. public Config getConfig ()
  81. {
  82. return _config;
  83. }
  84. // documentation inherited from interface PresentsContext
  85. public Client getClient ()
  86. {
  87. return _client;
  88. }
  89. // documentation inherited from interface PresentsContext
  90. public DObjectManager getDObjectManager ()
  91. {
  92. return _client.getDObjectManager();
  93. }
  94. // documentation inherited from interface CrowdContext
  95. public LocationDirector getLocationDirector ()
  96. {
  97. return _locdir;
  98. }
  99. // documentation inherited from interface CrowdContext
  100. public OccupantDirector getOccupantDirector ()
  101. {
  102. return _occdir;
  103. }
  104. // documentation inherited from interface CrowdContext
  105. public ChatDirector getChatDirector ()
  106. {
  107. return _chatdir;
  108. }
  109. // documentation inherited from interface CrowdContext
  110. public void setPlaceView (PlaceView view)
  111. {
  112. setView((GlView)view);
  113. }
  114. // documentation inherited from interface CrowdContext
  115. public void clearPlaceView (PlaceView view)
  116. {
  117. setView(null);
  118. }
  119. // documentation inherited from interface CrowdContext
  120. public SceneDirector getSceneDirector ()
  121. {
  122. return _scenedir;
  123. }
  124. // documentation inherited from interface TudeyContext
  125. public Root getRoot ()
  126. {
  127. return _root;
  128. }
  129. @Override // documentation inherited
  130. protected void initSharedManagers ()
  131. {
  132. // create the Presents client
  133. _client = new Client(null, getRunQueue());
  134. // create the tool server
  135. Injector injector = Guice.createInjector(new ToolServer.ToolModule(_client));
  136. _server = injector.getInstance(ToolServer.class);
  137. try {
  138. _server.init(injector);
  139. } catch (Exception e) {
  140. throw new RuntimeException("Failed to initialize server.", e);
  141. }
  142. // get references to the server managers
  143. _rsrcmgr = _server.getResourceManager();
  144. _cfgmgr = _server.getConfigManager();
  145. _colorpos = _server.getColorPository();
  146. }
  147. @Override // documentation inherited
  148. protected void didInit ()
  149. {
  150. super.didInit();
  151. // log on to our local server
  152. _server.startStandaloneClient(new Name("editor"));
  153. }
  154. @Override // documentation inherited
  155. protected void willShutdown ()
  156. {
  157. super.willShutdown();
  158. // log off of our local server
  159. _server.stopStandaloneClient();
  160. }
  161. @Override // documentation inherited
  162. protected void updateView (float elapsed)
  163. {
  164. if (_view != null) {
  165. _view.tick(elapsed);
  166. }
  167. _root.tick(elapsed);
  168. // call super.updateView afterwards so that the camera position will be up-to-date
  169. super.updateView(elapsed);
  170. }
  171. @Override // documentation inherited
  172. protected void compositeView ()
  173. {
  174. super.compositeView();
  175. if (_view != null) {
  176. _view.composite();
  177. }
  178. _root.composite();
  179. }
  180. /**
  181. * Sets the current view.
  182. */
  183. protected void setView (GlView view)
  184. {
  185. if (_view != null) {
  186. _view.wasRemoved();
  187. }
  188. if ((_view = view) != null) {
  189. _view.wasAdded();
  190. }
  191. }
  192. /** The tool configuration. */
  193. protected Config _config = new Config("tool");
  194. /** The Presents client. */
  195. protected Client _client;
  196. /** The tool server. */
  197. protected ToolServer _server;
  198. /** Handles requests to change location. */
  199. protected LocationDirector _locdir;
  200. /** Provides access to occupant lists. */
  201. protected OccupantDirector _occdir;
  202. /** Handles chat requests. */
  203. protected ChatDirector _chatdir;
  204. /** Handles scene access. */
  205. protected SceneDirector _scenedir;
  206. /** The user interface root. */
  207. protected Root _root;
  208. /** The current view, if any. */
  209. protected GlView _view;
  210. }