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

Project1

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

[原创发布] 汉字输入

[复制链接]

Lv3.寻梦者

梦石
0
星屑
1680
在线时间
91 小时
注册时间
2019-5-16
帖子
45
跳转到指定楼层
1
发表于 2019-6-8 20:49:47 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

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

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

x
本帖最后由 Aephiex 于 2019-6-8 20:54 编辑



如截图所示,是让用户在读音列表中选择,随后列出所有符合该读音的汉字。
进行长篇汉字输入时难免会有些麻烦,好在人名并不会太长。
可以通过 pageup pagedown 在读音列表中快速翻页,跳转到想要的读音那里。

RUBY 代码复制
  1. #==============================================================================
  2. # ◆ 汉字输入 by Aephiex
  3. #------------------------------------------------------------------------------
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #------------------------------------------------------------------------------
  9. #
  10. #  主要机能
  11. #  ◎ 给角色命名时可以输入汉字。
  12. #     先选择汉字的读音,再列出符合该读音的汉字,供用户选择。
  13. #     收录约6500个汉字,包括日常汉字和某些教学用汉字,满足大部分起名需求。
  14. #     选择汉字读音期间可通过 pageup 和 pagedown 快速翻页跳转到想要的读音。
  15. #     输入汉字期间,按 shift 可以退格。
  16. #  ◎ 增加在事件可调用的脚本指令 input_text(max_char = 8, default = "")
  17. #     要求玩家输入长度8的文本,默认为空。
  18. #     该指令会将玩家输入的文本作为返回值返回。
  19. #     可以用一个 $game_variables[...] 接收它,从而可以在事件文本中用 \V[...] 调用。
  20. #
  21. #  特别注意
  22. #  ◎ 请务必确保随赠的 "Kanji_CHS.dat" 已放在 Data 目录中。
  23. #
  24. #==============================================================================
  25.  
  26. module AephiexConfig
  27.  
  28.   # 是否允许输入希腊字母和俄语字母
  29.   # 注意在有些字体下这些字母会显示为□
  30.   GREEK_RUSSIAN   = true
  31.  
  32.   # 是否允许输入特殊字母(Á之类的戴帽子的英文字母等)
  33.   # 注意在有些字体下这些字母会显示为□
  34.   LATIN_SPECIAL   = true
  35.  
  36. end
  37.  
  38. module AephiexString
  39.  
  40.   # 汉字输入画面的指令名
  41.   NMI_ABC123           = "英/数"
  42.   NMI_GrRu             = "希/俄"
  43.   NMI_LatinSp          = "特殊字母"
  44.   NMI_SPCHAR           = "特殊字符"
  45.   NMI_Kanji            = "汉字"
  46.   NMI_OK               = "完成"
  47.   NMI_Back             = "退格"
  48.   NMI_Clear            = "清空"
  49.   NMI_Reset            = "重置"
  50.  
  51. end
  52.  
  53. #==============================================================================
  54. # □ Game_Interpreter
  55. #==============================================================================
  56. class Game_Interpreter
  57.   #--------------------------------------------------------------------------
  58.   # ¤ スクリプト:テキスト入力
  59.   #--------------------------------------------------------------------------
  60.   def input_text(max_char = 8, default = "")
  61.     return if $game_party.in_battle
  62.     SceneManager.call(Scene_Name)
  63.     SceneManager.scene.prepare(default, max_char)
  64.     Fiber.yield
  65.     val = $temp_input_string
  66.     $temp_input_string = nil
  67.     return val
  68.   end
  69. end
  70.  
  71.  
  72. #==============================================================================
  73. # □ Scene_Name
  74. #==============================================================================
  75. class Scene_Name
  76.   #--------------------------------------------------------------------------
  77.   # ● 開始処理
  78.   #--------------------------------------------------------------------------
  79.   def start
  80.     super
  81.     @actor = @actor_id.is_a?(String) ? @actor_id : $game_actors[@actor_id]
  82.     @edit_window = Window_NameEdit.new(@actor, @max_char)
  83.     @command_window = Window_NameInput_Command.new(@edit_window, @@kanji_data ||= load_data("Data/Kanji_CHS.dat"))
  84.     @command_window.set_handler(:command_ok, method(:char_selection))
  85.     @command_window.set_handler(:ok, method(:on_input_ok))
  86.     @char_window = Window_NameInput_Char.new(@command_window, @edit_window)
  87.     @char_window.set_handler(:cancel, method(:command_selection))
  88.     command_selection
  89.   end
  90.   #--------------------------------------------------------------------------
  91.   # ● 入力[決定]
  92.   #--------------------------------------------------------------------------
  93.   def on_input_ok
  94.     if @actor.is_a?(String)
  95.       $temp_input_string = @edit_window.name
  96.     else
  97.       @actor.name = @edit_window.name
  98.     end
  99.     return_scene
  100.   end
  101.   #--------------------------------------------------------------------------
  102.   # ¤ コマンド選択開始
  103.   #--------------------------------------------------------------------------
  104.   def command_selection
  105.     @char_window.select(-1)
  106.     @char_window.deactivate
  107.     @command_window.activate
  108.   end
  109.   #--------------------------------------------------------------------------
  110.   # ¤ 文字選択開始
  111.   #--------------------------------------------------------------------------
  112.   def char_selection
  113.     @command_window.deactivate
  114.     @char_window.select(0)
  115.     @char_window.activate
  116.   end
  117. end
  118.  
  119.  
  120. #==============================================================================
  121. # □ Window_NameEdit
  122. #==============================================================================
  123. class Window_NameEdit
  124.   #--------------------------------------------------------------------------
  125.   # ● オブジェクト初期化
  126.   #--------------------------------------------------------------------------
  127.   def initialize(actor, max_char)
  128.     x = (Graphics.width - 360) / 2
  129.     y = (Graphics.height - (fitting_height(4) + fitting_height(9) + 8)) / 2
  130.     super(x, y, 360, fitting_height(4))
  131.     @max_char = max_char
  132.     if actor.is_a?(String)
  133.       @actor = nil
  134.       @default_name = @name = actor
  135.     else
  136.       @actor = actor
  137.       @default_name = @name = actor.name[0, @max_char]
  138.     end
  139.     @index = @name.size
  140.     deactivate
  141.     refresh
  142.   end
  143.   #--------------------------------------------------------------------------
  144.   # ● 名前を描画する左端の座標を取得
  145.   #--------------------------------------------------------------------------
  146.   def left
  147.     if @left == nil
  148.       if @actor
  149.         name_center = (contents_width + face_width) / 2
  150.         name_width = (@max_char + 1) * char_width
  151.         v1 = name_center - name_width / 2
  152.         v2 = contents_width - name_width
  153.         @left = v1 < v2 ? v1 : v2
  154.       else
  155.         @left = (contents_width - @max_char * char_width) / 2
  156.       end
  157.     end
  158.     return @left
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # ● 文字の幅を取得
  162.   #--------------------------------------------------------------------------
  163.   def char_width
  164.     return @char_width ||= text_size("あ").width
  165.   end
  166.   #--------------------------------------------------------------------------
  167.   # ○ クリア
  168.   #--------------------------------------------------------------------------
  169.   def clear
  170.     @name = ""
  171.     @index = 0
  172.     refresh
  173.   end
  174.   #--------------------------------------------------------------------------
  175.   # ● リフレッシュ
  176.   #--------------------------------------------------------------------------
  177.   def refresh
  178.     contents.clear
  179.     draw_actor_face(@actor, 0, 0) if @actor
  180.     @max_char.times {|i| draw_underline(i) }
  181.     @name.size.times {|i| draw_char(i) }
  182.     cursor_rect.set(item_rect(@index))
  183.   end
  184.   #--------------------------------------------------------------------------
  185.   # ● 文字を描画
  186.   #--------------------------------------------------------------------------
  187.   def draw_char(index)
  188.     rect = item_rect(index)
  189.     rect.x -= 1
  190.     rect.width += 4
  191.     change_color(normal_color)
  192.     draw_text(rect, @name[index] || "", 1)
  193.   end
  194. end
  195.  
  196.  
  197. #==============================================================================
  198. # ■ Window_NameInput_Command
  199. #------------------------------------------------------------------------------
  200. # 文字ページを入れ替えるコマンドウィンドウ。
  201. #==============================================================================
  202. class Window_NameInput_Command < Window_Command
  203.   #--------------------------------------------------------------------------
  204.   # ¤ クラス定数
  205.   #--------------------------------------------------------------------------
  206.   LATIN1 = [ 'A','B','C','D','E',  'a','b','c','d','e',
  207.              'F','G','H','I','J',  'f','g','h','i','j',
  208.              'K','L','M','N','O',  'k','l','m','n','o',
  209.              'P','Q','R','S','T',  'p','q','r','s','t',
  210.              'U','V','W','X','Y',  'u','v','w','x','y',
  211.              'Z','[',']','^','_',  'z','{','}','|','~',
  212.              '0','1','2','3','4',  '!','#','$','%','&',
  213.              '5','6','7','8','9',  '(',')','*','+','-',
  214.              '/','=','@','<','>',  ':',';',' ']
  215.   LATIN2 = [ 'Α','Β','Γ','Δ','Ε',  'α','β','γ','δ','ε',
  216.              'Ζ','Η','Θ','Ι','Κ',  'ζ','η','θ','ι','κ',
  217.              'Λ','Μ','Ν','Ξ','Ο',  'λ','μ','ν','ξ','ο',
  218.              'Π','Ρ','Σ','Τ','Υ',  'π','ρ','σ','τ','υ',
  219.              'Φ','Χ','Ψ','Ω','℩',  'φ','χ','ψ','ω','ς',
  220.              'А','Б','В','Г','Д',  'а','б','в','г','д',
  221.              'Е','Ё','Ж','З','И',  'е','ё','ж','з','и',
  222.              'Й','К','Л','М','Н',  'й','к','л','м','н',
  223.              'О','П','Р','С','Т',  'о','п','р','с','т',
  224.              'У','Ф','Х','Ц','Ч',  'у','ф','х','ц','ч',
  225.              'Ш','Щ','Ъ','Ы','Ь',  'ш','щ','ъ','ы','ь',
  226.              'Э','Ю','Я',' ',' ',  'э','ю','я',' ',' ']
  227.   LATIN3 = [ 'Á','É','Í','Ó','Ú',  'á','é','í','ó','ú',
  228.              'À','È','Ì','Ò','Ù',  'à','è','ì','ò','ù',
  229.              'Â','Ê','Î','Ô','Û',  'â','ê','î','ô','û',
  230.              'Ä','Ë','Ï','Ö','Ü',  'ä','ë','ï','ö','ü',
  231.              'Ā','Ē','Ī','Ō','Ū',  'ā','ē','ī','ō','ū',
  232.              'Ã','Å','Æ','Ç','Ð',  'ã','å','æ','ç','ð',
  233.              'Ñ','Õ','Ø','Š','Ŵ',  'ñ','õ','ø','š','ŵ',
  234.              'Ý','Ŷ','Ÿ','Ž','Þ',  'ý','ÿ','ŷ','ž','þ',
  235.              'IJ','Œ','ij','œ','∀',  '«','»',' ']
  236.   SPCHAR  = [ '·','~','—','☆','★' ]
  237.   #--------------------------------------------------------------------------
  238.   # ¤ インスタンス変数
  239.   #--------------------------------------------------------------------------
  240.   attr_accessor :char_window
  241.   attr_accessor :edit_window
  242.   attr_accessor :kanji_mode
  243.   #--------------------------------------------------------------------------
  244.   # ▲ オブジェクト初期化
  245.   #--------------------------------------------------------------------------
  246.   def initialize(edit_window, kanji_data)
  247.     @kanji_data = kanji_data
  248.     @edit_window = edit_window
  249.     super(edit_window.x - 60, edit_window.y + edit_window.height + 8)
  250.     @index = 0
  251.     refresh
  252.     update_cursor
  253.   end
  254.   #--------------------------------------------------------------------------
  255.   # △ インデックスセッター
  256.   #--------------------------------------------------------------------------
  257.   alias index_setter@Window_NameInput_Command index=
  258.   def index=(value)
  259.     last_index = @index
  260.     index_setter@Window_NameInput_Command(value)
  261.     if @char_window && @index != last_index
  262.       @char_window.char_list = current_ext
  263.     end
  264.   end
  265.   #--------------------------------------------------------------------------
  266.   # ▲ ウィンドウ幅の取得
  267.   #--------------------------------------------------------------------------
  268.   def window_width
  269.     return 120
  270.   end
  271.   #--------------------------------------------------------------------------
  272.   # ▲ ウィンドウ高さの取得
  273.   #--------------------------------------------------------------------------
  274.   def window_height
  275.     return fitting_height(9)
  276.   end
  277.   #--------------------------------------------------------------------------
  278.   # ¤ コマンドリストの作成
  279.   #--------------------------------------------------------------------------
  280.   def make_command_list
  281.     if !@kanji_mode
  282.       add_command(AephiexString::NMI_ABC123, :command_ok, true, LATIN1)
  283.       add_command(AephiexString::NMI_GrRu, :command_ok, true, LATIN2) if AephiexConfig::GREEK_RUSSIAN
  284.       add_command(AephiexString::NMI_LatinSp, :command_ok, true, LATIN3) if AephiexConfig::LATIN_SPECIAL
  285.       add_command(AephiexString::NMI_SPCHAR, :command_ok, true, SPCHAR)
  286.       add_command(AephiexString::NMI_Kanji, :kanji_mode, true)
  287.       add_command(AephiexString::NMI_Back, :backspace, true)
  288.       add_command(AephiexString::NMI_Clear, :clear, true)
  289.       add_command(AephiexString::NMI_Reset, :reset, true)
  290.       add_command(AephiexString::NMI_OK, :name_ok, true)
  291.     else
  292.       @kanji_data.keys.each do |key|
  293.         add_command(key.to_s, :command_ok, true, @kanji_data[key])
  294.       end
  295.     end
  296.   end
  297.   #--------------------------------------------------------------------------
  298.   # ▲ 表示行数の取得
  299.   #--------------------------------------------------------------------------
  300.   def visible_line_number
  301.     return 9
  302.   end
  303.   #--------------------------------------------------------------------------
  304.   # ▲ 項目の描画
  305.   #--------------------------------------------------------------------------
  306.   def draw_item(index)
  307.     change_color((@list[index][:ext] ? normal_color : system_color))
  308.     draw_text(item_rect_for_text(index), command_name(index), 1)
  309.   end
  310.   #--------------------------------------------------------------------------
  311.   # ▲ 決定やキャンセルなどのハンドリング処理
  312.   #--------------------------------------------------------------------------
  313.   def process_handling
  314.     return unless open? && active
  315.     return process_back     if Input.trigger?(:A)
  316.     return process_ok       if Input.trigger?(:C)
  317.     return process_cancel   if Input.trigger?(:B)
  318.   end
  319.   #--------------------------------------------------------------------------
  320.   # ¤ 一つ戻す
  321.   #--------------------------------------------------------------------------
  322.   def process_back
  323.     if @edit_window.back
  324.       Sound.play_cancel
  325.     else
  326.       Sound.play_buzzer
  327.     end
  328.   end
  329.   #--------------------------------------------------------------------------
  330.   # ▲ 決定ボタンが押されたときの処理
  331.   #--------------------------------------------------------------------------
  332.   def process_ok
  333.     case current_symbol
  334.     when :kanji_mode
  335.       Sound.play_ok
  336.       @kanji_mode = true
  337.       refresh
  338.       self.top_row = 0
  339.       self.index = 0
  340.     when :backspace
  341.       process_back
  342.     when :clear
  343.       @edit_window.clear
  344.       Sound.play_ok
  345.     when :reset
  346.       @edit_window.restore_default
  347.       Sound.play_ok
  348.     when :name_ok
  349.       if @edit_window.name.empty?
  350.         if @edit_window.restore_default
  351.           Sound.play_ok
  352.         else
  353.           Sound.play_buzzer
  354.         end
  355.       else
  356.         Sound.play_ok
  357.         call_ok_handler
  358.       end
  359.     when :command_ok
  360.       super
  361.     end
  362.   end
  363.   #--------------------------------------------------------------------------
  364.   # ▲ キャンセルボタンが押されたときの処理
  365.   #--------------------------------------------------------------------------
  366.   def process_cancel
  367.     if @kanji_mode
  368.       Sound.play_cancel
  369.       @kanji_mode = nil
  370.       refresh
  371.       self.top_row = 0
  372.       self.select_symbol(:kanji_mode)
  373.     end
  374.   end
  375. end
  376.  
  377.  
  378. #==============================================================================
  379. # ■ Window_NameInput_Char
  380. #------------------------------------------------------------------------------
  381. # 文字を選択し入力するウィンドウ。
  382. #==============================================================================
  383. class Window_NameInput_Char < Window_Command
  384.   #--------------------------------------------------------------------------
  385.   # ¤ インスタンス変数
  386.   #--------------------------------------------------------------------------
  387.   attr_accessor :command_window
  388.   attr_accessor :edit_window
  389.   attr_accessor :char_list
  390.   #--------------------------------------------------------------------------
  391.   # ▲ オブジェクト初期化
  392.   #--------------------------------------------------------------------------
  393.   def initialize(command_window, edit_window)
  394.     @command_window = command_window
  395.     command_window.char_window = self
  396.     @edit_window = edit_window
  397.     super(edit_window.x + 60, edit_window.y + edit_window.height + 8)
  398.     self.char_list = command_window.current_ext
  399.     update_cursor
  400.     deactivate
  401.   end
  402.   #--------------------------------------------------------------------------
  403.   # ¤ チャーリストセッター
  404.   #--------------------------------------------------------------------------
  405.   def char_list=(value)
  406.     @char_list = value
  407.     self.top_row = 0
  408.     self.index = -1
  409.     refresh
  410.   end
  411.   #--------------------------------------------------------------------------
  412.   # ▲ ウィンドウ幅の取得
  413.   #--------------------------------------------------------------------------
  414.   def window_width
  415.     return @edit_window.width
  416.   end
  417.   #--------------------------------------------------------------------------
  418.   # ▲ 表示行数の取得
  419.   #--------------------------------------------------------------------------
  420.   def visible_line_number
  421.     return 9
  422.   end
  423.   #--------------------------------------------------------------------------
  424.   # ▲ 桁数の取得
  425.   #--------------------------------------------------------------------------
  426.   def col_max
  427.     return 10
  428.   end
  429.   #--------------------------------------------------------------------------
  430.   # ▲ アライメントの取得
  431.   #--------------------------------------------------------------------------
  432.   def alignment
  433.     return 1
  434.   end
  435.   #--------------------------------------------------------------------------
  436.   # ▲ コマンドリストの作成
  437.   #--------------------------------------------------------------------------
  438.   def make_command_list
  439.     if @char_list && @char_list.size > 0
  440.       @char_list.each {|c| add_command(c, :on_char_ok) }
  441.     end
  442.   end
  443.   #--------------------------------------------------------------------------
  444.   # ▲ 決定やキャンセルなどのハンドリング処理
  445.   #--------------------------------------------------------------------------
  446.   def process_handling
  447.     return unless open? && active
  448.     return process_back     if Input.trigger?(:A)
  449.     return process_ok       if Input.trigger?(:C)
  450.     return process_cancel   if Input.trigger?(:B)
  451.   end
  452.   #--------------------------------------------------------------------------
  453.   # ¤ 一つ戻す
  454.   #--------------------------------------------------------------------------
  455.   def process_back
  456.     if @edit_window.back
  457.       Sound.play_cancel
  458.     else
  459.       Sound.play_buzzer
  460.     end
  461.   end
  462.   #--------------------------------------------------------------------------
  463.   # ▲ 決定ボタンが押されたときの処理
  464.   #--------------------------------------------------------------------------
  465.   def process_ok
  466.     if current_data
  467.       if @edit_window.add(current_data[:name])
  468.         Sound.play_ok
  469.       else
  470.         Sound.play_buzzer
  471.       end
  472.     end
  473.   end
  474.   #--------------------------------------------------------------------------
  475.   # ▲ 項目を描画する矩形の取得
  476.   #--------------------------------------------------------------------------
  477.   def item_rect(index)
  478.     if index >= 0
  479.       rect = Rect.new
  480.       rect.x = index % 10 * 32 + index % 10 / 5 * 16
  481.       rect.y = index / 10 * line_height
  482.       rect.width = 32
  483.       rect.height = line_height
  484.       return rect
  485.     else
  486.       return Rect.new(0, 0, 0, 0)
  487.     end
  488.   end
  489. end


汉字档案库文件
务必解压后放在工程的 Data/ 目录下,否则此脚本将找不到汉字库。
Kanji_CHS.rar (21.84 KB, 下载次数: 275)

评分

参与人数 7+7 收起 理由
昀然见南山 + 1 精品文章
jerat1009 + 1 我很赞同
小小西 + 1 塞糖
空调虎 + 1 精品文章
hyrious + 1 塞糖
泠鸰 + 1 精品文章
Nil2018 + 1 精品文章

查看全部评分

Lv2.观梦者

梦石
0
星屑
664
在线时间
101 小时
注册时间
2016-7-15
帖子
143
2
发表于 2019-6-8 21:18:26 | 只看该作者
支持一波
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
45
在线时间
2 小时
注册时间
2019-6-8
帖子
4
3
发表于 2019-6-9 03:09:33 | 只看该作者
已经借用,支持楼主!
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
0
在线时间
5 小时
注册时间
2019-6-11
帖子
23
4
发表于 2019-6-11 16:10:35 | 只看该作者
膜拜,蟹蟹大佬
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
121
在线时间
28 小时
注册时间
2019-5-17
帖子
7
5
发表于 2019-6-21 18:59:17 | 只看该作者
膜拜大佬
回复 支持 反对

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
181
在线时间
35 小时
注册时间
2018-2-17
帖子
2
6
发表于 2019-8-19 13:24:45 | 只看该作者
支持一下VA的汉字输入
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
1854
在线时间
395 小时
注册时间
2014-7-7
帖子
264

开拓者

7
发表于 2019-8-27 00:32:26 | 只看该作者
震惊!p1惊现来打厨
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
4946
在线时间
987 小时
注册时间
2017-12-30
帖子
51
8
发表于 2019-8-27 05:23:53 | 只看该作者
感谢分享!
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
262
在线时间
49 小时
注册时间
2019-1-12
帖子
48
9
发表于 2020-2-5 13:11:38 | 只看该作者
谢谢!非常有帮助!!!
回复 支持 反对

使用道具 举报

Lv2.观梦者

梦石
0
星屑
814
在线时间
122 小时
注册时间
2018-7-23
帖子
114
10
发表于 2021-4-2 12:53:23 | 只看该作者
我觉得这个脚本在搞我。我这个103的VA似乎用不了这个脚本的样子是。
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

GMT+8, 2024-4-19 12:26

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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