11#import <QuartzCore/QuartzCore.h>
12#import <MetalKit/MetalKit.h>
13#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
15#include "IGraphicsIOS.h"
16#include "IGraphicsCoreText.h"
18#import "IGraphicsIOS_view.h"
24#pragma clang diagnostic ignored "-Wdeprecated-declarations"
27BEGIN_IGRAPHICS_NAMESPACE
30void GetScreenDimensions(
int& width,
int& height)
32 CGRect bounds = [[UIScreen mainScreen] bounds];
33 width = bounds.size.width;
34 height = bounds.size.height;
37float GetScaleForScreen(
int plugWidth,
int plugHeight)
40 GetScreenDimensions(width, height);
41 return std::min((
float) width / (
float) plugWidth, (
float) height / (
float) plugHeight);
44float GetScaleForScreen(
int plugWidth,
int plugHeight)
50END_IGRAPHICS_NAMESPACE
54using namespace igraphics;
56StaticStorage<CoreTextFontDescriptor> sFontDescriptorCache;
60std::map<std::string, MTLTexturePtr> gTextureMap;
61NSArray<id<MTLTexture>>* gTextures;
63IGraphicsIOS::IGraphicsIOS(
IGEditorDelegate& dlg,
int w,
int h,
int fps,
float scale)
64: IGRAPHICS_DRAW_CLASS(dlg, w, h, fps, scale)
67#if defined IGRAPHICS_METAL && !defined IGRAPHICS_SKIA
68 if(!gTextureMap.size())
70 NSBundle* pBundle = [NSBundle mainBundle];
72 if(IsOOPAuv3AppExtension())
73 pBundle = [NSBundle bundleWithPath: [[[pBundle bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]];
75 NSArray<NSURL*>* pTextureFiles = [pBundle URLsForResourcesWithExtension:
@"ktx" subdirectory:
@""];
77 if ([pTextureFiles count])
79 MTKTextureLoader* textureLoader = [[MTKTextureLoader alloc] initWithDevice:MTLCreateSystemDefaultDevice()];
81 NSError* pError = nil;
82 NSDictionary* textureOptions = @{ MTKTextureLoaderOptionSRGB: [NSNumber numberWithBool:NO] };
84 gTextures = [textureLoader newTexturesWithContentsOfURLs:pTextureFiles options:textureOptions error:&pError];
86 for(
int i=0; i < gTextures.count; i++)
88 gTextureMap.insert(std::make_pair([[[pTextureFiles[i] lastPathComponent] stringByDeletingPathExtension] UTF8String], (MTLTexturePtr) gTextures[i]));
91 DBGMSG(
"Preloaded %i textures\n", (
int) [pTextureFiles count]);
93 [textureLoader release];
100IGraphicsIOS::~IGraphicsIOS()
105void* IGraphicsIOS::OpenWindow(
void* pParent)
109 IGRAPHICS_VIEW* view = [[IGRAPHICS_VIEW alloc] initWithIGraphics:
this];
110 mView = (
void*) view;
112#ifdef IGRAPHICS_ACCESSKIT
113 auto pAccessibility = std::make_unique<IGraphicsAccessibilityIOS>(*
this, view);
114 view->mAccessibility = pAccessibility.get();
115 mAccessibility = std::move(pAccessibility);
119 OnViewInitialized((
void*) [view layer]);
122 CGFloat scale = [UIScreen mainScreen].scale;
127 SetScreenScale(scale);
129 GetDelegate()->LayoutUI(
this);
130 GetDelegate()->OnUIOpen();
132 [view setMultipleTouchEnabled:MultiTouchEnabled()];
136 [(UIView*) pParent addSubview: view];
142void IGraphicsIOS::CloseWindow()
146 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
147#ifdef IGRAPHICS_ACCESSKIT
148 pView->mAccessibility =
nullptr;
149 mAccessibility =
nullptr;
151 [pView removeFromSuperview];
159bool IGraphicsIOS::WindowIsOpen()
164void IGraphicsIOS::PlatformResize(
bool parentHasResized)
168 CGRect r = CGRectMake(0., 0.,
static_cast<CGFloat
>(WindowWidth()),
static_cast<CGFloat
>(WindowHeight()));
169 [(IGRAPHICS_VIEW*) mView setFrame: r ];
173void IGraphicsIOS::AttachPlatformView(
const IRECT& r,
void* pView)
175 IGRAPHICS_VIEW* pMainView = (IGRAPHICS_VIEW*) mView;
177 UIView* pNewSubView = (UIView*) pView;
178 [pNewSubView setFrame:ToCGRect(
this, r)];
180 [pMainView addSubview:pNewSubView];
183void IGraphicsIOS::RemovePlatformView(
void* pView)
185 [(UIView*) pView removeFromSuperview];
188void IGraphicsIOS::HidePlatformView(
void* pView,
bool hide)
190 [(UIView*) pView setHidden:hide];
193EMsgBoxResult IGraphicsIOS::ShowMessageBox(
const char* str,
const char* title, EMsgBoxType type, IMsgBoxCompletionHandlerFunc completionHandler)
195 ReleaseMouseCapture();
196 [(IGRAPHICS_VIEW*) mView showMessageBox:str : title : type : completionHandler];
197 return EMsgBoxResult::kNoResult;
200void IGraphicsIOS::AttachGestureRecognizer(EGestureType type)
203 [(IGRAPHICS_VIEW*) mView attachGestureRecognizer:type];
206void IGraphicsIOS::ForceEndUserEdit()
210 [(IGRAPHICS_VIEW*) mView endUserInput];
214const char* IGraphicsIOS::GetPlatformAPIStr()
219void IGraphicsIOS::GetMouseLocation(
float& x,
float&y)
const
221 [(IGRAPHICS_VIEW*) mView getLastTouchLocation: x : y];
224void IGraphicsIOS::PromptForFile(WDL_String& fileName, WDL_String& path, EFileAction action,
const char* ext, IFileDialogCompletionHandlerFunc completionHandler)
226 assert(completionHandler !=
nullptr &&
"You must provide a completion handler on iOS");
228 NSString* pDefaultFileName = nil;
229 NSString* pDefaultPath = nil;
230 NSMutableArray* pFileTypes = [[NSMutableArray alloc] init];
232 if (fileName.GetLength())
233 pDefaultFileName = [NSString stringWithUTF8String:fileName.Get()];
235 pDefaultFileName =
@"";
237 if (path.GetLength())
238 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
244 if (CStringHasContents(ext))
246 NSArray* pFileExtensions = [[NSString stringWithUTF8String:ext] componentsSeparatedByString:
@" "];
248 for (NSString* pFileExtension in pFileExtensions)
250 UTType* pUTType = [UTType typeWithFilenameExtension:pFileExtension];
251 [pFileTypes addObject:pUTType];
255 [(IGRAPHICS_VIEW*) mView promptForFile: pDefaultFileName : pDefaultPath : action : pFileTypes : completionHandler];
258void IGraphicsIOS::PromptForDirectory(WDL_String& path, IFileDialogCompletionHandlerFunc completionHandler)
260 assert(completionHandler !=
nullptr &&
"You must provide a completion handler on iOS");
262 NSString* pDefaultPath = nil;
264 if (path.GetLength())
265 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
271 [(IGRAPHICS_VIEW*) mView promptForDirectory:pDefaultPath : completionHandler];
274bool IGraphicsIOS::PromptForColor(
IColor& color,
const char* str, IColorPickerHandlerFunc func)
276 [(IGRAPHICS_VIEW*) mView promptForColor: color: str: func];
286 CGRect areaRect = ToCGRect(
this, bounds);
287 pReturnMenu = [(IGRAPHICS_VIEW*) mView createPopupMenu: menu: areaRect];
291 if(pReturnMenu && pReturnMenu->GetFunction())
292 pReturnMenu->ExecFunction();
297void IGraphicsIOS::CreatePlatformTextEntry(
int paramIdx,
const IText& text,
const IRECT& bounds,
int length,
const char* str)
299 ReleaseMouseCapture();
300 CGRect areaRect = ToCGRect(
this, bounds);
301 [(IGRAPHICS_VIEW*) mView createTextEntry: paramIdx : text: str: length: areaRect];
304bool IGraphicsIOS::OpenURL(
const char* url,
const char* msgWindowTitle,
const char* confirmMsg,
const char* errMsgOnFailure)
306 NSURL* pNSURL =
nullptr;
307 if (strstr(url,
"http"))
308 pNSURL = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
310 pNSURL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:url]];
314 UIResponder* pResponder = (UIResponder*) mView;
316 if ([pResponder respondsToSelector:
@selector(openURL:)])
317 [pResponder performSelector:
@selector(openURL:) withObject: pNSURL];
319 pResponder = [pResponder nextResponder];
326void* IGraphicsIOS::GetWindow()
335int IGraphicsIOS::GetUserOSVersion()
340bool IGraphicsIOS::GetTextFromClipboard(WDL_String& str)
345bool IGraphicsIOS::SetTextInClipboard(
const char* str)
350PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fileNameOrResID)
352 return CoreTextHelpers::LoadPlatformFont(fontID, fileNameOrResID, GetBundleID());
355PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fontName, ETextStyle style)
357 return CoreTextHelpers::LoadPlatformFont(fontID, fontName, style);
360PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
void* pData,
int dataSize)
362 return CoreTextHelpers::LoadPlatformFont(fontID, pData, dataSize);
365void IGraphicsIOS::CachePlatformFont(
const char* fontID,
const PlatformFontPtr& font)
367 CoreTextHelpers::CachePlatformFont(fontID, font, sFontDescriptorCache);
370void IGraphicsIOS::LaunchBluetoothMidiDialog(
float x,
float y)
372 ReleaseMouseCapture();
373 NSDictionary* dic = @{
@"x": @(x),
@"y": @(y)};
374 [[NSNotificationCenter defaultCenter] postNotificationName:
@"LaunchBTMidiDialog" object:nil userInfo:dic];
377EUIAppearance IGraphicsIOS::GetUIAppearance()
const
379 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
383 return [[pView traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark ? EUIAppearance::Dark
384 : EUIAppearance::Light;
388 return EUIAppearance::Light;
392void IGraphicsIOS::ActivateGLContext()
394 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
395 [pView activateGLContext];
398void IGraphicsIOS::DeactivateGLContext()
400 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
401 [pView deactivateGLContext];
405#if defined IGRAPHICS_NANOVG
406 #include "IGraphicsNanoVG.cpp"
407#elif defined IGRAPHICS_SKIA
408 #include "IGraphicsSkia.cpp"
410 #error Either NO_IGRAPHICS or one and only one choice of graphics library must be defined!
An editor delegate base class that uses IGraphics for the UI.
virtual void AttachGestureRecognizer(EGestureType type)
Registers a gesture recognizer with the graphics context.
Used to manage color data, independent of draw class/platform.
Used to manage a rectangular area, independent of draw class/platform.
IText is used to manage font and text/text entry style for a piece of text on the UI,...