basic_gui.rb 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. ## Create some Basic GUI, this is a bit more complicated then it
  19. ## should be due to the lack of proper button-banel class and layout manager
  20. ## GUI class which holds all the GUI components and the state of them
  21. class GUI
  22. attr_reader :workspace, :gui, :tileselector, :objectselector
  23. def run()
  24. ## Enter main loop here
  25. @gui.run()
  26. end
  27. def initialize(datadir = "../data")
  28. @datadir = datadir
  29. ## Init the GUI manager
  30. @editor = Editor.new()
  31. @gui = @editor.get_gui_manager()
  32. myrect = CL_Rect.new(CL_Point.new(0, 56), CL_Size.new(665, 488+56))
  33. @editor_map = EditorMapComponent.new(myrect, @gui.get_component())
  34. @workspace = Workspace.new(myrect.get_width(), myrect.get_height())
  35. @editor_map.set_workspace(@workspace)
  36. @minimap = Minimap.new(@editor_map, CL_Rect.new(CL_Point.new(3 + myrect.left,
  37. 488+3-14 + myrect.top),
  38. CL_Size.new(794-134-16, 50)),
  39. @gui.get_component())
  40. @button_panel = ButtonPanel.new(0, 23, 800, 33, true, @gui.get_component)
  41. @button_panel.add_icon(@datadir + "/images/icons24/stock_new.png")
  42. @button_panel.add_icon(@datadir + "/images/icons24/stock_open.png", proc{ level_load() })
  43. @button_panel.add_small_icon(@datadir + "/images/icons24/downarrow.png", proc{ $controller.recent_files_menu.run() })
  44. @button_panel.add_icon(@datadir + "/images/icons24/stock_save.png", proc{ level_save() })
  45. @button_panel.add_icon(@datadir + "/images/icons24/stock_save_as.png", proc{ level_save_as() })
  46. @button_panel.add_separator()
  47. @button_panel.add_icon(@datadir + "/images/icons24/stock_copy.png")
  48. @button_panel.add_icon(@datadir + "/images/icons24/stock_paste.png")
  49. @button_panel.add_separator()
  50. @undo_icon = @button_panel.add_icon(@datadir + "/images/icons24/stock_undo.png", proc{ @workspace.get_map().undo() })
  51. @redo_icon = @button_panel.add_icon(@datadir + "/images/icons24/stock_redo.png", proc{ @workspace.get_map().redo() })
  52. @undo_icon.disable()
  53. @redo_icon.disable()
  54. @button_panel.add_separator()
  55. @minimap_icon = @button_panel.add_icon(@datadir + "/images/icons24/minimap.png", proc{ toggle_minimap() })
  56. @grid_icon = @button_panel.add_icon(@datadir + "/images/icons24/grid.png", proc{ toggle_grid() })
  57. @button_panel.add_separator()
  58. @button_panel.add_icon(@datadir + "/images/icons24/background.png")
  59. @button_panel.add_icon(@datadir + "/images/icons24/interactive.png")
  60. @button_panel.add_icon(@datadir + "/images/icons24/foreground.png")
  61. @button_panel.add_icon(@datadir + "/images/icons24/eye.png")
  62. @layer_menu = Menu.new(CL_Point.new(32*15+2, 54), @gui.get_component())
  63. @toolbar = ButtonPanel.new(0, 23+33, 33, 32*4+2, false, @gui.get_component)
  64. @paint = @toolbar.add_icon(@datadir + "/images/tools/stock-tool-pencil-22.png", proc{ $controller.set_tilemap_paint_tool() })
  65. @select = @toolbar.add_icon(@datadir + "/images/tools/stock-tool-rect-select-22.png", proc{ $controller.set_tilemap_select_tool() })
  66. @zoom = @toolbar.add_icon(@datadir + "/images/tools/stock-tool-zoom-22.png", proc{ $controller.set_zoom_tool() })
  67. @object = @toolbar.add_icon(@datadir + "/images/tools/stock-tool-clone-22.png", proc{ $controller.set_objmap_select_tool() })
  68. # $foreground_icon.set_callback(proc{ show_foreground() })
  69. # $interactive_icon.set_callback(proc{ show_interactive() })
  70. # $background_icon.set_callback(proc{ show_background() })
  71. # $eye_icon.set_callback(proc{ $layer_menu.run() })
  72. @layer_menu.add_item($mysprite, "Show all", proc{ show_all() })
  73. @layer_menu.add_item($mysprite, "Show current", proc{ show_current() })
  74. @layer_menu.add_item($mysprite, "Show only current", proc{ show_only_current() })
  75. @menu = CL_Menu.new(@gui.get_component())
  76. @menu.add_item("File/Open...", proc{ level_load() })
  77. @menu.add_item("File/Save...", proc{ level_save() })
  78. # @menu.add_item("File/Save Commands...", menu_file_save_commands)
  79. @menu.add_item("File/Save As...", proc{ level_save_as() })
  80. @menu.add_item("File/Quit", proc{ @gui.quit() })
  81. @menu.add_item("Edit/Resize", proc{ resize_level() })
  82. @menu.add_item("Edit/Resize to selection", proc{ resize_level_to_selection()})
  83. # @menu.add_item("Edit/Debug Shell", proc{ run_python()})
  84. @menu.add_item("Zoom/1:4 (25%) ", proc{ set_zoom(0.25) })
  85. @menu.add_item("Zoom/1:2 (50%) ", proc{ set_zoom(0.5) })
  86. @menu.add_item("Zoom/1:1 (100%) ", proc{ set_zoom(1.0) })
  87. @menu.add_item("Zoom/2:1 (200%) ", proc{ set_zoom(2.0) })
  88. @menu.add_item("Zoom/4:1 (400%) ", proc{ set_zoom(4.0) })
  89. @load_dialog = SimpleFileDialog.new("Load SuperTux Level", "Load", "Cancel", @gui.get_component())
  90. @load_dialog.set_filename($datadir + "levels/")
  91. @save_dialog = SimpleFileDialog.new("Save SuperTux Level as...", "Save", "Cancel", @gui.get_component())
  92. @save_dialog.set_filename($datadir + "levels/")
  93. toggle_minimap()
  94. # Init the GUI, so that button state is in sync with internal state
  95. # show_interactive()
  96. # show_current()
  97. # set_tilemap_paint_tool()
  98. @selector_window = Panel.new(CL_Rect.new(CL_Point.new(800-134, 23+33), CL_Size.new(128 + 6, 558)),
  99. @gui.get_component())
  100. @tileselector = TileSelector.new(CL_Rect.new(CL_Point.new(3, 3), CL_Size.new(128, 552)), @selector_window)
  101. @tileselector.set_tileset($tileset)
  102. @tileselector.set_scale(0.5)
  103. @tileselector.set_tiles($tileset.get_tiles())
  104. @tileselector.show(true)
  105. @objectselector = ObjectSelector.new(CL_Rect.new(0, 0, 128, 256), 42, 42, @selector_window)
  106. @objectselector.show(false)
  107. connect_v1_ObjMapObject(@objectselector.sig_drop(), proc{ on_object_drop() })
  108. # $game_objects.each do |object|
  109. # @objectselector.add_brush(ObjectBrush.new(make_sprite($datadir + object[1]),
  110. # make_metadata(object)))
  111. # end
  112. connect_v2(@editor_map.sig_on_key("f1"), proc{ |x, y| toggle_minimap()})
  113. connect_v2(@editor_map.sig_on_key("m"), proc{ |x, y| toggle_minimap()})
  114. connect_v2(@editor_map.sig_on_key("g"), proc{ |x, y| toggle_grid()})
  115. connect_v2(@editor_map.sig_on_key("4"), proc{ |x, y| toggle_display_props()})
  116. connect_v2(@editor_map.sig_on_key("3"), proc{ |x, y| show_foreground()})
  117. connect_v2(@editor_map.sig_on_key("2"), proc{ |x, y| show_interactive()})
  118. connect_v2(@editor_map.sig_on_key("1"), proc{ |x, y| show_background()})
  119. end
  120. def level_load()
  121. print "level_load\n"
  122. end
  123. def level_save()
  124. print "level_save\n"
  125. end
  126. def level_save_as()
  127. print "level_save_as\n"
  128. end
  129. def toggle_minimap()
  130. if @minimap.is_visible()
  131. @minimap.show(false)
  132. @minimap_icon.set_up()
  133. else
  134. @minimap.show(true)
  135. @minimap_icon.set_down()
  136. end
  137. end
  138. def toggle_grid()
  139. tilemap = @workspace.get_map().get_metadata().layers[0];
  140. tilemap.set_draw_grid(!tilemap.get_draw_grid())
  141. if tilemap.get_draw_grid()
  142. @grid_icon.set_down()
  143. else
  144. @grid_icon.set_up()
  145. end
  146. end
  147. def set_tilemap_paint_tool()
  148. @paint.set_down()
  149. @select.set_up()
  150. @zoom.set_up()
  151. @object.set_up()
  152. end
  153. def set_tilemap_select_tool()
  154. @paint.set_up()
  155. @select.set_down()
  156. @zoom.set_up()
  157. @object.set_up()
  158. end
  159. def set_zoom_tool()
  160. @paint.set_up()
  161. @select.set_up()
  162. @zoom.set_down()
  163. @object.set_up()
  164. end
  165. def set_objmap_select_tool()
  166. @paint.set_up()
  167. @select.set_up()
  168. @zoom.set_up()
  169. @object.set_down()
  170. end
  171. def set_zoom(zoom)
  172. gc = @editor_map.get_workspace().get_gc_state()
  173. pos = gc.get_pos()
  174. gc.set_zoom(zoom)
  175. gc.set_pos(pos)
  176. end
  177. end
  178. # EOF #