public ColorScheme(RegistryKey key) : this() { Name = key.Name.Substring(key.Name.LastIndexOf('\\') + 1); // Border - may be texture or static color object borderTextureObject = key.GetValue("BorderTexture"); if (borderTextureObject != null) { if (TextureLibrary.Contains((string)borderTextureObject)) { BorderTexture = TextureLibrary.Lookup((string)borderTextureObject); } else { throw new Exception("Unknown texture specified in color scheme: " + (string)borderTextureObject); } } else { BorderColor = Color.FromArgb(Convert.ToInt32(key.GetValue("BorderColor"))); } HighlightColor = Color.FromArgb(Convert.ToInt32(key.GetValue("HighlightColor"))); TextColor = Color.FromArgb(Convert.ToInt32(key.GetValue("TextColor"))); int nSquareColors = 0; object value; // repeat for each square color until we run out of specified colors/textures while (true) { // first see if a texture is defined for this square color string valuename = "SquareTexture" + (nSquareColors + 1).ToString(); value = key.GetValue(valuename); if (value != null) { if (TextureLibrary.Contains((string)value)) { if (SquareTextures == null) { SquareTextures = new Dictionary <int, Texture>(); } Texture texture = TextureLibrary.Lookup((string)value); SquareTextures.Add(nSquareColors, texture); SquareColors.Add(nSquareColors, texture.SubstituteColor); nSquareColors++; } else { throw new Exception("Unknown texture specified in color scheme: " + valuename); } } else { // if no texture check for a specific color valuename = "SquareColor" + (nSquareColors + 1).ToString(); value = key.GetValue(valuename); if (value != null) { SquareColors.Add(nSquareColors, Color.FromArgb(Convert.ToInt32(value))); nSquareColors++; } else { // out of square colors/textures break; } } } NumberOfColors = nSquareColors; int nPlayerColors = 0; string playerColorValueName = "PlayerColor" + (nPlayerColors + 1).ToString(); while ((value = key.GetValue(playerColorValueName)) != null) { PlayerColors.Add(nPlayerColors, Color.FromArgb(Convert.ToInt32(value))); nPlayerColors++; playerColorValueName = "PlayerColor" + (nPlayerColors + 1).ToString(); } Modified = false; }
public void WriteToRegistry(RegistryKey key) { Modified = false; // save the standard color values that all schemes have key.SetValue("BorderColor", unchecked ((Int32)BorderColor.ToArgb()), RegistryValueKind.DWord); key.SetValue("HighlightColor", unchecked ((Int32)HighlightColor.ToArgb()), RegistryValueKind.DWord); key.SetValue("TextColor", unchecked ((Int32)TextColor.ToArgb()), RegistryValueKind.DWord); // save out all square colors/textures counting the number of // square colors in the scheme as we go int nSquareColors = 0; while (true) { if (SquareTextures != null && SquareTextures.ContainsKey(nSquareColors)) { key.SetValue("SquareTexture" + (nSquareColors + 1).ToString(), SquareTextures[nSquareColors].Name, RegistryValueKind.String); } else if (SquareColors.ContainsKey(nSquareColors)) { key.SetValue("SquareColor" + (nSquareColors + 1).ToString(), unchecked ((Int32)SquareColors[nSquareColors].ToArgb()), RegistryValueKind.DWord); // if there's a specified texture already, remove it or else // it will override the color when we load this scheme again if (key.GetValue("SquareTexture" + (nSquareColors + 1).ToString()) != null) { key.DeleteValue("SquareTexture" + (nSquareColors + 1).ToString()); } } else { break; } nSquareColors++; } // clear out any additional colors/textures specified in // the registry that we no longer have while (true) { bool found = false; int extraColorCount = nSquareColors + 1; if (key.GetValue("SquareTexture" + extraColorCount.ToString()) != null) { key.DeleteValue("SquareTexture" + extraColorCount.ToString()); found = true; } if (key.GetValue("SquareColor" + extraColorCount.ToString()) != null) { key.DeleteValue("SquareColor" + extraColorCount.ToString()); found = true; } extraColorCount++; if (!found) { break; } } // write out the player colors foreach (KeyValuePair <int, Color> pair in PlayerColors) { key.SetValue("PlayerColor" + (pair.Key + 1).ToString(), unchecked ((Int32)pair.Value.ToArgb()), RegistryValueKind.DWord); } }