123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System;
- using Godot;
- namespace TBL.GodotSharp.Ui.AutoAdaption
- {
- /// <summary>
- /// 自动自适应控制器
- /// <para>控制所有的 <see cref="AutoAdaptionUiRoot"/></para>
- /// </summary>
- public class AutoAdaptionController : Node
- {
- /// <summary>
- /// 设计 Ui 尺寸
- /// </summary>
- [Export] public Vector2 DesignSize;
- /// <summary>
- /// 自动缩放 Ui 开关
- /// </summary>
- [Export] private bool _autoScaleUi;
- /// <summary>
- /// 世界视图缩放值
- /// </summary>
- private int _worldViewScale = 1;
- /// <summary>
- /// 自动 Ui 缩放值
- /// </summary>
- private float _autoScaleShrink = 1.0f;
- /// <summary>
- /// 是否启用自动 Ui 调节
- /// </summary>
- public bool AutoScaleUi
- {
- get => _autoScaleUi;
- set
- {
- _autoScaleUi = value;
- var winSize = OS.WindowSize;
- var minSize = DesignSize;
- if (winSize.x < winSize.y)
- {
- minSize.y = minSize.x;
- }
- else
- {
- minSize.x = minSize.y;
- }
- if (_autoScaleUi)
- {
- var d = 1;
- if (winSize.x < winSize.y) d = 0;
- _autoScaleShrink = Mathf.Max(1.0f, Mathf.Floor(winSize[d] / minSize[d]));
- }
- else
- {
- _autoScaleShrink = 1.0f;
- }
- GetTree().SetScreenStretch(SceneTree.StretchMode.Disabled, SceneTree.StretchAspect.Expand, minSize);
- AutoAdaptionUiRoot.AdaptionShrink = _autoScaleShrink;
- if (_autoScaleUi)
- {
- WorldViewScale = Mathf.RoundToInt(_autoScaleShrink);
- }
- else
- {
- WorldViewScale = 1;
- }
- }
- }
- /// <summary>
- /// 世界视图缩放率
- /// </summary>
- public int WorldViewScale
- {
- get => Mathf.Max(1, _worldViewScale);
- set
- {
- _worldViewScale = value;
- // TODO: 世界视图
- // WorldViewContainer.WorldViewShrink = WorldViewScale;
- }
- }
- /// <summary>
- /// 自动 Ui 缩放率
- /// </summary>
- public float AutoScaleShrink => _autoScaleShrink;
- /// <summary>
- /// 屏幕尺寸变化事件
- /// </summary>
- protected void _OnScreenResized()
- {
- AutoScaleUi = _autoScaleUi;
- WorldViewScale = _worldViewScale;
- }
- #region Node
- public override async void _EnterTree()
- {
- var tree = GetTree();
- if (DesignSize == Vector2.Zero)
- DesignSize = new Vector2(
- Convert.ToSingle(ProjectSettings.GetSetting("display/window/size/width")),
- Convert.ToSingle(ProjectSettings.GetSetting("display/window/size/height"))
- );
- AutoScaleUi = _autoScaleUi;
- WorldViewScale = _worldViewScale;
- Name = nameof(AutoAdaptionController);
- tree.Connect("screen_resized", this, nameof(_OnScreenResized));
- await ToSignal(tree, "idle_frame");
- tree.EmitSignal("screen_resized");
- }
- public override void _ExitTree()
- {
- AutoScaleUi = _autoScaleUi;
- WorldViewScale = _worldViewScale;
- }
- #endregion
- /// <summary>
- /// 设置控件为填满设计尺寸
- /// </summary>
- public void SetFullParentDesignSize(Control target)
- {
- target.AnchorLeft = target.AnchorTop = 0.5f;
- target.AnchorRight = target.AnchorBottom = 0.5f;
- target.MarginLeft = DesignSize.x * -0.5f;
- target.MarginRight = DesignSize.x * 0.5f;
- target.MarginTop = DesignSize.x * -0.5f;
- target.MarginBottom = DesignSize.x * 0.5f;
- }
- }
- }
|