Changeset 19657

Show
Ignore:
Timestamp:
09/21/08 02:04:48 (4 months ago)
Author:
hogelog
Message:

適当に先読みするように
色々適当に書き換え

Files:
1 modified

Legend:

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

    r19648 r19657  
    2626EXTS = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.jpeg'] 
    2727 
     28FONTPATH = 'C:\Windows\Fonts\GOTHIC.TTF' 
     29FONTSIZE = 32 
     30 
    2831class SDL::Surface 
     32    def self.plain_surface(w, h, format) 
     33        SDL::Surface.new(SDL::HWSURFACE, w, h, format) 
     34    end 
    2935    def fill(color) 
    3036        self.fill_rect(0, 0, self.w, self.h, color) 
     
    3541    def scale_draw(surface, scale) 
    3642        SDL::Surface.transform_draw(surface, self, 0, scale, scale, 0, 0, 0, 0, SDL::Surface::TRANSFORM_AA) 
     43    end 
     44    def scale_transform(scale) 
     45        self.transform_surface(0, 0, scale, scale,  SDL::Surface::TRANSFORM_AA) 
    3746    end 
    3847end 
     
    6170        @entries = entries 
    6271        @table = [] 
     72        @curpage = 0 
    6373    end 
    64     def load(num) 
    65         if(@table[num]) 
    66             @table[num] 
    67         else 
    68             begin 
    69                 path = @entries[num] 
    70                 image = SDL::Surface.load(path) 
    71                 image.displayFormat 
    72                 return @table[num] = image 
    73             rescue SDL::Error 
    74                 return @table[num] = nil 
     74    def load(page) 
     75        unless @table[page] 
     76            if path = @entries[page] 
     77                begin 
     78                    image = SDL::Surface.load(path) 
     79                    xscale = 1.0 * SCREEN.w / image.w 
     80                    yscale = 2.0 * SCREEN.h / image.h 
     81                    scale = xscale < yscale ? xscale : yscale 
     82                    w = image.w * scale 
     83                    h = image.h * scale 
     84 
     85                    @table[page] = image.scale_transform(scale) 
     86                rescue SDL::Error 
     87                    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                    @table[page] = surface 
     91                end 
    7592            end 
    7693        end 
     94        @table[page] 
     95    end 
     96    def preload 
     97        self.load(@curpage==self.length-1 ? 0 : @curpage+1) 
    7798    end 
    7899    def length 
    79100        @entries.length 
    80101    end 
     102    def next_page 
     103        @curpage = @curpage==self.length-1 ? 0 : @curpage+1 
     104        self.load(@curpage) 
     105    end 
     106    def prev_page 
     107        @curpage = @curpage==0 ? self.length - 1 : @curpage-1 
     108        self.load(@curpage) 
     109    end 
    81110end 
    82111 
    83112class QuitException < Exception 
    84 end 
    85 class NextIMAGE < Exception 
    86 end 
    87 class PrevIMAGE < Exception 
    88 end 
    89 class MoveUp < Exception 
    90 end 
    91 class MoveDown < Exception 
    92113end 
    93114 
     
    99120 
    100121    SDL.init(SDL::INIT_VIDEO) 
     122    SDL::TTF.init 
    101123 
    102     SDL::Screen.open(WIDTH, HEIGHT, 0, SDL::HWSURFACE|SDL::FULLSCREEN) 
     124    DEFAULT_FONT = SDL::TTF.open(FONTPATH, FONTSIZE) 
     125 
     126    SDL::Screen.open(WIDTH, HEIGHT, 0, SDL::HWSURFACE|SDL::HWACCEL|SDL::FULLSCREEN) 
    103127    SCREEN = SDL::Screen.get 
    104  
    105     def draw(image, scale) 
    106         SCREEN.fill(0) 
    107         SCREEN.scale_draw(image, scale) 
    108         SCREEN.update 
    109  
    110         while event = SDL::Event.wait 
    111             case event 
    112             when SDL::Event2::Quit 
    113                 raise QuitException 
    114             when SDL::Event2::KeyDown 
    115                 case event.sym 
    116                 when SDL::Key::Q, SDL::Key::ESCAPE 
    117                     raise QuitException 
    118                 when SDL::Key::LEFT 
    119                     raise PrevIMAGE 
    120                 when SDL::Key::RIGHT 
    121                     raise NextIMAGE 
    122                 when SDL::Key::UP 
    123                     raise MoveUp 
    124                 when SDL::Key::DOWN 
    125                     raise MoveDown 
    126                 end 
    127             end 
    128         end 
    129     end 
    130128 
    131129    input_path = ARGV[0] 
     
    134132 
    135133    upview = true 
    136     enough = true 
    137134 
    138135    pages = Pages.new(entries) 
     136    image = pages.load(curpage) 
    139137 
    140138    loop do 
    141         begin 
    142             image = pages.load(curpage) 
     139        copy = image 
     140        unless image.h <= SCREEN.h 
     141            h = upview ? 0 : image.h - SCREEN.h 
     142            copy = image.copy_rect(0, h, SCREEN.w, SCREEN.h) 
     143        end 
    143144 
    144             xscale = 1.0 * SCREEN.w / image.w 
    145             yscale = 2.0 * SCREEN.h / image.h 
    146             scale = xscale < yscale ? xscale : yscale 
     145        SCREEN.fill(0) 
     146        SCREEN.put(copy, 0, 0) 
     147        SCREEN.update 
     148        pages.preload 
    147149 
    148             enough = image.h*scale < SCREEN.h 
    149  
    150             if enough 
    151                 draw(image, xscale) 
    152             else 
    153                 h = SCREEN.h / scale 
    154                 surface = image.copy_rect(0, upview ? 0 : image.h-h, image.w, h) 
    155                 draw(surface, xscale) 
     150        case event = SDL::Event.wait 
     151        when SDL::Event::Quit 
     152            raise QuitException 
     153        when SDL::Event::KeyDown 
     154            case event.sym 
     155            when SDL::Key::Q, SDL::Key::ESCAPE 
     156                raise QuitException 
     157            when SDL::Key::LEFT 
     158                image = pages.prev_page 
     159                upview = true 
     160            when SDL::Key::RIGHT 
     161                image = pages.next_page 
     162                upview = true 
     163            when SDL::Key::UP 
     164                if upview || image.h <= SCREEN.h 
     165                    upview = false 
     166                    image, scale, enough = pages.prev_page 
     167                else 
     168                    upview = true 
     169                end 
     170            when SDL::Key::DOWN 
     171                if !upview || image.h <= SCREEN.h 
     172                    upview = true 
     173                    image = pages.next_page 
     174                else 
     175                    upview = false 
     176                end 
    156177            end 
    157  
    158         rescue MoveUp 
    159             if enough || upview 
    160                 upview = false 
    161                 curpage = curpage==0 ? pages.length - 1 : curpage-1 
    162             else 
    163                 upview = true 
    164             end 
    165         rescue MoveDown 
    166             if enough || !upview 
    167                 upview = true 
    168                 curpage = curpage==pages.length-1 ? 0 : curpage+1 
    169             else 
    170                 upview = false 
    171             end 
    172         rescue PrevIMAGE 
    173             curpage = curpage==0 ? pages.length - 1 : curpage-1 
    174         rescue NextIMAGE 
    175             curpage = curpage==pages.length-1 ? 0 : curpage+1 
    176178        end 
    177179    end 
    178  
    179180rescue SDL::Error => error 
    180181    puts error.message 
     
    184185end 
    185186 
    186 # vim: set sw=4 ts=4 et fenc=utf-8: 
     187# vim: set sw=4 ts=4 et: