Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Draw Text Fix Method
#1
Draw Text Fix Method
by SephirothSpawn
Mar 1 2006

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given.


Introduction
Fixes how text is draw in the Font class.

It will take the paramters you send to the draw_text method and check the text. It will shrink the font until the text fits in both the width, and height of your parameters.

Features
  • Automatic Height Adjustment
  • Autmatic Widht Adjustment
  • Turns all the text arguments into strings, so you can pass number without .to_s
  • Can turn on Bold or Italic if resizing occurs
  • Returns font to normal state after resizing occurs
Instructions
Just Copy and Paste the script above Main

The Script
[codebox]
Code:
#==============================================================================
# Draw Text Fix
#==============================================================================
# SephirothSpawn
# Version 1
# 03.01.06
#==============================================================================

#------------------------------------------------------------------------------
# * SDK Log Script
#------------------------------------------------------------------------------
SDK.log("Draw Text Fix", "SephirothSpawn", 1, "03.01.06")

#------------------------------------------------------------------------------
# * Begin SDK Enable Test
#------------------------------------------------------------------------------
if SDK.state('Draw Text Fix') == true

#==============================================================================
# ** Font
#==============================================================================
class Font
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :turn_bold, :turn_italic
  #--------------------------------------------------------------------------
  # * Alias Initialize
  #--------------------------------------------------------------------------
  if @draw_text_fix.nil?
    alias draw_text_fix_init initialize
    @draw_text_fix = true
  end
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    # Orginal Initialize Method
    draw_text_fix_init
    # Turns Off Font Fix Options
    @turn_bold, @turn_italic = false, false
  end
end

#==============================================================================
# ** Bitmap
#==============================================================================
class Bitmap
  #--------------------------------------------------------------------------
  # * Alias Draw Text
  #--------------------------------------------------------------------------
  if @draw_text_fix.nil?
    alias draw_text_fix draw_text
    @draw_text_fix = true
  end
  #--------------------------------------------------------------------------
  # * Draw Text
  #--------------------------------------------------------------------------
  def draw_text(*args)
    # Stores Font Properties
    temp_font = self.font.dup
    #-------------------------------
    # ~ Rect & Text
    #-------------------------------
    if args.size == 2
      # Stores Parameters
      r, t = args[0], args[1].to_s
      # Adjust Height
      if r.height < self.font.size
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Adjust Size
        self.font.size = r.height
      end
      # Adjust Width
      if self.text_size(t).width > r.width
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Loops Until Font Fits
        until self.text_size(t).width < r.width
          self.font.size -= 1
        end
      end
      # Draws Text
      self.draw_text_fix(r, t)
    #-------------------------------
    # ~ Rect, Text & Align
    #-------------------------------
    elsif args.size == 3
      # Stores Parameters
      r, t, a = args[0], args[1].to_s, args[2]
      # Adjust Height
      if r.height < self.font.size
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Adjust Size
        self.font.size = r.height
      end
      # Adjust Width
      if self.text_size(t).width > r.width
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Loops Until Font Fits
        until self.text_size(t).width < r.width
          self.font.size -= 1
        end
      end
      # Draws Text
      self.draw_text_fix(r, t, a)
    #-------------------------------
    # ~ X, Y, Width, Height & Text
    #-------------------------------
    elsif args.size == 5
      # Stores Parameters
      x, y, w, h, t =
        args[0], args[1], args[2], args[3], args[4].to_s
      # Adjust Height
      if h < self.font.size
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Adjust Size
        self.font.size = h
      end
      # Adjust Width
      if self.text_size(t).width > w
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Loops Until Font Fits
        until self.text_size(t).width < w
          self.font.size -= 1
        end
      end
      # Draws Text
      self.draw_text_fix(x, y, w, h, t)
    #-------------------------------
    # ~ X, Y, Width, Height, Text & Align
    #-------------------------------
    elsif args.size == 6
      # Stores Parameters
      x, y, w, h, t, a =
        args[0], args[1], args[2], args[3], args[4].to_s, args[5]
      # Adjust Height
      if h < self.font.size
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Adjust Size
        self.font.size = h
      end
      # Adjust Width
      if self.text_size(t).width > w
        # Turns On Font Options
        self.font.bold = true if self.font.turn_bold
        self.font.italic = true if self.font.turn_italic
        # Loops Until Font Fits
        until self.text_size(t).width < w
          self.font.size -= 1
        end
      end
      # Draws Text
      self.draw_text_fix(x, y, w, h, t, a)
    end
    # Restores Previous Font
    self.font = temp_font
  end
end

#--------------------------------------------------------------------------
# * End SDK Enable Test
#--------------------------------------------------------------------------
end
[/codebox]

Demo Window
Copy and Paste this Above main. Then, make an event with a call script.
[codebox]
Code:
#==============================================================================
# ** Sample Window
#==============================================================================
class Window_DrawTextFixSample < Window_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0, 320, 240)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.draw_text(0, 0, contents.width, 24, 'A sample line. Woot!', 1)
    self.contents.draw_text(0, 24, contents.width, 24, 46, 1)
    self.contents.draw_text(0, 48, 200, 24, 'long long long line of text that wont fit')
    self.contents.font.turn_bold = true
    self.contents.draw_text(0, 72, 200, 24, 'long long long line of text that wont fit')
    self.contents.font.turn_bold = false
    self.contents.draw_text(Rect.new(0, 96, 200, 10), 'long long long line of text that wont fit')
    self.contents.draw_text(0, 120, contents.width, 14, 'height adjustment')
    self.contents.draw_text(0, 134, contents.width, 24, 'height adjustment')
  end
end
[/codebox]

Call Script : @window = Window_DrawTextFixSample.new

Compatibility

Is SDK Compatable & Compiant. Works with and without SDK.

Author's Notes
Enjoy!
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text over allies with AMS (SBABS v3 required) shado 0 2,257 08-23-2006, 01:00 PM
Last Post: shado
  Gradient method for bars Tsunokiette 0 1,987 04-05-2006, 01:00 PM
Last Post: Tsunokiette
  Metroid Text Sound Script Constance 0 2,150 12-21-2005, 01:00 PM
Last Post: Constance
  Text Variations v.2 Tsunokiette 0 2,175 11-23-2005, 01:00 PM
Last Post: Tsunokiette
  Different way to draw text Sheol 0 2,204 11-14-2005, 01:00 PM
Last Post: Sheol



Users browsing this thread: