|
| 1 | +using System; |
| 2 | +using System.Windows.Forms; |
| 3 | +using InputManager; |
| 4 | + |
| 5 | +namespace WiiBalanceWalker |
| 6 | +{ |
| 7 | + /// <summary>Actions place holders as controls are passed after they load.</summary> |
| 8 | + public class ActionList |
| 9 | + { |
| 10 | + public ActionItem Left; |
| 11 | + public ActionItem Right; |
| 12 | + public ActionItem Forward; |
| 13 | + public ActionItem Backward; |
| 14 | + public ActionItem Modifier; |
| 15 | + public ActionItem Jump; |
| 16 | + public ActionItem DiagonalLeft; |
| 17 | + public ActionItem DiagonalRight; |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary>Abstracts away selecting, saving, and sending multiple input types.</summary> |
| 21 | + public class ActionItem |
| 22 | + { |
| 23 | + public bool IsActive { get; private set; } |
| 24 | + string settingName; |
| 25 | + string inputText; |
| 26 | + int inputAmount; |
| 27 | + int inputType; |
| 28 | + Keys inputKeys; |
| 29 | + Mouse.MouseKeys inputMouseKeys; |
| 30 | + System.Timers.Timer inputTimer = new System.Timers.Timer() { Interval = 2, Enabled = false }; |
| 31 | + |
| 32 | + public ActionItem(string settingName, ComboBox controlType, NumericUpDown controlAmount) |
| 33 | + { |
| 34 | + // The name where the action should be saved. |
| 35 | + |
| 36 | + this.settingName = settingName; |
| 37 | + |
| 38 | + // Track changes to the controls. |
| 39 | + |
| 40 | + controlType.SelectedIndexChanged += new EventHandler(ControlType_SelectedIndexChanged); |
| 41 | + controlAmount.ValueChanged += new EventHandler(ControlAmount_ValueChanged); |
| 42 | + |
| 43 | + // Add nothing and mouse movement options to control. |
| 44 | + |
| 45 | + controlType.Items.Add(new ItemWithText("", "Do Nothing")); |
| 46 | + controlType.Items.Add(new ItemWithText("MouseMoveX", "Mouse Move X")); |
| 47 | + controlType.Items.Add(new ItemWithText("MouseMoveY", "Mouse Move Y")); |
| 48 | + |
| 49 | + // Add mouse button options to control. |
| 50 | + |
| 51 | + foreach (Mouse.MouseKeys item in Enum.GetValues(typeof(Mouse.MouseKeys))) |
| 52 | + { |
| 53 | + controlType.Items.Add(new ItemWithText(item, "Mouse Hold " + item)); |
| 54 | + } |
| 55 | + |
| 56 | + // Add keyboard options to control. |
| 57 | + |
| 58 | + foreach (Keys item in Enum.GetValues(typeof(Keys))) |
| 59 | + { |
| 60 | + controlType.Items.Add(new ItemWithText(item, "Key " + item)); |
| 61 | + } |
| 62 | + |
| 63 | + // Get the last known settings. |
| 64 | + |
| 65 | + inputText = (string)Properties.Settings.Default["Action" + settingName]; |
| 66 | + inputAmount = (int)Properties.Settings.Default["Amount" + settingName]; |
| 67 | + |
| 68 | + // Put the controls to the last known settings. |
| 69 | + |
| 70 | + controlType.Text = inputText; |
| 71 | + controlAmount.Value = inputAmount; |
| 72 | + |
| 73 | + |
| 74 | + inputTimer.Elapsed += new System.Timers.ElapsedEventHandler(inputTimer_Elapsed); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + public void ControlType_SelectedIndexChanged(object sender, EventArgs e) |
| 79 | + { |
| 80 | + // Convert control generic object back into item. |
| 81 | + |
| 82 | + var itemWithText = (ItemWithText)((ComboBox)sender).SelectedItem; |
| 83 | + var itemTypeName = itemWithText.Item.GetType().Name; |
| 84 | + |
| 85 | + // Set the active item. |
| 86 | + |
| 87 | + if (itemTypeName == "Keys") |
| 88 | + { |
| 89 | + inputKeys = (Keys)itemWithText.Item; |
| 90 | + inputType = 1; |
| 91 | + } |
| 92 | + else if (itemTypeName == "MouseKeys") |
| 93 | + { |
| 94 | + inputType = 2; |
| 95 | + inputMouseKeys = (Mouse.MouseKeys)itemWithText.Item; |
| 96 | + } |
| 97 | + else if (itemTypeName == "String") |
| 98 | + { |
| 99 | + var itemText = (String)itemWithText.Item; |
| 100 | + |
| 101 | + if (itemText == "") inputType = 0; |
| 102 | + else if (itemText == "MouseMoveX") inputType = 3; |
| 103 | + else if (itemText == "MouseMoveY") inputType = 4; |
| 104 | + } |
| 105 | + |
| 106 | + // Remember settings. |
| 107 | + |
| 108 | + inputText = ((ComboBox)sender).Text; |
| 109 | + Save(); |
| 110 | + } |
| 111 | + |
| 112 | + public void ControlAmount_ValueChanged(object sender, EventArgs e) |
| 113 | + { |
| 114 | + // Convert control decimal to integer and remember setting. |
| 115 | + |
| 116 | + inputAmount = (int)((NumericUpDown)sender).Value; |
| 117 | + Save(); |
| 118 | + } |
| 119 | + |
| 120 | + void inputTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) |
| 121 | + { |
| 122 | + switch (inputType) |
| 123 | + { |
| 124 | + case 3: |
| 125 | + Mouse.MoveRelative(inputAmount, 0); |
| 126 | + break; |
| 127 | + case 4: |
| 128 | + Mouse.MoveRelative(0, inputAmount); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public void Start() |
| 134 | + { |
| 135 | + // Keyboard Keys and Mouse buttons need only to signal down then up to hold. |
| 136 | + // Mouse movement is incremental jumps so needs a faster repeating timer for smooth movement. |
| 137 | + |
| 138 | + if (this.IsActive) return; |
| 139 | + this.IsActive = true; |
| 140 | + |
| 141 | + switch (inputType) |
| 142 | + { |
| 143 | + case 0: |
| 144 | + return; |
| 145 | + case 1: |
| 146 | + Keyboard.KeyDown(inputKeys); |
| 147 | + break; |
| 148 | + case 2: |
| 149 | + Mouse.ButtonDown(inputMouseKeys); |
| 150 | + break; |
| 151 | + case 3: |
| 152 | + inputTimer.Enabled = true; |
| 153 | + break; |
| 154 | + case 4: |
| 155 | + inputTimer.Enabled = true; |
| 156 | + break; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public void Stop() |
| 161 | + { |
| 162 | + if (!this.IsActive) return; |
| 163 | + this.IsActive = false; |
| 164 | + |
| 165 | + switch (inputType) |
| 166 | + { |
| 167 | + case 0: |
| 168 | + return; |
| 169 | + case 1: |
| 170 | + Keyboard.KeyUp(inputKeys); |
| 171 | + break; |
| 172 | + case 2: |
| 173 | + Mouse.ButtonUp(inputMouseKeys); |
| 174 | + break; |
| 175 | + case 3: |
| 176 | + inputTimer.Enabled = false; |
| 177 | + break; |
| 178 | + case 4: |
| 179 | + inputTimer.Enabled = false; |
| 180 | + break; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public void Save() |
| 185 | + { |
| 186 | + Properties.Settings.Default["Action" + settingName] = inputText; |
| 187 | + Properties.Settings.Default["Amount" + settingName] = inputAmount; |
| 188 | + Properties.Settings.Default.Save(); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /// <summary>Used to store objects with custom text as a control item.</summary> |
| 193 | + public class ItemWithText |
| 194 | + { |
| 195 | + public object Item { get; private set; } |
| 196 | + public string Text { get; private set; } |
| 197 | + |
| 198 | + public ItemWithText(object item, string text) |
| 199 | + { |
| 200 | + this.Item = item; |
| 201 | + this.Text = text; |
| 202 | + } |
| 203 | + |
| 204 | + public override string ToString() |
| 205 | + { |
| 206 | + return this.Text; |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments