|
| 1 | +// Copyright 2014 Craig Courtney |
| 2 | +// Copyright 2025 Helios Contributors |
| 3 | +// |
| 4 | +// Helios is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Helios is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace GadrocsWorkshop.Helios.Controls |
| 18 | +{ |
| 19 | + using GadrocsWorkshop.Helios.Controls; |
| 20 | + using GadrocsWorkshop.Helios.ComponentModel; |
| 21 | + using GadrocsWorkshop.Helios.Controls.Capabilities; |
| 22 | + using System; |
| 23 | + using System.Xml; |
| 24 | + |
| 25 | + [HeliosControl("Helios.Base.LockingThreeWayToggleSwitch", "Locking Three Way Toggle Switches", "Three Way Toggle Switches", typeof(ThreeWayToggleSwitchRenderer), HeliosControlFlags.None)] |
| 26 | + public class ThreeWayToggleSwitchLocking : ThreeWayToggleSwitch |
| 27 | + { |
| 28 | + private HeliosValue _positionOneLockValue; |
| 29 | + private HeliosValue _positionTwoLockValue; |
| 30 | + private HeliosValue _positionThreeLockValue; |
| 31 | + |
| 32 | + private bool _positionOneLocked = false; |
| 33 | + private bool _positionTwoLocked = false; |
| 34 | + private bool _positionThreeLocked = false; |
| 35 | + public ThreeWayToggleSwitchLocking() : this("Locking Three Way Toggle Switch", new System.Windows.Size(50, 100)) {} |
| 36 | + public ThreeWayToggleSwitchLocking(string name, System.Windows.Size size) |
| 37 | + : base(name, size) |
| 38 | + { |
| 39 | + _positionOneLockValue = new HeliosValue(this, BindingValue.Empty, "", "position one lock", "Lock Position 1.", "True / False", BindingValueUnits.Boolean); |
| 40 | + _positionOneLockValue.Execute += PositionOneLock_Execute; |
| 41 | + Values.Add(_positionOneLockValue); |
| 42 | + Actions.Add(_positionOneLockValue); |
| 43 | + _positionTwoLockValue = new HeliosValue(this, BindingValue.Empty, "", "position two lock", "Lock Position 2.", "True / False", BindingValueUnits.Boolean); |
| 44 | + _positionTwoLockValue.Execute += PositionTwoLock_Execute; |
| 45 | + Values.Add(_positionTwoLockValue); |
| 46 | + Actions.Add(_positionTwoLockValue); |
| 47 | + _positionThreeLockValue = new HeliosValue(this, BindingValue.Empty, "", "position three lock", "Lock Position 3.", "True / False", BindingValueUnits.Boolean); |
| 48 | + _positionThreeLockValue.Execute += PositionThreeLock_Execute; |
| 49 | + Values.Add(_positionThreeLockValue); |
| 50 | + Actions.Add(_positionThreeLockValue); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + #region Properties |
| 55 | + |
| 56 | + #endregion |
| 57 | + |
| 58 | + void PositionOneLock_Execute(object action, HeliosActionEventArgs e) |
| 59 | + { |
| 60 | + _positionOneLocked = e.Value.BoolValue; |
| 61 | + } |
| 62 | + void PositionTwoLock_Execute(object action, HeliosActionEventArgs e) |
| 63 | + { |
| 64 | + _positionTwoLocked = e.Value.BoolValue; |
| 65 | + } |
| 66 | + void PositionThreeLock_Execute(object action, HeliosActionEventArgs e) |
| 67 | + { |
| 68 | + _positionThreeLocked = e.Value.BoolValue; |
| 69 | + } |
| 70 | + #region HeliosControl Implementation |
| 71 | + |
| 72 | + public override void Reset() |
| 73 | + { |
| 74 | + base.Reset(); |
| 75 | + |
| 76 | + BeginTriggerBypass(true); |
| 77 | + SwitchPosition = DefaultPosition; |
| 78 | + EndTriggerBypass(true); |
| 79 | + } |
| 80 | + |
| 81 | + protected override void ThrowSwitch(ToggleSwitchBase.SwitchAction action) |
| 82 | + { |
| 83 | + if (action == SwitchAction.Increment) |
| 84 | + { |
| 85 | + switch (SwitchPosition) |
| 86 | + { |
| 87 | + case ThreeWayToggleSwitchPosition.One: |
| 88 | + if (!_positionTwoLocked) |
| 89 | + { |
| 90 | + SwitchPosition = ThreeWayToggleSwitchPosition.Two; |
| 91 | + } |
| 92 | + break; |
| 93 | + case ThreeWayToggleSwitchPosition.Two: |
| 94 | + if (!_positionThreeLocked) |
| 95 | + { |
| 96 | + SwitchPosition = ThreeWayToggleSwitchPosition.Three; |
| 97 | + } |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + else if (action == SwitchAction.Decrement) |
| 102 | + { |
| 103 | + switch (SwitchPosition) |
| 104 | + { |
| 105 | + case ThreeWayToggleSwitchPosition.Two: |
| 106 | + if (!_positionOneLocked) |
| 107 | + { |
| 108 | + SwitchPosition = ThreeWayToggleSwitchPosition.One; |
| 109 | + } |
| 110 | + break; |
| 111 | + case ThreeWayToggleSwitchPosition.Three: |
| 112 | + if (!_positionTwoLocked) |
| 113 | + { |
| 114 | + SwitchPosition = ThreeWayToggleSwitchPosition.Two; |
| 115 | + } |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public override void MouseUp(System.Windows.Point location) |
| 122 | + { |
| 123 | + base.MouseUp(location); |
| 124 | + |
| 125 | + switch (SwitchPosition) |
| 126 | + { |
| 127 | + case ThreeWayToggleSwitchPosition.One: |
| 128 | + if (SwitchType == ThreeWayToggleSwitchType.MomOnMom || SwitchType == ThreeWayToggleSwitchType.MomOnOn) |
| 129 | + { |
| 130 | + if (!_positionTwoLocked) |
| 131 | + { |
| 132 | + SwitchPosition = ThreeWayToggleSwitchPosition.Two; |
| 133 | + } |
| 134 | + } |
| 135 | + break; |
| 136 | + case ThreeWayToggleSwitchPosition.Three: |
| 137 | + if (SwitchType == ThreeWayToggleSwitchType.OnOnMom || SwitchType == ThreeWayToggleSwitchType.MomOnMom) |
| 138 | + { |
| 139 | + if (!_positionTwoLocked) |
| 140 | + { |
| 141 | + SwitchPosition = ThreeWayToggleSwitchPosition.Two; |
| 142 | + } |
| 143 | + } |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + public override void WriteXml(XmlWriter writer) |
| 149 | + { |
| 150 | + base.WriteXml(writer); |
| 151 | + } |
| 152 | + |
| 153 | + public override void ReadXml(XmlReader reader) |
| 154 | + { |
| 155 | + base.ReadXml(reader); |
| 156 | + } |
| 157 | + |
| 158 | + #endregion |
| 159 | + |
| 160 | + #region Actions |
| 161 | + |
| 162 | + void SetPositionAction_Execute(object action, HeliosActionEventArgs e) |
| 163 | + { |
| 164 | + try |
| 165 | + { |
| 166 | + BeginTriggerBypass(e.BypassCascadingTriggers); |
| 167 | + int newPosition = 0; |
| 168 | + if (int.TryParse(e.Value.StringValue, out newPosition)) |
| 169 | + { |
| 170 | + if (newPosition > 0 && newPosition <= 3) |
| 171 | + { |
| 172 | + SwitchPosition = (ThreeWayToggleSwitchPosition)newPosition; |
| 173 | + } |
| 174 | + } |
| 175 | + EndTriggerBypass(e.BypassCascadingTriggers); |
| 176 | + } |
| 177 | + catch |
| 178 | + { |
| 179 | + // No-op if the parse fails we won't set the position. |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + #endregion |
| 184 | + } |
| 185 | +} |
0 commit comments