gui.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ## $Id$
  2. ##
  3. ## Flexlay - A Generic 2D Game Editor
  4. ## Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
  5. ##
  6. ## This program is free software: you can redistribute it and/or modify
  7. ## it under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation, either version 3 of the License, or
  9. ## (at your option) any later version.
  10. ##
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ## GNU General Public License for more details.
  15. ##
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. require "layout_component.rb"
  19. require "gui_specs.rb"
  20. ## GUI class which holds all the GUI components and the state of them
  21. class GUI
  22. attr_reader :workspace, :gui, :tileselector, :objectselector, :tilegroup_menu, :layer_menu, :recent_files_menu
  23. def run()
  24. ## Enter main loop here
  25. @gui.run()
  26. end
  27. def initialize()
  28. @gui = GUIManager.new()
  29. @components = LayoutComponent.create_from_sexpr(CL_Rect.new(0,0, $screen_width, $screen_height),
  30. SExpression.new($guilayout_spec),
  31. @gui.get_component())
  32. connect_v2_graceful($flexlay.sig_resize(), proc{|w, h|
  33. @components.set_size(w, h)
  34. })
  35. post_initalize()
  36. end
  37. def post_initalize()
  38. @editor_map = @components.get('editormap').component
  39. @workspace = Workspace.new()
  40. @editor_map.set_workspace(@workspace)
  41. @button_panel = @components.get('buttonpanel').component
  42. @button_panel.items["undo"].disable()
  43. @button_panel.items["redo"].disable()
  44. @layer_menu = Menu.new(CL_Point.new(32*15+2, 54), @gui.get_component())
  45. @layer_menu.add_item("Show all", proc{ show_all() })
  46. @layer_menu.add_item("Show current", proc{ show_current() })
  47. @layer_menu.add_item("Show only current", proc{ show_only_current() })
  48. @toolbar = ButtonPanel.new_from_spec(0, 23+33, 33, 32*4+2, false, $toolbar_spec, @gui.get_component)
  49. @menu = @components.get('menubar').component
  50. @tileselector = @components.get('tileselector').component
  51. @tileselector.set_tileset($tileset)
  52. @tileselector.set_scale(1)
  53. @tileselector.set_tiles($tileset.get_tiles)
  54. @tileselector.show(true)
  55. @objectselector = @components.get('objectselector').component
  56. @objectselector.show(false)
  57. connect_v2_ObjectBrush_Point(@objectselector.sig_drop(), proc{|brush, pos| on_object_drop(brush, pos) })
  58. @load_dialog = SimpleFileDialog.new("Load Sector", "Load", "Cancel", @gui.get_component())
  59. @load_dialog.set_filename($datadir + "levels/")
  60. @save_dialog = SimpleFileDialog.new("Save Sector as...", "Save", "Cancel", @gui.get_component())
  61. @save_dialog.set_filename($datadir + "levels/")
  62. register_keybindings($keybinding_spec)
  63. end
  64. def register_keybindings(spec)
  65. spec.each{ |(key, callback)|
  66. connect_v2(@editor_map.sig_on_key(key), callback)
  67. }
  68. end
  69. def on_map_change()
  70. puts "Mapchange"
  71. if (@workspace.get_map().undo_stack_size() > 0)
  72. @button_panel.items["undo"].enable()
  73. else
  74. @button_panel.items["undo"].disable()
  75. end
  76. if (@workspace.get_map().redo_stack_size() > 0)
  77. @button_panel.items["redo"].enable()
  78. else
  79. @button_panel.items["redo"].disable()
  80. end
  81. end
  82. def set_zoom(zoom)
  83. gc = @editor_map.get_gc_state()
  84. pos = gc.get_pos()
  85. gc.set_zoom(zoom)
  86. gc.set_pos(pos)
  87. end
  88. def set_tool_icon(tool)
  89. if tool == :tilemap_paint then @toolbar.items["paint" ].set_down() else @toolbar.items["paint" ].set_up() end
  90. if tool == :tilemap_select then @toolbar.items["select"].set_down() else @toolbar.items["select"].set_up() end
  91. if tool == :zoom then @toolbar.items["zoom" ].set_down() else @toolbar.items["zoom" ].set_up() end
  92. if tool == :object_select then @toolbar.items["object"].set_down() else @toolbar.items["object"].set_up() end
  93. if tool == :tilemap_paint then
  94. @objectselector.show(false)
  95. @tileselector.show(true)
  96. elsif tool == :object_select then
  97. @objectselector.show(true)
  98. @tileselector.show(false)
  99. end
  100. end
  101. def level_load()
  102. @load_dialog.run(proc{|filename| $controller.load_level(filename) })
  103. end
  104. def level_save()
  105. @save_dialog.run(proc{|filename| $controller.save_level(filename) })
  106. end
  107. def level_save_as()
  108. @save_dialog.run(proc{|filename| $controller.save_level(filename) })
  109. end
  110. def toggle_grid()
  111. tilemap = @workspace.get_map().get_data().layers[0];
  112. tilemap.set_draw_grid(!tilemap.get_draw_grid())
  113. if tilemap.get_draw_grid()
  114. @button_panel.items["grid"].set_down()
  115. else
  116. @button_panel.items["grid"].set_up()
  117. end
  118. end
  119. def new_level()
  120. level = @workspace.get_map().get_metadata()
  121. dialog = GenericDialog.new("Create New Sector", @gui.get_component())
  122. dialog.add_int("Width: ", 512)
  123. dialog.add_int("Height: ", 64)
  124. dialog.set_callback(proc{|w, h|
  125. Level.new_from_size(w, h).activate($gui.workspace)
  126. })
  127. end
  128. end
  129. # EOF #