Class: LifeGame::FieldArea

Inherits:
Gtk::DrawingArea
  • Object
show all
Defined in:
lib/kaki-lifegame.rb

Constant Summary collapse

CellSize =
11
Space =
1
L =
CellSize + Space * 2
WaitChange =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ FieldArea

Returns a new instance of FieldArea.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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

Instance Attribute Details

#areahObject (readonly)

Returns the value of attribute areah.



215
216
217
# File 'lib/kaki-lifegame.rb', line 215

def areah
  @areah
end

#areawObject (readonly)

Returns the value of attribute areaw.



215
216
217
# File 'lib/kaki-lifegame.rb', line 215

def areaw
  @areaw
end

#label1=(value) ⇒ Object (writeonly)

Sets the attribute label1

Parameters:

  • value

    the value to set the attribute label1 to.



214
215
216
# File 'lib/kaki-lifegame.rb', line 214

def label1=(value)
  @label1 = value
end

#label2=(value) ⇒ Object (writeonly)

Sets the attribute label2

Parameters:

  • value

    the value to set the attribute label2 to.



214
215
216
# File 'lib/kaki-lifegame.rb', line 214

def label2=(value)
  @label2 = value
end

#time_goes=(value) ⇒ Object (writeonly)

Sets the attribute time_goes

Parameters:

  • value

    the value to set the attribute time_goes to.



214
215
216
# File 'lib/kaki-lifegame.rb', line 214

def time_goes=(value)
  @time_goes = value
end

Instance Method Details

#area_size_changeObject



361
362
363
# File 'lib/kaki-lifegame.rb', line 361

def area_size_change
  @areaw, @areah = @f.width * L, @f.height * L
end

#backObject



345
346
347
348
349
# File 'lib/kaki-lifegame.rb', line 345

def back
  @f.back
  redraw
  show_step
end

#background_renewalObject



304
305
306
307
308
# 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) ⇒ Object



351
352
353
354
355
356
357
358
359
# 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

#clearObject



272
273
274
275
276
277
# File 'lib/kaki-lifegame.rb', line 272

def clear
  @f.clear
  redraw
  @f.step_reset
  show_step
end

#each_cellObject



266
267
268
269
270
# File 'lib/kaki-lifegame.rb', line 266

def each_cell
  @f.height.times do |y|
    @f.width.times {|x| yield(x, y)}
  end
end

#fasterObject



330
331
332
333
334
335
# 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_stepObject



233
234
235
236
237
238
239
240
241
242
# 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

#gridObject



310
311
312
313
# File 'lib/kaki-lifegame.rb', line 310

def grid
  @grid = !@grid
  background_renewal
end

#load_file(file_name) ⇒ Object



296
297
298
299
300
301
302
# 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

#preserveObject



248
249
250
# File 'lib/kaki-lifegame.rb', line 248

def preserve
  @f.preserve
end

#redrawObject



262
263
264
# File 'lib/kaki-lifegame.rb', line 262

def redraw
  each_cell {|x, y| renewal_one_place(x, y)}
end

#renewal_one_place(x, y) ⇒ Object



244
245
246
# 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) ⇒ Object



227
228
229
230
231
# 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

#restoreObject



252
253
254
255
256
# File 'lib/kaki-lifegame.rb', line 252

def restore
  @f.restore
  redraw
  show_step
end

#save_file(file_name) ⇒ Object



292
293
294
# File 'lib/kaki-lifegame.rb', line 292

def save_file(file_name)
  @f.save(file_name)
end

#scatterObject



279
280
281
282
283
284
285
286
287
288
289
290
# 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) ⇒ Object



221
222
223
224
225
# 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) ⇒ Object



315
316
317
318
# File 'lib/kaki-lifegame.rb', line 315

def set_cell_color(color)
  @cell_color = color
  redraw
end

#set_color(color_name) ⇒ Object



217
218
219
# File 'lib/kaki-lifegame.rb', line 217

def set_color(color_name)
  @gc.rgb_fg_color = @color[color_name]
end

#set_wait_timeObject



337
338
339
340
341
342
343
# 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_stepObject



258
259
260
# File 'lib/kaki-lifegame.rb', line 258

def show_step
  @label1.set_text("Step : #{@f.step}")
end

#show_wait_timeObject



320
321
322
# File 'lib/kaki-lifegame.rb', line 320

def show_wait_time
  @label2.set_text("#{@wait_time} ミリ秒")
end

#slowerObject



324
325
326
327
328
# File 'lib/kaki-lifegame.rb', line 324

def slower
  @wait_time += WaitChange
  set_wait_time
  show_wait_time
end