-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
325 lines (281 loc) · 10.5 KB
/
MainWindow.xaml.cs
File metadata and controls
325 lines (281 loc) · 10.5 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow : Window
{
Control draggedItem;
Point itemRelativePosition;
bool IsDragging;
ScaleTransform scaler;
DialogueEditor only;
unitDialogue linkfrom;
unitDialogue linkto;
Dictionary<Button, object> Button_obj_Map;
unitDialogue head;
public int nodecount;
Button prevOpen;
public MainWindow()
{
only = new DialogueEditor();
head = null;
IsDragging = false;
Button_obj_Map = new Dictionary<Button, object>();
nodecount = 0;
scaler = new ScaleTransform();
prevOpen = null;
InitializeComponent();
jesus.LayoutTransform = scaler;
jesus.MouseWheel += scale_Canvas;
linkfrom = null;
linkto = null;
SidePanel.Children.Add(only.hmm);
}
public void scale_Canvas(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0)
{
scaler.ScaleX *= 1.1;
scaler.ScaleY *= 1.1;
}
else
{
scaler.ScaleX /= 1.1;
scaler.ScaleY /= 1.1;
}
}
private void add_Dialogue(object sender, RoutedEventArgs e)
{
Button newBtn = new Button();
newBtn.Height = 32;
newBtn.Width = 150;
newBtn.Style = (Style)this.FindResource("MaterialDesignFlatLightBgButton");
newBtn.Content = "Dialogue "+nodecount.ToString();
unitDialogue obj = new unitDialogue(nodecount);
nodecount++;
Button_obj_Map.Add(newBtn, obj);
btn_mapper(newBtn);
Point p = Mouse.GetPosition(jesus);
Canvas.SetLeft(newBtn, p.X - 10);
Canvas.SetTop(newBtn, p.Y - 10);
jesus.Children.Add(newBtn);
if (head == null)
head = obj;
}
private void btn_mapper(object sender)
{
Button pointa = sender as Button;
pointa.Click += btn_simpleclick;
pointa.PreviewMouseLeftButtonDown += btn_PreviewMouseLeftButtonDown;
pointa.PreviewMouseLeftButtonUp += btn_PreviewMouseLeftButtonUp;
pointa.PreviewMouseMove += btn_PreviewMouseMove;
unitDialogue temp = getUnitDialogueFromButton(pointa) as unitDialogue;
pointa.ContextMenu = temp.cm;
pointa.ContextMenuOpening += linkfrom_init;
pointa.ContextMenuClosing += problem_handler;
}
public void linkfrom_init(object sender, ContextMenuEventArgs e)
{
linkfrom = getUnitDialogueFromButton(sender);
}
public dynamic getUnitDialogueFromButton(object sender)
{
dynamic a;
Button temp = sender as Button;
Button_obj_Map.TryGetValue(temp, out a);
return a;
}
public void problem_handler(object sender, ContextMenuEventArgs e)
{
Button temp = sender as Button;
if(linkfrom!=null )
{
if (linkfrom.contextItemSelected == -1)
{
linkfrom = null;
}
else if(linkfrom.contextItemSelected==linkfrom.paths.Count)
{
jesus.Children.Remove(temp);
while (linkfrom.paths.Count > 0)
{
if (linkfrom.paths[0].line == null)
{
linkfrom.paths.RemoveAt(0);
}
else
{
lineDeleter(linkfrom.paths[0].line, null);
}
}
while (linkfrom.from.Count > 0)
{
lineDeleter(linkfrom.from[0].line,null);
}
linkfrom.paths = null;
linkfrom.from = null;
Button_obj_Map.Remove(temp);
linkfrom = null;
}
else {
var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush)converter.ConvertFromString("#f5f0ff");
jesus.Background = brush;
}
}
}
private void btn_simpleclick(object sender, RoutedEventArgs e)
{
Button temp = sender as Button;
unitDialogue next= getUnitDialogueFromButton(sender) as unitDialogue;
if (linkfrom == null)
{
if (only.open != null)
{
only.open.dialogue.Clear();
only.open.dialogue.Append(only.enterDialogue.Text);
}
only.open = next;
only.refresher(only.open);
}
else
{
linkto = next;
if (linkfrom.Equals(linkto) || linkfrom.contextItemSelected==-1)
{
linkfrom = null;
linkto = null;
return;
}
linkfrom.pathLinker(linkto);
lineCreator(linkfrom, linkto);
linkfrom = null;
linkto = null;
jesus.Background = new SolidColorBrush(Colors.GhostWhite);
}
if (prevOpen != null)
prevOpen.Style = (Style)this.FindResource("MaterialDesignFlatLightBgButton");
temp.Style = (Style)this.FindResource("MaterialDesignFlatDarkBgButton");
prevOpen = temp;
}
private void btn_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
IsDragging = true;
draggedItem = (Button)sender;
itemRelativePosition = e.GetPosition(draggedItem);
}
private void btn_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Point p = Mouse.GetPosition(jesus);
if (!IsDragging)
return;
IsDragging = false;
}
private void btn_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (!IsDragging)
return;
Point canvasRelativePosition = e.GetPosition(jesus);
Canvas.SetTop(draggedItem, canvasRelativePosition.Y - itemRelativePosition.Y);
Canvas.SetLeft(draggedItem, canvasRelativePosition.X - itemRelativePosition.X);
Button temp = sender as Button;
lineUpdater(temp, e);
}
public void lineCreator(unitDialogue from, unitDialogue to)
{
Line line = new Line();
line.Stroke = System.Windows.Media.Brushes.LightSteelBlue;
line.StrokeThickness = 5;
Canvas.SetZIndex(line, -1);
var c1 = lineCreator_coordinatesHelper(from);
var c2 = lineCreator_coordinatesHelper(to);
lineCoordinatesUpdater(line, c1.Item1, c1.Item2,1);
lineCoordinatesUpdater(line, c2.Item1, c2.Item2, 2);
List<unitDialogue> connx = new List<unitDialogue>();
connx.Add(from);
connx.Add(to);
line.Tag = connx;
line.MouseRightButtonDown+= new MouseButtonEventHandler(lineDeleter);
linkfrom.paths[linkfrom.contextItemSelected].line = line;
linkfrom.ContextBuilder();
linkfrom.contextItemSelected = -1;
jesus.Children.Add(line);
}
public Tuple<double,double> lineCreator_coordinatesHelper(unitDialogue lmao)
{
Button temp = getKey(lmao);
double X1 = Canvas.GetLeft(temp) + 75;
double Y1 = Canvas.GetTop(temp) + 16;
return new Tuple<double,double>(X1,Y1);
}
public void lineCoordinatesUpdater(Line temp, double X, double Y,int pos)
{
if (pos == 1)
{
temp.X1 = X;
temp.Y1 = Y;
}
else
{
temp.X2 = X;
temp.Y2 = Y;
}
}
public void lineDeleter(object sender, MouseEventArgs e)
{
Line temp = sender as Line;
List<unitDialogue> lmao1 = temp.Tag as List<unitDialogue>;
int indexToDelete1 = -1;
int indexToDelete2 = -1;
for(int i = 0; i < lmao1[0].paths.Count; i++)
{
if (lmao1[0].paths[i].line.Equals(temp))
{
indexToDelete1 = i;
break;
}
}
for (int i = 0; i < lmao1[1].from.Count; i++)
{
if (lmao1[1].from[i].line.Equals(temp))
{
indexToDelete2 = i;
break;
}
}
lmao1[0].paths[indexToDelete1].line = null;
lmao1[0].paths[indexToDelete1].next = null;
lmao1[1].from.RemoveAt(indexToDelete2);
lmao1[0].ContextBuilder();
jesus.Children.Remove(temp);
}
public void lineUpdater(Button temp, MouseEventArgs e)
{
unitDialogue slave = getUnitDialogueFromButton(temp) as unitDialogue;
Point lmao = e.GetPosition(temp);
Point canvasRelativePosition = e.GetPosition(jesus);
for (int i = 0; i < slave.from.Count; i++)
lineCoordinatesUpdater(slave.from[i].line, canvasRelativePosition.X-lmao.X + 75, canvasRelativePosition.Y - lmao.Y + 16,2);
for (int i = 0; i < slave.paths.Count; i++)
if (slave.paths[i].line != null)
lineCoordinatesUpdater(slave.paths[i].line, canvasRelativePosition.X - lmao.X + 75, canvasRelativePosition.Y - lmao.Y + 16, 1);
}
public Button getKey(unitDialogue param)
{
Button temp = null;
foreach(KeyValuePair<Button, object> entry in Button_obj_Map)
if (entry.Value == param)
temp = entry.Key;
return temp;
}
public void save(object sender, RoutedEventArgs e)
{
}
}
}