Class: LifeGame::FieldArea
- Inherits:
-
Gtk::DrawingArea
- Object
- Gtk::DrawingArea
- LifeGame::FieldArea
- Defined in:
- lib/kaki-lifegame.rb
Constant Summary collapse
- CellSize =
11
- Space =
1
- L =
CellSize + Space * 2
Instance Attribute Summary collapse
-
#label ⇒ Object
writeonly
Sets the attribute label.
-
#time_goes ⇒ Object
writeonly
Sets the attribute time_goes.
Instance Method Summary collapse
- #clear ⇒ Object
- #each_cell ⇒ 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
- #show_step ⇒ Object
Constructor Details
#initialize(field) ⇒ FieldArea
Returns a new instance of FieldArea.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/kaki-lifegame.rb', line 116 def initialize(field) super() set_size_request(AreaW, AreaH) @f = field @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 {|_, v| @colormap.alloc_color(v, false, true)} @cell_color = :green @grid = false signal_connect("expose_event") do @gc = Gdk::GC.new(window) set_color(@grid ? :grid_color : :black) window.draw_rectangle(@gc, true, 0, 0, AreaW, AreaH) redraw end @time_goes = false add_events(Gdk::Event::BUTTON_PRESS_MASK | Gdk::Event::BUTTON_MOTION_MASK) signal_connect("button_press_event") do |w, e| unless @time_goes x, y = e.x.to_i / L, e.y.to_i / L c = @f.get_cell(x, y) c.zero? ? set_cell(x, y) : reset_cell(x, y) end end signal_connect('motion_notify_event') do |w, e| unless @time_goes x, y = e.x.to_i / L, e.y.to_i / L set_cell(x, y) if e.state. reset_cell(x, y) if e.state. end end Gtk.timeout_add(500) do go_on_one_step if @time_goes true end end |
Instance Attribute Details
#label=(value) ⇒ Object (writeonly)
Sets the attribute label
162 163 164 |
# File 'lib/kaki-lifegame.rb', line 162 def label=(value) @label = value end |
#time_goes=(value) ⇒ Object (writeonly)
Sets the attribute time_goes
162 163 164 |
# File 'lib/kaki-lifegame.rb', line 162 def time_goes=(value) @time_goes = value end |
Instance Method Details
#clear ⇒ Object
219 220 221 222 223 224 |
# File 'lib/kaki-lifegame.rb', line 219 def clear @f.clear redraw @f.step_reset show_step end |
#each_cell ⇒ Object
213 214 215 216 217 |
# File 'lib/kaki-lifegame.rb', line 213 def each_cell Field::Height.times do |y| Field::Width.times {|x| yield(x, y)} end end |
#go_on_one_step ⇒ Object
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/kaki-lifegame.rb', line 180 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
243 244 245 246 247 248 |
# File 'lib/kaki-lifegame.rb', line 243 def grid @grid = !@grid set_color(@grid ? :grid_color : :black) window.draw_rectangle(@gc, true, 0, 0, AreaW, AreaH) redraw end |
#load_file(file_name) ⇒ Object
237 238 239 240 241 |
# File 'lib/kaki-lifegame.rb', line 237 def load_file(file_name) @f.load(file_name) redraw show_step end |
#preserve ⇒ Object
195 196 197 |
# File 'lib/kaki-lifegame.rb', line 195 def preserve @f.preserve end |
#redraw ⇒ Object
209 210 211 |
# File 'lib/kaki-lifegame.rb', line 209 def redraw each_cell {|x, y| renewal_one_place(x, y)} end |
#renewal_one_place(x, y) ⇒ Object
191 192 193 |
# File 'lib/kaki-lifegame.rb', line 191 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
174 175 176 177 178 |
# File 'lib/kaki-lifegame.rb', line 174 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
199 200 201 202 203 |
# File 'lib/kaki-lifegame.rb', line 199 def restore @f.restore redraw show_step end |
#save_file(file_name) ⇒ Object
233 234 235 |
# File 'lib/kaki-lifegame.rb', line 233 def save_file(file_name) @f.save(file_name) end |
#scatter ⇒ Object
226 227 228 229 230 231 |
# File 'lib/kaki-lifegame.rb', line 226 def scatter 100.times do x, y = rand(Field::Width), rand(Field::Height) set_cell(x, y) if @f.get_cell(x, y).zero? end end |
#set_cell(x, y) ⇒ Object
168 169 170 171 172 |
# File 'lib/kaki-lifegame.rb', line 168 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
250 251 252 253 |
# File 'lib/kaki-lifegame.rb', line 250 def set_cell_color(color) @cell_color = color redraw end |
#set_color(color_name) ⇒ Object
164 165 166 |
# File 'lib/kaki-lifegame.rb', line 164 def set_color(color_name) @gc.rgb_fg_color = @color[color_name] end |
#show_step ⇒ Object
205 206 207 |
# File 'lib/kaki-lifegame.rb', line 205 def show_step @label.set_text("Step : #{@f.step}") end |