#encoding=utf-8
#
# YAWMP+ - NameWindow by Calendar99
# Version: 1.0.0
# Time: 2019.9.10
#
# 依存关系:
# 需要插入YAWMP(Yet Another Window Message with Portrait)之下、main之上
#
# 使用方法:
# 事件中显示文字时,写<说话人的名字/>
# 则显示时会出现"说话人的名字"的姓名窗口
# 另外,若该姓名是作为单独一行存在的,文字会自动删除空行
#
# 问题报告:
# 未支持文字的缩放带来的窗口大小与绘制位置变化
#
module CLD99
module YAWMP_PATCH
# 直接禁用名字识别功能(推荐禁用)
# 禁用 = true 启用 = false
#
FORBID_NAME_SYSTEM = true
# 姓名框 X 坐标微调
# 正整数 = 往右 负整数 = 往左
X_OFFSET = 0
# 姓名框 Y 坐标微调
# 正整数 = 往下 负整数 = 往上
Y_OFFSET = 4
end
end
class Game_Message
attr_accessor :talker # 用于姓名框显示
attr_accessor :name_window_pos # 用于姓名框位置
# [别名修改]清除
#
alias clear_for_yawmp_name_window clear
def clear
clear_for_yawmp_name_window # 原方法
@talker = ''
@name_window_pos = {}
end
# [别名修改]添加内容
#
alias add_for_yawmp_name add
def add(text)
text = text.sub(/<(.*)\/>\n?/, '')
@talker = $1 if $1
@texts.push(text) unless text.empty?
end
end
class Window_Message
# [别名修改]生成所有窗口
#
alias create_all_windows_for_yawmp_name_window create_all_windows
def create_all_windows
create_all_windows_for_yawmp_name_window # 原方法
@name_window = CLD99::Window_Name.new(self) # 生成姓名窗口
end
# [别名修改]更新所有窗口
#
alias update_all_windows_for_yawmp_name_window update_all_windows
def update_all_windows
update_all_windows_for_yawmp_name_window # 原方法
@name_window.update # 更新姓名窗口
end
# [别名修改]翻页处理
#
alias new_page_for_yawmp_name_window new_page
def new_page(text, pos)
new_page_for_yawmp_name_window(text, pos) # 原方法
$game_message.name_window_pos = pos.dup # 传递位置
end
# [别名修改]打开窗口
#
alias open_for_yawmp_name_window open
def open
open_for_yawmp_name_window # 原方法
name = $game_message.talker
if name && !name.empty? # 需要姓名窗口
@name_window.refresh
@name_window.open.activate
end
end
# [别名修改]关闭窗口
#
alias close_for_yawmp_name_window close
def close
close_for_yawmp_name_window # 原方法
@name_window.close
end
# [覆盖]处理所有内容
#
def process_all_text
text = convert_escape_characters $game_message.all_text
skin_id = YAWMP.get_variable YAWMP::Dialogbox[:var_skin]
if @background.zero? # normal background
#unless YAWMP.get_switch YAWMP::Name[:swi_disable] # 原来的条件
if !YAWMP.get_switch(YAWMP::Name[:swi_disable]) && # 此处修改了条件
!CLD99::YAWMP_PATCH::FORBID_NAME_SYSTEM #
ls = text.split "\n"
if ls[0] && !ls[0].empty?
ldeco = YAWMP::Name[:deco][0]
rdeco = YAWMP::Name[:deco][1]
color = YAWMP::Name[:color][:default]
YAWMP::Name[:color][:name].each do |name, ncolor|
if name.is_a? String
if ls[0].start_with? name
color = ncolor
break
end
elsif name.is_a? Regexp
if ls[0].match name
color = ncolor
break
end
end
end
YAWMP::Name[:color][:face].each do |face, fcolor|
if face.is_a? String
if $game_message.face_name.downcase.start_with? face.downcase
color = fcolor
break
end
elsif face.is_a? Regexp
if $game_message.face_name.match face
color = fcolor
break
end
end
end
YAWMP::Name[:skin][:name].each do |name, nskin|
if name.is_a? String
if ls[0].start_with? name
skin_id = nskin
break
end
elsif name.is_a? Regexp
if ls[0].match name
skin_id = nskin
break
end
end
end
YAWMP::Name[:skin][:face].each do |face, fskin|
if face.is_a? String
if $game_message.face_name.downcase.start_with? face.downcase
skin_id = fskin
break
end
elsif face.is_a? Regexp
if $game_message.face_name.match face
skin_id = fskin
break
end
end
end
ls[0] = "\e>\ec[#{color}]#{ldeco}#{ls[0]}#{rdeco}\ec[0]"
text = ls.join "\n" + "\s" * YAWMP::Name[:indent]
end
end
end
load_dialogbox skin_id
#open_and_wait # 将open_and_wait运行的位置由此处往后推迟了两行
pos = {}
new_page text, pos
open_and_wait
process_character(text.slice!(0, 1), text, pos) until text.empty?
end
end
class CLD99::Window_Name
# 方法代理
#
[:update, :convert_escape_characters, :text_size,
:open, :close].each do |symbol|
define_method symbol do |*args|
@window.send(symbol, *args)
end
end
# 初始化
#
def initialize(message_window)
@message_window = message_window
@window_bag = {} # 容纳窗口的哈希
# 自身并不是窗口,在内部包含窗口
init_window(window_width, window_height)
end
# 生成内部窗口
#
def init_window(width, height)
if @window # 需要处理先前窗口
openness = @window.openness
@window.visible = false # 隐藏窗口
@window.contents.clear # 清除文本
else
openness = 0
end
if @window_bag[[width, height]] # 已有同属性窗口
@window = @window_bag[[width, height]] # 取出窗口
@window.visible = true # 显示窗口
else
@window = Window_Base.new(0, 0, width, height)
@window_bag[[width, height]] = @window # 将此窗口按属性储存
end
@window.openness = openness
@window.deactivate
end
# 边距尺寸
#
def padding
return 12
end
# 获取窗口的宽度
#
def window_width(text_width = 136)
return text_width + padding * 2
end
# 获取窗口的高度
#
def window_height(line_height = 24)
visible_line_number = 1
return line_height * visible_line_number + padding * 2
end
# 刷新
#
def refresh
return raise "@window illegal" if !@window || !@window.is_a?(Window)
return if (name = $game_message.talker).empty?
return if (pos = $game_message.name_window_pos).empty?
line_height = 24
name = convert_escape_characters(name) # 处理控制文字
text_width = text_width(name)
if pos[:height] != line_height || @window.width != text_width
# 大小不匹配 重新创建窗口
init_window(window_width(text_width), window_height(pos[:height]))
end
@window.x = pos[:x] + padding + CLD99::YAWMP_PATCH::X_OFFSET # 决定窗口X坐标
@window.y = @message_window.y - @window.height + CLD99::YAWMP_PATCH::Y_OFFSET
# 绘制名字
process_all_text(pos, name)
end
# 文字宽度
#
def text_width(original_text)
text = original_text.dup
icon_number = 0
text.gsub!(/\e/, "\\") # 恢复出转义符
text.gsub!(/\\I\[\d+\]/i){ icon_number += 1; ''} # 消除图标并统计数量
text.gsub!(/\\(?:[\{\}\$\.\|!<>\^\\]|[A-Z]+)(?:\[\d+\])?/i, '') # 清除控制符,便于准确测量长度
@window.text_size(text).width + icon_number * 24
end
# 绘制名字(总体)
#
def process_all_text(pos, text)
pos[:x] = 0 # 文字绘制前x坐标清零
@window.contents.clear
@window.reset_font_settings
process_character(text.slice!(0, 1), text, pos) until text.empty?
end
# 绘制名字(单个)
#
def process_character(c, text, pos)
case c
when "\r", "\n", "\f" # 无事发生
else
@window.process_character(c, text, pos)
end
end
end