using System; using Crestron.SimplSharp; using Crestron.SimplSharpPro.DeviceSupport; using PepperDash.Core; namespace PepperDash.Essentials.Core { public class ModalDialog { /// /// Bool press 3991 /// public const uint Button1Join = 3991; /// /// Bool press 3992 /// public const uint Button2Join = 3992; /// /// 3993 /// public const uint CancelButtonJoin = 3993; /// ///For visibility of single button. Bool feedback 3994 /// public const uint OneButtonVisibleJoin = 3994; /// /// For visibility of two buttons. Bool feedback 3995. /// public const uint TwoButtonVisibleJoin = 3995; /// /// Shows the timer guage if in use. Bool feedback 3996 /// public const uint TimerVisibleJoin = 3996; /// /// Visibility join to show "X" button 3997 /// public const uint CancelVisibleJoin = 3997; /// /// Shows the modal subpage. Boolean feeback join 3999 /// public const uint ModalVisibleJoin = 3999; /// /// The seconds value of the countdown timer. Ushort join 3991 /// //public const uint TimerSecondsJoin = 3991; /// /// The full ushort value of the countdown timer for a gauge. Ushort join 3992 /// public const uint TimerGaugeJoin = 3992; /// /// Text on button one. String join 3991 /// public const uint Button1TextJoin = 3991; /// /// Text on button two. String join 3992 /// public const uint Button2TextJoin = 3992; /// /// Message text. String join 3994 /// public const uint MessageTextJoin = 3994; /// /// Title text. String join 3995 /// public const uint TitleTextJoin = 3995; /// /// Icon name. String join 3996 /// public const uint IconNameJoin = 3996; /// /// Returns true when modal is showing /// public bool ModalIsVisible { get { return TriList.BooleanInput[ModalVisibleJoin].BoolValue; } } /// /// /// public bool CanCancel { get; private set; } BasicTriList TriList; Action ModalCompleteAction; static object CompleteActionLock = new object(); /// /// Creates a new modal to be shown on provided TriList /// /// public ModalDialog(BasicTriList triList) { TriList = triList; // Attach actions to buttons triList.SetSigFalseAction(Button1Join, () => OnModalComplete(1)); triList.SetSigFalseAction(Button2Join, () => OnModalComplete(2)); triList.SetSigFalseAction(CancelButtonJoin, () => { if (CanCancel) CancelDialog(); }); CanCancel = true; } /// /// Shows the dialog /// /// Number of buttons to show. 0, 1, 2 /// The amount of time to show the dialog. Use 0 for no timeout. /// If the progress bar gauge needs to count down instead of up /// The action to run when the dialog is dismissed. Parameter will be 1 or 2 if button pressed, or 0 if dialog times out /// True when modal is created. public bool PresentModalDialog(uint numberOfButtons, string title, string iconName, string message, string button1Text, string button2Text, bool showGauge, bool showCancel, Action completeAction) { // Don't reset dialog if visible now if (!ModalIsVisible) { ModalCompleteAction = completeAction; TriList.StringInput[TitleTextJoin].StringValue = title; if (string.IsNullOrEmpty(iconName)) iconName = "Blank"; TriList.StringInput[IconNameJoin].StringValue = iconName; TriList.StringInput[MessageTextJoin].StringValue = message; if (numberOfButtons == 0) { // Show no buttons TriList.BooleanInput[OneButtonVisibleJoin].BoolValue = false; TriList.BooleanInput[TwoButtonVisibleJoin].BoolValue = false; } else if (numberOfButtons == 1) { // Show one button TriList.BooleanInput[OneButtonVisibleJoin].BoolValue = true; TriList.BooleanInput[TwoButtonVisibleJoin].BoolValue = false; TriList.StringInput[Button1TextJoin].StringValue = button1Text; } else if (numberOfButtons == 2) { // Show two TriList.BooleanInput[OneButtonVisibleJoin].BoolValue = false; TriList.BooleanInput[TwoButtonVisibleJoin].BoolValue = true; TriList.StringInput[Button1TextJoin].StringValue = button1Text; TriList.StringInput[Button2TextJoin].StringValue = button2Text; } // Show/hide guage TriList.BooleanInput[TimerVisibleJoin].BoolValue = showGauge; CanCancel = showCancel; TriList.BooleanInput[CancelVisibleJoin].BoolValue = showCancel; //Reveal and activate TriList.BooleanInput[ModalVisibleJoin].BoolValue = true; WakePanel(); return true; } return false; } /// /// Wakes the panel by turning on the backlight if off /// public void WakePanel() { try { var panel = TriList as TswFt5Button; if (panel != null && panel.ExtenderSystemReservedSigs.BacklightOffFeedback.BoolValue) panel.ExtenderSystemReservedSigs.BacklightOn(); } catch { Debug.Console(1, "Error Waking Panel. Maybe testing with Xpanel?"); } } /// /// Hide dialog from elsewhere, fires CompleteAction /// public void CancelDialog() { OnModalComplete(0); } /// /// Hides dialog. Fires no action /// public void HideDialog() { TriList.BooleanInput[ModalVisibleJoin].BoolValue = false; } // When the modal is cleared or times out, clean up the various bits void OnModalComplete(uint buttonNum) { TriList.BooleanInput[ModalVisibleJoin].BoolValue = false; var action = ModalCompleteAction; if (action != null) action(buttonNum); } } }