设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 3034|回复: 3
打印 上一主题 下一主题

[原创发布] 强化数值输入的处理(用数字键输入)

[复制链接]

Lv3.寻梦者

梦石
3
星屑
178
在线时间
161 小时
注册时间
2010-9-30
帖子
114
跳转到指定楼层
1
发表于 2014-7-28 14:29:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 某死灵法师 于 2014-7-28 14:37 编辑

有没有发现RMXP自带的数值输入处理好坑!!
上下左右操作什么的好麻烦!!
于是这个强化版数值输入处理脚本诞生了……
使用方法:替换原来的Window_InputNumber
功能:数字键直接输入(大小键盘均支持),backspace消掉上一格,delete消掉当前格,原本的上下左右依然保留
注:需要全键盘脚本
  1. #==============================================================================
  2. # ■ Window_InputNumber
  3. #------------------------------------------------------------------------------
  4. #  信息窗口内部使用、输入数值的窗口强化版 by司令死灵法师
  5. #==============================================================================

  6. class Window_InputNumber < Window_Base
  7.   #--------------------------------------------------------------------------
  8.   # ● 初始化对像
  9.   #     digits_max : 位数
  10.   #--------------------------------------------------------------------------
  11.   def initialize(digits_max)
  12.     @digits_max = digits_max
  13.     @number = 0
  14.     # 从数字的幅度计算(假定与 0~9 等幅)光标的幅度
  15.     dummy_bitmap = Bitmap.new(32, 32)
  16.     @cursor_width = dummy_bitmap.text_size("0").width + 8
  17.     dummy_bitmap.dispose
  18.     super(0, 0, @cursor_width * @digits_max + 32, 64)
  19.     self.contents = Bitmap.new(width - 32, height - 32)
  20.     self.z += 9999
  21.     self.opacity = 0
  22.     @index = 0
  23.     refresh
  24.     update_cursor_rect
  25.   end
  26.   #--------------------------------------------------------------------------
  27.   # ● 取得数值
  28.   #--------------------------------------------------------------------------
  29.   def number
  30.     return @number
  31.   end
  32.   #--------------------------------------------------------------------------
  33.   # ● 设置数值
  34.   #     number : 新的数值
  35.   #--------------------------------------------------------------------------
  36.   def number=(number)
  37.     @number = [[number, 0].max, 10 ** @digits_max - 1].min
  38.     refresh
  39.   end
  40.   #--------------------------------------------------------------------------
  41.   # ● 更新光标矩形
  42.   #--------------------------------------------------------------------------
  43.   def update_cursor_rect
  44.     self.cursor_rect.set(@index * @cursor_width, 0, @cursor_width, 32)
  45.   end
  46.   #--------------------------------------------------------------------------
  47.   # ● 刷新画面
  48.   #--------------------------------------------------------------------------
  49.   def update
  50.     super
  51.     #按下小键盘数字键
  52.     0x60.upto(0x69) do |key|
  53.       if Kboard.repeat?(key)
  54.         $game_system.se_play($data_system.cursor_se)
  55.         add_number_in(key - 0x60)
  56.         #光标右移一格
  57.         if @digits_max >= 2
  58.           @index = (@index + 1) % @digits_max
  59.         end
  60.         refresh
  61.       end
  62.     end
  63.     #按下大键盘数字键
  64.     0x30.upto(0x39) do |key|
  65.       if Kboard.repeat?(key)
  66.         $game_system.se_play($data_system.cursor_se)
  67.         add_number_in(key - 0x30)
  68.         #光标右移一格
  69.         if @digits_max >= 2
  70.           @index = (@index + 1) % @digits_max
  71.         end
  72.         refresh
  73.       end
  74.     end
  75.     #按下backspace,消除上一格
  76.     if Kboard.repeat?($R_Key_BACK)
  77.       $game_system.se_play($data_system.cancel_se)
  78.       #光标左移一格
  79.       if @digits_max >= 2
  80.         @index = (@index + @digits_max - 1) % @digits_max
  81.       end
  82.       add_number_in(0)
  83.       refresh
  84.     end
  85.     #按下delete,消除当前格
  86.     if Kboard.repeat?($R_Key_DELETE)
  87.       $game_system.se_play($data_system.cancel_se)
  88.       add_number_in(0)
  89.       refresh
  90.     end
  91.     # 方向键上
  92.     if Kboard.repeat?($R_Key_UP)
  93.       $game_system.se_play($data_system.cursor_se)
  94.       # 取得现在位置的数字位数
  95.       place = 10 ** (@digits_max - 1 - @index)
  96.       n = @number / place % 10
  97.       @number -= n * place
  98.       n = (n + 1) % 10
  99.       # 再次设置现在位的数字
  100.       @number += n * place
  101.       refresh
  102.     end
  103.     # 方向键下
  104.     if Kboard.repeat?($R_Key_DOWN)
  105.       $game_system.se_play($data_system.cursor_se)
  106.       # 取得现在位置的数字位数
  107.       place = 10 ** (@digits_max - 1 - @index)
  108.       n = @number / place % 10
  109.       @number -= n * place
  110.       n = (n - 1) % 10
  111.       # 再次设置现在位的数字
  112.       @number += n * place
  113.       refresh
  114.     end
  115.     # 方向键左
  116.     if Kboard.repeat?($R_Key_LEFT)
  117.       if @digits_max >= 2
  118.         $game_system.se_play($data_system.cursor_se)
  119.         @index = (@index + @digits_max - 1) % @digits_max
  120.       end
  121.     end
  122.     # 方向键右
  123.     if Kboard.repeat?($R_Key_RIGHT)
  124.       if @digits_max >= 2
  125.         $game_system.se_play($data_system.cursor_se)
  126.         @index = (@index + 1) % @digits_max
  127.       end
  128.     end
  129.     update_cursor_rect
  130.   end
  131.   #--------------------------------------------------------------------------
  132.   # ● 刷新
  133.   #--------------------------------------------------------------------------
  134.   def refresh
  135.     self.contents.clear
  136.     self.contents.font.color = normal_color
  137.     s = sprintf("%0*d", @digits_max, @number)
  138.     for i in 0...@digits_max
  139.       self.contents.draw_text(i * @cursor_width + 4, 0, 32, 32, s[i,1])
  140.     end
  141.   end
  142.   #--------------------------------------------------------------------------
  143.   # ● 处理当前位置的数值输入
  144.   #--------------------------------------------------------------------------
  145.   def add_number_in(in_number)
  146.     place = 10 ** (@digits_max - 1 - @index)
  147.     n = @number / place % 10
  148.     @number -= n * place
  149.     n = in_number
  150.     @number += n * place
  151.   end
  152. end
  153. #==============================================================================
  154. # 本脚本来自www.66RPG.com,使用和转载请保留此信息
  155. #==============================================================================
