class PhantomAnimationEditor::EditorWindow

Public Class Methods

new(width = 800, height = 600) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 9
def initialize(width = 800, height = 600)
  @play = false
  @loop_status = true
  @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 = PhantomAnimationEditor::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']
  add_filter(@import_button)
  @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.active = true
  @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
    stop_animation if @play
    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

Public Instance Methods

add_dialog() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 207
def add_dialog
  dialog = create_dialog('Open File', Gtk::FileChooserAction::OPEN, Gtk::Stock::OPEN)
  dialog.set_select_multiple(true) 
  add_filter(dialog)

  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
add_filter(dialog, types = %w(png jpg jpeg gif svg)) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 263
def add_filter(dialog, types = %w(png jpg jpeg gif svg))
  dialog.add_filter(create_multiple_filter(types))
  types.each do |type|
    dialog.add_filter(create_filter(type))
  end
end
create_confirm_dialog(title, mode) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 286
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) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 255
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_filter(type) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 279
def create_filter(type)
  filter = Gtk::FileFilter.new
  filter.name = "#{type} File"
  filter.add_pattern("*.#{type}")
  filter
end
create_frame(filename) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 300
def create_frame(filename)
  frame = PhantomAnimationEditor::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
create_multiple_filter(types) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 270
def create_multiple_filter(types)
  filter = Gtk::FileFilter.new
  filter.name = 'Image File'
  types.each do |type|
    filter.add_pattern("*.#{type}")
  end
  filter
end
export_dialog() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 234
def export_dialog
  dialog = create_dialog('Save File', Gtk::FileChooser::Action::SAVE, Gtk::Stock::SAVE)
  add_filter(dialog, %w(png svg))
  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) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 359
def file_export(filename)
  adapter = PhantomAnimationEditor::Adapter.new
  adapter.export(@frame_list, filename, @frames_status, @loop_status)
end
file_import(filename) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 345
def file_import(filename)
  adapter = PhantomAnimationEditor::Adapter.new
  data = adapter.import(@frame_list, filename)

  data[:frames].each do |frame|
    @frame_list << frame
    @frame_list.frame_hbox.pack_start(frame, expand: false, fill: false, padding: 10)
  end
  @frame_list.phantom_svg.frames.concat(data[:phantom_frames])

  $preview.set_pixbuf(@frame_list.pixbuf(@frame_list.cur)) if @frame_list.size > 0
  @window_base.show_all
end
help_dialog() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 246
def help_dialog
  # TODO dialog message
  dialog = Gtk::MessageDialog.new(
    message: 'Phantom animation editor is a software to create an animation file.',
    parent: @window_base)
  dialog.run
  dialog.destroy
end
import_dialog() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 222
def import_dialog
  dialog = create_dialog('Import File', Gtk::FileChooserAction::OPEN, Gtk::Stock::OPEN)
  add_filter(dialog)

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

  dialog.destroy
end
move_to_first() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 201
def move_to_first
  if @frame_list.size > 1
    swap_frame(@frame_list.cur, 0)
  end
end
move_to_last() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 195
def move_to_last
  if @frame_list.size > 1
    swap_frame(@frame_list.cur, @frame_list.size - 1)
  end
end
move_to_next() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 183
def move_to_next
  if @frame_list.cur != nil && @frame_list.cur < @frame_list.size - 1
    swap_frame(@frame_list.cur, @frame_list.cur + 1)
  end
end
move_to_prev() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 189
def move_to_prev
  if @frame_list.cur != nil && @frame_list.cur != 0
    swap_frame(@frame_list.cur, @frame_list.cur - 1)
  end
end
play_animation() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 319
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_animation() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 337
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) click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 307
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_reload() click to toggle source
# File lib/phantom_animation_editor/editor_window.rb, line 314
def view_reload
  @frame_list.view_reload
  @window_base.show_all
end