OSDN Git Service

デバックコードを追加した
[fooeditengine/FooEditEngine.git] / Core / Util.cs
1 /*
2  * Copyright (C) 2013 FooProject
3  * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
5
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
10  */
11 using System;
12 using System.Linq;
13 using System.Collections.Generic;
14 using System.Globalization;
15 using System.Text;
16 using System.Reflection;
17 using System.Collections;
18
19 namespace FooEditEngine
20 {
21     class Util
22     {
23 #if METRO || WINDOWS_UWP
24         public static void GetDpi(out float dpix, out float dpiy)
25         {
26             dpix = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
27             dpiy = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
28         }
29
30         public static double GetScale()
31         {
32             float dpi;
33             Util.GetDpi(out dpi, out dpi);
34             return dpi / 96.0;
35         }
36
37         public static Point GetClientPoint(Point screen, Windows.UI.Xaml.UIElement element)
38         {
39             //Windows10以降では補正する必要がある
40             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
41             screen = screen.Offset(-win_rect.X, -win_rect.Y);
42
43             var gt = element.TransformToVisual(element);
44             return gt.TransformPoint(screen);
45         }
46         public static Point GetScreentPoint(Point client, Windows.UI.Xaml.UIElement element)
47         {
48             //ウィンドウ内での絶対座標を取得する
49             var gt = element.TransformToVisual(element);
50             Point screenPoint = gt.TransformPoint(client);
51
52             //Windows10以降では補正する必要がある
53             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
54             screenPoint = screenPoint.Offset(win_rect.X, win_rect.Y);
55
56             return screenPoint;
57         }
58         public static Windows.Foundation.Rect GetClientRect(Windows.Foundation.Rect screen, Windows.UI.Xaml.UIElement element)
59         {
60             //Windows10以降では補正する必要がある
61             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
62             screen.X -= win_rect.X;
63             screen.Y -= win_rect.Y;
64
65             var gt = element.TransformToVisual(element);
66             return gt.TransformBounds(screen);
67         }
68         public static Windows.Foundation.Rect GetScreentRect(Windows.Foundation.Rect client, Windows.UI.Xaml.UIElement element)
69         {
70             //ウィンドウ内での絶対座標を取得する
71             var gt = element.TransformToVisual(element);
72             Windows.Foundation.Rect screenRect = gt.TransformBounds(client);
73
74             //Windows10以降では補正する必要がある
75             Windows.Foundation.Rect win_rect = Windows.UI.Xaml.Window.Current.CoreWindow.Bounds;
76             screenRect.X += win_rect.X;
77             screenRect.Y += win_rect.Y;
78
79             return screenRect;
80         }
81         public static IEnumerable<char> GetEnumrator(string s)
82         {
83             char[] chars = s.ToCharArray();
84             return chars;
85         }
86 #else
87         public static IEnumerable<char> GetEnumrator(string s)
88         {
89             return s;
90         }
91 #endif
92
93         public static T ConvertAbsIndexToRelIndex<T>(T n, int StartIndex, int Length) where T : IRange
94         {
95             n = Util.NormalizeIMaker<T>(n);
96
97             int markerEnd = n.start + n.length - 1;
98
99             int EndIndex = StartIndex + Length;
100
101             if (n.start >= StartIndex && markerEnd <= EndIndex)
102                 n.start -= StartIndex;
103             else if (n.start >= StartIndex && n.start <= EndIndex)
104             {
105                 n.start -= StartIndex;
106                 n.length = EndIndex - StartIndex + 1;
107             }
108             else if (markerEnd >= StartIndex && markerEnd <= EndIndex)
109             {
110                 n.start = 0;
111                 n.length = markerEnd - StartIndex + 1;
112             }
113             else if (n.start >= StartIndex && markerEnd <= EndIndex)
114                 n.start -= StartIndex;
115             else if (n.start <= StartIndex && markerEnd > EndIndex)
116             {
117                 n.start = 0;
118                 n.length = EndIndex - StartIndex + 1;
119             }
120             else
121             {
122                 n.start = -1;
123                 n.length = 0;
124             }
125             return n;
126         }
127
128         public static bool IsWordSeparator(char c)
129         {
130             if (c == Document.NewLine || char.IsSeparator(c) || char.IsPunctuation(c) || CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.MathSymbol)
131                 return true;
132             else
133                 return false;
134         }
135         public static void Swap<T>(ref T a, ref T b)
136         {
137             T c = b;
138             b = a;
139             a = c;
140         }
141
142         public static string Generate(char c, int count)
143         {
144             StringBuilder tabstr = new StringBuilder();
145             for (int j = count; j > 0; j--)
146                 tabstr.Append(c);
147             return tabstr.ToString();
148         }
149
150         public static Rectangle OffsetAndDeflate(Rectangle r, Size s)
151         {
152             return new Rectangle(r.X + s.Width,r.Y + s.Height, r.Width - s.Width, r.Height - s.Height);
153         }
154
155         public static T NormalizeIMaker<T>(T m) where T : IRange
156         {
157             if (m.length > 0)
158                 return m;
159             m.start = m.start + m.length;
160             m.length = Math.Abs(m.length);
161             return m;
162         }
163
164         public static int RoundUp(double x)
165         {
166             return (int)(x + 0.5);
167         }
168
169     }
170 }