Class: APNGAsmGUI::EditorWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/apngasm-gui/editor_window.rb

Instance Method Summary collapse

Constructor Details

#initialize(width = 800, height = 600) ⇒ EditorWindow

Returns a new instance of EditorWindow.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/apngasm-gui/editor_window.rb', line 7

def initialize(width = 800, height = 600)
  @play = false
  @loop_status = false
  @frames_status = false

  @builder = Gtk::Builder.new
  @builder.add_from_file(File.expand_path('../layout.glade', __FILE__))

  @window_base = @builder['editor_window']
  @window_base.set_default_size(width, height)

  $preview = @builder['preview_image']

  @frame_hbox = Gtk::Box.new(:horizontal)
  @frame_list = APNGAsmGUI::FrameList.new(@frame_hbox)

  @scrolled_window = @builder['frame_list_scrolled_window']
  @scrolled_window.add_with_viewport(@frame_hbox)

  @first_button = @builder['first_button']
  @first_button.signal_connect('clicked') do
    move_to_first
  end

  @back_button = @builder['back_button']
  @back_button.signal_connect('clicked') do
    move_to_prev
  end

  @play_button = @builder['play_button']
  @play_button.signal_connect('clicked') do
    if @play
      stop_animation
    elsif @frame_list.size > 1
      play_animation
    end
  end

  @forward_button = @builder['forward_button']
  @forward_button.signal_connect('clicked') do
    move_to_next
  end

  @last_button = @builder['last_button']
  @last_button.signal_connect('clicked') do
    move_to_last
  end

  @add_frame_button = @builder['add_frame_button']
  @add_frame_button.signal_connect('clicked') do
    add_dialog
  end

  @import_button = @builder['file_chooser']
  @import_button.add_filter(create_filter)
  @import_button.signal_connect('file_set') do |response|
    file_import(File.expand_path(response.filename))
  end

  @export_button = @builder['export_button']
  @export_button.signal_connect('clicked') do
    export_dialog if @frame_list.size > 0
  end

  @loop_checkbutton = @builder['loop_checkbutton']
  @loop_checkbutton.signal_connect('toggled') do
    @loop_status = @loop_checkbutton.active?
    @play_loop.set_active(@loop_status)
  end

  @frames_checkbutton = @builder['frames_checkbutton']
  @frames_checkbutton.signal_connect('toggled') do
    @frames_status = @frames_checkbutton.active?
  end

  @file_new = @builder['menu_file_new']
  @file_new.signal_connect('activate') do
    label = Gtk::Label.new('New File?')
    label.show
    dialog = create_confirm_dialog(label, "new")
    dialog.destroy
  end

  @file_import = @builder['menu_file_import']
  @file_import.signal_connect('activate') do
    import_dialog
  end

  @file_export = @builder['menu_file_export']
  @file_export.signal_connect('activate') do
    export_dialog if @frame_list.size > 0
  end

  @file_export_frames = @builder['menu_file_export_frames']
  @file_export_frames.signal_connect('activate') do
    @frames_checkbutton.set_active(true)
    @frames_status = true
    export_dialog if @frame_list.size > 0
  end

  @file_quit = @builder['menu_file_quit']
  @file_quit.signal_connect('activate') do
    label = Gtk::Label.new('Quit?')
    label.show
    dialog = create_confirm_dialog(label, "quit")
    dialog.destroy
  end

  @frame_add = @builder['menu_frame_add']
  @frame_add.signal_connect('activate') do
    add_dialog
  end

  @frame_next = @builder['menu_frame_next']
  @frame_next.signal_connect('activate') do
    move_to_next
  end

  @frame_prev = @builder['menu_frame_prev']
  @frame_prev.signal_connect('activate') do
    move_to_prev
  end

  @frame_last = @builder['menu_frame_last']
  @frame_last.signal_connect('activate') do
    move_to_last
  end

  @frame_first = @builder['menu_frame_first']
  @frame_first.signal_connect('activate') do
    move_to_first
  end

  @frame_delete = @builder['menu_frame_delete']
  @frame_delete.signal_connect('activate') do
    if @frame_list.size > 1
      @frame_list.delete_at(@frame_list.cur)
    end
  end

  @play_play = @builder['menu_play_play']
  @play_play.signal_connect('activate') do
    if @frame_list.size > 1 && !@play
      play_animation
    end
  end

  @play_stop = @builder['menu_play_stop']
  @play_stop.signal_connect('activate') do
    stop_animation if @play
  end

  @play_loop = @builder['menu_play_loop']
  @play_loop.set_active(@loop_status)
  @play_loop.signal_connect('toggled') do
    @loop_status = @play_loop.active?
    @loop_checkbutton.set_active(@loop_status)
  end

  @help_about = @builder['menu_help_about']
  @help_about.signal_connect('activate') do
    help_dialog
  end

  @window_base.signal_connect('destroy') do
    Gtk.main_quit
  end

  @window_base.show_all
  Gtk.main
