浏览代码

iOS status bar + keyboard events

/main
xingwei.zhu 6 年前
当前提交
ec84eae5
共有 9 个文件被更改,包括 344 次插入52 次删除
  1. 37
      Runtime/Plugins/platform/ios/DeviceScreen.mm
  2. 21
      Runtime/editor/editor_utils.cs
  3. 11
      Runtime/editor/editor_window.cs
  4. 95
      Runtime/engine/DisplayMetrics.cs
  5. 23
      Runtime/engine/UIWidgetsPanel.cs
  6. 34
      Runtime/Plugins/platform/ios/UIWidgetsViewController.h
  7. 26
      Runtime/Plugins/platform/ios/UIWidgetsViewController.h.meta
  8. 113
      Runtime/Plugins/platform/ios/UIWidgetsViewController.mm
  9. 36
      Runtime/Plugins/platform/ios/UIWidgetsViewController.mm.meta

37
Runtime/Plugins/platform/ios/DeviceScreen.mm


#import <UIKit/UIKit.h>
extern "C"
{
int IOSDeviceScaleFactor()
{
return [[UIScreen mainScreen] scale];
}
struct viewPadding
{
float top;
float bottom;
float left;
float right;
};
viewPadding IOSGetViewportPadding()
{
viewPadding _viewPadding;
CGFloat scale = [[UIScreen mainScreen] scale];
if (@available(iOS 11, *)) {
_viewPadding.bottom = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom * scale;
_viewPadding.top = [UIApplication sharedApplication].keyWindow.safeAreaInsets.top * scale;
_viewPadding.left = [UIApplication sharedApplication].keyWindow.safeAreaInsets.left * scale;
_viewPadding.right = [UIApplication sharedApplication].keyWindow.safeAreaInsets.right * scale;
} else {
CGRect statusFrame = [UIApplication sharedApplication].statusBarFrame;
_viewPadding.bottom = 0;
_viewPadding.top = statusFrame.size.height * scale;
_viewPadding.left = 0;
_viewPadding.right = 0;
}
return _viewPadding;
}
int IOSDeviceScaleFactor()
{
return [[UIScreen mainScreen] scale];
}
}

21
Runtime/editor/editor_utils.cs


get { return this._lastDevicePixelRatio; }
}
public viewMetrics viewMetrics {
get {
return new viewMetrics {
insets_bottom = 0,
insets_left = 0,
insets_right = 0,
insets_top = 0,
padding_left = 0,
padding_top = 0,
padding_right = 0,
padding_bottom = 0
};
}
}
public WindowPadding viewInsets {
get { return WindowPadding.zero; }
}
}
static class GameViewUtil {

11
Runtime/editor/editor_window.cs


float _lastWindowWidth;
float _lastWindowHeight;
bool _viewMetricsChanged;
readonly MicrotaskQueue _microtaskQueue = new MicrotaskQueue();
readonly TimerProvider _timerProvider = new TimerProvider();
readonly Rasterizer _rasterizer = new Rasterizer();

}
protected virtual void updateSafeArea() {
}
public void onViewMetricsChanged() {
this._viewMetricsChanged = true;
}
protected abstract bool hasFocus();

return true;
}
if (this._viewMetricsChanged) {
return true;
}
return false;
}

this._lastWindowHeight * this._devicePixelRatio);
this.updateSafeArea();
this._viewMetricsChanged = false;
if (this.onMetricsChanged != null) {
this.onMetricsChanged();
}

95
Runtime/engine/DisplayMetrics.cs


