Add Touch input handling
This commit is contained in:
17
openVulkanoCpp/Host/iOS/OpenVulkanoView.h
Normal file
17
openVulkanoCpp/Host/iOS/OpenVulkanoView.h
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <MetalKit/MTKView.h>
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark OpenVulkanoView
|
||||
|
||||
//@interface OpenVulkanoView : UIView
|
||||
@interface OpenVulkanoView : MTKView
|
||||
@end
|
||||
125
openVulkanoCpp/Host/iOS/OpenVulkanoView.mm
Normal file
125
openVulkanoCpp/Host/iOS/OpenVulkanoView.mm
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* 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 <map>
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark OpenVulkanoView
|
||||
|
||||
using namespace OpenVulkano;
|
||||
|
||||
@interface OpenVulkanoView()
|
||||
{
|
||||
CADisplayLink * m_displayLink;
|
||||
std::map<UITouch*, int32_t> 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
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <MetalKit/MTKView.h>
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark OpenVulkanoViewController
|
||||
@@ -14,10 +13,3 @@
|
||||
-(void*) makeGraphicsApp;
|
||||
@end
|
||||
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark OpenVulkanoView
|
||||
|
||||
@interface OpenVulkanoView : MTKView
|
||||
@end
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "ExampleApps/CubesExampleApp.hpp"
|
||||
#include "Base/UI/IVulkanWindow.hpp"
|
||||
|
||||
#import <MetalKit/MTKView.h>
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark OpenVulkanoViewController
|
||||
@@ -203,13 +204,3 @@ public:
|
||||
@end
|
||||
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark OpenVulkanoView
|
||||
|
||||
@implementation OpenVulkanoView
|
||||
|
||||
/** Returns a Metal-compatible layer. */
|
||||
+(Class) layerClass { return [CAMetalLayer class]; }
|
||||
|
||||
@end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user