DasKyu

Untitled

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