Advertisement
JademusSreg

Input

Jun 6th, 2011
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. //=====================================================
  2. // Input, a script to capture key and mouse input
  3. //
  4. // This script will catch and store player input
  5. // from the keyboard and mouse in a struct array.
  6. // Keys and buttons are records as bool states,
  7. // while mouse clicks and movement are kept as
  8. // int and fixed values for the UI and World
  9. // respectively.
  10. //
  11. // To get the state of the X key, you would use:
  12. // Input[p].key.state[c_keyX]
  13. // To get the point of the cursor, you could:
  14. // Point(Input[p].mouse.x,Input[p].mouse.y)
  15. //
  16. // The user may configure the DETECT bool
  17. // constants to set which triggers will be
  18. // created on initialization of the script.
  19. //=====================================================
  20. include "TriggerLibs/natives"
  21.  
  22. // CONFIGURABLE CONSTANTS
  23. static const bool DETECT_KEYDOWN = true;
  24. static const bool DETECT_KEYUP = true;
  25. static const bool DETECT_BUTTONDOWN = true;
  26. static const bool DETECT_BUTTONUP = true;
  27. static const bool DETECT_MOVEMENT = true;
  28. static const int INPUT_BUTTONS = 6;
  29. static const int INPUT_KEYS = 99;
  30. // STRUCTS
  31. struct Button { bool[INPUT_BUTTONS] state; };
  32. struct Key { bool[INPUT_KEYS] state; };
  33. struct Mouse
  34. {
  35.     int uix;
  36.     int uiy;
  37.     fixed x;
  38.     fixed y;
  39.     fixed z;
  40. };
  41. struct InputData
  42. {
  43.     Key key;
  44.     Button button;
  45.     Mouse mouse;
  46. };
  47. // GLOBALS
  48. InputData[c_maxPlayers] Input;
  49. // FUNCTIONS
  50. bool Input_KeyOn (bool testConds, bool runActions)
  51. {
  52.     Input[EventPlayer()].key.state[EventKeyPressed()] = true;
  53.     return true;
  54. }
  55. bool Input_KeyOff (bool testConds, bool runActions)
  56. {
  57.     Input[EventPlayer()].key.state[EventKeyPressed()] = false;
  58.     return true;
  59. }
  60. bool Input_ButtonOn (bool testConds, bool runActions)
  61. {
  62.     int p = EventPlayer();
  63.     Input[p].button.state[EventMouseClickedButton()] = true;
  64.     Input[p].mouse.uix = EventMouseClickedPosXUI();
  65.     Input[p].mouse.uiy = EventMouseClickedPosYUI();
  66.     Input[p].mouse.x = EventMouseClickedPosXWorld();
  67.     Input[p].mouse.y = EventMouseClickedPosYWorld();
  68.     Input[p].mouse.z = EventMouseClickedPosZWorld();
  69.     return true;
  70. }
  71. bool Input_ButtonOff (bool testConds, bool runActions)
  72. {
  73.     int p = EventPlayer();
  74.     Input[p].button.state[EventMouseClickedButton()] = false;
  75.     Input[p].mouse.uix = EventMouseClickedPosXUI();
  76.     Input[p].mouse.uiy = EventMouseClickedPosYUI();
  77.     Input[p].mouse.x = EventMouseClickedPosXWorld();
  78.     Input[p].mouse.y = EventMouseClickedPosYWorld();
  79.     Input[p].mouse.z = EventMouseClickedPosZWorld();
  80.     return true;
  81. }
  82. bool Input_Movement (bool testConds, bool runActions)
  83. {
  84.     int p = EventPlayer();
  85.     Input[p].mouse.uix = EventMouseMovedPosXUI();
  86.     Input[p].mouse.uiy = EventMouseMovedPosYUI();
  87.     Input[p].mouse.x = EventMouseMovedPosXWorld();
  88.     Input[p].mouse.y = EventMouseMovedPosYWorld();
  89.     Input[p].mouse.z = EventMouseMovedPosZWorld();
  90.     return true;
  91. }
  92. void Input_Init ()
  93. {
  94.     if (DETECT_KEYDOWN)
  95.     {
  96.         TriggerAddEventKeyPressed(TriggerCreate("Input_KeyOn"),c_playerAny,c_keyNone,true,0,0,0);
  97.     }
  98.     if (DETECT_KEYUP)
  99.     {
  100.         TriggerAddEventKeyPressed(TriggerCreate("Input_KeyOff"),c_playerAny,c_keyNone,false,0,0,0);
  101.     }
  102.     if (DETECT_BUTTONDOWN)
  103.     {
  104.         TriggerAddEventMouseClicked(TriggerCreate("Input_ButtonOn"),c_playerAny,c_mouseButtonNone,true);
  105.     }
  106.     if (DETECT_BUTTONUP)
  107.     {
  108.         TriggerAddEventMouseClicked(TriggerCreate("Input_ButtonOff"),c_playerAny,c_mouseButtonNone,false);
  109.     }
  110.     if (DETECT_MOVEMENT)
  111.     {
  112.         TriggerAddEventMouseMoved(TriggerCreate("Input_Movement"), c_playerAny);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement