11#if !__has_feature(objc_arc)
12#error This file must be compiled with Arc. Use -fobjc-arc flag
15#import <QuartzCore/QuartzCore.h>
16#import <Metal/Metal.h>
17#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
19#import "IGraphicsIOS_view.h"
21#include "IGraphicsCoreText.h"
25extern StaticStorage<CoreTextFontDescriptor> sFontDescriptorCache;
27@implementation IGRAPHICS_UITABLEVC
29- (int) menuIndexFromIndexPath: (NSIndexPath*) indexPath
31 return [
self.items[indexPath.row][1] intValue];
37 self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
38 self.tableView.dataSource = self;
39 self.tableView.delegate = self;
40 self.tableView.scrollEnabled = YES;
41 self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
42 self.items = [[NSMutableArray alloc] init];
44 int numItems = mMenu->NItems();
46 NSMutableString* elementTitle;
48 for (
int i = 0; i < numItems; ++i)
52 elementTitle = [[NSMutableString alloc] initWithUTF8String: pMenuItem->GetText()];
54 if (mMenu->GetPrefix())
56 NSString* prefixString = nil;
58 switch (mMenu->GetPrefix())
60 case 1: prefixString = [NSString stringWithFormat:
@"%1d: ", i+1]; break;
61 case 2: prefixString = [NSString stringWithFormat:
@"%02d: ", i+1]; break;
62 case 3: prefixString = [NSString stringWithFormat:
@"%03d: ", i+1]; break;
65 prefixString = [NSString stringWithUTF8String:
""]; break;
68 [elementTitle insertString:prefixString atIndex:0];
71 [
self.items addObject: @[elementTitle, [NSNumber numberWithInt:i]]];
74 [
self.view addSubview:self.tableView];
81 mGraphics = pGraphics;
87- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection : (NSInteger) section
89 return self.items.count;
92- (NSInteger) numberOfSectionsInTableView: (UITableView*) tableView
97- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
99 static NSString *identifer =
@"cell";
100 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
104 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
107 cell.textLabel.text = [NSString stringWithFormat:@"%@", self.items[indexPath.row][0]];
109 IPopupMenu::Item* pItem = mMenu->GetItem([self menuIndexFromIndexPath:indexPath]);
111 if (pItem->GetChecked())
112 cell.accessoryType = UITableViewCellAccessoryCheckmark;
114 cell.accessoryType = pItem->GetSubmenu() ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
116 cell.textLabel.enabled = cell.userInteractionEnabled = pItem->GetEnabled();
121- (CGFloat) tableView:(UITableView*) tableView heightForRowAtIndexPath: (NSIndexPath*) indexPath
124 IPopupMenu::Item* pItem = mMenu->GetItem([self menuIndexFromIndexPath:indexPath]);
126 if (pItem->GetIsSeparator())
129 return self.tableView.rowHeight;
132- (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
134 IPopupMenu::Item* pItem = mMenu->GetItem([self menuIndexFromIndexPath:indexPath]);
136 if (pItem->GetIsSeparator())
139 return self.tableView.rowHeight;
142- (void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(NSIndexPath*) indexPath
144 int menuIdx = [
self menuIndexFromIndexPath:indexPath];
151 IGRAPHICS_UITABLEVC* newViewController = [[IGRAPHICS_UITABLEVC alloc] initWithIPopupMenuAndIGraphics: pSubMenu : mGraphics];
152 [newViewController setTitle:[NSString stringWithUTF8String:CStringHasContents(pSubMenu->GetRootTitle()) ? pSubMenu->GetRootTitle() : pItem->GetText()]];
153 [
self.navigationController pushViewController:newViewController animated:YES];
158 if (pItem->GetIsChoosable())
160 [self dismissViewControllerAnimated:YES completion:nil];
162 mMenu->SetChosenItemIdx(menuIdx);
164 if (mMenu->GetFunction())
165 mMenu->ExecFunction();
167 mGraphics->SetControlValueAfterPopupMenu(mMenu);
171- (CGSize) preferredContentSize
173 if (self.presentingViewController && self.tableView != nil)
175 CGSize tempSize = self.presentingViewController.view.bounds.size;
176 tempSize.width = 300;
177 CGSize size = [self.tableView sizeThatFits:tempSize];
180 return [super preferredContentSize];
184- (void) setPreferredContentSize:(CGSize)preferredContentSize
186 super.preferredContentSize = preferredContentSize;
189- (void) tableView: (UITableView *) tableView commitEditingStyle: (UITableViewCellEditingStyle) editingStyle forRowAtIndexPath: (NSIndexPath*) indexPath
191 if (editingStyle == UITableViewCellEditingStyleDelete)
193 mGraphics->DeleteFromPopupMenu(mMenu, [self menuIndexFromIndexPath:indexPath]);
194 [
self.items removeObjectAtIndex:indexPath.row];
195 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
199- (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath*) indexPath
201 return mMenu->GetItem([self menuIndexFromIndexPath:indexPath])->GetIsDeletable();
206#ifdef IGRAPHICS_ACCESSKIT
207IGraphicsAccessibilityIOS::IGraphicsAccessibilityIOS(
IGraphics& graphics, UIView* pView)
208: IGraphicsAccessibility(graphics)
210 mAdapter = accesskit_ios_adapter_new((__bridge
void*) pView, ActivationHandler,
this, ActionHandler,
this, DeactivationHandler,
this);
213IGraphicsAccessibilityIOS::~IGraphicsAccessibilityIOS()
215 accesskit_ios_adapter_free(mAdapter);
218void IGraphicsAccessibilityIOS::DeactivationHandler(
void* pUserData)
221 static_cast<IGraphicsAccessibilityIOS*
>(pUserData)->SetInactive();
224void IGraphicsAccessibilityIOS::UpdateIfActive(accesskit_tree_update_factory factory,
void* pUserData)
226 if (accesskit_ios_queued_events* pEvents = accesskit_ios_adapter_update_if_active(mAdapter, factory, pUserData))
227 accesskit_ios_queued_events_raise(pEvents);
230void IGraphicsAccessibilityIOS::ViewDidAppear()
232 if (accesskit_ios_queued_events* pEvents = accesskit_ios_adapter_view_did_appear(mAdapter))
233 accesskit_ios_queued_events_raise(pEvents);
236bool IGraphicsAccessibilityIOS::IsAccessibilityElement()
238 return accesskit_ios_adapter_is_accessibility_element(mAdapter);
241NSArray* IGraphicsAccessibilityIOS::GetElements()
243 return (__bridge NSArray*) accesskit_ios_adapter_accessibility_elements(mAdapter);
246id IGraphicsAccessibilityIOS::HitTest(CGPoint point)
248 return (__bridge
id) accesskit_ios_adapter_hit_test(mAdapter, point.x, point.y);
252@implementation IGRAPHICS_VIEW
258 mGraphics = pGraphics;
259 CGRect r = CGRectMake(0.f, 0.f, (
float) pGraphics->WindowWidth(), (
float) pGraphics->WindowHeight());
260 self = [
super initWithFrame:r];
265 CGFloat scale = [UIScreen mainScreen].scale;
268 self.layer.frame = self.frame;
269 self.layer.opaque = YES;
270 self.layer.contentsScale = scale;
271 self.contentScaleFactor = scale;
273 CAMetalLayer* mtlLayer = (CAMetalLayer*) self.layer;
274 mtlLayer.framebufferOnly = YES;
275 mtlLayer.device = MTLCreateSystemDefaultDevice();
277#if defined IGRAPHICS_GL
278 EGLDisplay display = eglGetPlatformDisplay(EGLenum(EGL_PLATFORM_ANGLE_ANGLE), 0, 0);
281 DBGMSG(
"eglGetPlatformDisplay() returned error %i", eglGetError());
284 if (eglInitialize(display, nil, nil) == 0) {
285 DBGMSG(
"eglInitialize() returned error %i", eglGetError());
288 const EGLint configAttribs[9] = {
294 EGLint numConfigs = 0;
295 EGLConfig configs[1];
297 if (eglChooseConfig(display, configAttribs, configs, 1, &numConfigs) == 0) {
298 DBGMSG(
"eglChooseConfig() returned error %i", eglGetError());
302 DBGMSG(
"Empty config returned in eglChooseConfig()");
305#if defined IGRAPHICS_GLES2
306 const EGLint contextAttribs [5] = {
307 EGL_CONTEXT_MAJOR_VERSION, 2,
308 EGL_CONTEXT_MINOR_VERSION, 0,
311#elif defined IGRAPHICS_GLES3
312 const EGLint contextAttribs [5] = {
313 EGL_CONTEXT_MAJOR_VERSION, 3,
314 EGL_CONTEXT_MINOR_VERSION, 0,
319 EGLContext context = eglCreateContext(display, configs[0],
nullptr, contextAttribs);
322 DBGMSG(
"eglCreateContext() returned error %d", eglGetError());
325 EGLSurface surface = eglCreateWindowSurface(display, configs[0], (__bridge EGLNativeWindowType) [self layer],
nullptr);
328 DBGMSG(
"eglCreateWindowSurface() returned error %d", eglGetError());
331 mEGLSurface = surface;
332 mEGLDisplay = display;
333 mEGLContext = context;
336 self.multipleTouchEnabled = NO;
338 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackgroundNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
339 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForegroundNotification:) name:UIApplicationWillEnterForegroundNotification object:nil];
340 mColorPickerHandlerFunc =
nullptr;
342 UIHoverGestureRecognizer* hoverGestureRecognizer =
343 [[UIHoverGestureRecognizer alloc] initWithTarget:self action:@selector(onHoverGesture:)];
344 [
self addGestureRecognizer: hoverGestureRecognizer];
349- (void) setFrame:(CGRect) frame
351 [
super setFrame:frame];
356 CGFloat scale = [UIScreen mainScreen].scale;
358 scale = self.window.screen.scale;
362 [CATransaction begin];
363 [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
364 [
self.layer setFrame:frame];
367 CAMetalLayer* mtlLayer = (CAMetalLayer*) self.layer;
368 CGSize drawableSize = self.bounds.size;
369 drawableSize.width *= scale;
370 drawableSize.height *= scale;
371 mtlLayer.drawableSize = drawableSize;
373 [CATransaction commit];
376- (void) onTouchEvent:(ETouchEvent) eventType withTouches:(NSSet*) touches withEvent:(UIEvent*) event
378 if(mGraphics ==
nullptr)
381 NSEnumerator* pEnumerator = [[event allTouches] objectEnumerator];
384 std::vector<IMouseInfo> points;
386 while ((pTouch = [pEnumerator nextObject]))
388 CGPoint pos = [pTouch locationInView:pTouch.view];
392 auto ds = mGraphics->GetDrawScale();
395 point.ms.touchID =
reinterpret_cast<ITouchID
>(pTouch);
396 point.ms.touchRadius = [pTouch majorRadius];
398 point.x = pos.x / ds;
399 point.y = pos.y / ds;
400 CGPoint posPrev = [pTouch previousLocationInView: self];
401 point.dX = (pos.x - posPrev.x) / ds;
402 point.dY = (pos.y - posPrev.y) / ds;
404 if([touches containsObject:pTouch])
408 points.push_back(point);
414 if(eventType == ETouchEvent::Began)
415 mGraphics->OnMouseDown(points);
417 if(eventType == ETouchEvent::Moved)
418 mGraphics->OnMouseDrag(points);
420 if(eventType == ETouchEvent::Ended)
421 mGraphics->OnMouseUp(points);
423 if(eventType == ETouchEvent::Cancelled)
424 mGraphics->OnTouchCancelled(points);
427- (void) touchesBegan:(NSSet*) touches withEvent:(UIEvent*) event
429 [
self onTouchEvent:ETouchEvent::Began withTouches:touches withEvent:event];
432- (void) touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event
434 [
self onTouchEvent:ETouchEvent::Moved withTouches:touches withEvent:event];
437- (void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event
439 [
self onTouchEvent:ETouchEvent::Ended withTouches:touches withEvent:event];
442- (void) touchesCancelled:(NSSet*) touches withEvent:(UIEvent*) event
444 [
self onTouchEvent:ETouchEvent::Cancelled withTouches:touches withEvent:event];
449 return CAMetalLayer.class;
452- (void) didMoveToSuperview
454 [
super didMoveToSuperview];
457 self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(redraw:)];
458 [
self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
459 self.displayLink.preferredFramesPerSecond = mGraphics->FPS();
463 [
self.displayLink invalidate];
464 self.displayLink = nil;
468#ifdef IGRAPHICS_ACCESSKIT
469- (void) didMoveToWindow
471 [
super didMoveToWindow];
473 if (self.window && mAccessibility)
474 mAccessibility->ViewDidAppear();
478- (BOOL) isAccessibilityElement
480 return mAccessibility ? mAccessibility->IsAccessibilityElement() : NO;
483- (NSArray*) accessibilityElements
485 return mAccessibility ? mAccessibility->GetElements() : nil;
488- (id) accessibilityHitTest:(CGPoint) point
490 return mAccessibility ? mAccessibility->HitTest(point) : nil;
494- (void) drawRect:(CGRect)rect
500 mGraphics->SetPlatformContext(UIGraphicsGetCurrentContext());
503 if (mGraphics->IsDirty(rects))
505 mGraphics->SetAllControlsClean();
506 mGraphics->Draw(rects);
512- (void) redraw:(CADisplayLink*) displayLink
515 [
self setNeedsDisplay];
517 [
self drawRect:CGRect()];
526- (BOOL) acceptsFirstResponder
531- (BOOL) canBecomeFirstResponder
536- (void) removeFromSuperview
538 [
self.displayLink invalidate];
539 self.displayLink = nil;
542#ifdef IGRAPHICS_ACCESSKIT
543 mAccessibility =
nullptr;
545 mMenuTableController = nil;
546 mMenuNavigationController = nil;
548#if defined IGRAPHICS_GL
549 if (mEGLDisplay != EGL_NO_DISPLAY)
551 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
552 if (mEGLSurface != EGL_NO_SURFACE)
553 eglDestroySurface(mEGLDisplay, mEGLSurface);
554 if (mEGLContext != EGL_NO_CONTEXT)
555 eglDestroyContext(mEGLDisplay, mEGLContext);
556 eglTerminate(mEGLDisplay);
558 mEGLDisplay = EGL_NO_DISPLAY;
559 mEGLSurface = EGL_NO_SURFACE;
560 mEGLContext = EGL_NO_CONTEXT;
564- (BOOL) textFieldShouldReturn:(UITextField*) textField
566 if (textField == mTextField)
568 mGraphics->SetControlValueAfterTextEdit([[mTextField text] UTF8String]);
569 mGraphics->SetAllControlsDirty();
576- (BOOL) textField:(UITextField*) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*) string
582 NSString* proposedText = [mTextField.text stringByReplacingCharactersInRange:range withString:string];
584 if (proposedText.length > mTextFieldLength)
587 IControl* pInTextEntry = mGraphics->GetControlInTextEntry();
595 NSMutableCharacterSet *characterSet = [[NSMutableCharacterSet alloc] init];
597 switch ( pParam->
Type() )
599 case IParam::kTypeEnum:
600 case IParam::kTypeInt:
601 case IParam::kTypeBool:
602 [characterSet addCharactersInString:@"0123456789-+"];
604 case IParam::kTypeDouble:
605 [characterSet addCharactersInString:@"0123456789.-+"];
611 if ([
string rangeOfCharacterFromSet:characterSet.invertedSet].location != NSNotFound)
619- (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController:(UIPresentationController*) controller
621 return UIModalPresentationNone;
624- (BOOL) presentationControllerShouldDismiss:(UIPopoverPresentationController*) popoverPresentationController
631 mMenuTableController = [[IGRAPHICS_UITABLEVC alloc] initWithIPopupMenuAndIGraphics:&menu : mGraphics];
632 [mMenuTableController setTitle: [NSString stringWithUTF8String:menu.GetRootTitle()]];
634 mMenuNavigationController = [[UINavigationController alloc] initWithRootViewController:mMenuTableController];
636 mMenuNavigationController.modalPresentationStyle = UIModalPresentationPopover;
637 mMenuNavigationController.popoverPresentationController.sourceView = self;
638 mMenuNavigationController.popoverPresentationController.sourceRect = bounds;
640 mMenuNavigationController.popoverPresentationController.delegate = self;
642 [
self.window.rootViewController presentViewController:mMenuNavigationController animated:YES completion:nil];
647- (void) createTextEntry: (
int) paramIdx : (const
IText&) text : (const
char*) str : (
int) length : (CGRect) areaRect
652 mAlertController = [UIAlertController alertControllerWithTitle:@"Input a value:" message:@"" preferredStyle:UIAlertControllerStyleAlert];
654 __weak IGRAPHICS_VIEW* weakSelf = self;
656 void (^cancelHandler)(UIAlertAction*) = ^(UIAlertAction *action)
658 __strong IGRAPHICS_VIEW* strongSelf = weakSelf;
659 strongSelf->mGraphics->SetAllControlsDirty();
660 [strongSelf endUserInput];
663 UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:cancelHandler];
664 [mAlertController addAction:cancelAction];
666 void (^okHandler)(UIAlertAction*) = ^(UIAlertAction *action)
668 __strong IGRAPHICS_VIEW* strongSelf = weakSelf;
669 strongSelf->mGraphics->SetControlValueAfterTextEdit([[strongSelf->mTextField text] UTF8String]);
670 strongSelf->mGraphics->SetAllControlsDirty();
671 [strongSelf endUserInput];
674 UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:okHandler];
675 [mAlertController addAction:okAction];
676 [mAlertController setPreferredAction:okAction];
678 [mAlertController addTextFieldWithConfigurationHandler:^(UITextField* aTextField) {
679 __strong IGRAPHICS_VIEW* strongSelf = weakSelf;
680 strongSelf->mTextField = aTextField;
681 strongSelf->mTextFieldLength = length;
682 aTextField.delegate = strongSelf;
683 [aTextField setText:[NSString stringWithUTF8String:str]];
685 [
self.window.rootViewController presentViewController:mAlertController animated:YES completion:nil];
690 [
self becomeFirstResponder];
691 [
self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
692 [mTextField setDelegate: nil];
693 mAlertController =
nullptr;
694 mTextField =
nullptr;
695 mGraphics->ClearInTextEntryControl();
698- (void) showMessageBox: (const
char*) str : (const
char*) title : (EMsgBoxType) type : (IMsgBoxCompletionHandlerFunc) completionHandler
702 NSString* message = [NSString stringWithUTF8String:str];
703 NSString* titleNs = [NSString stringWithUTF8String:title];
705 UIAlertController* alertController = [UIAlertController alertControllerWithTitle:titleNs message:message preferredStyle:UIAlertControllerStyleAlert];
707 void (^handlerBlock)(UIAlertAction*) =
708 ^(UIAlertAction* action) {
710 if (completionHandler !=
nullptr)
712 EMsgBoxResult result = EMsgBoxResult::kCANCEL;
714 if ([action.title isEqualToString:
@"OK"])
715 result = EMsgBoxResult::kOK;
716 if ([action.title isEqualToString:
@"Cancel"])
717 result = EMsgBoxResult::kCANCEL;
718 if ([action.title isEqualToString:
@"Yes"])
719 result = EMsgBoxResult::kYES;
720 if ([action.title isEqualToString:
@"No"])
721 result = EMsgBoxResult::kNO;
722 if ([action.title isEqualToString:
@"Retry"])
723 result = EMsgBoxResult::kRETRY;
725 completionHandler(result);
730 if (type == kMB_OK || type == kMB_OKCANCEL)
732 UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:handlerBlock];
733 [alertController addAction:okAction];
736 if (type == kMB_YESNO || type == kMB_YESNOCANCEL)
738 UIAlertAction* yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:handlerBlock];
739 [alertController addAction:yesAction];
741 UIAlertAction* noAction = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:handlerBlock];
742 [alertController addAction:noAction];
745 if (type == kMB_RETRYCANCEL)
747 UIAlertAction* retryAction = [UIAlertAction actionWithTitle:@"Retry" style:UIAlertActionStyleDefault handler:handlerBlock];
748 [alertController addAction:retryAction];
751 if (type == kMB_OKCANCEL || type == kMB_YESNOCANCEL || type == kMB_RETRYCANCEL)
753 UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:handlerBlock];
754 [alertController addAction:cancelAction];
757 [[NSOperationQueue mainQueue] addOperationWithBlock:^{
758 [
self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
762- (void) promptForFile: (NSString*) fileName : (NSString*) path : (EFileAction) action : (NSArray*) contentTypes : (IFileDialogCompletionHandlerFunc) completionHandler
766 mFileDialogFunc = completionHandler;
768 UIDocumentPickerViewController* vc = NULL;
769 NSURL* url = [[NSURL alloc] initFileURLWithPath:path];
771 if (action == EFileAction::Open)
773 vc = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:contentTypes asCopy:YES];
774 [vc setDirectoryURL:url];
778 vc = [[UIDocumentPickerViewController alloc] initForExportingURLs:@[url]];
781 [vc setDelegate:self];
783 [
self.window.rootViewController presentViewController:vc animated:YES completion:nil];
786- (void) promptForDirectory: (NSString*) path : (IFileDialogCompletionHandlerFunc) completionHandler
790 mFileDialogFunc = completionHandler;
792 UIDocumentPickerViewController* vc = NULL;
793 NSURL* url = [[NSURL alloc] initFileURLWithPath:path];
795 NSMutableArray* pFileTypes = [[NSMutableArray alloc] init];
796 UTType* directoryType = [UTType typeWithIdentifier:@"public.folder"];
797 [pFileTypes addObject:directoryType];
799 vc = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:pFileTypes];
800 [vc setDirectoryURL:url];
802 [vc setDelegate:self];
804 [
self.window.rootViewController presentViewController:vc animated:YES completion:nil];
807- (BOOL) promptForColor: (
IColor&) color : (const
char*) str : (IColorPickerHandlerFunc) func
811 UIColorPickerViewController* colorSelectionController = [[UIColorPickerViewController alloc] init];
813 colorSelectionController.modalPresentationStyle = UIModalPresentationPopover;
814 colorSelectionController.popoverPresentationController.delegate = self;
815 colorSelectionController.popoverPresentationController.sourceView = self;
818 mGraphics->GetMouseLocation(x, y);
819 colorSelectionController.popoverPresentationController.sourceRect = CGRectMake(x, y, 1, 1);
821 colorSelectionController.delegate = self;
822 colorSelectionController.selectedColor = ToUIColor(color);
823 colorSelectionController.supportsAlpha = YES;
825 mColorPickerHandlerFunc = func;
827 [
self.window.rootViewController presentViewController:colorSelectionController animated:YES completion:nil];
832- (void) attachGestureRecognizer: (EGestureType) type
834 UIGestureRecognizer* gestureRecognizer;
838 case EGestureType::DoubleTap:
839 case EGestureType::TripleTap:
841 gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapGesture:)];
842 [(UITapGestureRecognizer*) gestureRecognizer setNumberOfTapsRequired: type == EGestureType::DoubleTap ? 2 : 3];
843 [(UITapGestureRecognizer*) gestureRecognizer setNumberOfTouchesRequired:1];
846 case EGestureType::LongPress1:
847 case EGestureType::LongPress2:
849 gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPressGesture:)];
850 [(UILongPressGestureRecognizer*) gestureRecognizer setNumberOfTouchesRequired: type == EGestureType::LongPress1 ? 1 : 2];
853 case EGestureType::SwipeLeft:
855 gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeGesture:)];
856 [(UISwipeGestureRecognizer*) gestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
859 case EGestureType::SwipeRight:
861 gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeGesture:)];
862 [(UISwipeGestureRecognizer*) gestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
865 case EGestureType::SwipeUp:
867 gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeGesture:)];
868 [(UISwipeGestureRecognizer*) gestureRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
871 case EGestureType::SwipeDown:
873 gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(onSwipeGesture:)];
874 [(UISwipeGestureRecognizer*) gestureRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];
877 case EGestureType::Pinch:
879 gestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(onPinchGesture:)];
882 case EGestureType::Rotate:
884 gestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotateGesture:)];
891 gestureRecognizer.delegate = self;
892 gestureRecognizer.cancelsTouchesInView = YES;
893 gestureRecognizer.delaysTouchesBegan = YES;
894 [
self addGestureRecognizer:gestureRecognizer];
897- (void) onTapGesture: (UITapGestureRecognizer*) recognizer
899 CGPoint p = [recognizer locationInView:self];
900 auto ds = mGraphics->GetDrawScale();
904 info.type = recognizer.numberOfTapsRequired == 2 ? EGestureType::DoubleTap : EGestureType::TripleTap;
906 mGraphics->OnGestureRecognized(info);
909- (void) onLongPressGesture: (UILongPressGestureRecognizer*) recognizer
911 CGPoint p = [recognizer locationInView:self];
912 auto ds = mGraphics->GetDrawScale();
916 if(recognizer.state == UIGestureRecognizerStateBegan)
917 info.state = EGestureState::Began;
918 else if(recognizer.state == UIGestureRecognizerStateChanged)
919 info.state = EGestureState::InProcess;
920 else if(recognizer.state == UIGestureRecognizerStateEnded)
921 info.state = EGestureState::Ended;
923 info.type = recognizer.numberOfTouchesRequired == 1 ? EGestureType::LongPress1 : EGestureType::LongPress2;
925 mGraphics->OnGestureRecognized(info);
928- (void) onSwipeGesture: (UISwipeGestureRecognizer*) recognizer
930 CGPoint p = [recognizer locationInView:self];
931 auto ds = mGraphics->GetDrawScale();
936 switch (recognizer.direction) {
937 case UISwipeGestureRecognizerDirectionLeft: info.type = EGestureType::SwipeLeft;
break;
938 case UISwipeGestureRecognizerDirectionRight: info.type = EGestureType::SwipeRight;
break;
939 case UISwipeGestureRecognizerDirectionUp: info.type = EGestureType::SwipeUp;
break;
940 case UISwipeGestureRecognizerDirectionDown: info.type = EGestureType::SwipeDown;
break;
945 mGraphics->OnGestureRecognized(info);
948- (void) onPinchGesture: (UIPinchGestureRecognizer*) recognizer
950 CGPoint p = [recognizer locationInView:self];
951 auto ds = mGraphics->GetDrawScale();
955 info.velocity = recognizer.velocity;
956 info.scale = recognizer.scale;
958 if(recognizer.state == UIGestureRecognizerStateBegan)
959 info.state = EGestureState::Began;
960 else if(recognizer.state == UIGestureRecognizerStateChanged)
961 info.state = EGestureState::InProcess;
962 else if(recognizer.state == UIGestureRecognizerStateEnded)
963 info.state = EGestureState::Ended;
965 info.type = EGestureType::Pinch;
967 mGraphics->OnGestureRecognized(info);
970- (void) onRotateGesture: (UIRotationGestureRecognizer*) recognizer
972 CGPoint p = [recognizer locationInView:self];
973 auto ds = mGraphics->GetDrawScale();
977 info.velocity = recognizer.velocity;
978 info.angle = RadToDeg(recognizer.rotation);
980 if(recognizer.state == UIGestureRecognizerStateBegan)
981 info.state = EGestureState::Began;
982 else if(recognizer.state == UIGestureRecognizerStateChanged)
983 info.state = EGestureState::InProcess;
984 else if(recognizer.state == UIGestureRecognizerStateEnded)
985 info.state = EGestureState::Ended;
987 info.type = EGestureType::Rotate;
989 mGraphics->OnGestureRecognized(info);
992- (void) onHoverGesture: (UIHoverGestureRecognizer*) recognizer
994 CGPoint pos = [recognizer locationInView:self];
998 auto ds = mGraphics->GetDrawScale();
1000 info.y = pos.y / ds;
1003 mGraphics->OnMouseOver(info.x, info.y, info.ms);
1006-(BOOL) gestureRecognizer:(UIGestureRecognizer*) gestureRecognizer shouldReceiveTouch:(UITouch*) touch
1008 CGPoint pos = [touch locationInView:touch.view];
1012 auto ds = mGraphics->GetDrawScale();
1014 if (mGraphics->RespondsToGesture(pos.x / ds, pos.y / ds))
1023- (void) applicationDidEnterBackgroundNotification:(NSNotification*) notification
1025 [
self.displayLink setPaused:YES];
1028- (void) applicationWillEnterForegroundNotification:(NSNotification*) notification
1030 [
self.displayLink setPaused:NO];
1033- (BOOL) delaysContentTouches
1038- (void) presentationControllerDidDismiss: (UIPresentationController*) presentationController
1040 mGraphics->SetControlValueAfterPopupMenu(
nullptr);
1043- (void) documentPicker:(UIDocumentPickerViewController*) controller didPickDocumentsAtURLs:(NSArray <NSURL*>*) urls
1045 WDL_String fileName, path;
1047 if (urls.count == 1)
1049 NSURL* pSource = urls[0];
1050 NSString* pFullPath = [pSource path];
1051 fileName.Set([pFullPath UTF8String]);
1053 NSString* pTruncatedPath = [pFullPath stringByDeletingLastPathComponent];
1057 path.Set([pTruncatedPath UTF8String]);
1061 if (mFileDialogFunc)
1062 mFileDialogFunc(fileName, path);
1067 if (mFileDialogFunc)
1068 mFileDialogFunc(fileName, path);
1072- (void) documentPickerWasCancelled:(UIDocumentPickerViewController*) controller
1074 WDL_String fileName, path;
1076 if (mFileDialogFunc)
1077 mFileDialogFunc(fileName, path);
1080- (void) colorPickerViewControllerDidSelectColor:(UIColorPickerViewController*) viewController;
1082 if (mColorPickerHandlerFunc)
1084 IColor c = FromUIColor([viewController selectedColor]);
1085 mColorPickerHandlerFunc(c);
1089- (void) colorPickerViewControllerDidFinish:(UIColorPickerViewController*) viewController;
1091 mColorPickerHandlerFunc =
nullptr;
1094- (void) traitCollectionDidChange: (UITraitCollection*) previousTraitCollection
1096 [
super traitCollectionDidChange: previousTraitCollection];
1100 mGraphics->OnAppearanceChanged([self.traitCollection userInterfaceStyle] == UIUserInterfaceStyleDark ? EUIAppearance::Dark
1101 : EUIAppearance::Light);
1105- (void) getLastTouchLocation: (
float&) x : (
float&) y
1107 const float scale = mGraphics->GetDrawScale();
1112- (void) activateGLContext
1115 if (mEGLDisplay != EGL_NO_DISPLAY && mEGLContext != EGL_NO_CONTEXT)
1116 eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext);
1120- (void) deactivateGLContext
1123 if (mEGLDisplay != EGL_NO_DISPLAY)
1124 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1131 if (mEGLDisplay != EGL_NO_DISPLAY && mEGLSurface != EGL_NO_SURFACE)
1132 eglSwapBuffers(mEGLDisplay, mEGLSurface);
This file contains the base IControl implementation, along with some base classes for specific types ...
The lowest level base class of an IGraphics control.
const IParam * GetParam(int valIdx=0) const
Get a const pointer to the IParam object (owned by the editor delegate class), associated with this c...
The lowest level base class of an IGraphics context.
IGraphics platform class for IOS.
EParamType Type() const
Get the parameter's type.
Used to manage a list of rectangular areas and optimize them for drawing to the screen.
Used to describe a particular gesture.
Used to group mouse coordinates with mouse modifier information.
Used to manage color data, independent of draw class/platform.
IText is used to manage font and text/text entry style for a piece of text on the UI,...