internal static ControlOverview Open(bool onload = false) { BlockHandlerController.InitializeBuildingBlockIDs(); foreach (ControlOverview x in ACM.Instance.gameObject.GetComponents <ControlOverview>()) { Destroy(x); } var instance = ACM.Instance.gameObject.AddComponent <ControlOverview>(); instance.enabled = false; instance.windowRect.x = Screen.width - 280 - 400; instance.windowRect.y = 200; instance.RefreshOverview(); if (onload) { if (instance.AxisList.TrueForAll(name => { var axis = AxisManager.Get(name); return(axis != null && (axis.Status == AxisStatus.OK || axis.Status == AxisStatus.NotRunning)); })) { Destroy(instance); return(null); } } instance.enabled = true; return(instance); }
/// <summary> /// Called on every frame to apply the value. /// </summary> protected virtual void Update() { if (Game.IsSimulating) { var axis = AxisManager.Get(Axis); if (Enabled && Block != null && axis != null && axis.Status == AxisStatus.OK) { Apply(axis.OutputValue); } } }
/// <summary> /// Returns a list of all active controls on a block. /// A control is active if it is enabled and has bound an axis. /// </summary> /// <param name="GUID">GUID of the block.</param> /// <returns>Returns a list of controls.</returns> public static List <Control> GetActiveBlockControls(Guid GUID) { var list = new List <Control>(); if (Blocks.ContainsKey(GUID)) { foreach (Control c in Blocks[GUID]) { if (c.Enabled && AxisManager.Get(c.Axis) != null) { list.Add(c); } } } return(list); }
/// <summary> /// Is called on OnInitialisation to retrieve BlockHandler reference. /// If the control has no associated block, it is deleted. /// </summary> protected virtual void Initialise() { try { Block = BlockHandlerController.GetBlock(BlockGUID); var axis = AxisManager.Get(Axis); if (Enabled && Block != null && axis != null && axis.Status == AxisStatus.OK) { ClearKeys(); } } catch (BlockNotFoundException) { Block = null; ControlManager.Blocks.Remove(BlockGUID); } }
public void DrawAxis(Rect windowRect) { // Draw graphs Rect graphRect = new Rect( GUI.skin.window.padding.left, GUI.skin.window.padding.top + 36, windowRect.width - GUI.skin.window.padding.left - GUI.skin.window.padding.right, 20); Rect leftGraphRect = new Rect( GUI.skin.window.padding.left, GUI.skin.window.padding.top + 100, (windowRect.width - GUI.skin.window.padding.left - GUI.skin.window.padding.right) / 2 - 4, 20); Rect rightGraphRect = new Rect( GUI.skin.window.padding.left + leftGraphRect.width + 8, GUI.skin.window.padding.top + 100, (windowRect.width - GUI.skin.window.padding.left - GUI.skin.window.padding.right) / 2 - 4, 20); Util.DrawRect(graphRect, Color.gray); Util.FillRect(new Rect( graphRect.x + graphRect.width / 2, graphRect.y, 1, graphRect.height), Color.gray); Util.DrawRect(leftGraphRect, Color.gray); Util.FillRect(new Rect( leftGraphRect.x + leftGraphRect.width / 2, leftGraphRect.y, 1, leftGraphRect.height), Color.gray); Util.DrawRect(rightGraphRect, Color.gray); Util.FillRect(new Rect( rightGraphRect.x + rightGraphRect.width / 2, rightGraphRect.y, 1, rightGraphRect.height), Color.gray); var axis_a = AxisManager.Get(Axis.SubAxis1); var axis_b = AxisManager.Get(Axis.SubAxis2); float a = axis_a != null ? axis_a.OutputValue : 0; float b = axis_b != null ? axis_b.OutputValue : 0; // Draw axis value GUILayout.Label(" <color=#808080><b>" + Axis.OutputValue.ToString("0.00") + "</b></color>", new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleLeft }, GUILayout.Height(20)); // Draw method select int i = (int)Axis.Method; int num_methods = Enum.GetValues(typeof(ChainAxis.ChainMethod)).Length; GUILayout.BeginHorizontal(); if (GUILayout.Button("<", Elements.Buttons.Default, GUILayout.Width(30))) { i--; } if (i < 0) { i += num_methods; } GUILayout.Label(Enum.GetNames(typeof(ChainAxis.ChainMethod))[i], new GUIStyle(Elements.InputFields.Default) { alignment = TextAnchor.MiddleCenter }); if (GUILayout.Button(">", Elements.Buttons.Default, GUILayout.Width(30))) { i++; } if (i == num_methods) { i = 0; } Axis.Method = (ChainAxis.ChainMethod)i; GUILayout.EndHorizontal(); // Draw sub axis values GUILayout.BeginHorizontal(GUILayout.Height(20)); GUILayout.Label(" <color=#808080><b>" + (axis_a == null ? "" : axis_a.Status == AxisStatus.OK ? a.ToString("0.00") : InputAxis.GetStatusString(axis_a.Status)) + "</b></color>", new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleLeft }, GUILayout.MinWidth(leftGraphRect.width), GUILayout.Height(20)); GUILayout.Label(" <color=#808080><b>" + (axis_b == null ? "" : axis_b.Status == AxisStatus.OK ? b.ToString("0.00") : InputAxis.GetStatusString(axis_a.Status)) + "</b></color>", new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleLeft, margin = new RectOffset(8, 0, 0, 0) }, GUILayout.MinWidth(rightGraphRect.width), GUILayout.Height(20)); GUILayout.EndHorizontal(); // Draw yellow lines if (Axis.Status == AxisStatus.OK) { Util.FillRect(new Rect( graphRect.x + graphRect.width / 2 + graphRect.width / 2 * Axis.OutputValue, graphRect.y, 1, graphRect.height), Color.yellow); } if (axis_a != null && Axis.Status == AxisStatus.OK) { Util.FillRect(new Rect( leftGraphRect.x + leftGraphRect.width / 2 + leftGraphRect.width / 2 * a, leftGraphRect.y, 1, leftGraphRect.height), Color.yellow); } if (axis_b != null && Axis.Status == AxisStatus.OK) { Util.FillRect(new Rect( rightGraphRect.x + rightGraphRect.width / 2 + rightGraphRect.width / 2 * b, rightGraphRect.y, 1, rightGraphRect.height), Color.yellow); } // Draw axis select buttons GUILayout.BeginHorizontal(); var buttonRect = GUILayoutUtility.GetRect(new GUIContent(" "), spaar.ModLoader.UI.Elements.Buttons.Default, GUILayout.MaxWidth(leftGraphRect.width)); if (Axis.SubAxis1 == null) { if (GUI.Button(buttonRect, "Select Input Axis", spaar.ModLoader.UI.Elements.Buttons.Disabled)) { error = null; var callback = new SelectAxisDelegate((InputAxis axis) => { try { Axis.SubAxis1 = axis.Name; } catch (InvalidOperationException e) { error = "<color=#FFFF00><b>Chain cycle error</b></color>\n" + e.Message; } }); if (popup == null) { popup = AxisSelector.Open(callback, true); } else { popup.Callback = callback; } popup.windowRect.x = windowRect.x + buttonRect.x - 8; popup.windowRect.y = windowRect.y + buttonRect.y - 8; } } else { if (GUI.Button(buttonRect, Axis.SubAxis1, axis_a != null ? axis_a.Saveable ? spaar.ModLoader.UI.Elements.Buttons.Default : spaar.ModLoader.UI.Elements.Buttons.Disabled : spaar.ModLoader.UI.Elements.Buttons.Red)) { error = null; var callback = new SelectAxisDelegate((InputAxis axis) => { try { Axis.SubAxis1 = axis.Name; } catch (InvalidOperationException e) { error = "<color=#FFFF00><b>Chain cycle error</b></color>\n" + e.Message; } }); if (popup == null) { popup = AxisSelector.Open(callback, true); } else { popup.Callback = callback; } popup.windowRect.x = windowRect.x + buttonRect.x - 8; popup.windowRect.y = windowRect.y + buttonRect.y - 8; } } if (Axis.SubAxis2 == null) { if (GUILayout.Button("Select Input Axis", spaar.ModLoader.UI.Elements.Buttons.Disabled, GUILayout.MaxWidth(rightGraphRect.width))) { error = null; var callback = new SelectAxisDelegate((InputAxis axis) => { try { Axis.SubAxis2 = axis.Name; } catch (InvalidOperationException e) { error = "<color=#FFFF00><b>Chain cycle error</b></color>\n" + e.Message; } }); if (popup == null) { popup = AxisSelector.Open(callback, true); } else { popup.Callback = callback; } popup.windowRect.x = windowRect.x + buttonRect.x - 8; popup.windowRect.y = windowRect.y + buttonRect.y - 8; } } else { if (GUILayout.Button(Axis.SubAxis2, axis_b != null ? axis_b.Saveable ? spaar.ModLoader.UI.Elements.Buttons.Default : spaar.ModLoader.UI.Elements.Buttons.Disabled : spaar.ModLoader.UI.Elements.Buttons.Red, GUILayout.MaxWidth(rightGraphRect.width))) { error = null; var callback = new SelectAxisDelegate((InputAxis axis) => { try { Axis.SubAxis2 = axis.Name; } catch (InvalidOperationException e) { error = "<color=#FFFF00><b>Chain cycle error</b></color>\n" + e.Message; } }); if (popup == null) { popup = AxisSelector.Open(callback, true); } else { popup.Callback = callback; } popup.windowRect.x = windowRect.x + buttonRect.x - 8; popup.windowRect.y = windowRect.y + buttonRect.y - 8; } } GUILayout.EndHorizontal(); // Check for mouse exit if (popup != null && !popup.ContainsMouse) { GameObject.Destroy(popup); } }
internal static void Save(MachineInfo machineInfo) { try { var axisStack = new Stack <string>(); var axisList = new List <string>(); // save all controls and collect all bound axes foreach (BlockInfo blockInfo in machineInfo.Blocks) { if (ControlManager.Blocks.ContainsKey(blockInfo.Guid)) { var controls = ControlManager.GetActiveBlockControls(blockInfo.Guid); if (controls.Count == 0) { continue; } var control_names = new List <string>(); foreach (Control c in controls) { if (!axisStack.Contains(c.Axis)) { axisStack.Push(c.Axis); } control_names.Add(c.Name); c.Save(blockInfo); } blockInfo.BlockData.Write("ac-controllist", control_names.ToArray()); } } // go through stack and save all axes and chained axes while (axisStack.Count > 0) { var a = AxisManager.Get(axisStack.Pop()); if (a == null || axisList.Contains(a.Name)) { continue; } axisList.Add(a.Name); if (a.Type == AxisType.Chain) { var chain = a as ChainAxis; axisStack.Push(chain.SubAxis1); axisStack.Push(chain.SubAxis2); } if (a.Type == AxisType.Custom) { var custom = a as CustomAxis; foreach (var linked in custom.LinkedAxes) { axisStack.Push(linked); } } a.Save(machineInfo); } // save axis list and metadata if (axisList.Count != 0) { machineInfo.MachineData.Write("ac-version", Assembly.GetExecutingAssembly().GetName().Version.ToString()); machineInfo.MachineData.Write("ac-axislist", axisList.ToArray()); } } catch (Exception e) { Debug.Log("[ACM]: Error saving machine's controls."); Debug.LogException(e); } }
/// <summary> /// Returns an axis with given name. /// Looks in machine axes first, then in locally saved axes. /// This is the preferred method. /// </summary> /// <param name="name">Name of the axis.</param> /// <returns></returns> public static InputAxis GetAxis(string name) { return(AxisManager.Get(name)); }
private void DrawControl(Control c) { // Draw control label GUILayout.Label(c.Name, Elements.Labels.Title); // Draw axis select button GUILayout.BeginHorizontal(); var buttonRect = GUILayoutUtility.GetRect(GUIContent.none, Elements.Buttons.Default); if (c.Axis == null) { if (GUI.Button(buttonRect, "Select Input Axis", Elements.Buttons.Disabled)) { var callback = new SelectAxisDelegate((InputAxis axis) => { c.Axis = axis.Name; c.Enabled = true; }); if (popup == null) { popup = AxisSelector.Open(callback, true); } else { popup.Callback = callback; } popup.windowRect.x = windowRect.x + buttonRect.x - 8; popup.windowRect.y = windowRect.y + buttonRect.y - 8; } } else { var a = AxisManager.Get(c.Axis); if (GUI.Button(buttonRect, c.Axis, a != null ? a.Saveable ? Elements.Buttons.Default : Elements.Buttons.Disabled : Elements.Buttons.Red)) { var callback = new SelectAxisDelegate((InputAxis axis) => { c.Axis = axis.Name; c.Enabled = true; }); if (popup == null) { popup = AxisSelector.Open(callback, true); } else { popup.Callback = callback; } popup.windowRect.x = windowRect.x + buttonRect.x - 8; popup.windowRect.y = windowRect.y + buttonRect.y - 8; } if (a != null && GUILayout.Button("✎", new GUIStyle(Elements.Buttons.Default) { fontSize = 20, padding = new RectOffset(-3, 0, 0, 0) }, GUILayout.Width(30), GUILayout.MaxHeight(28))) { var Editor = ACM.Instance.gameObject.AddComponent <AxisEditorWindow>(); Editor.windowRect.x = Mathf.Clamp(windowRect.x + windowRect.width, -320 + GUI.skin.window.padding.top, Screen.width - GUI.skin.window.padding.top); Editor.windowRect.y = Mathf.Clamp(windowRect.y, 0, Screen.height - GUI.skin.window.padding.top); Editor.EditAxis(a, new SelectAxisDelegate((InputAxis new_axis) => { c.Axis = new_axis.Name; })); } if (GUILayout.Button("×", Elements.Buttons.Red, GUILayout.Width(30))) { c.Enabled = false; c.Axis = null; } } GUILayout.EndHorizontal(); if (c.Enabled) { // Draw graph var axis = AxisManager.Get(c.Axis); float axis_value = axis != null ? axis.OutputValue : 0; float control_value = 0; string text; if (axis == null) { text = "NOT FOUND"; } else { if (axis_value > 0) { control_value = Mathf.Lerp(c.Center, c.Max, axis_value); } else { control_value = Mathf.Lerp(c.Center, c.Min, -axis_value); } if (axis.Status == AxisStatus.OK) { text = control_value.ToString("0.00"); } else { text = InputAxis.GetStatusString(axis.Status); } } GUILayout.Label("<color=#808080><b>" + text + "</b></color>", new GUIStyle(Elements.Labels.Default) { padding = new RectOffset(38, 38, 4, 0), richText = true, alignment = TextAnchor.MiddleLeft }, GUILayout.Height(20)); var graphRect = GUILayoutUtility.GetLastRect(); graphRect.x += 30; graphRect.height += 44; graphRect.width -= 60; Util.DrawRect(graphRect, Color.gray); Util.FillRect(new Rect( graphRect.x + graphRect.width / 2, graphRect.y, 1, graphRect.height), Color.gray); if (axis != null && axis.Status == AxisStatus.OK) { Util.FillRect(new Rect( graphRect.x + graphRect.width / 2 + graphRect.width / 2 * axis_value, graphRect.y, 1, graphRect.height), Color.yellow); } // Draw invert button if (GUI.Button(new Rect(graphRect.x + graphRect.width - 28, graphRect.y, 28, 28), "<size=24><color=#808080>⇄</color></size>", Elements.Labels.Default)) { c.Invert(); } // Draw interval input fields GUILayout.BeginHorizontal(); { var oldColor = GUI.backgroundColor; GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f, 0.7f); float min_parsed = c.Min; c.min_string = Regex.Replace( GUILayout.TextField( c.min_string, new GUIStyle(Elements.InputFields.Default) { alignment = TextAnchor.MiddleCenter }, GUILayout.Width(60)), @"[^0-9\-.]", ""); if (c.min_string != c.Min.ToString() && !c.min_string.EndsWith(".0") && !c.min_string.EndsWith(".") && c.min_string != "-" && c.min_string != "-0") { float.TryParse(c.min_string, out min_parsed); c.Min = min_parsed; c.min_string = c.Min.ToString(); } GUILayout.FlexibleSpace(); float cen_parsed = c.Center; c.cen_string = Regex.Replace( GUILayout.TextField( c.cen_string, new GUIStyle(Elements.InputFields.Default) { alignment = TextAnchor.MiddleCenter }, GUILayout.Width(60)), @"[^0-9\-.]", ""); if (c.cen_string != c.Center.ToString() && !c.cen_string.EndsWith(".0") && !c.cen_string.EndsWith(".") && c.cen_string != "-" && c.cen_string != "-0") { float.TryParse(c.cen_string, out cen_parsed); c.Center = cen_parsed; c.cen_string = c.Center.ToString(); } GUILayout.FlexibleSpace(); float max_parsed = c.Max; c.max_string = Regex.Replace( GUILayout.TextField( c.max_string, new GUIStyle(Elements.InputFields.Default) { alignment = TextAnchor.MiddleCenter }, GUILayout.Width(60)), @"[^0-9\-.]", ""); if (c.max_string != c.Max.ToString() && !c.max_string.EndsWith(".0") && !c.max_string.EndsWith(".") && c.max_string != "-" && c.max_string != "-0") { float.TryParse(c.max_string, out max_parsed); c.Max = max_parsed; c.max_string = c.Max.ToString(); } GUI.backgroundColor = oldColor; } GUILayout.EndHorizontal(); } }
private void DrawAxis(string axis) { GUILayout.BeginHorizontal(); var buttonRect = GUILayoutUtility.GetRect(new GUIContent(" "), Elements.Buttons.Default); var a = AxisManager.Get(axis); if (GUI.Button(buttonRect, axis, a != null ? a.Saveable ? Elements.Buttons.Default : Elements.Buttons.Disabled : Elements.Buttons.Red)) { var callback = new SelectAxisDelegate((InputAxis new_axis) => { AssignAxis(axis, new_axis.Name); }); if (popup == null) { popup = AxisSelector.Open(callback, true); } else { popup.Callback = callback; } popup.windowRect.x = windowRect.x + buttonRect.x - 8; popup.windowRect.y = windowRect.y + GUI.skin.window.padding.top + buttonRect.y - scrollPosition.y - 8; } if (a != null && GUILayout.Button("✎", new GUIStyle(Elements.Buttons.Default) { fontSize = 20, padding = new RectOffset(-3, 0, 0, 0) }, GUILayout.Width(30), GUILayout.MaxHeight(28))) { var Editor = ACM.Instance.gameObject.AddComponent <AxisEditorWindow>(); Editor.windowRect.x = Mathf.Clamp(windowRect.x + windowRect.width, -320 + GUI.skin.window.padding.top, Screen.width - GUI.skin.window.padding.top); Editor.windowRect.y = Mathf.Clamp(windowRect.y, 0, Screen.height - GUI.skin.window.padding.top); Editor.EditAxis(a, new SelectAxisDelegate((InputAxis new_axis) => { AssignAxis(axis, new_axis.Name); })); } GUILayout.EndHorizontal(); // Draw graph string text; if (a == null) { text = "NOT FOUND"; } else if (a.Status != AxisStatus.OK) { text = InputAxis.GetStatusString(a.Status); } else { text = ""; } GUILayout.Label(" <color=#808080><b>" + text + "</b></color>", new GUIStyle(Elements.Labels.Default) { richText = true, alignment = TextAnchor.MiddleLeft, margin = new RectOffset(8, 8, 8, 8) }, GUILayout.Height(20)); var graphRect = GUILayoutUtility.GetLastRect(); Util.DrawRect(graphRect, Color.gray); Util.FillRect(new Rect( graphRect.x + graphRect.width / 2, graphRect.y, 1, graphRect.height), Color.gray); if (a != null && a.Status == AxisStatus.OK) { Util.FillRect(new Rect( graphRect.x + graphRect.width / 2 + graphRect.width / 2 * a.OutputValue, graphRect.y, 1, graphRect.height), Color.yellow); } // Draw assigned controls list foreach (Control c in Controls[axis]) { GUILayout.BeginHorizontal(); if (GUILayout.Button("×", new GUIStyle(Elements.Buttons.Red) { margin = new RectOffset(8, 8, 0, 0) }, GUILayout.Width(20), GUILayout.Height(20))) { c.Axis = null; c.Enabled = false; } string block_name; try { block_name = BlockHandlerController.GetID(c.BlockGUID); } catch { block_name = "..."; } GUILayout.Label("<b>" + c.Name + "</b> for " + block_name, Elements.Labels.LogEntry); GUILayout.EndHorizontal(); } GUILayout.Box(GUIContent.none, GUILayout.Height(8)); }