Skip to content

Commit

Permalink
fix #34
Browse files Browse the repository at this point in the history
  • Loading branch information
kou-yeung committed Mar 13, 2020
1 parent 34b53bb commit 7e5dd51
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Assets/WebGLSupport/WebGLInput/Detail.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions Assets/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace WebGLSupport.Detail
{
public class RebuildChecker
{
IInputField input;

string beforeString;
int beforeCaretPosition;
int beforeSelectionFocusPosition;
int beforeSelectionAnchorPosition;
//Vector2 anchoredPosition;

public RebuildChecker(IInputField input)
{
this.input = input;
}

public bool NeedRebuild(bool debug = false)
{
var res = false;

// any not same
if (beforeString != input.text)
{
if(debug) Debug.Log(string.Format("beforeString : {0} != {1}", beforeString, input.text));
beforeString = input.text;
res = true;
}

if (beforeCaretPosition != input.caretPosition)
{
if (debug) Debug.Log(string.Format("beforeCaretPosition : {0} != {1}", beforeCaretPosition, input.caretPosition));
beforeCaretPosition = input.caretPosition;
res = true;
}

if (beforeSelectionFocusPosition != input.selectionFocusPosition)
{
if (debug) Debug.Log(string.Format("beforeSelectionFocusPosition : {0} != {1}", beforeSelectionFocusPosition, input.selectionFocusPosition));
beforeSelectionFocusPosition = input.selectionFocusPosition;
res = true;
}

if (beforeSelectionAnchorPosition != input.selectionAnchorPosition)
{
if (debug) Debug.Log(string.Format("beforeSelectionAnchorPosition : {0} != {1}", beforeSelectionAnchorPosition, input.selectionAnchorPosition));
beforeSelectionAnchorPosition = input.selectionAnchorPosition;
res = true;
}

//if (anchoredPosition != input.TextComponentRectTransform().anchoredPosition)
//{
// if (debug) Debug.Log(string.Format("anchoredPosition : {0} != {1}", anchoredPosition, input.TextComponentRectTransform().anchoredPosition));
// anchoredPosition = input.TextComponentRectTransform().anchoredPosition;
// res = true;
//}
return res;
}
}
}
11 changes: 11 additions & 0 deletions Assets/WebGLSupport/WebGLInput/Detail/RebuildChecker.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Assets/WebGLSupport/WebGLInput/WebGLInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void Awake()
/// <param name="eventData"></param>
public void OnSelect(/*BaseEventData eventData*/)
{
var rect = GetScreenCoordinates(input.TextComponentRectTransform());
var rect = GetScreenCoordinates(input.RectTransform());
bool isPassword = input.contentType == ContentType.Password;

var x = (int)(rect.x);
Expand Down Expand Up @@ -298,8 +298,7 @@ void Update()
input.selectionAnchorPosition = start;
}

input.Rebuild(CanvasUpdate.LatePreRender);
input.SetAllDirty();
input.Rebuild();
}
private void OnEnable()
{
Expand All @@ -311,8 +310,8 @@ private void OnDisable()
}
public int CompareTo(WebGLInput other)
{
var a = GetScreenCoordinates(input.TextComponentRectTransform());
var b = GetScreenCoordinates(other.input.TextComponentRectTransform());
var a = GetScreenCoordinates(input.RectTransform());
var b = GetScreenCoordinates(other.input.RectTransform());
var res = b.y.CompareTo(a.y);
if (res == 0) res = a.x.CompareTo(b.x);
return res;
Expand Down
9 changes: 4 additions & 5 deletions Assets/WebGLSupport/WebGLInput/Wrapper/IInputField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ public interface IInputField
int characterLimit { get; }
int caretPosition { get; }
bool isFocused { get; }
int selectionFocusPosition { set; }
int selectionAnchorPosition { set; }
int selectionFocusPosition { get; set; }
int selectionAnchorPosition { get; set; }
bool ReadOnly { get; }
bool OnFocusSelectAll { get; }

RectTransform TextComponentRectTransform();
RectTransform RectTransform();
void ActivateInputField();
void DeactivateInputField();
void Rebuild(CanvasUpdate update);
void SetAllDirty();
void Rebuild();
}
}
21 changes: 12 additions & 9 deletions Assets/WebGLSupport/WebGLInput/Wrapper/WrappedInputField.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using UnityEngine.UI;
using WebGLSupport.Detail;

namespace WebGLSupport
{
Expand All @@ -9,6 +10,7 @@ namespace WebGLSupport
class WrappedInputField : IInputField
{
InputField input;
RebuildChecker checker;

public bool ReadOnly { get { return input.readOnly; } }

Expand Down Expand Up @@ -50,11 +52,13 @@ public bool isFocused

public int selectionFocusPosition
{
get { return input.selectionFocusPosition; }
set { input.selectionFocusPosition = value; }
}

public int selectionAnchorPosition
{
get { return input.selectionAnchorPosition; }
set { input.selectionAnchorPosition = value; }
}

Expand All @@ -66,9 +70,10 @@ public bool OnFocusSelectAll
public WrappedInputField(InputField input)
{
this.input = input;
checker = new RebuildChecker(this);
}

public RectTransform TextComponentRectTransform()
public RectTransform RectTransform()
{
return input.textComponent.GetComponent<RectTransform>();
}
Expand All @@ -83,15 +88,13 @@ public void DeactivateInputField()
input.DeactivateInputField();
}

public void Rebuild(CanvasUpdate update)
public void Rebuild()
{
input.Rebuild(update);
}

public void SetAllDirty()
{
input.textComponent.SetAllDirty();
if (checker.NeedRebuild())
{
input.Rebuild(CanvasUpdate.LatePreRender);
input.textComponent.SetAllDirty();
}
}
}

}
55 changes: 45 additions & 10 deletions Assets/WebGLSupport/WebGLInput/Wrapper/WrappedTMPInputField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using WebGLSupport.Detail;

namespace WebGLSupport
{
Expand All @@ -15,6 +16,7 @@ namespace WebGLSupport
class WrappedTMPInputField : IInputField
{
TMP_InputField input;
RebuildChecker checker;

public bool ReadOnly { get { return input.readOnly; } }

Expand Down Expand Up @@ -56,11 +58,13 @@ public bool isFocused

public int selectionFocusPosition
{
get { return input.selectionFocusPosition; }
set { input.selectionFocusPosition = value; }
}

public int selectionAnchorPosition
{
get { return input.selectionAnchorPosition; }
set { input.selectionAnchorPosition = value; }
}

Expand All @@ -72,11 +76,16 @@ public bool OnFocusSelectAll
public WrappedTMPInputField(TMP_InputField input)
{
this.input = input;
checker = new RebuildChecker(this);
}

public RectTransform TextComponentRectTransform()
public RectTransform RectTransform()
{
return input.textComponent.GetComponent<RectTransform>();
// 表示範囲
// MEMO :
// TMP では textComponent を移動させてクリッピングするため、
// 表示範囲外になる場合があるので、自分の範囲を返す
return input.GetComponent<RectTransform>();
}

public void ActivateInputField()
Expand All @@ -89,14 +98,40 @@ public void DeactivateInputField()
input.DeactivateInputField();
}

public void Rebuild(CanvasUpdate update)
{
input.Rebuild(update);
}

public void SetAllDirty()
{
input.textComponent.SetAllDirty();
public void Rebuild()
{
if (input.textComponent.enabled && checker.NeedRebuild())
{
//================================
// fix bug for tmp
// TMPの不具合で、正しく座標を設定されてなかったため、試しに対応する
var rt = input.textComponent.GetComponent<RectTransform>();
var size = input.textComponent.GetPreferredValues();
if (size.x < rt.rect.xMax)
{
// textComponent の座標を更新
var pos = rt.anchoredPosition;
pos.x = 0;
rt.anchoredPosition = pos;

// caret の座標更新
var caret = input.GetComponentInChildren<TMP_SelectionCaret>();
var caretRect = caret.GetComponent<RectTransform>();
caretRect.anchoredPosition = rt.anchoredPosition;
}
//==============================

// HACK : 1フレーム無効にする
// MEMO : 他にいい方法Rebuildがあれば対応する
// LayoutRebuilder.ForceRebuildLayoutImmediate(); で試してダメでした
input.textComponent.enabled = false;
input.Rebuild(CanvasUpdate.LatePreRender);
input.textComponent.SetAllDirty();
}
else
{
input.textComponent.enabled = true;
}
}
}

Expand Down

0 comments on commit 7e5dd51

Please sign in to comment.