#!/usr/bin/ruby =begin +----------------+ |~~~~~~~~~~~~~~~~| |[ ][~~~]| |Hp: | |Atk: | |Def: | |Lvl: | |Xp: | +----------------+ =end # This initializes our library require 'gtk2' require 'creature_xml' $creature = CreatureXML.new #---------------------------------------# # Event handlers #---------------------------------------# def delete_event( widget, event ) puts "delete event occurred." # Change true to false and the main window will be destroyed with # a "delete_event". return false end # Another callback def destroy( widget ) Gtk.main_quit end #---------------------------------------# # Window handlers and window decor #---------------------------------------# # This is called in all Ruby-GNOME2 applications. Gtk.init # Create a new window. window = Gtk::Window.new( Gtk::Window::TOPLEVEL ) # Sets the window title. window.set_title( "Stendhal creature browser" ) # Sets the border width of the window. window.set_border_width( 10 ) window.signal_connect( "delete_event" ) { # Called when the window is being closed delete_event( nil, nil ) } window.signal_connect( "destroy" ) { # Called if delete_event returned False destroy( nil ) } #---------------------------------------# # Actual GUI devlopment #---------------------------------------# container = Gtk::VBox.new name_directions = Gtk::Label.new("Please enter the name of the creature you wish to look up: ") name_box = Gtk::ComboBox.new(true) # true - The items in the list are only strings $creature.get_all_creatures.each_key{ |key| name_box.append_text key } name_btn = Gtk::Button.new("Update") name_image = Gtk::Image.new(Gtk::Stock::REFRESH, Gtk::IconSize::BUTTON) name_btn.set_image(name_image) name_hbox = Gtk::HBox.new stats_container = Gtk::VBox.new hp_text = Gtk::Label.new("") atk_text = Gtk::Label.new("") def_text = Gtk::Label.new("") lvl_text = Gtk::Label.new("") xp_text = Gtk::Label.new("") stats_container.pack_start hp_text stats_container.pack_start atk_text stats_container.pack_start def_text stats_container.pack_start lvl_text stats_container.pack_start xp_text container.pack_start name_directions name_hbox.pack_start name_box name_hbox.pack_start name_btn container.pack_start name_hbox container.pack_start stats_container name_btn.signal_connect("clicked") do stats_container.hide stats = $creature.get_creature_stats(name_box.active) hp_text.set_text("Health: #{stats[0]}") atk_text.set_text("Attack: #{stats[1]}") def_text.set_text("Defense: #{stats[2]}") lvl_text.set_text("Level: #{stats[5]}") xp_text.set_text("Experience gained: #{stats[4]}") stats_container.show end window.add( container ) # "show" time! container.show name_directions.show name_box.show name_btn.show name_hbox.show hp_text.show atk_text.show def_text.show lvl_text.show xp_text.show window.show # All Ruby-GNOME2 applications must have a Gtk.main. Control ends here # and waits for an event to occur ( like a key press or mouse event ). Gtk.main