IceDragon200

[IEI]Gallery

May 24th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.91 KB | None | 0 0
  1. #==============================================================================#
  2. # ♥ IEI - Gallery
  3. #==============================================================================#
  4. # // • Created By    : IceDragon
  5. # // • Data Created  : 05/17/2012
  6. # // • Data Modified : 05/17/2012
  7. # // • Version       : 1.0
  8. #==============================================================================#
  9. ($imported||={})["IEI::Gallery"] = 0x10000
  10. # // IEI Core Header
  11. ($imported||={})["IEI::Core"]||=0x01001
  12. unless $imported["IEI::Core"] >= 0x10000
  13.   module IEI
  14.     module Core
  15.     end
  16.     module Sprite
  17.     end
  18.     module Window
  19.     end
  20.     module Scene
  21.     end
  22.   end
  23.   unless $imported["EDOS::Data"]
  24.     module Scene
  25.       Base = Scene_Base
  26.     end  
  27.     class Sprite
  28.       Base = Sprite_Base
  29.     end
  30.     class Window
  31.       Base = Window_Base
  32.       Selectable = Window_Selectable
  33.       Command = Window_Command
  34.     end
  35.   end  
  36. end  
  37. #==============================================================================#
  38. # ■ IEI::Gallery
  39. #==============================================================================#
  40. module IEI
  41.   module Gallery
  42.     # // Do not modify
  43.     class CG
  44.       class << self
  45.         alias :[] new
  46.       end
  47.       FUNC_TRUE   = proc { true }
  48.       FUNC_SWITCH = proc { switch?(@switch_id) }
  49.       attr_accessor :id, :name, :filenames, :switch_id
  50.       def initialize(id,name,filenames,switch_id=nil,&block)
  51.         @id,@name,@filenames,@switch_id = id,name,Array(filenames),switch_id
  52.         init_condition(&block)
  53.       end
  54.       def size
  55.         @filenames.size
  56.       end
  57.       def init_condition(&block)
  58.         @condition = block_given? ? block : (@switch_id ? FUNC_SWITCH : FUNC_TRUE)
  59.       end
  60.       def switch?(id)
  61.         $game_switches[id]
  62.       end
  63.       def condition_met?
  64.         instance_exec(&@condition) #rescue true
  65.       end
  66.     end
  67.     # // Edit Here
  68.     @sets = {}
  69.     def self.sets ; @sets ; end
  70.     # // CG[id,"CG-Name","filename"]
  71.     # // CG[id,"CG-Name","filename",switch_id]
  72.     # // CG[id,"CG-Name",["filename","filename","filename"],switch_id]
  73.   end
  74. end
  75. #==============================================================================#
  76. # ♥ IEI::Sprite::CG
  77. #==============================================================================#
  78. class IEI::Sprite::CG < Sprite
  79.   def initialize(viewport=nil,cg=nil)
  80.     super(viewport)
  81.     self.cg = cg
  82.   end
  83.   attr_reader :cg
  84.   def cg=(new_cg)
  85.     @cg = new_cg
  86.     @index = 0
  87.     update_bitmap
  88.   end
  89.   def update_bitmap
  90.     self.bitmap = @cg ? Cache.picture(@cg.filenames[@index]) : nil
  91.   end
  92.   def update
  93.     last_index = index
  94.     super
  95.     update_handler
  96.     Sound.play_cursor if last_index != index
  97.   end
  98.   def update_handler
  99.     return unless active?
  100.     return call_handler(:next) if Input.trigger?(:DOWN) or Input.trigger?(:RIGHT)
  101.     return call_handler(:prev) if Input.trigger?(:UP) or Input.trigger?(:LEFT)
  102.     return call_handler(:ok) if Input.trigger?(:C)
  103.     return call_handler(:cancel) if Input.trigger?(:B)
  104.   end
  105.   # // Edits
  106.   def index= n
  107.     @index = n.to_i
  108.     @index %= [item_max,1].max
  109.     update_bitmap
  110.   end
  111.   def item_max
  112.     @cg ? @cg.filenames.size : 0
  113.   end
  114.   # // indexing
  115.   def index
  116.     @index ||= 0
  117.   end unless method_defined? :index
  118.   def index= n
  119.     @index = n.to_i
  120.   end unless method_defined? :index=
  121.   def item_max
  122.     0
  123.   end unless method_defined? :item_max
  124.   def wrap_index wrap=item_max
  125.     self.index %= wrap > 0 ? wrap : 1
  126.     self
  127.   end
  128.   def select n
  129.     self.index = n
  130.     self
  131.   end
  132.   def deselect
  133.     self.index = -1
  134.     self
  135.   end
  136.   def next
  137.     self.index += 1
  138.     self
  139.   end
  140.   def prev
  141.     self.index -= 1
  142.     self
  143.   end
  144.   # // passive_state
  145.   def active
  146.     !!@active
  147.   end unless method_defined? :active
  148.   def active= state
  149.     @active = !!state
  150.   end unless method_defined? :active=
  151.   alias :active? :active
  152.   def activate
  153.     self.active = true
  154.     self
  155.   end
  156.   def deactivate
  157.     self.active = false
  158.     self
  159.   end
  160.   def toggle_active
  161.     self.active = !self.active
  162.     self
  163.   end
  164.   # // Visibility
  165.   def visible
  166.     !!@visible
  167.   end unless method_defined? :visible
  168.   def visible= n
  169.     @visible = !!n
  170.   end unless method_defined? :visible=
  171.   def toggle_visible
  172.     self.visible = !self.visible
  173.     self
  174.   end
  175.   def show
  176.     self.visible = true
  177.     self
  178.   end
  179.   def hide
  180.     self.visible = false
  181.     self
  182.   end
  183.   # // basic_handler
  184.   def handler
  185.     @handler ||= {}
  186.   end
  187.   attr_writer :handler
  188.   private :handler, :handler=
  189.   def set_handler symbol,function
  190.     handler[symbol] = function
  191.     self
  192.   end
  193.   def rem_handler symbol
  194.     handler.delete(symbol)
  195.     self
  196.   end
  197.   def call_handler symbol
  198.     handler[symbol].call if handler.has_key? symbol
  199.   end
  200. end
  201. #==============================================================================#
  202. # ♥ IEI::Window::Gallery_Set
  203. #==============================================================================#
  204. class IEI::Window::Gallery_Set < Window::Selectable
  205.   def initialize(x,y,w,h)
  206.     make_item_list
  207.     super
  208.     activate.select 0
  209.     refresh
  210.   end
  211.   attr_reader :cg_window
  212.   def cg_window=(window)
  213.     @cg_window = window
  214.     update_cg_window
  215.   end
  216.   def index=(n)
  217.     super(n)
  218.     update_cg_window
  219.   end
  220.   def update_cg_window
  221.     @cg_window.set = IEI::Gallery.sets[self.item] if @cg_window
  222.   end
  223.   def item(i=self.index)
  224.     @items[i]
  225.   end
  226.   def make_item_list
  227.     @items = IEI::Gallery.sets.keys.sort
  228.   end
  229.   def item_max
  230.     @items.size
  231.   end
  232.   def draw_item(index)
  233.     #rect  = item_rect(index)
  234.     trect = item_rect_for_text(index)
  235.     str   = @items[index]
  236.     draw_text(trect,str)
  237.   end
  238. end
  239. #==============================================================================#
  240. # ♥ IEI::Window::Gallery_CGs
  241. #==============================================================================#
  242. class IEI::Window::Gallery_CGs < Window::Selectable
  243.   def initialize(x,y,w,h)
  244.     @set = []
  245.     make_item_list
  246.     super
  247.     select(0)
  248.     refresh
  249.   end
  250.   attr_reader :set
  251.   def set=(set)
  252.     @set = set
  253.     make_item_list
  254.     refresh
  255.   end
  256.   def cg(i=index)
  257.     @set[i]
  258.   end
  259.   def make_item_list
  260.     @items = @set.sort_by{|cg|cg.id}.collect{|cg|
  261.       [cg.filenames,cg.condition_met?]
  262.     }
  263.   end
  264.   def item_max
  265.     @items.size
  266.   end
  267.   def item_width
  268.     self.width / col_max - spacing
  269.   end
  270.   def item_height
  271.     (item_width * 0.75).to_i
  272.   end
  273.   def spacing
  274.     2
  275.   end
  276.   def col_max
  277.     4
  278.   end
  279.   BLANK_BMP = Bitmap.new(128,96)
  280.   def draw_item(index)
  281.     rect   = item_rect(index)
  282.     item_a = @items[index]
  283.     bmp_fn,valid = item_a[0].first, item_a[1]
  284.     bmp    = Cache.picture(bmp_fn) rescue BLANK_BMP
  285.     unless valid
  286.       contents.fill_rect rect, Color.new(48,48,48,128)
  287.     else
  288.       srect = Rect.fit_in bmp.rect, rect
  289.       contents.stretch_blt srect, bmp, bmp.rect
  290.     end
  291.   end
  292.   def current_item_enabled?
  293.     return false unless @items[self.index]
  294.     @items[self.index][1]
  295.   end
  296. end
  297. #==============================================================================#
  298. # ♥ IEI::Scene::Gallery
  299. #==============================================================================#
  300. class IEI::Scene::Gallery < Scene::Base
  301.   # // Scene::Base
  302.   def start
  303.     super
  304.     #create_background if respond_to? :create_background
  305.     create_all_windows
  306.     create_sprites
  307.     auto_add_windows if $imported["EDOS::Data"]
  308.   end
  309.   def terminate
  310.     super
  311.     #dispose_background if respond_to? :dispose_background
  312.     @cg_sprite.dispose unless @cg_sprite
  313.   end
  314.   def update
  315.     super
  316.     #update_background if respond_to? :update_background
  317.     @cg_sprite.update
  318.   end
  319.   # // Create
  320.   def create_all_windows
  321.     create_set_window
  322.     create_cg_window
  323.   end
  324.   def create_sprites
  325.     @cg_sprite = IEI::Sprite::CG.new @viewport
  326.     @cg_sprite.set_handler :next  , method(:next_cg_image)
  327.     @cg_sprite.set_handler :prev  , method(:prev_cg_image)
  328.     @cg_sprite.set_handler :ok    , method(:end_cg_view)
  329.     @cg_sprite.set_handler :cancel, method(:end_cg_view)
  330.     @cg_sprite.hide
  331.     @cg_sprite.z = 201
  332.   end
  333.   # // Windows
  334.   def create_set_window
  335.     @set_window = IEI::Window::Gallery_Set.new(0,0,(Graphics.width*0.25).to_i,Graphics.height)
  336.     @set_window.set_handler :cancel, method(:return_scene)
  337.     @set_window.set_handler :ok    , method(:start_cg_selection)
  338.     #add_window @set_window
  339.   end
  340.   def create_cg_window
  341.     x = @set_window.x + @set_window.width
  342.     w = Graphics.width - x
  343.     h = @set_window.height
  344.     @cg_window = IEI::Window::Gallery_CGs.new(x,0,w,h)
  345.     @cg_window.set_handler :ok    , method(:start_cg_view)
  346.     @cg_window.set_handler :cancel, method(:start_set_selection)
  347.     @set_window.cg_window = @cg_window
  348.     #add_window @cg_window
  349.   end
  350.   # // Sprites
  351.   # // Nothing here but us whitespace
  352.   # // Commands
  353.   def start_set_selection
  354.     @set_window.activate
  355.   end
  356.   def start_cg_selection
  357.     @cg_window.activate
  358.   end
  359.   def end_cg_selection
  360.     @set_window.activate
  361.   end
  362.   def start_cg_view
  363.     @cg_sprite.activate.show
  364.     @cg_sprite.cg = @cg_window.cg
  365.     @cg_sprite.x = (Graphics.width - @cg_sprite.width) / 2
  366.     @cg_sprite.y = (Graphics.height - @cg_sprite.height) / 2
  367.     @cg_window.deactivate.close
  368.     @set_window.deactivate.close
  369.   end
  370.   def end_cg_view
  371.     @cg_sprite.deactivate.hide
  372.     @cg_window.activate.open
  373.     @set_window.deactivate.open
  374.   end
  375.   def next_cg_image
  376.     if @cg_sprite.index < @cg_sprite.item_max
  377.       @cg_sprite.next
  378.     else
  379.       Sound.play_buzzer  
  380.     end  
  381.   end
  382.   def prev_cg_image
  383.     if @cg_sprite.index > 0
  384.       @cg_sprite.prev
  385.     else
  386.       Sound.play_buzzer  
  387.     end  
  388.   end
  389. end
  390. Scene_Gallery = IEI::Scene::Gallery
  391. #==============================================================================#
  392. # EOF
  393. #==============================================================================#
Advertisement
Add Comment
Please, Sign In to add comment