HKI Core
InputSystem.cs
Go to the documentation of this file.
1 #pragma warning disable 0649
2 using UnityEngine;
3 using System.Collections;
4 using System.Collections.Generic;
5 
6 namespace HKI.Core.GameSystems.Input
7 {
11  [CreateAssetMenu(fileName = "Input System", menuName = "HKI/Game Systems/Input System", order = 0)]
12  public class InputSystem : GameSystem
13  {
14  // Public variables
18  [SerializeField] KeyInputHandler[] KeyInputHandlers;
19 
24 
25  [SerializeField] AxisInputHandler[] AxisInputHandlers;
27 
28  // Private variables
29  int inputHandlersLength = 0;
30 
31  InputHandler[] inputHandlers = null;
32 
33  // Interface functions
38  public override IEnumerator Init()
39  {
40  bool validationSuccessful = true;
41 
42  // Keyboard input
43  Validate("key toggle", KeyToggleInputHandlers, ref validationSuccessful);
44  Validate("key down", KeyDownInputHandlers, ref validationSuccessful);
45  Validate("key up", KeyUpInputHandlers, ref validationSuccessful);
46  Validate("key ", KeyInputHandlers, ref validationSuccessful);
47 
48  // Mouse input
49  Validate("mouse position", MousePositionInputHandler, ref validationSuccessful);
50  Validate("mouse button down", MouseButtonDownInputHandlers, ref validationSuccessful);
51  Validate("mouse button up", MouseButtonUpInputHandlers, ref validationSuccessful);
52  Validate("mouse button", MouseButtonInputHandlers, ref validationSuccessful);
53 
54  // Axis input
55  Validate("axis", AxisInputHandlers, ref validationSuccessful);
56  Validate("axis raw", AxisRawInputHandlers, ref validationSuccessful);
57 
58  List<InputHandler> inputHandlersList = new List<InputHandler>();
59 
60  // Keyboard input
61  if (KeyToggleInputHandlers != null && KeyToggleInputHandlers.Length > 0)
62  inputHandlersList.AddRange(KeyToggleInputHandlers);
63 
64  if (KeyDownInputHandlers != null && KeyDownInputHandlers.Length > 0)
65  inputHandlersList.AddRange(KeyDownInputHandlers);
66 
67  if(KeyUpInputHandlers != null && KeyUpInputHandlers.Length > 0)
68  inputHandlersList.AddRange(KeyUpInputHandlers);
69 
70  if(KeyInputHandlers != null && KeyInputHandlers.Length > 0)
71  inputHandlersList.AddRange(KeyInputHandlers);
72 
73  // Mouse input
74  inputHandlersList.Add(MousePositionInputHandler);
75 
76  if (MouseButtonDownInputHandlers != null && MouseButtonDownInputHandlers.Length > 0)
77  inputHandlersList.AddRange(MouseButtonDownInputHandlers);
78 
79  if (MouseButtonUpInputHandlers != null && MouseButtonUpInputHandlers.Length > 0)
80  inputHandlersList.AddRange(MouseButtonUpInputHandlers);
81 
82  if (MouseButtonInputHandlers != null && MouseButtonInputHandlers.Length > 0)
83  inputHandlersList.AddRange(MouseButtonInputHandlers);
84 
85  // Axis input
86  if (AxisInputHandlers != null && AxisInputHandlers.Length > 0)
87  inputHandlersList.AddRange(AxisInputHandlers);
88 
89  if (AxisRawInputHandlers != null && AxisRawInputHandlers.Length > 0)
90  inputHandlersList.AddRange(AxisRawInputHandlers);
91 
92  inputHandlers = inputHandlersList.ToArray();
93  inputHandlersLength = inputHandlers.Length;
94 
95  initSuccessful = validationSuccessful;
96 
97  yield break;
98  }
99 
100  // Update function
104  public override void Update()
105  {
106  if(inputHandlers != null)
107  {
108  for (int i = 0; i < inputHandlersLength; i++)
109  inputHandlers[i].Update();
110  }
111  }
112 
113  // Helper functions
120  void Validate(string inputHandlerTypeName, InputHandler inputhandler, ref bool validationSuccessful)
121  {
122  if(inputhandler != null)
123  {
124  if (!inputhandler.Validate())
125  {
126  Debug.LogError("(InputSystem) validation failed on " + inputHandlerTypeName + " input handler");
127  validationSuccessful = false;
128  }
129  }
130  }
131 
138  void Validate(string inputHandlerTypeName, InputHandler[] inputhandlers, ref bool validationSuccessful)
139  {
140  if(inputhandlers != null)
141  {
142  for (int i = 0; i < inputhandlers.Length; i++)
143  {
144  if (!inputhandlers[i].Validate())
145  {
146  Debug.LogError("(InputSystem) validation failed on " + inputHandlerTypeName + " input handler with the index: " + i.ToString());
147  validationSuccessful = false;
148  }
149  }
150  }
151  }
152  }
153 }
This GameSystem purpose it to fetch the information from the engine layer and feed it into an input e...
Definition: InputSystem.cs:12
MouseButtonDownInputHandler [] MouseButtonDownInputHandlers
Definition: InputSystem.cs:21
override void Update()
Implementation of the GameSystem update function. This function updates all input handlers...
Definition: InputSystem.cs:104
KeyUpInputHandler [] KeyUpInputHandlers
Definition: InputSystem.cs:17
MousePositionInputHandler MousePositionInputHandler
Definition: InputSystem.cs:20
KeyToggleInputHandler [] KeyToggleInputHandlers
Definition: InputSystem.cs:15
Interface for all InputHandler.
Definition: InputHandlers.cs:9
KeyDownInputHandler [] KeyDownInputHandlers
Definition: InputSystem.cs:16
override IEnumerator Init()
This is the implementation of the GameSystem init function. It validates all input handlers and creat...
Definition: InputSystem.cs:38
AxisInputHandler [] AxisInputHandlers
Definition: InputSystem.cs:25
MouseButtonUpInputHandler [] MouseButtonUpInputHandlers
Definition: InputSystem.cs:22
bool Validate()
Validation function that check if the handler is setup properly (all parameters are set and not null)...
AxisRawInputHandler [] AxisRawInputHandlers
Definition: InputSystem.cs:26
MouseButtonInputHandler [] MouseButtonInputHandlers
Definition: InputSystem.cs:23
void Validate(string inputHandlerTypeName, InputHandler[] inputhandlers, ref bool validationSuccessful)
Validate an array of input handlers.
Definition: InputSystem.cs:138
This ScriptableObject is the abstract base class for all GameSystems. It provides "interface" functio...
Definition: GameSystem.cs:10
void Validate(string inputHandlerTypeName, InputHandler inputhandler, ref bool validationSuccessful)
Validate a single input handler.
Definition: InputSystem.cs:120