Last active
August 29, 2015 14:00
-
-
Save myokoym/11024339 to your computer and use it in GitHub Desktop.
Image Viewer using Gosu and Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "gosu" | |
module GosuImageViewer | |
class Window < Gosu::Window | |
def initialize(width=640, height=480) | |
super(width, height, false) | |
self.caption = "Image Viewer using Gosu" | |
@images = [] | |
@scale = 1.0 | |
@angle = 0 | |
@mode = 1 | |
@spin = false | |
@auto = false | |
reset_auto_count | |
end | |
def add_image(path) | |
@images << path.encode("UTF-8") | |
load_image if @images.size == @mode | |
end | |
def update | |
@scale += 0.01 if button_down?(Gosu::KbL) | |
@scale -= 0.01 if button_down?(Gosu::KbS) | |
@angle += 1 if button_down?(Gosu::KbJ) || @spin | |
@angle -= 1 if button_down?(Gosu::KbK) | |
if @auto | |
@auto_count -= 1 | |
if @auto_count <= 0 | |
@mode.times { next_image } | |
reset_auto_count | |
end | |
end | |
end | |
def draw | |
draw_image unless @images.empty? | |
end | |
def button_down(id) | |
case id | |
when Gosu::Kb0 | |
@mode = 0 | |
when Gosu::Kb1 | |
@mode = 1 | |
when Gosu::Kb2 | |
@mode = 2 | |
load_image | |
when Gosu::Kb4 | |
@mode = 4 | |
load_image | |
when Gosu::Kb9 | |
@mode = 9 | |
load_image | |
when Gosu::KbN | |
next_image | |
when Gosu::KbP | |
prev_image | |
when Gosu::KbQ | |
@spin = !@spin | |
when Gosu::KbA | |
@auto = !@auto | |
when Gosu::KbR | |
@scale = 1.0 | |
@angle = 0 | |
when Gosu::KbEscape | |
close | |
end | |
end | |
private | |
def load_image | |
@mode.times do |i| | |
if @images[i].is_a?(String) | |
path = @images[i] | |
begin | |
@images[i] = Gosu::Image.new(self, path, false) | |
rescue RuntimeError => e | |
p e | |
@images.delete_at(i) | |
load_image | |
end | |
end | |
end | |
end | |
def next_image | |
@images.push(@images.shift) | |
load_image | |
end | |
def prev_image | |
@images.unshift(@images.pop) | |
load_image | |
end | |
def draw_image | |
case @mode | |
when 1 | |
draw_image_1(@images.first) | |
when 2 | |
draw_image_2(@images.take(2)) | |
when 4 | |
draw_image_4(@images.take(4)) | |
when 9 | |
draw_image_9(@images.take(9)) | |
end | |
end | |
def draw_image_1(image) | |
center_x = self.width * 0.5 | |
center_y = self.height * 0.5 | |
image.draw_rot(center_x, center_y, 1, | |
@angle, 0.5, 0.5, | |
image_factor_x(image), image_factor_y(image)) | |
end | |
def draw_image_2(images) | |
images.each_with_index do |image, i| | |
center_x = self.width * (0.25 + i * 0.5) | |
center_y = self.height * 0.5 | |
image.draw_rot(center_x, center_y, 1, | |
@angle, 0.5, 0.5, | |
image_factor_x(image, 0.5), image_factor_y(image)) | |
end | |
end | |
def draw_image_4(images) | |
images.each_with_index do |image, i| | |
center_x = self.width * (0.25 + i % 2 * 0.5) | |
center_y = self.height * (0.25 + (i / 2).floor * 0.5) | |
image.draw_rot(center_x, center_y, 1, | |
@angle, 0.5, 0.5, | |
image_factor_x(image, 0.5), image_factor_y(image, 0.5)) | |
end | |
end | |
def draw_image_9(images) | |
images.each_with_index do |image, i| | |
center_x = self.width * (0.166 + i % 3 * 0.333) | |
center_y = self.height * (0.166 + (i / 3).floor * 0.333) | |
image.draw_rot(center_x, center_y, 1, | |
@angle, 0.5, 0.5, | |
image_factor_x(image, 0.333), image_factor_y(image, 0.333)) | |
end | |
end | |
def image_factor_x(image, scale=1.0) | |
if image.width > (self.width * scale) | |
@scale * self.width * scale / image.width | |
else | |
@scale | |
end | |
end | |
def image_factor_y(image, scale=1.0) | |
if image.height > (self.height * scale) | |
@scale * self.height * scale / image.height | |
else | |
@scale | |
end | |
end | |
def reset_auto_count | |
@auto_count = 60 * @mode | |
end | |
end | |
end | |
window = GosuImageViewer::Window.new | |
ARGV.each do |path| | |
next if /\.(png|jpe?g|gif|bmp|raw|sgi|ico|psd|tiff?|tga|tpic)/i !~ path | |
window.add_image(path) | |
end | |
window.show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment