@bts = Sprite.new(@v)
@bts.bitmap = Bitmap.new(@w - 3,@h)
@bts.bitmap.font.size = 10
@bts.bitmap.font.color = Color.new(255,255,255)
@bts.z = @v.z
# 辅助属性
@bk_count = 0
$聊天 = default_text.scan(/./)
@max_width = @w - 3
$聊天宽度 = $聊天.size
@save_pos = $聊天宽度
# 光标Caret
@v1 = Viewport.new(rect.x,@caret_y,@w + 3,@caret_h)
$caret_sp = Sprite.new(@v1)
@v1.z = @v.z + 1
@caret_bitmap = Bitmap.new(3,@caret_h)
@caret_bitmap.fill_rect(0,0,1,@caret_h,Color.new(0,0,0,180))
@caret_bitmap.fill_rect(1,0,1,@caret_h,Color.new(0,0,0))
@caret_bitmap.fill_rect(2,0,1,@caret_h,Color.new(120,120,120))
$caret_sp.bitmap = @caret_bitmap
$caret_sp.x = self.get_width($聊天[0,$聊天宽度].to_s)
$caret_sp.y = 0
$caret_sp.visible = false
@caret_flash_count = 0
# 刷新
refresh
# 设置焦点
TypeAPI.setFocus
# 开始输入
TypeAPI.startType
end
#--------------------------------------------------------------------------
# ● 初始化文字
#--------------------------------------------------------------------------
def clear(default_text = "")
$聊天 = default_text.scan(/./)
$caret_sp.x = self.get_width($聊天[0,$聊天宽度].to_s)
TypeAPI.setFocus
text = TypeAPI.getText
refresh
end
#--------------------------------------------------------------------------
# ● 设置活动标记
#--------------------------------------------------------------------------
def active=(value)
if value != true and value != false
return
end
@active = value
$caret_sp.visible = @active
@v.visible = @active
end
#--------------------------------------------------------------------------
# ● 释放
#--------------------------------------------------------------------------
def dispose
@caret_bitmap.dispose
$caret_sp.bitmap.dispose
$caret_sp.dispose
@cts.bitmap.dispose
@bts.bitmap.dispose
@bts.dispose
@cts.dispose
@v.dispose
@v1.dispose
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def refresh
@cts.bitmap.clear
@bts.bitmap.clear
if $聊天.to_s.include?("\\")
$ShowCursor.call(1)
p "非法字符"
$ShowCursor.call(0)
$Ltf.clear
$caret_sp.visible = false
$聊天宽度 = 0
end
@cts.bitmap.draw_text(0,0,@w,@h,$聊天.to_s)
@bts.bitmap.draw_text(25,0,@w,@h,"")
end
#--------------------------------------------------------------------------
# ● 获取文字
#--------------------------------------------------------------------------
def get_text
return $聊天.to_s
end
#--------------------------------------------------------------------------
# ● 取得字符宽度
#--------------------------------------------------------------------------
def get_width(str)
return @cts.bitmap.text_size(str).width
end
#--------------------------------------------------------------------------
# ● 更新
#--------------------------------------------------------------------------
def update
# 非激活状态则返回
unless @active
return
end
# 获取按键信息
key_info = TypeAPI.getKeyInfos
case key_info
when 0x09 # Tab
# 按下 Tab 键的情况自己定义怎么处理
return
when 0x0d # Enter
#$game_switches[1] = true
# 按下 Enter 键的情况自己定义怎么处理
return
when 0x1B # Esc
# 按下 Esc 键的情况自己定义怎么处理
return
end
self.update_text
self.update_lrb
self.update_back
self.update_caret
end
#--------------------------------------------------------------------------
# ● 更新文字
#--------------------------------------------------------------------------
def update_text
# 文字刷新
TypeAPI.setFocus
text = TypeAPI.getText
#禁止使用符号"\"
if text != ""
for char in text.scan(/./)
if self.get_width($聊天.to_s + char) <= @max_width
$聊天[$聊天宽度,0] = char
$聊天宽度 += 1
else
break
end
end
refresh
end
end
#--------------------------------------------------------------------------
# ● 更新左右按键
#--------------------------------------------------------------------------
def update_lrb
if RInput.trigger?(RInput::LEFT) and $聊天宽度 > 0
$聊天宽度 -= 1
return
end
if RInput.trigger?(RInput::RIGHT) and $聊天宽度 < $聊天.size
$聊天宽度 += 1
return
end
end
#--------------------------------------------------------------------------
# ● 更新退格
#--------------------------------------------------------------------------
def update_back
# 前退格处理
@bk_count += TypeAPI.ifBack
if @bk_count > 0
@bk_count -= 1
if $聊天宽度 > 0
$聊天.delete_at($聊天宽度 - 1);$聊天宽度 -= 1;refresh
end
end
end
#--------------------------------------------------------------------------
# ● 更新光标闪烁
#--------------------------------------------------------------------------
def update_caret
# 闪烁计时
@caret_flash_count += 1
if @caret_flash_count == 30
$caret_sp.visible = !$caret_sp.visible
@caret_flash_count = 0
end
# Caret位置刷新
if @save_pos != $聊天宽度
@save_pos = $聊天宽度
$caret_sp.x = self.get_width($聊天[0,@save_pos].to_s)
end
end