Files
cocos4/native/cocos/platform/ios/View.mm
T
2020-10-30 16:15:47 +08:00

115 lines
3.4 KiB
Plaintext

/****************************************************************************
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
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.
****************************************************************************/
#import "View.h"
#include "bindings/event/EventDispatcher.h"
#include "platform/Application.h"
namespace {
void dispatchEvents(cc::TouchEvent &touchEvent, NSSet *touches) {
for (UITouch *touch in touches) {
touchEvent.touches.push_back({static_cast<float>([touch locationInView:[touch view]].x),
static_cast<float>([touch locationInView:[touch view]].y),
static_cast<int>((intptr_t)touch)});
}
cc::EventDispatcher::dispatchTouchEvent(touchEvent);
touchEvent.touches.clear();
}
}
@implementation View {
cc::TouchEvent _touchEvent;
}
@synthesize preventTouch;
#ifndef CC_USE_METAL
+ (Class)layerClass {
return [CAEAGLLayer class];
}
#endif
- (id)initWithFrame:(CGRect)frame {
#ifdef CC_USE_METAL
if (self = [super initWithFrame:frame device:MTLCreateSystemDefaultDevice()]) {
self.framebufferOnly = YES;
self.delegate = self;
self.preventTouch = FALSE;
}
#else
if (self = [super initWithFrame:frame]) {
self.preventTouch = FALSE;
}
#endif
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.preventTouch)
return;
_touchEvent.type = cc::TouchEvent::Type::BEGAN;
dispatchEvents(_touchEvent, touches);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.preventTouch)
return;
_touchEvent.type = cc::TouchEvent::Type::MOVED;
dispatchEvents(_touchEvent, touches);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.preventTouch)
return;
_touchEvent.type = cc::TouchEvent::Type::ENDED;
dispatchEvents(_touchEvent, touches);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.preventTouch)
return;
_touchEvent.type = cc::TouchEvent::Type::CANCELLED;
dispatchEvents(_touchEvent, touches);
}
- (void)setPreventTouchEvent:(BOOL)flag {
self.preventTouch = flag;
}
#ifdef CC_USE_METAL
- (void)drawInMTKView:(nonnull MTKView *)view {
cc::Application::getInstance()->tick();
}
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size {
//TODO
}
#endif
@end