#!ruby -Ku # -*- mode: ruby; encoding: utf-8 -*- # Last updated: <2015/01/07 20:46:25 +0900> # # DXRubyのサンプルを一覧表示して起動できるランチャースクリプト require 'dxruby' TITLE_STR = "DXRuby 1.4.1 サンプルランチャー (↑↓:カーソル移動 / Z:決定 / ESC:終了)" CUR_CHAR = ">> " $fnt = Font.new(16, "MS ゴシック", :weight => true) $fntsmall = Font.new(12, "MS ゴシック") $fnth = 18 # スクリプトファイル一覧を取得 def get_file_list(rootdir = ".") lst = {} # ディレクトリ内のファイル(ディレクトリ)一覧を取得 Dir.entries(rootdir).each do |d| next if d =~ /^\.{1,2}$/ next if d =~ /^_/ next unless File::ftype(d) == "directory" lst[d] = [] end # スクリプトファイル名の一覧を取得 lst.keys.each do |d| lst[d].push(["BACK", "フォルダ選択に戻ります。"]) filepath = File.join(d, "readme_sample.txt") scriptname = "" fg = false File.open(filepath, 'r:cp932:utf-8') do |f| f.each_line do |l| unless fg if l =~ /^([0-9a-zA-Z_\.]+)$/ # スクリプト名を取得 scriptname = $1 fg = true end else if l =~ /[ ]{4}(.+)$/ # 説明文を取得 msg = $1 lst[d].push([scriptname, msg]) scriptname = "" fg = false end end end end end return lst end # サムネイル画像を読み込み def get_thumb_list(thumb_dir = "./_screenshot/thumb") imgs = {} Dir.entries(thumb_dir).each do |d| if d =~ /\.png$/ imgs[d] = Image.load(File.join(thumb_dir, d)) end end return imgs end # 選択肢一覧を表示 def disp_list(lst, sel, msg = nil) cx, cy = 0, 8 + $fnth * (2 + sel) - 2 Window.drawBoxFill(cx, cy, Window.width / 2, cy + $fnth, [64, 96, 192]) x, y = 8, 8 Window.drawFont(x, y, TITLE_STR, $fnt) y += $fnth * 2 lst.each_with_index do |d, i| s = ((i == sel)? CUR_CHAR : " ") + d Window.drawFont(x, y, s, $fnt) y = y + $fnth end if msg != nil # 説明文を描画 col = [255, 200, 200] bgcol = [64, 64, 64] x, y = 2, Window.height - $fnth * 3 Window.drawBoxFill(0, y - 4, Window.width, Window.height, bgcol) linelen = 40 while msg.length > 0 if msg.length >= linelen Window.drawFont(x, y, msg.slice(0, linelen), $fntsmall, :color => col) msg = msg.slice(linelen, msg.length - linelen) else Window.drawFont(x, y, msg, $fntsmall, :color => col) msg = "" end y += $fnth end end end # ---------------------------------------- # メイン処理 first_pwd = Dir.pwd filelist = get_file_list() imgs = get_thumb_list() step = 0 sel_root = 0 sel_dir = "" sel_sub = 0 Window.loop do break if Input.keyPush?(K_ESCAPE) case step when 0 # ---------------------------------------- # ディレクトリの一覧を描画 lst = filelist.keys # カーソルキーの上下でカーソル移動 if Input.keyPush?(K_UP) or Input.keyPush?(K_DOWN) y = (Input.keyPush?(K_UP))? -1 : 1 sz = filelist.keys.length sel_root = (sel_root + y + sz) % sz end # Z or ENTER で決定 if Input.keyPush?(K_Z) or Input.keyPush?(K_RETURN) sel_dir = filelist.keys[sel_root] sel_sub = 0 step += 1 end # 描画 disp_list(lst, sel_root) when 1 # ---------------------------------------- # ファイルリストを描画 lst = filelist[sel_dir].map {|d| d[0]} if Input.keyPush?(K_UP) or Input.keyPush?(K_DOWN) y = (Input.keyPush?(K_UP))? -1 : 1 sz = filelist[sel_dir].length sel_sub = (sel_sub + y + sz) % sz end step += 1 if Input.keyPush?(K_Z) or Input.keyPush?(K_RETURN) thumb_name = sel_dir + "_" + lst[sel_sub] + ".png" if imgs.has_key?(thumb_name) img = imgs[thumb_name] x = 320 + (320 - img.width) / 2 y = (Window.height - img.height) / 2 Window.draw(x, y, img) end msg = filelist[sel_dir][sel_sub][1] disp_list(lst, sel_sub, msg) when 2 # ---------------------------------------- # サンプルスクリプトを実行 if sel_sub == 0 # ディレクトリ選択に戻る step = 0 else # スクリプトを実行 scriptname = filelist[sel_dir][sel_sub][0] Dir.chdir(File.join(first_pwd, sel_dir)) pid = Process.spawn("ruby #{scriptname}") Process.detach(pid) Dir.chdir(first_pwd) step -= 1 end end end exit