namespace Unity.UIWidgets.engine {
[StructLayout(LayoutKind.Sequential)]
public struct ViewPadding {
public float top;
public float bottom;
public float left;
public float right;
public struct viewMetrics {
public float insets_top;
public float insets_bottom;
public float insets_left;
public float insets_right;
public float padding_top;
public float padding_bottom;
public float padding_left;
public float padding_right;
}
public static class DisplayMetricsProvider {

float devicePixelRatio { get; }
viewMetrics viewMetrics { get; }
WindowPadding viewInsets { get; }
WindowPadding _viewPadding = null;
viewMetrics? _viewMetrics = null;
public void OnEnable() {
}

}
public void Update() {
//view metrics marks dirty
this._viewMetrics = null;
}

}
public WindowPadding viewPadding {
get {
return new WindowPadding(this.viewMetrics.padding_left,
this.viewMetrics.padding_top,
this.viewMetrics.padding_right,
this.viewMetrics.padding_bottom);
}
}
public WindowPadding viewInsets {
get {
return new WindowPadding(this.viewMetrics.insets_left,
this.viewMetrics.insets_top,
this.viewMetrics.insets_right,
this.viewMetrics.insets_bottom);
}
}
public viewMetrics viewMetrics {
if (this._viewPadding != null) {
return this._viewPadding;
if (this._viewMetrics != null) {
return this._viewMetrics.Value;
this._viewPadding = WindowPadding.zero;
this._viewMetrics = new viewMetrics {
insets_bottom = 0,
insets_left = 0,
insets_right = 0,
insets_top = 0,
padding_left = Screen.safeArea.x,
padding_top = Screen.safeArea.y,
padding_right = Screen.width - Screen.safeArea.width - Screen.safeArea.x,
padding_bottom = Screen.height - Screen.safeArea.height - Screen.safeArea.y
};
this._viewPadding = WindowPadding.zero;
this._viewMetrics = new viewMetrics {
insets_bottom = 0,
insets_left = 0,
insets_right = 0,
insets_top = 0,
padding_left = 0,
padding_top = 0,
padding_right = 0,
padding_bottom = 0
};
ViewPadding padding = IOSGetViewportPadding();
this._viewPadding = new WindowPadding(left: padding.left, top: padding.top, right: padding.right,
bottom: padding.bottom);
IOSDeviceStartup();
viewMetrics metrics = IOSGetViewportPadding();
this._viewMetrics = metrics;
this._viewPadding = WindowPadding.zero;
this._viewMetrics = new viewMetrics {
insets_bottom = 0,
insets_left = 0,
insets_right = 0,
insets_top = 0,
padding_left = 0,
padding_top = 0,
padding_right = 0,
padding_bottom = 0
};
return this._viewPadding;
return this._viewMetrics.Value;
}
}

static extern int IOSDeviceScaleFactor();
[DllImport("__Internal")]
static extern ViewPadding IOSGetViewportPadding();
static extern viewMetrics IOSGetViewportPadding();
[DllImport("__Internal")]
static extern void IOSDeviceStartup();
}
}

23
Runtime/engine/UIWidgetsPanel.cs


using System.Collections.Generic;
using Unity.UIWidgets.async;
using Unity.UIWidgets.editor;
using Unity.UIWidgets.external.simplejson;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;

readonly UIWidgetsPanel _uiWidgetsPanel;
bool _needsPaint;
this._viewInsets = this._uiWidgetsPanel.viewInsets;
}
protected override bool hasFocus() {

Vector2 _lastMouseMove;
HashSet<int> _enteredPointers;
bool _viewMetricsCallbackRegistered;
bool _mouseEntered {
get { return !this._enteredPointers.isEmpty(); }

const int mouseButtonNum = 3;
void _handleViewMetricsChanged(string method, List<JSONNode> args) {
this._windowAdapter.onViewMetricsChanged();
this._displayMetrics.Update();
}
protected override void OnEnable() {
base.OnEnable();
//Disable the default touch -> mouse event conversion on mobile devices

this._displayMetrics.OnEnable();
if (_repaintEvent == null) {
_repaintEvent = new Event {type = EventType.Repaint};
}

get { return this._displayMetrics.viewPadding; }
}
public WindowPadding viewInsets {
get { return this._displayMetrics.viewInsets; }
}
base.OnDisable();
}

protected virtual void Update() {
this._displayMetrics.Update();
UIWidgetsMessageManager.ensureUIWidgetsMessageManagerIfNeeded();
if (!this._viewMetricsCallbackRegistered) {
this._viewMetricsCallbackRegistered = true;
UIWidgetsMessageManager.instance?.AddChannelMessageDelegate("ViewportMatricsChanged",
this._handleViewMetricsChanged);
}
if (this._mouseEntered) {
if (this._lastMouseMove.x != Input.mousePosition.x || this._lastMouseMove.y != Input.mousePosition.y) {

34
Runtime/Plugins/platform/ios/UIWidgetsViewController.h


#ifndef PLATFORM_IOS_FRAMEWORK_SOURCE_UIWIDGETSVIEWCONTROLLER_H_
#define PLATFORM_IOS_FRAMEWORK_SOURCE_UIWIDGETSVIEWCONTROLLER_H_
#import <UIKit/UIKit.h>
#include "UIWidgetsTextInputDelegate.h"
struct viewPadding
{
float top;
float bottom;
float left;
float right;
};
struct viewMetrics
{
float insets_top;
float insets_bottom;
float insets_left;
float insets_right;
float padding_top;
float padding_bottom;
float padding_left;
float padding_right;
};
@interface UIWidgetsViewController : NSObject
@property viewPadding padding;
@property viewPadding viewInsets;
@end
#endif // PLATFORM_IOS_FRAMEWORK_SOURCE_UIWIDGETSVIEWCONTROLLER_H_

26
Runtime/Plugins/platform/ios/UIWidgetsViewController.h.meta


fileFormatVersion: 2
guid: a797fb0a2ddb84d4e8ac7f6da5d70819
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 1
isExplicitlyReferenced: 0
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

113
Runtime/Plugins/platform/ios/UIWidgetsViewController.mm


#include "UIWidgetsViewController.h"
#include "UIWidgetsMessageManager.h"
#include <Foundation/Foundation.h>
#include <UIKit/UIKit.h>
@implementation UIWidgetsViewController {
}
@synthesize viewInsets;
@synthesize padding;
- (instancetype)init {
self = [super init];
if (self) {
viewInsets.bottom = 0;
viewInsets.top = 0;
viewInsets.left = 0;
viewInsets.right = 0;
CGFloat scale = [[UIScreen mainScreen] scale];
if (@available(iOS 11, *)) {
padding.bottom = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom * scale;
padding.top = [UIApplication sharedApplication].keyWindow.safeAreaInsets.top * scale;
padding.left = [UIApplication sharedApplication].keyWindow.safeAreaInsets.left * scale;
padding.right = [UIApplication sharedApplication].keyWindow.safeAreaInsets.right * scale;
} else {
CGRect statusFrame = [UIApplication sharedApplication].statusBarFrame;
padding.bottom = 0;
padding.top = statusFrame.size.height * scale;
padding.left = 0;
padding.right = 0;
}
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
[center addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
return self;
}
-(void)keyboardWillBeHidden:(NSNotification*)notification {
viewInsets.bottom = 0;
CGFloat scale = [UIScreen mainScreen].scale;
if (@available(iOS 11, *)) {
CGFloat cur_padding = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom * scale;
padding.bottom = cur_padding;
} else {
CGRect statusFrame = [UIApplication sharedApplication].statusBarFrame;
CGFloat cur_padding = statusFrame.size.height * scale;
padding.top = cur_padding;
}
UIWidgetsMethodMessage(@"ViewportMatricsChanged", @"UIWidgetViewController.keyboardHide", @[]);
}
-(void)keyboardWillChangeFrame:(NSNotification*)notification {
NSDictionary* info = [notification userInfo];
CGFloat bottom = CGRectGetHeight([[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]);
CGFloat scale = [UIScreen mainScreen].scale;
viewInsets.bottom = bottom * scale;
padding.bottom = 0;
UIWidgetsMethodMessage(@"ViewportMatricsChanged", @"UIWidgetViewController.keyboardHide", @[]);
}
-(void)tryLaunch {
}
+ (instancetype)sharedInstance {
static UIWidgetsViewController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[UIWidgetsViewController alloc] init];
});
return sharedInstance;
}
@end
extern "C"
{
void IOSDeviceStartup()
{
[[UIWidgetsViewController sharedInstance] tryLaunch];
}
viewMetrics IOSGetViewportPadding()
{
viewMetrics metrics;
viewPadding insets = [[UIWidgetsViewController sharedInstance] viewInsets];
viewPadding padding = [[UIWidgetsViewController sharedInstance] padding];
metrics.insets_bottom = insets.bottom;
metrics.insets_top = insets.top;
metrics.insets_left = insets.left;
metrics.insets_right = insets.right;
metrics.padding_bottom = padding.bottom;
metrics.padding_top = padding.top;
metrics.padding_left = padding.left;
metrics.padding_right = padding.right;
return metrics;
}
}

36
Runtime/Plugins/platform/ios/UIWidgetsViewController.mm.meta


fileFormatVersion: 2
guid: fc3d8a9d616f147929ae9b8898fa01bf
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 1
isExplicitlyReferenced: 0
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings: {}
- first:
tvOS: tvOS
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存