/src/editor/ui/Windows.as

https://github.com/initials/Ogmo-Editor · ActionScript · 385 lines · 291 code · 75 blank · 19 comment · 40 complexity · 9d16b104a830b423fc56cf691263f91f MD5 · raw file

  1. package editor.ui
  2. {
  3. import editor.*;
  4. import editor.definitions.*;
  5. import editor.tools.*;
  6. import editor.tools.object.*;
  7. import flash.display.Sprite;
  8. import flash.events.Event;
  9. import flash.text.TextField;
  10. public class Windows extends Sprite
  11. {
  12. //Windows
  13. public var windowLevelInfo:LevelInfoWindow;
  14. public var windowLayers:Window;
  15. public var windowTilesets:Window;
  16. public var windowTilesetPalette:Window;
  17. public var windowTileRect:TileRectangleWindow;
  18. public var windowObjectPalette:ObjectPaletteWindow;
  19. public var windowObjectInfo:ObjectInfoWindow;
  20. public var windowGridInfo:ObjectInfoWindow;
  21. public var windowObjectGenInfo:ObjectInfoWindow;
  22. public var windowTools:ToolWindow;
  23. public var tilePalette:TilePalette;
  24. public var windowLayersVisibilities:Sprite;
  25. public function Windows()
  26. {
  27. addEventListener( Event.ADDED_TO_STAGE, init );
  28. }
  29. private function init( e:Event = null ):void
  30. {
  31. removeEventListener( Event.ADDED_TO_STAGE, init );
  32. addEventListener( Event.REMOVED_FROM_STAGE, destroy );
  33. stage.addEventListener( Event.RESIZE, onResize );
  34. var i:int;
  35. var button:TextButton;
  36. var layer:LayerDefinition;
  37. var func:Function;
  38. var value:ValueDefinition;
  39. var offsetX:uint = 0;
  40. //Level info window
  41. windowLevelInfo = new LevelInfoWindow;
  42. addChild( windowLevelInfo );
  43. windowLevelInfo.active = true;
  44. //The Layer Window
  45. if (Ogmo.project.layers.length > 1)
  46. {
  47. offsetX = 110;
  48. windowLayers = new Window( 100, (TextButton.HEIGHT + 2) * Ogmo.project.layers.length + 3, "Layers" );
  49. windowLayers.x = 20;
  50. windowLayers.y = 20 + Window.BAR_HEIGHT;
  51. windowLayersVisibilities = new Sprite;
  52. windowLayersVisibilities.x = 80;
  53. for ( i = 0; i < Ogmo.project.layers.length; i++ )
  54. {
  55. layer = Ogmo.project.layers[ i ];
  56. button = new TextButton( 74, layer.name, buttonSetLayer );
  57. button.x = 3;
  58. button.y = 3 + (i * (TextButton.HEIGHT + 2));
  59. button.layerNum = i;
  60. windowLayers.ui.addChild( button );
  61. var visButton:LayerVisibilityButton = new LayerVisibilityButton( i );
  62. visButton.y = 3 + (22 * i);
  63. windowLayersVisibilities.addChild( visButton );
  64. }
  65. windowLayers.addChild( windowLayersVisibilities );
  66. addChild( windowLayers );
  67. windowLayers.active = true;
  68. }
  69. //The Tileset Selector Window
  70. if (Ogmo.project.tilesetsCount > 1)
  71. {
  72. windowTilesets = new Window( 80, (TextButton.HEIGHT + 2) * Ogmo.project.tilesetsCount + 3, "Tilesets" );
  73. windowTilesets.x = 20 + offsetX;
  74. windowTilesets.y = 20 + Window.BAR_HEIGHT;
  75. for ( i = 0; i < Ogmo.project.tilesets.length; i++ )
  76. {
  77. button = new TextButton( 74, Ogmo.project.tilesets[ i ].tilesetName, buttonSetTilesetPalette );
  78. button.x = 3;
  79. button.y = 3 + (i * (TextButton.HEIGHT + 2));
  80. button.tilesetNum = i;
  81. windowTilesets.ui.addChild( button );
  82. }
  83. addChild( windowTilesets );
  84. }
  85. //The Tileset Palette Window
  86. if (Ogmo.project.tilesetsCount > 0)
  87. {
  88. windowTilesetPalette = new Window( 100, 100, "Palette" );
  89. windowTilesetPalette.x = 20 + offsetX + (Ogmo.project.tilesetsCount > 1 ? 90 : 0);
  90. windowTilesetPalette.y = 20 + Window.BAR_HEIGHT;
  91. addChild( windowTilesetPalette );
  92. }
  93. //Tile rect window
  94. if (Ogmo.project.tilesetsCount > 0)
  95. {
  96. windowTileRect = new TileRectangleWindow;
  97. addChild( windowTileRect );
  98. }
  99. if (Ogmo.project.objectsCount > 0)
  100. {
  101. //The Object Palette Window
  102. windowObjectPalette = new ObjectPaletteWindow(20 + offsetX);
  103. addChild( windowObjectPalette );
  104. //The Object Info Window
  105. windowObjectInfo = new ObjectInfoWindow;
  106. addChild( windowObjectInfo );
  107. }
  108. //grid
  109. if (true)
  110. {
  111. //The Object Info Window
  112. windowGridInfo = new ObjectInfoWindow;
  113. addChild( windowGridInfo );
  114. windowGridInfo.title = "Grid Procedural Options";
  115. windowGridInfo.bodyHeight = 200;
  116. var vm:ValueModifier;
  117. vm = new EnterTextInt( 95, 0, 50, null, 0, 0, 100);
  118. windowGridInfo.ui.addChild( new Label( "Num Blocks :", 0, 5, "Left", "Center" ) );
  119. windowGridInfo.ui.addChild( vm );
  120. vm.value = 7;
  121. vm = new EnterTextInt( 95, 20, 50, null, 0, 0, 100);
  122. windowGridInfo.ui.addChild( new Label( "Min Width :", 0, 25, "Left", "Center" ) );
  123. windowGridInfo.ui.addChild( vm );
  124. vm.value = 2;
  125. vm = new EnterTextInt( 95, 40, 50, null, 0, 0, 100);
  126. windowGridInfo.ui.addChild( new Label( "Max Width :", 0, 45, "Left", "Center" ) );
  127. windowGridInfo.ui.addChild( vm );
  128. vm.value = 10;
  129. vm = new EnterTextInt( 95, 60, 50, null, 0, 0, 100);
  130. windowGridInfo.ui.addChild( new Label( "Min Height :", 0, 65, "Left", "Center" ) );
  131. windowGridInfo.ui.addChild( vm );
  132. vm.value = 1;
  133. vm = new EnterTextInt( 95, 80, 50, null, 0, 0, 100);
  134. windowGridInfo.ui.addChild( new Label( "Max Height :", 0, 85, "Left", "Center" ) );
  135. windowGridInfo.ui.addChild( vm );
  136. vm.value = 8;
  137. vm = new EnterTextInt( 95, 100, 50, null, 0, 0, 1);
  138. windowGridInfo.ui.addChild( new Label( "Walls,Floor :", 0, 105, "Left", "Center" ) );
  139. windowGridInfo.ui.addChild( vm );
  140. vm.value = 1;
  141. vm = new EnterTextInt( 95, 120, 50, null, 0, 0, 1);
  142. windowGridInfo.ui.addChild( new Label( "Whole Level :", 0, 125, "Left", "Center" ) );
  143. windowGridInfo.ui.addChild( vm );
  144. vm.value = 1;
  145. vm = new EnterTextInt( 95, 140, 50, null, 0, 0, 100);
  146. windowGridInfo.ui.addChild( new Label( "Cave Smoothing :", 0, 145, "Left", "Center" ) );
  147. windowGridInfo.ui.addChild( vm );
  148. vm.value = 6;
  149. vm = new EnterTextNum( 95, 160, 50, null, 0, 0, 1.0);
  150. windowGridInfo.ui.addChild( new Label( "Cave Wall Ratio :", 0, 165, "Left", "Center" ) );
  151. windowGridInfo.ui.addChild( vm );
  152. vm.value = 0.5;
  153. //The Object Info Window
  154. windowObjectGenInfo = new ObjectInfoWindow;
  155. addChild( windowObjectGenInfo );
  156. windowObjectGenInfo.y += 250;
  157. windowObjectGenInfo.title = "Object Procedural Options";
  158. windowObjectGenInfo.bodyHeight = 200;
  159. vm = new EnterTextInt( 95, 0, 50, null, 0, 0, 100);
  160. windowObjectGenInfo.ui.addChild( new Label( "Num Objects :", 0, 5, "Left", "Center" ) );
  161. windowObjectGenInfo.ui.addChild( vm );
  162. vm.value = 10;
  163. vm = new EnterTextInt( 95, 20, 50, null, 0, 0, 1);
  164. windowObjectGenInfo.ui.addChild( new Label( "Top :", 0, 25, "Left", "Center" ) );
  165. windowObjectGenInfo.ui.addChild( vm );
  166. vm.value = 1;
  167. vm = new EnterTextInt( 95, 40, 50, null, 0, 0, 1);
  168. windowObjectGenInfo.ui.addChild( new Label( "Right :", 0, 45, "Left", "Center" ) );
  169. windowObjectGenInfo.ui.addChild( vm );
  170. vm.value = 0;
  171. vm = new EnterTextInt( 95, 60, 50, null, 0, 0, 1);
  172. windowObjectGenInfo.ui.addChild( new Label( "Bottom :", 0, 65, "Left", "Center" ) );
  173. windowObjectGenInfo.ui.addChild( vm );
  174. vm.value = 0;
  175. vm = new EnterTextInt( 95, 80, 50, null, 0, 0, 1);
  176. windowObjectGenInfo.ui.addChild( new Label( "Left :", 0, 85, "Left", "Center" ) );
  177. windowObjectGenInfo.ui.addChild( vm );
  178. vm.value = 0;
  179. }
  180. //Tool window
  181. addChild(windowTools = new ToolWindow);
  182. for ( i = 0; i < numChildren; i++ )
  183. {
  184. (getChildAt( i ) as Window).stickToEdges( Ogmo.STAGE_DEFAULT_WIDTH, Ogmo.STAGE_DEFAULT_HEIGHT );
  185. (getChildAt( i ) as Window).enforceBounds();
  186. }
  187. }
  188. private function destroy( e:Event ):void
  189. {
  190. removeEventListener( Event.REMOVED_FROM_STAGE, destroy );
  191. stage.removeEventListener( Event.RESIZE, onResize );
  192. }
  193. /* ========================== UTILITIES ========================== */
  194. public function set mouse( to:Boolean ):void
  195. {
  196. mouseChildren = to;
  197. mouseEnabled = to;
  198. }
  199. public function setLayer( to:int ):void
  200. {
  201. //Set selected in layer window buttons
  202. if (windowLayers)
  203. {
  204. for ( var i:int = 0; i < windowLayers.ui.numChildren; i++ )
  205. {
  206. if (i == to)
  207. (windowLayers.ui.getChildAt( i ) as TextButton).selected = true;
  208. else
  209. (windowLayers.ui.getChildAt( i ) as TextButton).selected = false;
  210. }
  211. }
  212. //Activate and deactivate windows correctly
  213. if (Ogmo.level.currentLayer is TileLayer)
  214. {
  215. if (windowTilesets)
  216. windowTilesets.active = true;
  217. if (windowTilesetPalette)
  218. windowTilesetPalette.active = true;
  219. if (windowObjectPalette)
  220. windowObjectPalette.active = false;
  221. if (windowObjectInfo)
  222. windowObjectInfo.active = false;
  223. }
  224. else if (Ogmo.level.currentLayer is GridLayer)
  225. {
  226. if (windowTilesets)
  227. windowTilesets.active = false;
  228. if (windowTilesetPalette)
  229. windowTilesetPalette.active = false;
  230. if (windowObjectPalette)
  231. windowObjectPalette.active = false;
  232. if (windowObjectInfo)
  233. windowObjectInfo.active = false;
  234. }
  235. else if (Ogmo.level.currentLayer is ObjectLayer)
  236. {
  237. if (windowTilesets)
  238. windowTilesets.active = false;
  239. if (windowTilesetPalette)
  240. windowTilesetPalette.active = false;
  241. if (windowObjectPalette)
  242. windowObjectPalette.active = true;
  243. if (windowObjectInfo)
  244. windowObjectInfo.active = true;
  245. }
  246. }
  247. public function setTileset( to:int ):void
  248. {
  249. //Set selected in tilesets window buttons
  250. if (windowTilesets)
  251. {
  252. for ( var i:int = 0; i < windowTilesets.ui.numChildren; i++ )
  253. {
  254. if (i == to)
  255. (windowTilesets.ui.getChildAt( i ) as TextButton).selected = true;
  256. else
  257. (windowTilesets.ui.getChildAt( i ) as TextButton).selected = false;
  258. }
  259. }
  260. //Create the palette in the palette window
  261. if (windowTilesetPalette.ui.numChildren > 0)
  262. windowTilesetPalette.ui.removeChildAt( 0 );
  263. var t:TilePalette = new TilePalette( Ogmo.level.selTileset );
  264. t.x = 5;
  265. t.y = 5;
  266. tilePalette = t;
  267. windowTilesetPalette.ui.addChild( t );
  268. //Update the tileset rectangle
  269. windowTileRect.rectangle = Ogmo.project.tilesets[to].rectangle;
  270. }
  271. public function setObjectFolder( to:ObjectFolder ):void
  272. {
  273. Ogmo.level.selObjectFolder = to;
  274. windowObjectPalette.setFolder(to);
  275. }
  276. public function resetObjectsSelected():void
  277. {
  278. for ( var i:int = 0; i < windowObjectPalette.ui.numChildren; i++ )
  279. (windowObjectPalette.ui.getChildAt( i ) as ObjectButton).selected = false;
  280. }
  281. public function updateVisibilities():void
  282. {
  283. if (windowLayersVisibilities == null)
  284. return;
  285. for ( var i:int = 0; i < windowLayersVisibilities.numChildren; i++ )
  286. {
  287. (windowLayersVisibilities.getChildAt( i ) as LayerVisibilityButton).setImage();
  288. }
  289. }
  290. /* ========================== EVENTS ========================== */
  291. private function addedToStage( e:Event ):void
  292. {
  293. removeEventListener( Event.ADDED_TO_STAGE, addedToStage );
  294. stage.addEventListener( Event.RESIZE, onResize, false, 0, true );
  295. }
  296. private function onResize( e:Event ):void
  297. {
  298. for ( var i:int = 0; i < numChildren; i++ )
  299. (getChildAt( i ) as Window).enforceBounds();
  300. }
  301. private function buttonSetLayer( obj:TextButton ):void
  302. {
  303. Ogmo.level.setLayer( obj.layerNum );
  304. }
  305. private function buttonSetTilesetPalette( obj:TextButton ):void
  306. {
  307. Ogmo.level.setTileset( obj.tilesetNum );
  308. }
  309. }
  310. }