| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- /*
- Copyright (C) 2011 by Ivan Safrin
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #include "PolyCoreInput.h"
- #include "PolyInputEvent.h"
- #include "PolyCoreServices.h"
- #include "PolyCore.h"
- namespace Polycode {
-
- JoystickInfo::JoystickInfo() {
- for(int i=0; i < 32; i++) {
- joystickAxisState[i] = 0.0;
- }
- for(int i=0; i < 64; i++) {
- joystickButtonState[i] = false;
- }
- }
-
- CoreInput::CoreInput() : EventDispatcher() {
- clearInput();
- simulateTouchWithMouse = false;
- simulateMouseWithTouch = false;
- ignoreOffScreenTouch = false;
- keyRepeat = true;
- }
-
- void CoreInput::clearInput() {
- mouseButtons[0] = false;
- mouseButtons[1] = false;
- mouseButtons[2] = false;
-
- for(int i=0; i < 512; i++) {
- keyboardState[i] = 0;
- }
- }
-
- CoreInput::~CoreInput() {
- }
-
- unsigned int CoreInput::getNumJoysticks() {
- return joysticks.size();
- }
-
- JoystickInfo *CoreInput::getJoystickInfoByIndex(unsigned int index) {
- if(index > joysticks.size()-1 || joysticks.size() == 0) {
- return NULL;
- }
- return &joysticks[index];
- }
-
- bool CoreInput::getJoystickButtonState(int joystickIndex, int button) {
- JoystickInfo *info = getJoystickInfoByIndex(joystickIndex);
- if(info) {
- return info->joystickButtonState[button];
- } else {
- return false;
- }
- }
-
- Number CoreInput::getJoystickAxisValue(int joystickIndex, int axis) {
- JoystickInfo *info = getJoystickInfoByIndex(joystickIndex);
- if(info) {
- return info->joystickAxisState[axis];
- } else {
- return 0.0;
- }
- }
-
- JoystickInfo *CoreInput::getJoystickInfoByID(unsigned int deviceID) {
- for(int i=0;i<joysticks.size();i++) {
- if(joysticks[i].deviceID == deviceID) {
- joysticks[i].deviceIndex = i;
- return &joysticks[i];
- }
- }
- return NULL;
- }
-
- void CoreInput::addJoystick(unsigned int deviceID) {
- JoystickInfo joystick;
- joystick.deviceID = deviceID;
- joysticks.push_back(joystick);
- InputEvent *evt = new InputEvent();
- evt->joystickDeviceID = deviceID;
- evt->joystickIndex = joysticks.size()-1;
- dispatchEvent(evt, InputEvent::EVENT_JOYDEVICE_ATTACHED);
- }
-
- void CoreInput::removeJoystick(unsigned int deviceID) {
- for(int i=0;i<joysticks.size();i++) {
- if(joysticks[i].deviceID == deviceID) {
- joysticks.erase(joysticks.begin()+i);
- InputEvent *evt = new InputEvent();
- evt->joystickDeviceID = deviceID;
- evt->joystickIndex = i;
- dispatchEvent(evt, InputEvent::EVENT_JOYDEVICE_DETACHED);
- return;
- }
- }
- }
-
- void CoreInput::joystickAxisMoved(unsigned int axisID, float value, unsigned int deviceID) {
- JoystickInfo *info = getJoystickInfoByID(deviceID);
- if(info) {
- info->joystickAxisState[axisID] = value;
- InputEvent *evt = new InputEvent();
- evt->joystickDeviceID = deviceID;
- evt->joystickAxis = axisID;
- evt->joystickAxisValue = value;
- evt->joystickIndex = info->deviceIndex;
- dispatchEvent(evt, InputEvent::EVENT_JOYAXIS_MOVED);
- }
- }
-
- void CoreInput::joystickButtonDown(unsigned int buttonID, unsigned int deviceID) {
- JoystickInfo *info = getJoystickInfoByID(deviceID);
- if(info) {
- info->joystickButtonState[buttonID] = true;
- InputEvent *evt = new InputEvent();
- evt->joystickDeviceID = deviceID;
- evt->joystickButton = buttonID;
- evt->joystickIndex = info->deviceIndex;
- dispatchEvent(evt, InputEvent::EVENT_JOYBUTTON_DOWN);
- }
- }
-
- void CoreInput::joystickButtonUp(unsigned int buttonID, unsigned int deviceID) {
- JoystickInfo *info = getJoystickInfoByID(deviceID);
- if(info) {
- info->joystickButtonState[buttonID] = false;
- InputEvent *evt = new InputEvent();
- evt->joystickDeviceID = deviceID;
- evt->joystickButton = buttonID;
- evt->joystickIndex = info->deviceIndex;
- dispatchEvent(evt, InputEvent::EVENT_JOYBUTTON_UP);
- }
- }
-
-
- bool CoreInput::getMouseButtonState(int mouseButton) {
- return mouseButtons[mouseButton];
- }
-
- void CoreInput::setMouseButtonState(int mouseButton, bool state, int ticks) {
- InputEvent *evt = new InputEvent(mousePosition, ticks);
- evt->mouseButton = mouseButton;
- if(state)
- dispatchEvent(evt, InputEvent::EVENT_MOUSEDOWN);
- else
- dispatchEvent(evt, InputEvent::EVENT_MOUSEUP);
- mouseButtons[mouseButton] = state;
-
- if(simulateTouchWithMouse) {
- TouchInfo touch;
- touch.position = mousePosition;
- touch.id = 0;
- std::vector<TouchInfo> touches;
- touches.push_back(touch);
-
- if(state) {
- touchesBegan(touch, touches, ticks);
- } else {
- touchesEnded(touch, touches, ticks);
- }
- }
- }
-
- void CoreInput::mouseWheelDown(int ticks) {
- InputEvent *evt = new InputEvent(mousePosition, ticks);
- dispatchEvent(evt, InputEvent::EVENT_MOUSEWHEEL_DOWN);
- }
-
- void CoreInput::mouseWheelUp(int ticks) {
- InputEvent *evt = new InputEvent(mousePosition, ticks);
- dispatchEvent(evt, InputEvent::EVENT_MOUSEWHEEL_UP);
- }
-
- void CoreInput::setMousePosition(int x, int y, int ticks) {
- mousePosition.x = x;
- mousePosition.y = y;
- InputEvent *evt = new InputEvent(mousePosition, ticks);
- dispatchEvent(evt, InputEvent::EVENT_MOUSEMOVE);
-
- if(simulateTouchWithMouse) {
-
-
-
- TouchInfo touch;
- touch.position = mousePosition;
- touch.id = 0;
- std::vector<TouchInfo> touches;
- touches.push_back(touch);
- /*
- if(!mouseButtons[MOUSE_BUTTON1]) {
- mouseButtons[MOUSE_BUTTON1] = true;
- touchesBegan(touch, touches, ticks);
- }
- */
- touchesMoved(touch, touches, ticks);
- }
- }
-
- Vector2 CoreInput::getMouseDelta() {
- return deltaMousePosition;
- }
-
- void CoreInput::setDeltaPosition(int x, int y) {
- deltaMousePosition.x = (Number)x;
- deltaMousePosition.y = (Number)y;
- }
- Vector2 CoreInput::getMousePosition() {
- return mousePosition;
- }
-
- bool CoreInput::getKeyState(PolyKEY keyCode) {
- if(keyCode < 512)
- return keyboardState[keyCode];
- else
- return false;
- }
-
- void CoreInput::setKeyState(PolyKEY keyCode, wchar_t code, bool newState, int ticks) {
-
- if(newState && !keyRepeat) {
- if(keyboardState[keyCode]) {
- return;
- }
- }
-
- InputEvent *evt = new InputEvent(keyCode, code, ticks);
- if(keyCode < 512)
- keyboardState[keyCode] = newState;
- if(newState) {
- dispatchEvent(evt, InputEvent::EVENT_KEYDOWN);
- } else {
- dispatchEvent(evt, InputEvent::EVENT_KEYUP);
- }
- }
-
- void CoreInput::touchesBegan(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
- if(ignoreOffScreenTouch) {
- Core *core = CoreServices::getInstance()->getCore();
- if(touch.position.x < 0 || touch.position.x > core->getXRes() || touch.position.y < 0 || touch.position.y > core->getYRes()) {
- return;
- }
- }
- InputEvent *evt = new InputEvent();
- evt->touch = touch;
- evt->touches = touches;
- evt->timestamp = ticks;
- dispatchEvent(evt, InputEvent::EVENT_TOUCHES_BEGAN);
- if(simulateMouseWithTouch) {
- mousePosition = touch.position;
- setMouseButtonState(MOUSE_BUTTON1, true, ticks);
- }
- }
-
- void CoreInput::touchesMoved(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
- if(ignoreOffScreenTouch) {
- Core *core = CoreServices::getInstance()->getCore();
- if(touch.position.x < 0 || touch.position.x > core->getXRes() || touch.position.y < 0 || touch.position.y > core->getYRes()) {
- return;
- }
- }
- InputEvent *evt = new InputEvent();
- evt->touch = touch;
- evt->touches = touches;
- evt->timestamp = ticks;
- dispatchEvent(evt, InputEvent::EVENT_TOUCHES_MOVED);
- if(simulateMouseWithTouch) {
- setMousePosition(touch.position.x, touch.position.y, ticks);
- }
- }
-
- void CoreInput::touchesEnded(TouchInfo touch, std::vector<TouchInfo> touches, int ticks) {
- if(ignoreOffScreenTouch) {
- Core *core = CoreServices::getInstance()->getCore();
- if(touch.position.x < 0 || touch.position.x > core->getXRes() || touch.position.y < 0 || touch.position.y > core->getYRes()) {
- return;
- }
- }
- InputEvent *evt = new InputEvent();
- evt->touch = touch;
- evt->touches = touches;
- evt->timestamp = ticks;
- dispatchEvent(evt, InputEvent::EVENT_TOUCHES_ENDED);
- if(simulateMouseWithTouch) {
- mousePosition = touch.position;
- setMouseButtonState(MOUSE_BUTTON1, false, ticks);
- }
- }
-
- }
|