From 0c69c5bfffa7b3120cbfd36108bdd762ddc81746 Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 23 Aug 2013 18:55:24 -0400 Subject: [PATCH] change to a Portable Class Library and modify code to build as one --- Gwen/Anim/Animation.cs | 1 + Gwen/Color.cs | 1 - Gwen/Control/Base.cs | 1 + Gwen/Control/ComboBox.cs | 1 + Gwen/Control/Menu.cs | 1 + Gwen/Extensions/ListExtensions.cs | 157 ++++++++++++++++++++++++++++++ Gwen/Gwen.csproj | 28 +++--- Gwen/Platform/Windows.cs | 58 ----------- Gwen/Point.cs | 1 - Gwen/Properties/AssemblyInfo.cs | 8 -- Gwen/Rectangle.cs | 1 - Gwen/Util.cs | 7 +- GwenCS.sln | 40 ++++---- 13 files changed, 203 insertions(+), 102 deletions(-) create mode 100644 Gwen/Extensions/ListExtensions.cs delete mode 100644 Gwen/Platform/Windows.cs diff --git a/Gwen/Anim/Animation.cs b/Gwen/Anim/Animation.cs index 2d2c6aa..b10a2e5 100644 --- a/Gwen/Anim/Animation.cs +++ b/Gwen/Anim/Animation.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Gwen.Control; +using Gwen.Extensions; namespace Gwen.Anim { diff --git a/Gwen/Color.cs b/Gwen/Color.cs index 615aa8a..74a64d2 100644 --- a/Gwen/Color.cs +++ b/Gwen/Color.cs @@ -2,7 +2,6 @@ using System; namespace Gwen { - [Serializable] public struct Color { public byte A; diff --git a/Gwen/Control/Base.cs b/Gwen/Control/Base.cs index 8340c82..742e3dd 100644 --- a/Gwen/Control/Base.cs +++ b/Gwen/Control/Base.cs @@ -7,6 +7,7 @@ using System.Linq; using Gwen.Anim; using Gwen.DragDrop; using Gwen.Input; +using Gwen.Extensions; namespace Gwen.Control { diff --git a/Gwen/Control/ComboBox.cs b/Gwen/Control/ComboBox.cs index 4b45080..684a40c 100644 --- a/Gwen/Control/ComboBox.cs +++ b/Gwen/Control/ComboBox.cs @@ -1,6 +1,7 @@ using System; //using System.Drawing; using Gwen.ControlInternal; +using Gwen.Extensions; namespace Gwen.Control { diff --git a/Gwen/Control/Menu.cs b/Gwen/Control/Menu.cs index 01e976d..3f0edbf 100644 --- a/Gwen/Control/Menu.cs +++ b/Gwen/Control/Menu.cs @@ -2,6 +2,7 @@ //using System.Drawing; using System.Linq; using Gwen.ControlInternal; +using Gwen.Extensions; namespace Gwen.Control { diff --git a/Gwen/Extensions/ListExtensions.cs b/Gwen/Extensions/ListExtensions.cs new file mode 100644 index 0000000..d08cd6e --- /dev/null +++ b/Gwen/Extensions/ListExtensions.cs @@ -0,0 +1,157 @@ +using System; +using System.Collections.Generic; + +namespace Gwen.Extensions +{ + // These methods copied from Mono's System.Collections.Generic.List implementation + // due to not being present in the Portable Class Library implementation. + // source: https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Collections.Generic/List.cs + + #region Copy of Mono license + + // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com) + // Copyright (C) 2005 David Waite + // Copyright (C) 2011,2012 Xamarin, Inc (http://www.xamarin.com) + // + // Permission is hereby granted, free of charge, to any person obtaining + // a copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to + // permit persons to whom the Software is furnished to do so, subject to + // the following conditions: + // + // The above copyright notice and this permission notice shall be + // included in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + #endregion + + public static class ListExtensions + { + public static T Find(this List list, Predicate match) + { + list.CheckMatch(match); + int i = list.GetIndex(0, list.Capacity, match); + return (i != -1) ? list[i] : default(T); + } + + private static void CheckMatch(this List list, Predicate match) + { + if (match == null) + throw new ArgumentNullException ("match"); + } + + public static List FindAll(this List list, Predicate match) + { + list.CheckMatch(match); + if (list.Capacity <= 0x10000) // <= 8 * 1024 * 8 (8k in stack) + return list.FindAllStackBits(match); + else + return list.FindAllList(match); + } + + private static List FindAllStackBits(this List list, Predicate match) + { + unsafe + { + uint *bits = stackalloc uint[(list.Capacity / 32) + 1]; + uint *ptr = bits; + int found = 0; + uint bitmask = 0x80000000; + + for (int i = 0; i < list.Capacity; i++) + { + if (match(list[i])) + { + (*ptr) = (*ptr) | bitmask; + found++; + } + + bitmask = bitmask >> 1; + if (bitmask == 0) + { + ptr++; + bitmask = 0x80000000; + } + } + + T[] results = new T[found]; + bitmask = 0x80000000; + ptr = bits; + int j = 0; + for (int i = 0; i < list.Capacity && j < found; i++) + { + if (((*ptr) & bitmask) == bitmask) + results[j++] = list[i]; + + bitmask = bitmask >> 1; + if (bitmask == 0) + { + ptr++; + bitmask = 0x80000000; + } + } + + return new List(results); + } + } + + private static List FindAllList(this List list, Predicate match) + { + List results = new List(); + for (int i = 0; i < list.Capacity; i++) + if (match(list[i])) + results.Add(list[i]); + + return results; + } + + public static int FindIndex(this List list, Predicate match) + { + list.CheckMatch(match); + return list.GetIndex(0, list.Capacity, match); + } + + private static int GetIndex(this List list, int startIndex, int count, Predicate match) + { + int end = startIndex + count; + for (int i = startIndex; i < end; i ++) + if (match(list[i])) + return i; + + return -1; + } + + public static int FindLastIndex(this List list, Predicate match) + { + list.CheckMatch(match); + return list.GetLastIndex(0, list.Capacity, match); + } + + private static int GetLastIndex(this List list, int startIndex, int count, Predicate match) + { + // unlike FindLastIndex, takes regular params for search range + for (int i = startIndex + count; i != startIndex;) + if (match(list[--i])) + return i; + return -1; + } + + public static void ForEach(this List list, Action action) + { + if (action == null) + throw new ArgumentNullException ("action"); + for(int i=0; i < list.Capacity; i++) + action(list[i]); + } + } +} + diff --git a/Gwen/Gwen.csproj b/Gwen/Gwen.csproj index c723efc..652d73c 100644 --- a/Gwen/Gwen.csproj +++ b/Gwen/Gwen.csproj @@ -5,10 +5,13 @@ AnyCPU 10.0.0 2.0 - {D3F5E624-3AF2-418F-A180-8A4172928065} + {BA49D193-A334-4D34-8835-510654776102} + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} Library Gwen Gwen + Profile14 + v4.0 true @@ -19,32 +22,33 @@ prompt 4 false - true - 1591 + true - none + full true bin\Release prompt 4 false - true - 1591 + true - + + + + @@ -145,8 +149,6 @@ - - @@ -155,8 +157,10 @@ - - - + + + + + \ No newline at end of file diff --git a/Gwen/Platform/Windows.cs b/Gwen/Platform/Windows.cs deleted file mode 100644 index 36470ea..0000000 --- a/Gwen/Platform/Windows.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using Microsoft.Win32; - -// todo: compile/run only on windows - -namespace Gwen.Platform -{ - /// - /// Windows-specific utility functions. - /// - public static class Windows - { - private const String FontRegKey = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts"; - - private static Dictionary m_FontPaths; - - /// - /// Gets a font file path from font name. - /// - /// Font name. - /// Font file path. - public static String GetFontPath(String fontName) - { - // is this reliable? we rely on lazy jitting to not run win32 code on linux - if (Environment.OSVersion.Platform != PlatformID.Win32NT) - return null; - - if (m_FontPaths == null) - InitFontPaths(); - - if (!m_FontPaths.ContainsKey(fontName)) - return null; - - return m_FontPaths[fontName]; - } - - private static void InitFontPaths() - { - // very hacky but better than nothing - m_FontPaths = new Dictionary(); - String fontsDir = Environment.GetFolderPath(Environment.SpecialFolder.Fonts); - - RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default); - RegistryKey subkey = key.OpenSubKey(FontRegKey); - foreach (String fontName in subkey.GetValueNames()) - { - String fontFile = (String)subkey.GetValue(fontName); - if (!fontName.EndsWith(" (TrueType)")) - continue; - String font = fontName.Replace(" (TrueType)", ""); - m_FontPaths[font] = Path.Combine(fontsDir, fontFile); - } - key.Dispose(); - } - } -} diff --git a/Gwen/Point.cs b/Gwen/Point.cs index ead9241..2bad218 100644 --- a/Gwen/Point.cs +++ b/Gwen/Point.cs @@ -2,7 +2,6 @@ using System; namespace Gwen { - [Serializable] public struct Point { public int X; diff --git a/Gwen/Properties/AssemblyInfo.cs b/Gwen/Properties/AssemblyInfo.cs index 6ec482a..a87e7db 100644 --- a/Gwen/Properties/AssemblyInfo.cs +++ b/Gwen/Properties/AssemblyInfo.cs @@ -15,14 +15,6 @@ using System.Runtime.InteropServices; [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("89f684eb-caf7-4ff0-80c6-4437040c8b8d")] - // Version information for an assembly consists of the following four values: // // Major Version diff --git a/Gwen/Rectangle.cs b/Gwen/Rectangle.cs index 6936ff6..56a9439 100644 --- a/Gwen/Rectangle.cs +++ b/Gwen/Rectangle.cs @@ -2,7 +2,6 @@ using System; namespace Gwen { - [Serializable] public struct Rectangle { public int X; diff --git a/Gwen/Util.cs b/Gwen/Util.cs index 92302db..6589e88 100644 --- a/Gwen/Util.cs +++ b/Gwen/Util.cs @@ -11,7 +11,12 @@ namespace Gwen { public static int Round(float x) { - return (int)Math.Round(x, MidpointRounding.AwayFromZero); + //return (int)Math.Round(x, MidpointRounding.AwayFromZero); + if (x > 0) + return (int)Math.Floor(x + 0.5); + else + return (int)Math.Ceiling(x - 0.5); + } /* public static int Trunc(float x) diff --git a/GwenCS.sln b/GwenCS.sln index 525606f..1cc6ec1 100644 --- a/GwenCS.sln +++ b/GwenCS.sln @@ -1,20 +1,20 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gwen", "Gwen\Gwen.csproj", "{D3F5E624-3AF2-418F-A180-8A4172928065}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D3F5E624-3AF2-418F-A180-8A4172928065}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D3F5E624-3AF2-418F-A180-8A4172928065}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D3F5E624-3AF2-418F-A180-8A4172928065}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D3F5E624-3AF2-418F-A180-8A4172928065}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = Gwen\Gwen.csproj - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gwen", "Gwen\Gwen.csproj", "{BA49D193-A334-4D34-8835-510654776102}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BA49D193-A334-4D34-8835-510654776102}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BA49D193-A334-4D34-8835-510654776102}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BA49D193-A334-4D34-8835-510654776102}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BA49D193-A334-4D34-8835-510654776102}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = Gwen\Gwen.csproj + EndGlobalSection +EndGlobal