-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
102 lines (86 loc) · 2.19 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
using System;
using System.Collections.Generic;
using System.Linq;
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;
using System.Windows.Controls.Primitives;
using Graph.ViewModels;
namespace Graph
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var main = new MainViewModel();
this.DataContext = main;
}
private bool _mouseDown = false;
private int _lastCtrlClicked = -1;
private Point _last;
private async void Node_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
int nowClicked = -1;
var grid = sender as Grid;
if (grid == null) return;
grid.CaptureMouse();
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{
// Ctrl is pressed
var node = grid.DataContext as NodeViewModel;
if (node == null) return;
nowClicked = node.Key;
}
if (nowClicked != -1 && _lastCtrlClicked != -1 && nowClicked != _lastCtrlClicked)
{
// Connect
var main = DataContext as MainViewModel;
if (main == null) return;
await ((MainViewModel)DataContext).exactStop();
main.Connect(_lastCtrlClicked, nowClicked);
_lastCtrlClicked = -1;
}
else
_lastCtrlClicked = nowClicked;
_last = e.GetPosition(grid);
_mouseDown = true;
e.Handled = true;
}
private void Node_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_mouseDown)
{
_mouseDown = false;
var grid = sender as Grid;
if (grid == null) return;
grid.ReleaseMouseCapture();
}
e.Handled = true;
}
private void Node_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
Point pt = Mouse.GetPosition(GraphCanvas);
var grid = sender as Grid;
if (grid == null)
return;
Canvas.SetLeft(grid, pt.X - _last.X);
Canvas.SetTop(grid, pt.Y - _last.Y);
}
e.Handled = true;
}
}
}