iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IGraphicsIOS.mm
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#import <QuartzCore/QuartzCore.h>
12#import <MetalKit/MetalKit.h>
13#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
14
15#include "IGraphicsIOS.h"
16#include "IGraphicsCoreText.h"
17
18#import "IGraphicsIOS_view.h"
19
20#include <map>
21#include <string>
22#include <cassert>
23
24#pragma clang diagnostic ignored "-Wdeprecated-declarations"
25
26BEGIN_IPLUG_NAMESPACE
27BEGIN_IGRAPHICS_NAMESPACE
28
29#if !TARGET_OS_VISION
30void GetScreenDimensions(int& width, int& height)
31{
32 CGRect bounds = [[UIScreen mainScreen] bounds];
33 width = bounds.size.width;
34 height = bounds.size.height;
35}
36
37float GetScaleForScreen(int plugWidth, int plugHeight)
38{
39 int width, height;
40 GetScreenDimensions(width, height);
41 return std::min((float) width / (float) plugWidth, (float) height / (float) plugHeight);
42}
43#else
44float GetScaleForScreen(int plugWidth, int plugHeight)
45{
46 return 1.0;
47}
48#endif
49
50END_IGRAPHICS_NAMESPACE
51END_IPLUG_NAMESPACE
52
53using namespace iplug;
54using namespace igraphics;
55
56StaticStorage<CoreTextFontDescriptor> sFontDescriptorCache;
57
58#pragma mark -
59
60std::map<std::string, MTLTexturePtr> gTextureMap;
61NSArray<id<MTLTexture>>* gTextures;
62
63IGraphicsIOS::IGraphicsIOS(IGEditorDelegate& dlg, int w, int h, int fps, float scale)
64: IGRAPHICS_DRAW_CLASS(dlg, w, h, fps, scale)
65{
66
67#if defined IGRAPHICS_METAL && !defined IGRAPHICS_SKIA
68 if(!gTextureMap.size())
69 {
70 NSBundle* pBundle = [NSBundle mainBundle];
71
72 if(IsOOPAuv3AppExtension())
73 pBundle = [NSBundle bundleWithPath: [[[pBundle bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]];
74
75 NSArray<NSURL*>* pTextureFiles = [pBundle URLsForResourcesWithExtension:@"ktx" subdirectory:@""];
76
77 if ([pTextureFiles count])
78 {
79 MTKTextureLoader* textureLoader = [[MTKTextureLoader alloc] initWithDevice:MTLCreateSystemDefaultDevice()];
80
81 NSError* pError = nil;
82 NSDictionary* textureOptions = @{ MTKTextureLoaderOptionSRGB: [NSNumber numberWithBool:NO] };
83
84 gTextures = [textureLoader newTexturesWithContentsOfURLs:pTextureFiles options:textureOptions error:&pError];
85
86 for(int i=0; i < gTextures.count; i++)
87 {
88 gTextureMap.insert(std::make_pair([[[pTextureFiles[i] lastPathComponent] stringByDeletingPathExtension] UTF8String], (MTLTexturePtr) gTextures[i]));
89 }
90
91 DBGMSG("Preloaded %i textures\n", (int) [pTextureFiles count]);
92
93 [textureLoader release];
94 textureLoader = nil;
95 }
96 }
97#endif
98}
99
100IGraphicsIOS::~IGraphicsIOS()
101{
102 CloseWindow();
103}
104
105void* IGraphicsIOS::OpenWindow(void* pParent)
106{
107 TRACE
108 CloseWindow();
109 IGRAPHICS_VIEW* view = [[IGRAPHICS_VIEW alloc] initWithIGraphics: this];
110 mView = (void*) view;
111
112#ifdef IGRAPHICS_ACCESSKIT
113 auto pAccessibility = std::make_unique<IGraphicsAccessibilityIOS>(*this, view);
114 view->mAccessibility = pAccessibility.get();
115 mAccessibility = std::move(pAccessibility);
116#endif
117
118 IGraphics::ScopedGLContext scopedGLContext{this};
119 OnViewInitialized((void*) [view layer]);
120
121#if !TARGET_OS_VISION
122 CGFloat scale = [UIScreen mainScreen].scale;
123#else
124 CGFloat scale = 2.0;
125#endif
126
127 SetScreenScale(scale);
128
129 GetDelegate()->LayoutUI(this);
130 GetDelegate()->OnUIOpen();
131
132 [view setMultipleTouchEnabled:MultiTouchEnabled()];
133
134 if (pParent)
135 {
136 [(UIView*) pParent addSubview: view];
137 }
138
139 return mView;
140}
141
142void IGraphicsIOS::CloseWindow()
143{
144 if (mView)
145 {
146 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
147#ifdef IGRAPHICS_ACCESSKIT
148 pView->mAccessibility = nullptr;
149 mAccessibility = nullptr;
150#endif
151 [pView removeFromSuperview];
152 [pView release];
153 mView = nullptr;
154
155 OnViewDestroyed();
156 }
157}
158
159bool IGraphicsIOS::WindowIsOpen()
160{
161 return mView;
162}
163
164void IGraphicsIOS::PlatformResize(bool parentHasResized)
165{
166 if (mView)
167 {
168 CGRect r = CGRectMake(0., 0., static_cast<CGFloat>(WindowWidth()), static_cast<CGFloat>(WindowHeight()));
169 [(IGRAPHICS_VIEW*) mView setFrame: r ];
170 }
171}
172
173void IGraphicsIOS::AttachPlatformView(const IRECT& r, void* pView)
174{
175 IGRAPHICS_VIEW* pMainView = (IGRAPHICS_VIEW*) mView;
176
177 UIView* pNewSubView = (UIView*) pView;
178 [pNewSubView setFrame:ToCGRect(this, r)];
179
180 [pMainView addSubview:pNewSubView];
181}
182
183void IGraphicsIOS::RemovePlatformView(void* pView)
184{
185 [(UIView*) pView removeFromSuperview];
186}
187
188void IGraphicsIOS::HidePlatformView(void* pView, bool hide)
189{
190 [(UIView*) pView setHidden:hide];
191}
192
193EMsgBoxResult IGraphicsIOS::ShowMessageBox(const char* str, const char* title, EMsgBoxType type, IMsgBoxCompletionHandlerFunc completionHandler)
194{
195 ReleaseMouseCapture();
196 [(IGRAPHICS_VIEW*) mView showMessageBox:str : title : type : completionHandler];
197 return EMsgBoxResult::kNoResult; // we need to rely on completionHandler
198}
199
200void IGraphicsIOS::AttachGestureRecognizer(EGestureType type)
201{
203 [(IGRAPHICS_VIEW*) mView attachGestureRecognizer:type];
204}
205
206void IGraphicsIOS::ForceEndUserEdit()
207{
208 if (mView)
209 {
210 [(IGRAPHICS_VIEW*) mView endUserInput];
211 }
212}
213
214const char* IGraphicsIOS::GetPlatformAPIStr()
215{
216 return "iOS";
217}
218
219void IGraphicsIOS::GetMouseLocation(float& x, float&y) const
220{
221 [(IGRAPHICS_VIEW*) mView getLastTouchLocation: x : y];
222}
223
224void IGraphicsIOS::PromptForFile(WDL_String& fileName, WDL_String& path, EFileAction action, const char* ext, IFileDialogCompletionHandlerFunc completionHandler)
225{
226 assert(completionHandler != nullptr && "You must provide a completion handler on iOS");
227
228 NSString* pDefaultFileName = nil;
229 NSString* pDefaultPath = nil;
230 NSMutableArray* pFileTypes = [[NSMutableArray alloc] init];
231
232 if (fileName.GetLength())
233 pDefaultFileName = [NSString stringWithUTF8String:fileName.Get()];
234 else
235 pDefaultFileName = @"";
236
237 if (path.GetLength())
238 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
239 else
240 pDefaultPath = @"";
241
242 fileName.Set(""); // reset it
243
244 if (CStringHasContents(ext))
245 {
246 NSArray* pFileExtensions = [[NSString stringWithUTF8String:ext] componentsSeparatedByString: @" "];
247
248 for (NSString* pFileExtension in pFileExtensions)
249 {
250 UTType* pUTType = [UTType typeWithFilenameExtension:pFileExtension];
251 [pFileTypes addObject:pUTType];
252 }
253 }
254
255 [(IGRAPHICS_VIEW*) mView promptForFile: pDefaultFileName : pDefaultPath : action : pFileTypes : completionHandler];
256}
257
258void IGraphicsIOS::PromptForDirectory(WDL_String& path, IFileDialogCompletionHandlerFunc completionHandler)
259{
260 assert(completionHandler != nullptr && "You must provide a completion handler on iOS");
261
262 NSString* pDefaultPath = nil;
263
264 if (path.GetLength())
265 pDefaultPath = [NSString stringWithUTF8String:path.Get()];
266 else
267 pDefaultPath = @"";
268
269 path.Set(""); // reset it
270
271 [(IGRAPHICS_VIEW*) mView promptForDirectory:pDefaultPath : completionHandler];
272}
273
274bool IGraphicsIOS::PromptForColor(IColor& color, const char* str, IColorPickerHandlerFunc func)
275{
276 [(IGRAPHICS_VIEW*) mView promptForColor: color: str: func];
277 return false;
278}
279
280IPopupMenu* IGraphicsIOS::CreatePlatformPopupMenu(IPopupMenu& menu, const IRECT bounds, bool& isAsync)
281{
282 IPopupMenu* pReturnMenu = nullptr;
283 isAsync = true;
284 if (mView)
285 {
286 CGRect areaRect = ToCGRect(this, bounds);
287 pReturnMenu = [(IGRAPHICS_VIEW*) mView createPopupMenu: menu: areaRect];
288 }
289
290 //synchronous
291 if(pReturnMenu && pReturnMenu->GetFunction())
292 pReturnMenu->ExecFunction();
293
294 return pReturnMenu;
295}
296
297void IGraphicsIOS::CreatePlatformTextEntry(int paramIdx, const IText& text, const IRECT& bounds, int length, const char* str)
298{
299 ReleaseMouseCapture();
300 CGRect areaRect = ToCGRect(this, bounds);
301 [(IGRAPHICS_VIEW*) mView createTextEntry: paramIdx : text: str: length: areaRect];
302}
303
304bool IGraphicsIOS::OpenURL(const char* url, const char* msgWindowTitle, const char* confirmMsg, const char* errMsgOnFailure)
305{
306 NSURL* pNSURL = nullptr;
307 if (strstr(url, "http"))
308 pNSURL = [NSURL URLWithString:[NSString stringWithUTF8String:url]];
309 else
310 pNSURL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:url]];
311
312 if (pNSURL)
313 {
314 UIResponder* pResponder = (UIResponder*) mView;
315 while(pResponder) {
316 if ([pResponder respondsToSelector: @selector(openURL:)])
317 [pResponder performSelector: @selector(openURL:) withObject: pNSURL];
318
319 pResponder = [pResponder nextResponder];
320 }
321 return true;
322 }
323 return false;
324}
325
326void* IGraphicsIOS::GetWindow()
327{
328 if (mView)
329 return mView;
330 else
331 return 0;
332}
333
334// static
335int IGraphicsIOS::GetUserOSVersion()
336{
337 return (int) 0; //TODO
338}
339
340bool IGraphicsIOS::GetTextFromClipboard(WDL_String& str)
341{
342 return false;
343}
344
345bool IGraphicsIOS::SetTextInClipboard(const char* str)
346{
347 return false;
348}
349
350PlatformFontPtr IGraphicsIOS::LoadPlatformFont(const char* fontID, const char* fileNameOrResID)
351{
352 return CoreTextHelpers::LoadPlatformFont(fontID, fileNameOrResID, GetBundleID());
353}
354
355PlatformFontPtr IGraphicsIOS::LoadPlatformFont(const char* fontID, const char* fontName, ETextStyle style)
356{
357 return CoreTextHelpers::LoadPlatformFont(fontID, fontName, style);
358}
359
360PlatformFontPtr IGraphicsIOS::LoadPlatformFont(const char* fontID, void* pData, int dataSize)
361{
362 return CoreTextHelpers::LoadPlatformFont(fontID, pData, dataSize);
363}
364
365void IGraphicsIOS::CachePlatformFont(const char* fontID, const PlatformFontPtr& font)
366{
367 CoreTextHelpers::CachePlatformFont(fontID, font, sFontDescriptorCache);
368}
369
370void IGraphicsIOS::LaunchBluetoothMidiDialog(float x, float y)
371{
372 ReleaseMouseCapture();
373 NSDictionary* dic = @{@"x": @(x), @"y": @(y)};
374 [[NSNotificationCenter defaultCenter] postNotificationName:@"LaunchBTMidiDialog" object:nil userInfo:dic];
375}
376
377EUIAppearance IGraphicsIOS::GetUIAppearance() const
378{
379 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
380
381 if (pView)
382 {
383 return [[pView traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark ? EUIAppearance::Dark
384 : EUIAppearance::Light;
385 }
386 else
387 {
388 return EUIAppearance::Light;
389 }
390}
391
392void IGraphicsIOS::ActivateGLContext()
393{
394 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
395 [pView activateGLContext];
396}
397
398void IGraphicsIOS::DeactivateGLContext()
399{
400 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
401 [pView deactivateGLContext];
402}
403
404
405#if defined IGRAPHICS_NANOVG
406 #include "IGraphicsNanoVG.cpp"
407#elif defined IGRAPHICS_SKIA
408 #include "IGraphicsSkia.cpp"
409#else
410 #error Either NO_IGRAPHICS or one and only one choice of graphics library must be defined!
411#endif
An editor delegate base class that uses IGraphics for the UI.
virtual void AttachGestureRecognizer(EGestureType type)
Registers a gesture recognizer with the graphics context.
Definition: IGraphics.cpp:2738
A class for setting the contents of a pop up menu.
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,...