molegato

Clean message window

Aug 10th, 2012
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.90 KB | None | 0 0
  1. #==============================================================================
  2. # CLEAN MESSAGE WINDOW
  3. # Author Molegato
  4. # Version 1.0.2
  5. #------------------------------------------------------------------------------
  6. # Just some small size fixes to the message window, without adding new features
  7. # Now the window can change its size to fit text (vertically) and automatically
  8. # add line jumps when a line is too long.
  9. # Also is possible to change the maximum line number and to actively change if
  10. # the message box fits vertically the text or not with a script call.
  11. # To do so, use this:
  12. #     $game_system.scalewindow=false/true
  13. #==============================================================================
  14.  
  15. $imported = {} if $imported.nil?
  16. $imported['Molegato-Clean_message'] = true
  17.  
  18. #==============================================================================
  19. # CONFIGURATION, YOU CAN TOUCH THIS
  20. #==============================================================================
  21. module CLEAN_MESSAGE
  22.   #Default maximum number of lines. more than 4 will result in huge windows
  23.   LINE_NUMBER=4
  24.  
  25.   #Default state of the scalation. if set to true, the window will fit vertically
  26.   #the text.
  27.   SCALE_WINDOW=true
  28. end
  29. #==============================================================================
  30. # END OF CONFIGURATION. EVERYTHING UNDER HERE IS A HELLISH MESS OF DEFICIENT
  31. # AMATEUR SCRIPTING. BE AWARE THAT MESSING WITH IT CAN RESULT IN DISASTER
  32. #==============================================================================
  33.  
  34.  
  35. class Game_System
  36.   attr_accessor :scalewindow
  37.  
  38.   alias molegato_clean_message_initialize initialize
  39.   def initialize
  40.     molegato_clean_message_initialize
  41.     @scalewindow=CLEAN_MESSAGE::SCALE_WINDOW
  42.   end
  43. end
  44.  
  45. class Window_Message < Window_Base
  46.  
  47.   alias molegato_initialize initialize
  48.   def initialize
  49.     @current_text=""
  50.     molegato_initialize
  51.   end
  52.  
  53.   def visible_line_number
  54.     lines=0
  55.     @current_text.each_line do |line|
  56.       lines+=1
  57.     end
  58.     if $game_system.scalewindow
  59.       return [[lines,minimum_height].max(),CLEAN_MESSAGE::LINE_NUMBER].min
  60.     else
  61.       return CLEAN_MESSAGE::LINE_NUMBER
  62.     end
  63.   end
  64.    
  65.   def minimum_height
  66.     if $game_message.face_name.empty?
  67.       return 1
  68.     else
  69.       return 4
  70.     end
  71.   end
  72.  
  73.   def contents_height
  74.     fitting_height(CLEAN_MESSAGE::LINE_NUMBER) - standard_padding * 2
  75.   end
  76.  
  77.   alias molegato_update update
  78.   def update
  79.     self.height=window_height
  80.     molegato_update
  81.     #update_placement
  82.   end
  83.  
  84.   def open_and_wait
  85.     open
  86.     create_back_bitmap
  87.     create_back_sprite
  88.    
  89.     until open?
  90.       Fiber.yield
  91.       update_placement
  92.     end
  93.   end
  94.  
  95.   def process_all_text
  96.     pos = {}
  97.     text = convert_escape_characters($game_message.all_text)
  98.     text = text_with_autojumps(text)
  99.     @current_text=text.clone()
  100.     open_and_wait
  101.     new_page(text, pos)
  102.     process_character(text.slice!(0, 1), text, pos) until text.empty?
  103.   end
  104.  
  105.   def settings_changed?
  106.     text = convert_escape_characters($game_message.all_text)
  107.     text = text_with_autojumps(text)
  108.     lines=0
  109.     text.each_line do |line|
  110.       lines+=1
  111.     end
  112.    
  113.     @background != $game_message.background ||
  114.     @position != $game_message.position ||
  115.     lines != visible_line_number
  116.   end
  117.    
  118.   def text_with_autojumps(text)  
  119.     totaltext=''
  120.     lines=text.split("\n")
  121.     lines.each do |current_line|
  122.       words=current_line.split(' ')
  123.       lineatotal=''
  124.       linea=''
  125.       words.each do |current_word|
  126.         if contents.text_size(linea+current_word).width<=contents_width-new_line_x
  127.           linea+=current_word+' '
  128.         else
  129.           linea+="\n"
  130.           lineatotal+=linea
  131.           linea=current_word+' '
  132.         end
  133.       end
  134.       lineatotal+=linea
  135.       current_line=lineatotal+"\n"
  136.       totaltext+=current_line
  137.     end
  138.     return totaltext
  139.   end
  140.  
  141. end
Advertisement
Add Comment
Please, Sign In to add comment