TheoAllen

Theo - Anywhere Texts On Screen (ENG)

Aug 26th, 2013
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.15 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. # =============================================================================
  7. ($imported ||= {})[:Theo_AnywhereText] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.08.26 - Finished script
  12. # 2013.08.25 - Started script
  13. # =============================================================================
  14. =begin
  15.  
  16.   ----------------------------------------------------------------------------
  17.   Introduction :
  18.   This script allow you to have many texts written on screen.
  19.  
  20.   ----------------------------------------------------------------------------
  21.   How to use :
  22.   Simply put this script below material but above main
  23.   Read the manual
  24.  
  25.   ----------------------------------------------------------------------------
  26.   Script calls :
  27.   Write this following line to script call to add text on your screen
  28.  
  29.   text(key,x,y,text,z,show,width,height,color1,color2)
  30.  
  31.   Paramaters that must be filled:
  32.   - key  >> Is a hash key. You may fill it with numeric or string. It's used to
  33.             delete your on screen text later
  34.   - x    >> X coordinate from top-left
  35.   - y    >> Y coordinate from top-left
  36.   - text >> Is the text you want to display on screen. It's support escape
  37.             characters such as \N[1] in show text message. But, you have to use
  38.             double slash to activate. For example \\N[1]
  39.  
  40.   Parameters that can be ommited :
  41.   - z      >> Z coordinate on screen. The larger number means the closer to
  42.               player text will be displayed. If it's ommited, the default value
  43.               is 0.
  44.   - show   >> show duration in frame. If you want to text stay on screen until
  45.               you delete it manualy, use -1. The default value is -1
  46.   - width  >> width of a rectangle that will be used to draw a box
  47.   - height >> height of a rectangle that will be used to draw a box
  48.   - color1 >> Color of the rectangle. Must be filled by
  49.               Color.new(red,green,blue,alpha)
  50.   - color2 >> Second Color of the rectangle. The default value is same as
  51.               color1
  52.  
  53.   To delete a certain text. Use this script call
  54.   del_text(key)
  55.  
  56.   Key is a hash key. Same as above
  57.  
  58.   To clear entire screen, use
  59.   clear_texts
  60.  
  61.   ----------------------------------------------------------------------------
  62.   Terms of use :
  63.   Credit me, TheoAllen. You are free to edit this script by your own. As long
  64.   as you don't claim it yours. For commercial purpose, don't forget to give me
  65.   a free copy of the game.
  66.  
  67. =end
  68. # =============================================================================
  69. # No configuration is avalaible
  70. # Do not touch anything pass this line
  71. # =============================================================================
  72. class Game_Interpreter
  73.  
  74.   def text(key,x,y,text,z = 0,show = -1, width = 0, height = 0,
  75.       color1 = Color.new, color2=color1)
  76.     return if $game_system.anytexts.include?(key)
  77.     txt = TextDTStruct.new(key,x,y,z,width,height,text,show,color1,color2)
  78.     $game_system.anytexts[key] = txt
  79.     $game_temp.text_to_add = key
  80.     Fiber.yield
  81.   end
  82.  
  83.   def del_text(key)
  84.     return unless $game_system.anytexts.include?(key)
  85.     $game_temp.text_to_delete = key
  86.     Fiber.yield
  87.   end
  88.  
  89.   def clear_texts
  90.     $game_system.anytexts.keys.each do |key|
  91.       del_text(key)
  92.     end
  93.   end
  94.  
  95. end
  96.  
  97. class Game_Temp
  98.   attr_accessor :text_to_delete
  99.   attr_accessor :text_to_add
  100.  
  101.   alias theo_anytext_init initialize
  102.   def initialize
  103.     theo_anytext_init
  104.     @text_to_delete = nil
  105.     @text_to_add = nil
  106.   end
  107.  
  108. end
  109.  
  110. class Game_System
  111.   attr_reader :anytexts
  112.  
  113.   alias theo_anytext_init initialize
  114.   def initialize
  115.     theo_anytext_init
  116.     @anytexts = {}
  117.   end
  118.  
  119. end
  120.  
  121. class TextDTStruct
  122.   attr_accessor :key
  123.   attr_accessor :x
  124.   attr_accessor :y
  125.   attr_accessor :z
  126.   attr_accessor :width
  127.   attr_accessor :height
  128.   attr_accessor :text
  129.   attr_accessor :show
  130.   attr_accessor :color1
  131.   attr_accessor :color2
  132.  
  133.   def initialize(key, x, y, z, width, height, text, show, color1, color2)
  134.     @key = key
  135.     @x = x
  136.     @y = y
  137.     @z = z
  138.     @width = width
  139.     @height = height
  140.     @text = text
  141.     @show = show
  142.     @color1 = color1
  143.     @color2 = color2
  144.   end
  145.  
  146. end
  147.  
  148. class Anywhere_Text < Window_Base
  149.   attr_reader :key
  150.  
  151.   def initialize(key,viewport)
  152.     pad = standard_padding
  153.     super(-pad,-pad,Graphics.width+pad,Graphics.height+pad)
  154.     self.viewport = viewport
  155.     self.opacity = 0
  156.     load_data(key)
  157.     draw_contents
  158.   end
  159.  
  160.   def load_data(key)
  161.     @data = $game_system.anytexts[key]
  162.     @key = @key
  163.     @text = @data.text
  164.     @xpos = @data.x
  165.     @ypos = @data.y
  166.     @w = @data.width
  167.     @h = @data.height
  168.     @color1 = @data.color1
  169.     @color2 = @data.color2
  170.     self.z = @data.z
  171.   end
  172.  
  173.   def draw_contents
  174.     rect = Rect.new(@xpos,@ypos,@w,@h)
  175.     contents.gradient_fill_rect(rect,@color1,@color2)
  176.     draw_text_ex(@xpos,@ypos,@text)
  177.   end
  178.  
  179.   def update
  180.     super
  181.     update_dispose
  182.   end
  183.  
  184.   def update_dispose
  185.     @data.show = [@data.show - 1,-1].max
  186.     dispose if @data.show == 0
  187.   end
  188.  
  189. end
  190.  
  191. class Text_Hash
  192.  
  193.   def initialize(viewport = nil)
  194.     @viewport = viewport
  195.     @data = {}
  196.     init_used_text
  197.   end
  198.  
  199.   def init_used_text
  200.     $game_system.anytexts.keys.each do |key|
  201.       add(key)
  202.     end
  203.   end
  204.  
  205.   def update
  206.     update_disposed
  207.     update_delete
  208.     update_add
  209.     update_text
  210.   end
  211.  
  212.   def update_disposed
  213.     @data.values.each do |text|
  214.       next unless text.disposed?
  215.       delete(text.key)
  216.     end
  217.   end
  218.  
  219.   def update_delete
  220.     del_key = $game_temp.text_to_delete
  221.     unless del_key.nil?
  222.       delete(del_key)
  223.       $game_temp.text_to_delete = nil
  224.     end
  225.   end
  226.  
  227.   def update_add
  228.     add_key = $game_temp.text_to_add
  229.     unless add_key.nil?
  230.       add(add_key)
  231.       $game_temp.text_to_add = nil
  232.     end
  233.   end
  234.  
  235.   def update_text
  236.     @data.values.each {|text| text.update unless text.disposed?}
  237.   end
  238.  
  239.   def delete(key)
  240.     text = @data.delete(key)
  241.     $game_system.anytexts.delete(key)
  242.     return unless text
  243.     text.dispose
  244.   end
  245.  
  246.   def add(key)
  247.     new_text = Anywhere_Text.new(key,@viewport)
  248.     @data[key] = new_text
  249.   end
  250.  
  251.   def dispose
  252.     @data.values.each {|text| text.dispose}
  253.   end
  254.  
  255. end
  256.  
  257. class Spriteset_Map
  258.  
  259.   alias theo_anytext_crv create_viewports
  260.   def create_viewports
  261.     theo_anytext_crv
  262.     create_anytexts
  263.   end
  264.  
  265.   def create_anytexts
  266.     @anytexts = Text_Hash.new(@viewport2)
  267.   end
  268.  
  269.   alias theo_anytext_update update
  270.   def update
  271.     theo_anytext_update
  272.     update_anytexts
  273.   end
  274.  
  275.   def update_anytexts
  276.     @anytexts.update
  277.   end
  278.  
  279.   alias theo_anytext_dispose dispose
  280.   def dispose
  281.     theo_anytext_dispose
  282.     dispose_anytexts
  283.   end
  284.  
  285.   def dispose_anytexts
  286.     @anytexts.dispose
  287.   end
  288.  
  289. end
Advertisement
Add Comment
Please, Sign In to add comment