Class: GrayScott::Controller

Inherits:
Object
  • Object
show all
Includes:
Color, XumoShortHand
Defined in:
lib/gray_scott/controller.rb,
lib/gray_scott/controller/aboutdialog.rb

Defined Under Namespace

Classes: AboutDialog

Constant Summary

Constants included from XumoShortHand

XumoShortHand::SFloat, XumoShortHand::UInt8

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Color

#blue, #colorize, #grayscale, #green, #hsv2rgb, #red, #reverse_blue, #reverse_green, #reverse_red, #uInt8_dstack

Constructor Details

#initialize(dir, height: 256, width: 256) ⇒ Controller

Returns a new instance of Controller.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gray_scott/controller.rb', line 11

def initialize(dir, height: 256, width: 256)
  @resource_dir = dir
  @height = height
  @width = width
  @model = Model.new(height: height, width: width)
  @show_u = false
  @color = 'colorful'
  @frames = 5
  @msec = 40

  builder = Gtk::Builder.new
  builder.add_from_file File.join(resource_dir, 'gray_scott.glade')

  %w[win execute_button gimage legend_image uv_combobox pen_density pen_radius].each do |s|
    instance_variable_set('@' + s, builder.get_object(s))
  end

  builder.connect_signals { |handler| method(handler) }

  @win.show_all # window
  on_clear_clicked
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



9
10
11
# File 'lib/gray_scott/controller.rb', line 9

def color
  @color
end

#heightObject

Returns the value of attribute height.



9
10
11
# File 'lib/gray_scott/controller.rb', line 9

def height
  @height
end

#modelObject

Returns the value of attribute model.



9
10
11
# File 'lib/gray_scott/controller.rb', line 9

def model
  @model
end

#resource_dirObject

Returns the value of attribute resource_dir.



9
10
11
# File 'lib/gray_scott/controller.rb', line 9

def resource_dir
  @resource_dir
end

#widthObject

Returns the value of attribute width.



9
10
11
# File 'lib/gray_scott/controller.rb', line 9

def width
  @width
end

Instance Method Details

#create_pixbuf(ar) ⇒ Object



163
164
165
166
167
168
# File 'lib/gray_scott/controller.rb', line 163

def create_pixbuf(ar)
  data = ar.to_string
  height, width = ar.shape
  pixbuf = GdkPixbuf::Pixbuf.new data: data, width: width, height: height
  pixbuf.scale_simple 512, 512, :bilinear
end

#displayObject



54
55
56
# File 'lib/gray_scott/controller.rb', line 54

def display
  @gimage.pixbuf = create_pixbuf(colorize((@show_u ? model.u : model.v), @color))
end

#display_legendObject



58
59
60
61
62
63
64
# File 'lib/gray_scott/controller.rb', line 58

def display_legend
  legend = (SFloat.new(1, 512).seq * SFloat.ones(16, 1)) / 512.0
  data = colorize(legend, @color)
  string = data.to_string
  pixbuf = GdkPixbuf::Pixbuf.new data: string, width: 512, height: 16
  @legend_image.pixbuf = pixbuf
end

#doing_now?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/gray_scott/controller.rb', line 170

def doing_now?
  @doing_now
end

#executeObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/gray_scott/controller.rb', line 74

def execute
  @doing_now = true
  GLib::Timeout.add @msec do
    @frames.times do
      model.update
    end
    display
    @doing_now
  end
end

#main_quitObject



132
133
134
# File 'lib/gray_scott/controller.rb', line 132

def main_quit
  Gtk.main_quit
end

#on_clear_clickedObject



136
137
138
139
140
# File 'lib/gray_scott/controller.rb', line 136

def on_clear_clicked
  model.clear
  display_legend
  display
end

#on_color_combobox_changed(w) ⇒ Object



147
148
149
150
151
# File 'lib/gray_scott/controller.rb', line 147

def on_color_combobox_changed(w)
  @color = w.active_text
  display_legend
  display unless doing_now?
end

#on_convert_clickedObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gray_scott/controller.rb', line 119

def on_convert_clicked
  stop if doing_now?
  dialog = Gtk::FileChooserDialog.new(title: 'Save PNG image',
                                      action: :save,
                                      buttons: [i[save accept], i[cancel cancel]])
  dialog.do_overwrite_confirmation = true
  if dialog.run == :accept
    filename = dialog.filename
    @gimage.pixbuf.save(filename, :png)
  end
  dialog.destroy
end

#on_execute_toggled(widget) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/gray_scott/controller.rb', line 66

def on_execute_toggled(widget)
  if widget.active?
    execute
  else
    @doing_now = false
  end
end

#on_f_changed(f) ⇒ Object



38
39
40
# File 'lib/gray_scott/controller.rb', line 38

def on_f_changed(f)
  model.f = f.value
end

#on_frames_changed(frames) ⇒ Object



46
47
48
# File 'lib/gray_scott/controller.rb', line 46

def on_frames_changed(frames)
  @frames = frames.value.to_i
end

#on_k_changed(k) ⇒ Object



42
43
44
# File 'lib/gray_scott/controller.rb', line 42

def on_k_changed(k)
  model.k = k.value
end

#on_motion(_widget, e) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/gray_scott/controller.rb', line 153

def on_motion(_widget, e)
  x = e.x * width / 512
  y = e.y * height / 512
  r = @pen_radius.value
  if x > r && y > r && x < (width - 1 - r) && y < (height - 1 - r)
    model.v[(y - r)..(y + r), (x - r)..(x + r)] = @pen_density.value
  end
  display unless doing_now?
end

#on_msec_changed(msec) ⇒ Object



50
51
52
# File 'lib/gray_scott/controller.rb', line 50

def on_msec_changed(msec)
  @msec = msec.value.to_i
end

#on_open_clickedObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gray_scott/controller.rb', line 90

def on_open_clicked
  stop if doing_now?
  dialog = Gtk::FileChooserDialog.new(title: 'Open Gray-Scott Model',
                                      action: :open,
                                      buttons: [i[open accept], i[cancel cancel]])
  if dialog.run == :accept
    filename = dialog.filename
    str = File.read(filename)
    # TODO: check model
    @model = Marshal.load(str)
    display
  end
  dialog.destroy
end

#on_save_clickedObject



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gray_scott/controller.rb', line 105

def on_save_clicked
  stop if doing_now?
  dialog = Gtk::FileChooserDialog.new(title: 'Save Gray-Scott Model',
                                      action: :save,
                                      buttons: [i[save accept], i[cancel cancel]])
  dialog.do_overwrite_confirmation = true
  if dialog.run == :accept
    filename = dialog.filename
    str = Marshal.dump(model)
    File.write(filename, str)
  end
  dialog.destroy
end

#on_uv_combobox_changed(w) ⇒ Object



142
143
144
145
# File 'lib/gray_scott/controller.rb', line 142

def on_uv_combobox_changed(w)
  @show_u = w.active_text == 'U'
  display unless doing_now?
end

#show_aboutObject



34
35
36
# File 'lib/gray_scott/controller.rb', line 34

def show_about
  AboutDialog.new resource_dir
end

#stopObject



85
86
87
88
# File 'lib/gray_scott/controller.rb', line 85

def stop
  @doing_now = false
  @execute_button.active = false
end