Project1
标题:
强化数值输入的处理(用数字键输入)
[打印本页]
作者:
某死灵法师
时间:
2014-7-28 14:29
标题:
强化数值输入的处理(用数字键输入)
本帖最后由 某死灵法师 于 2014-7-28 14:37 编辑
有没有发现RMXP自带的数值输入处理好坑!!
上下左右操作什么的好麻烦!!
于是这个强化版数值输入处理脚本诞生了……
使用方法:替换原来的Window_InputNumber
功能:数字键直接输入(大小键盘均支持),backspace消掉上一格,delete消掉当前格,原本的上下左右依然保留
注:需要全键盘脚本
#==============================================================================
# ■ Window_InputNumber
#------------------------------------------------------------------------------
# 信息窗口内部使用、输入数值的窗口强化版 by司令死灵法师
#==============================================================================
class Window_InputNumber < Window_Base
#--------------------------------------------------------------------------
# ● 初始化对像
# digits_max : 位数
#--------------------------------------------------------------------------
def initialize(digits_max)
@digits_max = digits_max
@number = 0
# 从数字的幅度计算(假定与 0~9 等幅)光标的幅度
dummy_bitmap = Bitmap.new(32, 32)
@cursor_width = dummy_bitmap.text_size("0").width + 8
dummy_bitmap.dispose
super(0, 0, @cursor_width * @digits_max + 32, 64)
self.contents = Bitmap.new(width - 32, height - 32)
self.z += 9999
self.opacity = 0
@index = 0
refresh
update_cursor_rect
end
#--------------------------------------------------------------------------
# ● 取得数值
#--------------------------------------------------------------------------
def number
return @number
end
#--------------------------------------------------------------------------
# ● 设置数值
# number : 新的数值
#--------------------------------------------------------------------------
def number=(number)
@number = [[number, 0].max, 10 ** @digits_max - 1].min
refresh
end
#--------------------------------------------------------------------------
# ● 更新光标矩形
#--------------------------------------------------------------------------
def update_cursor_rect
self.cursor_rect.set(@index * @cursor_width, 0, @cursor_width, 32)
end
#--------------------------------------------------------------------------
# ● 刷新画面
#--------------------------------------------------------------------------
def update
super
#按下小键盘数字键
0x60.upto(0x69) do |key|
if Kboard.repeat?(key)
$game_system.se_play($data_system.cursor_se)
add_number_in(key - 0x60)
#光标右移一格
if @digits_max >= 2
@index = (@index + 1) % @digits_max
end
refresh
end
end
#按下大键盘数字键
0x30.upto(0x39) do |key|
if Kboard.repeat?(key)
$game_system.se_play($data_system.cursor_se)
add_number_in(key - 0x30)
#光标右移一格
if @digits_max >= 2
@index = (@index + 1) % @digits_max
end
refresh
end
end
#按下backspace,消除上一格
if Kboard.repeat?($R_Key_BACK)
$game_system.se_play($data_system.cancel_se)
#光标左移一格
if @digits_max >= 2
@index = (@index + @digits_max - 1) % @digits_max
end
add_number_in(0)
refresh
end
#按下delete,消除当前格
if Kboard.repeat?($R_Key_DELETE)
$game_system.se_play($data_system.cancel_se)
add_number_in(0)
refresh
end
# 方向键上
if Kboard.repeat?($R_Key_UP)
$game_system.se_play($data_system.cursor_se)
# 取得现在位置的数字位数
place = 10 ** (@digits_max - 1 - @index)
n = @number / place % 10
@number -= n * place
n = (n + 1) % 10
# 再次设置现在位的数字
@number += n * place
refresh
end
# 方向键下
if Kboard.repeat?($R_Key_DOWN)
$game_system.se_play($data_system.cursor_se)
# 取得现在位置的数字位数
place = 10 ** (@digits_max - 1 - @index)
n = @number / place % 10
@number -= n * place
n = (n - 1) % 10
# 再次设置现在位的数字
@number += n * place
refresh
end
# 方向键左
if Kboard.repeat?($R_Key_LEFT)
if @digits_max >= 2
$game_system.se_play($data_system.cursor_se)
@index = (@index + @digits_max - 1) % @digits_max
end
end
# 方向键右
if Kboard.repeat?($R_Key_RIGHT)
if @digits_max >= 2
$game_system.se_play($data_system.cursor_se)
@index = (@index + 1) % @digits_max
end
end
update_cursor_rect
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def refresh
self.contents.clear
self.contents.font.color = normal_color
s = sprintf("%0*d", @digits_max, @number)
for i in 0...@digits_max
self.contents.draw_text(i * @cursor_width + 4, 0, 32, 32, s[i,1])
end
end
#--------------------------------------------------------------------------
# ● 处理当前位置的数值输入
#--------------------------------------------------------------------------
def add_number_in(in_number)
place = 10 ** (@digits_max - 1 - @index)
n = @number / place % 10
@number -= n * place
n = in_number
@number += n * place
end
end
#==============================================================================
# 本脚本来自www.66RPG.com,使用和转载请保留此信息
#==============================================================================
复制代码
再附上全键盘脚本:
#==============================================================================
# 本脚本来自www.66RPG.com,使用和转载请保留此信息
#==============================================================================
#
# 全键盘
# ★、Kboard.press?($R_Key_CTLR) 返回是否按下这个键的判断。
# 比如条件分歧:Kboard.keyboard($R_Key_H)
# 则当按下键盘H键的时候条件分歧成立
#
# ★、Kboard.repeat?(键位) == 1 当连续按住某键返回真,否则为false
# 比如条件分歧:Kboard.keyb($R_Key_U) == 1
# 则当持续按下键盘U的时候条件分歧成立
#
# ★、Kboard.trigger?(键位,1) 似乎可以做开关用。按下一次变为true,再按变false
# 修改:夏娜
#
#============================================================================
module Kboard
#==========================================================================
# 以下是全键盘按键列表
#--------------------------------------------------------------------------
$Rmouse_BUTTON_L = 0x01 # left mouse button
$Rmouse_BUTTON_R = 0x02 # right mouse button
$Rmouse_BUTTON_M = 0x04 # middle mouse button
$Rmouse_BUTTON_4 = 0x05 # 4th mouse button
$Rmouse_BUTTON_5 = 0x06 # 5th mouse button
#--------------------------------------------------------------------------
$R_Key_BACK = 0x08 # BACKSPACE key
$R_Key_TAB = 0x09 # TAB key
$R_Key_RETURN = 0x0D # ENTER key
$R_Key_ENTER = 0x0D # ENTER key
$R_Key_SHIFT = 0x10 # SHIFT key
$R_Key_CTLR = 0x11 # CTLR key
$R_Key_CTRL = 0x11 # CTLR key
$R_Key_ALT = 0x12 # ALT key
$R_Key_PAUSE = 0x13 # PAUSE key
$R_Key_CAPITAL = 0x14 # CAPS LOCK key
$R_Key_ESCAPE = 0x1B # ESC key
$R_Key_SPACE = 0x20 # SPACEBAR
$R_Key_PRIOR = 0x21 # PAGE UP key
$R_Key_NEXT = 0x22 # PAGE DOWN key
$R_Key_END = 0x23 # END key
$R_Key_HOME = 0x24 # HOME key
$R_Key_LEFT = 0x25 # LEFT ARROW key
$R_Key_UP = 0x26 # UP ARROW key
$R_Key_RIGHT = 0x27 # RIGHT ARROW key
$R_Key_DOWN = 0x28 # DOWN ARROW key
$R_Key_SELECT = 0x29 # SELECT key
$R_Key_PRINT = 0x2A # PRINT key
$R_Key_SNAPSHOT = 0x2C # PRINT SCREEN key
$R_Key_INSERT = 0x2D # INS key
$R_Key_DELETE = 0x2E # DEL key
#--------------------------------------------------------------------------
$R_Key_0 = 0x30 # 0 key
$R_Key_1 = 0x31 # 1 key
$R_Key_2 = 0x32 # 2 key
$R_Key_3 = 0x33 # 3 key
$R_Key_4 = 0x34 # 4 key
$R_Key_5 = 0x35 # 5 key
$R_Key_6 = 0x36 # 6 key
$R_Key_7 = 0x37 # 7 key
$R_Key_8 = 0x38 # 8 key
$R_Key_9 = 0x39 # 9 key
#--------------------------------------------------------------------------
$R_Key_A = 0x41 # A key
$R_Key_B = 0x42 # B key
$R_Key_C = 0x43 # C key
$R_Key_D = 0x44 # D key
$R_Key_E = 0x45 # E key
$R_Key_F = 0x46 # F key
$R_Key_G = 0x47 # G key
$R_Key_H = 0x48 # H key
$R_Key_I = 0x49 # I key
$R_Key_J = 0x4A # J key
$R_Key_K = 0x4B # K key
$R_Key_L = 0x4C # L key
$R_Key_M = 0x4D # M key
$R_Key_N = 0x4E # N key
$R_Key_O = 0x4F # O key
$R_Key_P = 0x50 # P key
$R_Key_Q = 0x51 # Q key
$R_Key_R = 0x52 # R key
$R_Key_S = 0x53 # S key
$R_Key_T = 0x54 # T key
$R_Key_U = 0x55 # U key
$R_Key_V = 0x56 # V key
$R_Key_W = 0x57 # W key
$R_Key_X = 0x58 # X key
$R_Key_Y = 0x59 # Y key
$R_Key_Z = 0x5A # Z key
#--------------------------------------------------------------------------
$R_Key_LWIN = 0x5B # Left Windows key (Microsoft Natural keyboard)
$R_Key_RWIN = 0x5C # Right Windows key (Natural keyboard)
$R_Key_APPS = 0x5D # Applications key (Natural keyboard)
#--------------------------------------------------------------------------
$R_Key_NUMPAD0 = 0x60 # Numeric keypad 0 key
$R_Key_NUMPAD1 = 0x61 # Numeric keypad 1 key
$R_Key_NUMPAD2 = 0x62 # Numeric keypad 2 key
$R_Key_NUMPAD3 = 0x63 # Numeric keypad 3 key
$R_Key_NUMPAD4 = 0x64 # Numeric keypad 4 key
$R_Key_NUMPAD5 = 0x65 # Numeric keypad 5 key
$R_Key_NUMPAD6 = 0x66 # Numeric keypad 6 key
$R_Key_NUMPAD7 = 0x67 # Numeric keypad 7 key
$R_Key_NUMPAD8 = 0x68 # Numeric keypad 8 key
$R_Key_NUMPAD9 = 0x69 # Numeric keypad 9 key
$R_Key_MULTIPLY = 0x6A # Multiply key (*)
$R_Key_ADD = 0x6B # Add key (+)
$R_Key_SEPARATOR = 0x6C # Separator key
$R_Key_SUBTRACT = 0x6D # Subtract key (-)
$R_Key_DECIMAL = 0x6E # Decimal key
$R_Key_DIVIDE = 0x6F # Divide key (/)
#--------------------------------------------------------------------------
$R_Key_F1 = 0x70 # F1 key
$R_Key_F2 = 0x71 # F2 key
$R_Key_F3 = 0x72 # F3 key
$R_Key_F4 = 0x73 # F4 key
$R_Key_F5 = 0x74 # F5 key
$R_Key_F6 = 0x75 # F6 key
$R_Key_F7 = 0x76 # F7 key
$R_Key_F8 = 0x77 # F8 key
$R_Key_F9 = 0x78 # F9 key
$R_Key_F10 = 0x79 # F10 key
$R_Key_F11 = 0x7A # F11 key
$R_Key_F12 = 0x7B # F12 key
#--------------------------------------------------------------------------
$R_Key_NUMLOCK = 0x90 # NUM LOCK key
$R_Key_SCROLL = 0x91 # SCROLL LOCK key
#--------------------------------------------------------------------------
$R_Key_LSHIFT = 0xA0 # Left SHIFT key
$R_Key_RSHIFT = 0xA1 # Right SHIFT key
$R_Key_LCONTROL = 0xA2 # Left CONTROL key
$R_Key_RCONTROL = 0xA3 # Right CONTROL key
$R_Key_L_ALT = 0xA4 # Left ALT key
$R_Key_R_ALT = 0xA5 # Right ALT key
#--------------------------------------------------------------------------
$R_Key_SEP = 0xBC # , key
$R_Key_DASH = 0xBD # - key
$R_Key_DOTT = 0xBE # . Key
module_function
@R_Key_Hash = {}
@R_Key_Repeat = {}
GetKeyState = Win32API.new("user32","GetAsyncKeyState",['I'],'I')
def press?(rkey)
return GetKeyState.call(rkey) != 0
end
def repeat?(rkey)
result = GetKeyState.call(rkey)
if result != 0
if @R_Key_Repeat[rkey].nil?
@R_Key_Repeat[rkey] = 0
return true
end
@R_Key_Repeat[rkey] += 1
else
@R_Key_Repeat[rkey] = nil
@R_Key_Hash[rkey] = 0
end
if !@R_Key_Repeat[rkey].nil? and @R_Key_Repeat[rkey] > 4 # 4乃准确数字
@R_Key_Repeat[rkey] = 0
return true
else
return false
end
end
def trigger?(rkey)
result = GetKeyState.call(rkey)
if @R_Key_Hash[rkey] == 1 and result != 0
return false
end
if result != 0
@R_Key_Hash[rkey] = 1
return true
else
@R_Key_Hash[rkey] = 0
return false
end
end
复制代码
作者:
chd114
时间:
2014-7-28 23:09
backspace消掉上一格···是指消掉19这个数里面的1吗?
作者:
你最珍贵
时间:
2014-7-29 17:23
貌似已经有过类似的脚本而且好像更高端233
作者:
某死灵法师
时间:
2014-7-29 18:13
chd114 发表于 2014-7-28 23:09
backspace消掉上一格···是指消掉19这个数里面的1吗?
不是,小键盘输入后会自动到下一格
总之就当作平常打字一样啦~
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1