Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: Ruby  |  size: 8.32 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #===============================================================================
  2. # Windows Features
  3. # By Trebor777
  4. # Edit by Shocks
  5. # Version 3.0
  6. #-------------------------------------------------------------------------------
  7. # Permits the drag & drop, the resizing, close and click features.
  8. # Added the widgets handling.
  9. # Only one window or icon(see icon class) can be drag at the same time
  10. #===============================================================================
  11. SDK.log("win_features", "Trebor777", "2.0", "27-11-06")
  12. #-------------------------------------------------------------------------------
  13. # Begin SDK Enabled Check
  14. #-------------------------------------------------------------------------------
  15. if SDK.state('win_features')
  16. #===============================================================================
  17. # Class window Base
  18. #===============================================================================
  19. class Window_Base
  20.   attr_accessor :dragable
  21.   attr_accessor :scalable
  22.   attr_accessor :clickable
  23.   attr_accessor :closable
  24.   attr_accessor :widgets
  25.   #--------------------------------------------------------------------------
  26.   alias old_initialize initialize
  27.   #--------------------------------------------------------------------------
  28.   def initialize(x, y, width, height)
  29.     @dragable = false
  30.     @scalable = false
  31.     @clickable = false
  32.     @closable = false
  33.     @widgets = []
  34.     old_initialize(x, y, width, height)
  35.     self.margin = 16 # 16 is the default margin size
  36.   end
  37.   #--------------------------------------------------------------------------
  38.   # Dispose the windows and the widgets it has.
  39.   #--------------------------------------------------------------------------
  40.   def dispose
  41.     super
  42.     for w in @widgets
  43.       w.dispose
  44.     end
  45.   end
  46.   #--------------------------------------------------------------------------
  47.   # Change the normal color to black instead of white
  48.   #--------------------------------------------------------------------------
  49.   def normal_color
  50.     return Color.new(0, 0, 0, 255)
  51.   end
  52.   #--------------------------------------------------------------------------
  53.   # Frame Update, Check the different status to execute the different features
  54.   #--------------------------------------------------------------------------
  55.   def update
  56.     super
  57.     if $game_system.windowskin_name != @windowskin_name
  58.       @windowskin_name = $game_system.windowskin_name
  59.       self.windowskin = RPG::Cache.windowskin(@windowskin_name)
  60.     end
  61.     @old_opacity = self.back_opacity if @old_opacity == nil
  62.     @old_opacity2 = self.contents_opacity if @old_opacity2 == nil
  63.     # Check for close
  64.     if @closable and in_area?([self.width-15, 3, 10, 10]) and $click_esquerdo and $dragging == false
  65.       for w in @widgets
  66.         w.visible = false
  67.         w.active = false
  68.       end
  69.       self.active = false
  70.       self.visible = false
  71.       on_close
  72.     end
  73.     # Check for click
  74.     if @clickable and (self.visible or self.opacity > 0)
  75.       if $click_esquerdo and in_area?
  76.         on_click_left
  77.       elsif $click_esquerdo and !in_area? and self.active
  78.         out_click
  79.       end
  80.       if $click_direito and in_area?
  81.         on_click_right
  82.       end
  83.       if Input.trigger(Input::Mouse_Middle) and in_area?
  84.         on_click_middle
  85.       end
  86.     end
  87.     # Check for Drag and resizing.
  88.     if (@dragging and !Input.pressed(Input::Mouse_Left))
  89.       @dragging = nil
  90.       $dragging = false
  91.     end
  92.     if (@scaling and !Input.pressed(Input::Mouse_Left))
  93.       @scaling = nil
  94.     end
  95.     if Input.pressed(Input::mouse_Left)) and self.visible and !$item_drag
  96.       if @dragable and (in_area?([0, 0, self.width, 10]) or @dragging)
  97.         drag if (!$dragging or @dragging) and !$scrolling
  98.       end
  99.       if @scalable and (in_area?([self.width-10,self.height-10,10,10]) or @scaling)
  100.         scale if @scaling or !$scrolling
  101.       end
  102.     end
  103.    
  104.     # Retornar opacidade (fade-in)
  105.     for i in 1...4
  106.       if !$dragging and self.back_opacity != @old_opacity
  107.         self.back_opacity += i if @old_opacity + i > self.back_opacity
  108.         self.back_opacity -= i if @old_opacity - i < self.back_opacity
  109.       end
  110.     end
  111.     for i in 1...4
  112.       if !$dragging and self.contents_opacity != @old_opacity2
  113.         self.contents_opacity += i if @old_opacity2 + i > self.contents_opacity
  114.         self.contents_opacity -= i if @old_opacity2 - i < self.contents_opacity
  115.       end
  116.     end  
  117.     # Update all the linked widgets
  118.     for w in @widgets
  119.       w.update
  120.     end
  121.   end
  122.   #--------------------------------------------------------------------------
  123.   # Check if the mouse cursor is in the given area of the window
  124.   #   args = [x,y,w,h], by default represent the entire window.
  125.   #--------------------------------------------------------------------------
  126.   def in_area?(args=[0,0,self.width,self.height])
  127.     if $mouse.x.between?(self.x+args[0],self.x + args[2]+args[0]) and
  128.       $mouse.y.between?(self.y+args[1],self.y + args[3]+args[1])
  129.       return true
  130.     end
  131.     return false
  132.   end
  133.   #--------------------------------------------------------------------------
  134.   # Drag & Drop method
  135.   #--------------------------------------------------------------------------
  136.   def drag
  137.     if @dragging == nil
  138.       @dif_x = $mouse.x - self.x
  139.       @dif_y = $mouse.y - self.y
  140.       @dragging = true
  141.       $dragging = true
  142.     end
  143.     if @dragging
  144.       self.contents_opacity = 160
  145.       self.back_opacity = 160
  146.       self.x = $mouse.x - @dif_x
  147.       self.y = $mouse.y - @dif_y
  148.     end
  149.   end
  150.   #--------------------------------------------------------------------------
  151.   # Resizing method
  152.   #--------------------------------------------------------------------------
  153.   def scale
  154.     if @scaling==nil
  155.       self.ox=0
  156.       self.oy=0
  157.       @scaling=true
  158.     end
  159.     if @scaling
  160.       self.width = $mouse.x- self.x if $mouse.x- self.x>self.margin*2
  161.       self.height = $mouse.y- self.y if $mouse.y- self.y>self.margin*2
  162.     end
  163.   end
  164.   #--------------------------------------------------------------------------
  165.   # On event methods
  166.   #   Usually called in the update method.
  167.   #--------------------------------------------------------------------------
  168.   def on_click_left
  169.   end
  170.   #--------------------------------------------------------------------------
  171.   def on_click_right
  172.   end
  173.   #--------------------------------------------------------------------------
  174.   def on_click_middle
  175.   end
  176.   #--------------------------------------------------------------------------
  177.   def on_close
  178.   end
  179.   #--------------------------------------------------------------------------
  180.   def out_click
  181.   end
  182.   #--------------------------------------------------------------------------
  183.   # Set the visibility for the window, and for the widgets.
  184.   #--------------------------------------------------------------------------
  185.   def visible=(t)
  186.     @visible=t
  187.     for w in @widgets
  188.       w.visible=t
  189.     end
  190.     update_visible
  191.     update_arrows
  192.   end
  193. end
  194.  
  195. #===============================================================================
  196. # Add the mouse support for Window_Command, to allow option selection with the
  197. # mouse.
  198. #===============================================================================
  199. class Window_Command
  200.  
  201.   alias cursor_mouse_update update
  202.   alias cursor_mouse_initialize initialize
  203.   #--------------------------------------------------------------------------
  204.   def initialize(width, commands)
  205.     @ind_box = []
  206.     cursor_mouse_initialize(width, commands)
  207.   end
  208.   #--------------------------------------------------------------------------
  209.   def update
  210.     # Fill @ind_box only one time.
  211.     if @ind_box == []
  212.       cursor_width = self.width / @column_max - 32
  213.       for i in 0...@item_max
  214.         x = i % @column_max * (cursor_width + 32)
  215.         y = i / @column_max * 32 - self.oy+self.margin
  216.         @ind_box.push([[x,y,cursor_width,32],i])
  217.       end
  218.     end
  219.     # Original update
  220.     cursor_mouse_update
  221.     # Check mouse State and update the index
  222.     return if @ind_box == nil
  223.     for b in @ind_box
  224.       if in_area?(b[0])and self.active and Input.trigger(Input::Mouse_Left)
  225.         @index = b[1]
  226.         update_cursor_rect
  227.       end
  228.     end
  229.   end
  230.   #--------------------------------------------------------------------------
  231. end
  232. end