Advertisement
DasKyu

Untitled

Mar 29th, 2021 (edited)
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.59 KB | None | 0 0
  1. ##########################
  2. #    Kyu TextLog V3.0    #
  3. ########CONSTANTS########
  4. PADY = 25 #Márgenes en el eje Y
  5. PADX = 25 #Márgenes en el eje X
  6. INTERPAD = 12 #Distancia entre cuadros de texto.
  7. IMG = nil #Imagen de fondo. Dejar en nil si no se quiere ninguna. Si se usa: "nombreImagen".
  8. DEFAULTCOLOR = "<c3=f8f8f8,0f0f0f>" #Color por defecto que se mostrará en el log
  9. ##########################
  10.  
  11. if defined?(PluginManager)
  12.   PluginManager.register({
  13.     :name => "Kyu´s Textlog",
  14.     :version => "3.0",
  15.     :credits => "Kyu"
  16.   })
  17. end
  18.  
  19. class Log
  20.   def initialize()
  21.     $PokemonGlobal.log ||= [] if !$PokemonGlobal.log
  22.     @pos = $PokemonGlobal.log.length-1
  23.     @viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  24.     @viewport.z = 99999
  25.     @sprite = Sprite.new(@viewport)
  26.    
  27.     #Background.
  28.     if !IMG
  29.       @sprite.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  30.       @sprite.bitmap.fill_rect(0,0,Graphics.width,Graphics.height,Color.new(0,0,0))
  31.       @sprite.opacity = 150
  32.     else
  33.       @sprite.bitmap = Bitmap.new("Graphics/Pictures/"+IMG)
  34.     end
  35.    
  36.     #Text are drawn over this.
  37.     @sprite2 = Sprite.new(@viewport)
  38.     @sprite2.y = PADY
  39.     @sprite2.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  40.     @canvas = @sprite2.bitmap
  41.     pbSetSystemFont(@canvas)
  42.     @totalheight = 0
  43.    
  44.     @w = 0
  45.     @lines = 0 #Lines currently drawn. Used to know how much @pos
  46.     #will increase when pressing up.
  47.    
  48.     #Calculates how many texts can be displayed. This are added to
  49.     #an array and then drawn.
  50.     drawLines()
  51.  
  52.     self.update
  53.   end
  54.  
  55.   def drawLines(down=false)
  56.     if !down
  57.       aux=[]
  58.       while @totalheight <= Graphics.height - 2*PADY and @pos-@w >= 0
  59.         @totalheight+= 32*$PokemonGlobal.log[@pos-@w].length + INTERPAD
  60.         if @totalheight <= Graphics.height - 2*PADY
  61.           aux.push($PokemonGlobal.log[@pos-@w])
  62.         end
  63.         @w+=1
  64.       end
  65.       @totalheight = 0
  66.       for i in 0...aux.length
  67.         for z in 0...aux[aux.length-1-i].length
  68.           chr = getFormattedText(@canvas,PADX,@totalheight+(32*z),
  69.                 Graphics.width - 2*PADX,Graphics.height,
  70.                 aux[aux.length-1-i][z],32)
  71.           drawFormattedChars(@canvas,chr)
  72.         end
  73.         @totalheight += 32*aux[aux.length-1-i].length + INTERPAD
  74.       end
  75.       @lines = aux.length
  76.       @pos+=1
  77.       aux.clear
  78.     else #When pressing down
  79.       while @totalheight <= Graphics.height - 2*PADY and (@pos+@w) <= $PokemonGlobal.log.length - 1
  80.         if @totalheight + 32*$PokemonGlobal.log[@pos + @w].length <= Graphics.height - 2*PADY
  81.           for i in 0...$PokemonGlobal.log[@pos + @w].length
  82.             chr = getFormattedText(@canvas,PADX,@totalheight+(32*i),
  83.                   Graphics.width - 2*PADX, Graphics.height,
  84.                   $PokemonGlobal.log[@pos + @w][i],32)
  85.             drawFormattedChars(@canvas,chr)
  86.           end
  87.         end
  88.         @totalheight += 32*$PokemonGlobal.log[@pos + @w].length + INTERPAD
  89.         @w += 1
  90.       end
  91.       @pos+= @w-1
  92.       @lines = @w-1
  93.     end
  94.   end
  95.  
  96.   def update
  97.     loop do
  98.       if Input.trigger?(Input::DOWN) and @pos < $PokemonGlobal.log.length - 1
  99.         @canvas.clear
  100.         @totalheight = 0
  101.         @w = 0
  102.         #Dibuja cuadros de texto hasta que el siguiente sobrepase el límite.
  103.         drawLines(true)
  104.       end
  105.      
  106.       if Input.trigger?(Input::UP) and @pos - @lines > 0
  107.         @canvas.clear
  108.         @pos-=@lines+1
  109.         @totalheight = 0
  110.         @w = 0
  111.         #Mismo funcionamiento que al iniciar la pantalla.
  112.         drawLines()
  113.       end
  114.      
  115.       if Input.trigger?(Input::B) #Cerrar
  116.         @canvas.clear
  117.         @canvas.dispose
  118.         @sprite.dispose
  119.         @viewport.dispose
  120.         Input.update
  121.         Graphics.update
  122.         break
  123.       end
  124.      
  125.       Input.update
  126.       Graphics.update
  127.     end
  128.   end
  129. end
  130.  
  131. class Scene_Map #Añade la acción de Input F en Scene_Map para abrir el log
  132.   alias update_old update
  133.   def update
  134.     if Input.trigger?(Input::F)
  135.       Log.new
  136.     end
  137.     update_old
  138.   end
  139. end
  140.  
  141.  
  142. class PokemonGlobalMetadata #Añade la variable global log, que guarda todo.
  143.   attr_accessor :log
  144.   alias kyu_initialize initialize
  145.   def initialize
  146.     kyu_initialize
  147.     @log ||= []
  148.   end
  149. end
  150.  
  151. module NameBox #Implementación para dar compatibilidad a la textbox de Bezier.
  152.   def self.getName
  153.     color = shadowc3tag(@namebox.baseColor,@namebox.shadowColor)
  154.     return color+@namebox.text
  155.   end
  156. end
  157.  
  158. def getLineChunks(value) #Divide un texto en varias líneas en función de PADX
  159.   regex = [/<[cC][^>]*>/,/<\/[cC][^>]*>/,/<[bB]>/,/<\/[bB]>/,/<[iI]>/,/<\/[iI]>/,
  160.   /<[uU]>/,/<\/[uU]>/,/<[sS]>/,/<\/[sS]>/,/<outln>/,/<\/outln>/,/<outln2>/,
  161.   /<\/outln2>/,/<fn=\d+>/,/<\/fn>/,/<fs=\d+>/,/<\/fs>/,/<[oO]=\d*>/,/<\/[oO]>/,
  162.   /<ac>/,/<\/ac>/,/<al>/,/<\/al>/,/<ar>/,/<\/ar>/]
  163.   bitmap = Bitmap.new(Graphics.width - 2*PADX,Graphics.height)
  164.   width = Graphics.width - 2*PADX
  165.   pbSetSystemFont(bitmap)
  166.   totalwidth = 0 #Anchura total de la línea actual
  167.   count = 0 #Línea actual
  168.  
  169.   #Color, bold, italic, underlined, struck, outline, thickoutline, font,
  170.   #fontsize, opacity, centered, left-centered,righ-centered
  171.   regs=["","","","","","","","","","","","",""]
  172.   ret=[""] #Array con todas las líneas del texto.
  173.  
  174.   value = value.clone
  175.   text = []
  176.  
  177.   #Adding line breaks after alignments
  178.   value.gsub!(/<ac>/,"\n<ac>")
  179.   value.gsub!(/<\/ac>/,"</ac>\n")
  180.   value.gsub!(/<al>/,"\n<al>")
  181.   value.gsub!(/<\/al>/,"</al>\n")
  182.   value.gsub!(/<ar>/,"\n<ar>")
  183.   value.gsub!(/<\/ar>/,"</ar>\n")
  184.  
  185.   #Procesamiento de los saltos de línea
  186.   while value[/.*((\n)|(<br\/>))/] != nil
  187.     val = value.slice!(/.*((\n)|(<br\/>))/)
  188.     if val[/<r>/]
  189.       val.gsub!(/<r>/,"<ar>")
  190.       val.insert(-2,"</ar>")
  191.     end
  192.     text.push(val)
  193.   end
  194.   #Añade lo que queda después de los saltos de línea
  195.   text.push(value)
  196.  
  197.   #Análisis de todas las palabras del texto línea por línea
  198.   text.each{|line|
  199.     words = line.split
  200.     aux = []
  201.     words.each_index{|i|
  202.       if words[i][/<[^>]*>(<[^>]*>|\w+)/] != nil
  203.         val = words[i].slice!(/<[^>]*>/)
  204.         aux.push(val)
  205.         aux.push(words[i])
  206.       elsif words[i][/\w+<[^>]*>/] != nil
  207.         val = words[i].slice!(/<[^>]*>/)
  208.         aux.push(words[i])
  209.         aux.push(val)
  210.       else
  211.         aux.push(words[i])
  212.       end
  213.     }
  214.     words = aux
  215.     init = "" #Expresiones de apertura Ej: <fs=X>
  216.     ending = "" #Expresiones de cierre Ej: </fs>
  217.    
  218.     words.each{|word|
  219.         if word[/^<.*>$/] == nil
  220.           word+= " "
  221.         end
  222.         init = "" #Expresiones de apertura Ej: <fs=X>
  223.         ending = "" #Expresiones de cierre Ej: </fs>
  224.        
  225.         #Detecta comandos especiales y los activa en regs hasta que encuentra
  226.         #el de cierre.
  227.         regex.each_index{|index|
  228.           aux = word.slice!(regex[index])
  229.           if aux != nil
  230.             if index % 2 != 0 #Comando de cierre
  231.               if index == 1
  232.                 regs[0].slice!(/[^<]*<[cC][^>]*>$/)
  233.                 regs[0] = DEFAULTCOLOR
  234.                 ending += DEFAULTCOLOR
  235.               else
  236.                 regs[((index+1)/2)-1]=""
  237.               end
  238.               ending += aux
  239.               if index == 14 #</fn>
  240.                 pbSetSystemFont(bitmap)
  241.               elsif index == 3 #</b>
  242.                 bitmap.font.bold = false
  243.               elsif index == 5 #</i>
  244.                 bitmap.font.italic = false
  245.               end
  246.             else #Comando de apertura
  247.               regs[index/2] = aux
  248.               init += aux
  249.               if index == 14 #<fn=X>
  250.                 bitmap.font.name = aux.gsub(/fn=/){""}
  251.               elsif index == 2 #<b>
  252.                 bitmap.font.bold = true
  253.               elsif index == 4 #<i>
  254.                 bitmap.font.italic = true
  255.               end
  256.             end
  257.           end
  258.         }
  259.        
  260.         #En word solo queda la palabra neta sin comandos especiales. Se mide
  261.         #cuanto ocupa en bitmap y se añade a totalwidth. En función de ello
  262.         #se realiza un salto de línea o no.
  263.         wordwidth = bitmap.text_size(word).width
  264.         totalwidth += wordwidth
  265.         if totalwidth > width
  266.           count += 1
  267.           ret.push("")
  268.           regs.each{|reg|
  269.             ret[count]+=reg
  270.           }
  271.           ret[count]+= word + ending
  272.           totalwidth = wordwidth
  273.         else
  274.           ret[count] += (init + word + ending)
  275.         end
  276.       }
  277.       count += 1
  278.       ret.push("")
  279.       regs.each{|reg|
  280.         ret[count]+=reg
  281.         }
  282.       ret[count]+=ending
  283.       totalwidth = 0
  284.     }
  285.   return ret
  286. end
  287.  
  288. ################################################################################
  289. #Este método sirve para cambiar los colores de los textos de una textbox para
  290. #el textlog.
  291. #Para añadir un nuevo color, basta con añadir una línea como esta:
  292. #text.gsub!(tag_a_sustituir, nueva_tag)
  293. ################################################################################
  294. def changeSkinColor(text)
  295.     text = text.clone
  296.     text.gsub!("<c2=421F2117>", "<c3=3050c8,c9cbd1>")  # Blue
  297.     text.gsub!("<c3=E82010,F8A8B8>", "<c3=F83018,F8B0A0>")  # Red
  298.     text.gsub!("<c2=7FF05EE8>", "<c2=7FF05EE8>")  # Yellow
  299.     text.gsub!("<c2=7E1F5D17>", "<c2=7E1F5D17>")  # Magenta
  300.     text.gsub!("<c2=43FF22F7>", "<c2=43FF22F7>")  # Cyan
  301.     text.gsub!("<c2=63184210>", "<c2=63184210>")  # Grey
  302.     text.gsub!("<c2=7FFF5EF7>", "<c2=7FFF5EF7>")  # White
  303.     text.gsub!("<c2=6546675A>","<c3=3050c8,c9cbd1>") #\b
  304.     text.gsub!("<c2=043C675A>","<c3=F83018,F8B0A0>") #\r
  305.     return text
  306. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement