Advertisement
Holy87

Scrollbar

Oct 3rd, 2020 (edited)
1,759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.79 KB | None | 0 0
  1. =begin
  2.  ==============================================================================
  3.   ■ Barra di scorrimento di Holy87
  4.       versione 1.0
  5.       Difficoltà utente: ★
  6.       Licenza: CC. Chiunque può scaricare, modificare, distribuire e utilizzare
  7.       lo script nei propri progetti, sia amatoriali che commerciali. Vietata
  8.       l'attribuzione impropria.
  9.  ==============================================================================
  10.     Questo script mostra una barra di scorrimento alle finestre con cursore
  11.     quando il numero degli oggetti presente supera quello visualizzabile.
  12.  ==============================================================================
  13.   ■ Installazione e istruzioni
  14.     Installare questo script sotto Materials e prima del Main.
  15.     In basso è possibile configurare colore e dimensioni.
  16. =end
  17.  
  18. #==============================================================================
  19. # ** CONFIGURAZIONE
  20. #==============================================================================
  21. module Scrollbar_Settings
  22.   # Colore della barra. Va inserito l'indice del colore del testo,
  23.   # quello sulla Windowskin.
  24.   SCROLLBAR_FOREGROUND_COLOR = 0
  25.   # colore di sfondo della barra.
  26.   SCROLLBAR_BACKGROUND_COLOR = 19
  27.   # spessore della barra, in pixel
  28.   BAR_THICKNESS = 4
  29.   # lo spostamento a destra rispetto alla fine del testo
  30.   BAR_SPACING = 1
  31.   # Scegli se vuoi mostrare la barra solo sulle finestre attive
  32.   SHOW_ONLY_ON_ACTIVE = false
  33. end
  34.  
  35. # ~ ~ ~ ~ ~ ~ ~ ATTENZIONE: NON MODIFICARE DA QUI ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. #==============================================================================
  43. # ** Window_Selectable
  44. #==============================================================================
  45. class Window_Selectable < Window_Base
  46.   attr_accessor :scrollbar_visible
  47.  
  48.   include Scrollbar_Settings
  49.  
  50.   alias window_scrollbar_initialize initialize unless $@
  51.   alias window_scrollbar_dispose dispose unless $@
  52.   alias window_scrollbar_update update unless $@
  53.   alias window_scrollbar_viewport viewport= unless $@
  54.  
  55.   def initialize(x, y, width, height)
  56.     window_scrollbar_initialize(x, y, width, height)
  57.     init_scrollbar
  58.   end
  59.  
  60.   def dispose
  61.     window_scrollbar_dispose
  62.     dispose_scrollbar
  63.   end
  64.  
  65.   def update
  66.     window_scrollbar_update
  67.     update_scrollbar
  68.   end
  69.  
  70.   # @param [Viewport] new_viewport
  71.   def viewport=(new_viewport)
  72.     window_scrollbar_viewport(new_viewport)
  73.     set_scrollbar_viewport(new_viewport)
  74.   end
  75.  
  76.   def init_scrollbar
  77.     return if @vert_scrollbar_background != nil
  78.     @scrollbar_visible = true
  79.     @vert_scrollbar_background = Sprite.new(@viewport)
  80.     @vert_scrollbar_foreground = Sprite.new(@viewport)
  81.     @vert_scrollbar_background.bitmap = scrollbar_background_bitmap
  82.     @vert_scrollbar_foreground.bitmap = scrollbar_foreground_bitmap
  83.     update_scrollbar
  84.   end
  85.  
  86.   def set_scrollbar_viewport(viewport)
  87.     @vert_scrollbar_background.viewport = viewport
  88.     @vert_scrollbar_foreground.viewport = viewport
  89.   end
  90.  
  91.   # free scrollbar graphic
  92.   def dispose_scrollbar
  93.     @vert_scrollbar_foreground.dispose
  94.     @vert_scrollbar_background.dispose
  95.   end
  96.  
  97.   def scrollbar_visible=(value)
  98.     @scrollbar_visible = value
  99.     update_scrollbar
  100.   end
  101.  
  102.   # update the scrollbar position
  103.   def update_scrollbar
  104.     @vert_scrollbar_background.visible = scrollbar_visible?
  105.     @vert_scrollbar_foreground.visible = scrollbar_visible?
  106.     return unless scrollbar_visible?
  107.     @vert_scrollbar_foreground.x = vert_scrollbar_x
  108.     @vert_scrollbar_background.x = vert_scrollbar_x
  109.     @vert_scrollbar_background.y = vert_scrollbar_y
  110.     @vert_scrollbar_foreground.y = vert_scrollbar_y
  111.     @vert_scrollbar_background.z = self.z + 10
  112.     @vert_scrollbar_foreground.z = self.z + 12
  113.     @vert_scrollbar_background.opacity = self.contents_opacity
  114.     @vert_scrollbar_foreground.opacity = self.contents_opacity
  115.     @vert_scrollbar_foreground.zoom_y = scrollbar_height * vert_scrollbar_rate
  116.     @vert_scrollbar_background.zoom_y = scrollbar_height
  117.     @vert_scrollbar_foreground.y += vert_scrollbar_position
  118.   end
  119.  
  120.   # @return [Fixnum, Float]
  121.   def vert_scrollbar_rate
  122.     return 1 if contents.nil? or contents.height == 0
  123.     #noinspection RubyYardReturnMatch
  124.     [[0, scrollbar_height.to_f / contents.height.to_f].max, 1].min
  125.   end
  126.  
  127.   def vert_scrollbar_x
  128.     self.x + self.width - padding + BAR_SPACING
  129.   end
  130.  
  131.   def vert_scrollbar_y
  132.     self.y + padding
  133.   end
  134.  
  135.   def scrollbar_height
  136.     self.height - padding * 2
  137.   end
  138.  
  139.   def vert_scrollbar_position
  140.     self.oy * vert_scrollbar_rate
  141.   end
  142.  
  143.   def scrollbar_visible?
  144.     return false unless self.visible
  145.     return false unless self.open?
  146.     return false if self.index < 0
  147.     return false if SHOW_ONLY_ON_ACTIVE and !active
  148.     return false if contents.height <= scrollbar_height
  149.     @scrollbar_visible
  150.   end
  151.  
  152.   # @return [Bitmap]
  153.   def scrollbar_background_bitmap
  154.     bitmap = Bitmap.new(BAR_THICKNESS, 1)
  155.     bitmap.fill_rect(0, 0, BAR_THICKNESS, 1, scrollbar_background_color)
  156.     bitmap
  157.   end
  158.  
  159.   # @return [Bitmap]
  160.   def scrollbar_foreground_bitmap
  161.     bitmap = Bitmap.new(BAR_THICKNESS, 1)
  162.     bitmap.fill_rect(0, 0, BAR_THICKNESS, 1, scrollbar_color)
  163.     bitmap
  164.   end
  165.  
  166.   # Il colore della barra
  167.   # @return [Color]
  168.   def scrollbar_color
  169.     text_color(SCROLLBAR_FOREGROUND_COLOR)
  170.   end
  171.  
  172.   # Il colore dello sfondo della barra
  173.   # @return [Color]
  174.   def scrollbar_background_color
  175.     text_color(SCROLLBAR_BACKGROUND_COLOR)
  176.   end
  177.  
  178.   if $imported['H87_Windowskins']
  179.     def refresh_windowskin(need_refresh = false)
  180.       super
  181.       init_scrollbar if @vert_scrollbar_foreground.nil?
  182.       @vert_scrollbar_background.bitmap = scrollbar_background_bitmap
  183.       @vert_scrollbar_foreground.bitmap = scrollbar_foreground_bitmap
  184.     end
  185.   end
  186. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement