-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
228 lines (193 loc) · 6.8 KB
/
MainWindow.xaml.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Superflat
{
public class Block
{
public string Name { get; set; }
public string Id { get; set; }
public int Count { get; set; }
public string IdString => Count == 1 ? Id : $"{Count}*{Id}";
public string TString => ToString();
public override string ToString()
{
return Count < 2 ? Name : $"{Count}×{Name}";
}
public Block WithCount(int val)
{
return new Block
{
Name = Name,
Id = Id,
Count = val
};
}
}
public class Biome
{
public string Name { get; set; }
public string Id { get; set; }
public override string ToString()
{
return Name;
}
}
public class DragAndDropListBox : ListBox
{
private Point _dragStartPoint;
private static P FindVisualParent<P>(DependencyObject child)
where P : DependencyObject
{
var parentObject = VisualTreeHelper.GetParent(child);
if (parentObject == null)
return null;
if (parentObject is P parent)
return parent;
return FindVisualParent<P>(parentObject);
}
public DragAndDropListBox()
{
PreviewMouseMove += ListBox_PreviewMouseMove;
var style = new Style(typeof(ListBoxItem));
style.Setters.Add(new Setter(AllowDropProperty, true));
style.Setters.Add(
new EventSetter(
PreviewMouseLeftButtonDownEvent,
new MouseButtonEventHandler(ListBoxItem_PreviewMouseLeftButtonDown)));
ItemContainerStyle = style;
}
private void ListBox_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point point = e.GetPosition(null);
Vector diff = _dragStartPoint - point;
if (e.LeftButton != MouseButtonState.Pressed ||
(!(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance) &&
!(Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))) return;
var lbi = FindVisualParent<ListBoxItem>(((DependencyObject)e.OriginalSource));
if (lbi != null)
{
DragDrop.DoDragDrop(lbi, lbi.DataContext, DragDropEffects.Move);
}
}
private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_dragStartPoint = e.GetPosition(null);
}
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public BindingList<Block> Blocks { get; } = new BindingList<Block>();
public BindingList<Biome> Biomes { get; } = new BindingList<Biome>();
public ReorderableList<Block> SelectedBlocks { get; }
public ConfigBuilder Builder { get; set; }
public MainWindow()
{
DataContext = this;
foreach (var line in File.ReadAllLines("blocks.txt"))
{
var split = line.Split(' ');
Blocks.Add(new Block { Id = split[1], Name = split[0] });
}
foreach (var line in File.ReadAllLines("biomes.txt"))
{
var split = line.Split(' ');
Biomes.Add(new Biome { Id = split[1], Name = split[0] });
}
Builder = new ConfigBuilder();
InitializeComponent();
SelectedBlocks = new ReorderableList<Block>(selectedBlockList, "TString");
UpdateString();
}
private void UpdateString()
{
Builder.Layers = SelectedBlocks.ToArray();
resultBox.Text = Builder.GetString();
}
private void AddButton_Click(object sender, RoutedEventArgs e)
{
if (blockList.SelectedItem != null)
{
var item = (Block)blockList.SelectedItem;
SelectedBlocks.Add(item.WithCount(int.Parse(countBox.Text)));
UpdateString();
}
}
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
var selected = new object[selectedBlockList.SelectedItems.Count];
selectedBlockList.SelectedItems.CopyTo(selected, 0);
foreach (var block in selected)
{
SelectedBlocks.Remove((Block)block);
}
selectedBlockList.UnselectAll();
UpdateString();
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
SelectedBlocks.Clear();
UpdateString();
}
private void RemoveDrop(object sender, DragEventArgs e)
{
var source = (Block)e.Data.GetData(typeof(Block));
if (source.Count != 0)
{
SelectedBlocks.Remove(source);
UpdateString();
}
}
private void AddDrop(object sender, DragEventArgs e)
{
var source = (Block)e.Data.GetData(typeof(Block));
if (source.Count == 0)
{
SelectedBlocks.Add(source.WithCount(int.Parse(countBox.Text)));
UpdateString();
}
}
private void countBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = !(e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 || e.Key == Key.Delete || e.Key == Key.Back);
if ((e.Key == Key.Delete || e.Key == Key.Back) && ((TextBox)sender).Text.Length == 1)
{
((TextBox)sender).Text = "1";
e.Handled = true;
}
}
private void UpdateCallback(object sender, EventArgs e)
{
UpdateString();
}
private void RemoveSelected(object sender, KeyEventArgs e)
{
if (e.Key != Key.Delete) return;
var selected = new object[selectedBlockList.SelectedItems.Count];
selectedBlockList.SelectedItems.CopyTo(selected, 0);
foreach (var block in selected)
{
SelectedBlocks.Remove((Block)block);
}
selectedBlockList.UnselectAll();
UpdateString();
}
}
}