/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #import "OpenVulkanoView.h" #include "Input/Touch/InputDeviceTouch.hpp" #include "Input/InputManager.hpp" #include #pragma mark - #pragma mark OpenVulkanoView using namespace OpenVulkano; @interface OpenVulkanoView() { CADisplayLink * m_displayLink; std::map m_touchToIdMap; Input::InputDeviceTouch m_touchDevice; } @end @implementation OpenVulkanoView /** Returns a Metal-compatible layer. */ +(Class) layerClass { return [CAMetalLayer class]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self commonInit]; } return self; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { [self commonInit]; } return self; } - (void)commonInit { [self setMultipleTouchEnabled:YES]; m_touchDevice.Init(); //TODO link window Input::InputManager::GetInstance()->RegisterInputDevice(&m_touchDevice); } - (void)dealloc { for(auto& it : m_touchToIdMap) { m_touchDevice.RemoveTouch(it.second); } Input::InputManager::GetInstance()->UnregisterInputDevice(&m_touchDevice); m_touchDevice.Close(); [super dealloc]; } - (Math::Vector2f)getTouchPosition:(UITouch*)touch { CGPoint uitouchLocation = [touch locationInView:touch.view]; return { uitouchLocation.x * self.contentScaleFactor, uitouchLocation.y * self.contentScaleFactor }; } - (void)touchesBegan:(NSSet*)inTouches withEvent:(UIEvent*)inEvent { for (UITouch* touch in inTouches) { Math::Vector2f touchLocation = [self getTouchPosition: touch]; uint32_t touchId = m_touchDevice.AddTouch(touchLocation); m_touchDevice.TouchDown(touchId); m_touchToIdMap.insert(std::make_pair(touch, touchId)); } } - (void)touchesCancelled:(NSSet*)inTouches withEvent:(UIEvent*)inEvent { for (UITouch* touch in inTouches) { Math::Vector2f touchLocation = [self getTouchPosition: touch]; auto it = m_touchToIdMap.find(touch); if (it != m_touchToIdMap.end()) { m_touchDevice.TouchUp(it->second); m_touchDevice.RemoveTouch(it->second); m_touchToIdMap.erase(it); } } } - (void)touchesEnded:(NSSet*)inTouches withEvent:(UIEvent*)inEvent { for (UITouch* touch in inTouches) { Math::Vector2f touchLocation = [self getTouchPosition: touch]; auto it = m_touchToIdMap.find(touch); if (it != m_touchToIdMap.end()) { m_touchDevice.TouchUp(it->second); m_touchDevice.RemoveTouch(it->second); m_touchToIdMap.erase(it); } } } - (void)touchesMoved:(NSSet*)inTouches withEvent:(UIEvent*)inEvent { for (UITouch* touch in inTouches) { Math::Vector2f touchLocation = [self getTouchPosition: touch]; auto it = m_touchToIdMap.find(touch); if (it != m_touchToIdMap.end()) { m_touchDevice.TouchMoved(it->second, touchLocation); } } } @end