Class: LifeGame::FieldArea
- Inherits:
-
Gtk::DrawingArea
- Object
- Gtk::DrawingArea
- LifeGame::FieldArea
- Defined in:
- lib/kaki-lifegame.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#areah ⇒ Object
readonly
Returns the value of attribute areah.
-
#areaw ⇒ Object
readonly
Returns the value of attribute areaw.
-
#label1 ⇒ Object
writeonly
Sets the attribute label1.
-
#label2 ⇒ Object
writeonly
Sets the attribute label2.
-
#time_goes ⇒ Object
writeonly
Sets the attribute time_goes.
Instance Method Summary collapse
- #area_size_change ⇒ Object
- #back ⇒ Object
- #background_renewal ⇒ Object
- #change_window_size(size) ⇒ Object
- #clear ⇒ Object
- #each_cell ⇒ Object
- #faster ⇒ Object
- #go_on_one_step ⇒ Object
- #grid ⇒ Object
-
#initialize(field) ⇒ FieldArea
constructor
A new instance of FieldArea.
- #load_file(file_name) ⇒ Object
- #preserve ⇒ Object
- #redraw ⇒ Object
- #renewal_one_place(x, y) ⇒ Object
- #reset_cell(x, y) ⇒ Object
- #restore ⇒ Object
- #save_file(file_name) ⇒ Object
- #scatter ⇒ Object
- #set_cell(x, y) ⇒ Object
- #set_cell_color(color) ⇒ Object
- #set_color(color_name) ⇒ Object
- #set_wait_time ⇒ Object
- #show_step ⇒ Object
- #show_wait_time ⇒ Object
- #slower ⇒ Object
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. reset_cell(x, y) if e.state. end end set_wait_time end |
Instance Attribute Details
#areah ⇒ Object (readonly)
Returns the value of attribute areah.
215 216 217 |
# File 'lib/kaki-lifegame.rb', line 215 def areah @areah end |
#areaw ⇒ Object (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
214 215 216 |
# File 'lib/kaki-lifegame.rb', line 214 def label1=(value) @label1 = value end |
#label2=(value) ⇒ Object (writeonly)
Sets the attribute label2
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
214 215 216 |
# File 'lib/kaki-lifegame.rb', line 214 def time_goes=(value) @time_goes = value end |
Instance Method Details
#area_size_change ⇒ Object
361 362 363 |
# File 'lib/kaki-lifegame.rb', line 361 def area_size_change @areaw, @areah = @f.width * L, @f.height * L end |
#back ⇒ Object
345 346 347 348 349 |
# File 'lib/kaki-lifegame.rb', line 345 def back @f.back redraw show_step end |
#background_renewal ⇒ Object
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 |
#clear ⇒ Object
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_cell ⇒ Object
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 |
#faster ⇒ Object
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_step ⇒ Object
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 |
#grid ⇒ Object
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 |
#preserve ⇒ Object
248 249 250 |
# File 'lib/kaki-lifegame.rb', line 248 def preserve @f.preserve end |
#redraw ⇒ Object
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 |
#restore ⇒ Object
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 |
#scatter ⇒ Object
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_time ⇒ Object
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_step ⇒ Object
258 259 260 |
# File 'lib/kaki-lifegame.rb', line 258 def show_step @label1.set_text("Step : #{@f.step}") end |
#show_wait_time ⇒ Object
320 321 322 |
# File 'lib/kaki-lifegame.rb', line 320 def show_wait_time @label2.set_text("#{@wait_time} ミリ秒") end |
#slower ⇒ Object
324 325 326 327 328 |
# File 'lib/kaki-lifegame.rb', line 324 def slower @wait_time += WaitChange set_wait_time show_wait_time end |