#!/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 = ['open.swf', 'open2.swf', 'briefing.swf', 
'q1-1t.swf', 'q1-1.swf', 'a1-1.swf',
'q2-1t.swf', 'q2-1.swf', 'a2-1.swf',
'q3-1t.swf', 'q3-1.swf', 'a3-1.swf',
'q4-1t.swf', 'q4-1.swf', 'a4-1.swf',
'q5-1t.swf', 'q5-1.swf', 'a5-1.swf',
'q6-1t.swf', 'q6-1.swf', 'a6-1.swf',
'q7-1t.swf', 'q7-1.swf', 'a7-1.swf',
'q8-1t.swf', 'q8-1.swf', 'a8-1.swf',
'q9-1t.swf', 'q9-1.swf', 'a9-1.swf',
'q10-1t.swf', 'q10-1.swf', 'a10-1.swf',
'end.swf',
 '.']
set_scale(1.0)

m = SWFMovie.new
m.set_dimension(400, 400)
nextclick(files[1], m)
m.next_frame
m.save(files[0])
files.shift
m = nil

## make title
m = SWFMovie.new
m.set_dimension(400, 400)
SWFFont.font_path.push '.'
f = SWFFont.new('KochiMincho.fdb')

nextclick(files[1], m)
# wait
1.upto(8) do |i|
  m.next_frame
end
logo = m.add(SWFBitmap.new('openlogo-nd-100.jpg'));
logo.scale_to(4, 3);
16.downto(0) do |i|
  logo.add_color(16 * i, 16 * i, 16 * i);
  m.next_frame
end
# wait
1.upto(8) do |i|
  m.next_frame
end
text = centertext('Debian Trivia', 40, f)
title = m.add(text)
title.move_to(200, 200)
16.downto(0) do |i|
  title.add_color(16 * i, 16 * i, 16 * i)
  m.next_frame
end
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)
SWFFont.font_path.push '.'
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
head = lefttext('説明', 40, f)
i = m.add(head)
i.move_to(0, 10)
multilinetext([
'□ Debian にまつわるトリビア',
'□ 回答は選択式',
'  ☆ 負け抜け',
'  ☆ いわゆる天才クイズ方式',
'□ これがわかればあなたもデビルマン',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# ready

# 第一問
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

# q1
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('Install-Sizeが1桁のパッケージは', 35, f)
i = m.add(head)
i.move_to(0, 10)
head = lefttext('存在する- Yes or No?', 35, f)
i = m.add(head)
i.move_to(0, 40)
multilinetext([
'',
'Yes',
'No'
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A1: No
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)
# for release
notice = m.add(centertext("発表時はこのように書きましたが真の正解はYesです", 18, f))
notice.move_to(300,240)
text = centertext('No', 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

# 第2問
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

# q1
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('では最小のInstall-Sizeの値は?', 35, f)
i = m.add(head)
i.move_to(0, 10)
multilinetext([
'A.10',
'B.16',
'C.107',
'D.256',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A2: 10
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)
# for release
notice = m.add(centertext("発表時はこのように書きましたが真の正解は0です", 18, f))
notice.move_to(300,240)
notice = m.add(centertext("3.0r0のssleayがこれに相当します", 18, f))
notice.move_to(300,260)
text = centertext('A.10', 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

# 第3問
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

# Q3
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('woodyのdpkgを入れると/usr/binには', 35, f)
i = m.add(head)
i.move_to(0, 10)
head = lefttext('いくつのコマンドがinstallされる?', 35, f)
i = m.add(head)
i.move_to(0, 40)
multilinetext([
'',
'A.5',
'B.10',
'C.11',
'D.13',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A3: A.5
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.5', 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

# 第4問
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

# Q4
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('Branden Robinsonは誰?', 35, f)
i = m.add(head)
i.move_to(0, 10)
img1 = m.add(SWFBitmap.new('branden.jpg'))
img2 = m.add(SWFBitmap.new('mutsumi.jpg'))
img1.move_to(30, 100)
img2.move_to(330, 60)
img2.scale(2.0, 2.0) # for half.gif
t1 = m.add(centertext('A', 40, f))
t2 = m.add(centertext('B', 40, f))
t1.move_to(150, 360)
t2.move_to(450, 360)
# for release
notice = m.add(lefttext("プライバシーに配慮し一部画像を変更しています", 18, f))
notice.move_to(20,385)
m.next_frame
m.save(files[0])
files.shift

# A4: A
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', 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

# 第5問
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

# Q5
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('ではそのBrandenのIRC nickは?', 35, f)
i = m.add(head)
i.move_to(0, 10)
multilinetext([
'A.overfriend',
'B.overfiend',
'C.underfriend',
'D.hanzubon',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A5: B.overfiend
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('B.overfiend', 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

# 第6問
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

# Q6
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('Martin Michlmayrは誰?', 35, f)
i = m.add(head)
i.move_to(0, 10)
img1 = m.add(SWFBitmap.new('linus.jpg'))
img2 = m.add(SWFBitmap.new('yaegashi.jpg'))
img3 = m.add(SWFBitmap.new('tbm.jpg'))
img1.scale(0.8, 0.8)
#img2.scale(0.6, 0.6) # commented out for hitahita
img3.scale(0.5, 0.5)
img1.move_to(20,  80)
img2.move_to(220, 80)
img3.move_to(420, 80)
t1 = m.add(centertext('A', 40, f))
t2 = m.add(centertext('B', 40, f))
t3 = m.add(centertext('C', 40, f))
t1.move_to(100, 360)
t2.move_to(300, 360)
t3.move_to(500, 360)
# for release
notice = m.add(lefttext("プライバシーに配慮し一部画像を変更しています", 18, f))
notice.move_to(20,385)
m.next_frame
m.save(files[0])
files.shift

# A6: C
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('C', 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

# 第7問
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

# Q7
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('ではそのMartinはどこの国の人?', 35, f)
i = m.add(head)
i.move_to(0, 10)
multilinetext([
'A.France',
'B.Australia',
'C.Austria',
'D.Netherlands',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A7: C.Austria
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('C.Austria', 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

# 第8問
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

# Q8
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('woodyのdebian-keyringでGnuPG', 35, f)
i = m.add(head)
i.move_to(0, 10)
head = lefttext('keyringの先頭に出るのは誰?', 35, f)
i = m.add(head)
i.move_to(0, 40)
multilinetext([
'',
'A.James Troup',
'B.Wichert Akkerman',
'C.Milan Zamazal',
'D.Anthony Towns',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A8: C.Milan Zamazal
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('C.Miran Zamazal', 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

# 第9問
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

# Q9
m = SWFMovie.new
m.set_dimension(600, 400)
f = SWFFont.new('KochiMincho.fdb')
nextclick(files[1], m)
#              ('1234567890123456789012345678901234567', 35, f)
head = lefttext('ではpgp keyringでは?', 35, f)
i = m.add(head)
i.move_to(0, 10)
multilinetext([
'A.Patrick Cole',
'B.Martin Schulze',
'C.Brian May',
'D.James Troup',
], 30, f, m)
m.next_frame
m.save(files[0])
files.shift

# A9: A.Patrik Cole
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.Patrick Cole', 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

