IceDragon200

HM_WindowSelectable V1.2

Jul 24th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.93 KB | None | 0 0
  1. #==============================================================================#
  2. # ** ICY-CORE - HM_Window_Selectable
  3. #------------------------------------------------------------------------------#
  4. # ** Created by    : IceDragon (http://www.rpgmakervx.net/)
  5. # ** Version       : 1.2
  6. # ** Date Modified : 01/08/2011
  7. #------------------------------------------------------------------------------#
  8. #  This is a superclass for the HM Style Window Selectable.
  9. #==============================================================================#
  10. $imported = {} if $imported == nil
  11. $imported["ICY_HM_Window_Selectable"] = true
  12.  
  13. class ICY_HM_Window_Selectable < Window_Selectable
  14.   attr_accessor :column_max
  15.   attr_accessor :item_sq_spacing
  16.   attr_accessor :rect_size
  17.   attr_accessor :selection_size
  18.  
  19.   def initialize(x, y, width, height)
  20.     @column_max = 1
  21.     @item_sq_spacing = 48
  22.     @rect_size = 32
  23.     @selection_size = 42
  24.     @index = 0
  25.     @reset = 0
  26.     @nw_x = 0
  27.     @nw_y = 0
  28.     @coun = 0
  29.     super(x, y, width, height)
  30.   end
  31.  
  32.   def off_x
  33.     width_break = (self.width - 32)
  34.     column_size = ([(@column_max * @item_sq_spacing), width_break].min)
  35.     return Integer(((width_break - column_size) / 2))
  36.   end
  37.  
  38.   def off_y
  39.     height_break = (self.height - 32)
  40.     row_size = ([(page_row_max * @item_sq_spacing), height_break].min)
  41.     return Integer(((height_break - row_size) / 2))
  42.   end
  43.  
  44.   #--------------------------------------------------------------------------
  45.   # * Create Window Contents
  46.   #--------------------------------------------------------------------------
  47.   def create_contents
  48.     self.contents.dispose
  49.     maxbitmap = 8192
  50.     dw = [width - 32, maxbitmap].min
  51.     dh = [[height - 32, row_max * @item_sq_spacing].max, maxbitmap].min
  52.     bitmap = Bitmap.new(dw, dh)
  53.     self.contents = bitmap
  54.   end
  55.  
  56.   #--------------------------------------------------------------------------
  57.   # * Get rectangle for displaying items
  58.   #     index : item number
  59.   #--------------------------------------------------------------------------
  60.   def item_rect(index)
  61.     rect = Rect.new(off_x, off_y, 0, 0)
  62.     rect.width += @rect_size
  63.     rect.height += @rect_size
  64.     rect.x += index % @column_max * @item_sq_spacing
  65.     rect.y += index / @column_max * @item_sq_spacing
  66.     return rect
  67.   end
  68.  
  69.   def refresh
  70.     prep_coord_vars
  71.   end
  72.  
  73.   def prep_coord_vars
  74.     @reset = off_x
  75.     @nw_x = @reset
  76.     @nw_y = off_y
  77.     @coun = 0
  78.   end
  79.  
  80.   def advance_space
  81.     @coun += 1
  82.     @nw_x += @item_sq_spacing
  83.     if @coun == @column_max
  84.       @coun = 0
  85.       @nw_x = @reset
  86.       @nw_y += @item_sq_spacing
  87.     end
  88.   end
  89.   #--------------------------------------------------------------------------
  90.   # * Get Top Row
  91.   #--------------------------------------------------------------------------
  92.   def top_row
  93.     return self.oy / @item_sq_spacing#WLH
  94.   end
  95.   #--------------------------------------------------------------------------
  96.   # * Set Top Row
  97.   #     row : row shown on top
  98.   #--------------------------------------------------------------------------
  99.   def top_row=(row)
  100.     row = 0 if row < 0
  101.     row = row_max - 1 if row > row_max - 1
  102.     self.oy = row * @item_sq_spacing
  103.   end
  104.   #--------------------------------------------------------------------------
  105.   # * Get Number of Rows Displayable on 1 Page
  106.   #--------------------------------------------------------------------------
  107.   def page_row_max
  108.     return (self.height / @item_sq_spacing)
  109.   end
  110.   #--------------------------------------------------------------------------
  111.   # * Get Number of Items Displayable on 1 Page
  112.   #--------------------------------------------------------------------------
  113.   def page_item_max
  114.     return page_row_max * @column_max #+ @item_sq_spacing
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # * Move cursor one page down
  118.   #--------------------------------------------------------------------------
  119.   def cursor_pagedown
  120.     if top_row + page_row_max < row_max
  121.       @index = [@index + page_item_max, @item_max - 1].min
  122.       self.top_row += page_row_max - @item_sq_spacing
  123.     end
  124.   end
  125.   #--------------------------------------------------------------------------
  126.   # * Move cursor one page up
  127.   #--------------------------------------------------------------------------
  128.   def cursor_pageup
  129.     if top_row > 0
  130.       @index = [@index - page_item_max, 0].max
  131.       self.top_row -= page_row_max - @item_sq_spacing
  132.     end
  133.   end
  134.   #--------------------------------------------------------------------------
  135.   # * Update cursor
  136.   #--------------------------------------------------------------------------
  137.   def update_cursor
  138.     if @index < 0                   # If the cursor position is less than 0
  139.       self.cursor_rect.empty        # Empty cursor
  140.     else                            # If the cursor position is 0 or more
  141.       row = @index / @column_max    # Get current row
  142.       if row < top_row              # If before the currently displayed
  143.         self.top_row = row          # Scroll up
  144.       end
  145.       if row > bottom_row           # If after the currently displayed
  146.         self.bottom_row = row       # Scroll down
  147.       end
  148.        y_l = @index / @column_max
  149.        y_pos = ((@item_sq_spacing * y_l)- self.oy) + off_y
  150.        x_l = self.index - (@column_max * y_l)
  151.        x_pos = (x_l * @item_sq_spacing) + off_x
  152.        if @selection_size > @rect_size
  153.          subitive = (@selection_size - @rect_size) / 2
  154.          self.cursor_rect.set(x_pos.to_i - subitive, y_pos.to_i - subitive, @selection_size, @selection_size)
  155.        elsif @selection_size < @rect_size
  156.          additive = (@rect_size - @selection_size) / 2
  157.          self.cursor_rect.set(x_pos.to_i + additive, y_pos.to_i + additive, @selection_size, @selection_size)
  158.         else
  159.         self.cursor_rect.set(x_pos.to_i, y_pos.to_i, @selection_size, @selection_size)
  160.        end
  161.     end
  162.   end
  163.  
  164. end
  165.  
  166. class ICY_HM_Window_Command < ICY_HM_Window_Selectable
  167.  
  168.   #--------------------------------------------------------------------------
  169.   # * Public Instance Variables
  170.   #--------------------------------------------------------------------------
  171.   attr_reader   :commands                 # command
  172.   #--------------------------------------------------------------------------
  173.   # * Object Initialization
  174.   #     width      : window width
  175.   #     commands   : command string array
  176.   #     column_max : digit count (if 2 or more, horizontal selection)
  177.   #     row_max    : row count (0: match command count)
  178.   #     spacing    : blank space when items are arrange horizontally
  179.   #--------------------------------------------------------------------------
  180.   def initialize(width, commands, column_max = 1, row_max = 0, spacing = 32)
  181.     if row_max == 0
  182.       row_max = (commands.size + column_max - 1) / column_max
  183.     end
  184.     super(0, 0, width, 48+(24 * row_max))
  185.     @commands = commands
  186.     @item_max = commands.size
  187.     @column_max = column_max
  188.     refresh
  189.     self.index = 0
  190.   end
  191.   #--------------------------------------------------------------------------
  192.   # * Refresh
  193.   #--------------------------------------------------------------------------
  194.   def refresh
  195.     prep_coord_vars
  196.     self.contents.clear
  197.     create_contents
  198.     for i in 0...@item_max
  199.       draw_item(i)
  200.     end
  201.   end
  202.   #--------------------------------------------------------------------------
  203.   # * Draw Item
  204.   #     index   : item number
  205.   #     enabled : enabled flag. When false, draw semi-transparently.
  206.   #--------------------------------------------------------------------------
  207.   def draw_item(index, enabled = true)
  208.     rect = item_rect(index)
  209.     rect.x += 4
  210.     rect.width -= 8
  211.     self.contents.clear_rect(rect)
  212.     draw_icon(@commands[index], rect.x, rect.y, enabled)
  213.   end
  214.  
  215. end
  216.  
  217. class HM_Window_Selectable < ICY_HM_Window_Selectable ; end
  218. class HM_Window_Command < ICY_HM_Window_Command ; end
Advertisement
Add Comment
Please, Sign In to add comment