Changeset 19701 for lang/ruby

Show
Ignore:
Timestamp:
09/22/08 03:11:32 (2 months ago)
Author:
hogelog
Message:

ディレクトリ移動機能追加

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/misc/fitview.rb

    r19657 r19701  
    1919# 
    2020 
     21require 'kconv' 
    2122require 'sdl' 
    2223 
     
    2627EXTS = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.jpeg'] 
    2728 
    28 FONTPATH = 'C:\Windows\Fonts\GOTHIC.TTF' 
    29 FONTSIZE = 32 
     29FONTS_PATH = ['c:\hoge\sys\mplus_j12r.bdf', 'c:\hoge\sys\mplus_f12r.bdf'] 
     30FONTSIZE = 12 
    3031 
    3132class SDL::Surface 
     
    6162    if entries.length==0 
    6263        puts "no image: #{File.expand_path('.')}" 
    63         raise QuitException 
     64        raise SelectDirectoryEvent 
    6465    end 
    6566 
     
    8586                    @table[page] = image.scale_transform(scale) 
    8687                rescue SDL::Error 
     88                    text = "cannot open #{path}" 
    8789                    surface = SDL::Surface.plain_surface(SCREEN.w, SCREEN.h, SCREEN.format) 
    88                     text = "cannot open #{path}" 
    89                     DEFAULT_FONT.draw_solid_utf8(surface, text, 0, 0, 255, 255, 255) 
     90                    draw_text(surface, text, 0, 0) 
    9091                    @table[page] = surface 
    9192                end 
     
    101102    end 
    102103    def next_page 
    103         @curpage = @curpage==self.length-1 ? 0 : @curpage+1 
     104        if @curpage==self.length-1 
     105            raise SelectDirectoryEvent 
     106        end 
     107        @curpage = @curpage+1 
    104108        self.load(@curpage) 
    105109    end 
    106110    def prev_page 
    107         @curpage = @curpage==0 ? self.length - 1 : @curpage-1 
     111        if @curpage==0 
     112            raise SelectDirectoryEvent 
     113        end 
     114        @curpage = @curpage-1 
    108115        self.load(@curpage) 
    109116    end 
    110117end 
    111  
    112 class QuitException < Exception 
    113 end 
    114  
    115 begin 
    116     if ARGV.length==0 
    117         puts 'no input' 
    118         raise QuitException 
    119     end 
    120  
    121     SDL.init(SDL::INIT_VIDEO) 
    122     SDL::TTF.init 
    123  
    124     DEFAULT_FONT = SDL::TTF.open(FONTPATH, FONTSIZE) 
    125  
    126     SDL::Screen.open(WIDTH, HEIGHT, 0, SDL::HWSURFACE|SDL::HWACCEL|SDL::FULLSCREEN) 
    127     SCREEN = SDL::Screen.get 
    128  
    129     input_path = ARGV[0] 
     118def fitview(input_path) 
    130119    basename = File.basename(input_path) 
    131120    entries, curpage = open_path(input_path) 
     
    164153                if upview || image.h <= SCREEN.h 
    165154                    upview = false 
    166                     image, scale, enough = pages.prev_page 
     155                    image = pages.prev_page 
    167156                else 
    168157                    upview = true 
     
    178167        end 
    179168    end 
     169end 
     170 
     171def dirselect 
     172    dirs = Dir.entries('.').reject{|file| !File.directory?(file)} 
     173    select = 0 
     174    loop do 
     175        SCREEN.fill(0) 
     176        SCREEN.draw_rect(0, select*FONTSIZE, SCREEN.w, FONTSIZE, 255, true, 100) 
     177        dirs.each_with_index{|dir, i| 
     178            draw_text(SCREEN, dir, 0, i*FONTSIZE) 
     179        } 
     180        SCREEN.update 
     181        case event = SDL::Event.wait 
     182        when SDL::Event::Quit 
     183            raise QuitException 
     184        when SDL::Event::KeyDown 
     185            case event.sym 
     186            when SDL::Key::Q, SDL::Key::ESCAPE 
     187                raise QuitException 
     188            when SDL::Key::LEFT, SDL::Key::RIGHT 
     189                return dirs[select] 
     190            when SDL::Key::UP 
     191                select = select==0? dirs.length-1 : select-1 
     192            when SDL::Key::DOWN 
     193                select = select==dirs.length-1? 0 : select+1 
     194            end 
     195        end 
     196    end 
     197end 
     198def draw_text(surface, text, x, y) 
     199    DEFAULT_FONT.put(surface, text.tojis, 0, y, 255, 255, 255) 
     200end 
     201 
     202class SelectDirectoryEvent < Exception 
     203end 
     204class QuitException < Exception 
     205end 
     206 
     207begin 
     208    if ARGV.length==0 
     209        puts 'no input' 
     210        raise QuitException 
     211    end 
     212 
     213    SDL.init(SDL::INIT_VIDEO) 
     214 
     215    DEFAULT_FONT = SDL::Kanji.open(FONTS_PATH[0], FONTSIZE) 
     216    FONTS_PATH[1..-1].each{|font| 
     217        DEFAULT_FONT.add(font) 
     218    } 
     219 
     220    SDL::Screen.open(WIDTH, HEIGHT, 0, SDL::HWSURFACE|SDL::HWACCEL|SDL::FULLSCREEN) 
     221    SCREEN = SDL::Screen.get 
     222 
     223    image_path = ARGV[0] 
     224    loop do 
     225        begin 
     226            fitview(image_path) 
     227        rescue SelectDirectoryEvent 
     228            while dir = dirselect 
     229                if dir == '.' 
     230                    image_path = '.' 
     231                    break 
     232                else 
     233                    Dir.chdir(dir) 
     234                end 
     235            end 
     236        end 
     237    end 
     238 
    180239rescue SDL::Error => error 
    181240    puts error.message