Skip to content

Instantly share code, notes, and snippets.

@myokoym
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myokoym/10478877 to your computer and use it in GitHub Desktop.
Save myokoym/10478877 to your computer and use it in GitHub Desktop.
A game launcher by Ruby/GTK3.
require "gtk3"
require "bricks_meet_balls"
class GameButton < Gtk::Button
PROPERTIES = [
"num_of_rows",
"num_of_columns",
"num_of_balls",
"ball_images",
"brick_images",
"background_image",
"endless",
]
PROPERTIES.each do |property|
attr_accessor property
end
attr_accessor :width, :height
def initialize
super(:label => "Game Start")
signal_connect("clicked") do
Thread.new do
@game = BricksMeetBalls::Window.new(width, height)
PROPERTIES.each do |property|
value = __send__(property)
@game.__send__("#{property}=", value) if value
end
@game.show
end
end
end
end
class NumberInputEntry < Gtk::Box
def initialize(game_button, target, default=0)
super(:horizontal)
label = Gtk::Label.new
label.text = target.gsub(/_/, " ")
add(label)
entry = Gtk::Entry.new
entry.text = default.to_s
game_button.send("#{target}=", default)
entry.max_length = 3
entry.signal_connect("changed") do
text = entry.text
game_button.__send__("#{target}=", text.to_i) if /[0-9]+/ =~ text
end
pack_end(entry)
end
end
class FileChooser < Gtk::Box
def initialize(game_button, target)
super(:horizontal)
label = Gtk::Label.new
title = target.gsub(/_/, " ")
label.text = title
add(label)
chooser = Gtk::FileChooserButton.new(title, :open)
chooser.signal_connect("selection-changed") do
filename = chooser.filename
game_button.__send__("#{target}=", filename) unless filename.empty?
end
pack_end(chooser)
end
end
class EndressCheckButton < Gtk::CheckButton
def initialize(game_button, default=false)
super("endless")
game_button.endless = default
signal_connect("toggled") do
game_button.endless = (self.active? ? true : false)
end
end
end
class ControlWindow < Gtk::Window
def initialize
super
self.title = "Control Window"
signal_connect("destroy") do
Gtk.main_quit
end
vbox = Gtk::Box.new(:vertical)
add(vbox)
game_button = GameButton.new
vbox.add(game_button)
vbox.add(NumberInputEntry.new(game_button, "width", 640))
vbox.add(NumberInputEntry.new(game_button, "height", 480))
vbox.add(NumberInputEntry.new(game_button, "num_of_rows", 10))
vbox.add(NumberInputEntry.new(game_button, "num_of_columns", 8))
vbox.add(NumberInputEntry.new(game_button, "num_of_balls", 29))
vbox.add(FileChooser.new(game_button, "ball_images"))
vbox.add(FileChooser.new(game_button, "brick_images"))
vbox.add(FileChooser.new(game_button, "background_image"))
vbox.add(EndressCheckButton.new(game_button))
end
end
window = ControlWindow.new
window.show_all
Gtk.main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment