Advertisement
eshrasic

Untitled

Aug 10th, 2015
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 38.24 KB | None | 0 0
  1. #==============================================================================
  2. # Hotkeys v2.0
  3. # Author Eshra
  4. # First Version Finished 10 Aug 2015
  5. #------------------------------------------------------------------------------
  6. #
  7. # Usage:
  8. #
  9. # Requires Era Module v0.2 available at
  10. #   https://rascripts.wordpress.com/2015/08/10/era-module-v0-2/
  11. #
  12. # Install by copying and pasting this script under the "Materials" Section of the
  13. # Script Editor below 'Era Module'.
  14. #
  15. # Adds an option for setting hotkeys to the main menu. Hotkeys allow items and
  16. # skills to be used while on the map. The hotkeys you've set are displayed on
  17. # an action bar. The action bar can be hidden from the new "options" menu.
  18. #
  19. # Important notes:
  20. # Items that are set to hotkeys must NOT be set as 'only usable in battle' if
  21. # you intend to use a hotkey with that item.
  22. #
  23. # The required extended Input module (by by Cidiomar R Dias Jr) is included
  24. # below. No additional setup is necessary.
  25. #------------------------------------------------------------------------------
  26. #==============================================================================
  27.  
  28. #------------------------------------------------------------------------------
  29. # ** Input
  30. #------------------------------------------------------------------------------
  31. # Created By Cidiomar R. Dias JĂșnior
  32. # Originally posted at http://rgss3help.cidiomar.com/?p=145
  33. #
  34. # Terms of Use: http://rgss3help.cidiomar.com/?page_id=159
  35. #------------------------------------------------------------------------------
  36. # A module that handles input data from a gamepad or keyboard.
  37. # Managed by symbols rather than button numbers in RGSS3.
  38. #==============================================================================
  39. module Input  
  40.   #--------------------------------------------------------------------------
  41.   # * Keymap in symbols
  42.   # To get a key from the keymap, you can use Input.key(sym) or Input:KEYMAP[sym]
  43.   # Or if you want to use a symbol in a input function, just pass the symbol
  44.   # as argument.
  45.   #--------------------------------------------------------------------------
  46.   KEYMAP = {
  47.     LBUTTON:              0x01,  RBUTTON:              0x02,
  48.     CANCEL:               0x03,  MBUTTON:              0x04,
  49.     XBUTTON1:             0x05,  XBUTTON2:             0x06,
  50.     BACK:                 0x08,  TAB:                  0x09,
  51.     CLEAR:                0x0c,  RETURN:               0x0d,
  52.     SHIFT:                0x10,  CONTROL:              0x11,
  53.     MENU:                 0x12,  PAUSE:                0x13,
  54.     CAPITAL:              0x14,  KANA:                 0x15,
  55.     KANA:                 0x15,  KANA:                 0x15,
  56.     JUNJA:                0x17,  FINAL:                0x18,
  57.     HANJA:                0x19,  HANJA:                0x19,
  58.     ESCAPE:               0x1b,  CONVERT:              0x1c,
  59.     NONCONVERT:           0x1d,  ACCEPT:               0x1e,
  60.     MODECHANGE:           0x1f,  SPACE:                0x20,
  61.     PRIOR:                0x21,  NEXT:                 0x22,
  62.     END:                  0x23,  HOME:                 0x24,
  63.     LEFT:                 0x25,  UP:                   0x26,
  64.     RIGHT:                0x27,  DOWN:                 0x28,
  65.     SELECT:               0x29,  PRINT:                0x2a,
  66.     EXECUTE:              0x2b,  SNAPSHOT:             0x2c,
  67.     INSERT:               0x2d,  DELETE:               0x2e,
  68.     HELP:                 0x2f,  N0:                   0x30,
  69.     KEY_1:                0x31,  KEY_2:                0x32,
  70.     KEY_3:                0x33,  KEY_4:                0x34,
  71.     KEY_5:                0x35,  KEY_6:                0x36,
  72.     KEY_7:                0x37,  KEY_8:                0x38,
  73.     KEY_9:                0x39,  colon:                0x3a,
  74.     semicolon:            0x3b,  less:                 0x3c,
  75.     equal:                0x3d,  greater:              0x3e,
  76.     question:             0x3f,  at:                   0x40,
  77.     LETTER_A:             0x41,  LETTER_B:             0x42,
  78.     LETTER_C:             0x43,  LETTER_D:             0x44,
  79.     LETTER_E:             0x45,  LETTER_F:             0x46,
  80.     LETTER_G:             0x47,  LETTER_H:             0x48,
  81.     LETTER_I:             0x49,  LETTER_J:             0x4a,
  82.     LETTER_K:             0x4b,  LETTER_L:             0x4c,
  83.     LETTER_M:             0x4d,  LETTER_N:             0x4e,
  84.     LETTER_O:             0x4f,  LETTER_P:             0x50,
  85.     LETTER_Q:             0x51,  LETTER_R:             0x52,
  86.     LETTER_S:             0x53,  LETTER_T:             0x54,
  87.     LETTER_U:             0x55,  LETTER_V:             0x56,
  88.     LETTER_W:             0x57,  LETTER_X:             0x58,
  89.     LETTER_Y:             0x59,  LETTER_Z:             0x5a,
  90.     LWIN:                 0x5b,  RWIN:                 0x5c,
  91.     APPS:                 0x5d,  asciicircum:          0x5e,
  92.     SLEEP:                0x5f,  NUMPAD0:              0x60,
  93.     NUMPAD1:              0x61,  NUMPAD2:              0x62,
  94.     NUMPAD3:              0x63,  NUMPAD4:              0x64,
  95.     NUMPAD5:              0x65,  NUMPAD6:              0x66,
  96.     NUMPAD7:              0x67,  NUMPAD8:              0x68,
  97.     NUMPAD9:              0x69,  MULTIPLY:             0x6a,
  98.     ADD:                  0x6b,  SEPARATOR:            0x6c,
  99.     SUBTRACT:             0x6d,  DECIMAL:              0x6e,
  100.     DIVIDE:               0x6f,  F1:                   0x70,
  101.     F2:                   0x71,  F3:                   0x72,
  102.     F4:                   0x73,  F5:                   0x74,
  103.     F6:                   0x75,  F7:                   0x76,
  104.     F8:                   0x77,  F9:                   0x78,
  105.     F10:                  0x79,  F11:                  0x7a,
  106.     F12:                  0x7b,  F13:                  0x7c,
  107.     F14:                  0x7d,  F15:                  0x7e,
  108.     F16:                  0x7f,  F17:                  0x80,
  109.     F18:                  0x81,  F19:                  0x82,
  110.     F20:                  0x83,  F21:                  0x84,
  111.     F22:                  0x85,  F23:                  0x86,
  112.     F24:                  0x87,  NUMLOCK:              0x90,
  113.     SCROLL:               0x91,  LSHIFT:               0xa0,
  114.     RSHIFT:               0xa1,  LCONTROL:             0xa2,
  115.     RCONTROL:             0xa3,  LMENU:                0xa4,
  116.     RMENU:                0xa5,  BROWSER_BACK:         0xa6,
  117.     BROWSER_FORWARD:      0xa7,  BROWSER_REFRESH:      0xa8,
  118.     BROWSER_STOP:         0xa9,  BROWSER_SEARCH:       0xaa,
  119.     BROWSER_FAVORITES:    0xab,  BROWSER_HOME:         0xac,
  120.     VOLUME_MUTE:          0xad,  VOLUME_DOWN:          0xae,
  121.     VOLUME_UP:            0xaf,  MEDIA_NEXT_TRACK:     0xb0,
  122.     MEDIA_PREV_TRACK:     0xb1,  MEDIA_STOP:           0xb2,
  123.     MEDIA_PLAY_PAUSE:     0xb3,  LAUNCH_MAIL:          0xb4,
  124.     LAUNCH_MEDIA_SELECT:  0xb5,  LAUNCH_APP1:          0xb6,
  125.     LAUNCH_APP2:          0xb7,  cedilla:              0xb8,
  126.     onesuperior:          0xb9,  masculine:            0xba,
  127.     guillemotright:       0xbb,  onequarter:           0xbc,
  128.     onehalf:              0xbd,  threequarters:        0xbe,
  129.     questiondown:         0xbf,  Agrave:               0xc0,
  130.     Aacute:               0xc1,  Acircumflex:          0xc2,
  131.     Atilde:               0xc3,  Adiaeresis:           0xc4,
  132.     Aring:                0xc5,  AE:                   0xc6,
  133.     Ccedilla:             0xc7,  Egrave:               0xc8,
  134.     Eacute:               0xc9,  Ecircumflex:          0xca,
  135.     Ediaeresis:           0xcb,  Igrave:               0xcc,
  136.     Iacute:               0xcd,  Icircumflex:          0xce,
  137.     Idiaeresis:           0xcf,  ETH:                  0xd0,
  138.     Ntilde:               0xd1,  Ograve:               0xd2,
  139.     Oacute:               0xd3,  Ocircumflex:          0xd4,
  140.     Otilde:               0xd5,  Odiaeresis:           0xd6,
  141.     multiply:             0xd7,  Oslash:               0xd8,
  142.     Ugrave:               0xd9,  Uacute:               0xda,
  143.     Ucircumflex:          0xdb,  Udiaeresis:           0xdc,
  144.     Yacute:               0xdd,  THORN:                0xde,
  145.     ssharp:               0xdf,  agrave:               0xe0,
  146.     aacute:               0xe1,  acircumflex:          0xe2,
  147.     atilde:               0xe3,  adiaeresis:           0xe4,
  148.     PROCESSKEY:           0xe5,  ae:                   0xe6,
  149.     PACKET:               0xe7,  egrave:               0xe8,
  150.     eacute:               0xe9,  ecircumflex:          0xea,
  151.     ediaeresis:           0xeb,  igrave:               0xec,
  152.     iacute:               0xed,  icircumflex:          0xee,
  153.     idiaeresis:           0xef,  eth:                  0xf0,
  154.     ntilde:               0xf1,  ograve:               0xf2,
  155.     oacute:               0xf3,  ocircumflex:          0xf4,
  156.     otilde:               0xf5,  ATTN:                 0xf6,
  157.     CRSEL:                0xf7,  EXSEL:                0xf8,
  158.     EREOF:                0xf9,  PLAY:                 0xfa,
  159.     ZOOM:                 0xfb,  NONAME:               0xfc,
  160.     PA1:                  0xfd,  thorn:                0xfe,
  161.     ydiaeresis:           0xff
  162.   }
  163.   KEYMAP[:WIN]  = [KEYMAP[:LWIN], KEYMAP[:RWIN]]
  164.  
  165.   #--------------------------------------------------------------------------
  166.   # * Default Keys, you can configure here instead of by pressing F1.
  167.   #--------------------------------------------------------------------------
  168.   #UP    = [KEYMAP[:UP]]
  169.   #DOWN  = [KEYMAP[:DOWN]]
  170.   #LEFT  = [KEYMAP[:LEFT]]
  171.   #RIGHT = [KEYMAP[:RIGHT]]
  172.   #A     = [KEYMAP[:SHIFT]]
  173.   #B     = [KEYMAP[:LETTER_X]]
  174.   #C     = [KEYMAP[:LETTER_Z]]
  175.   #X     = [KEYMAP[:LETTER_B]]
  176.   #Y     = []
  177.   #Z     = []
  178.   #L     = [KEYMAP[:PRIOR]]
  179.   #R     = [KEYMAP[:NEXT]]
  180.   #F5    = [KEYMAP[:F5]]
  181.   #F6    = [KEYMAP[:F6]]
  182.   #F7    = [KEYMAP[:F7]]
  183.   #F8    = [KEYMAP[:F8]]
  184.   #F9    = [KEYMAP[:F9]]
  185.   #SHIFT = [KEYMAP[:SHIFT]]
  186.   #CTRL  = [KEYMAP[:CONTROL]]
  187.   #ALT   = [KEYMAP[:MENU]]
  188.   #I     = [KEYMAP[:LETTER_I]]
  189.   #E     = [KEYMAP[:LETTER_E]]
  190.   #S     = [KEYMAP[:LETTER_S]]
  191.   #T     = [KEYMAP[:LETTER_T]]
  192.   UP    = [KEYMAP[:UP]]
  193.   DOWN  = [KEYMAP[:DOWN]]
  194.   LEFT  = [KEYMAP[:LEFT]]
  195.   RIGHT = [KEYMAP[:RIGHT]]
  196.   A     = [KEYMAP[:SHIFT]]
  197.   B     = [KEYMAP[:LETTER_X]]
  198.   C     = [KEYMAP[:LETTER_Z]]
  199.   X     = [KEYMAP[:LETTER_B]]
  200.   Y     = []
  201.   Z     = []
  202.   L     = [KEYMAP[:PRIOR]]
  203.   R     = [KEYMAP[:NEXT]]
  204.   F5    = [KEYMAP[:F5]]
  205.   F6    = [KEYMAP[:F6]]
  206.   F7    = [KEYMAP[:F7]]
  207.   F8    = [KEYMAP[:F8]]
  208.   F9    = [KEYMAP[:F9]]
  209.   SHIFT = [KEYMAP[:SHIFT]]
  210.   CTRL  = [KEYMAP[:CONTROL]]
  211.   ALT   = [KEYMAP[:MENU]]
  212.   I     = [KEYMAP[:LETTER_I]]
  213.   E     = [KEYMAP[:LETTER_E]]
  214.   S     = [KEYMAP[:LETTER_S]]
  215.   T     = [KEYMAP[:LETTER_T]]
  216.  
  217.   #--------------------------------------------------------------------------
  218.   # * Symbol version of default keys.
  219.   #--------------------------------------------------------------------------
  220.   SYM_KEYS = {
  221.     :UP     => UP,
  222.     :LEFT   => LEFT,
  223.     :DOWN   => DOWN,
  224.     :RIGHT  => RIGHT,
  225.     :A      => A,
  226.     :B      => B,
  227.     :C      => C,
  228.     :X      => X,
  229.     :Y      => Y,
  230.     :Z      => Z,
  231.     :L      => L,
  232.     :R      => R,
  233.     :F5     => F5,
  234.     :F6     => F6,
  235.     :F7     => F7,
  236.     :F8     => F8,
  237.     :F9     => F9,
  238.     :SHIFT  => SHIFT,
  239.     :CTRL   => CTRL,
  240.     :ALT    => ALT,
  241.     :I      => I,
  242.     :E      => E,
  243.     :S      => S,
  244.     :T      => T
  245.   }
  246.   #--------------------------------------------------------------------------
  247.   # * Key Codes Used in Events Conditional Branchs
  248.   #--------------------------------------------------------------------------
  249.   EventsKeyCodes = {
  250.      2 => :DOWN,
  251.      4 => :LEFT,
  252.      6 => :RIGHT,
  253.      8 => :UP,
  254.     11 => :A,
  255.     12 => :B,
  256.     13 => :C,
  257.     14 => :X,
  258.     15 => :Y,
  259.     16 => :Z,
  260.     17 => :L,
  261.     18 => :R
  262.   }
  263.   #--------------------------------------------------------------------------
  264.   # * Internal APIs to handle keyboard functions
  265.   #--------------------------------------------------------------------------
  266.   GetKeyboardState  = Win32API.new('user32.dll', 'GetKeyboardState',  'I', 'I')
  267.   MapVirtualKeyEx   = Win32API.new('user32.dll', 'MapVirtualKeyEx', 'IIL', 'I')
  268.   ToUnicodeEx       = Win32API.new('user32.dll', 'ToUnicodeEx', 'LLPPILL', 'L')
  269.   #----------
  270.   @language_layout  = Win32API.new('user32.dll', 'GetKeyboardLayout', 'L', 'L').call(0)
  271.   DOWN_STATE_MASK   = (0x8 << 0x04)
  272.   DEAD_KEY_MASK     = (0x8 << 0x1C)
  273.   #-----
  274.   UNICODE_TO_UTF8   = Encoding::Converter.new(Encoding.list[2], Encoding.list[1])
  275.  
  276.   #--------------------------------------------------------------------------
  277.   # * States of keys
  278.   #--------------------------------------------------------------------------
  279.   @state            = DL::CPtr.new(DL.malloc(256), 256)
  280.   @triggered        = Array.new(256, false)
  281.   @pressed          = Array.new(256, false)
  282.   @released         = Array.new(256, false)
  283.   @repeated         = Array.new(256, 0)
  284.  
  285.   #--------------------------------------------------------------------------
  286.   # * Singleton attrs of states
  287.   #--------------------------------------------------------------------------
  288.   class << self
  289.     attr_reader :triggered, :pressed, :released, :repeated, :state
  290.   end
  291.  
  292.   #--------------------------------------------------------------------------
  293.   # * Get a key code by simbol
  294.   #--------------------------------------------------------------------------
  295.   def self.key(sym)
  296.     KEYMAP[sym] || 0
  297.   end
  298.  
  299.   #--------------------------------------------------------------------------
  300.   # * Updates input data.
  301.   # As a general rule, this method is called once per frame.
  302.   #--------------------------------------------------------------------------
  303.   def self.update
  304.     GetKeyboardState.call(@state.to_i)
  305.     #-----
  306.     0.upto(255) do |key|
  307.       if @state[key] & DOWN_STATE_MASK == DOWN_STATE_MASK
  308.         @released[key] = false
  309.         @pressed[key]  = true if (@triggered[key] = !@pressed[key])
  310.         @repeated[key] < 17 ? @repeated[key] += 1 : @repeated[key] = 15
  311.       elsif !@released[key] and @pressed[key]
  312.         @triggered[key] = false
  313.         @pressed[key]   = false
  314.         @repeated[key]  = 0
  315.         @released[key]  = true
  316.       else
  317.         @released[key]  = false
  318.       end
  319.     end
  320.   end
  321.  
  322.   #--------------------------------------------------------------------------
  323.   # * Checks the status of the directional buttons, translates the data into
  324.   # a specialized 4-direction input format, and returns the number pad
  325.   # equivalent (2, 4, 6, 8).
  326.   #
  327.   # If no directional buttons are being pressed (or the equivalent), returns 0.
  328.   #--------------------------------------------------------------------------
  329.   def self.dir4
  330.     return 2 if self.press?(DOWN)
  331.     return 4 if self.press?(LEFT)
  332.     return 6 if self.press?(RIGHT)
  333.     return 8 if self.press?(UP)
  334.     return 0
  335.   end
  336.  
  337.   #--------------------------------------------------------------------------
  338.   # * Checks the status of the directional buttons, translates the data into
  339.   # a specialized 8-direction input format, and returns the number pad
  340.   # equivalent (1, 2, 3, 4, 6, 7, 8, 9).
  341.   #
  342.   #If no directional buttons are being pressed (or the equivalent), returns 0.
  343.   #--------------------------------------------------------------------------
  344.   def self.dir8
  345.     down = self.press?(DOWN)
  346.     left = self.press?(LEFT)
  347.     return 1 if down and left
  348.     right = self.press?(RIGHT)
  349.     return 3 if down and right
  350.     up = self.press?(UP)
  351.     return 7 if up and left
  352.     return 9 if up and right
  353.     return 2 if down
  354.     return 4 if left
  355.     return 6 if right
  356.     return 8 if up
  357.     return 0
  358.   end
  359.  
  360.   #--------------------------------------------------------------------------
  361.   # * Determines whether the button corresponding to the symbol sym is
  362.   # currently being pressed.
  363.   #
  364.   # If the button is being pressed, returns TRUE. If not, returns FALSE.
  365.   #
  366.   #   if Input.press?(:C)
  367.   #     do_something
  368.   #   end
  369.   #--------------------------------------------------------------------------
  370.   def self.press?(keys)
  371.     if keys.is_a?(Numeric)
  372.       k = keys.to_i
  373.       return (@pressed[k] and !@triggered[k])
  374.     elsif keys.is_a?(Array)
  375.       return keys.any? {|key| self.press?(key) }
  376.     elsif keys.is_a?(Symbol)
  377.       if SYM_KEYS.key?(keys)
  378.         return SYM_KEYS[keys].any? {|key| (@pressed[key]  and !@triggered[key]) }
  379.       elsif (KEYMAP.key?(keys))
  380.         k = KEYMAP[keys]
  381.         return (@pressed[k] and !@triggered[k])
  382.       else
  383.         return false
  384.       end
  385.     end
  386.   end
  387.  
  388.   #--------------------------------------------------------------------------
  389.   # * Determines whether the button corresponding to the symbol sym is
  390.   # currently being pressed again.
  391.   # "Pressed again" is seen as time having passed between the button being
  392.   # not pressed and being pressed.
  393.   #
  394.   # If the button is being pressed, returns TRUE. If not, returns FALSE.
  395.   #--------------------------------------------------------------------------
  396.   def self.trigger?(keys)
  397.     if keys.is_a?(Numeric)
  398.       return @triggered[keys.to_i]
  399.     elsif keys.is_a?(Array)
  400.       return keys.any? {|key| @triggered[key]}
  401.     elsif keys.is_a?(Symbol)
  402.       if SYM_KEYS.key?(keys)
  403.         return SYM_KEYS[keys].any? {|key| @triggered[key]}
  404.       elsif KEYMAP.key?(keys)
  405.         return @triggered[KEYMAP[keys]]
  406.       else
  407.         return false
  408.       end
  409.     end
  410.   end
  411.  
  412.   #--------------------------------------------------------------------------
  413.   # * Determines whether the button corresponding to the symbol sym is
  414.   # currently being pressed again.
  415.   # Unlike trigger?, takes into account the repeated input of a button being
  416.   # held down continuously.
  417.   #
  418.   # If the button is being pressed, returns TRUE. If not, returns FALSE.
  419.   #--------------------------------------------------------------------------
  420.   def self.repeat?(keys)
  421.     if keys.is_a?(Numeric)
  422.       key = keys.to_i
  423.       return @repeated[key] == 1 || @repeated[key] == 16
  424.     elsif keys.is_a?(Array)
  425.       return keys.any? {|key| @repeated[key] == 1 || @repeated[key] == 16}
  426.     elsif keys.is_a?(Symbol)
  427.       if SYM_KEYS.key?(keys)
  428.         return SYM_KEYS[keys].any? {|key| @repeated[key] == 1 || @repeated[key] == 16}
  429.       elsif KEYMAP.key?(keys)
  430.         return @repeated[KEYMAP[keys]] == 1 || @repeated[KEYMAP[keys]] == 16
  431.       else
  432.         return false
  433.       end
  434.     end
  435.   end
  436.  
  437.   #--------------------------------------------------------------------------
  438.   # * Determines whether the button corresponding to the symbol sym
  439.   # was released.
  440.   #
  441.   # If the button was released, returns TRUE. If not, returns FALSE.
  442.   #--------------------------------------------------------------------------
  443.   def self.release?(keys)
  444.     if keys.is_a?(Numeric)
  445.       return @released[keys.to_i]
  446.     elsif keys.is_a?(Array)
  447.       return keys.any? {|key| @released[key]}
  448.     elsif keys.is_a?(Symbol)
  449.       if SYM_KEYS.key?(keys)
  450.         return SYM_KEYS[keys].any? {|key| @released[key]}
  451.       elsif KEYMAP.key?(keys)
  452.         return @released[KEYMAP[keys]]
  453.       else
  454.         return false
  455.       end
  456.     end
  457.   end
  458.  
  459.   #--------------------------------------------------------------------------
  460.   # * Gets the character that correspond to vk using the keyboard layout
  461.   #--------------------------------------------------------------------------
  462.   def self.get_character(vk)
  463.     c = MapVirtualKeyEx.call(vk, 2, @language_layout)
  464.     return '' if c < 32 && (c & DEAD_KEY_MASK != DEAD_KEY_MASK)
  465.     #-----
  466.     result = "\0" * 2
  467.     length = ToUnicodeEx.call(vk, MapVirtualKeyEx.call(vk, 0, @language_layout),
  468.                               @state, result, 2, 0, @language_layout)
  469.     #-----
  470.     return (length == 0 ? '' : result)
  471.   end
  472.  
  473.   #--------------------------------------------------------------------------
  474.   # * Accents Table, to bo used in conversion from ASCII to UTF8
  475.   #--------------------------------------------------------------------------
  476.   AccentsCharsConv = {
  477.     'A' =>  ['À', 'Á', 'Â', 'Ã', 'Ä'],
  478.     'E' =>  ['È', 'É', 'Ê',  0,  'Ë'],
  479.     'I' =>  ['Ì', 'Í', 'Î',  0,  'Ï'],
  480.     'O' =>  ['Ò', 'Ó', 'Ô', 'Õ', 'Ö'],
  481.     'U' =>  ['Ù', 'Ú', 'Û',  0,  'Ü'],
  482.     'C' =>  [ 0 , 'Ç',  0 ,  0,  0 ],
  483.     'N' =>  [ 0 ,  0,  0 , 'Ñ',  0 ],
  484.     'Y' =>  [ 0 , 'Ý',  0 ,  0,  'Ćž'],
  485.     'a' =>  ['Ă ', 'ĂĄ', 'Ăą', 'ĂŁ', 'Ă€'],
  486.     'e' =>  ['Ăš', 'Ă©', 'ĂȘ',  0 , 'Ă«'],
  487.     'i' =>  ['ĂŹ', 'Ă­', 'Ăź',  0 , 'ĂŻ'],
  488.     'o' =>  ['ĂČ', 'Ăł', 'ĂŽ', 'Ă”', 'ö'],
  489.     'u' =>  ['Ăč', 'Ăș', 'Ă»',  0 , 'ĂŒ'],
  490.     'c' =>  [ 0 , 'ç',  0 ,  0 ,  0 ],
  491.     'n' =>  [ 0 ,  0 ,  0 , 'ñ',  0 ],
  492.     'y' =>  [ 0 , 'Ăœ',  0 ,  0 , 'Ăż'],
  493.   }
  494.   # Letters that can have accent
  495.   PssbLetters    = 'AEIOUCNYaeioucny'
  496.   # Accents, in ASCII, configured at runtime to avoid encoding troubles
  497.   Accents        = [96.chr, 180.chr, 94.chr, 126.chr, 168.chr].join
  498.   NonCompatChars = [180, 168]
  499.   @last_accent  = nil
  500.  
  501.   #--------------------------------------------------------------------------
  502.   # * Get inputed string transcoded to UTF8
  503.   #--------------------------------------------------------------------------
  504.   def self.UTF8String
  505.     result = ''
  506.     31.upto(255) {|key|
  507.         if self.repeat?(key)
  508.           c = self.get_character(key)
  509.           if (cc = c.unpack('C')[0]) and NonCompatChars.include?(cc)
  510.             result += cc.chr
  511.           else
  512.             result += UNICODE_TO_UTF8.convert(c) if c != ''
  513.           end
  514.         end
  515.     }
  516.     return '' if result == ''
  517.     #-----
  518.     if @last_accent
  519.       result      = @last_accent + result
  520.       @last_accent = nil
  521.     end
  522.     f_result = ''
  523.     jump    = false
  524.     for i in 0 ... result.length
  525.       c = result[i]
  526.       if jump
  527.         jump = false
  528.         next
  529.       end
  530.       #-----
  531.       if Accents.include?(c)
  532.         if (nc = result[i+1]) != nil
  533.           if PssbLetters.include?(nc)
  534.             if (ac = AccentsCharsConv[nc][Accents.index(c)]) != 0
  535.               f_result << ac
  536.               jump = true
  537.             else
  538.               f_result << c
  539.               f_result << nc
  540.               jump = true
  541.             end
  542.           elsif Accents.include?(nc)
  543.             f_result << c
  544.             f_result << nc
  545.             jump = true
  546.           else
  547.             f_result << c
  548.             f_result << nc
  549.             jump = true
  550.           end
  551.         else
  552.           @last_accent = c
  553.         end
  554.       else
  555.         f_result << c
  556.       end
  557.     end
  558.     #-----
  559.     return f_result
  560.   end
  561.   #----------
  562. end
  563.  
  564. #
  565. # Game_Interpreter
  566. # def command_111 modification to work properly with my Input System
  567. #
  568. class Game_Interpreter
  569.   #--------------------------------------------------------------------------
  570.   # * Conditional Branch
  571.   #--------------------------------------------------------------------------
  572.   def command_111
  573.     result = false
  574.     case @params[0]
  575.     when 0  # Switch
  576.       result = ($game_switches[@params[1]] == (@params[2] == 0))
  577.     when 1  # Variable
  578.       value1 = $game_variables[@params[1]]
  579.       if @params[2] == 0
  580.         value2 = @params[3]
  581.       else
  582.         value2 = $game_variables[@params[3]]
  583.       end
  584.       case @params[4]
  585.       when 0  # value1 is equal to value2
  586.         result = (value1 == value2)
  587.       when 1  # value1 is greater than or equal to value2
  588.         result = (value1 >= value2)
  589.       when 2  # value1 is less than or equal to value2
  590.         result = (value1 <= value2)
  591.       when 3  # value1 is greater than value2
  592.         result = (value1 > value2)
  593.       when 4  # value1 is less than value2
  594.         result = (value1 < value2)
  595.       when 5  # value1 is not equal to value2
  596.         result = (value1 != value2)
  597.       end
  598.     when 2  # Self switch
  599.       if @event_id > 0
  600.         key = [@map_id, @event_id, @params[1]]
  601.         result = ($game_self_switches[key] == (@params[2] == 0))
  602.       end
  603.     when 3  # Timer
  604.       if $game_timer.working?
  605.         if @params[2] == 0
  606.           result = ($game_timer.sec >= @params[1])
  607.         else
  608.           result = ($game_timer.sec <= @params[1])
  609.         end
  610.       end
  611.     when 4  # Actor
  612.       actor = $game_actors[@params[1]]
  613.       if actor
  614.         case @params[2]
  615.         when 0  # in party
  616.           result = ($game_party.members.include?(actor))
  617.         when 1  # name
  618.           result = (actor.name == @params[3])
  619.         when 2  # Class
  620.           result = (actor.class_id == @params[3])
  621.         when 3  # Skills
  622.           result = (actor.skill_learn?($data_skills[@params[3]]))
  623.         when 4  # Weapons
  624.           result = (actor.weapons.include?($data_weapons[@params[3]]))
  625.         when 5  # Armors
  626.           result = (actor.armors.include?($data_armors[@params[3]]))
  627.         when 6  # States
  628.           result = (actor.state?(@params[3]))
  629.         end
  630.       end
  631.     when 5  # Enemy
  632.       enemy = $game_troop.members[@params[1]]
  633.       if enemy
  634.         case @params[2]
  635.         when 0  # appear
  636.           result = (enemy.alive?)
  637.         when 1  # state
  638.           result = (enemy.state?(@params[3]))
  639.         end
  640.       end
  641.     when 6  # Character
  642.       character = get_character(@params[1])
  643.       if character
  644.         result = (character.direction == @params[2])
  645.       end
  646.     when 7  # Gold
  647.       case @params[2]
  648.       when 0  # Greater than or equal to
  649.         result = ($game_party.gold >= @params[1])
  650.       when 1  # Less than or equal to
  651.         result = ($game_party.gold <= @params[1])
  652.       when 2  # Less than
  653.         result = ($game_party.gold < @params[1])
  654.       end
  655.     when 8  # Item
  656.       result = $game_party.has_item?($data_items[@params[1]])
  657.     when 9  # Weapon
  658.       result = $game_party.has_item?($data_weapons[@params[1]], @params[2])
  659.     when 10  # Armor
  660.       result = $game_party.has_item?($data_armors[@params[1]], @params[2])
  661.     when 11  # Button
  662.       key = Input::EventsKeyCodes[@params[1]]
  663.       result = Input.press?(key) if key
  664.     when 12  # Script
  665.       result = eval(@params[1])
  666.     when 13  # Vehicle
  667.       result = ($game_player.vehicle == $game_map.vehicles[@params[1]])
  668.     end
  669.     @branch[@indent] = result
  670.     command_skip if !@branch[@indent]
  671.   end
  672. end
  673.  
  674. #==============================================================================
  675. # Hotkeys v2.0
  676. #
  677. # *Usage above
  678. #------------------------------------------------------------------------------
  679. raise "requires updated Era module" if ($imported ||= {})["Era Module"] < 0.2
  680. $imported[:Era_Hotkeys] = 2.0
  681.  
  682. class Game_Player < Game_Character
  683.   alias hkey_update_rq update
  684.   def update
  685.     hkey_update_rq
  686.     check_hotkeys
  687.   end
  688.  
  689.   def check_hotkeys
  690.     $game_system.game_hotkeys.each do |v|
  691.       v.try_skill  if Input.trigger?(v.key)
  692.     end
  693.   end
  694. end
  695.  
  696. class Game_System
  697.   def game_hotkeys; era_obj.game_hotkeys; end
  698.   def bind_hotkey(id, sym, usr, ksym)
  699.     game_hotkeys.bind_key(id, sym, usr, ksym)
  700.   end
  701.   def bind_or_remove_hotkey(id, sym, usr, ksym)
  702.     game_hotkeys.bind_or_remove_hotkey(id, sym, usr, ksym)
  703.   end
  704. end
  705.  
  706. class CEra
  707.   alias pub_mems_alias_hkeys init_public_mems
  708.   def init_public_mems(opts = {})
  709.     pub_mems_alias_hkeys(opts)
  710.     init_hkeys
  711.   end
  712.  
  713.   def init_hkeys; @game_hotkeys = Game_Hotkeys.new; end
  714.   def game_hotkeys; @game_hotkeys; end
  715. end
  716.  
  717. class Game_Hotkeys
  718.   attr_reader :hotkeys
  719.   def initialize
  720.     @hotkeys = {}
  721.     @visible = true
  722.   end
  723.  
  724.   def bind_or_remove_hotkey(id, sym, usr, ksym)
  725.     fkey = find_hotkey(db_arr(sym)[id])
  726.     if fkey == ksym
  727.       remove_hotkey_h(ksym)#(db_arr(sym)[id])
  728.     else
  729.       bind_key(id, sym, usr, ksym)
  730.     end
  731.   end
  732.  
  733.   def db_arr(sym); sym == :item ? $data_items : $data_skills; end
  734.  
  735.   # removes hotkey based on key sym, potentially much faster
  736.   def remove_hotkey_h(ksym); @hotkeys.delete(ksym); end
  737.  
  738.   # params Example (2,:skill, $game_party.members[0], :Z)
  739.   def bind_key(id, sym, battler, keysym)
  740.     remove_hotkey(db_arr(sym)[id])
  741.     @hotkeys[keysym] = Hotkey_Binding.new(id, sym, battler, keysym)
  742.   end
  743.  
  744.   def each
  745.     @hotkeys.keys.each{ |k| yield @hotkeys[k] }
  746.   end
  747.  
  748.   def size
  749.     @hotkeys.keys.size
  750.   end
  751.  
  752.   # finds associated hotkey for item, returns nil if none found
  753.   def find_hotkey(item)
  754.     sym = item.is_a?(RPG::Skill) ? :skill : :item
  755.     each{|v| return v.key if v.skill_id == item.id && sym == v.item_type_sym}
  756.     nil
  757.   end
  758.  
  759.   def remove_hotkey(item)
  760.     sym = item.is_a?(RPG::Skill) ? :skill : :item
  761.     @hotkeys.keys.inject(false) do |r,k|
  762.      
  763.       next unless sym = (v = @hotkeys[k]).item_type_sym
  764.       @hotkeys.delete(k) if (tmp = v.skill_id == item.id)
  765.       r = r || tmp
  766.     end
  767.   end
  768.  
  769.   def toggle_visibility
  770.     @visible ? @visible = false : @visible = true
  771.   end
  772.  
  773.   def visible; @visible; end
  774. end
  775.  
  776. class Hotkey_Binding
  777.   attr_reader :user      #  battler that is using the skill
  778.   attr_reader :key       # key to press to activate the skill
  779.   attr_reader :skill_id
  780.   attr_reader :item_type_sym
  781.  
  782.   def initialize(skill_id, item_type_sym, battler, keysym)
  783.     @user = battler
  784.     @key = keysym
  785.     @skill_id = skill_id
  786.     @item_type_sym = item_type_sym
  787.   end
  788.  
  789.   def can_use?
  790.     return false if !user.usable?(item)
  791.     return false if item.scope == 11 && !user.item_test(user, item)
  792.     return true
  793.   end
  794.  
  795.   def try_skill
  796.     (item.scope<=6 || item.scope==11 ? use_skill : friend_selection) if can_use?
  797.   end
  798.  
  799.   def friend_selection
  800.     SceneManager.call(Scene_HkeyActorSelect)
  801.     scene = SceneManager.scene
  802.     scene.item = item
  803.     scene.item_user = @user
  804.   end
  805.  
  806.   # Before the item is used need to access:
  807.   #   battler.item_test(user, item)
  808.   def use_skill
  809.     Sound.play_use_item
  810.     @user.use_item(item)
  811.     use_item_on_targets
  812.     check_gameover
  813.   end
  814.  
  815.   def check_gameover
  816.     SceneManager.goto(Scene_Gameover) if $game_party.all_dead?
  817.   end
  818.  
  819.   def use_item_on_targets
  820.     item_targets.each do |target|
  821.       item.repeats.times { target.item_apply(@user, item) }
  822.     end
  823.   end
  824.  
  825.   def item
  826.     case @item_type_sym
  827.     when :skill; $data_skills[@skill_id];
  828.     when :item;  $data_items[@skill_id];
  829.     end
  830.   end
  831.  
  832.   def item_targets
  833.     if !item.for_friend?
  834.       []
  835.     elsif item.for_all?
  836.       $game_party.members
  837.     else
  838.       [$game_party.members[0]]
  839.     end
  840.   end
  841.  
  842.   def actor_win_index
  843.     scene = SceneManager.scene
  844.     scene.is_a?(Scene_Map) ? scene.actor_window_index : -1
  845.   end
  846.  
  847.   def to_s
  848.     "user #{@user}, #{@item_type_sym.to_s} #{@skill_id}, Key: #{@key}"
  849.   end
  850. end
  851.  
  852.  
  853. class Scene_HkeyActorSelect < Scene_ItemBase
  854.   def start
  855.     super
  856.     @actor_window.show
  857.     @actor_window.activate
  858.     @actor_window.select_last
  859.   end
  860.  
  861.   def user; @user; end
  862.   def item_user=(actor); @user = actor; end
  863.   def item; @hkey_item; end
  864.   def item=(itm); @hkey_item = itm; end
  865.   def on_actor_cancel; return_scene; end
  866.   def play_se_for_item; Sound.play_use_item; end
  867.    
  868.   def on_actor_ok
  869.     if item_usable?
  870.       use_item
  871.       return_scene
  872.     else
  873.       Sound.play_buzzer
  874.     end
  875.   end
  876. end # Scene_HkeyActorSelect
  877.  
  878. class Window_MenuCommand < Window_Command
  879.   alias add_orig_coms_hkeys add_main_commands#add_original_commands
  880.   def add_main_commands#add_original_commands
  881.    
  882.     add_command("Hotkeys",   :hot_keys,   main_commands_enabled)
  883.     add_orig_coms_hkeys
  884.     add_command("Options",   :hkey_options,   main_commands_enabled)
  885.   end
  886. end
  887.  
  888. class Window_HkeyCategory < Window_HorzCommand
  889.   attr_reader   :hkeyselect_win  
  890.  
  891.   def initialize(x,y); super(x, y); end
  892.  
  893.   def window_width; Graphics.width; end
  894.  
  895.   def col_max; return 2; end
  896.  
  897.   def update
  898.     super
  899.     @hkeyselect_win.category = current_symbol if @hkeyselect_win
  900.   end
  901.  
  902.   def make_command_list
  903.     add_command(Vocab::item,     :item)
  904.     add_command(Vocab::skill,   :skill)
  905.   end
  906.  
  907.   def hkeyselect_win=(window)
  908.     @hkeyselect_win = window
  909.     update
  910.   end
  911. end
  912.  
  913. # Allows hotkeys to be set
  914. class Window_HkeySelection < Window_Selectable
  915.  
  916.   def initialize(x,y,w,h)
  917.     super(x,y,w,h)
  918.     @data = []
  919.     @category = nil
  920.     @last_selection = 0
  921.   end
  922.  
  923.   def user; $game_party.menu_actor; end
  924.  
  925.   def deactivate
  926.     @last_selection = @index
  927.     super
  928.   end
  929.  
  930.   def category=(cat); @category = cat; end
  931.  
  932.   def select_last
  933.     select(@data.size >  @last_selection ? @last_selection || 0 : 0)
  934.   end
  935.  
  936.   def make_item_list
  937.     case @category
  938.     when :skill
  939.       @data = user ? user.skills : []#.select {|skill| include?(skill) } : []
  940.     when :item
  941.       @data = $game_party.all_items
  942.     end
  943.   end
  944.  
  945.   def activate
  946.     super
  947.     refresh
  948.   end
  949.  
  950.   def refresh
  951.     make_item_list
  952.     create_contents
  953.     draw_all_items
  954.   end
  955.  
  956.   def draw_item(index)
  957.     skill = @data[index]
  958.     if skill
  959.       rect = item_rect(index)
  960.       rect.width -= 4
  961.       draw_item_name(skill, rect.x, rect.y, true)
  962.     end
  963.   end
  964.  
  965.   def draw_item_name(item, x, y, enabled = true, width = 172)
  966.     return unless item
  967.     draw_icon(item.icon_index, x, y, enabled)
  968.     change_color(normal_color, enabled)
  969.     draw_text(x + 24, y, width, line_height, item.name + " " + hkeystr(item))
  970.   end
  971.  
  972.   def item_max
  973.     @data ? @data.size : 1
  974.   end
  975.  
  976.   def col_max
  977.     return 2
  978.   end
  979.  
  980.   def hkeystr(skill)
  981.     str = $game_system.game_hotkeys.find_hotkey(skill).to_s.gsub(/key_|letter_/i,'')
  982.    (str.eql?("") ? "" : " :" +  str)
  983.   end
  984.  
  985.   def update
  986.     super
  987.     validkeys = Input::KEYMAP
  988.     validkeys.keys.each do |k|
  989.       item = @data[@index]
  990.       next if k == :UP || k==:DOWN || k==:LEFT || k==:RIGHT || !item
  991.      
  992.       if Input.trigger?(validkeys[k])
  993.         tsym = item.is_a?(RPG::Skill) ? :skill : :item
  994.         $game_system.bind_or_remove_hotkey(item.id, tsym, user, k)#id, sym, usr, ksym
  995.         refresh
  996.         break
  997.       end
  998.     end
  999.   end # update
  1000.  
  1001. end
  1002.  
  1003. class Window_HkeyToggle < Window_Selectable
  1004.   def item_max; 1; end
  1005.   def draw_item(index)
  1006.     r = item_rect(index)
  1007.     is_on = $game_system.game_hotkeys.visible
  1008.     draw_text(r.x,r.y,145,line_height,"Hotkey Bar: " +
  1009.       (is_on ? "On " : "Off"))
  1010.   end
  1011. end
  1012.  
  1013. class Scene_Menu < Scene_MenuBase
  1014.   alias start_hkey_scm start
  1015.   def start
  1016.     start_hkey_scm
  1017.     create_hkey_toggle_win
  1018.   end
  1019.  
  1020.   def create_hkey_toggle_win
  1021.     @hkey_toggle_window = Window_HkeyToggle.new(
  1022.       0,@command_window.height,@command_window.width,60)
  1023.     @hkey_toggle_window.set_handler(:ok, method(:toggle_hkey_visible))
  1024.     @hkey_toggle_window.set_handler(:cancel, method(:hkey_toggle_window_hide))
  1025.     @hkey_toggle_window.hide
  1026.   end
  1027.  
  1028.   def toggle_hkey_visible
  1029.     $game_system.game_hotkeys.toggle_visibility
  1030.     @hkey_toggle_window.refresh
  1031.     @hkey_toggle_window.activate
  1032.     @hkey_toggle_window.select(0)
  1033.   end
  1034.  
  1035.   def hkey_toggle_window_hide
  1036.     @hkey_toggle_window.deactivate
  1037.     @hkey_toggle_window.hide
  1038.     @command_window.activate
  1039.   end
  1040.  
  1041.   alias create_com_win_hkeys create_command_window
  1042.   def create_command_window
  1043.     create_com_win_hkeys
  1044.     @command_window.set_handler(:hot_keys, method(:command_personal))
  1045.     @command_window.set_handler(:hkey_options, method(:command_hkey_opts))
  1046.   end
  1047.  
  1048.   def command_hkey_opts
  1049.     @hkey_toggle_window.activate
  1050.     @hkey_toggle_window.show
  1051.     @hkey_toggle_window.refresh
  1052.     @command_window.deactivate
  1053.   end
  1054.  
  1055.   alias on_persok_hkeys on_personal_ok
  1056.   def on_personal_ok
  1057.     on_persok_hkeys
  1058.     SceneManager.call(Scene_Hkey) if @command_window.current_symbol == :hot_keys
  1059.   end
  1060. end
  1061.  
  1062. # Scene which allows hotkeys to be setup
  1063. class Scene_Hkey < Scene_MenuBase
  1064.   def start
  1065.     super
  1066.     create_help_window
  1067.     create_hkey_category_win
  1068.     create_hkey_select_win
  1069.   end
  1070.  
  1071.   def create_help_window
  1072.     super
  1073.     @help_window.set_text "Hot Key Setup"
  1074.   end
  1075.  
  1076.   def create_hkey_category_win
  1077.     @category_window = Window_HkeyCategory.new(0,@help_window.height)
  1078.     @category_window.set_handler(:ok,   method(:on_category_ok))
  1079.     @category_window.set_handler(:cancel,   method(:return_scene))
  1080.     @category_window.activate
  1081.   end
  1082.  
  1083.   def on_category_ok
  1084.     @hkey_select_window.activate
  1085.     @hkey_select_window.select_last
  1086.     @help_window.set_text "Press a key to set it as a hotkey for any " +
  1087.       "#{@category_window.current_symbol.to_s}"
  1088.   end
  1089.  
  1090.   def create_hkey_select_win
  1091.     wy = @category_window.y+@category_window.height
  1092.     @hkey_select_window = Window_HkeySelection.new(
  1093.       0, wy, Graphics.width,Graphics.height-wy)
  1094.     @hkey_select_window.set_handler(:cancel,   method(:on_hkey_select_cancel))
  1095.     @category_window.hkeyselect_win = @hkey_select_window
  1096.   end
  1097.  
  1098.   def on_hkey_select_cancel
  1099.     @hkey_select_window.deactivate
  1100.     @category_window.activate
  1101.     @help_window.set_text "Hot Key Setup"
  1102.   end
  1103. end
  1104.  
  1105. class Scene_Map < Scene_Base
  1106.   alias create_wins_hkeys create_all_windows
  1107.   def create_all_windows
  1108.     create_wins_hkeys
  1109.     create_hkey_display
  1110.   end
  1111.  
  1112.   def create_hkey_display
  1113.     @hotkey_window = Window_Hotkeys.new
  1114.     @hotkey_window.refresh
  1115.     check_display_hkey_window
  1116.   end
  1117.  
  1118.   alias upd_scne_map update
  1119.   def update
  1120.     upd_scne_map
  1121.     check_display_hkey_window
  1122.   end
  1123.  
  1124.   alias perform_trans_hkeys perform_transition
  1125.   def perform_transition
  1126.     perform_trans_hkeys
  1127.     check_display_hkey_window
  1128.   end
  1129.  
  1130.   def check_display_hkey_window
  1131.     hide_hkey_window? ? @hotkey_window.hide : @hotkey_window.show
  1132.   end
  1133.  
  1134.   def hide_hkey_window?
  1135.    !$game_system.game_hotkeys.visible || $game_message.visible || @hotkey_window.amt_shown <= 0
  1136.   end
  1137. end
  1138.  
  1139. class Window_Hotkeys < Window_Base
  1140.   def initialize
  1141.     w = Graphics.width-40
  1142.     visible = false
  1143.     super((Graphics.width-w)/2,Graphics.height - 70,w,50)
  1144.     self.arrows_visible = false
  1145.   end
  1146.  
  1147.   def amt_shown
  1148.     [$game_system.game_hotkeys.size, max_shown].min
  1149.   end
  1150.  
  1151.   def max_shown; 12; end
  1152.  
  1153.   def draw_each_hotkey
  1154.     ind = 0
  1155.     amt = amt_shown
  1156.  
  1157.     $game_system.game_hotkeys.each do |v|
  1158.       draw_icon(v.item.icon_index, ind, 0, v.can_use?)
  1159.       draw_text(ind+16, 0, 12,20, v.key.to_s.gsub!(/key_|letter_/i,''))
  1160.       ind += contents.width/max_shown
  1161.     end
  1162.   end
  1163.  
  1164.   def activate
  1165.     super
  1166.     show
  1167.     refresh
  1168.   end
  1169.  
  1170.   def max_width
  1171.     Graphics.width-40
  1172.   end
  1173.  
  1174.   def refresh
  1175.     contents.clear
  1176.    
  1177.     bsize = max_width/max_shown
  1178.     self.width = [(bsize * amt_shown),max_width].min
  1179.     self.width += standard_padding/2 if self.width > 0
  1180.     self.x = (Graphics.width - self.width)/2
  1181.     draw_each_hotkey
  1182.   end
  1183.  
  1184. end #eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement