Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###############################################################################
- # SES: Window_Book
- # ***Scripter's Resource (Minimal scripting knowledge required!)***
- # v1.4
- # 11.20.2012
- # Compatibility: VXAce/RGSS3
- # Author: Enelvon
- #===============================================================================
- # Terms of Use:
- #
- # This script may be used for free in any game, whether it's commercial or not.
- # The only requirement is that I be credited in some visible manner. You may not
- # claim this script as your own work. You are free to modify this script, but
- # may not redistribute it except for in a thread that I have started that relates
- # to it in some way.
- #===============================================================================
- # Changelog:
- # 11.18.2012: v1.0 - Script written.
- # 11.19.2012: v1.1 - Fixed a bug with escape characters where only the first one
- # would be processed for each word.
- # 11.19.2012: v1.2 - Fixed a second escape character bug where they were not
- # being applied correctly.
- # 11.19.2012: v1.3 - Added in [Scene_Book] to allow simple books to be displayed
- # a little more easily.
- # 11.20.2012: v1.4 - Modified [write_page_text] to be able to take parameters.
- #===============================================================================
- # This script is my darling. It's a versatile way to create multi-page windows -
- # and it is definitely NOT plug-and-play. You need to at (at the very least)
- # read the instructions to get any use out of it, but after that even a
- # non-scripter should be able to create wonderful books.
- #
- # *clears her throat* Now that I have that out of the way, this script is
- # essentially a scripter's resource. It provides a parent class for multi-page
- # windows, with a focus on those used to display information (though it is also
- # possible, with some additions, to include pages with selectable areas). Its
- # write_page_text method draws text with the draw_text_ex method, allowing
- # you to use any control character that you would normally use in game text.
- # Further down, I have included a tutorial on creating a basic game controls
- # guide as an example of a simple use of the script.
- #===============================================================================
- # Required Scripts: None
- # Known Incompatibilities: None, and as this is a new class (and thus contains
- # no redefinitions of the base scripts), it should never have any.
- #===============================================================================
- # Installation: Place below Materials and above all other custom scripts, or
- # with the Window_* classes if you want to keep things organized.
- #===============================================================================
- $imported = {} if $imported.nil?
- $imported["SES - Window_Book"] = 1.4
- class Window_Book < Window_Base
- attr_accessor :pagetext
- def initialize(x = 0, y = 0, width = 544, height = 416, wrap_type = :word, skin = nil)
- @skin = skin || ($imported["SES - Window Swap"] ? Cache.windows("Book Window") : Cache.system("Window"))
- @wrap = wrap_type; @pagetext ||= {}; @pagetext.default = []
- super(x, y, width, height)
- self.z, @page = 255, 1; refresh; activate
- end
- def update
- super
- process_cursor_move; scrolldata.each_key do |k| eval("@#{k}.update") end
- end
- def dispose
- scrolldata.each_key do |k| eval("@#{k}.dispose") end
- super
- end
- def max_pages; 1; end
- def dy; 0; end
- def refresh
- self.oy = 0; create_contents; draw_scroll; eval("draw_page#{@page}")
- end
- def contents_height(padding = 2)
- i, b = dy / line_height + 1, Bitmap.new(1,1)
- @page ||= 1
- @pagetext[@page].each do |l|
- if @wrap == :word then t = l.split(/\s/)
- else t = []; l.each_char do |c| t.push(c) end end
- x = 0
- t.each do |w|
- dw = convert_escape_characters(w).gsub(/\\(\w)(\[(\w+)\])?/) { "" }
- (i += 1; x = 0) if x + b.text_size(dw + (@wrap == :word ? " " : "")).width > contents_width
- next if x == 0 && w == " "
- x += b.text_size(dw + (@wrap == :word ? " " : "")).width
- end
- i += 1
- end
- return [line_height*i, height - standard_padding * padding].max
- end
- def method_missing(method)
- if method =~ /^draw_page(\d+)$/
- write_page_text
- else
- super(method)
- end
- end
- def write_page_text(text = @pagetext[@page], i = 0)
- @page ||= 1
- text.each do |l|
- if @wrap == :word then t = l.split(/\s/)
- else t = []; l.each_char do |c| t.push(c) end end
- cc, x = "", 0
- t.each do |w|
- w = convert_escape_characters(w).gsub(/\e(\w)(\[(\w+)\])?/) { |s| cc += s; "" }
- (i += 1; x = 0) if x + text_size(w + (@wrap == :word ? " " : "")).width > contents_width
- next if x == 0 && w == " "
- draw_text_ex(x, i * line_height + dy, cc + w + (@wrap == :word ? " " : ""))
- x += text_size(w + (@wrap == :word ? " " : "")).width
- end
- i += 1
- end
- end
- def arrow_bitmap(d)
- self.windowskin if (@page < max_pages && d == :r) ||
- (@page > 1 && d == :l)
- end
- def sx
- self.x +
- (contents_width - text_size("#{max_pages}/#{max_pages}").width + 4) / 2 - 5
- end
- def scrolldata
- x = text_size("#{max_pages}/#{max_pages}").width + 4
- y = if Graphics.height - height == 0 then height - 28
- else self.y + height - 28 end
- { :pagecount => { :x => sx + 10, :y => y - 7,
- :bitmap => Bitmap.new(x, 24), :src_rect => Rect.new(0,0,x+4,line_height),
- :d_rect => Rect.new(0, 0, x + 4, line_height) },
- :rarrow => { :x => sx + x + 28, :y => y,
- :bitmap => arrow_bitmap(:r), :src_rect => Rect.new(104,25,7,12) },
- :larrow => { :x => sx - 12, :y => y,
- :bitmap => arrow_bitmap(:l), :src_rect => Rect.new(80,25,7,12) } }
- end
- def draw_scroll
- scrolldata.each_pair do |k,v|
- eval("@#{k}.dispose if !@#{k}.nil?
- @#{k}, @#{k}.x, @#{k}.y, @#{k}.z, @#{k}.bitmap, @#{k}.src_rect =
- Sprite.new, v[:x], v[:y], 255, v[:bitmap], v[:src_rect]
- @#{k}.bitmap.draw_text(v[:d_rect], '#{@page}/#{max_pages}', 1) if v[:d_rect]
- ")
- end
- end
- def process_cursor_move
- (@changed = false; return) if @changed
- oldpage = @page
- if Input.trigger?(:LEFT) && 1 < @page then @page -= 1
- elsif Input.trigger?(:RIGHT) && @page < max_pages then @page += 1
- elsif ((Input.press?(:UP)) if Input.repeat?(:UP)) && self.oy > 0
- self.oy -= 24; draw_scroll
- elsif ((Input.press?(:DOWN)) if Input.repeat?(:DOWN)) && contents_height(4) + 24 - height >= self.oy
- draw_scroll; self.oy += 24 end
- (refresh; @changed = true) if oldpage != @page
- end
- end
- class Game_Interpreter
- def show_book(book)
- SceneManager.goto(Scene_Book); SceneManager.scene.set_book(book)
- end
- end
- class Scene_Book < Scene_Base
- def set_book(book)
- @book = book.new
- end
- def update
- super
- @book.update if @book
- SceneManager.return if Input.trigger?(:B)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment