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

Project1

 找回密码
 注册会员
搜索

如何在给定的场合,让玩家输入文字答案

查看数: 4293 | 评论数: 13 | 收藏 1
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2014-7-12 09:50

正文摘要:

现在文字是可以输出来 但是就是不能控制输入框出现的时机。求解,非常感谢!

回复

为什么不早说 发表于 2014-7-14 17:27:20
lanyuting 发表于 2014-7-12 14:10
我不是要改名字,是要让玩家输入文本,就想文字答案那种

这其实是用更改名字的方法输入答案
wolves 发表于 2014-7-14 09:34:03
本帖最后由 wolves 于 2014-7-14 09:41 编辑

我有一个输入脚本比较不错,看看这个脚本:
(总共有四段,显示的位置在最下面)
第一个:获取窗口句柄
  1. #==============================================================================
  2. # ■ Kernel
  3. #------------------------------------------------------------------------------
  4. #  该模块中定义了可供所有类使用的方法。Object 类中包含了该模块。
  5. #==============================================================================
  6. module Kernel
  7.   #--------------------------------------------------------------------------
  8.   # ● 需要的 Windows API 函数
  9.   #--------------------------------------------------------------------------
  10.   GetWindowThreadProcessId = Win32API.new("user32", "GetWindowThreadProcessId", "LP", "L")
  11.   GetWindow = Win32API.new("user32", "GetWindow", "LL", "L")
  12.   GetClassName = Win32API.new("user32", "GetClassName", "LPL", "L")
  13.   GetCurrentThreadId = Win32API.new("kernel32", "GetCurrentThreadId", "V", "L")
  14.   GetForegroundWindow = Win32API.new("user32", "GetForegroundWindow", "V", "L")
  15.   #--------------------------------------------------------------------------
  16.   # ● 获取窗口句柄
  17.   #--------------------------------------------------------------------------
  18.   def get_hWnd
  19.     # 获取调用线程(RM 的主线程)的进程标识
  20.     threadID = GetCurrentThreadId.call
  21.     # 获取 Z 次序中最靠前的窗口
  22.     hWnd = GetWindow.call(GetForegroundWindow.call, 0)
  23.     # 枚举所有窗口
  24.     while hWnd != 0
  25.       # 如果创建该窗口的线程标识匹配本线程标识
  26.       if threadID == GetWindowThreadProcessId.call(hWnd, 0)
  27.         # 分配一个 11 个字节的缓冲区
  28.         className = " " * 11
  29.         # 获取该窗口的类名
  30.         GetClassName.call(hWnd, className, 12)
  31.         # 如果匹配 RGSS Player 则跳出循环
  32.         break if className == "RGSS Player"
  33.       end
  34.       # 获取下一个窗口
  35.       hWnd = GetWindow.call(hWnd, 2)
  36.     end
  37.     return hWnd
  38.   end
  39. end
复制代码
第二个:TextBox
  1. #==============================================================================
  2. # ■ TextBox  by EngShun
  3. #------------------------------------------------------------------------------
  4. #  输入框的类。
  5. #==============================================================================
  6. class TextBox
  7.   #--------------------------------------------------------------------------
  8.   # ● 设置API
  9.   #--------------------------------------------------------------------------
  10.   CreateWindow = Win32API.new('user32','CreateWindowEx','lpplllllllll','l')
  11.   ShowWindow = Win32API.new('user32','ShowWindow','ll','l')
  12.   DestroyWindow = Win32API.new('user32','DestroyWindow','l','l')
  13.   GetWindowText = Win32API.new('user32','GetWindowText','lpl','l')
  14.   SetWindowText = Win32API.new("user32", "SetWindowText", "lp", "l")
  15.   GetWindowTextLength = Win32API.new('user32','GetWindowTextLength','l','l')
  16.   UpdateWindow = Win32API.new('user32','UpdateWindow','l','i')
  17.   SetFocus = Win32API.new('user32','SetFocus','l','l')
  18.   SendMessage = Win32API.new('user32','SendMessage','llll','l')
  19.   MultiByteToWideChar = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  20.   WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')
  21.   GetKeyState = Win32API.new("user32","GetAsyncKeyState",'I','I')
  22.   #--------------------------------------------------------------------------
  23.   # ● 定义实例变量
  24.   #--------------------------------------------------------------------------
  25.   attr_accessor :focus
  26.   #--------------------------------------------------------------------------
  27.   # ● 初始化
  28.   #--------------------------------------------------------------------------
  29.   def initialize(text = "",limit = 8,number=false)
  30.     c = 0x40011000
  31.     c = (c|0x00001000) if number
  32.     [url=home.php?mod=space&uid=37298]@Window[/url] = CreateWindow.call(0, 'EDIT', u2s(text), c, 0, 480-22, 640, 22, get_hWnd, 0, 0, 0)
  33.     SendMessage.call(@window,0xC5,limit,0)
  34.     @focus = true
  35.     @disposed = false
  36.   end
  37.   #--------------------------------------------------------------------------
  38.   # ● 释放
  39.   #--------------------------------------------------------------------------
  40.   def dispose
  41.     @disposed = true
  42.     DestroyWindow.call(@window)
  43.   end
  44.   #--------------------------------------------------------------------------
  45.   # ● 刷新
  46.   #--------------------------------------------------------------------------
  47.   def update
  48.     return if !@focus or @disposed
  49.     UpdateWindow.call(@window)
  50.     SetFocus.call(@window)
  51.   end
  52.   #--------------------------------------------------------------------------
  53.   # ● 获取内容
  54.   #--------------------------------------------------------------------------
  55.   def text
  56.     return if @disposed
  57.     l = GetWindowTextLength.call(@window)
  58.     str = "\0" * (l + 1)
  59.     GetWindowText.call(@window, str, l+1)
  60.     return s2u(str.delete("\0"))
  61.   end
  62.   #--------------------------------------------------------------------------
  63.   # ● 更改内容
  64.   #--------------------------------------------------------------------------
  65.   def text=(str)
  66.     SetWindowText.call(@window, u2s(str))
  67.     return if @disposed
  68.   end
  69.   #--------------------------------------------------------------------------
  70.   # ● 获取光标位置
  71.   #--------------------------------------------------------------------------
  72.   def sel_pos
  73.     return if @disposed
  74.     return (SendMessage.call(@window,0xB0,0,0) % 65535) / 2
  75.   end
  76.   #--------------------------------------------------------------------------
  77.   # ● 设置光标位置
  78.   #--------------------------------------------------------------------------
  79.   def sel_pos=(i)
  80.     return if @disposed
  81.     SendMessage.call(@window,0xB1,i,i)
  82.   end
  83.   #--------------------------------------------------------------------------
  84.   # ● 获取光标位置
  85.   #--------------------------------------------------------------------------
  86.   def limit
  87.     return if @disposed
  88.     return SendMessage.call(@window,0xD5,0,0)
  89.   end
  90.   #--------------------------------------------------------------------------
  91.   # ● 设置光标位置
  92.   #--------------------------------------------------------------------------
  93.   def limit=(i)
  94.     return if @disposed
  95.     SendMessage.call(@window,0xC5,i,0)
  96.   end
  97.   #--------------------------------------------------------------------------
  98.   # ● 是否按下回车键
  99.   #--------------------------------------------------------------------------
  100.   def press_enter?
  101.     return if @disposed
  102.     return (@focus and (GetKeyState.call(13) != 0))
  103.   end
  104.   #--------------------------------------------------------------------------
  105.   # ● 转码:系统转UTF-8
  106.   #--------------------------------------------------------------------------
  107.   def s2u(text)
  108.     len = MultiByteToWideChar.call(0, 0, text, -1, nil, 0);
  109.     buf = "\0" * (len*2)
  110.     MultiByteToWideChar.call(0, 0, text, -1, buf, buf.size/2);
  111.     len = WideCharToMultiByte.call(65001, 0, buf, -1, nil, 0, nil, nil);
  112.     ret = "\0" * len
  113.     WideCharToMultiByte.call(65001, 0, buf, -1, ret, ret.size, nil, nil);
  114.     return ret.delete("\000")
  115.   end
  116.   #--------------------------------------------------------------------------
  117.   # ● 转码:UTF-8转系统
  118.   #--------------------------------------------------------------------------
  119.   def u2s(text)
  120.     len = MultiByteToWideChar.call(65001, 0, text, -1, nil, 0);
  121.     buf = "\0" * (len*2)
  122.     MultiByteToWideChar.call(65001, 0, text, -1, buf, buf.size/2);
  123.     len = WideCharToMultiByte.call(0, 0, buf, -1, nil, 0, nil, nil);
  124.     ret = "\0" * len
  125.     WideCharToMultiByte.call(0, 0, buf, -1, ret, ret.size, nil, nil);
  126.     return ret.delete("\000")
  127.   end
  128.   private :u2s ,:s2u # 定义私有功能
  129. end
复制代码
第三个:Sprite_TextBox
  1. #==============================================================================
  2. # ■ Sprite_TextBox  by EngShun
  3. #------------------------------------------------------------------------------
  4. #  显示输入框用的活动块。
  5. #==============================================================================
  6. class Sprite_TextBox < Sprite
  7.   #--------------------------------------------------------------------------
  8.   # ● 设置缓存的类
  9.   #--------------------------------------------------------------------------
  10.   Cache = Struct.new(:bc ,:fc ,:fs ,:text ,:sel_pos)
  11.   #--------------------------------------------------------------------------
  12.   # ● 定义实例变量
  13.   #--------------------------------------------------------------------------
  14.   attr_accessor :textbox
  15.   attr_accessor :back_color
  16.   #--------------------------------------------------------------------------
  17.   # ● 初始化
  18.   #--------------------------------------------------------------------------
  19.   def initialize(x = 0,y = 0,w = 128,h = 32,bc = Color.new(255,255,255),fc = Color.new(0,0,0),tb = TextBox.new)
  20.     super(nil)
  21.     self.x = x
  22.     self.y = y
  23.     self.bitmap = Bitmap.new(w,h)
  24.     self.bitmap.font.color = fc
  25.     @back_color = bc
  26.     @textbox = tb
  27.     [url=home.php?mod=space&uid=341345]@Cache[/url] = Cache.new
  28.     refresh
  29.   end
  30.   #--------------------------------------------------------------------------
  31.   # ● 刷新
  32.   #--------------------------------------------------------------------------
  33.   def update
  34.     super
  35.     return unless @textbox.focus
  36.     @textbox.update
  37.     if (self.bitmap.font.color != @cache.fc) or
  38.        (self.bitmap.font.size != @cache.fs) or
  39.        (@textbox.sel_pos != @cache.sel_pos) or
  40.        (@textbox.text != @cache.text) or
  41.        (@back_color != @cache.bc)
  42.       refresh
  43.     end
  44.   end
  45.   #--------------------------------------------------------------------------
  46.   # ● 释放
  47.   #--------------------------------------------------------------------------
  48.   def dispose
  49.     self.bitmap.dispose
  50.     @textbox.dispose
  51.     super
  52.   end
  53.   #--------------------------------------------------------------------------
  54.   # ● 内容刷新
  55.   #--------------------------------------------------------------------------
  56.   def refresh
  57.     @cache.fc = self.bitmap.font.color
  58.     @cache.fs = self.bitmap.font.size
  59.     @cache.sel_pos = @textbox.sel_pos
  60.     @cache.text = @textbox.text
  61.     @cache.bc = @back_color
  62.     w = self.bitmap.width
  63.     h = self.bitmap.height
  64.     self.bitmap.fill_rect(0,0,w,h,@back_color)
  65.     self.bitmap.draw_text(4,0,w-4,h,@textbox.text)
  66.     t = @textbox.text.scan(/./)[0,@textbox.sel_pos].join("")
  67.     s = self.bitmap.text_size(t).width
  68.     self.bitmap.draw_text((4 + s)-16,0,32,h,"|",1)
  69.   end
  70.   private :refresh # 设置私有功能
  71. end
复制代码
第四个:Scene_Name
  1. #==============================================================================
  2. # ■ Scene_Name
  3. #------------------------------------------------------------------------------
  4. #  处理名称输入画面的类。
  5. #==============================================================================
  6. class Scene_Name
  7.   #--------------------------------------------------------------------------
  8.   # ● 主处理
  9.   #--------------------------------------------------------------------------
  10.   def main
  11.     @spriteset = Spriteset_Map.new
  12.     @actor = $game_actors[$game_temp.name_actor_id]
  13.     [url=home.php?mod=space&uid=37298]@Window[/url] = Window_Base.new(0,480-128,640,128)
  14.     @window.contents = Bitmap.new(576,96)
  15.     @window.draw_actor_graphic(@actor, 60, 70)
  16.     @window.contents.draw_text(128,0,480,32,$input)
  17.     @window.opacity = 180
  18.     @sprite_tb = Sprite_TextBox.new(144,410,464)
  19.     @sprite_tb.z = 500
  20.     @sprite_tb.textbox.limit = $game_temp.name_max_char
  21.     @sprite_tb.textbox.text = @actor.name
  22.     @sprite_tb.textbox.sel_pos = @actor.name.length
  23.     @sprite_tb.opacity = 180
  24.     Graphics.transition
  25.     loop do
  26.       Graphics.update
  27.       Input.update
  28.       update
  29.       break if $scene != self
  30.     end
  31.     Graphics.freeze
  32.     @sprite_tb.dispose
  33.     @window.dispose
  34.     @spriteset.dispose
  35.   end
  36.   #--------------------------------------------------------------------------
  37.   # ● 刷新画面
  38.   #--------------------------------------------------------------------------
  39.   def update
  40.     @sprite_tb.update
  41.     if @sprite_tb.textbox.press_enter?
  42.       if @sprite_tb.textbox.text == ""
  43.         $game_system.se_play($data_system.buzzer_se)
  44.         return
  45.       end
  46.       @actor.name = @sprite_tb.textbox.text
  47.       $scene = Scene_Map.new
  48.     end
  49.   end
  50. end
复制代码
调用方法: 修改某个角色的姓名,这个角色最好不用,因为他的名字就是你要输入的东西,角色姓名的调用方法自己百度吧,是个字符串,把这个字符串代入你要的变量,一切都ok了。
日月星辰 发表于 2014-7-13 09:24:05
https://rpg.blue/thread-363065-1-1.html

链接只是打个广告= =不过就是类似的功能呢

wwwcctvty 发表于 2014-7-12 18:36:38
写一个专用的scene,需要时调用出来就行了
lanyuting 发表于 2014-7-12 14:10:37
woaivo2 发表于 2014-7-12 10:24
创建另一个空白角色  不要有字 然后利用条件分歧 最后是这样

我不是要改名字,是要让玩家输入文本,就想文字答案那种

点评

他确实是这么做了……  发表于 2014-7-12 15:19
至高无上 发表于 2014-7-12 13:21:38
在首页看到两个一摸一样的图片,我还以为6R又坏掉了
杨广 发表于 2014-7-12 11:42:16
我爱吴悦淇 发表于 2014-7-12 11:16:52
http://www.66rpg.com/articles/4837
个人感觉这个会比较适合
lanyuting 发表于 2014-7-12 10:46:58
本帖最后由 hys111111 于 2014-7-12 12:34 编辑

加了这个脚本。。。求解啊
  1. =begin

  2.   脚本:【完美输入法修正】
  3.   
  4.   功能:输入法。
  5.   
  6.   说明: 直接用Type_Field创建输入域即可进行输入,在此可根据Type_Field域对象
  7.   
  8.         的活动标记active来自定义刷新等,在Type_Field中需要自己处理特殊按键
  9.         
  10.         的处理方法。具体不明白之处请参考范例工程。

  11.   作者:灼眼的夏娜
  12.   
  13.   补充: 至于以前那个版本也许很多人都注意到那个烦人的问题了吧,按Enter和Tab那
  14.   
  15.         些会出现不爽的声音,这个版本解决了那个问题,并简化添加了Type_Field类
  16.         
  17.         来方便创建输入域。

  18. =end

  19. #==============================================================================
  20. # ■ RInput
  21. #------------------------------------------------------------------------------
  22. #  全键盘处理的模块。
  23. #==============================================================================
  24. module RInput
  25.   
  26.   #--------------------------------------------------------------------------
  27.   # ● 常量定义
  28.   #--------------------------------------------------------------------------
  29.   ENTER             = 0x0D
  30.   SPACE             = 0x20
  31.   UP                = 0x26
  32.   DOWN              = 0x28
  33.   LEFT              = 0x25
  34.   RIGHT             = 0x27
  35.   
  36.   LCTRL             = 0xA2
  37.   LALT              = 0xA4

  38.   #--------------------------------------------------------------------------
  39.   # ● 临时Hash
  40.   #--------------------------------------------------------------------------
  41.   @R_Key_Hash = {}
  42.   @R_Key_Repeat = {}
  43.   
  44.   #--------------------------------------------------------------------------
  45.   # ● 唯一API
  46.   #--------------------------------------------------------------------------
  47.   GetKeyState = Win32API.new("user32","GetAsyncKeyState",['I'],'I')

  48.   #--------------------------------------------------------------------------
  49.   # ● 单键处理
  50.   #--------------------------------------------------------------------------
  51.   def self.trigger?(rkey)
  52.     result = GetKeyState.call(rkey)
  53.     if @R_Key_Hash[rkey] == 1 and result != 0
  54.       return false
  55.     end
  56.     if result != 0
  57.       @R_Key_Hash[rkey] = 1
  58.       return true
  59.     else
  60.       @R_Key_Hash[rkey] = 0
  61.       return false
  62.     end
  63.   end

  64. end

  65. #==============================================================================
  66. # ■ EasyConv
  67. #------------------------------------------------------------------------------
  68. #  转码模块。
  69. #==============================================================================
  70. module EasyConv

  71.   #--------------------------------------------------------------------------
  72.   # ● 常量定义
  73.   #--------------------------------------------------------------------------
  74.   CP_ACP = 0
  75.   CP_UTF8 = 65001

  76.   #--------------------------------------------------------------------------
  77.   # ● 模块函数
  78.   #--------------------------------------------------------------------------
  79.   module_function
  80.   
  81.   #--------------------------------------------------------------------------
  82.   # ● 转码
  83.   #--------------------------------------------------------------------------
  84.   def s2u(text)

  85.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  86.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')

  87.     len = m2w.call(CP_ACP, 0, text, -1, nil, 0)
  88.     buf = "\0" * (len*2)
  89.     m2w.call(CP_ACP, 0, text, -1, buf, buf.size/2)

  90.     len = w2m.call(CP_UTF8, 0, buf, -1, nil, 0, nil, nil)
  91.     ret = "\0" * len
  92.     w2m.call(CP_UTF8, 0, buf, -1, ret, ret.size, nil, nil)
  93.     ret[-1] = ""
  94.    
  95.     return ret
  96.   end

  97.   #--------------------------------------------------------------------------
  98.   # ● 转码
  99.   #--------------------------------------------------------------------------
  100.   def u2s(text)
  101.    
  102.     m2w = Win32API.new('kernel32', 'MultiByteToWideChar', 'ilpipi', 'i')
  103.     w2m = Win32API.new('kernel32', 'WideCharToMultiByte', 'ilpipipp', 'i')

  104.     len = m2w.call(CP_UTF8, 0, text, -1, nil, 0)
  105.     buf = "\0" * (len*2)
  106.     m2w.call(CP_UTF8, 0, text, -1, buf, buf.size/2)

  107.     len = w2m.call(CP_ACP, 0, buf, -1, nil, 0, nil, nil)
  108.     ret = "\0" * len
  109.     w2m.call(CP_ACP, 0, buf, -1, ret, ret.size, nil, nil)
  110.    
  111.     return ret
  112.   end

  113. end

  114. #==============================================================================
  115. # ■ TypeAPI
  116. #------------------------------------------------------------------------------
  117. #  输入法相关API模块。
  118. #==============================================================================
  119. module TypeAPI
  120.   
  121.   #--------------------------------------------------------------------------
  122.   # ● API定义
  123.   #--------------------------------------------------------------------------
  124.   GPPS         = Win32API.new("kernel32","GetPrivateProfileString",'pppplp','l')
  125.   FindWindow   = Win32API.new("user32", "FindWindow", 'pp', 'i')
  126.   CreateWindow = Win32API.new("NetGame","CreatWnd",'l','l')
  127.   SetHK        = Win32API.new("NetGame","SetHK",'v','v')
  128.   GetText      = Win32API.new("NetGame","GetWndText",'l','p')
  129.   SetFocus     = Win32API.new("user32","SetFocus",'l','l')
  130.   IfBack       = Win32API.new("NetGame","If_Back",'v','i')
  131.   StartType    = Win32API.new("NetGame","StartType",'v','v')
  132.   EndType      = Win32API.new("NetGame","EndType",'v','v')
  133.   GetKeyInfos  = Win32API.new("NetGame","GetKeyInfo",'v','i')
  134.   
  135.   #--------------------------------------------------------------------------
  136.   # ● 模块函数
  137.   #--------------------------------------------------------------------------
  138.   module_function
  139.   
  140.   #--------------------------------------------------------------------------
  141.   # ● 获取参数
  142.   #--------------------------------------------------------------------------
  143.   def getParam
  144.     val = "\0" * 256
  145.     GPPS.call("Game","Title","",val,256,"./Game.ini")
  146.     val.delete!("\0")
  147.     return val
  148.   end
  149.   
  150.   #--------------------------------------------------------------------------
  151.   # ● 获取窗口
  152.   #--------------------------------------------------------------------------
  153.   def findWindow
  154.     return FindWindow.call("RGSS Player",self.getParam)
  155.   end
  156.   
  157.   #--------------------------------------------------------------------------
  158.   # ● 创建窗口
  159.   #--------------------------------------------------------------------------
  160.   def createWindow(hwnd)
  161.     return CreateWindow.call(hwnd)
  162.   end
  163.   
  164.   #--------------------------------------------------------------------------
  165.   # ● 设置HK
  166.   #--------------------------------------------------------------------------
  167.   def setHK
  168.     SetHK.call
  169.   end
  170.   
  171.   #--------------------------------------------------------------------------
  172.   # ● 获取文字
  173.   #--------------------------------------------------------------------------
  174.   def getText
  175.     return EasyConv.s2u(GetText.call(@subhwnd))
  176.   end
  177.   
  178.   #--------------------------------------------------------------------------
  179.   # ● 设置焦点
  180.   #--------------------------------------------------------------------------
  181.   def setFocus
  182.     SetFocus.call(@subhwnd)
  183.   end
  184.   
  185.   #--------------------------------------------------------------------------
  186.   # ● 转换焦点
  187.   #--------------------------------------------------------------------------
  188.   def lostFocus
  189.     SetFocus.call(@hwnd)
  190.   end
  191.   
  192.   #--------------------------------------------------------------------------
  193.   # ● 退格判定
  194.   #--------------------------------------------------------------------------
  195.   def ifBack
  196.     return IfBack.call
  197.   end
  198.   
  199.   #--------------------------------------------------------------------------
  200.   # ● 开始输入
  201.   #--------------------------------------------------------------------------
  202.   def startType
  203.     StartType.call
  204.   end
  205.   
  206.   #--------------------------------------------------------------------------
  207.   # ● 结束输入
  208.   #--------------------------------------------------------------------------
  209.   def endType
  210.     EndType.call
  211.   end
  212.   
  213.   #--------------------------------------------------------------------------
  214.   # ● 输入中特殊按键处理
  215.   #--------------------------------------------------------------------------
  216.   def getKeyInfos
  217.     return GetKeyInfos.call
  218.   end
  219.   
  220.   #--------------------------------------------------------------------------
  221.   # ● 获取句柄
  222.   #--------------------------------------------------------------------------
  223.   @hwnd    = self.findWindow
  224.   
  225.   @subhwnd = self.createWindow(@hwnd)

  226.   #--------------------------------------------------------------------------
  227.   # ● 设置HK应用
  228.   #--------------------------------------------------------------------------
  229.   self.setHK
  230.   
  231. end

  232. #==============================================================================
  233. # ■ Type_Field
  234. #------------------------------------------------------------------------------
  235. #  处理输入域的类。
  236. #==============================================================================
  237. class Type_Field
  238.   
  239.   #--------------------------------------------------------------------------
  240.   # ● 定义实例变量
  241.   #--------------------------------------------------------------------------
  242.   attr(:active)
  243.   
  244.   attr_accessor :event_tab
  245.   attr_accessor :event_enter
  246.   attr_accessor :enter_esc
  247.   
  248.   #--------------------------------------------------------------------------
  249.   # ● 初始化
  250.   #--------------------------------------------------------------------------
  251.   def initialize(v,default_text = "",default_careth = nil,default_fonts = 25,\
  252.     default_fontc = Color.new(0,0,0))
  253.     # active
  254.     @active = true
  255.     # 视口
  256.     rect = v.rect
  257.     @v = Viewport.new(rect.x,rect.y,rect.width,rect.height)
  258.     [url=home.php?mod=space&uid=133944]@w[/url] = rect.width
  259.     @h = rect.height
  260.     # 属性
  261.     @caret_h = default_careth.nil? ? @h : [@h,default_careth].min
  262.     @caret_y = rect.y + (@h - @caret_h) / 2
  263.     @font_size = [default_fonts,@h].min
  264.     # 描绘contents
  265.     @cts = Sprite.new(@v)
  266.     @cts.bitmap = Bitmap.new([url=home.php?mod=space&uid=133944]@w[/url] - 3,@h)
  267.     @cts.bitmap.font.size = @font_size
  268.     @cts.bitmap.font.color = default_fontc
  269.     # 辅助属性
  270.     @bk_count = 0
  271.     @text = default_text.scan(/./)
  272.     @max_width = @w - 3
  273.     @caret_pos = @text.size
  274.     @save_pos = @caret_pos
  275.     # 光标Caret
  276.     @v1 = Viewport.new(rect.x,@caret_y,@w + 3,@caret_h)
  277.     @caret_sp = Sprite.new(@v1)
  278.     @caret_bitmap = Bitmap.new(3,@caret_h)
  279.     @caret_bitmap.fill_rect(0,0,1,@caret_h,Color.new(0,0,0,180))
  280.     @caret_bitmap.fill_rect(1,0,1,@caret_h,Color.new(0,0,0))
  281.     @caret_bitmap.fill_rect(2,0,1,@caret_h,Color.new(120,120,120))
  282.     @caret_sp.bitmap = @caret_bitmap
  283.     @caret_sp.x = self.get_width(@text[0,@caret_pos].to_s)
  284.     @caret_sp.y = 0
  285.     @caret_sp.visible = false
  286.     @caret_flash_count = 0
  287.    
  288.     @event_tab = nil
  289.     @event_enter = nil
  290.     @event_esc = nil
  291.    
  292.     # 刷新
  293.     refresh
  294.     # 设置焦点
  295.     TypeAPI.setFocus
  296.     # 开始输入
  297.     TypeAPI.startType
  298.     #先清空
  299.     TypeAPI.getText
  300.   end
  301.   
  302.   #--------------------------------------------------------------------------
  303.   # ● 设置活动标记
  304.   #--------------------------------------------------------------------------
  305.   def active=(value)
  306.     if value != true and value != false
  307.       return
  308.     end
  309.     @active = value
  310.     @caret_sp.visible = @active
  311.   end
  312.   
  313.   #--------------------------------------------------------------------------
  314.   # ● 释放
  315.   #--------------------------------------------------------------------------
  316.   def dispose
  317.     @caret_bitmap.dispose
  318.     @caret_sp.bitmap.dispose
  319.     @caret_sp.dispose
  320.     @cts.bitmap.dispose
  321.     @cts.dispose
  322.     @v.dispose
  323.     @v1.dispose
  324.   end

  325.   #--------------------------------------------------------------------------
  326.   # ● 刷新
  327.   #--------------------------------------------------------------------------
  328.   def refresh
  329.     @cts.bitmap.clear
  330.     @cts.bitmap.draw_text(0,0,@w,@h,@text.to_s)
  331.   end

  332.   #--------------------------------------------------------------------------
  333.   # ● 获取文字
  334.   #--------------------------------------------------------------------------
  335.   def get_text
  336.     return @text.to_s
  337.   end

  338.   #--------------------------------------------------------------------------
  339.   # ● 取得字符宽度
  340.   #--------------------------------------------------------------------------
  341.   def get_width(str)
  342.     return @cts.bitmap.text_size(str).width
  343.   end
  344.   
  345.   #--------------------------------------------------------------------------
  346.   # ● 更新
  347.   #--------------------------------------------------------------------------
  348.   def update
  349.     # 非激活状态则返回
  350.     unless @active
  351.       return
  352.     end
  353.     # 获取按键信息
  354.     key_info = TypeAPI.getKeyInfos
  355.     case key_info
  356.     when 0x09 # Tab
  357.       # 按下 Tab 键的情况自己定义怎么处理
  358.       @event_tab.call(self) if @event_tab
  359.       return
  360.     when 0x0d # Enter
  361.       # 按下 Enter 键的情况自己定义怎么处理
  362.       @event_enter.call(self) if @event_enter
  363.       return
  364.     when 0x1B # Esc
  365.       # 按下 Esc 键的情况自己定义怎么处理
  366.       @event_esc.call(self) if @event_esc
  367.       return
  368.     end
  369.     self.update_text
  370.     self.update_lrb
  371.     self.update_back
  372.     self.update_caret
  373.   end

  374.   #--------------------------------------------------------------------------
  375.   # ● 更新文字
  376.   #--------------------------------------------------------------------------
  377.   def update_text
  378.     # 文字刷新
  379.     TypeAPI.setFocus
  380.     text = TypeAPI.getText
  381.     if text != ""
  382.       for char in text.scan(/./)
  383.         if self.get_width(@text.to_s + char) <= @max_width
  384.           @text[@caret_pos,0] = char
  385.           @caret_pos += 1
  386.         else
  387.           break
  388.         end
  389.       end
  390.       refresh
  391.     end
  392.   end
  393.   
  394.   #--------------------------------------------------------------------------
  395.   # ● 更新左右按键
  396.   #--------------------------------------------------------------------------
  397.   def update_lrb
  398.     if RInput.trigger?(RInput::LEFT) and @caret_pos > 0
  399.       @caret_pos -= 1
  400.       return
  401.     end
  402.     if RInput.trigger?(RInput::RIGHT) and @caret_pos < @text.size
  403.       @caret_pos += 1
  404.       return
  405.     end
  406.   end
  407.   
  408.   #--------------------------------------------------------------------------
  409.   # ● 更新退格
  410.   #--------------------------------------------------------------------------
  411.   def update_back
  412.     # 前退格处理
  413.     @bk_count += TypeAPI.ifBack
  414.     if @bk_count > 0
  415.       @bk_count -= 1
  416.       if @caret_pos > 0
  417.         @text.delete_at(@caret_pos - 1);@caret_pos -= 1;refresh
  418.       end
  419.     end
  420.   end
  421.   
  422.   #--------------------------------------------------------------------------
  423.   # ● 更新光标闪烁
  424.   #--------------------------------------------------------------------------
  425.   def update_caret
  426.     # 闪烁计时
  427.     @caret_flash_count += 1
  428.     if @caret_flash_count == 20
  429.       @caret_sp.visible = !@caret_sp.visible
  430.       @caret_flash_count = 0
  431.     end
  432.     # Caret位置刷新
  433.     if @save_pos != @caret_pos
  434.       @save_pos = @caret_pos
  435.       @caret_sp.x = self.get_width(@text[0,@save_pos].to_s)
  436.     end
  437.   end
  438.   
  439. end
复制代码

点评

我在你的另一个帖子已经回复了  发表于 2014-7-12 11:17
拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

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

GMT+8, 2024-11-26 23:51

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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