11 #import <QuartzCore/QuartzCore.h> 12 #import <MetalKit/MetalKit.h> 14 #include "IGraphicsIOS.h" 15 #include "IGraphicsCoreText.h" 17 #import "IGraphicsIOS_view.h" 22 #pragma clang diagnostic ignored "-Wdeprecated-declarations" 25 BEGIN_IGRAPHICS_NAMESPACE
27 void GetScreenDimensions(
int& width,
int& height)
29 CGRect bounds = [[UIScreen mainScreen] bounds];
30 width = bounds.size.width;
31 height = bounds.size.height;
34 float GetScaleForScreen(
int plugWidth,
int plugHeight)
37 GetScreenDimensions(width, height);
38 return std::min((
float) width / (
float) plugWidth, (
float) height / (
float) plugHeight);
41 END_IGRAPHICS_NAMESPACE
44 using namespace iplug;
45 using namespace igraphics;
47 StaticStorage<CoreTextFontDescriptor> sFontDescriptorCache;
51 std::map<std::string, MTLTexturePtr> gTextureMap;
52 NSArray<id<MTLTexture>>* gTextures;
54 IGraphicsIOS::IGraphicsIOS(
IGEditorDelegate& dlg,
int w,
int h,
int fps,
float scale)
55 : IGRAPHICS_DRAW_CLASS(dlg, w, h, fps, scale)
58 #if defined IGRAPHICS_METAL && !defined IGRAPHICS_SKIA 59 if(!gTextureMap.size())
61 NSBundle* pBundle = [NSBundle mainBundle];
63 if(IsAuv3AppExtension())
64 pBundle = [NSBundle bundleWithPath: [[[pBundle bundlePath] stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]];
66 NSArray<NSURL*>* pTextureFiles = [pBundle URLsForResourcesWithExtension:
@"ktx" subdirectory:
@""];
68 if ([pTextureFiles count])
70 MTKTextureLoader* textureLoader = [[MTKTextureLoader alloc] initWithDevice:MTLCreateSystemDefaultDevice()];
72 NSError* pError = nil;
73 NSDictionary* textureOptions = @{ MTKTextureLoaderOptionSRGB: [NSNumber numberWithBool:NO] };
75 gTextures = [textureLoader newTexturesWithContentsOfURLs:pTextureFiles options:textureOptions error:&pError];
77 for(
int i=0; i < gTextures.count; i++)
79 gTextureMap.insert(std::make_pair([[[pTextureFiles[i] lastPathComponent] stringByDeletingPathExtension] cStringUsingEncoding:NSUTF8StringEncoding], (MTLTexturePtr) gTextures[i]));
82 DBGMSG(
"Preloaded %i textures\n", (
int) [pTextureFiles count]);
84 [textureLoader release];
91 IGraphicsIOS::~IGraphicsIOS()
96 void* IGraphicsIOS::OpenWindow(
void* pParent)
100 IGRAPHICS_VIEW* view = [[IGRAPHICS_VIEW alloc] initWithIGraphics:
this];
101 mView = (
void*) view;
103 OnViewInitialized((
void*) [view metalLayer]);
105 SetScreenScale([UIScreen mainScreen].scale);
107 GetDelegate()->LayoutUI(
this);
108 GetDelegate()->OnUIOpen();
110 [view setMultipleTouchEnabled:MultiTouchEnabled()];
114 [(UIView*) pParent addSubview: view];
120 void IGraphicsIOS::CloseWindow()
124 #ifdef IGRAPHICS_IMGUI 127 IGRAPHICS_IMGUIVIEW* pImGuiView = (IGRAPHICS_IMGUIVIEW*) mImGuiView;
128 [pImGuiView removeFromSuperview];
129 [pImGuiView release];
130 mImGuiView =
nullptr;
134 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
135 [pView removeFromSuperview];
143 bool IGraphicsIOS::WindowIsOpen()
148 void IGraphicsIOS::PlatformResize(
bool parentHasResized)
152 CGRect r = CGRectMake(0., 0., static_cast<CGFloat>(WindowWidth()), static_cast<CGFloat>(WindowHeight()));
153 [(IGRAPHICS_VIEW*) mView setFrame: r ];
157 void IGraphicsIOS::AttachPlatformView(
const IRECT& r,
void* pView)
159 IGRAPHICS_VIEW* pMainView = (IGRAPHICS_VIEW*) mView;
161 UIView* pNewSubView = (UIView*) pView;
162 [pNewSubView setFrame:ToCGRect(
this, r)];
164 [pMainView addSubview:pNewSubView];
167 void IGraphicsIOS::RemovePlatformView(
void* pView)
169 [(UIView*) pView removeFromSuperview];
172 EMsgBoxResult IGraphicsIOS::ShowMessageBox(
const char* str,
const char* caption, EMsgBoxType type, IMsgBoxCompletionHanderFunc completionHandler)
174 ReleaseMouseCapture();
175 [(IGRAPHICS_VIEW*) mView showMessageBox:str :caption :type :completionHandler];
176 return EMsgBoxResult::kNoResult;
179 void IGraphicsIOS::AttachGestureRecognizer(EGestureType type)
182 [(IGRAPHICS_VIEW*) mView attachGestureRecognizer:type];
185 void IGraphicsIOS::ForceEndUserEdit()
189 [(IGRAPHICS_VIEW*) mView endUserInput];
193 const char* IGraphicsIOS::GetPlatformAPIStr()
198 void IGraphicsIOS::GetMouseLocation(
float& x,
float&y)
const 200 [(IGRAPHICS_VIEW*) mView getLastTouchLocation: x : y];
203 void IGraphicsIOS::PromptForFile(WDL_String& fileName, WDL_String& path, EFileAction action,
const char* ext)
207 void IGraphicsIOS::PromptForDirectory(WDL_String& dir)
211 bool IGraphicsIOS::PromptForColor(
IColor& color,
const char* str, IColorPickerHandlerFunc func)
213 [(IGRAPHICS_VIEW*) mView promptForColor: color: str: func];
223 CGRect areaRect = ToCGRect(
this, bounds);
224 pReturnMenu = [(IGRAPHICS_VIEW*) mView createPopupMenu: menu: areaRect];
228 if(pReturnMenu && pReturnMenu->GetFunction())
229 pReturnMenu->ExecFunction();
234 void IGraphicsIOS::CreatePlatformTextEntry(
int paramIdx,
const IText& text,
const IRECT& bounds,
int length,
const char* str)
236 ReleaseMouseCapture();
237 CGRect areaRect = ToCGRect(
this, bounds);
238 [(IGRAPHICS_VIEW*) mView createTextEntry: paramIdx : text: str: length: areaRect];
241 bool IGraphicsIOS::OpenURL(
const char* url,
const char* msgWindowTitle,
const char* confirmMsg,
const char* errMsgOnFailure)
243 NSURL* pNSURL =
nullptr;
244 if (strstr(url,
"http"))
245 pNSURL = [NSURL URLWithString:[NSString stringWithCString:url encoding:NSUTF8StringEncoding]];
247 pNSURL = [NSURL fileURLWithPath:[NSString stringWithCString:url encoding:NSUTF8StringEncoding]];
251 UIResponder* pResponder = (UIResponder*) mView;
253 if ([pResponder respondsToSelector:
@selector(openURL:)])
254 [pResponder performSelector:
@selector(openURL:) withObject: pNSURL];
256 pResponder = [pResponder nextResponder];
263 void* IGraphicsIOS::GetWindow()
272 int IGraphicsIOS::GetUserOSVersion()
277 bool IGraphicsIOS::GetTextFromClipboard(WDL_String& str)
282 bool IGraphicsIOS::SetTextInClipboard(
const char* str)
287 void IGraphicsIOS::CreatePlatformImGui()
289 #ifdef IGRAPHICS_IMGUI 292 IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
294 IGRAPHICS_IMGUIVIEW* pImGuiView = [[IGRAPHICS_IMGUIVIEW alloc] initWithIGraphicsView:pView];
295 [pView addSubview: pImGuiView];
296 mImGuiView = pImGuiView;
301 PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fileNameOrResID)
303 return CoreTextHelpers::LoadPlatformFont(fontID, fileNameOrResID, GetBundleID());
306 PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
const char* fontName, ETextStyle style)
308 return CoreTextHelpers::LoadPlatformFont(fontID, fontName, style);
311 PlatformFontPtr IGraphicsIOS::LoadPlatformFont(
const char* fontID,
void* pData,
int dataSize)
313 return CoreTextHelpers::LoadPlatformFont(fontID, pData, dataSize);
316 void IGraphicsIOS::CachePlatformFont(
const char* fontID,
const PlatformFontPtr& font)
318 CoreTextHelpers::CachePlatformFont(fontID, font, sFontDescriptorCache);
321 void IGraphicsIOS::LaunchBluetoothMidiDialog(
float x,
float y)
323 ReleaseMouseCapture();
324 NSDictionary* dic = @{
@"x": @(x),
@"y": @(y)};
325 [[NSNotificationCenter defaultCenter] postNotificationName:
@"LaunchBTMidiDialog" object:nil userInfo:dic];
328 #if defined IGRAPHICS_NANOVG 329 #include "IGraphicsNanoVG.cpp" 330 #elif defined IGRAPHICS_SKIA 331 #include "IGraphicsSkia.cpp" 333 #error Either NO_IGRAPHICS or one and only one choice of graphics library must be defined! Used to manage a rectangular area, independent of draw class/platform.
Used to manage color data, independent of draw class/platform.
virtual void AttachGestureRecognizer(EGestureType type)
Registers a gesture recognizer with the graphics context.
An editor delegate base class for a SOMETHING that uses IGraphics for it's UI.
IText is used to manage font and text/text entry style for a piece of text on the UI...