-
Notifications
You must be signed in to change notification settings - Fork 8
/
SimpleConfigPanel.cs
84 lines (66 loc) · 2.43 KB
/
SimpleConfigPanel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using ShootBlues;
using Squared.Util.Bind;
using System.IO;
using Squared.Task;
using System.Collections;
namespace ShootBlues.Script {
public class SimpleConfigPanel<T> : TaskUserControl, IConfigurationPanel
where T : ManagedScript {
public IBoundMember[] Prefs;
public T Script;
public SimpleConfigPanel (T script)
: base(Program.Scheduler) {
Script = script;
Prefs = new IBoundMember[0];
}
protected override void OnLoad (EventArgs e) {
base.OnLoad(e);
WireEventListeners(this.Controls.Cast<Control>());
}
protected void WireEventListeners (IEnumerable<Control> controls) {
foreach (var ctl in controls) {
var cb = ctl as CheckBox;
if (cb != null)
cb.CheckedChanged += ValuesChanged;
var nud = ctl as NumericUpDown;
if (nud != null)
nud.ValueChanged += ValuesChanged;
var trb = ctl as TrackBar;
if (trb != null)
trb.ValueChanged += ValuesChanged;
var txb = ctl as TextBox;
if (txb != null)
txb.TextChanged += ValuesChanged;
WireEventListeners(ctl.Controls.Cast<Control>());
}
}
public string GetMemberName (IBoundMember member) {
return ((Control)member.Target).Name;
}
public IEnumerator<object> LoadConfiguration () {
var rtc = new RunToCompletion<Dictionary<string, object>>(Script.Preferences.GetAll());
yield return rtc;
var dict = rtc.Result;
object value;
foreach (var bm in Prefs)
if (dict.TryGetValue(GetMemberName(bm), out value))
bm.Value = value;
}
public IEnumerator<object> SaveConfiguration () {
var prefsDict = new Dictionary<string, object>();
foreach (var bm in Prefs)
prefsDict[GetMemberName(bm)] = bm.Value;
yield return Script.Preferences.SetMultiple(prefsDict);
}
public void ValuesChanged (object sender, EventArgs args) {
Start(SaveConfiguration());
}
}
}