DasKyu

Untitled

Feb 15th, 2021
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.17 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. ##########################
  9.  
  10.  
  11. class Log
  12.   def initialize()
  13.     $PokemonGlobal.log ||= [] if !$PokemonGlobal.log
  14.     @pos = $PokemonGlobal.log.length-1
  15.     @viewport = Viewport.new(0,0,Graphics.width,Graphics.height)
  16.     @viewport.z = 99999
  17.     @sprite = Sprite.new(@viewport)
  18.    
  19.     #Crea un fondo negro o carga una imagen según el valor de IMG
  20.     if !IMG
  21.       @sprite.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  22.       @sprite.bitmap.fill_rect(0,0,Graphics.width,Graphics.height,Color.new(0,0,0))
  23.       @sprite.opacity = 150
  24.     else
  25.       @sprite.bitmap = Bitmap.new("Graphics/Pictures/"+IMG)
  26.     end
  27.    
  28.     #Donde se dibujan los textos
  29.     @sprite2 = Sprite.new(@viewport)
  30.     @sprite2.y = PADY
  31.     @sprite2.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  32.     @canvas = @sprite2.bitmap
  33.     pbSetSystemFont(@canvas)
  34.     @totalheight = 0
  35.    
  36.     @w = 0
  37.     @lines = 0 #Lineas en la página actual. Sirve para conocer el aumento de
  38.     #@pos a la hora de pulsar arriba.
  39.  
  40.     #Calcula cuantos textos pueden caber en pantalla, los añade en una
  41.     #array y los dibuja.
  42.     aux = []
  43.     while @totalheight <= Graphics.height - 2*PADY and @pos-@w >= 0
  44.       @totalheight+= 32*$PokemonGlobal.log[@pos-@w].length + INTERPAD
  45.       if @totalheight <= Graphics.height - 2*PADY
  46.         aux.push($PokemonGlobal.log[@pos-@w])
  47.       end
  48.       @w+=1
  49.     end
  50.     @totalheight = 0
  51.     for i in 0...aux.length
  52.       for z in 0...aux[aux.length-1-i].length
  53.         chr = getFormattedText(@canvas,PADX,@totalheight+(32*z),
  54.               Graphics.width - 2*PADX, Graphics.height,
  55.               aux[aux.length-1-i][z],32)
  56.         drawFormattedChars(@canvas,chr)
  57.       end
  58.       @totalheight += 32*aux[aux.length-1-i].length + INTERPAD
  59.     end
  60.     @lines = aux.length
  61.     @pos+=1
  62.     aux.clear
  63.  
  64.     self.update
  65.   end
  66.  
  67.   def update
  68.     loop do
  69.       if Input.trigger?(Input::DOWN) and @pos < $PokemonGlobal.log.length - 1
  70.         @canvas.clear
  71.         @totalheight = 0
  72.         @w = 0
  73.         #Dibuja cuadros de texto hasta que el siguiente sobrepase el límite.
  74.         while @totalheight <= Graphics.height - 2*PADY and (@pos+@w) <= $PokemonGlobal.log.length - 1
  75.           if @totalheight + 32*$PokemonGlobal.log[@pos + @w].length <= Graphics.height - 2*PADY
  76.             for i in 0...$PokemonGlobal.log[@pos + @w].length
  77.               chr = getFormattedText(@canvas,PADX,@totalheight+(32*i),
  78.                     Graphics.width - 2*PADX, Graphics.height,
  79.                     $PokemonGlobal.log[@pos + @w][i],32)
  80.               drawFormattedChars(@canvas,chr)
  81.             end
  82.           end
  83.           @totalheight += 32*$PokemonGlobal.log[@pos + @w].length + INTERPAD
  84.           @w += 1
  85.         end
  86.         @pos+= @w-1
  87.         @lines = @w-1
  88.       end
  89.      
  90.       if Input.trigger?(Input::UP) and @pos - @lines > 0
  91.         @canvas.clear
  92.         @pos-=@lines+1
  93.         @totalheight = 0
  94.         @w = 0
  95.         #Mismo funcionamiento que al iniciar la pantalla.
  96.         aux = []
  97.         while @totalheight <= Graphics.height - 2*PADY and @pos-@w >= 0
  98.           @totalheight+= 32*$PokemonGlobal.log[@pos-@w].length + INTERPAD
  99.           if @totalheight <= Graphics.height - 2*PADY
  100.             aux.push($PokemonGlobal.log[@pos-@w])
  101.           end
  102.           @w+=1
  103.         end
  104.         @totalheight = 0
  105.         for i in 0...aux.length
  106.           for z in 0...aux[aux.length-1-i].length
  107.             chr = getFormattedText(@canvas,PADX,@totalheight+(32*z),
  108.                   Graphics.width - 2*PADX,Graphics.height,
  109.                   aux[aux.length-1-i][z],32)
  110.             drawFormattedChars(@canvas,chr)
  111.           end
  112.           @totalheight += 32*aux[aux.length-1-i].length + INTERPAD
  113.         end
  114.         @lines = aux.length
  115.         @pos+=1
  116.         aux.clear
  117.       end
  118.      
  119.       if Input.trigger?(Input::B) #Cerrar
  120.         @canvas.clear
  121.         @canvas.dispose
  122.         @sprite.dispose
  123.         @viewport.dispose
  124.         Input.update
  125.         Graphics.update
  126.         break
  127.       end
  128.      
  129.       Input.update
  130.       Graphics.update
  131.     end
  132.   end
  133. end
  134.  
  135. class Scene_Map #Añade la acción de Input F en Scene_Map para abrir el log
  136.   alias update_old update
  137.   def update
  138.     if Input.trigger?(Input::F)
  139.       Log.new
  140.     end
  141.     update_old
  142.   end
  143. end
  144.  
  145.  
  146. class PokemonGlobalMetadata #Añade la variable global log, que guarda todo.
  147.   attr_accessor :log
  148.   alias kyu_initialize initialize
  149.   def initialize
  150.     kyu_initialize
  151.     @log ||= []
  152.   end
  153. end
  154.  
  155. def getLineChunks(value) #Divide un texto en varias líneas en función de PADX
  156.   regex = [/<[cC].*>/,/<\/[cC].*>/,/<[bB]>/,/<\/[bB]>/,/<[iI]>/,/<\/[iI]>/,
  157.   /<[uU]>/,/<\/[uU]>/,/<[sS]>/,/<\/[sS]>/,/<outln>/,/<\/outln>/,/<outln2>/,
  158.   /<\/outln2>/,/<fn=\d+>/,/<\/fn>/,/<fs=\d+>/,/<\/fs>/,/<[oO]=\d*>/,/<\/[oO]>/,
  159.   /<ac>/,/<\/ac>/,/<al>/,/<\/al>/,/<ar>/,/<\/ar>/]
  160.   width = Graphics.width - 2*PADX
  161.   bitmap = Bitmap.new(width,Graphics.height)
  162.   pbSetSystemFont(bitmap)
  163.   totalwidth = 0 #Anchura total de la línea actual
  164.   count = 0 #Línea actual
  165.  
  166.   #Color, bold, italic, underlined, struck, outline, thickoutline, font,
  167.   #fontsize, opacity, centered, left-centered,righ-centered
  168.   regs=["","","","","","","","","","","","",""]
  169.   ret=[""] #Array con todas las líneas del texto.
  170.  
  171.   words = value.split
  172.  
  173.   #Análisis de todas las palabras del texto.
  174.   words.each{|word|
  175.       word+= " " #Añade un espacio entre palabras.
  176.       init = "" #Expresiones de apertura Ej: <fs=X>
  177.       ending = "" #Expresiones de cierre Ej: </fs>
  178.      
  179.       #Detecta comandos especiales y los activa en regs hasta que encuentra
  180.       #el de cierre.
  181.       regex.each_index{|index|
  182.         aux = word.slice!(regex[index])
  183.         if aux != nil
  184.           if index % 2 != 0 #Comando de cierre
  185.             regs[((index+1)/2)-1]=""
  186.             ending += aux
  187.               if index == 14 #</fn>
  188.               pbSetSystemFont(bitmap)
  189.             end
  190.           else #Comando de apertura
  191.             regs[index/2] = aux
  192.             init += aux
  193.             if index == 14 #<fn=X>
  194.               bitmap.font.name = aux.gsub(/fn=/){""}
  195.             end
  196.           end
  197.         end
  198.       }
  199.      
  200.       #En word solo queda la palabra neta sin comandos especiales. Se mide
  201.       #cuanto ocupa en bitmap y se añade a totalwidth. En función de ello
  202.       #se realiza un salto de línea o no.
  203.       if word.slice!(/\\n/) != nil
  204.           count += 1
  205.           ret.push("")
  206.           regs.each{|reg|
  207.             ret[count]+=reg
  208.           }
  209.           ret[count]+= word + ending
  210.           totalwidth = 0
  211.       end
  212.      
  213.       wordwidth = bitmap.text_size(word).width
  214.       totalwidth += wordwidth
  215.       if totalwidth > width
  216.         count += 1
  217.         ret.push("")
  218.         regs.each{|reg|
  219.           ret[count]+=reg
  220.         }
  221.         ret[count]+= word + ending
  222.         totalwidth = wordwidth
  223.       else
  224.         ret[count] += (init + word + ending)
  225.       end
  226.   }
  227.   return ret
  228. end
Advertisement
Add Comment
Please, Sign In to add comment