-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hint.cs
510 lines (420 loc) · 14.2 KB
/
Hint.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Schema;
namespace Text_Editor
{
public enum ArrowDir
{
TopLeft, TopMid, TopRight,
BottomLeft, BottomMid, BottomRight
}
public partial class Hint : UserControl
{
private Image targetImage;
public ArrowDir ArrowDirection
{
get { return _arrowDirection; }
set { _arrowDirection = value; UpdateArrow(); }
}
private string _text;
public string LabelText
{
get { return _text; }
set { _text = value; label1.Text = _text; }
}
private ArrowDir _arrowDirection = ArrowDir.TopLeft;
public Image character = Properties.Resources.fox1;
public Image[] images = [Properties.Resources.fox1, Properties.Resources.fox2, Properties.Resources.fox3];
public int currentCharacterIndex = 0;
private Point[] offsets;
public RichTextBox textBox;
public Component targetControl;
public Image mainImage = Properties.Resources.hint_cloud;
public Image[] arrows;
public Requirement[] requirements;
private bool textNextLine = false;
public Hint()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.White;
this.targetControl = targetControl;
offsets =
[
new Point(26, 34),
new Point(93, 34),
new Point(166, 34),
new Point(26, 124),
new Point(0, 124),
new Point(166, 124)
];
arrows =
[
Properties.Resources.arrow_topleft,
Properties.Resources.arrow_topmid,
Properties.Resources.arrow_topright,
Properties.Resources.arrow_bottomleft,
Properties.Resources.arrow_bottommid,
Properties.Resources.arrow_bottomright,
];
var timer = new Timer();
timer.Interval = 300;
timer.Tick += (s, e) => Refresh();
timer.Enabled = true;
ParentChanged += (s, e) => ParentUpdated();
Enabled = true;
UpdateArrow();
}
private void UpdateArrow()
{
var arrow = arrows[(int)_arrowDirection];
targetImage = Utils.CombineAndResizeTwoImages(mainImage, arrow, 394, 229);
targetImage = Utils.CombineAndResizeTwoImages(targetImage, character, 394, 229);
if (this.Parent != null)
{
Parent.Invalidate(this.Bounds, true);
}
}
public void ParentUpdated()
{
if (Parent != null)
{
textBox = (RichTextBox)Parent.Controls["richTextBox1"];
textBox.TextChanged += TickAndRefresh;
}
else
textBox.TextChanged -= TickAndRefresh;
}
public void TickAndRefresh(object s, EventArgs e)
{
Invalidate();
if (textBox.Text.EndsWith("\n"))
{
textBox.Invalidate();
textNextLine = true;
}
else if (textNextLine)
{
textBox.Invalidate();
textNextLine = false;
}
}
public void OffsetPosition()
{
var selectedOffset = offsets[(int)ArrowDirection];
//Location.Offset(selectedOffset);
}
public bool drag = false;
public bool enab = false;
private int m_opacity = 100;
private int alpha;
public int Opacity
{
get
{
if (m_opacity > 100)
{
m_opacity = 100;
}
else if (m_opacity < 1)
{
m_opacity = 0;
}
return this.m_opacity;
}
set
{
this.m_opacity = value;
if (this.Parent != null)
{
Parent.Invalidate(this.Bounds, true);
}
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | 0x20;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
#if false
Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
Color frmColor = this.Parent.BackColor;
Brush bckColor = default(Brush);
alpha = (m_opacity * 255) / 100;
if (drag)
{
Color dragBckColor = default(Color);
if (BackColor != Color.Transparent)
{
int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
dragBckColor = Color.FromArgb(Rb, Gb, Bb);
}
else
{
dragBckColor = frmColor;
}
alpha = 255;
bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
}
else
{
bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
}
if (this.BackColor != Color.Transparent | drag)
{
g.FillRectangle(bckColor, bounds);
}
#endif
Size targetSize = new Size(Size.Width, Size.Height);
//label1.DrawToBitmap((Bitmap)targetImage, label1.Bounds);
e.Graphics.DrawImage(targetImage, Size.Width / 2 - targetSize.Width / 2, Size.Height / 2 - targetSize.Height / 2, Size.Width, Size.Height);
//bckColor.Dispose();
g.Dispose();
base.OnPaint(e);
}
protected override void OnBackColorChanged(EventArgs e)
{
if (this.Parent != null)
{
Parent.Invalidate(this.Bounds, true);
}
base.OnBackColorChanged(e);
}
protected override void OnParentBackColorChanged(EventArgs e)
{
this.Invalidate();
base.OnParentBackColorChanged(e);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
public class Requirement
{
public Component targetControl;
public Func<object, EventArgs, bool> checkMethod;
private bool _isMet;
public bool IsMet
{
get { return _isMet; }
set { _isMet = value; if (_isMet) onMet.Invoke(this, EventArgs.Empty); }
}
public event EventHandler onMet;
public Requirement(Component targetControl, Func<object, EventArgs, bool> checkMethod = null)
{
this.targetControl = targetControl;
this.checkMethod = checkMethod;
}
public void CheckSelf(object o, EventArgs args)
{
if (checkMethod(o, args))
IsMet = true;
}
}
public class HintRequirement : Requirement
{
public string text;
public ArrowDir direction;
public HintRequirement(Component targetControl, string text, ArrowDir direction, Func<object, EventArgs, bool> checkMethod = null) : base(targetControl, checkMethod)
{
this.text = text;
this.direction = direction;
}
}
public class HintStep
{
public Requirement[] requirements;
public List<Hint> hints = new List<Hint>();
public bool isRun = true;
public bool isComplete = false;
public Form form;
public bool lastStep = false;
public static HintStep CreateHintStep(Form topLevelForm, Point pos, Requirement[] requirements, bool lastStep = false, Image character = null)
{
List<Component> reqControls = new List<Component>();
var step = new HintStep(requirements);
step.lastStep = lastStep;
step.form = topLevelForm;
step.requirements = requirements;
foreach (var req in requirements)
{
if (req is HintRequirement)
{
var hint_req = (HintRequirement)req;
reqControls.Add(req.targetControl);
Hint hint = null;
foreach (var c in topLevelForm.Controls)
{
if (c is Hint)
{
hint = (Hint)c;
break;
}
}
if (hint == null)
hint =new Hint();
if (character != null)
hint.character = character;
else
hint.character = hint.images[hint.currentCharacterIndex];
hint.ArrowDirection = hint_req.direction;
hint.LabelText = hint_req.text;
hint.targetControl = req.targetControl;
hint.Parent = topLevelForm;
hint.Location = pos;
topLevelForm.Controls["richTextBox1"].Invalidate();
hint.OffsetPosition();
hint.BringToFront();
hint.Invalidate();
step.hints.Add(hint);
}
req.onMet += (s, e) => step.CheckRequirements();
}
foreach(dynamic c in topLevelForm.Controls)
{
if (!reqControls.Contains(c))
{
if (c is RichTextBox)
c.ReadOnly = true;
else
c.Enabled = false;
}
}
foreach (dynamic c in reqControls)
{
EnableParentRecursive(c);
c.Enabled = true;
}
return step;
}
public static void EnableParentRecursive(dynamic Comp)
{
if (Comp is Control && Comp?.Parent != null)
{
Comp.Parent.Enabled = true;
EnableParentRecursive(Comp.Parent);
}
else if (Comp is ToolStripItem && Comp?.Owner != null)
{
var owner = (ToolStrip)Comp.Owner;
owner.Enabled = true;
foreach (ToolStripItem i in owner.Items)
{
if (i != Comp)
i.Enabled = false;
}
}
}
public async Task AwaitStep()
{
while (!isComplete)
await Task.Delay(100);
return;
}
public void UnlockTopFormControls()
{
foreach(dynamic c in form.Controls)
{
if (c is RichTextBox)
c.ReadOnly = false;
else
c.Enabled = true;
}
}
public void Complete()
{
isComplete = true;
if (lastStep)
{
UnlockTopFormControls();
foreach (Hint h in hints)
{
h.Hide();
h.Dispose();
}
}
else
{
foreach (Hint h in hints)
{
if (h.currentCharacterIndex + 1 > 2)
h.currentCharacterIndex = 0;
else
h.currentCharacterIndex++;
}
}
isRun = false;
}
public void CheckRequirements()
{
if (!isRun)
return;
foreach(var req in requirements)
{
if (!req.IsMet)
return;
}
Complete();
}
public HintStep(Requirement[] requirements)
{
this.requirements = requirements;
}
}
public static class Utils
{
public static Bitmap CombineAndResizeTwoImages(Image image1, Image image2, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the images into the target bitmap
graphics.DrawImage(image1, 0, 0, result.Width, result.Height);
graphics.DrawImage(image2, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
}
}
public class CustomLabel : Label
{
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
using (Brush aBrush = new SolidBrush(Color.White))
{
e.Graphics.DrawString(Text, Font, aBrush, ClientRectangle);
}
}
}