Enelvon

SES: Window_Book [no comments]

Nov 29th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.14 KB | None | 0 0
  1. ###############################################################################
  2. # SES: Window_Book
  3. #   ***Scripter's Resource (Minimal scripting knowledge required!)***
  4. # v1.4
  5. # 11.20.2012
  6. # Compatibility: VXAce/RGSS3
  7. # Author: Enelvon
  8. #===============================================================================
  9. # Terms of Use:
  10. #
  11. # This script may be used for free in any game, whether it's commercial or not.
  12. # The only requirement is that I be credited in some visible manner. You may not
  13. # claim this script as your own work. You are free to modify this script, but
  14. # may not redistribute it except for in a thread that I have started that relates
  15. # to it in some way.
  16. #===============================================================================
  17. # Changelog:
  18. # 11.18.2012: v1.0 - Script written.
  19. # 11.19.2012: v1.1 - Fixed a bug with escape characters where only the first one
  20. #                    would be processed for each word.
  21. # 11.19.2012: v1.2 - Fixed a second escape character bug where they were not
  22. #                    being applied correctly.
  23. # 11.19.2012: v1.3 - Added in [Scene_Book] to allow simple books to be displayed
  24. #                    a little more easily.
  25. # 11.20.2012: v1.4 - Modified [write_page_text] to be able to take parameters.
  26. #===============================================================================                          
  27. # This script is my darling. It's a versatile way to create multi-page windows -
  28. # and it is definitely NOT plug-and-play. You need to at (at the very least)
  29. # read the instructions to get any use out of it, but after that even a
  30. # non-scripter should be able to create wonderful books.
  31. #
  32. # *clears her throat* Now that I have that out of the way, this script is
  33. # essentially a scripter's resource. It provides a parent class for multi-page
  34. # windows, with a focus on those used to display information (though it is also
  35. # possible, with some additions, to include pages with selectable areas). Its
  36. # write_page_text method draws text with the draw_text_ex method, allowing
  37. # you to use any control character that you would normally use in game text.
  38. # Further down, I have included a tutorial on creating a basic game controls
  39. # guide as an example of a simple use of the script.
  40. #===============================================================================
  41. # Required Scripts: None
  42. # Known Incompatibilities: None, and as this is a new class (and thus contains
  43. #     no redefinitions of the base scripts), it should never have any.
  44. #===============================================================================
  45. # Installation: Place below Materials and above all other custom scripts, or
  46. #     with the Window_* classes if you want to keep things organized.
  47. #===============================================================================
  48.  
  49. $imported = {} if $imported.nil?
  50. $imported["SES - Window_Book"] = 1.4
  51.  
  52. class Window_Book < Window_Base
  53.  
  54.   attr_accessor :pagetext
  55.  
  56.   def initialize(x = 0, y = 0, width = 544, height = 416, wrap_type = :word, skin = nil)
  57.     @skin = skin || ($imported["SES - Window Swap"] ? Cache.windows("Book Window") : Cache.system("Window"))
  58.     @wrap = wrap_type; @pagetext ||= {}; @pagetext.default = []
  59.     super(x, y, width, height)
  60.     self.z, @page = 255, 1; refresh; activate
  61.   end
  62.  
  63.   def update
  64.     super
  65.     process_cursor_move; scrolldata.each_key do |k| eval("@#{k}.update") end
  66.   end
  67.  
  68.   def dispose
  69.     scrolldata.each_key do |k| eval("@#{k}.dispose") end
  70.     super
  71.   end
  72.  
  73.   def max_pages; 1; end
  74.  
  75.   def dy; 0; end
  76.  
  77.   def refresh
  78.     self.oy = 0; create_contents; draw_scroll; eval("draw_page#{@page}")
  79.   end
  80.  
  81.   def contents_height(padding = 2)
  82.     i, b = dy / line_height + 1, Bitmap.new(1,1)
  83.     @page ||= 1
  84.     @pagetext[@page].each do |l|
  85.       if @wrap == :word then t = l.split(/\s/)
  86.       else t = []; l.each_char do |c| t.push(c) end end
  87.       x = 0
  88.       t.each do |w|
  89.         dw = convert_escape_characters(w).gsub(/\\(\w)(\[(\w+)\])?/) { "" }
  90.         (i += 1; x = 0) if x + b.text_size(dw + (@wrap == :word ? " " : "")).width > contents_width
  91.         next if x == 0 && w == " "
  92.         x += b.text_size(dw + (@wrap == :word ? " " : "")).width
  93.       end
  94.       i += 1
  95.     end
  96.     return [line_height*i, height - standard_padding * padding].max
  97.   end
  98.  
  99.   def method_missing(method)
  100.     if method =~ /^draw_page(\d+)$/
  101.       write_page_text
  102.     else
  103.       super(method)
  104.     end
  105.   end
  106.  
  107.   def write_page_text(text = @pagetext[@page], i = 0)
  108.     @page ||= 1
  109.     text.each do |l|
  110.       if @wrap == :word then t = l.split(/\s/)
  111.       else t = []; l.each_char do |c| t.push(c) end end
  112.       cc, x = "", 0
  113.       t.each do |w|
  114.         w = convert_escape_characters(w).gsub(/\e(\w)(\[(\w+)\])?/) { |s| cc += s; "" }
  115.         (i += 1; x = 0) if x + text_size(w + (@wrap == :word ? " " : "")).width > contents_width
  116.         next if x == 0 && w == " "
  117.         draw_text_ex(x, i * line_height + dy, cc + w + (@wrap == :word ? " " : ""))
  118.         x += text_size(w + (@wrap == :word ? " " : "")).width
  119.       end
  120.       i += 1
  121.     end
  122.   end
  123.  
  124.   def arrow_bitmap(d)
  125.     self.windowskin if (@page < max_pages && d == :r) ||
  126.                               (@page > 1 && d == :l)
  127.   end
  128.  
  129.   def sx
  130.     self.x +
  131.     (contents_width - text_size("#{max_pages}/#{max_pages}").width + 4) / 2 - 5
  132.   end
  133.  
  134.   def scrolldata
  135.     x = text_size("#{max_pages}/#{max_pages}").width + 4
  136.     y = if Graphics.height - height == 0 then height - 28
  137.         else self.y + height - 28 end
  138.     { :pagecount => { :x => sx + 10, :y => y - 7,
  139.         :bitmap => Bitmap.new(x, 24), :src_rect => Rect.new(0,0,x+4,line_height),
  140.         :d_rect => Rect.new(0, 0, x + 4, line_height) },
  141.       :rarrow => { :x => sx + x + 28, :y => y,
  142.         :bitmap => arrow_bitmap(:r), :src_rect => Rect.new(104,25,7,12) },
  143.       :larrow => { :x => sx - 12, :y => y,
  144.         :bitmap => arrow_bitmap(:l), :src_rect => Rect.new(80,25,7,12) } }
  145.   end
  146.  
  147.   def draw_scroll
  148.     scrolldata.each_pair do |k,v|
  149.       eval("@#{k}.dispose if !@#{k}.nil?
  150.      @#{k},      @#{k}.x, @#{k}.y, @#{k}.z, @#{k}.bitmap, @#{k}.src_rect  =
  151.      Sprite.new, v[:x],   v[:y],   255,     v[:bitmap], v[:src_rect]
  152.      @#{k}.bitmap.draw_text(v[:d_rect], '#{@page}/#{max_pages}', 1) if v[:d_rect]
  153.      ")
  154.     end
  155.   end
  156.  
  157.   def process_cursor_move
  158.     (@changed = false; return) if @changed
  159.     oldpage = @page
  160.     if Input.trigger?(:LEFT) && 1 < @page then @page -= 1
  161.     elsif Input.trigger?(:RIGHT) && @page < max_pages then @page += 1
  162.     elsif ((Input.press?(:UP)) if Input.repeat?(:UP)) && self.oy > 0
  163.       self.oy -= 24; draw_scroll
  164.     elsif ((Input.press?(:DOWN)) if Input.repeat?(:DOWN)) && contents_height(4) + 24 - height >= self.oy
  165.       draw_scroll; self.oy += 24 end
  166.     (refresh; @changed = true) if oldpage != @page
  167.   end
  168. end
  169.  
  170. class Game_Interpreter
  171.  
  172.   def show_book(book)
  173.     SceneManager.goto(Scene_Book); SceneManager.scene.set_book(book)
  174.   end
  175. end
  176.  
  177. class Scene_Book < Scene_Base
  178.    
  179.   def set_book(book)
  180.     @book = book.new
  181.   end
  182.    
  183.   def update
  184.     super
  185.     @book.update if @book
  186.     SceneManager.return if Input.trigger?(:B)
  187.   end
  188. end
Advertisement
Add Comment
Please, Sign In to add comment