复制代码
再附上全键盘脚本:
  1. #==============================================================================
  2. # 本脚本来自www.66RPG.com,使用和转载请保留此信息
  3. #==============================================================================
  4. #
  5. #  全键盘
  6. # ★、Kboard.press?($R_Key_CTLR)  返回是否按下这个键的判断。
  7. #     比如条件分歧:Kboard.keyboard($R_Key_H)
  8. #                   则当按下键盘H键的时候条件分歧成立
  9. #
  10. # ★、Kboard.repeat?(键位) == 1 当连续按住某键返回真,否则为false
  11. #     比如条件分歧:Kboard.keyb($R_Key_U) == 1
  12. #                   则当持续按下键盘U的时候条件分歧成立
  13. #
  14. # ★、Kboard.trigger?(键位,1) 似乎可以做开关用。按下一次变为true,再按变false
  15. #  修改:夏娜
  16. #
  17. #============================================================================

  18. module Kboard
  19.   #==========================================================================
  20.   # 以下是全键盘按键列表
  21.   #--------------------------------------------------------------------------
  22.   $Rmouse_BUTTON_L = 0x01        # left mouse button
  23.   $Rmouse_BUTTON_R = 0x02        # right mouse button
  24.   $Rmouse_BUTTON_M = 0x04        # middle mouse button
  25.   $Rmouse_BUTTON_4 = 0x05        # 4th mouse button
  26.   $Rmouse_BUTTON_5 = 0x06        # 5th mouse button
  27.   #--------------------------------------------------------------------------
  28.   $R_Key_BACK      = 0x08        # BACKSPACE key
  29.   $R_Key_TAB       = 0x09        # TAB key
  30.   $R_Key_RETURN    = 0x0D        # ENTER key
  31.   $R_Key_ENTER     = 0x0D        # ENTER key
  32.   $R_Key_SHIFT     = 0x10        # SHIFT key
  33.   $R_Key_CTLR      = 0x11        # CTLR key
  34.   $R_Key_CTRL      = 0x11        # CTLR key
  35.   $R_Key_ALT       = 0x12        # ALT key
  36.   $R_Key_PAUSE     = 0x13        # PAUSE key
  37.   $R_Key_CAPITAL   = 0x14        # CAPS LOCK key
  38.   $R_Key_ESCAPE    = 0x1B        # ESC key
  39.   $R_Key_SPACE     = 0x20        # SPACEBAR
  40.   $R_Key_PRIOR     = 0x21        # PAGE UP key
  41.   $R_Key_NEXT      = 0x22        # PAGE DOWN key
  42.   $R_Key_END       = 0x23        # END key
  43.   $R_Key_HOME      = 0x24        # HOME key
  44.   $R_Key_LEFT      = 0x25        # LEFT ARROW key
  45.   $R_Key_UP        = 0x26        # UP ARROW key
  46.   $R_Key_RIGHT     = 0x27        # RIGHT ARROW key
  47.   $R_Key_DOWN      = 0x28        # DOWN ARROW key
  48.   $R_Key_SELECT    = 0x29        # SELECT key
  49.   $R_Key_PRINT     = 0x2A        # PRINT key
  50.   $R_Key_SNAPSHOT  = 0x2C        # PRINT SCREEN key
  51.   $R_Key_INSERT    = 0x2D        # INS key
  52.   $R_Key_DELETE    = 0x2E        # DEL key
  53.   #--------------------------------------------------------------------------
  54.   $R_Key_0         = 0x30        # 0 key
  55.   $R_Key_1         = 0x31        # 1 key
  56.   $R_Key_2         = 0x32        # 2 key
  57.   $R_Key_3         = 0x33        # 3 key
  58.   $R_Key_4         = 0x34        # 4 key
  59.   $R_Key_5         = 0x35        # 5 key
  60.   $R_Key_6         = 0x36        # 6 key
  61.   $R_Key_7         = 0x37        # 7 key
  62.   $R_Key_8         = 0x38        # 8 key
  63.   $R_Key_9         = 0x39        # 9 key
  64.   #--------------------------------------------------------------------------
  65.   $R_Key_A         = 0x41        # A key
  66.   $R_Key_B         = 0x42        # B key
  67.   $R_Key_C         = 0x43        # C key
  68.   $R_Key_D         = 0x44        # D key
  69.   $R_Key_E         = 0x45        # E key
  70.   $R_Key_F         = 0x46        # F key
  71.   $R_Key_G         = 0x47        # G key
  72.   $R_Key_H         = 0x48        # H key
  73.   $R_Key_I         = 0x49        # I key
  74.   $R_Key_J         = 0x4A        # J key
  75.   $R_Key_K         = 0x4B        # K key
  76.   $R_Key_L         = 0x4C        # L key
  77.   $R_Key_M         = 0x4D        # M key
  78.   $R_Key_N         = 0x4E        # N key
  79.   $R_Key_O         = 0x4F        # O key
  80.   $R_Key_P         = 0x50        # P key
  81.   $R_Key_Q         = 0x51        # Q key
  82.   $R_Key_R         = 0x52        # R key
  83.   $R_Key_S         = 0x53        # S key
  84.   $R_Key_T         = 0x54        # T key
  85.   $R_Key_U         = 0x55        # U key
  86.   $R_Key_V         = 0x56        # V key
  87.   $R_Key_W         = 0x57        # W key
  88.   $R_Key_X         = 0x58        # X key
  89.   $R_Key_Y         = 0x59        # Y key
  90.   $R_Key_Z         = 0x5A        # Z key
  91.   #--------------------------------------------------------------------------
  92.   $R_Key_LWIN      = 0x5B        # Left Windows key (Microsoft Natural keyboard)
  93.   $R_Key_RWIN      = 0x5C        # Right Windows key (Natural keyboard)
  94.   $R_Key_APPS      = 0x5D        # Applications key (Natural keyboard)
  95.   #--------------------------------------------------------------------------
  96.   $R_Key_NUMPAD0   = 0x60        # Numeric keypad 0 key
  97.   $R_Key_NUMPAD1   = 0x61        # Numeric keypad 1 key
  98.   $R_Key_NUMPAD2   = 0x62        # Numeric keypad 2 key
  99.   $R_Key_NUMPAD3   = 0x63        # Numeric keypad 3 key
  100.   $R_Key_NUMPAD4   = 0x64        # Numeric keypad 4 key
  101.   $R_Key_NUMPAD5   = 0x65        # Numeric keypad 5 key
  102.   $R_Key_NUMPAD6   = 0x66        # Numeric keypad 6 key
  103.   $R_Key_NUMPAD7   = 0x67        # Numeric keypad 7 key
  104.   $R_Key_NUMPAD8   = 0x68        # Numeric keypad 8 key
  105.   $R_Key_NUMPAD9   = 0x69        # Numeric keypad 9 key
  106.   $R_Key_MULTIPLY  = 0x6A        # Multiply key (*)
  107.   $R_Key_ADD       = 0x6B        # Add key (+)
  108.   $R_Key_SEPARATOR = 0x6C        # Separator key
  109.   $R_Key_SUBTRACT  = 0x6D        # Subtract key (-)
  110.   $R_Key_DECIMAL   = 0x6E        # Decimal key
  111.   $R_Key_DIVIDE    = 0x6F        # Divide key (/)
  112.   #--------------------------------------------------------------------------
  113.   $R_Key_F1        = 0x70        # F1 key
  114.   $R_Key_F2        = 0x71        # F2 key
  115.   $R_Key_F3        = 0x72        # F3 key
  116.   $R_Key_F4        = 0x73        # F4 key
  117.   $R_Key_F5        = 0x74        # F5 key
  118.   $R_Key_F6        = 0x75        # F6 key
  119.   $R_Key_F7        = 0x76        # F7 key
  120.   $R_Key_F8        = 0x77        # F8 key
  121.   $R_Key_F9        = 0x78        # F9 key
  122.   $R_Key_F10       = 0x79        # F10 key
  123.   $R_Key_F11       = 0x7A        # F11 key
  124.   $R_Key_F12       = 0x7B        # F12 key
  125.   #--------------------------------------------------------------------------
  126.   $R_Key_NUMLOCK   = 0x90        # NUM LOCK key
  127.   $R_Key_SCROLL    = 0x91        # SCROLL LOCK key
  128.   #--------------------------------------------------------------------------
  129.   $R_Key_LSHIFT    = 0xA0        # Left SHIFT key
  130.   $R_Key_RSHIFT    = 0xA1        # Right SHIFT key
  131.   $R_Key_LCONTROL  = 0xA2        # Left CONTROL key
  132.   $R_Key_RCONTROL  = 0xA3        # Right CONTROL key
  133.   $R_Key_L_ALT    = 0xA4        # Left ALT key
  134.   $R_Key_R_ALT    = 0xA5        # Right ALT key
  135.   #--------------------------------------------------------------------------
  136.   $R_Key_SEP      = 0xBC        # , key
  137.   $R_Key_DASH      = 0xBD        # - key
  138.   $R_Key_DOTT      = 0xBE        # . Key
  139.   
  140.   
  141.   
  142. module_function

  143. @R_Key_Hash = {}
  144. @R_Key_Repeat = {}

  145. GetKeyState = Win32API.new("user32","GetAsyncKeyState",['I'],'I')

  146. def press?(rkey)
  147.    return GetKeyState.call(rkey) != 0
  148. end

  149. def repeat?(rkey)
  150.    result = GetKeyState.call(rkey)
  151.    if result != 0
  152.      if @R_Key_Repeat[rkey].nil?
  153.        @R_Key_Repeat[rkey] = 0
  154.        return true
  155.      end
  156.      @R_Key_Repeat[rkey] += 1
  157.    else
  158.      @R_Key_Repeat[rkey] = nil
  159.      @R_Key_Hash[rkey] = 0
  160.    end
  161.    if !@R_Key_Repeat[rkey].nil? and @R_Key_Repeat[rkey] > 4 # 4乃准确数字
  162.      @R_Key_Repeat[rkey] = 0
  163.      return true
  164.    else
  165.      return false
  166.    end
  167. end

  168. def trigger?(rkey)
  169.    result = GetKeyState.call(rkey)
  170.    if @R_Key_Hash[rkey] == 1 and result != 0
  171.      return false
  172.    end
  173.    if result != 0
  174.      @R_Key_Hash[rkey] = 1
  175.      return true
  176.    else
  177.      @R_Key_Hash[rkey] = 0
  178.      return false
  179.    end
  180. end
复制代码

评分

参与人数 1星屑 +30 收起 理由
无脑之人 + 30 塞糖

查看全部评分

Lv4.逐梦者

梦石
0
星屑
9280
在线时间
2504 小时
注册时间
2011-5-20
帖子
15389

开拓者

2
发表于 2014-7-28 23:09:45 | 只看该作者
backspace消掉上一格···是指消掉19这个数里面的1吗?
[img]http://service.t.sina.com.cn/widget/qmd/5339802982/c02e16bd/7.png
回复 支持 反对

使用道具 举报

Lv2.观梦者

故九江太守

梦石
0
星屑
554
在线时间
2159 小时
注册时间
2012-12-5
帖子
4463
3
发表于 2014-7-29 17:23:34 | 只看该作者
貌似已经有过类似的脚本而且好像更高端233
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
3
星屑
178
在线时间
161 小时
注册时间
2010-9-30
帖子
114
4
 楼主| 发表于 2014-7-29 18:13:26 | 只看该作者
chd114 发表于 2014-7-28 23:09
backspace消掉上一格···是指消掉19这个数里面的1吗?

不是,小键盘输入后会自动到下一格
总之就当作平常打字一样啦~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-9-22 01:05

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表