#!/usr/bin/ruby

require 'ming/ming'
include Ming

class SWFShape
  def draw_rect_origin(x, y)
        draw_line(x, 0)
        draw_line(0, y)
        draw_line(-x, 0)
        draw_line(0, -y)
        
        self
  end
end

# button
def nextclick(url, mov)
  s = SWFShape.new
  s.set_right_fill(s.add_fill(0xff, 0xff, 0xff))
  s.move_pen_to(0, 0)
  s.draw_rect_origin(600, 400)
  b = SWFButton.new
  b.add_shape(s, SWFBUTTON_HIT)
  nexturl = SWFAction.new("getURL('#{url}', '_level0');")
  b.set_action(nexturl)
  button = mov.add(b)
end

# title
def centertext(text, size, font)
  t = SWFText.new
  t.set_font(font)
  t.set_color(0x00, 0x80, 0x40)
  t.set_height(size)
  t.move_to(- t.get_width(text)/2, t.get_ascent/2)
  t.add_string(text)
  t
end

# テキストを横に揃える
def lefttext(text, size, font)
  t = SWFText.new
  t.set_font(font)
  t.set_color(0x00, 0x80, 0x40)
  t.set_height(size)
  t.move_to(0, t.get_ascent)
  t.add_string(text)
  t
end

# まとめて文字列を配置
def multilinetext(text, size, font, movie)
  x = 10
  y = 80
  dy = 45
  text.each do |line|
    l = lefttext(line, size, font)
    i = movie.add(l)
    i.move_to(x, y)
    y += dy
  end
end

# title
def qtitle(str, font, mov)
  h = 60
  t = SWFText.new
  t.set_font(font)
  t.set_color(0x00, 0x80, 0x40)
  t.set_height(h)
  t.move_to(- t.get_width(str)/2, t.get_ascent/2)
  t.add_string(str)
  i = mov.add(t)
  i.move_to(300, 200)
  7.downto(0) do |j|
    i.scale_to(1 + 0.5*j, 1 + 0.5*j)
    i.add_color(32 * j, 32 * j, 32 * j)
    mov.next_frame
  end
end

def showanswer(t, a, mov)
  5.downto(0) do |i|
    t.add_color(20*i, 20*i, 20*i)
    a.add_color(255, 255, 255)
    mov.next_frame
  end
  0.upto(8) do |i|
    a.add_color(255, 255, 255)
    mov.next_frame
  end
  9.downto(0) do |i|
    a.add_color(16*i, 16*i, 16*i)
    mov.next_frame
  end
end

files = [ 'q10-1t.swf', 'q10-1.swf', 'a10-1.swf',
'end.swf',
 '.']
set_scale(1.0)

# 第10問
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
qtitle('最終問題', f, m)
a = SWFAction.new("prevFrame(); play();")
m.add(a)
m.next_frame
m.save(files[0])
files.shift

# Q10
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('かつてKDEがGPL違反を起こしていた時', 35, f)
i = m.add(head)
i.move_to(0, 10)
head = lefttext('この問題のannouceを出したleaderは?', 35, f)
i = m.add(head)
i.move_to(0, 40)
multilinetext([
'',
'A.Ian Jakson',
'B.Wichert Akkerman',
'C.Bdale Garbee',
'D.Branden Robinson',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A10: A.Ian Jakson
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
text = centertext('正解', 40, f)
title = m.add(text)
title.move_to(300, 140)
text = centertext('A.Ian Jakson', 40, f)
ans = m.add(text)
ans.move_to(300, 200)
showanswer(title, ans, m)
a = SWFAction.new("prevFrame(); play();")
m.add(a)
m.next_frame
m.save(files[0])
files.shift

# 勝者決定
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
#nextclick(files[1], m)
qtitle('勝者決定!', f, m)
a = SWFAction.new("prevFrame(); play();")
m.add(a)
m.next_frame
m.save(files[0])
files.shift
