1414// You should have received a copy of the GNU General Public License
1515// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
17+ using System ;
18+ using System . Collections . Generic ;
1719using System . Linq ;
20+ using System . Threading ;
21+ using System . Threading . Tasks ;
22+ using PinMame ;
1823using UnityEditor ;
1924using UnityEngine ;
2025using VisualPinball . Engine . PinMAME . Games ;
2126using VisualPinball . Unity ;
27+ using VisualPinball . Unity . Editor ;
28+ using Object = UnityEngine . Object ;
2229
2330namespace VisualPinball . Engine . PinMAME . Editor
2431{
@@ -36,6 +43,7 @@ public class PinMameGamelogicEngineInspector : UnityEditor.Editor
3643 private int _selectedRomIndex ;
3744
3845 private TableAuthoring _tableAuthoring ;
46+ private PinMame . PinMame _pinMame ;
3947
4048 private PinMameRom Rom => _gle . Game . Roms [ _selectedRomIndex ] ;
4149
@@ -76,14 +84,6 @@ private void OnEnable()
7684
7785 public override void OnInspectorGUI ( )
7886 {
79- base . OnInspectorGUI ( ) ;
80-
81- EditorGUILayout . Space ( ) ;
82- EditorGUILayout . Separator ( ) ;
83-
84- // info label
85- EditorGUILayout . LabelField ( "Game Name" , _gle . romId ) ;
86-
8787 // game dropdown
8888 EditorGUI . BeginChangeCheck ( ) ;
8989 _selectedGameIndex = EditorGUILayout . Popup ( "Game" , _selectedGameIndex , _gameNames ) ;
@@ -108,27 +108,119 @@ public override void OnInspectorGUI()
108108 if ( EditorGUI . EndChangeCheck ( ) ) {
109109 _gle . romId = Rom . Id ;
110110 }
111+
112+ // info label
113+ EditorGUILayout . LabelField ( "ROM ID" , _gle . romId ) ;
114+
111115 EditorGUI . EndDisabledGroup ( ) ;
112116
113- EditorGUI . BeginDisabledGroup ( ! IsGameSet ) ;
117+ EditorGUILayout . Space ( ) ;
118+ EditorGUILayout . Separator ( ) ;
119+
120+ EditorGUI . BeginDisabledGroup ( ! IsGameSet || Application . isPlaying ) ;
114121 if ( GUILayout . Button ( "Populate Hardware" ) ) {
115122 if ( EditorUtility . DisplayDialog ( "PinMAME" , "This will clear all linked switches, coils and lamps and re-populate them. You sure you want to do that?" , "Yes" , "No" ) ) {
116123 _tableAuthoring . RepopulateHardware ( _gle ) ;
117124 TableSelector . Instance . TableUpdated ( ) ;
118125 SceneView . RepaintAll ( ) ;
119126 }
120127 }
128+ if ( GUILayout . Button ( "Create Displays" ) ) {
129+ var sceneDisplays = FindObjectsOfType < DisplayAuthoring > ( ) ;
130+ if ( sceneDisplays . Length > 0 ) {
131+ if ( EditorUtility . DisplayDialog ( "PinMAME" , "This will re-position all your displays, if you have any. You sure you want to do that?" , "Yes" , "No" ) ) {
132+ CreateDisplays ( sceneDisplays ) ;
133+ SceneView . RepaintAll ( ) ;
134+ }
135+ } else {
136+ CreateDisplays ( sceneDisplays ) ;
137+ SceneView . RepaintAll ( ) ;
138+ }
139+ }
121140 EditorGUI . EndDisabledGroup ( ) ;
141+ }
122142
123- // initial switches button
124- EditorGUI . BeginDisabledGroup ( ! Application . isPlaying ) ;
125- EditorGUILayout . Space ( ) ;
126- EditorGUILayout . Separator ( ) ;
127- if ( GUILayout . Button ( "Send initial switches" ) ) {
128- _gle . SendInitialSwitches ( ) ;
143+ private void CreateDisplays ( IEnumerable < DisplayAuthoring > sceneDisplays )
144+ {
145+ // retrieve layouts from pinmame
146+ var pinMame = PinMame . PinMame . Instance ( ) ;
147+ var displayLayouts = pinMame . GetAvailableDisplays ( _gle . romId ) ;
148+
149+ // retrieve already existing displays from scene
150+ var displayGameObjects = new Dictionary < string , DisplayAuthoring > ( ) ;
151+ foreach ( var displays in sceneDisplays ) {
152+ displayGameObjects [ displays . Id ] = displays ;
153+ }
154+ var ta = _gle . GetComponentInParent < TableAuthoring > ( ) ;
155+ var tableHeight = 0f ;
156+ var tableWidth = 1f ;
157+ if ( ta ) {
158+ tableHeight = ta . Table . GlassHeight * VpxConverter . GlobalScale ;
159+ tableWidth = ta . Table . Width * VpxConverter . GlobalScale ;
129160 }
130161
131- EditorGUI . EndDisabledGroup ( ) ;
162+ // get total height
163+ var numRows = displayLayouts . Values . Select ( l => l . top ) . Max ( ) / 2 ;
164+
165+ // get total width
166+ var lines = new Dictionary < int , int > { { 0 , 0 } } ;
167+ foreach ( var layout in displayLayouts . Values ) {
168+ if ( layout . IsDmd ) {
169+ continue ;
170+ }
171+ lines [ layout . top ] = lines . ContainsKey ( layout . top )
172+ ? lines [ layout . top ] + layout . length
173+ : layout . length ;
174+ }
175+ var numCols = lines . Values . ToList ( ) . Max ( ) ;
176+ var totalWidth = 0f ;
177+
178+ foreach ( var index in displayLayouts . Keys ) {
179+ var layout = displayLayouts [ index ] ;
180+
181+ var left = layout . left / 2 ;
182+ var top = layout . top / 2 ;
183+
184+ var id = layout . IsDmd
185+ ? $ "{ PinMameGamelogicEngine . DmdPrefix } { index } "
186+ : $ "{ PinMameGamelogicEngine . SegDispPrefix } { index } ";
187+
188+ var go = displayGameObjects . ContainsKey ( id )
189+ ? displayGameObjects [ id ] . gameObject
190+ : new GameObject ( ) ;
191+
192+ if ( layout . IsDmd ) {
193+ var auth = ! displayGameObjects . ContainsKey ( id )
194+ ? go . AddComponent < DmdAuthoring > ( )
195+ : go . GetComponent < DmdAuthoring > ( ) ;
196+ auth . Id = id ;
197+
198+ } else {
199+ var auth = ! displayGameObjects . ContainsKey ( id )
200+ ? go . AddComponent < SegmentDisplayAuthoring > ( )
201+ : go . GetComponent < SegmentDisplayAuthoring > ( ) ;
202+ auth . Id = id ;
203+ go . name = $ "Segment Display [{ index } ]";
204+
205+ auth . NumChars = layout . length ;
206+ go . transform . localScale = new Vector3 ( DisplayInspector . GameObjectScale , DisplayInspector . GameObjectScale , DisplayInspector . GameObjectScale ) ;
207+ var mesh = go . GetComponent < MeshFilter > ( ) . sharedMesh ;
208+ var charHeight = mesh . bounds . size . y * DisplayInspector . GameObjectScale ;
209+ var charWidth = mesh . bounds . size . x * DisplayInspector . GameObjectScale / layout . length ;
210+ totalWidth = charWidth * numCols ;
211+
212+ var globalLeft = ( tableWidth - totalWidth ) / 2 ;
213+
214+ go . transform . localPosition = new Vector3 (
215+ globalLeft - tableWidth / 2 + charWidth * left + mesh . bounds . size . x / 2 * DisplayInspector . GameObjectScale ,
216+ tableHeight + ( numRows - top ) * charHeight ,
217+ 1.1f ) ;
218+ }
219+ }
220+
221+
222+ var str = string . Join ( "\n " , displayLayouts . Keys . Select ( t => $ "{ t } : { displayLayouts [ t ] } ") ) ;
223+ Debug . Log ( $ "OnDisplaysAvailable ({ displayLayouts . Count } ): displays=\n { str } \n { tableWidth } - { totalWidth } ") ;
132224 }
133225 }
134226}
0 commit comments