yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
ios_platform_state.mm
Go to the documentation of this file.
2
3#if defined(__APPLE__)
4#include <TargetConditionals.h>
5#endif
6
7#if defined(__APPLE__) && (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
8#import <Foundation/Foundation.h>
9#import <UIKit/UIKit.h>
10#import <dispatch/dispatch.h>
11#endif
12
13namespace yaze {
14namespace platform {
15namespace ios {
16
17namespace {
18void* g_metal_view = nullptr;
21float g_touch_scale = 1.0f;
22
23// Last-pushed state for diffing (avoids redundant notifications).
25} // namespace
26
27void SetMetalView(void* view) {
28 g_metal_view = view;
29}
30
31void* GetMetalView() {
32 return g_metal_view;
33}
34
35void SetSafeAreaInsets(float left, float right, float top, float bottom) {
36 g_safe_area_insets = {left, right, top, bottom};
37}
38
40 return g_safe_area_insets;
41}
42
43void SetOverlayTopInset(float top) {
44 g_overlay_top_inset = top;
45}
46
48 return g_overlay_top_inset;
49}
50
51void SetTouchScale(float scale) {
52 g_touch_scale = scale;
53}
54
56 return g_touch_scale;
57}
58
59void PostOverlayCommand(const char* command) {
60#if defined(__APPLE__) && (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
61 if (!command || command[0] == '\0') {
62 return;
63 }
64 NSString* cmd = [NSString stringWithUTF8String:command];
65 if (!cmd || cmd.length == 0) {
66 return;
67 }
68
69 auto post = ^{
70 [[NSNotificationCenter defaultCenter]
71 postNotificationName:@"yaze.overlay.command"
72 object:nil
73 userInfo:@{@"command" : cmd}];
74 };
75
76 if ([NSThread isMainThread]) {
77 post();
78 } else {
79 dispatch_async(dispatch_get_main_queue(), post);
80 }
81#else
82 (void)command;
83#endif
84}
85
87#if defined(__APPLE__) && (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
88 dispatch_async(dispatch_get_main_queue(), ^{
89 switch (style) {
91 auto* gen = [[UIImpactFeedbackGenerator alloc]
92 initWithStyle:UIImpactFeedbackStyleLight];
93 [gen impactOccurred];
94 break;
95 }
97 auto* gen = [[UIImpactFeedbackGenerator alloc]
98 initWithStyle:UIImpactFeedbackStyleMedium];
99 [gen impactOccurred];
100 break;
101 }
102 case HapticStyle::kHeavy: {
103 auto* gen = [[UIImpactFeedbackGenerator alloc]
104 initWithStyle:UIImpactFeedbackStyleHeavy];
105 [gen impactOccurred];
106 break;
107 }
109 auto* gen = [[UISelectionFeedbackGenerator alloc] init];
110 [gen selectionChanged];
111 break;
112 }
114 auto* gen = [[UINotificationFeedbackGenerator alloc] init];
115 [gen notificationOccurred:UINotificationFeedbackTypeSuccess];
116 break;
117 }
119 auto* gen = [[UINotificationFeedbackGenerator alloc] init];
120 [gen notificationOccurred:UINotificationFeedbackTypeWarning];
121 break;
122 }
123 case HapticStyle::kError: {
124 auto* gen = [[UINotificationFeedbackGenerator alloc] init];
125 [gen notificationOccurred:UINotificationFeedbackTypeError];
126 break;
127 }
128 }
129 });
130#else
131 (void)style;
132#endif
133}
134
136#if defined(__APPLE__) && (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
137 dispatch_async(dispatch_get_main_queue(), ^{
138 [[NSNotificationCenter defaultCenter]
139 postNotificationName:@"yaze.input.undo"
140 object:nil];
141 });
142#endif
143}
144
146#if defined(__APPLE__) && (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
147 dispatch_async(dispatch_get_main_queue(), ^{
148 [[NSNotificationCenter defaultCenter]
149 postNotificationName:@"yaze.input.redo"
150 object:nil];
151 });
152#endif
153}
154
156#if defined(__APPLE__) && (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
157 dispatch_async(dispatch_get_main_queue(), ^{
158 [[NSNotificationCenter defaultCenter]
159 postNotificationName:@"yaze.input.toggle_sidebar"
160 object:nil];
161 });
162#endif
163}
164
166#if defined(__APPLE__) && (TARGET_OS_IPHONE == 1 || TARGET_IPHONE_SIMULATOR == 1)
167 // Diff against last-pushed state to avoid redundant notifications.
168 if (state.can_undo == g_last_editor_state.can_undo &&
169 state.can_redo == g_last_editor_state.can_redo &&
170 state.can_save == g_last_editor_state.can_save &&
171 state.is_dirty == g_last_editor_state.is_dirty &&
172 state.editor_type == g_last_editor_state.editor_type &&
173 state.rom_title == g_last_editor_state.rom_title) {
174 return;
175 }
176 g_last_editor_state = state;
177
178 // Capture values for the block (C strings must be copied to NSString now).
179 NSNumber* canUndo = @(state.can_undo);
180 NSNumber* canRedo = @(state.can_redo);
181 NSNumber* canSave = @(state.can_save);
182 NSNumber* isDirty = @(state.is_dirty);
183 NSString* editorType = [NSString stringWithUTF8String:state.editor_type.c_str()];
184 NSString* romTitle = [NSString stringWithUTF8String:state.rom_title.c_str()];
185 if (!editorType) {
186 editorType = @"";
187 }
188 if (!romTitle) {
189 romTitle = @"";
190 }
191
192 auto post = ^{
193 [[NSNotificationCenter defaultCenter]
194 postNotificationName:@"yaze.state.editor"
195 object:nil
196 userInfo:@{
197 @"canUndo" : canUndo,
198 @"canRedo" : canRedo,
199 @"canSave" : canSave,
200 @"isDirty" : isDirty,
201 @"editorType" : editorType,
202 @"romTitle" : romTitle,
203 }];
204 };
205
206 if ([NSThread isMainThread]) {
207 post();
208 } else {
209 dispatch_async(dispatch_get_main_queue(), post);
210 }
211#else
212 (void)state;
213#endif
214}
215
216} // namespace ios
217} // namespace platform
218} // namespace yaze
void TriggerHaptic(HapticStyle style)
void PostOverlayCommand(const char *command)
void SetOverlayTopInset(float top)
void PostEditorStateUpdate(const EditorStateSnapshot &state)
SafeAreaInsets GetSafeAreaInsets()
void SetSafeAreaInsets(float left, float right, float top, float bottom)
void SetMetalView(void *view)
void SetTouchScale(float scale)