class LifeGame::FieldArea

Constants

CellSize
L
Space
WaitChange

Attributes

areah[R]
areaw[R]
label1[W]
label2[W]
time_goes[W]

Public Class Methods

new(field) click to toggle source
Calls superclass method
# File lib/kaki-lifegame.rb, line 168
def initialize(field)
  super()
  
  @f = field
  
  @areaw, @areah = @f.width * L, @f.height * L
  set_size_request(@areaw, @areah)
  
  @colormap = Gdk::Colormap.system
  @color = {black:      Gdk::Color.new(0, 0, 0),
            green:      Gdk::Color.new(0xc784, 0xffff, 0x52c4),
            orange:     Gdk::Color.new(0xffff, 0xbb85, 0xf7a),
            grid_color: Gdk::Color.new(0x4b79, 0x4b79, 0x4b79)}
  @color.each_value {|v| @colormap.alloc_color(v, false, true)}
  @cell_color = :green
  
  @grid = false
  @wait_time = 500
  
  signal_connect("expose_event") do
    @gc = Gdk::GC.new(window)
    background_renewal
  end
  
  @time_goes = false
  add_events(Gdk::Event::BUTTON_PRESS_MASK | Gdk::Event::BUTTON_MOTION_MASK)
  
  event_to_xy = ->(e) {[e.x.to_i / L, e.y.to_i / L]}
  
  signal_connect("button_press_event") do |w, e|
    unless @time_goes
      x, y = event_to_xy[e]
      @f.get_cell(x, y).zero? ? set_cell(x, y) : reset_cell(x, y)
    end
  end
  
  signal_connect('motion_notify_event') do |w, e|
    unless @time_goes
      x, y = event_to_xy[e]
      set_cell(x, y)   if e.state.button1_mask?
      reset_cell(x, y) if e.state.button3_mask?
    end
  end
  
  set_wait_time
end

Public Instance Methods

area_size_change() click to toggle source
# File lib/kaki-lifegame.rb, line 361
def area_size_change
  @areaw, @areah = @f.width * L, @f.height * L
end
back() click to toggle source
# File lib/kaki-lifegame.rb, line 345
def back
  @f.back
  redraw
  show_step
end
background_renewal() click to toggle source
# File lib/kaki-lifegame.rb, line 304
def background_renewal
  set_color(@grid ? :grid_color : :black)
  window.draw_rectangle(@gc, true, 0, 0, @areaw, @areah)
  redraw
end
change_window_size(size) click to toggle source
# File lib/kaki-lifegame.rb, line 351
def change_window_size(size)
  if @f.change_window_size(size)
    area_size_change
    show_step
    true
  else
    false
  end
end
clear() click to toggle source
# File lib/kaki-lifegame.rb, line 272
def clear
  @f.clear
  redraw
  @f.step_reset
  show_step
end
each_cell() { |x, y| ... } click to toggle source
# File lib/kaki-lifegame.rb, line 266
def each_cell
  @f.height.times do |y|
    @f.width.times {|x| yield(x, y)}
  end
end
faster() click to toggle source
# File lib/kaki-lifegame.rb, line 330
def faster
  @wait_time -= WaitChange
  @wait_time = 50 if @wait_time < 50
  set_wait_time
  show_wait_time
end
go_on_one_step() click to toggle source
# File lib/kaki-lifegame.rb, line 233
def go_on_one_step
  @f.next
  
  each_cell do |x, y|
    renewal_one_place(x, y) if @f.renewal?(x, y)
  end
  
  @f.count
  show_step
end
grid() click to toggle source
# File lib/kaki-lifegame.rb, line 310
def grid
  @grid = !@grid
  background_renewal
end
load_file(file_name) click to toggle source
# File lib/kaki-lifegame.rb, line 296
def load_file(file_name)
  is_change = @f.load(file_name)
  area_size_change if is_change
  redraw
  show_step
  return is_change
end
preserve() click to toggle source
# File lib/kaki-lifegame.rb, line 248
def preserve
  @f.preserve
end
redraw() click to toggle source
# File lib/kaki-lifegame.rb, line 262
def redraw
  each_cell {|x, y| renewal_one_place(x, y)}
end
renewal_one_place(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 244
def renewal_one_place(x, y)
  @f.get_cell(x, y).zero? ? reset_cell(x, y) : set_cell(x, y)
end
reset_cell(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 227
def reset_cell(x, y)
  set_color(:black)
  @f.reset_cell(x, y)
  window.draw_rectangle(@gc, true, L * x + Space, L * y + Space, CellSize, CellSize)
end
restore() click to toggle source
# File lib/kaki-lifegame.rb, line 252
def restore
  @f.restore
  redraw
  show_step
end
save_file(file_name) click to toggle source
# File lib/kaki-lifegame.rb, line 292
def save_file(file_name)
  @f.save(file_name)
end
scatter() click to toggle source
# File lib/kaki-lifegame.rb, line 279
def scatter
  co1 = co2 = 0
  while co1 < 100 and co2 < 5000
    x, y = rand(@f.width), rand(@f.height)
    if @f.get_cell(x, y).zero?
      set_cell(x, y)
      co1 += 1
    else
      co2 += 1
    end
  end
end
set_cell(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 221
def set_cell(x, y)
  set_color(@cell_color)
  @f.set_cell(x, y)
  window.draw_rectangle(@gc, true, L * x + Space, L * y + Space, CellSize, CellSize)
end
set_cell_color(color) click to toggle source
# File lib/kaki-lifegame.rb, line 315
def set_cell_color(color)
  @cell_color = color
  redraw
end
set_color(color_name) click to toggle source
# File lib/kaki-lifegame.rb, line 217
def set_color(color_name)
  @gc.rgb_fg_color = @color[color_name]
end
set_wait_time() click to toggle source
# File lib/kaki-lifegame.rb, line 337
def set_wait_time
  Gtk.timeout_remove(@timer_id) if @timer_id
  @timer_id = Gtk.timeout_add(@wait_time) do
    go_on_one_step if @time_goes
    true
  end
end
show_step() click to toggle source
# File lib/kaki-lifegame.rb, line 258
def show_step
  @label1.set_text("Step : #{@f.step}")
end
show_wait_time() click to toggle source
# File lib/kaki-lifegame.rb, line 320
def show_wait_time
  @label2.set_text("#{@wait_time} ミリ秒")
end
slower() click to toggle source
# File lib/kaki-lifegame.rb, line 324
def slower
  @wait_time += WaitChange
  set_wait_time
  show_wait_time
end