Skip to content

Commit 3d7615c

Browse files
committed
editor: Pull available config and show it in the inspector.
1 parent 52cdce4 commit 3d7615c

File tree

5 files changed

+119
-23
lines changed

5 files changed

+119
-23
lines changed

VisualPinball.Engine.Mpf.Unity/Editor/MpfGamelogicEngineInspector.cs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@
99
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1010
// SOFTWARE.
1111

12+
// ReSharper disable AssignmentInConditionalExpression
13+
1214
using System.IO;
1315
using UnityEditor;
1416
using UnityEngine;
17+
using VisualPinball.Unity.Editor;
1518

1619
namespace VisualPinball.Engine.Mpf.Unity.Editor
1720
{
1821
[CustomEditor(typeof(MpfGamelogicEngine))]
1922
public class MpfGamelogicEngineInspector : UnityEditor.Editor
2023
{
2124
private MpfGamelogicEngine _mpfEngine;
25+
private bool _foldoutSwitches;
26+
private bool _foldoutCoils;
27+
private bool _foldoutLamps;
2228

2329
private void OnEnable()
2430
{
@@ -30,35 +36,59 @@ public override void OnInspectorGUI()
3036
var pos = EditorGUILayout.GetControlRect(true, 18f);
3137
pos = EditorGUI.PrefixLabel(pos, new GUIContent("Machine Folder"));
3238

33-
if (GUI.Button(pos, _mpfEngine.MachineFolder, EditorStyles.objectField)) {
34-
var path = EditorUtility.OpenFolderPanel("Mission Pinball Framework: Choose machine folder", _mpfEngine.MachineFolder, "");
39+
if (GUI.Button(pos, _mpfEngine.machineFolder, EditorStyles.objectField)) {
40+
var path = EditorUtility.OpenFolderPanel("Mission Pinball Framework: Choose machine folder", _mpfEngine.machineFolder, "");
3541
if (!string.IsNullOrWhiteSpace(path)) {
36-
_mpfEngine.MachineFolder = path;
42+
_mpfEngine.machineFolder = path;
3743
}
3844
}
3945

4046
if (GUILayout.Button("Synchronize")) {
41-
if (!Directory.Exists(_mpfEngine.MachineFolder)) {
47+
if (!Directory.Exists(_mpfEngine.machineFolder)) {
4248
EditorUtility.DisplayDialog("Mission Pinball Framework", "Gotta choose a valid machine folder first!", "Okay");
43-
} else if (!Directory.Exists(Path.Combine(_mpfEngine.MachineFolder, "config"))) {
44-
EditorUtility.DisplayDialog("Mission Pinball Framework", $"{_mpfEngine.MachineFolder} doesn't seem a valid machine folder. We expect a \"config\" subfolder in there!", "Okay");
49+
} else if (!Directory.Exists(Path.Combine(_mpfEngine.machineFolder, "config"))) {
50+
EditorUtility.DisplayDialog("Mission Pinball Framework", $"{_mpfEngine.machineFolder} doesn't seem a valid machine folder. We expect a \"config\" subfolder in there!", "Okay");
4551
} else {
4652
_mpfEngine.GetMachineDescription();
4753
}
4854
}
4955

50-
EditorGUILayout.LabelField("Switches", new GUIStyle(GUI.skin.label) { fontStyle = FontStyle.Bold });
51-
foreach (var switchDescription in _mpfEngine.MachineDescription.Switches) {
52-
EditorGUILayout.LabelField(switchDescription.HardwareNumber, switchDescription.Name);
53-
}
56+
var naStyle = new GUIStyle(GUI.skin.label) {
57+
fontStyle = FontStyle.Italic
58+
};
5459

60+
// list switches, coils and lamps
61+
if (_mpfEngine.AvailableCoils.Length + _mpfEngine.AvailableSwitches.Length + _mpfEngine.AvailableLamps.Length > 0) {
62+
if (_foldoutSwitches = EditorGUILayout.BeginFoldoutHeaderGroup(_foldoutSwitches, "Switches")) {
63+
foreach (var sw in _mpfEngine.AvailableSwitches) {
64+
EditorGUILayout.LabelField(new GUIContent($" [{sw.InternalId}] {sw.Id} ", Icons.Switch(sw.NormallyClosed, IconSize.Small)));
65+
}
66+
if (_mpfEngine.AvailableSwitches.Length == 0) {
67+
EditorGUILayout.LabelField("No switches in this machine.", naStyle);
68+
}
69+
}
70+
EditorGUILayout.EndFoldoutHeaderGroup();
5571

56-
// if (GUILayout.Button("StartGame")) {
57-
// _mpfEngine.Client.Play();
58-
// }
59-
// if (GUILayout.Button("GetMachineDescription")) {
60-
// Debug.Log(_mpfEngine.Client.GetMachineDescription());
61-
// }
72+
if (_foldoutCoils = EditorGUILayout.BeginFoldoutHeaderGroup(_foldoutCoils, "Coils")) {
73+
foreach (var sw in _mpfEngine.AvailableCoils) {
74+
EditorGUILayout.LabelField(new GUIContent($" [{sw.InternalId}] {sw.Id} ", Icons.Coil(IconSize.Small)));
75+
}
76+
if (_mpfEngine.AvailableCoils.Length == 0) {
77+
EditorGUILayout.LabelField("No coils in this machine.", naStyle);
78+
}
79+
}
80+
EditorGUILayout.EndFoldoutHeaderGroup();
81+
82+
if (_foldoutLamps = EditorGUILayout.BeginFoldoutHeaderGroup(_foldoutLamps, "Lamps")) {
83+
foreach (var sw in _mpfEngine.AvailableLamps) {
84+
EditorGUILayout.LabelField(new GUIContent($" [{sw.InternalId}] {sw.Id} ", Icons.Light(IconSize.Small)));
85+
}
86+
if (_mpfEngine.AvailableLamps.Length == 0) {
87+
EditorGUILayout.LabelField("No lamps in this machine.", naStyle);
88+
}
89+
}
90+
EditorGUILayout.EndFoldoutHeaderGroup();
91+
}
6292
}
6393
}
6494
}

VisualPinball.Engine.Mpf.Unity/Editor/VisualPinball.Engine.Mpf.Unity.Editor.asmdef

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "VisualPinball.Engine.Mpf.Unity.Editor",
33
"rootNamespace": "VisualPinball.Engine.Mpf.Unity.Editor",
44
"references": [
5+
"VisualPinball.Engine",
56
"VisualPinball.Unity",
67
"VisualPinball.Unity.Editor",
78
"VisualPinball.Engine.Mpf.Unity"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Visual Pinball Engine
2+
// Copyright (C) 2021 freezy and VPE Team
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
10+
// SOFTWARE.
11+
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using Mpf.Vpe;
15+
using VisualPinball.Engine.Game.Engines;
16+
17+
namespace VisualPinball.Engine.Mpf.Unity
18+
{
19+
public static class MpfExtensions
20+
{
21+
public static IEnumerable<GamelogicEngineSwitch> GetSwitches(this MachineDescription md)
22+
{
23+
return md.Switches.Select(sw => new GamelogicEngineSwitch(sw.Name, int.Parse(sw.HardwareNumber)) {
24+
NormallyClosed = sw.SwitchType.ToLower() == "nc"
25+
});
26+
}
27+
28+
public static IEnumerable<GamelogicEngineCoil> GetCoils(this MachineDescription md)
29+
{
30+
return md.Coils.Select(coil => new GamelogicEngineCoil(coil.Name, int.Parse(coil.HardwareNumber)));
31+
}
32+
33+
public static IEnumerable<GamelogicEngineLamp> GetLights(this MachineDescription md)
34+
{
35+
// todo color
36+
return md.Lights.Select(light => new GamelogicEngineLamp(light.Name, int.Parse(light.HardwareChannelColor)));
37+
}
38+
}
39+
}

VisualPinball.Engine.Mpf.Unity/Runtime/MpfExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VisualPinball.Engine.Mpf.Unity/Runtime/MpfGamelogicEngine.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
// SOFTWARE.
1111

1212
using System;
13-
using System.Collections.Generic;
14-
using Mpf.Vpe;
13+
using System.Linq;
1514
using UnityEngine;
1615
using VisualPinball.Engine.Game.Engines;
1716
using VisualPinball.Unity;
@@ -22,19 +21,43 @@ namespace VisualPinball.Engine.Mpf.Unity
2221
[ExecuteAlways]
2322
[DisallowMultipleComponent]
2423
[AddComponentMenu("Visual Pinball/Game Logic Engine/Mission Pinball Framework")]
25-
public class MpfGamelogicEngine : MonoBehaviour
24+
public class MpfGamelogicEngine : MonoBehaviour, IGamelogicEngine
2625
{
26+
public string Name { get; } = "Mission Pinball Framework";
27+
28+
public GamelogicEngineSwitch[] AvailableSwitches => availableSwitches;
29+
public GamelogicEngineCoil[] AvailableCoils => availableCoils;
30+
public GamelogicEngineLamp[] AvailableLamps => availableLamps;
31+
32+
public event EventHandler<LampEventArgs> OnLampChanged;
33+
public event EventHandler<LampsEventArgs> OnLampsChanged;
34+
public event EventHandler<LampColorEventArgs> OnLampColorChanged;
35+
36+
public event EventHandler<CoilEventArgs> OnCoilChanged;
37+
2738
[NonSerialized]
2839
public MpfClient Client = new MpfClient();
2940

30-
public string MachineFolder;
41+
public string machineFolder;
3142

32-
public MachineDescription MachineDescription;
43+
[SerializeField] private GamelogicEngineSwitch[] availableSwitches = new GamelogicEngineSwitch[0];
44+
[SerializeField] private GamelogicEngineCoil[] availableCoils = new GamelogicEngineCoil[0];
45+
[SerializeField] private GamelogicEngineLamp[] availableLamps = new GamelogicEngineLamp[0];
3346

34-
public void GetMachineDescription()
47+
public void OnInit(Player player, TableApi tableApi, BallManager ballManager)
3548
{
36-
MachineDescription = MpfApi.GetMachineDescription(MachineFolder);
3749
}
3850

51+
public void Switch(string id, bool isClosed)
52+
{
53+
}
54+
55+
public void GetMachineDescription()
56+
{
57+
var md = MpfApi.GetMachineDescription(machineFolder);
58+
availableSwitches = md.GetSwitches().ToArray();
59+
availableCoils = md.GetCoils().ToArray();
60+
availableLamps = md.GetLights().ToArray();
61+
}
3962
}
4063
}

0 commit comments

Comments
 (0)