Advertisement
LiTTleDRAgo

[RGSS] TheoAllen - Anywhere Texts on Screen (ported)

Sep 13th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.95 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Anywhere Texts On Screen
  3. # Version : 1.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (English Documentation)
  6. # Ported to XP by LiTTleDRAgo
  7. # =============================================================================
  8. ($imported ||= {})[:Theo_AnywhereText] = true
  9. # =============================================================================
  10. # CHANGE LOGS:
  11. # -----------------------------------------------------------------------------
  12. # 2013.08.26 - Finished script
  13. # 2013.08.25 - Started script
  14. # =============================================================================
  15. =begin
  16.  
  17.   ----------------------------------------------------------------------------
  18.   Introduction :
  19.   This script allow you to have many texts written on screen.
  20.  
  21.   ----------------------------------------------------------------------------
  22.   How to use :
  23.   Simply put this script below material but above main
  24.   Read the manual
  25.  
  26.   LiTTleDRAgo's note :
  27.   Require Drago Core Engine to work in RMXP
  28.   ----------------------------------------------------------------------------
  29.   Script calls :
  30.   Write this following line to script call to add text on your screen
  31.  
  32.   text(key,x,y,text,z,show,width,height,color1,color2)
  33.  
  34.   Paramaters that must be filled:
  35.   - key  >> Is a hash key. You may fill it with numeric or string. It's used to
  36.             delete your on screen text later
  37.   - x    >> X coordinate from top-left
  38.   - y    >> Y coordinate from top-left
  39.   - text >> Is the text you want to display on screen. It's support escape
  40.             characters such as \N[1] in show text message. But, you have to use
  41.             double slash to activate. For example \\N[1]
  42.  
  43.   Parameters that can be ommited :
  44.   - z      >> Z coordinate on screen. The larger number means the closer to
  45.               player text will be displayed. If it's ommited, the default value
  46.               is 0.
  47.   - show   >> show duration in frame. If you want to text stay on screen until
  48.               you delete it manualy, use -1. The default value is -1
  49.   - width  >> width of a rectangle that will be used to draw a box
  50.   - height >> height of a rectangle that will be used to draw a box
  51.   - color1 >> Color of the rectangle. Must be filled by
  52.               Color.new(red,green,blue,alpha)
  53.   - color2 >> Second Color of the rectangle. The default value is same as
  54.               color1
  55.  
  56.   To delete a certain text. Use this script call
  57.   del_text(key)
  58.  
  59.   Key is a hash key. Same as above
  60.  
  61.   To clear entire screen, use
  62.   clear_texts
  63.  
  64.   ----------------------------------------------------------------------------
  65.   Terms of use :
  66.   Credit me, TheoAllen. You are free to edit this script by your own. As long
  67.   as you don't claim it yours. For commercial purpose, don't forget to give me
  68.   a free copy of the game.
  69.  
  70.   LiTTleDRAgo:
  71.   Optional to credit me.
  72.  
  73. =end
  74. # =============================================================================
  75. # No configuration is avalaible
  76. # Do not touch anything pass this line
  77. # =============================================================================
  78. class Interpreter
  79.  
  80.   TextDTStruct = Struct.new(:key,:x,:y,:z,:width,:height,
  81.                           :text,:show,:color1,:color2)
  82.   def text(key,x,y,text,z = 0,show = -1, width = 0, height = 0,
  83.       color1 = Color.new(255,255,255), color2 = color1)
  84.     return if $game_system.anytexts.include?(key)
  85.     txt = TextDTStruct.new(key,x,y,z,width,height,text,show,color1,color2)
  86.     $game_system.anytexts[key] = txt
  87.     $game_temp.text_to_add.push(key)
  88.   end
  89.  
  90.   def del_text(key)
  91.     return unless $game_system.anytexts.include?(key)
  92.     $game_temp.text_to_delete.push(key)
  93.   end
  94.  
  95.   def clear_texts
  96.     $game_system.anytexts.keys.each { |key| del_text(key) }
  97.   end
  98.  
  99. end
  100.  
  101. class Game_Temp
  102.   define_method(:text_to_add)    { @text_to_add    ||= [] }
  103.   define_method(:text_to_delete) { @text_to_delete ||= [] }
  104. end
  105.  
  106. class Game_System
  107.   define_method(:anytexts) { @anytexts ||= {} }
  108. end
  109.  
  110. class Anywhere_Text < Window_Base
  111.   attr_reader :key
  112.   attr_writer :viewport  unless method_defined?(:viewport=)
  113.  
  114.   def initialize(key, viewport)
  115.     pad = standard_padding
  116.     super(-pad, -pad, window_wh.at(0)+pad, window_wh.at(1)+pad)
  117.     self.viewport = viewport
  118.     self.opacity  = 0    
  119.     self.contents = Bitmap.new(width - 32, height - 32)
  120.     contents.fill_rect(contents.rect,Color.new(0,0,0,0))
  121.     load_data(key)
  122.     draw_contents
  123.   end
  124.  
  125.   unless method_defined?(:standard_padding)
  126.     define_method(:standard_padding) { 12 }
  127.   end
  128.  
  129.   unless method_defined?(:draw_text_ex)
  130.     raise("You need Drago Core Engine to use this script in RPG Maker XP")
  131.   end
  132.  
  133.   def window_wh
  134.     return [Graphics.width, Graphics.height] if Graphics.respond_to?(:width)
  135.     return [640,480]
  136.   end
  137.  
  138.   def load_data(key)
  139.     @data = $game_system.anytexts[@key = key]
  140.     @text = @data.text
  141.     @xpos = @data.x
  142.     @ypos = @data.y
  143.     @w = @data.width
  144.     @h = @data.height
  145.     @color1 = @data.color1
  146.     @color2 = @data.color2
  147.     self.z = @data.z
  148.   end
  149.  
  150.   def draw_contents
  151.     rect = Rect.new(@xpos,@ypos,@w,@h)
  152.     contents.gradient_fill_rect(rect,@color1,@color2)
  153.     draw_text_ex(@xpos,@ypos,@text)
  154.   end
  155.  
  156.   def update
  157.     super
  158.     update_dispose
  159.   end
  160.  
  161.   def update_dispose
  162.     @data.show = [@data.show - 1, -1].max
  163.     self.dispose if @data.show == 0
  164.   end
  165.  
  166. end
  167.  
  168. class Text_Hash
  169.  
  170.   def initialize(viewport = nil)
  171.     @viewport, @data = viewport, {}    
  172.     init_used_text
  173.   end
  174.  
  175.   def init_used_text
  176.     $game_system.anytexts.keys.each { |key| add(key) }
  177.   end
  178.  
  179.   def update
  180.     update_disposed
  181.     update_change
  182.     update_text
  183.   end
  184.  
  185.   def update_disposed
  186.     @data.values.each { |text| text.disposed? && delete(text.key) }
  187.   end
  188.  
  189.   def update_change    
  190.     delete($game_temp.text_to_delete.shift)
  191.     add($game_temp.text_to_add.shift)
  192.   end
  193.  
  194.   def update_text
  195.     @data.values.each{|text| text.disposed? ? delete(text.key) : text.update }
  196.   end
  197.  
  198.   def delete(key)
  199.     return if key.nil?
  200.     $game_system.anytexts.delete(key)
  201.     (text = @data.delete(key)) && !text.disposed? && text.dispose
  202.   end
  203.  
  204.   def add(key)
  205.     return if key.nil?
  206.     @data[key] && !@data[key].disposed? && @data[key].dispose
  207.     @data[key] = Anywhere_Text.new(key,@viewport)
  208.   end
  209.  
  210.   def dispose
  211.     @data.values.each {|text| text.disposed? || text.dispose}
  212.   end
  213.  
  214. end
  215.  
  216. class Spriteset_Map
  217.  
  218.   unless method_defined?(:theo_update_anytexts)
  219.     alias_method(:theo_update_anytexts, :update)
  220.     alias_method(:theo_dispose_anytexts, :dispose)
  221.   end
  222.  
  223.   def update
  224.     theo_update_anytexts
  225.     update_anytexts
  226.   end
  227.  
  228.   def dispose
  229.     theo_dispose_anytexts
  230.     @anytexts.dispose
  231.   end
  232.  
  233.   def update_anytexts
  234.     @anytexts ||= Text_Hash.new(@viewport2)
  235.     @anytexts.update
  236.   end  
  237. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement