------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
-- Author: Ian Eller
-- Updated: 09/22/2008, ice

-- This list is from sdl_keysym.h

if m_name_values == nil then
  m_name_values = { }
  m_name_values[ "Mouse Left" ]   = 0
  m_name_values[ "Mouse Middle" ] = 1
  m_name_values[ "Mouse Right" ]  = 2
  m_name_values[ "Wheel Up" ]     = 3
  m_name_values[ "Wheel Down" ]   = 4
  m_name_values[ "Mouse X1" ]     = 5
  m_name_values[ "Mouse X2" ]     = 6
  
  m_values_names = { }
  m_values_names[ 0 ] = "Mouse Left"
  m_values_names[ 1 ] = "Mouse Middle"
  m_values_names[ 2 ] = "Mouse Right"
  m_values_names[ 3 ] = "Wheel Up"
  m_values_names[ 4 ] = "Wheel Down"
  m_values_names[ 5 ] = "Mouse X1"
  m_values_names[ 6 ] = "Mouse X2"
end

if names_values == nil then
  names_values = { }
  names_values[ "Backspace" ] = 8
  names_values[ "Tab" ]       = 9
  names_values[ "Clear" ]     = 12
  names_values[ "Enter" ]     = 13
  names_values[ "Pause" ]     = 19
  names_values[ "Escape" ]    = 27
  names_values[ "Spacebar" ]  = 32
  names_values[ "Tilde" ]     = 96

  -- 33-122 are mapped to ASCII
  names_values[ "0" ] = 48
  names_values[ "1" ] = 49
  names_values[ "2" ] = 50
  names_values[ "3" ] = 51
  names_values[ "4" ] = 52
  names_values[ "5" ] = 53
  names_values[ "6" ] = 54
  names_values[ "7" ] = 55
  names_values[ "8" ] = 56
  names_values[ "9" ] = 57
  
  names_values[ "a" ] = 97
  names_values[ "b" ] = 98
  names_values[ "c" ] = 99
  names_values[ "d" ] = 100
  names_values[ "e" ] = 101
  names_values[ "f" ] = 102
  names_values[ "g" ] = 103
  names_values[ "h" ] = 104
  names_values[ "i" ] = 105
  names_values[ "j" ] = 106
  names_values[ "k" ] = 107
  names_values[ "l" ] = 108
  names_values[ "m" ] = 109
  names_values[ "n" ] = 110
  names_values[ "o" ] = 111
  names_values[ "p" ] = 112
  names_values[ "q" ] = 113
  names_values[ "r" ] = 114
  names_values[ "s" ] = 115
  names_values[ "t" ] = 116
  names_values[ "u" ] = 117
  names_values[ "v" ] = 118
  names_values[ "w" ] = 119
  names_values[ "x" ] = 120
  names_values[ "y" ] = 121
  names_values[ "z" ] = 122

  names_values[ "Delete" ] = 127 

  -- 160-255 are "world" keys, whatever the hell those are

  -- Numeric keypad
  names_values[ "Numpad 0" ] = 256
  names_values[ "Numpad 1" ] = 257
  names_values[ "Numpad 2" ] = 258
  names_values[ "Numpad 3" ] = 259
  names_values[ "Numpad 4" ] = 260
  names_values[ "Numpad 5" ] = 261
  names_values[ "Numpad 6" ] = 262
  names_values[ "Numpad 7" ] = 263
  names_values[ "Numpad 9" ] = 265
  names_values[ "Numpad 8" ] = 264
  names_values[ "Numpad ." ] = 266
  names_values[ "Numpad /" ] = 267
  names_values[ "Numpad *" ] = 268
  names_values[ "Numpad -" ] = 269
  names_values[ "Numpad +" ] = 270
  names_values[ "Numpad Enter" ] = 271
  names_values[ "Numpad =" ] = 272

  -- Arrows + Home/End pad
  names_values[ "Arrow Up" ]    = 273
  names_values[ "Arrow Down" ]  = 274
  names_values[ "Arrow Right" ] = 275
  names_values[ "Arrow Left" ]  = 276
  names_values[ "Insert" ]      = 277
  names_values[ "Home" ]        = 278
  names_values[ "End" ]         = 279
  names_values[ "Page Up" ]     = 280
  names_values[ "Page Down" ]   = 281

  -- Function keys
  names_values[ "F1" ] = 282
  names_values[ "F2" ] = 283
  names_values[ "F3" ] = 284
  names_values[ "F4" ] = 285
  names_values[ "F5" ] = 286
  names_values[ "F6" ] = 287
  names_values[ "F7" ] = 288
  names_values[ "F8" ] = 289
  names_values[ "F9" ] = 290
  names_values[ "F10" ] = 291
  names_values[ "F11" ] = 292
  names_values[ "F12" ] = 293
  names_values[ "F13" ] = 294
  names_values[ "F14" ] = 295
  names_values[ "F15" ] = 296

  -- Key state modifier keys
  names_values[ "Num Lock" ]      = 300
  names_values[ "Caps Lock" ]     = 301
  names_values[ "Scroll Lock" ]   = 302
  names_values[ "Shift Right" ]   = 303
  names_values[ "Shift Left" ]    = 304
  names_values[ "Control Right" ] = 305
  names_values[ "Control Left" ]  = 306
  names_values[ "Alt Right" ]     = 307
  names_values[ "Alt Left" ]      = 308
  names_values[ "Meta Right" ]    = 309
  names_values[ "Meta Left" ]     = 310
  names_values[ "Windows Left" ]  = 311
  names_values[ "Windows Right" ] = 312
  names_values[ "Alt Gr" ]        = 313
  names_values[ "Multi-Key Compose" ] = 314

  -- Miscellaneous function keys
  names_values[ "Help" ]   = 315
  names_values[ "Print" ]  = 316
  names_values[ "Sysreq" ] = 317
  names_values[ "Break" ]  = 318
  names_values[ "Menu" ]   = 319
  names_values[ "Power" ]  = 320
  names_values[ "Euro" ]   = 321
  names_values[ "Undo" ]   = 322
  
  
  
  values_names = { }
  values_names[ 8 ]  = "Backspace"
  values_names[ 9 ]  = "Tab"
  values_names[ 12 ] = "Clear"
  values_names[ 13 ] = "Enter"
  values_names[ 19 ] = "Pause"
  values_names[ 27 ] = "Escape"
  values_names[ 32 ] = "Spacebar"
  values_names[ 96 ] = "Tilde"
  
  -- 33-122 are mapped to ASCII
  values_names[ 48 ] = "0"
  values_names[ 49 ] = "1"
  values_names[ 50 ] = "2"
  values_names[ 51 ] = "3"
  values_names[ 52 ] = "4"
  values_names[ 53 ] = "5"
  values_names[ 54 ] = "6"
  values_names[ 55 ] = "7"
  values_names[ 56 ] = "8"
  values_names[ 57 ] = "9"
  
  names_values[ 97 ]  = "a"
  names_values[ 98 ]  = "b"
  names_values[ 99 ]  = "c"
  names_values[ 100 ] = "d"
  names_values[ 101 ] = "e"
  names_values[ 102 ] = "f"
  names_values[ 103 ] = "g"
  names_values[ 104 ] = "h"
  names_values[ 105 ] = "i"
  names_values[ 106 ] = "j"
  names_values[ 107 ] = "k"
  names_values[ 108 ] = "l"
  names_values[ 109 ] = "m"
  names_values[ 110 ] = "n"
  names_values[ 111 ] = "o"
  names_values[ 112 ] = "p"
  names_values[ 113 ] = "q"
  names_values[ 114 ] = "r"
  names_values[ 115 ] = "s"
  names_values[ 116 ] = "t"
  names_values[ 117 ] = "u"
  names_values[ 118 ] = "v"
  names_values[ 119 ] = "w"
  names_values[ 120 ] = "x"
  names_values[ 121 ] = "y"
  names_values[ 122 ] = "z"
  
  values_names[ 127 ] = "Delete"

  -- 160-255 are "world" keys, whatever the hell those are

  -- Numeric keypad
  values_names[ 256 ] = "Numpad 0"
  values_names[ 257 ] = "Numpad 1"
  values_names[ 258 ] = "Numpad 2"
  values_names[ 259 ] = "Numpad 3"
  values_names[ 260 ] = "Numpad 4"
  values_names[ 261 ] = "Numpad 5"
  values_names[ 262 ] = "Numpad 6"
  values_names[ 263 ] = "Numpad 7"
  values_names[ 265 ] = "Numpad 9"
  values_names[ 264 ] = "Numpad 8"
  values_names[ 266 ] = "Numpad ."
  values_names[ 267 ] = "Numpad /"
  values_names[ 268 ] = "Numpad *"
  values_names[ 269 ] = "Numpad -"
  values_names[ 270 ] = "Numpad +"
  values_names[ 271 ] = "Numpad Enter"
  values_names[ 272 ] = "Numpad ] ="

  -- Arrows + Home/End pad
  values_names[ 273 ] = "Arrow Up"
  values_names[ 274 ] = "Arrow Down"
  values_names[ 275 ] = "Arrow Right"
  values_names[ 276 ] = "Arrow Left"
  values_names[ 277 ] = "Insert"
  values_names[ 278 ] = "Home"
  values_names[ 279 ] = "End"
  values_names[ 280 ] = "Page Up"
  values_names[ 281 ] = "Page Down"

  -- Function keys
  values_names[ 282 ] = "F1"
  values_names[ 283 ] = "F2"
  values_names[ 284 ] = "F3"
  values_names[ 285 ] = "F4"
  values_names[ 286 ] = "F5"
  values_names[ 287 ] = "F6"
  values_names[ 288 ] = "F7"
  values_names[ 289 ] = "F8"
  values_names[ 290 ] = "F9"
  values_names[ 291 ] = "F10"
  values_names[ 292 ] = "F11"
  values_names[ 293 ] = "F12"
  values_names[ 294 ] = "F13"
  values_names[ 295 ] = "F14"
  values_names[ 296 ] = "F15"

  -- Key state modifier keys
  values_names[ 300 ] = "Num Lock"
  values_names[ 301 ] = "Caps Lock"
  values_names[ 302 ] = "Scroll Lock"
  values_names[ 303 ] = "Shift Right"
  values_names[ 304 ] = "Shift Left"
  values_names[ 305 ] = "Control Right"
  values_names[ 306 ] = "Control Left"
  values_names[ 307 ] = "Alt Right"
  values_names[ 308 ] = "Alt Left"
  values_names[ 309 ] = "Meta Right"
  values_names[ 310 ] = "Meta Left"
  values_names[ 311 ] = "Windows Left"
  values_names[ 312 ] = "Windows Right"
  values_names[ 313 ] = "Alt Gr"
  values_names[ 314 ] = "Multi-Key Compose"

  -- Miscellaneous function keys
  values_names[ 315 ] = "Help"
  values_names[ 316 ] = "Print"
  values_names[ 317 ] = "Sysreq"
  values_names[ 318 ] = "Break"
  values_names[ 319 ] = "Menu"
  values_names[ 320 ] = "Power"
  values_names[ 321 ] = "Euro"
  values_names[ 322 ] = "Undo"
end


function mouse_names_to_values( name )
  return m_name_values[ name ]
  
end


function key_values_to_names( value )
  -- The SDLKey defines for letters are their lowercase values, so if
  -- an uppercase is passed, map it to the lower - otherwise just use it
  
  if ( value >= 65 and value < 97 ) then
    return string.format( '%c' , value + 32 )
    
  elseif ( value >= 33 and value <= 64 ) or ( value >= 97 and value <= 126 ) then
    return string.format( '%c' , value )
    
  else
    return values_names[ value ]
    
  end
  
end


function key_names_to_values( name )
  -- The SDLKey defines for letters are their lowercase values, so if
  -- an uppercase is passed, map it to the lower - otherwise just use it
  
  if string.len( name ) == 1 
  then
    local value = string.byte( name )
    
    if ( value >= 65 and value < 97 ) then
      return value + 32
      
    elseif ( value >= 33 and value <= 64 ) or ( value >= 97 and value <= 126 ) then
      return value
      
    end
    
  else
    return names_values[ name ]
    
  end
  
end


function letter_or_number( value )

  if ( value >= 32 and value < 97 ) or ( value >= 97 and value <= 122 ) or ( value >= 123 and value <= 126 ) then
    return true;
    
  end
  
  return false;
end