end

Instance Method Details

#add_dialogObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/apngasm-gui/editor_window.rb', line 203

def add_dialog
  dialog = create_dialog('Open File', Gtk::FileChooser::Action::OPEN, Gtk::Stock::OPEN)
  dialog.set_select_multiple(true) 
  dialog.add_filter(create_filter)

  if dialog.run == Gtk::ResponseType::ACCEPT
    dialog.filenames.each do |filename|
      create_frame(File.expand_path(filename))
    end
    @window_base.show_all
  end

  dialog.destroy
end

#create_confirm_dialog(title, mode) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/apngasm-gui/editor_window.rb', line 264

def create_confirm_dialog(title, mode)
  dialog = Gtk::Dialog.new
  dialog.child.pack_start(title, expand: true, fill: true, padding: 30)
  dialog.add_buttons(['Yes', Gtk::ResponseType::YES], ['No', Gtk::ResponseType::NO])

  if dialog.run == Gtk::ResponseType::YES
    @import_button.filename = 'blank'
    @frame_list.delete_all if mode == 'new'
    Gtk.main_quit if mode == 'quit'
  end

  dialog
end

#create_dialog(title, action, stock) ⇒ Object



249
250
251
252
253
254
255
# File 'lib/apngasm-gui/editor_window.rb', line 249

def create_dialog(title, action, stock)
  Gtk::FileChooserDialog.new(title: title,
                             parent: @window_base,
                             action: action,
                             buttons: [[Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL],
                                      [stock, Gtk::ResponseType::ACCEPT]])
end

#create_filterObject



257
258
259
260
261
262
# File 'lib/apngasm-gui/editor_window.rb', line 257

def create_filter
  filter = Gtk::FileFilter.new
  filter.name = 'PNG File'
  filter.add_pattern('*.png')
  filter
end

#create_frame(filename) ⇒ Object



278
279
280
281
282
283
# File 'lib/apngasm-gui/editor_window.rb', line 278

def create_frame(filename)
  frame = APNGAsmGUI::Frame.new(filename, @frame_list)
  @frame_list << frame
  $preview.set_pixbuf(frame.pixbuf)
  @frame_list.frame_hbox.pack_start(frame, expand: false, fill: false, padding: 10)
end

#export_dialogObject



230
231
232
233
234
235
236
237
238
239
# File 'lib/apngasm-gui/editor_window.rb', line 230

def export_dialog
  dialog = create_dialog('Save File', Gtk::FileChooser::Action::SAVE, Gtk::Stock::SAVE)
  dialog.do_overwrite_confirmation = true

  if dialog.run == Gtk::ResponseType::ACCEPT
    file_export(File.expand_path(dialog.filename))
  end

  dialog.destroy
end

#file_export(filename) ⇒ Object



336
337
338
339
# File 'lib/apngasm-gui/editor_window.rb', line 336

def file_export(filename)
  adapter = APNGAsmGUI::Adapter.new
  adapter.export(@frame_list, filename, @frames_status)
end

#file_import(filename) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/apngasm-gui/editor_window.rb', line 323

