Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # -- Script : DSI-TextByPicture
- # -- Author : dsiver144
- # -- Last Updated : 12/22/2018
- #==============================================================================
- # Script Calls:
- # set_picture_font(pic_id, font, size) : Use this before picture_text script call.
- # ex: set_picture_font(5, "Arial", 24)
- # picture_text(text, pic_id, origin, x, y, zoom_x, zoom_y, opacity, blend_type)
- # Param note:
- # - text : text to display to screen. (You can only use \c \v \n \i \p \g)
- # - pic_id : Picture Number
- # - origin: 0 (top_left) or 1 (center) (optional)
- # - x, y : is text coordinate. (optional)
- # - zoom_x, zoom_y: both of them are 100 by default (optional)
- # - opacity : 255 by default (optional)
- # - blend_type: 0 (Normal), 1 (Add), 2 (Sub) (optional)
- # ex: picture_text("Hello \\i[5], my name is \\c[5]dsiver144\\c[0]!", 5, 0, 50, 60)
- #==============================================================================
- class Game_Picture
- attr_accessor :text
- attr_accessor :font_name
- attr_accessor :font_size
- #--------------------------------------------------------------------------
- # * Erase Picture
- #--------------------------------------------------------------------------
- alias_method(:dsi_game_picture_text_erase, :erase)
- def erase
- dsi_game_picture_text_erase
- @text = nil
- @font_name = nil
- @font_size = nil
- end
- #--------------------------------------------------------------------------
- # * Calculate Bitmap Size
- #--------------------------------------------------------------------------
- def calc_bitmap_size(str)
- temp_bitmap = Bitmap.new(1,1)
- temp_bitmap.font.name = @font_name if @font_name
- temp_bitmap.font.size = @font_size if @font_size
- # Process string replace
- str = str.gsub(/\\c\[\d+\]/i, "")
- icon_size = str.scan(/\\i\[\d+\]/i).size * 24
- str = str.gsub(/\\i\[\d+\]/i, "")
- # Calculate text size
- size = temp_bitmap.text_size(str + " ")
- temp_bitmap.dispose
- return [size.width + icon_size + 2, size.height]
- end
- #--------------------------------------------------------------------------
- # * Bitmap Size
- #--------------------------------------------------------------------------
- def bitmap_size
- return calc_bitmap_size(@text)
- end
- #--------------------------------------------------------------------------
- # * Set Font
- #--------------------------------------------------------------------------
- def set_font(name, size)
- @font_name = name
- @font_size = size
- end
- #--------------------------------------------------------------------------
- # * Draw Text
- #--------------------------------------------------------------------------
- def draw_text(text, origin, x, y, zoom_x, zoom_y, opacity, blend_type)
- @text = text
- show("", origin, x, y, zoom_x, zoom_y, opacity, blend_type)
- end
- end
- class Sprite_Picture < Sprite
- #--------------------------------------------------------------------------
- # * Update Transfer Origin Bitmap
- #--------------------------------------------------------------------------
- def update_bitmap
- if @picture.name.empty?
- if @picture.text
- if !@drew_text
- self.bitmap = PictureBitmap.new(*@picture.bitmap_size)
- if @picture.font_name
- self.bitmap.font.name = @picture.font_name
- self.bitmap.font.size = @picture.font_size
- end
- self.bitmap.draw_text_ex(0, 0, @picture.text)
- @drew_text = true
- end
- else
- self.bitmap = nil
- @drew_text = false
- end
- else
- @drew_text = false
- self.bitmap = Cache.picture(@picture.name)
- end
- end
- end
- class Game_Interpreter
- def set_picture_font(picture_id, font_name, font_size)
- screen.pictures[picture_id].set_font(font_name, font_size)
- end
- def picture_text(text, picture_id, origin = 0, x = 0, y = 0, zoom_x = 100, zoom_y = 100, opacity = 255, blend_type = 0)
- screen.pictures[picture_id].draw_text(text, origin, x, y, zoom_x, zoom_y, opacity, blend_type)
- end
- end
- class PictureBitmap < Bitmap
- #--------------------------------------------------------------------------
- # * Draw Text with Control Characters
- #--------------------------------------------------------------------------
- def draw_text_ex(x, y, text)
- #reset_font_settings
- text = convert_escape_characters(text)
- pos = {:x => x, :y => y, :new_x => x, :height => self.font.size}
- process_character(text.slice!(0, 1), text, pos) until text.empty?
- end
- #--------------------------------------------------------------------------
- # * Convert Escape Characters
- #--------------------------------------------------------------------------
- def convert_escape_characters(text)
- result = text.to_s.clone
- result.gsub!(/\\/) { "\e" }
- result.gsub!(/\e\e/) { "\\" }
- result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
- result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
- result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
- result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
- result.gsub!(/\eG/i) { Vocab::currency_unit }
- result
- end
- #--------------------------------------------------------------------------
- # * Character Processing
- #--------------------------------------------------------------------------
- def process_character(c, text, pos)
- case c
- when "\e" # Control character
- process_escape_character(obtain_escape_code(text), text, pos)
- else # Normal character
- process_normal_character(c, pos)
- end
- end
- #--------------------------------------------------------------------------
- # * Normal Character Processing
- #--------------------------------------------------------------------------
- def process_normal_character(c, pos)
- text_width = text_size(c).width
- draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
- pos[:x] += text_width
- end
- #--------------------------------------------------------------------------
- # * Destructively Get Control Code
- #--------------------------------------------------------------------------
- def obtain_escape_code(text)
- text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
- end
- #--------------------------------------------------------------------------
- # * Destructively Get Control Code Argument
- #--------------------------------------------------------------------------
- def obtain_escape_param(text)
- text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
- end
- #--------------------------------------------------------------------------
- # * Control Character Processing
- # code : the core of the control character
- # e.g. "C" in the case of the control character \C[1].
- #--------------------------------------------------------------------------
- def process_escape_character(code, text, pos)
- case code.upcase
- when 'C'
- change_color(text_color(obtain_escape_param(text)))
- when 'I'
- process_draw_icon(obtain_escape_param(text), pos)
- end
- end
- #--------------------------------------------------------------------------
- # * Text Color
- #--------------------------------------------------------------------------
- def text_color(n)
- Cache.system("Window").get_pixel(64 + (n % 8) * 8, 96 + (n / 8) * 8)
- end
- #--------------------------------------------------------------------------
- # * Change Text Drawing Color
- # enabled : Enabled flag. When false, draw semi-transparently.
- #--------------------------------------------------------------------------
- def change_color(color)
- self.font.color.set(color)
- end
- #--------------------------------------------------------------------------
- # * Icon Drawing Process by Control Characters
- #--------------------------------------------------------------------------
- def process_draw_icon(icon_index, pos)
- draw_icon(icon_index, pos[:x], pos[:y])
- pos[:x] += 24
- end
- #--------------------------------------------------------------------------
- # * Draw Icon
- # enabled : Enabled flag. When false, draw semi-transparently.
- #--------------------------------------------------------------------------
- def draw_icon(icon_index, x, y, enabled = true)
- bitmap = Cache.system("Iconset")
- rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
- self.blt(x, y, bitmap, rect, 255)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement