Queenvespi

Portrait Script

Sep 26th, 2024 (edited)
2,587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.59 KB | Gaming | 0 0
  1. #===============================================================================
  2. # pbMessageDisplay with portrait and header box support
  3. #===============================================================================
  4. def pbMessageDisplay(msgwindow, message, letterbyletter = true, commandProc = nil)
  5.   return if !msgwindow
  6.   oldletterbyletter = msgwindow.letterbyletter
  7.   msgwindow.letterbyletter = (letterbyletter) ? true : false
  8.   ret = nil
  9.   commands = nil
  10.   facewindow = nil
  11.   headerwindow = nil  # To display the header (name)
  12.   choicewindow = nil  # To display choices
  13.   goldwindow = nil
  14.   coinwindow = nil
  15.   battlepointswindow = nil
  16.   cmdvariable = 0
  17.   cmdIfCancel = 0
  18.   msgwindow.waitcount = 0
  19.   autoresume = false
  20.   text = message.clone
  21.   linecount = (Graphics.height > 400) ? 3 : 2
  22.  
  23.   # Text replacement
  24.   text.gsub!(/\\sign\[([^\]]*)\]/i) { next "\\op\\cl\\ts[]\\w[" + $1 + "]" }
  25.   text.gsub!(/\\\\/, "\5")
  26.   text.gsub!(/\\1/, "\1")
  27.   if $game_actors
  28.     text.gsub!(/\\n\[([1-8])\]/i) { next $game_actors[$1.to_i].name }
  29.   end
  30.   text.gsub!(/\\pn/i,  $player.name) if $player
  31.   text.gsub!(/\\pm/i,  _INTL("${1}", $player.money.to_s_formatted)) if $player
  32.   text.gsub!(/\\n/i,   "\n")
  33.   text.gsub!(/\\\[([0-9a-f]{8,8})\]/i) { "<c2=" + $1 + ">" }
  34.   text.gsub!(/\\pg/i,  "\\b") if $player&.male?
  35.   text.gsub!(/\\pg/i,  "\\r") if $player&.female?
  36.   text.gsub!(/\\pog/i, "\\r") if $player&.male?
  37.   text.gsub!(/\\pog/i, "\\b") if $player&.female?
  38.   text.gsub!(/\\pg/i,  "")
  39.   text.gsub!(/\\pog/i, "")
  40.   male_text_tag = shadowc3tag(MessageConfig::MALE_TEXT_MAIN_COLOR, MessageConfig::MALE_TEXT_SHADOW_COLOR)
  41.   female_text_tag = shadowc3tag(MessageConfig::FEMALE_TEXT_MAIN_COLOR, MessageConfig::FEMALE_TEXT_SHADOW_COLOR)
  42.   text.gsub!(/\\b/i,   male_text_tag)
  43.   text.gsub!(/\\r/i,   female_text_tag)
  44.   text.gsub!(/\\[Ww]\[([^\]]*)\]/) do
  45.     w = $1.to_s
  46.     if w == ""
  47.       msgwindow.windowskin = nil
  48.     else
  49.       msgwindow.setSkin("Graphics/Windowskins/#{w}", false)
  50.     end
  51.     next ""
  52.   end
  53.   isDarkSkin = isDarkWindowskin(msgwindow.windowskin)
  54.   text.gsub!(/\\c\[([0-9]+)\]/i) do
  55.     next getSkinColor(msgwindow.windowskin, $1.to_i, isDarkSkin)
  56.   end
  57.   loop do
  58.     last_text = text.clone
  59.     text.gsub!(/\\v\[([0-9]+)\]/i) { $game_variables[$1.to_i] }
  60.     break if text == last_text
  61.   end
  62.   loop do
  63.     last_text = text.clone
  64.     text.gsub!(/\\l\[([0-9]+)\]/i) do
  65.       linecount = [1, $1.to_i].max
  66.       next ""
  67.     end
  68.     break if text == last_text
  69.   end
  70.   colortag = ""
  71.   if $game_system && $game_system.message_frame != 0
  72.     colortag = getSkinColor(msgwindow.windowskin, 0, true)
  73.   else
  74.     colortag = getSkinColor(msgwindow.windowskin, 0, isDarkSkin)
  75.   end
  76.   text = colortag + text
  77.  
  78.   # Controls
  79.   textchunks = []
  80.   controls = []
  81.   while text[/(?:\\(f|ff|ts|cl|me|se|wt|wtnp|ch|header|portrait)\[([^\]]*)\]|\\(g|cn|pt|wd|wm|op|cl|wu|\.|\||\!|\^))/i]
  82.     textchunks.push($~.pre_match)
  83.     if $~[1]
  84.       controls.push([$~[1].downcase, $~[2], -1])
  85.     else
  86.       controls.push([$~[3].downcase, "", -1])
  87.     end
  88.     text = $~.post_match
  89.   end
  90.   textchunks.push(text)
  91.   textchunks.each do |chunk|
  92.     chunk.gsub!(/\005/, "\\")
  93.   end
  94.   textlen = 0
  95.   controls.length.times do |i|
  96.     control = controls[i][0]
  97.     case control
  98.     when "wt", "wtnp", ".", "|"
  99.       textchunks[i] += "\2"
  100.     when "!"
  101.       textchunks[i] += "\1"
  102.     end
  103.     textlen += toUnformattedText(textchunks[i]).scan(/./m).length
  104.     controls[i][2] = textlen
  105.   end
  106.   text = textchunks.join
  107.   appear_timer_start = nil
  108.   appear_duration = 0.5   # In seconds
  109.   haveSpecialClose = false
  110.   specialCloseSE = ""
  111.   startSE = nil
  112.   controls.length.times do |i|
  113.     control = controls[i][0]
  114.     param = controls[i][1]
  115.     case control
  116.     when "op"
  117.       appear_timer_start = System.uptime
  118.     when "cl"
  119.       text = text.sub(/\001\z/, "")   # fix: '$' can match end of line as well
  120.       haveSpecialClose = true
  121.       specialCloseSE = param
  122.     when "f"
  123.       facewindow&.dispose
  124.       facewindow = PictureWindow.new("Graphics/Pictures/Portraits/#{param}")
  125.     when "ff"
  126.       facewindow&.dispose
  127.       facewindow = FaceWindowVX.new(param)
  128.     when "header"   # Display header (name) with the specified name
  129.       headerwindow&.dispose
  130.       headerwindow = Window_AdvancedTextPokemon.new(param)
  131.       headerwindow.setSkin("Graphics/Windowskins/goldskin")
  132.       headerwindow.resizeToFit(headerwindow.text, Graphics.width)
  133.       headerwindow.viewport = msgwindow.viewport
  134.       headerwindow.z = msgwindow.z - 1  # Ensure header window is below the choice window
  135.       # Position the header window opposite the portrait window
  136.       if facewindow && facewindow.x == 0
  137.         headerwindow.x = Graphics.width - headerwindow.width
  138.       else
  139.         headerwindow.x = Graphics.width - headerwindow.width
  140.       end
  141.       headerwindow.y = msgwindow.y - headerwindow.height
  142.       headerwindow.visible = true
  143.     when "portrait"   # Display portrait with the specified filename
  144.       facewindow&.dispose
  145.       facewindow = PictureWindow.new("Graphics/Pictures/Portraits/#{param}")
  146.       facewindow.x = 0
  147.       facewindow.y = Graphics.height - facewindow.height - msgwindow.height
  148.       facewindow.viewport = msgwindow.viewport
  149.       facewindow.z = msgwindow.z - 2  # Ensure face window is below the choice window
  150.     when "ch"
  151.       cmds = param.clone
  152.       cmdvariable = pbCsvPosInt!(cmds)
  153.       cmdIfCancel = pbCsvField!(cmds).to_i
  154.       commands = []
  155.       while cmds.length > 0
  156.         commands.push(pbCsvField!(cmds))
  157.       end
  158.       choicewindow = Window_CommandPokemon.new(commands)
  159.       choicewindow.viewport = msgwindow.viewport
  160.       choicewindow.z = msgwindow.z + 2  # Ensure choice window is above the header window
  161.       pbPositionChoiceWindow(choicewindow, msgwindow)
  162.     when "wtnp", "^"
  163.       text = text.sub(/\001\z/, "")   # fix: '$' can match end of line as well
  164.     when "se"
  165.       if controls[i][2] == 0
  166.         startSE = param
  167.         controls[i] = nil
  168.       end
  169.     end
  170.   end
  171.   if startSE
  172.     pbSEPlay(pbStringToAudioFile(startSE))
  173.   elsif !appear_timer_start && letterbyletter
  174.     pbPlayDecisionSE
  175.   end
  176.   # Position message window
  177.   pbRepositionMessageWindow(msgwindow, linecount)
  178.   if facewindow
  179.     facewindow.y = Graphics.height - facewindow.height - msgwindow.height
  180.     facewindow.viewport = msgwindow.viewport
  181.     facewindow.z        = msgwindow.z - 2
  182.   end
  183.   atTop = (msgwindow.y == 0)
  184.   # Show text
  185.   msgwindow.text = text
  186.   loop do
  187.     if appear_timer_start
  188.       y_start = (atTop) ? -msgwindow.height : Graphics.height
  189.       y_end = (atTop) ? 0 : Graphics.height - msgwindow.height
  190.       msgwindow.y = lerp(y_start, y_end, appear_duration, appear_timer_start, System.uptime)
  191.       appear_timer_start = nil if msgwindow.y == y_end
  192.     end
  193.     controls.length.times do |i|
  194.       next if !controls[i]
  195.       next if controls[i][2] > msgwindow.position || msgwindow.waitcount != 0
  196.       control = controls[i][0]
  197.       param = controls[i][1]
  198.       case control
  199.       when "f"
  200.         facewindow&.dispose
  201.         facewindow = PictureWindow.new("Graphics/Pictures/Portraits/#{param}")
  202.         facewindow.y = Graphics.height - facewindow.height - msgwindow.height
  203.         facewindow.viewport = msgwindow.viewport
  204.         facewindow.z        = msgwindow.z - 2
  205.       when "ff"
  206.         facewindow&.dispose
  207.         facewindow = FaceWindowVX.new(param)
  208.         facewindow.y = Graphics.height - facewindow.height - msgwindow.height
  209.         facewindow.viewport = msgwindow.viewport
  210.         facewindow.z        = msgwindow.z - 2
  211.       when "g"      # Display gold window
  212.         goldwindow&.dispose
  213.         goldwindow = pbDisplayGoldWindow(msgwindow)
  214.       when "cn"     # Display coins window
  215.         coinwindow&.dispose
  216.         coinwindow = pbDisplayCoinsWindow(msgwindow, goldwindow)
  217.       when "pt"     # Display battle points window
  218.         battlepointswindow&.dispose
  219.         battlepointswindow = pbDisplayBattlePointsWindow(msgwindow)
  220.       when "wu"
  221.         atTop = true
  222.         msgwindow.y = 0
  223.         if facewindow
  224.           facewindow.y = Graphics.height - facewindow.height - msgwindow.height
  225.         end
  226.       when "wm"
  227.         atTop = false
  228.         msgwindow.y = (Graphics.height - msgwindow.height) / 2
  229.         if facewindow
  230.           facewindow.y = Graphics.height - facewindow.height - msgwindow.height
  231.         end
  232.       when "wd"
  233.         atTop = false
  234.         msgwindow.y = Graphics.height - msgwindow.height
  235.         if facewindow
  236.           facewindow.y = Graphics.height - facewindow.height - msgwindow.height
  237.         end
  238.       when "ts"     # Change text speed
  239.         msgwindow.textspeed = (param == "") ? 0 : param.to_i / 80.0
  240.       when "."      # Wait 0.25 seconds
  241.         msgwindow.waitcount += 0.25
  242.       when "|"      # Wait 1 second
  243.         msgwindow.waitcount += 1.0
  244.       when "wt"     # Wait X/20 seconds
  245.         param = param.sub(/\A\s+/, "").sub(/\s+\z/, "")
  246.         msgwindow.waitcount += param.to_i / 20.0
  247.       when "wtnp"   # Wait X/20 seconds, no pause
  248.         param = param.sub(/\A\s+/, "").sub(/\s+\z/, "")
  249.         msgwindow.waitcount = param.to_i / 20.0
  250.         autoresume = true
  251.       when "^"      # Wait, no pause
  252.         autoresume = true
  253.       when "se"     # Play SE
  254.         pbSEPlay(pbStringToAudioFile(param))
  255.       when "me"     # Play ME
  256.         pbMEPlay(pbStringToAudioFile(param))
  257.       end
  258.       controls[i] = nil
  259.     end
  260.     break if !letterbyletter
  261.     Graphics.update
  262.     Input.update
  263.     facewindow&.update
  264.     headerwindow&.update
  265.     choicewindow&.update
  266.     if autoresume && msgwindow.waitcount == 0
  267.       msgwindow.resume if msgwindow.busy?
  268.       break if !msgwindow.busy?
  269.     end
  270.     if Input.trigger?(Input::USE) || Input.trigger?(Input::BACK)
  271.       if msgwindow.busy?
  272.         pbPlayDecisionSE if msgwindow.pausing?
  273.         msgwindow.resume
  274.       elsif !appear_timer_start
  275.         break
  276.       end
  277.     end
  278.     pbUpdateSceneMap
  279.     msgwindow.update
  280.     yield if block_given?
  281.     break if (!letterbyletter || commandProc || commands) && !msgwindow.busy?
  282.   end
  283.   Input.update   # Must call Input.update again to avoid extra triggers
  284.   msgwindow.letterbyletter = oldletterbyletter
  285.   if commands
  286.     $game_variables[cmdvariable] = pbShowCommands(msgwindow, commands, cmdIfCancel)
  287.     $game_map.need_refresh = true if $game_map
  288.   end
  289.   ret = commandProc.call(msgwindow) if commandProc
  290.   goldwindow&.dispose
  291.   coinwindow&.dispose
  292.   battlepointswindow&.dispose
  293.   facewindow&.dispose
  294.   headerwindow&.dispose  # Dispose of the header window
  295.   choicewindow&.dispose  # Dispose of the choice window
  296.   if haveSpecialClose
  297.     pbSEPlay(pbStringToAudioFile(specialCloseSE))
  298.     atTop = (msgwindow.y == 0)
  299.     y_start = (atTop) ? 0 : Graphics.height - msgwindow.height
  300.     y_end = (atTop) ? -msgwindow.height : Graphics.height
  301.     disappear_duration = 0.5   # In seconds
  302.     disappear_timer_start = System.uptime
  303.     loop do
  304.       msgwindow.y = lerp(y_start, y_end, disappear_duration, disappear_timer_start, System.uptime)
  305.       Graphics.update
  306.       Input.update
  307.       pbUpdateSceneMap
  308.       msgwindow.update
  309.       break if msgwindow.y == y_end
  310.     end
  311.   end
  312.   return ret
  313. end
  314.  
  315. #===============================================================================
  316. # Usage Examples
  317. #===============================================================================
  318. # In your event, you can use the following commands in the text to display a portrait and header box:
  319. # - \portrait[PortraitName]   # Shows a portrait image from Graphics/Pictures/
  320. # - \header[CharacterName]    # Displays the header (name) opposite the portrait
  321.  
Advertisement
Add Comment
Please, Sign In to add comment