- Timestamp:
- 09/21/08 02:04:48 (2 months ago)
- Files:
-
- 1 modified
-
lang/ruby/misc/fitview.rb (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/misc/fitview.rb
r19648 r19657 26 26 EXTS = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.jpeg'] 27 27 28 FONTPATH = 'C:\Windows\Fonts\GOTHIC.TTF' 29 FONTSIZE = 32 30 28 31 class SDL::Surface 32 def self.plain_surface(w, h, format) 33 SDL::Surface.new(SDL::HWSURFACE, w, h, format) 34 end 29 35 def fill(color) 30 36 self.fill_rect(0, 0, self.w, self.h, color) … … 35 41 def scale_draw(surface, scale) 36 42 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) 37 46 end 38 47 end … … 61 70 @entries = entries 62 71 @table = [] 72 @curpage = 0 63 73 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 75 92 end 76 93 end 94 @table[page] 95 end 96 def preload 97 self.load(@curpage==self.length-1 ? 0 : @curpage+1) 77 98 end 78 99 def length 79 100 @entries.length 80 101 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 81 110 end 82 111 83 112 class QuitException < Exception 84 end85 class NextIMAGE < Exception86 end87 class PrevIMAGE < Exception88 end89 class MoveUp < Exception90 end91 class MoveDown < Exception92 113 end 93 114 … … 99 120 100 121 SDL.init(SDL::INIT_VIDEO) 122 SDL::TTF.init 101 123 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) 103 127 SCREEN = SDL::Screen.get 104 105 def draw(image, scale)106 SCREEN.fill(0)107 SCREEN.scale_draw(image, scale)108 SCREEN.update109 110 while event = SDL::Event.wait111 case event112 when SDL::Event2::Quit113 raise QuitException114 when SDL::Event2::KeyDown115 case event.sym116 when SDL::Key::Q, SDL::Key::ESCAPE117 raise QuitException118 when SDL::Key::LEFT119 raise PrevIMAGE120 when SDL::Key::RIGHT121 raise NextIMAGE122 when SDL::Key::UP123 raise MoveUp124 when SDL::Key::DOWN125 raise MoveDown126 end127 end128 end129 end130 128 131 129 input_path = ARGV[0] … … 134 132 135 133 upview = true 136 enough = true137 134 138 135 pages = Pages.new(entries) 136 image = pages.load(curpage) 139 137 140 138 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 143 144 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 147 149 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 156 177 end 157 158 rescue MoveUp159 if enough || upview160 upview = false161 curpage = curpage==0 ? pages.length - 1 : curpage-1162 else163 upview = true164 end165 rescue MoveDown166 if enough || !upview167 upview = true168 curpage = curpage==pages.length-1 ? 0 : curpage+1169 else170 upview = false171 end172 rescue PrevIMAGE173 curpage = curpage==0 ? pages.length - 1 : curpage-1174 rescue NextIMAGE175 curpage = curpage==pages.length-1 ? 0 : curpage+1176 178 end 177 179 end 178 179 180 rescue SDL::Error => error 180 181 puts error.message … … 184 185 end 185 186 186 # vim: set sw=4 ts=4 et fenc=utf-8:187 # vim: set sw=4 ts=4 et:
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)