VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
InputScheme.h
1 /*
2  * Copyright (C) 2014 Google Inc.
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  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INPUTSCHEME_H
18 #define INPUTSCHEME_H
19 
20 #include <map>
21 #include "ControllerEvent.h"
22 
27 template <typename Input = int>
28 class InputScheme {
29 public:
33  typedef int Action;
34 
38  typedef std::map<Input, Action> Map;
39 
44  }
45 
50  explicit InputScheme(const Map& scheme)
51  : mScheme(scheme) {
52  }
53 
54  virtual ~InputScheme() { }
55 
59  bool empty() const { return mScheme.empty(); }
60 
64  typename Map::const_iterator begin() const {
65  return mScheme.cbegin();
66  }
67 
71  typename Map::const_iterator end() const {
72  return mScheme.cend();
73  }
74 
80  bool hasActionFor(const Input& input) const {
81  return mScheme.count(input) > 0;
82  }
83 
88  bool intersects(const InputScheme<Input>& inputScheme) const {
89  for (auto& inputActionPair : inputScheme) {
90  if (mScheme.count(inputActionPair.first)) {
91  return true;
92  }
93  }
94  return false;
95  }
96 
101  virtual bool handlesControllerEvent(const ControllerEvent* event) const = 0;
102 
103 private:
104  Map mScheme;
105 };
106 
107 #endif // INPUTSCHEME_H
bool empty() const
Returns whether or not the InputScheme contains any mappings.
Definition: InputScheme.h:59
bool hasActionFor(const Input &input) const
Returns whether or not this input scheme has an action associated with the specified input...
Definition: InputScheme.h:80
bool intersects(const InputScheme< Input > &inputScheme) const
Returns whether or not inputScheme intersects with this InputScheme.
Definition: InputScheme.h:88
A platform independent event generated by controller device.
Definition: ControllerEvent.h:34
virtual bool handlesControllerEvent(const ControllerEvent *event) const =0
Returns whether or not this InputScheme would handle event.
int Action
Output type.
Definition: InputScheme.h:33
Map::const_iterator end() const
Returns a const iterator positioned at the end of the InputScheme.
Definition: InputScheme.h:71
std::map< Input, Action > Map
Input "event" to Action output map.
Definition: InputScheme.h:38
InputScheme()
Constructs an empty input scheme.
Definition: InputScheme.h:43
Map::const_iterator begin() const
Returns a const iterator positioned at the beginning of the InputScheme.
Definition: InputScheme.h:64
InputScheme(const Map &scheme)
Constructs an InputScheme from the specified input to action map.
Definition: InputScheme.h:50
Represents a scheme mapping generic input "events" to output "actions".
Definition: InputScheme.h:28