-
Notifications
You must be signed in to change notification settings - Fork 0
/
WallWindow.xaml.cs
198 lines (161 loc) · 6.58 KB
/
WallWindow.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Tiles
{
/// <summary>
/// Interaction logic for WallWindow.xaml
/// </summary>
public partial class WallWindow : Window
{
private readonly Element _currentElement = new Element();
private double _sumWidth;
private double _sumHeight;
private int _rowNum;
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
_currentElement.X = Mouse.GetPosition((IInputElement) sender).X;
_currentElement.Y = Mouse.GetPosition((IInputElement) sender).Y;
_currentElement.InputElement?.CaptureMouse();
}
private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
{
_currentElement.InputElement?.ReleaseMouseCapture();
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
_currentElement.IsDragging = true;
if (_currentElement.IsDragging && _currentElement.InputElement != null)
{
var newX = Mouse.GetPosition((IInputElement) sender).X;
var newY = Mouse.GetPosition((IInputElement) sender).Y;
var rt = ((UIElement) _currentElement.InputElement).RenderTransform;
var offsetX = rt.Value.OffsetX;
var offsetY = rt.Value.OffsetY;
rt.SetValue(TranslateTransform.XProperty, offsetX + newX - _currentElement.X);
rt.SetValue(TranslateTransform.YProperty, offsetY + newY - _currentElement.Y);
_currentElement.X = newX;
_currentElement.Y = newY;
}
}
public void Tile_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_currentElement.InputElement = (IInputElement) sender;
}
public void Tile_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
var b = (Border) sender;
cnvWall.Children.Remove(b);
}
}
public void AddTile(Tile t, int cnt)
{
if (cnt > 0)
for (var i = 0; i < cnt; i++)
{
var translateTransform = new TranslateTransform();
var b = new Border();
var myBrush = new ImageBrush();
var bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(t.ImageFile);
bi.Rotation = t.ImageRotation;
bi.EndInit();
myBrush.ImageSource = bi;
b.BorderBrush = t.FugaColor;
b.BorderThickness = new Thickness(t.Fuga, t.Fuga, t.Fuga, t.Fuga);
if (t.ImageRotation == Rotation.Rotate90)
{
b.Height = t.ScaleHeight;
b.Width = t.ScaleWidth;
}
else
{
b.Height = t.ScaleHeight;
b.Width = t.ScaleWidth;
}
b.MouseLeftButtonDown += Tile_MouseLeftButtonDown;
b.MouseRightButtonDown += Tile_MouseRightButtonDown;
b.Background = myBrush;
b.RenderTransform = translateTransform;
cnvWall.Children.Add(b);
}
}
public void GenerateWall(Wall w, Tile t)
{
var translateTransform1 = new TranslateTransform();
var b = new Border();
var wid = Width / 2;
var tilehalf = t.ScaleWidth / 2;
var tmp = Math.Ceiling((wid - tilehalf) / t.ScaleWidth);
_sumHeight = 0;
if (w.IsCenter)
_sumWidth = wid - tilehalf - t.ScaleWidth * tmp;
else _sumWidth = 0;
_rowNum = 0;
while (_sumHeight <= Height)
{
_rowNum++;
while (_sumWidth <= Width + t.ScaleWidth * tmp)
{
b = new Border();
var myBrush = new ImageBrush();
b.BorderBrush = t.FugaColor;
b.BorderThickness = new Thickness(t.Fuga, t.Fuga, t.Fuga, t.Fuga);
var bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(t.ImageFile);
bi.Rotation = t.ImageRotation;
bi.EndInit();
myBrush.ImageSource = bi;
if (t.ImageRotation == Rotation.Rotate90)
{
b.Height = t.ScaleHeight;
b.Width = t.ScaleWidth;
}
else
{
b.Height = t.ScaleHeight;
b.Width = t.ScaleWidth;
}
b.MouseLeftButtonDown += Tile_MouseLeftButtonDown;
b.MouseRightButtonDown += Tile_MouseRightButtonDown;
b.Background = myBrush;
if (_sumWidth == 0)
{
if (_rowNum % 2 == 0 && w.IsInvert)
translateTransform1 = new TranslateTransform(_sumWidth - b.Width / 2, _sumHeight);
else translateTransform1 = new TranslateTransform(_sumWidth, _sumHeight);
_sumWidth = b.Width;
}
else
{
if (_rowNum % 2 == 0 && w.IsInvert)
translateTransform1 = new TranslateTransform(_sumWidth - b.Width / 2, _sumHeight);
else translateTransform1 = new TranslateTransform(_sumWidth, _sumHeight);
_sumWidth = _sumWidth + b.Width;
}
b.RenderTransform = translateTransform1;
cnvWall.Children.Add(b);
}
if (w.IsCenter)
_sumWidth = wid - tilehalf - t.ScaleWidth * tmp;
else _sumWidth = 0;
_sumHeight = _sumHeight + b.Height;
}
}
public WallWindow()
{
InitializeComponent();
_sumWidth = 0;
_sumHeight = 0;
_rowNum = 0;
}
}
}