프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Extensions / VisualHelper.cs @ fc5ed14c

이력 | 보기 | 이력해설 | 다운로드 (4.79 KB)

1 dbddfdd0 taeseongkim
using MarkupToPDF.Common;
2
using System;
3 8e5a4a6a taeseongkim
using System.Collections.Generic;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Windows;
8 dbddfdd0 taeseongkim
using System.Windows.Controls;
9 8e5a4a6a taeseongkim
using System.Windows.Media;
10
11
namespace KCOM
12
{
13
    public static class VisualHelper
14
    {
15
        public static Rect Bounds(this UIElement element, Visual parent)
16
        {
17
            return element.TransformToVisual(parent).TransformBounds(new Rect(element.RenderSize));
18
        }
19
20
        public static Rect Rect(this UIElement element)
21
        {
22
            return new Rect(element.RenderSize);
23
        }
24
25 3dbace4e taeseongkim
        public static Direction GetPointDirection(Point point,double angle)
26
        {
27
            Direction direction = Direction.None;
28
29
            double xValue = point.X;
30
            double yValue = point.Y;
31
32
            if(0 > xValue && Math.Abs(xValue) > Math.Abs(yValue))
33
            {
34
                direction = Direction.Left;
35
            }
36
37
            if (0 < xValue && Math.Abs(xValue) > Math.Abs(yValue))
38
            {
39
                direction = Direction.Right;
40
            }
41
42
            if (0 > yValue && Math.Abs(xValue) < Math.Abs(yValue))
43
            {
44
                direction = Direction.Up;
45
            }
46
47
            if (0 < yValue && Math.Abs(xValue) < Math.Abs(yValue))
48
            {
49
                direction = Direction.Down;
50
            }
51
52
            return direction;
53
        }
54
55 8e5a4a6a taeseongkim
        public static MoveDirection Movement(this Rect Owner,Rect contentRect)
56
        {
57
            MoveDirection result = new MoveDirection();
58
59
            if(contentRect.Top > 0)
60
            {
61
                result.Up = true;
62
            }
63
            
64
            if(contentRect.Left > 0)
65
            {
66
                result.Left = true;
67
            }
68
69
            if(Owner.Width > contentRect.Right)
70
            {
71
                result.Right = true;
72
            }
73
74
            if (Owner.Height > contentRect.Bottom)
75
            {
76
                result.Down = true;
77
            }
78
79
            return result;
80
        }
81 b74a9c91 taeseongkim
82
        public static IEnumerable<DependencyObject> FindAllChildren(this DependencyObject dpo, Predicate<DependencyObject> predicate)
83
        {
84
            //var results = new List<DependencyObject>();
85
            //if (predicate == null)
86
            //    yield return results;
87
88
89
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dpo); i++)
90
            {
91
                var child = VisualTreeHelper.GetChild(dpo, i);
92
                if (predicate(child))
93
                    yield return child;// results.Add(child);
94
95
                var subChildren = child.FindAllChildren(predicate);
96
                foreach (var item in subChildren)
97
                {
98
                    yield return item;
99
                }
100
101
                //results.AddRange(subChildren);
102
            }
103
        }
104 dbddfdd0 taeseongkim
105
        public static IEnumerable<CommentUserInfo> FindComment(this System.Windows.Controls.Canvas canvas, Rect searchRect)
106
        {
107
            foreach (CommentUserInfo child in canvas.FindAllChildren(x=>x is CommentUserInfo))
108
            {
109
                if (child is CommentUserInfo comment)
110
                {
111
                    if (searchRect.IntersectsWith(child.ItemRect))
112
                    {
113
                        yield return child;
114
                    }
115
                }
116
            }
117
        }
118
119
        public static CommentUserInfo FindDistanceComment(this Canvas canvas, Point point,double squareSize = 1)
120
        {
121
            double minDistance = double.MaxValue;
122
            CommentUserInfo closestComment = null;
123
124
            Rect searchRect = new Rect(
125
                point.X - squareSize,
126
                point.Y - squareSize,
127
                squareSize * 2,
128
                squareSize * 2
129
            );
130
131
            foreach (CommentUserInfo child in canvas.FindComment(searchRect))
132
            {
133
                if (child is CommentUserInfo comment)
134
                {
135
                    double left = comment.ItemRect.Left;
136
                    double top = comment.ItemRect.Top;
137
                    double centerX = left + comment.ItemRect.Width / 2;
138
                    double centerY = top + comment.ItemRect.Height / 2;
139
                    double distance = Math.Sqrt(Math.Pow(point.X - centerX, 2) + Math.Pow(point.Y - centerY, 2));
140
141
                    if (distance < minDistance)
142
                    {
143
                        minDistance = distance;
144
                        closestComment = comment;
145
                    }
146
                }
147
            }
148
149
            return closestComment;
150
        }
151 8e5a4a6a taeseongkim
    }
152
153
    public class MoveDirection
154
    {
155
        public bool Left { get; set; }
156
157
        public bool Right { get; set; }
158
159
        public bool Up { get; set; }
160
161
        public bool Down { get; set; }
162
    }
163 3dbace4e taeseongkim
164
    public enum Direction
165
    {
166
        None,
167
        Left,
168
169
        Right,
170
171
        Up,
172
173
        Down
174
    }
175 8e5a4a6a taeseongkim
}
클립보드 이미지 추가 (최대 크기: 500 MB)