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;
113 OnViewInitialized((
void*) [view layer]);
116 CGFloat scale = [UIScreen mainScreen].scale;
121 SetScreenScale(scale);
123 GetDelegate()->LayoutUI(
this);
124 GetDelegate()->OnUIOpen();
126 [view setMultipleTouchEnabled:MultiTouchEnabled()];
130 [(UIView*) pParent addSubview: view];
136void IGraphicsIOS::CloseWindow()
140 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
141 [pView removeFromSuperview];
149bool IGraphicsIOS::WindowIsOpen()
154void IGraphicsIOS::PlatformResize(
bool parentHasResized)
158 CGRect r = CGRectMake(0., 0.,
static_cast<CGFloat
>(WindowWidth()),
static_cast<CGFloat
>(WindowHeight()));
159 [(IGRAPHICS_VIEW*) mView setFrame: r ];
163void IGraphicsIOS::AttachPlatformView(
const IRECT& r,
void* pView)
165 IGRAPHICS_VIEW* pMainView = (IGRAPHICS_VIEW*) mView;
167 UIView* pNewSubView = (UIView*) pView;
168 [pNewSubView setFrame:ToCGRect(
this, r)];
170 [pMainView addSubview:pNewSubView];
173void IGraphicsIOS::RemovePlatformView(
void* pView)
175 [(UIView*) pView removeFromSuperview];
178void IGraphicsIOS::HidePlatformView(
void* pView,
bool hide)
180 [(UIView*) pView setHidden:hide];
183EMsgBoxResult IGraphicsIOS::ShowMessageBox(
const char* str,
const char* title, EMsgBoxType type, IMsgBoxCompletionHandlerFunc completionHandler)
185 ReleaseMouseCapture();
186 [(IGRAPHICS_VIEW*) mView showMessageBox:str : title : type : completionHandler];
187 return EMsgBoxResult::kNoResult;
190void IGraphicsIOS::AttachGestureRecognizer(EGestureType type)
193 [(IGRAPHICS_VIEW*) mView attachGestureRecognizer:type];
196void IGraphicsIOS::ForceEndUserEdit()
200 [(IGRAPHICS_VIEW*) mView endUserInput];
204const char* IGraphicsIOS::GetPlatformAPIStr()
209void IGraphicsIOS::GetMouseLocation(
float& x,
float&y)
const
211 [(IGRAPHICS_VIEW*) mView getLastTouchLocation: x : y];
214void IGraphicsIOS::PromptForFile(WDL_String& fileName, WDL_String& path, EFileAction action,
const char* ext, IFileDialogCompletionHandlerFunc completionHandler)
216 assert(completionHandler !=
nullptr &&
"You must provide a completion handler on iOS");
218 NSString* pDefaultFileName = nil;
219 NSString* pDefaultPath = nil;
220 NSMutableArray* pFileTypes = [[NSMutableArray alloc] init];
222 if (fileName.GetLength())
223 pDefaultFileName = [NSString stringWithUTF8String:fileName.Get()];
225 pDefaultFileName =
@"";
227 if (path.GetLength())
228 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
234 if (CStringHasContents(ext))
236 NSArray* pFileExtensions = [[NSString stringWithUTF8String:ext] componentsSeparatedByString:
@" "];
238 for (NSString* pFileExtension in pFileExtensions)
240 UTType* pUTType = [UTType typeWithFilenameExtension:pFileExtension];
241 [pFileTypes addObject:pUTType];
245 [(IGRAPHICS_VIEW*) mView promptForFile: pDefaultFileName : pDefaultPath : action : pFileTypes : completionHandler];
248void IGraphicsIOS::PromptForDirectory(WDL_String& path, IFileDialogCompletionHandlerFunc completionHandler)
250 assert(completionHandler !=
nullptr &&
"You must provide a completion handler on iOS");
252 NSString* pDefaultPath = nil;
254 if (path.GetLength())
255 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
261 [(IGRAPHICS_VIEW*) mView promptForDirectory:pDefaultPath : completionHandler];
264bool IGraphicsIOS::PromptForColor(
IColor& color,
const char* str, IColorPickerHandlerFunc func)
266 [(IGRAPHICS_VIEW*) mView promptForColor: color: str: func];
276 CGRect areaRect = ToCGRect(
this, bounds);
277 pReturnMenu = [(IGRAPHICS_VIEW*) mView createPopupMenu: menu: areaRect];
281 if(pReturnMenu && pReturnMenu->GetFunction())
282 pReturnMenu->ExecFunction();
287void IGraphicsIOS::CreatePlatformTextEntry(
int paramIdx,
const IText& text,
const IRECT& bounds,
int length,
const char* str)
289 ReleaseMouseCapture();
290 CGRect areaRect = ToCGRect(
this, bounds);
291 [(IGRAPHICS_VIEW*) mView createTextEntry: paramIdx : text: str: length: areaRect];
294bool IGraphicsIOS::OpenURL(
const char* url,
const char* msgWindowTitle,
const char* confirmMsg,
const char* errMsgOnFailure)
296 NSURL* pNSURL =
nullptr;
297 if (strstr(url,
"http"))
298 pNSURL = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
300 pNSURL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:url]];
304 UIResponder* pResponder = (UIResponder*) mView;
306 if ([pResponder respondsToSelector:
@selector(openURL:)])
307 [pResponder performSelector:
@selector(openURL:) withObject: pNSURL];
309 pResponder = [pResponder nextResponder];
316void* IGraphicsIOS::GetWindow()
325int IGraphicsIOS::GetUserOSVersion()
330bool IGraphicsIOS::GetTextFromClipboard(WDL_String& str)
335bool IGraphicsIOS::SetTextInClipboard(
const char* str)
340PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fileNameOrResID)
342 return CoreTextHelpers::LoadPlatformFont(fontID, fileNameOrResID, GetBundleID());
345PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fontName, ETextStyle style)
347 return CoreTextHelpers::LoadPlatformFont(fontID, fontName, style);
350PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
void* pData,
int dataSize)
352 return CoreTextHelpers::LoadPlatformFont(fontID, pData, dataSize);
355void IGraphicsIOS::CachePlatformFont(
const char* fontID,
const PlatformFontPtr& font)
357 CoreTextHelpers::CachePlatformFont(fontID, font, sFontDescriptorCache);
360void IGraphicsIOS::LaunchBluetoothMidiDialog(
float x,
float y)
362 ReleaseMouseCapture();
363 NSDictionary* dic = @{
@"x": @(x),
@"y": @(y)};
364 [[NSNotificationCenter defaultCenter] postNotificationName:
@"LaunchBTMidiDialog" object:nil userInfo:dic];
367EUIAppearance IGraphicsIOS::GetUIAppearance()
const
369 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
373 return [[pView traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark ? EUIAppearance::Dark
374 : EUIAppearance::Light;
378 return EUIAppearance::Light;
382void IGraphicsIOS::ActivateGLContext()
384 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
385 [pView activateGLContext];
388void IGraphicsIOS::DeactivateGLContext()
390 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
391 [pView deactivateGLContext];
395#if defined IGRAPHICS_NANOVG
396 #include "IGraphicsNanoVG.cpp"
397#elif defined IGRAPHICS_SKIA
398 #include "IGraphicsSkia.cpp"
400 #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,...