Advertisement
DarkSoul144

DSI-TextByPicture

Dec 20th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. #==============================================================================
  2. # -- Script : DSI-TextByPicture
  3. # -- Author : dsiver144
  4. # -- Last Updated : 12/22/2018
  5. #==============================================================================
  6. # Script Calls:
  7. # set_picture_font(pic_id, font, size) : Use this before picture_text script call.
  8. # ex: set_picture_font(5, "Arial", 24)
  9. # picture_text(text, pic_id, origin, x, y, zoom_x, zoom_y, opacity, blend_type)
  10. # Param note:
  11. # - text : text to display to screen. (You can only use \c \v \n \i \p \g)
  12. # - pic_id : Picture Number
  13. # - origin: 0 (top_left) or 1 (center) (optional)
  14. # - x, y : is text coordinate. (optional)
  15. # - zoom_x, zoom_y: both of them are 100 by default (optional)
  16. # - opacity : 255 by default (optional)
  17. # - blend_type: 0 (Normal), 1 (Add), 2 (Sub) (optional)
  18. # ex: picture_text("Hello \\i[5], my name is \\c[5]dsiver144\\c[0]!", 5, 0, 50, 60)
  19. #==============================================================================
  20. class Game_Picture
  21. attr_accessor :text
  22. attr_accessor :font_name
  23. attr_accessor :font_size
  24. #--------------------------------------------------------------------------
  25. # * Erase Picture
  26. #--------------------------------------------------------------------------
  27. alias_method(:dsi_game_picture_text_erase, :erase)
  28. def erase
  29. dsi_game_picture_text_erase
  30. @text = nil
  31. @font_name = nil
  32. @font_size = nil
  33. end
  34. #--------------------------------------------------------------------------
  35. # * Calculate Bitmap Size
  36. #--------------------------------------------------------------------------
  37. def calc_bitmap_size(str)
  38. temp_bitmap = Bitmap.new(1,1)
  39. temp_bitmap.font.name = @font_name if @font_name
  40. temp_bitmap.font.size = @font_size if @font_size
  41. # Process string replace
  42. str = str.gsub(/\\c\[\d+\]/i, "")
  43. icon_size = str.scan(/\\i\[\d+\]/i).size * 24
  44. str = str.gsub(/\\i\[\d+\]/i, "")
  45. # Calculate text size
  46. size = temp_bitmap.text_size(str + " ")
  47. temp_bitmap.dispose
  48. return [size.width + icon_size + 2, size.height]
  49. end
  50. #--------------------------------------------------------------------------
  51. # * Bitmap Size
  52. #--------------------------------------------------------------------------
  53. def bitmap_size
  54. return calc_bitmap_size(@text)
  55. end
  56. #--------------------------------------------------------------------------
  57. # * Set Font
  58. #--------------------------------------------------------------------------
  59. def set_font(name, size)
  60. @font_name = name
  61. @font_size = size
  62. end
  63. #--------------------------------------------------------------------------
  64. # * Draw Text
  65. #--------------------------------------------------------------------------
  66. def draw_text(text, origin, x, y, zoom_x, zoom_y, opacity, blend_type)
  67. @text = text
  68. show("", origin, x, y, zoom_x, zoom_y, opacity, blend_type)
  69. end
  70.  
  71. end
  72.  
  73. class Sprite_Picture < Sprite
  74. #--------------------------------------------------------------------------
  75. # * Update Transfer Origin Bitmap
  76. #--------------------------------------------------------------------------
  77. def update_bitmap
  78. if @picture.name.empty?
  79. if @picture.text
  80. if !@drew_text
  81. self.bitmap = PictureBitmap.new(*@picture.bitmap_size)
  82. if @picture.font_name
  83. self.bitmap.font.name = @picture.font_name
  84. self.bitmap.font.size = @picture.font_size
  85. end
  86. self.bitmap.draw_text_ex(0, 0, @picture.text)
  87. @drew_text = true
  88. end
  89. else
  90. self.bitmap = nil
  91. @drew_text = false
  92. end
  93. else
  94. @drew_text = false
  95. self.bitmap = Cache.picture(@picture.name)
  96. end
  97. end
  98. end
  99.  
  100. class Game_Interpreter
  101.  
  102. def set_picture_font(picture_id, font_name, font_size)
  103. screen.pictures[picture_id].set_font(font_name, font_size)
  104. end
  105.  
  106. def picture_text(text, picture_id, origin = 0, x = 0, y = 0, zoom_x = 100, zoom_y = 100, opacity = 255, blend_type = 0)
  107. screen.pictures[picture_id].draw_text(text, origin, x, y, zoom_x, zoom_y, opacity, blend_type)
  108. end
  109.  
  110. end
  111.  
  112. class PictureBitmap < Bitmap
  113. #--------------------------------------------------------------------------
  114. # * Draw Text with Control Characters
  115. #--------------------------------------------------------------------------
  116. def draw_text_ex(x, y, text)
  117. #reset_font_settings
  118. text = convert_escape_characters(text)
  119. pos = {:x => x, :y => y, :new_x => x, :height => self.font.size}
  120. process_character(text.slice!(0, 1), text, pos) until text.empty?
  121. end
  122. #--------------------------------------------------------------------------
  123. # * Convert Escape Characters
  124. #--------------------------------------------------------------------------
  125. def convert_escape_characters(text)
  126. result = text.to_s.clone
  127. result.gsub!(/\\/) { "\e" }
  128. result.gsub!(/\e\e/) { "\\" }
  129. result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  130. result.gsub!(/\eV\[(\d+)\]/i) { $game_variables[$1.to_i] }
  131. result.gsub!(/\eN\[(\d+)\]/i) { actor_name($1.to_i) }
  132. result.gsub!(/\eP\[(\d+)\]/i) { party_member_name($1.to_i) }
  133. result.gsub!(/\eG/i) { Vocab::currency_unit }
  134. result
  135. end
  136. #--------------------------------------------------------------------------
  137. # * Character Processing
  138. #--------------------------------------------------------------------------
  139. def process_character(c, text, pos)
  140. case c
  141. when "\e" # Control character
  142. process_escape_character(obtain_escape_code(text), text, pos)
  143. else # Normal character
  144. process_normal_character(c, pos)
  145. end
  146. end
  147. #--------------------------------------------------------------------------
  148. # * Normal Character Processing
  149. #--------------------------------------------------------------------------
  150. def process_normal_character(c, pos)
  151. text_width = text_size(c).width
  152. draw_text(pos[:x], pos[:y], text_width * 2, pos[:height], c)
  153. pos[:x] += text_width
  154. end
  155. #--------------------------------------------------------------------------
  156. # * Destructively Get Control Code
  157. #--------------------------------------------------------------------------
  158. def obtain_escape_code(text)
  159. text.slice!(/^[\$\.\|\^!><\{\}\\]|^[A-Z]+/i)
  160. end
  161. #--------------------------------------------------------------------------
  162. # * Destructively Get Control Code Argument
  163. #--------------------------------------------------------------------------
  164. def obtain_escape_param(text)
  165. text.slice!(/^\[\d+\]/)[/\d+/].to_i rescue 0
  166. end
  167. #--------------------------------------------------------------------------
  168. # * Control Character Processing
  169. # code : the core of the control character
  170. # e.g. "C" in the case of the control character \C[1].
  171. #--------------------------------------------------------------------------
  172. def process_escape_character(code, text, pos)
  173. case code.upcase
  174. when 'C'
  175. change_color(text_color(obtain_escape_param(text)))
  176. when 'I'
  177. process_draw_icon(obtain_escape_param(text), pos)
  178. end
  179. end
  180. #--------------------------------------------------------------------------
  181. # * Text Color
  182. #--------------------------------------------------------------------------
  183. def text_color(n)
  184. Cache.system("Window").get_pixel(64 + (n % 8) * 8, 96 + (n / 8) * 8)
  185. end
  186. #--------------------------------------------------------------------------
  187. # * Change Text Drawing Color
  188. # enabled : Enabled flag. When false, draw semi-transparently.
  189. #--------------------------------------------------------------------------
  190. def change_color(color)
  191. self.font.color.set(color)
  192. end
  193. #--------------------------------------------------------------------------
  194. # * Icon Drawing Process by Control Characters
  195. #--------------------------------------------------------------------------
  196. def process_draw_icon(icon_index, pos)
  197. draw_icon(icon_index, pos[:x], pos[:y])
  198. pos[:x] += 24
  199. end
  200. #--------------------------------------------------------------------------
  201. # * Draw Icon
  202. # enabled : Enabled flag. When false, draw semi-transparently.
  203. #--------------------------------------------------------------------------
  204. def draw_icon(icon_index, x, y, enabled = true)
  205. bitmap = Cache.system("Iconset")
  206. rect = Rect.new(icon_index % 16 * 24, icon_index / 16 * 24, 24, 24)
  207. self.blt(x, y, bitmap, rect, 255)
  208. end
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement