using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharpPro; using Crestron.SimplSharpPro.DeviceSupport; using PepperDash.Core; namespace PepperDash.Essentials.Core.SmartObjects { public class SmartObjectDynamicList : SmartObjectHelperBase { public const string SigNameScrollToItem = "Scroll To Item"; public const string SigNameSetNumberOfItems = "Set Number of Items"; public uint NameSigOffset { get; private set; } public ushort Count { get { return SmartObject.UShortInput[SigNameSetNumberOfItems].UShortValue; } set { SmartObject.UShortInput[SigNameSetNumberOfItems].UShortValue = value; } } /// /// The limit of the list object, as defined by VTPro settings. Zero if object /// is not a list /// public int MaxCount { get; private set; } public SmartObjectDynamicList(SmartObject so, bool useUserObjectHandler, uint nameSigOffset) : base(so, useUserObjectHandler) { try { // Just try to touch the count signal to make sure this is indeed a dynamic list var c = Count; NameSigOffset = nameSigOffset; MaxCount = SmartObject.BooleanOutput.Count(s => s.Name.EndsWith("Pressed")); //Debug.Console(2, "Smart object {0} has {1} max", so.ID, MaxCount); } catch { var msg = string.Format("SmartObjectDynamicList: Smart Object {0:X2}-{1} is not a dynamic list. Ignoring", so.Device.ID, so.ID); Debug.Console(0, Debug.ErrorLogLevel.Error, msg); } } /// /// Builds a new list item /// public void SetItem(uint index, string mainText, string iconName, Action action) { SetItemMainText(index, mainText); SetItemIcon(index, iconName); SetItemButtonAction(index, action); //try //{ // SetMainButtonText(index, text); // SetIcon(index, iconName); // SetButtonAction(index, action); //} //catch(Exception e) //{ // Debug.Console(1, "Cannot set Dynamic List item {0} on smart object {1}", index, SmartObject.ID); // ErrorLog.Warn(e.ToString()); //} } public void SetItemMainText(uint index, string text) { if (index > MaxCount) return; // The list item template defines CIPS tags that refer to standard joins (SmartObject.Device as BasicTriList).StringInput[NameSigOffset + index].StringValue = text; } public void SetItemIcon(uint index, string iconName) { if (index > MaxCount) return; SmartObject.StringInput[string.Format("Set Item {0} Icon Serial", index)].StringValue = iconName; } public void SetItemButtonAction(uint index, Action action) { if (index > MaxCount) return; SmartObject.BooleanOutput[string.Format("Item {0} Pressed", index)].UserObject = action; } /// /// Sets the feedback on the given line, clearing others when interlocked is set /// public void SetFeedback(uint index, bool interlocked) { if (interlocked) ClearFeedbacks(); SmartObject.BooleanInput[string.Format("Item {0} Selected", index)].BoolValue = true; } /// /// Clears all button feedbacks /// public void ClearFeedbacks() { for(int i = 1; i<= Count; i++) SmartObject.BooleanInput[string.Format("Item {0} Selected", i)].BoolValue = false; } /// /// Removes Action object from all buttons /// public void ClearActions() { Debug.Console(2, "SO CLEAR"); for(ushort i = 1; i <= MaxCount; i++) SmartObject.BooleanOutput[string.Format("Item {0} Pressed", i)].UserObject = null; } } }