2019-08-29 18:59:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tools/sk_app/ios/WindowContextFactory_ios.h"
|
|
|
|
#include "tools/sk_app/ios/Window_ios.h"
|
|
|
|
|
|
|
|
@interface WindowViewController : UIViewController
|
|
|
|
|
|
|
|
- (WindowViewController*)initWithWindow:(sk_app::Window_ios*)initWindow;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
using sk_app::Window;
|
|
|
|
|
|
|
|
namespace sk_app {
|
|
|
|
|
|
|
|
Window_ios* Window_ios::gWindow = nullptr;
|
|
|
|
|
|
|
|
Window* Window::CreateNativeWindow(void*) {
|
|
|
|
// already have a window
|
|
|
|
if (Window_ios::MainWindow()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Window_ios* window = new Window_ios();
|
|
|
|
if (!window->initWindow()) {
|
|
|
|
delete window;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window_ios::initWindow() {
|
|
|
|
// we already have a window
|
|
|
|
if (fWindow) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-09 20:53:39 +00:00
|
|
|
// Create a view controller to track certain events
|
2019-08-29 18:59:17 +00:00
|
|
|
WindowViewController* viewController = [[WindowViewController alloc] initWithWindow:this];
|
|
|
|
if (nil == viewController) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
|
|
|
if (nil == fWindow) {
|
|
|
|
[viewController release];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fWindow.backgroundColor = [UIColor whiteColor];
|
|
|
|
|
|
|
|
viewController.view = nil;
|
|
|
|
[fWindow setRootViewController:viewController];
|
|
|
|
[fWindow makeKeyAndVisible];
|
|
|
|
|
|
|
|
gWindow = this;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window_ios::closeWindow() {
|
|
|
|
if (nil != fWindow) {
|
|
|
|
gWindow = nullptr;
|
|
|
|
[fWindow release];
|
|
|
|
fWindow = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Window_ios::attach(BackendType attachType) {
|
|
|
|
this->initWindow();
|
|
|
|
|
|
|
|
window_context_factory::IOSWindowInfo info;
|
|
|
|
info.fWindow = this;
|
|
|
|
info.fViewController = fWindow.rootViewController;
|
|
|
|
switch (attachType) {
|
2019-09-03 13:42:57 +00:00
|
|
|
#ifdef SK_METAL
|
|
|
|
case kMetal_BackendType:
|
|
|
|
fWindowContext = MakeMetalForIOS(info, fRequestedDisplayParams);
|
|
|
|
break;
|
|
|
|
#endif
|
2020-03-19 19:54:28 +00:00
|
|
|
#ifdef SK_GL
|
2019-08-29 18:59:17 +00:00
|
|
|
case kNativeGL_BackendType:
|
|
|
|
default:
|
|
|
|
fWindowContext = MakeGLForIOS(info, fRequestedDisplayParams);
|
|
|
|
break;
|
2020-03-19 19:54:28 +00:00
|
|
|
#else
|
|
|
|
default:
|
|
|
|
#endif
|
|
|
|
case kRaster_BackendType:
|
|
|
|
fWindowContext = MakeRasterForIOS(info, fRequestedDisplayParams);
|
|
|
|
break;
|
2019-08-29 18:59:17 +00:00
|
|
|
}
|
|
|
|
this->onBackendCreated();
|
|
|
|
|
|
|
|
return (SkToBool(fWindowContext));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window_ios::PaintWindow() {
|
|
|
|
gWindow->onPaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window_ios::onInval() {
|
|
|
|
// TODO: send expose event
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sk_app
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@implementation WindowViewController {
|
|
|
|
sk_app::Window_ios* fWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (WindowViewController*)initWithWindow:(sk_app::Window_ios *)initWindow {
|
|
|
|
fWindow = initWindow;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:49:24 +00:00
|
|
|
- (void)viewWillTransitionToSize:(CGSize)size
|
|
|
|
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
|
|
|
|
// handle rotations here
|
|
|
|
}
|
2019-08-29 18:59:17 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@implementation MainView {
|
|
|
|
sk_app::Window_ios* fWindow;
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:49:24 +00:00
|
|
|
- (IBAction)panGestureAction:(UIGestureRecognizer*)sender {
|
|
|
|
CGPoint location = [sender locationInView:self];
|
|
|
|
switch (sender.state) {
|
|
|
|
case UIGestureRecognizerStateBegan:
|
|
|
|
fWindow->onMouse(location.x, location.y,
|
2019-09-09 20:53:39 +00:00
|
|
|
skui::InputState::kDown, skui::ModifierKey::kNone);
|
2019-09-05 17:49:24 +00:00
|
|
|
break;
|
|
|
|
case UIGestureRecognizerStateChanged:
|
|
|
|
fWindow->onMouse(location.x, location.y,
|
|
|
|
skui::InputState::kMove, skui::ModifierKey::kNone);
|
|
|
|
break;
|
|
|
|
case UIGestureRecognizerStateEnded:
|
|
|
|
fWindow->onMouse(location.x, location.y,
|
|
|
|
skui::InputState::kUp, skui::ModifierKey::kNone);
|
|
|
|
break;
|
|
|
|
case UIGestureRecognizerStateCancelled:
|
|
|
|
fWindow->onMouse(location.x, location.y,
|
|
|
|
skui::InputState::kUp, skui::ModifierKey::kNone);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)tapGestureAction:(UIGestureRecognizer*)sender {
|
|
|
|
CGPoint location = [sender locationInView:self];
|
|
|
|
switch (sender.state) {
|
|
|
|
case UIGestureRecognizerStateEnded:
|
|
|
|
fWindow->onMouse(location.x, location.y,
|
|
|
|
skui::InputState::kDown, skui::ModifierKey::kNone);
|
|
|
|
fWindow->onMouse(location.x, location.y,
|
|
|
|
skui::InputState::kUp, skui::ModifierKey::kNone);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 20:53:39 +00:00
|
|
|
- (IBAction)pinchGestureAction:(UIGestureRecognizer*)sender {
|
|
|
|
CGPoint location = [sender locationInView:self];
|
|
|
|
UIPinchGestureRecognizer* pinchGestureRecognizer = (UIPinchGestureRecognizer*) sender;
|
|
|
|
float scale = pinchGestureRecognizer.scale;
|
|
|
|
switch (sender.state) {
|
|
|
|
case UIGestureRecognizerStateBegan:
|
|
|
|
fWindow->onPinch(skui::InputState::kDown, scale, location.x, location.y);
|
|
|
|
break;
|
|
|
|
case UIGestureRecognizerStateChanged:
|
|
|
|
fWindow->onPinch(skui::InputState::kMove, scale, location.x, location.y);
|
|
|
|
break;
|
|
|
|
case UIGestureRecognizerStateEnded:
|
|
|
|
fWindow->onPinch(skui::InputState::kUp, scale, location.x, location.y);
|
|
|
|
break;
|
|
|
|
case UIGestureRecognizerStateCancelled:
|
|
|
|
fWindow->onPinch(skui::InputState::kUp, scale, location.x, location.y);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)swipeRightGestureAction:(UIGestureRecognizer*)sender {
|
|
|
|
if (UIGestureRecognizerStateEnded == sender.state) {
|
|
|
|
fWindow->onFling(skui::InputState::kRight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)swipeLeftGestureAction:(UIGestureRecognizer*)sender {
|
|
|
|
if (UIGestureRecognizerStateEnded == sender.state) {
|
|
|
|
fWindow->onFling(skui::InputState::kLeft);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:59:17 +00:00
|
|
|
- (MainView*)initWithWindow:(sk_app::Window_ios *)initWindow {
|
|
|
|
self = [super init];
|
|
|
|
|
2019-09-05 17:49:24 +00:00
|
|
|
UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] init];
|
2019-09-09 20:53:39 +00:00
|
|
|
panGestureRecognizer.maximumNumberOfTouches = 1;
|
2019-09-05 17:49:24 +00:00
|
|
|
[panGestureRecognizer addTarget:self action:@selector(panGestureAction:)];
|
|
|
|
[self addGestureRecognizer:panGestureRecognizer];
|
|
|
|
|
|
|
|
UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
|
|
|
|
[tapGestureRecognizer addTarget:self action:@selector(tapGestureAction:)];
|
|
|
|
[self addGestureRecognizer:tapGestureRecognizer];
|
|
|
|
|
2019-09-09 20:53:39 +00:00
|
|
|
UIPinchGestureRecognizer* pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] init];
|
|
|
|
[pinchGestureRecognizer addTarget:self action:@selector(pinchGestureAction:)];
|
|
|
|
[self addGestureRecognizer:pinchGestureRecognizer];
|
|
|
|
|
|
|
|
UISwipeGestureRecognizer* swipeRightGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
|
|
|
|
swipeRightGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
|
|
|
|
[swipeRightGestureRecognizer addTarget:self action:@selector(swipeRightGestureAction:)];
|
|
|
|
[self addGestureRecognizer:swipeRightGestureRecognizer];
|
|
|
|
|
|
|
|
UISwipeGestureRecognizer* swipeLeftGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
|
|
|
|
swipeLeftGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
|
|
|
|
[swipeLeftGestureRecognizer addTarget:self action:@selector(swipeLeftGestureAction:)];
|
|
|
|
[self addGestureRecognizer:swipeLeftGestureRecognizer];
|
|
|
|
|
|
|
|
// disable pan recognition until swipes fail
|
|
|
|
[panGestureRecognizer requireGestureRecognizerToFail:swipeLeftGestureRecognizer];
|
|
|
|
[panGestureRecognizer requireGestureRecognizerToFail:swipeRightGestureRecognizer];
|
|
|
|
|
2019-08-29 18:59:17 +00:00
|
|
|
fWindow = initWindow;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|