Advertisement
Guest User

BigFace - 1.0

a guest
Jan 26th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.84 KB | None | 0 0
  1. #BigFace by Core
  2. #1.0
  3.  
  4. #big faceset should be 8 times longer in width than usual
  5.  
  6. module BigFace
  7.   def self.load_face(face_name)
  8.     bitmap = ''
  9.    
  10.     begin
  11.       begin
  12.         bitmap = Cache.face(face_name)
  13.       rescue Exception => e
  14.         raise
  15.       end
  16.     rescue
  17.       return
  18.     end
  19.    
  20.     return bitmap
  21.   end
  22.  
  23.   def self.convert_data(data)
  24.     data['width'] = data['width'].to_i
  25.     data['height'] = data['height'].to_i
  26.     data['textX'] = data['textX'].to_i
  27.     data['z'] = data['z'].to_i
  28.     data['frames'] = data['frames'].to_i
  29.     data['frameSpeed'] = data['frameSpeed'].to_i  
  30.     data['align'] = data['align'].delete!("\n")
  31.    
  32.     if data['alwaysTalk'] == "true" then
  33.       data['alwaysTalk'] = true
  34.     else
  35.       data['alwaysTalk'] = false
  36.     end
  37.    
  38.     data['height'] = data['height'] / data['frames']
  39.    
  40.     return data
  41.   end
  42.  
  43.   def self.face_data(bitmap, face_name)
  44.     data = {
  45.       "width" => bitmap.width / 8,
  46.       "height" => bitmap.height,
  47.       "textX" => 0,
  48.       "frames" => 1,
  49.       "frameSpeed" => 3,
  50.       "z" => 201,
  51.       "align" => "bottom",
  52.       "alwaysTalk" => false,
  53.     }
  54.    
  55.     face_name = "Graphics/Faces/" + face_name + ".txt"
  56.    
  57.     if not File.exists?(face_name)
  58.         return data
  59.     end
  60.    
  61.     File.foreach(face_name) { |line|
  62.       array = line.split('=')
  63.       data[array[0]] = array[1]
  64.     }
  65.    
  66.     return convert_data(data)
  67.   end
  68.  
  69.   def self.draw_face(window, face_name, face_index, x, y, enabled = true)
  70.     face_name = face_name + "-big"
  71.     faceBitmap = load_face(face_name)
  72.  
  73.     if not faceBitmap then return true end
  74.    
  75.     data = face_data(faceBitmap, face_name)
  76.    
  77.     width = data['width']
  78.     height = data['height']
  79.  
  80.     rect = Rect.new(face_index * width, 0, width, height)
  81.     bitmap = Bitmap.new(width, height)
  82.     bitmap.blt(0, 0, faceBitmap, rect, enabled ? 255 : translucent_alpha)
  83.  
  84.     face = window.face
  85.    
  86.     window.faceData = data
  87.     window.faceBitmap = faceBitmap
  88.     window.faceIndex = face_index
  89.    
  90.     face.bitmap = bitmap
  91.     face.z = data['z']
  92.    
  93.     if data['align'] == 'bottom' or not data['align']
  94.       face.y = face.y + (window.height - height) - window.padding
  95.     elsif data['align'] == 'top'
  96.       face.y = window.y - height
  97.     end
  98.  
  99.     return false
  100.   end
  101.  
  102.   def self.update_face(window)
  103.     face = window.face
  104.     data = window.faceData
  105.    
  106.     if data.empty?() then return end
  107.  
  108.     faceBitmap = window.faceBitmap
  109.        
  110.     width = data['width']
  111.     height = data['height']
  112.     frame = ((window.talkFrame * data['frameSpeed']) % data['frames']).floor()
  113.  
  114.     rect = Rect.new(window.faceIndex * width, height * frame, width, height)
  115.     bitmap = Bitmap.new(width, height)
  116.     bitmap.blt(0, 0, faceBitmap, rect)
  117.     face.bitmap = bitmap
  118.   end
  119. end
  120.  
  121. #Modifying draw_face function
  122.  
  123. class Window_Base < Window
  124.   alias old_draw_face draw_face
  125.   alias old_initialize initialize
  126.   alias old_close close
  127.  
  128.   attr_accessor :faceData, :faceBitmap, :faceIndex, :face, :talkFrame
  129.  
  130.   def close
  131.     dispose_face
  132.     old_close
  133.   end
  134.  
  135.   def dispose_face
  136.     if @face.bitmap then @face.bitmap.dispose end
  137.     if @faceBitmap then @faceBitmap.dispose end
  138.      
  139.     @face.dispose
  140.   end
  141.  
  142.   def clear_face  
  143.     if @face
  144.       dispose_face
  145.     end
  146.    
  147.     @talkFrame = 0.0
  148.     @faceData = {}
  149.    
  150.     @face = Sprite.new
  151.     @face.x = self.x + self.padding
  152.     @face.y = self.y
  153.   end
  154.  
  155.   def initialize(x, y, width, height)
  156.     old_initialize(x, y, width, height)
  157.     clear_face
  158.   end
  159.  
  160.   def draw_face(face_name, face_index, x, y, enabled = true)
  161.     clear_face
  162.     drawOld = BigFace::draw_face(self, face_name, face_index, x, y, enabled)
  163.    
  164.     if drawOld
  165.       old_draw_face(face_name, face_index, x, y, enabled)
  166.     end
  167. #~     bitmap = Cache.face(face_name)
  168. #~     rect = Rect.new(face_index % 4 * 96, face_index / 4 * 96, 96, 96)
  169. #~     contents.blt(x, y, bitmap, rect, enabled ? 255 : translucent_alpha)
  170. #~     bitmap.dispose
  171.   end
  172. end
  173.  
  174. #for talk frames
  175. class Window_Message < Window_Base
  176.   alias old_process_character process_character
  177.   alias old_wait wait
  178.   alias old_process_all_text process_all_text
  179.   alias old_new_line_x new_line_x
  180.  
  181.   def new_line_x
  182.     if @faceData.empty?()
  183.       old_new_line_x
  184.     else
  185.       @faceData['width'] + @faceData['textX']
  186.     end
  187.   end
  188.  
  189.   def update_face_talk
  190.     BigFace::update_face(self)
  191.   end
  192.  
  193.   def can_talk
  194.     if not @faceData['alwaysTalk'] then @talkFrame = 0.0 end
  195.   end
  196.  
  197.   def process_character(c, text, pos)
  198.     @talkFrame = @talkFrame + 0.1
  199.    
  200.     if c == "\e"
  201.       can_talk
  202.     end
  203.    
  204.     update_face_talk
  205.    
  206.     old_process_character(c, text, pos)
  207.   end
  208.  
  209.   def process_all_text
  210.     old_process_all_text
  211.  
  212.     can_talk
  213.     update_face_talk
  214.   end
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement