@@ -26,11 +26,12 @@ namespace AquaMai.Mods.Utils;
2626public static class DisplayTouchInGame
2727{
2828 private static GameObject prefab ;
29- private static GameObject [ ] canvasGameObjects = new GameObject [ 2 ] ;
29+ private static readonly List < GameObject > [ ] canvasGameObjects = new List < GameObject > [ ] { new ( ) , new ( ) } ;
3030 private static TextMeshProUGUI tmp ;
3131 private static TextMeshProUGUI [ ] tmps = new TextMeshProUGUI [ 2 ] ;
32- // 0: 不显示,1: 上框透明底,2: 上框白底,3: 下框
32+ // 0: 不显示,1: 上框透明底,2: 上框白底,3: 下框,4: 上框透明底+下框,5: 上框白底+下框
3333 public static int [ ] displayType = [ 0 , 0 ] ;
34+ public const int displayTypeVer = 2 ;
3435
3536 [ ConfigEntry (
3637 name : "默认显示" ,
@@ -75,14 +76,21 @@ public static void OnGameProcessUpdate()
7576 {
7677 for ( int i = 0 ; i < 2 ; i ++ )
7778 {
78- if ( displayType [ i ] != 2 ) continue ;
79+ // 只有上框白底(2)才会创建 tmps;组合模式(5=2+3)同样需要更新计时
80+ if ( displayType [ i ] is not ( 2 or 5 ) ) continue ;
81+ if ( tmps [ i ] == null ) continue ;
7982 tmps [ i ] . text = $ "{ TimeSpan . FromMilliseconds ( PracticeMode . CurrentPlayMsec ) : mm\\:ss\\.fff} ";
8083 }
8184 if ( ! KeyListener . GetKeyDownOrLongPress ( key , longPress ) ) return ;
8285 displayType = displayType [ 0 ] == 0 ? [ 1 , 1 ] : [ 0 , 0 ] ;
8386 for ( int i = 0 ; i < 2 ; i ++ )
8487 {
85- canvasGameObjects [ i ] . SetActive ( displayType [ i ] != 0 ) ;
88+ if ( canvasGameObjects [ i ] == null ) continue ;
89+ foreach ( var go in canvasGameObjects [ i ] )
90+ {
91+ if ( go == null ) continue ;
92+ go . SetActive ( displayType [ i ] != 0 ) ;
93+ }
8694 }
8795 }
8896
@@ -107,100 +115,121 @@ public static void OnGameStart(GameMonitor[] ____monitors)
107115
108116 for ( int i = 0 ; i < 2 ; i ++ )
109117 {
110- var sub = ____monitors [ i ] . gameObject . transform . Find ( "Canvas/Sub" ) ;
111- if ( displayType [ i ] == 3 )
118+ canvasGameObjects [ i ] . Clear ( ) ;
119+ var type = displayType [ i ] ;
120+ if ( type is < 1 or > 5 ) continue ;
121+ if ( type is 4 or 5 )
112122 {
113- sub = Traverse . Create ( ____monitors [ i ] ) . Field < GameCtrl > ( "GameController" ) . Value ? . transform ;
114- }
115- if ( sub == null )
116- {
117- MelonLogger . Error ( $ "[DisplayTouchInGame] sub is null for monitor { i } ") ;
118- continue ;
123+ CreateDisplay ( 3 , ____monitors [ i ] ) ;
124+ type -= 3 ;
119125 }
120- var canvas = new GameObject ( "[AquaMai] DisplayTouchInGame" ) ;
121- canvas . transform . SetParent ( sub , false ) ;
122- canvas . SetActive ( displayType [ i ] > 0 ) ;
123- canvasGameObjects [ i ] = canvas ;
124- GameObject buttons = null ;
126+ if ( type > 3 ) continue ;
127+ CreateDisplay ( type , ____monitors [ i ] ) ;
128+ }
129+ }
125130
126- if ( displayType [ i ] == 3 )
127- {
128- var rect = canvas . AddComponent < RectTransform > ( ) ;
129- rect . sizeDelta = new Vector2 ( 1080 , 1080 ) ;
130- rect . localPosition = Vector3 . zero ;
131- var canvasComp = canvas . AddComponent < Canvas > ( ) ;
132- canvasComp . renderMode = RenderMode . WorldSpace ;
133- canvasComp . sortingOrder = - 32768 ;
134- // canvasComp.sortingOrder = 1;
135- // canvasComp.sortingLayerID = -385436797; // GameMovie
136- }
137- if ( displayType [ i ] == 2 )
138- {
139- var rect = canvas . AddComponent < RectTransform > ( ) ;
140- rect . sizeDelta = new Vector2 ( 1080 , 450 ) ;
141- rect . localPosition = Vector3 . zero ;
142- var img = canvas . AddComponent < Image > ( ) ;
143- img . color = Color . white ;
144-
145- var t = Object . Instantiate ( tmp , canvas . transform , false ) ;
146- t . text = "" ;
147- t . transform . localPosition = new Vector3 ( - 500f , 0f , 0f ) ;
148- t . transform . localScale = Vector3 . one * 2 ;
149- tmps [ i ] = t ;
150- }
131+ // type 只能传入 1 2 3,如果是 4 或 5 的话,拆分成两个 call
132+ private static void CreateDisplay ( int type , GameMonitor monitor )
133+ {
134+ if ( type is < 1 or > 3 ) throw new ArgumentException ( "这不对吧" ) ;
135+ var sub = monitor . gameObject . transform . Find ( "Canvas/Sub" ) ;
136+ if ( type == 3 )
137+ {
138+ sub = Traverse . Create ( monitor ) . Field < GameCtrl > ( "GameController" ) . Value ? . transform ;
139+ }
140+ if ( sub == null )
141+ {
142+ MelonLogger . Error ( $ "[DisplayTouchInGame] sub is null") ;
143+ return ;
144+ }
145+ var index = monitor . MonitorIndex ;
146+ var canvas = new GameObject ( "[AquaMai] DisplayTouchInGame" ) ;
147+ canvas . transform . SetParent ( sub , false ) ;
148+ canvas . SetActive ( type > 0 ) ;
149+ canvasGameObjects [ index ] . Add ( canvas ) ;
150+ GameObject buttons = null ;
151+
152+ if ( type == 3 )
153+ {
154+ var rect = canvas . AddComponent < RectTransform > ( ) ;
155+ rect . sizeDelta = new Vector2 ( 1080 , 1080 ) ;
156+ rect . localPosition = Vector3 . zero ;
157+ var canvasComp = canvas . AddComponent < Canvas > ( ) ;
158+ canvasComp . renderMode = RenderMode . WorldSpace ;
159+ canvasComp . sortingOrder = - 32768 ;
160+ // canvasComp.sortingOrder = 1;
161+ // canvasComp.sortingLayerID = -385436797; // GameMovie
162+ }
163+ if ( type == 2 )
164+ {
165+ var rect = canvas . AddComponent < RectTransform > ( ) ;
166+ rect . sizeDelta = new Vector2 ( 1080 , 450 ) ;
167+ rect . localPosition = Vector3 . zero ;
168+ var img = canvas . AddComponent < Image > ( ) ;
169+ img . color = Color . white ;
170+
171+ var t = Object . Instantiate ( tmp , canvas . transform , false ) ;
172+ t . text = "" ;
173+ t . transform . localPosition = new Vector3 ( - 500f , 0f , 0f ) ;
174+ t . transform . localScale = Vector3 . one * 2 ;
175+ tmps [ index ] = t ;
176+ }
151177
152- if ( displayType [ i ] != 3 )
178+ if ( type != 3 )
179+ {
180+ // init button display
181+ buttons = new GameObject ( "Buttons" ) ;
182+ buttons . transform . SetParent ( canvas . transform , false ) ;
183+ buttons . transform . localPosition = Vector3 . zero ;
184+ buttons . transform . localScale = Vector3 . one * 450 / 1080f ;
185+ }
186+
187+ var touchPanel = Object . Instantiate ( prefab , canvas . transform , false ) ;
188+ Object . Destroy ( touchPanel . GetComponent < MouseTouchPanel > ( ) ) ;
189+ foreach ( Transform item in touchPanel . transform )
190+ {
191+ if ( item . name . StartsWith ( "CircleGraphic" ) )
153192 {
154- // init button display
155- buttons = new GameObject ( "Buttons" ) ;
156- buttons . transform . SetParent ( canvas . transform , false ) ;
157- buttons . transform . localPosition = Vector3 . zero ;
158- buttons . transform . localScale = Vector3 . one * 450 / 1080f ;
193+ Object . Destroy ( item . gameObject ) ;
194+ continue ;
159195 }
196+ Object . Destroy ( item . GetComponent < MeshButton > ( ) ) ;
197+ Object . Destroy ( item . GetComponent < Collider > ( ) ) ;
198+ }
199+ touchPanel . transform . localPosition = Vector3 . zero ;
200+ var touchDisplay = touchPanel . AddComponent < Display > ( ) ;
201+ touchDisplay . player = index ;
202+ touchDisplay . type = type ;
160203
161- var touchPanel = Object . Instantiate ( prefab , canvas . transform , false ) ;
162- Object . Destroy ( touchPanel . GetComponent < MouseTouchPanel > ( ) ) ;
204+ if ( type != 3 )
205+ {
163206 foreach ( Transform item in touchPanel . transform )
164207 {
165- if ( item . name . StartsWith ( "CircleGraphic" ) )
208+ var customGraphic = item . GetComponent < CustomGraphic > ( ) ;
209+ customGraphic . color = Color . blue ;
210+ if ( item . name . StartsWith ( "A" ) )
166211 {
167- Object . Destroy ( item . gameObject ) ;
168- continue ;
212+ var btn = Object . Instantiate ( item , buttons . transform , false ) ;
213+ btn . name = item . name ;
169214 }
170- Object . Destroy ( item . GetComponent < MeshButton > ( ) ) ;
171- Object . Destroy ( item . GetComponent < Collider > ( ) ) ;
215+ var tmp = item . GetComponentInChildren < TextMeshProUGUI > ( ) ;
216+ tmp . color = Color . black ;
172217 }
173- touchPanel . transform . localPosition = Vector3 . zero ;
174- var touchDisplay = touchPanel . AddComponent < Display > ( ) ;
175- touchDisplay . player = i ;
176218
177- if ( displayType [ i ] != 3 )
178- {
179- foreach ( Transform item in touchPanel . transform )
180- {
181- var customGraphic = item . GetComponent < CustomGraphic > ( ) ;
182- customGraphic . color = Color . blue ;
183- if ( item . name . StartsWith ( "A" ) )
184- {
185- var btn = Object . Instantiate ( item , buttons . transform , false ) ;
186- btn . name = item . name ;
187- }
188- var tmp = item . GetComponentInChildren < TextMeshProUGUI > ( ) ;
189- tmp . color = Color . black ;
190- }
191-
192- touchPanel . transform . localScale = Vector3 . one * 0.95f * 450 / 1080f ;
193- var buttonDisplay = buttons . AddComponent < Display > ( ) ;
194- buttonDisplay . player = i ;
195- buttonDisplay . isButton = true ;
196- }
219+ touchPanel . transform . localScale = Vector3 . one * 0.95f * 450 / 1080f ;
220+ var buttonDisplay = buttons . AddComponent < Display > ( ) ;
221+ buttonDisplay . player = index ;
222+ buttonDisplay . isButton = true ;
223+ buttonDisplay . type = type ;
197224 }
225+
198226 }
199227
200228 private class Display : MonoBehaviour
201229 {
202230 public int player ;
203231 public bool isButton ;
232+ public int type ;
204233
205234 private List < CustomGraphic > _buttonList ;
206235 private Color _offTouchCol = new Color ( 0f , 0f , 1f ) ;
@@ -218,7 +247,7 @@ private void Start()
218247 CustomGraphic component = item . GetComponent < CustomGraphic > ( ) ;
219248 _buttonList . Add ( component ) ;
220249 }
221- if ( displayType [ player ] == 3 )
250+ if ( type == 3 )
222251 {
223252 _offTouchCol = Color . clear ;
224253 _onTouchCol = new Color ( 1f , 0f , 0f , 0.3f ) ;
0 commit comments