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
29void GetScreenDimensions(
int& width,
int& height)
31 CGRect bounds = [[UIScreen mainScreen] bounds];
32 width = bounds.size.width;
33 height = bounds.size.height;
36float GetScaleForScreen(
int plugWidth,
int plugHeight)
39 GetScreenDimensions(width, height);
40 return std::min((
float) width / (
float) plugWidth, (
float) height / (
float) plugHeight);
43END_IGRAPHICS_NAMESPACE
47using namespace igraphics;
49StaticStorage<CoreTextFontDescriptor> sFontDescriptorCache;
53std::map<std::string, MTLTexturePtr> gTextureMap;
54NSArray<id<MTLTexture>>* gTextures;
56IGraphicsIOS::IGraphicsIOS(
IGEditorDelegate& dlg,
int w,
int h,
int fps,
float scale)
57: IGRAPHICS_DRAW_CLASS(dlg, w, h, fps, scale)
60#if defined IGRAPHICS_METAL && !defined IGRAPHICS_SKIA
61 if(!gTextureMap.size())
63 NSBundle* pBundle = [NSBundle mainBundle];
65 if(IsOOPAuv3AppExtension())
66 pBundle = [NSBundle bundleWithPath: [[[pBundle bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]];
68 NSArray<NSURL*>* pTextureFiles = [pBundle URLsForResourcesWithExtension:
@"ktx" subdirectory:
@""];
70 if ([pTextureFiles count])
72 MTKTextureLoader* textureLoader = [[MTKTextureLoader alloc] initWithDevice:MTLCreateSystemDefaultDevice()];
74 NSError* pError = nil;
75 NSDictionary* textureOptions = @{ MTKTextureLoaderOptionSRGB: [NSNumber numberWithBool:NO] };
77 gTextures = [textureLoader newTexturesWithContentsOfURLs:pTextureFiles options:textureOptions error:&pError];
79 for(
int i=0; i < gTextures.count; i++)
81 gTextureMap.insert(std::make_pair([[[pTextureFiles[i] lastPathComponent] stringByDeletingPathExtension] UTF8String], (MTLTexturePtr) gTextures[i]));
84 DBGMSG(
"Preloaded %i textures\n", (
int) [pTextureFiles count]);
86 [textureLoader release];
93IGraphicsIOS::~IGraphicsIOS()
98void* IGraphicsIOS::OpenWindow(
void* pParent)
102 IGRAPHICS_VIEW* view = [[IGRAPHICS_VIEW alloc] initWithIGraphics:
this];
103 mView = (
void*) view;
105 OnViewInitialized((
void*) [view metalLayer]);
107 SetScreenScale([UIScreen mainScreen].scale);
109 GetDelegate()->LayoutUI(
this);
110 GetDelegate()->OnUIOpen();
112 [view setMultipleTouchEnabled:MultiTouchEnabled()];
116 [(UIView*) pParent addSubview: view];
122void IGraphicsIOS::CloseWindow()
126 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
127 [pView removeFromSuperview];
135bool IGraphicsIOS::WindowIsOpen()
140void IGraphicsIOS::PlatformResize(
bool parentHasResized)
144 CGRect r = CGRectMake(0., 0.,
static_cast<CGFloat
>(WindowWidth()),
static_cast<CGFloat
>(WindowHeight()));
145 [(IGRAPHICS_VIEW*) mView setFrame: r ];
149void IGraphicsIOS::AttachPlatformView(
const IRECT& r,
void* pView)
151 IGRAPHICS_VIEW* pMainView = (IGRAPHICS_VIEW*) mView;
153 UIView* pNewSubView = (UIView*) pView;
154 [pNewSubView setFrame:ToCGRect(
this, r)];
156 [pMainView addSubview:pNewSubView];
159void IGraphicsIOS::RemovePlatformView(
void* pView)
161 [(UIView*) pView removeFromSuperview];
164void IGraphicsIOS::HidePlatformView(
void* pView,
bool hide)
166 [(UIView*) pView setHidden:hide];
169EMsgBoxResult IGraphicsIOS::ShowMessageBox(
const char* str,
const char* title, EMsgBoxType type, IMsgBoxCompletionHandlerFunc completionHandler)
171 ReleaseMouseCapture();
172 [(IGRAPHICS_VIEW*) mView showMessageBox:str : title : type : completionHandler];
173 return EMsgBoxResult::kNoResult;
176void IGraphicsIOS::AttachGestureRecognizer(EGestureType type)
179 [(IGRAPHICS_VIEW*) mView attachGestureRecognizer:type];
182void IGraphicsIOS::ForceEndUserEdit()
186 [(IGRAPHICS_VIEW*) mView endUserInput];
190const char* IGraphicsIOS::GetPlatformAPIStr()
195void IGraphicsIOS::GetMouseLocation(
float& x,
float&y)
const
197 [(IGRAPHICS_VIEW*) mView getLastTouchLocation: x : y];
200void IGraphicsIOS::PromptForFile(WDL_String& fileName, WDL_String& path, EFileAction action,
const char* ext, IFileDialogCompletionHandlerFunc completionHandler)
202 assert(completionHandler !=
nullptr &&
"You must provide a completion handler on iOS");
204 NSString* pDefaultFileName = nil;
205 NSString* pDefaultPath = nil;
206 NSMutableArray* pFileTypes = [[NSMutableArray alloc] init];
208 if (fileName.GetLength())
209 pDefaultFileName = [NSString stringWithUTF8String:fileName.Get()];
211 pDefaultFileName =
@"";
213 if (path.GetLength())
214 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
220 if (CStringHasContents(ext))
222 NSArray* pFileExtensions = [[NSString stringWithUTF8String:ext] componentsSeparatedByString:
@" "];
224 for (NSString* pFileExtension in pFileExtensions)
226 UTType* pUTType = [UTType typeWithFilenameExtension:pFileExtension];
227 [pFileTypes addObject:pUTType];
231 [(IGRAPHICS_VIEW*) mView promptForFile: pDefaultFileName : pDefaultPath : action : pFileTypes : completionHandler];
234void IGraphicsIOS::PromptForDirectory(WDL_String& path, IFileDialogCompletionHandlerFunc completionHandler)
236 assert(completionHandler !=
nullptr &&
"You must provide a completion handler on iOS");
238 NSString* pDefaultPath = nil;
240 if (path.GetLength())
241 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
247 [(IGRAPHICS_VIEW*) mView promptForDirectory:pDefaultPath : completionHandler];
250bool IGraphicsIOS::PromptForColor(
IColor& color,
const char* str, IColorPickerHandlerFunc func)
252 [(IGRAPHICS_VIEW*) mView promptForColor: color: str: func];
262 CGRect areaRect = ToCGRect(
this, bounds);
263 pReturnMenu = [(IGRAPHICS_VIEW*) mView createPopupMenu: menu: areaRect];
267 if(pReturnMenu && pReturnMenu->GetFunction())
268 pReturnMenu->ExecFunction();
273void IGraphicsIOS::CreatePlatformTextEntry(
int paramIdx,
const IText& text,
const IRECT& bounds,
int length,
const char* str)
275 ReleaseMouseCapture();
276 CGRect areaRect = ToCGRect(
this, bounds);
277 [(IGRAPHICS_VIEW*) mView createTextEntry: paramIdx : text: str: length: areaRect];
280bool IGraphicsIOS::OpenURL(
const char* url,
const char* msgWindowTitle,
const char* confirmMsg,
const char* errMsgOnFailure)
282 NSURL* pNSURL =
nullptr;
283 if (strstr(url,
"http"))
284 pNSURL = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
286 pNSURL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:url]];
290 UIResponder* pResponder = (UIResponder*) mView;
292 if ([pResponder respondsToSelector:
@selector(openURL:)])
293 [pResponder performSelector:
@selector(openURL:) withObject: pNSURL];
295 pResponder = [pResponder nextResponder];
302void* IGraphicsIOS::GetWindow()
311int IGraphicsIOS::GetUserOSVersion()
316bool IGraphicsIOS::GetTextFromClipboard(WDL_String& str)
321bool IGraphicsIOS::SetTextInClipboard(
const char* str)
326PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fileNameOrResID)
328 return CoreTextHelpers::LoadPlatformFont(fontID, fileNameOrResID, GetBundleID());
331PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fontName, ETextStyle style)
333 return CoreTextHelpers::LoadPlatformFont(fontID, fontName, style);
336PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
void* pData,
int dataSize)
338 return CoreTextHelpers::LoadPlatformFont(fontID, pData, dataSize);
341void IGraphicsIOS::CachePlatformFont(
const char* fontID,
const PlatformFontPtr& font)
343 CoreTextHelpers::CachePlatformFont(fontID, font, sFontDescriptorCache);
346void IGraphicsIOS::LaunchBluetoothMidiDialog(
float x,
float y)
348 ReleaseMouseCapture();
349 NSDictionary* dic = @{
@"x": @(x),
@"y": @(y)};
350 [[NSNotificationCenter defaultCenter] postNotificationName:
@"LaunchBTMidiDialog" object:nil userInfo:dic];
353EUIAppearance IGraphicsIOS::GetUIAppearance()
const
355 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
359 return [[pView traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark ? EUIAppearance::Dark
360 : EUIAppearance::Light;
364 return EUIAppearance::Light;
368#if defined IGRAPHICS_NANOVG
369 #include "IGraphicsNanoVG.cpp"
370#elif defined IGRAPHICS_SKIA
371 #include "IGraphicsSkia.cpp"
373 #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,...