def file_import(filename)
  adapter = APNGAsmGUI::Adapter.new
  new_frames = adapter.import(@frame_list, filename)

  new_frames.each do |frame|
    @frame_list << frame
    @frame_list.frame_hbox.pack_start(frame, expand: false, fill: false, padding: 10)
  end

  $preview.set_pixbuf(@frame_list.pixbuf(@frame_list.cur)) if @frame_list.size > 0
  @window_base.show_all
end

#help_dialogObject



241
242
243
244
245
246
247
# File 'lib/apngasm-gui/editor_window.rb', line 241

def help_dialog
  # TODO dialog message
  dialog = Gtk::MessageDialog.new(message: 'apngasm-gui is a software to create an APNG file.',
                           parent: @window_base)
  dialog.run
  dialog.destroy
end

#import_dialogObject



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/apngasm-gui/editor_window.rb', line 218

def import_dialog
  dialog = create_dialog('Import File', Gtk::FileChooser::Action::OPEN, Gtk::Stock::OPEN)
  dialog.add_filter(create_filter)

  if dialog.run == Gtk::ResponseType::ACCEPT
    file_import(File.expand_path(dialog.filename))
    @import_button.filename = dialog.filename
  end

  dialog.destroy
end

#move_to_firstObject



197
198
199
200
201
# File 'lib/apngasm-gui/editor_window.rb', line 197

def move_to_first
  if @frame_list.size > 1
    swap_frame(@frame_list.cur, 0)
  end
end

#move_to_lastObject



191
192
193
194
195
# File 'lib/apngasm-gui/editor_window.rb', line 191

def move_to_last
  if @frame_list.size > 1
    swap_frame(@frame_list.cur, @frame_list.size - 1)
  end
end

#move_to_nextObject



179
180
181
182
183
# File 'lib/apngasm-gui/editor_window.rb', line 179

def move_to_next
  if @frame_list.cur < @frame_list.size - 1
    swap_frame(@frame_list.cur, @frame_list.cur + 1)
  end
end

#move_to_prevObject



185
186
187
188
189
# File 'lib/apngasm-gui/editor_window.rb', line 185

def move_to_prev
  if @frame_list.cur != 0
    swap_frame(@frame_list.cur, @frame_list.cur - 1)
  end
end

#play_animationObject



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/apngasm-gui/editor_window.rb', line 297

def play_animation
  @play = true
  image = Gtk::Image.new(stock: Gtk::Stock::MEDIA_STOP, size: Gtk::IconSize::IconSize::BUTTON)
  @play_button.set_image(image)

  @frame_list.cur = 0
  @handle = GLib::Idle.add {
    unless @loop_status
      stop_animation if @frame_list.cur == @frame_list.size - 1
    end

    $preview.set_pixbuf(@frame_list.pixbuf(@frame_list.cur))
    @frame_list.cur - 1 < 0 ? delay_num = @frame_list.size - 1 : delay_num = @frame_list.cur - 1
    sleep(@frame_list.delay(delay_num) * 0.001)
    @frame_list.cur + 1 >= @frame_list.size ? @frame_list.cur = 0 : @frame_list.cur += 1
  }
end

#stop_animationObject



315
316
317
318
319
320
321
# File 'lib/apngasm-gui/editor_window.rb', line 315

def stop_animation
  GLib::Source.remove(@handle)

  @play = false
  image = Gtk::Image.new(stock: Gtk::Stock::MEDIA_PLAY, size: Gtk::IconSize::IconSize::BUTTON)
  @play_button.set_image(image)
end

#swap_frame(old_position, new_position) ⇒ Object



285
286
287
288
289
290
# File 'lib/apngasm-gui/editor_window.rb', line 285

def swap_frame(old_position, new_position)
  @frame_list.swap(old_position, new_position)
  @frame_list.cur = new_position
  $preview.set_pixbuf(@frame_list.pixbuf(@frame_list.cur))
  view_reload
end

#view_reloadObject



292
293
294
295
# File 'lib/apngasm-gui/editor_window.rb', line 292

def view_reload
  @frame_list.view_reload
  @window_base.show_all
end