-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrawObjMgr.cs
367 lines (318 loc) · 12.7 KB
/
DrawObjMgr.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace PenTabletNotebook {
class DrawObjMgr {
private List<DrawObj> mDOList = new List<DrawObj>();
private List<DrawObj> mDOUndoList = new List<DrawObj>();
private int mSelectedDOidx = -1;
private Canvas mCanvas;
private Brush mBrush;
private PenModeEnum mPenMode = PenModeEnum.PM_Pen;
private bool mHitBoxSelected = false;
public Canvas GetCanvas() {
return mCanvas;
}
public PenModeEnum PenMode {
get { return mPenMode; }
}
/// <summary>
/// 中で保持しているDrawObjのリストを戻します。
/// コピーではなくオリジナルのポインタを戻します。
/// </summary>
public IEnumerable<DrawObj> GetDOList() {
return mDOList;
}
/// <summary>
/// 中で保持しているDrawObjのリストを引数の物に入れ替えます。
/// doListのオブジェクトは、Canvasに追加してからこの関数を呼びます。
/// 新しいDOを最後に追加します。
/// </summary>
public void SetDOList(IEnumerable<DrawObj> doList) {
Clear(ClearMode.CM_ZeroObj);
foreach (var d in doList) {
mDOList.Add(d);
}
NewDU();
}
public void SetPenMode(PenModeEnum pm) {
mPenMode = pm;
switch (pm) {
case PenModeEnum.PM_Pen:
case PenModeEnum.PM_Eraser:
// 全オブジェクトの描画モードを通常描画に戻す。
foreach (var d in mDOList) {
d.ModeChangeTo(DrawObj.Mode.M_Draw);
}
// 選択DO番号を最後のDUにします。
System.Diagnostics.Debug.Assert(0 < mDOList.Count);
mSelectedDOidx = mDOList.Count - 1;
break;
case PenModeEnum.PM_Select:
break;
default:
System.Diagnostics.Debug.Assert(false);
break;
}
}
public void SetCurPenBrush(Brush b) {
mBrush = b;
CurDU.SetBrush(b);
}
private DrawObj CurDU {
get {
return mDOList[mSelectedDOidx];
}
}
public DrawObjMgr(Canvas c, Brush b) {
mCanvas = c;
mBrush = b;
// 最初の1個目の描画オブジェクト作成。選択状態にする。
NewDU();
}
/// <summary>
/// 新しいDOをリストの最後に追加し選択状態にします。
/// </summary>
private void NewDU() {
var dObj = new DrawObjFreePen(DrawObjNew.mNextIdx++, mCanvas, mBrush);
mDOList.Add(dObj);
mSelectedDOidx = mDOList.Count - 1;
}
private void DeleteDO(int i) {
var d = mDOList[i];
d.ModeChangeTo(DrawObj.Mode.M_Draw);
d.DeleteFromCanvas();
mDOList.RemoveAt(i);
mDOUndoList.Add(d);
// 最後のオブジェクトを選択状態にします。
mSelectedDOidx = mDOList.Count - 1;
}
public void MouseMove(Object sender, MouseEventArgs e) {
var msePos = e.GetPosition(mCanvas);
/*
Console.WriteLine("MM {0},{1} {2}{3}{4} {5} {6}", (int)msePos.X, (int)msePos.Y,
e.LeftButton == MouseButtonState.Pressed ? "L" : " ",
e.MiddleButton == MouseButtonState.Pressed ? "M" : " ",
e.RightButton == MouseButtonState.Pressed ? "R" : " ",
e.XButton1 == MouseButtonState.Pressed ? "X1" : " ",
e.XButton2 == MouseButtonState.Pressed ? "X2" : " ");
*/
switch (mPenMode) {
case PenModeEnum.PM_Pen:
CurDU.MouseMove(sender, e);
break;
case PenModeEnum.PM_Eraser:
if (e.LeftButton == MouseButtonState.Pressed) {
// マウス左ボタン押下中。
for (int i = mDOList.Count - 1; 0 <= i; --i) {
var d = mDOList[i];
if (d.GetHitArea().Contains(msePos)) {
// 当たったオブジェクトをCanvasから除外しUndoリストに追加。
DeleteDO(i);
break;
}
}
}
break;
case PenModeEnum.PM_Select:
if (e.LeftButton == MouseButtonState.Pressed) {
// マウス左ボタン押下中。
if (!mHitBoxSelected) {
return;
}
CurDU.HOS.MouseMove(sender, e);
CurDU.Resize(CurDU.HOS.XYWH);
} else {
// ホバー中。
if (mHitBoxSelected) {
// ヒット矩形選択状態。
Console.WriteLine("D: MouseMove HitBox is selected");
} else {
// ヒットしたらヒット矩形描画モードにします。
bool bHit = false;
for (int i = mDOList.Count - 1; 0 <= i; --i) {
var d = mDOList[i];
var ha = d.GetHitArea();
//Console.WriteLine("d {0} : {1},{2},{3},{4}", i, ha.Left, ha.Top, ha.Right, ha.Bottom);
if (!bHit && ha.Contains(msePos)) {
// 当たった。ヒット矩形表示。
if (d.CurrentMode != DrawObj.Mode.M_DrawHitBox) {
d.ModeChangeTo(DrawObj.Mode.M_DrawHitBox);
Console.WriteLine("DHB {0},{1}", (int)msePos.X, (int)msePos.Y);
}
d.HOS.MouseMove(sender, e);
bHit = true;
} else {
// 当たらない。ヒット矩形非表示。
if (d.CurrentMode != DrawObj.Mode.M_Draw) {
d.ModeChangeTo(DrawObj.Mode.M_Draw);
}
}
}
}
}
break;
}
}
public void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
var msePos = e.GetPosition(mCanvas);
Console.WriteLine("Ldown {0} {1}", (int)msePos.X, (int)msePos.Y);
switch (mPenMode) {
case PenModeEnum.PM_Pen:
CurDU.MouseLeftButtonDown(sender, e);
break;
case PenModeEnum.PM_Select:
if (mHitBoxSelected) {
// 選択中のDrawObjを選択解除。通常描画モードに戻します。
var d = mDOList[mSelectedDOidx];
d.ModeChangeTo(DrawObj.Mode.M_Draw);
mHitBoxSelected = false;
}
for (int i = mDOList.Count - 1; 0 <= i; --i) {
var d = mDOList[i];
if (d.GetHitArea().Contains(msePos)) {
// 当たった。ヒット矩形を表示します。
mSelectedDOidx = i;
d.ModeChangeTo(DrawObj.Mode.M_DrawHitBox);
mHitBoxSelected = true;
d.HOS.MouseLeftButtonDown(sender, e);
break;
}
}
break;
}
}
public void MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
var msePos = e.GetPosition(mCanvas);
//Console.WriteLine("Lup {0} {1}", (int)msePos.X, (int)msePos.Y);
switch (mPenMode) {
case PenModeEnum.PM_Pen:
CurDU.MouseLeftButtonUp(sender, e);
NewDU();
break;
case PenModeEnum.PM_Select:
if (mHitBoxSelected) {
// 通常描画モードに戻します。
var d = mDOList[mSelectedDOidx];
d.HOS.MouseLeftButtonUp(sender, e);
// DrawObjをリサイズします。
d.Resize(d.HOS.XYWH);
//d.ModeChangeTo(DrawObj.Mode.M_Draw);
//mHitBoxSelected = false;
}
// 最後のオブジェクトを選択状態にします。
//mSelectedDOidx = mDOList.Count - 1;
break;
}
}
public void MouseRightButtonUp(object sender, MouseButtonEventArgs e) {
var msePos = e.GetPosition(mCanvas);
Console.WriteLine("Rup {0} {1}", (int)msePos.X, (int)msePos.Y);
switch (mPenMode) {
case PenModeEnum.PM_Pen:
// ペンモードで右クリ:Undo。
Undo();
break;
case PenModeEnum.PM_Select:
// 選択モードで右クリ:削除。
for (int i = mDOList.Count - 1; 0 <= i; --i) {
var d = mDOList[i];
if (d.GetHitArea().Contains(msePos)) {
// 当たったオブジェクト削除。
DeleteDO(i);
break;
}
}
break;
}
}
public bool CanUndo() {
// 空ではないDOを数えます。
int count = 0;
for (int i = 0; i < mDOList.Count; ++i) {
var o = mDOList[i];
if (!o.IsEmpty()) {
++count;
}
}
return count != 0;
}
public bool CanRedo() {
return 0 < mDOUndoList.Count;
}
/// <summary>
/// mDOListの最新dObjが空の時削除。
/// </summary>
private void ClearEmptyObj() {
while (0 < mDOList.Count) {
var o = mDOList[mDOList.Count - 1];
if (o.IsEmpty()) {
o.DeleteFromCanvas();
mDOList.RemoveAt(mDOList.Count - 1);
} else {
// 空ではないdObjが現れた。
return;
}
}
}
public enum ClearMode {
CM_NewDU,
CM_ZeroObj,
}
/// <summary>
/// 中で保持しているDrawObjを全て消去、キャンバスからも消します。
/// </summary>
/// <param name="cm">CM_NewDUのとき、空のDrawObjを追加します。CM_ZeroObjの場合DrawObjの数が0となり、mSelectedDOidxが無効な位置を指します。</param>
public void Clear(ClearMode cm) {
Console.WriteLine("DOMgr.Clear({0})", cm);
while (0 < mDOList.Count) {
var o = mDOList[mDOList.Count - 1];
o.DeleteFromCanvas();
mDOList.RemoveAt(mDOList.Count - 1);
}
mDOUndoList.Clear();
if (cm == ClearMode.CM_NewDU) {
NewDU();
} else {
mSelectedDOidx = -1;
}
}
public bool Undo() {
if (mDOList.Count < 2) {
return false;
}
// 最新の空で無いobjを削除。
ClearEmptyObj();
if (0 < mDOList.Count) {
DrawObj o = mDOList[mDOList.Count - 1];
mDOList.RemoveAt(mDOList.Count - 1);
o.DeleteFromCanvas();
// oは空で無い描画物なので、Undoに追加。
mDOUndoList.Add(o);
} else {
// 空のOBJしか無い。
Console.WriteLine("Undo but there are only empty objects!");
}
NewDU();
return true;
}
public bool Redo() {
if (mDOUndoList.Count == 0) {
return false;
}
var o = mDOUndoList[mDOUndoList.Count - 1];
mDOUndoList.RemoveAt(mDOUndoList.Count - 1);
ClearEmptyObj();
mDOList.Add(o);
o.AddToCanvas(mCanvas);
NewDU();
return true;
}
}
}