namespace UIWidgets.Examples { using UIWidgets; using UnityEngine; using UnityEngine.Serialization; /// /// Notification and Dialog tests. /// public class UITestSamples : MonoBehaviour { /// /// Question icon. /// [SerializeField] [FormerlySerializedAs("questionIcon")] protected Sprite QuestionIcon; /// /// Attention icon. /// [SerializeField] [FormerlySerializedAs("attentionIcon")] protected Sprite AttentionIcon; /// /// Simple notification template. /// [SerializeField] [FormerlySerializedAs("notifySimple")] protected Notify NotifySimpleTemplate; /// /// Auto-hide notification template. /// [SerializeField] [FormerlySerializedAs("notifyAutoHide")] protected Notify NotifyAutoHideTemplate; /// /// Sample dialog template. /// [SerializeField] [FormerlySerializedAs("dialogSample")] protected Dialog DialogSampleTemplate; /// /// Sign-in dialog template. /// [SerializeField] [FormerlySerializedAs("dialogSignIn")] protected Dialog DialogSignInTemplate; /// /// TreeView dialog template. /// [SerializeField] [FormerlySerializedAs("dialogTreeView")] protected Dialog DialogTreeViewTemplate; /// /// Popup template. /// [SerializeField] [FormerlySerializedAs("popupSample")] protected Popup PopupTemplate; /// /// Modal popup template. /// [SerializeField] [FormerlySerializedAs("popupModalSample")] protected Popup PopupModalTemplate; /// /// Show sticky notification. /// public void ShowNotifySticky() { NotifySimpleTemplate.Clone().Show( "Sticky Notification. Click on the × above to close.", customHideDelay: 0f); } /// /// Show 3 notification, one by one in this order: /// - Queue Notification 3 /// - Queue Notification 2 /// - Queue Notification 1 /// public void ShowNotifyStack() { NotifySimpleTemplate.Clone().Show( "Stack Notification 1.", customHideDelay: 3f, sequenceType: NotifySequence.First); NotifySimpleTemplate.Clone().Show( "Stack Notification 2.", customHideDelay: 3f, sequenceType: NotifySequence.First); NotifySimpleTemplate.Clone().Show( "Stack Notification 3.", customHideDelay: 3f, sequenceType: NotifySequence.First); } /// /// Show 3 notification, one by one in this order: /// - Queue Notification 1. /// - Queue Notification 2. /// - Queue Notification 3. /// public void ShowNotifyQueue() { NotifySimpleTemplate.Clone().Show( "Queue Notification 1.", customHideDelay: 3f, sequenceType: NotifySequence.Last); NotifySimpleTemplate.Clone().Show( "Queue Notification 2.", customHideDelay: 3f, sequenceType: NotifySequence.Last); NotifySimpleTemplate.Clone().Show( "Queue Notification 3.", customHideDelay: 3f, sequenceType: NotifySequence.Last); } /// /// Show only one notification and hide current notifications from sequence, if exists. /// Will be displayed only Queue Notification 3. /// public void ShowNotifySequenceClear() { NotifySimpleTemplate.Clone().Show( "Stack Notification 1.", customHideDelay: 3f, sequenceType: NotifySequence.First); NotifySimpleTemplate.Clone().Show( "Stack Notification 2.", customHideDelay: 3f, sequenceType: NotifySequence.First); NotifySimpleTemplate.Clone().Show( "Stack Notification 3.", customHideDelay: 3f, sequenceType: NotifySequence.First, clearSequence: true); } /// /// Show notify and close after 3 seconds. /// public void ShowNotifyAutohide() { NotifyAutoHideTemplate.Clone().Show("Achievement unlocked. Hide after 3 seconds.", customHideDelay: 3f); } bool CallShowNotifyAutohide(DialogBase dialog, int buttonIndex) { ShowNotifyAutohide(); return true; } /// /// Show notify with rotate animation. /// public void ShowNotifyAutohideRotate() { NotifyAutoHideTemplate.Clone().Show( "Achievement unlocked. Hide after 4 seconds.", customHideDelay: 4f, hideAnimation: NotificationBase.AnimationRotateVertical); } /// /// Show notify with collapse animation. /// public void ShowNotifyBlack() { NotifyAutoHideTemplate.Clone().Show( "Another Notification. Hide after 5 seconds or click on the × above to close.", customHideDelay: 5f, hideAnimation: NotificationBase.AnimationCollapseVertical, slideUpOnHide: false); } bool ShowNotifyYes(DialogBase dialog, int buttonIndex) { NotifyAutoHideTemplate.Clone().Show("Action on 'Yes' button click.", customHideDelay: 3f); return true; } bool ShowNotifyNo(DialogBase dialog, int buttonIndex) { NotifyAutoHideTemplate.Clone().Show("Action on 'No' button click.", customHideDelay: 3f); return true; } /// /// Show simple dialog. /// public void ShowDialogSimple() { var canvas = UtilitiesUI.FindTopmostCanvas(transform).GetComponent(); var dialog = DialogSampleTemplate.Clone(); var actions = new DialogButton[] { new DialogButton("Close", DialogBase.DefaultClose), }; dialog.Show( title: "Simple Dialog", message: "Simple dialog with only close button.", buttons: actions, focusButton: "Close", canvas: canvas); } /// /// Show dialog in the same position when it was closed. /// public void ShowDialogInPosition() { var dialog = DialogSampleTemplate.Clone(); var actions = new DialogButton[] { new DialogButton("Close", Close), }; dialog.Show( title: "Simple Dialog", message: "Simple dialog with only close button.", buttons: actions, focusButton: "Close", position: dialog.transform.localPosition); } /// /// Check if dialog can be closed. /// /// Current dialog. /// Index of the clicked button. /// true if dialog can be closed; otherwise, false. public virtual bool Close(DialogBase dialog, int buttonIndex) { return true; } bool CallShowDialogSimple(DialogBase dialog, int buttonIndex) { ShowDialogSimple(); return true; } /// /// Show warning. /// public void ShowWarning() { var actions = new DialogButton[] { new DialogButton("OK", DialogBase.DefaultClose), }; DialogSampleTemplate.Clone().Show( title: "Warning window", message: "Warning test", buttons: actions, focusButton: "OK", icon: AttentionIcon); } /// /// Show dialog with Yes/No/Cancel buttons. /// public void ShowDialogYesNoCancel() { var actions = new DialogButton[] { new DialogButton("Yes", ShowNotifyYes), new DialogButton("No", ShowNotifyNo), new DialogButton("Cancel", DialogBase.DefaultClose), }; DialogSampleTemplate.Clone().Show( title: "Dialog Yes No Cancel", message: "Question?", buttons: actions, focusButton: "Yes", icon: QuestionIcon); } /// /// Show dialog with lots of text. /// public void ShowDialogExtended() { var actions = new DialogButton[] { new DialogButton("Show notification", CallShowNotifyAutohide), new DialogButton("Open simple dialog", CallShowDialogSimple), new DialogButton("Close", DialogBase.DefaultClose), }; DialogSampleTemplate.Clone().Show( title: "Another Dialog", message: "Same template with another position and long text.\nChange\nheight\nto\nfit\ntext.", buttons: actions, focusButton: "Show notification", position: new Vector3(40, -40, 0)); } /// /// Show modal dialog. /// public void ShowDialogModal() { var actions = new DialogButton[] { new DialogButton("Close", DialogBase.DefaultClose), }; DialogSampleTemplate.Clone().Show( title: "Modal Dialog", message: "Simple Modal Dialog.", buttons: actions, focusButton: "Close", modal: true, modalColor: new Color(0, 0, 0, 0.8f)); } /// /// Show sing-in dialog. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0302:Display class allocation to capture closure", Justification = "Required.")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0301:Closure Allocation Source", Justification = "Required.")] public void ShowDialogSignIn() { // create dialog from template var dialog = DialogSignInTemplate.Clone(); // helper component with references to input fields var helper = dialog.GetComponent(); // reset input fields to default helper.Refresh(); var actions = new DialogButton[] { // on click call SignInNotify new DialogButton("Sign in", SignInNotify), // on click close dialog new DialogButton("Cancel", DialogBase.DefaultClose), }; // open dialog dialog.Show( title: "Sign into your Account", buttons: actions, focusButton: "Sign in", modal: true, modalColor: new Color(0, 0, 0, 0.8f)); } // using dialog bool SignInNotify(DialogBase dialog, int index) { var helper = dialog.GetComponent(); // return true if Username.text and Password not empty; otherwise, false if (!helper.Validate()) { // return false to keep dialog open return false; } // using dialog input var message = string.Format("Sign in.\nUsername: {0}\nPassword: ", helper.UsernameAdapter.text); NotifyAutoHideTemplate.Clone().Show(message, customHideDelay: 3f); // return true to close dialog return true; } /// /// Show dialog with TreeView. /// public void ShowDialogTreeView() { // create dialog from template var dialog = DialogTreeViewTemplate.Clone(); // helper component with references to input fields var helper = dialog.GetComponent(); var actions = new DialogButton[] { // on click close dialog new DialogButton("Close", DialogBase.DefaultClose), }; // open dialog dialog.Show( title: "Dialog with TreeView", buttons: actions, focusButton: "Close", modal: true, modalColor: new Color(0, 0, 0, 0.8f)); // reset input fields to default helper.Refresh(); } /// /// Show simple popup. /// public void ShowPopup() { PopupTemplate.Clone().Show( title: "Simple Popup", message: "Simple Popup."); } /// /// Show modal popup. /// public void ShowPopupModal() { PopupModalTemplate.Clone().Show( title: "Modal Popup", message: "Alert text.", modal: true, modalColor: new Color(0.0f, 0.0f, 0.0f, 0.8f)); } } }