Files
RaindropViewer/Assets/Raindrop/Core/Settings.cs
T
alexiscatnip 5e6cc3e7ea Oops, forgot to commit many things:
- bootstrapper now not longer init the Logger.
- bootstrapper now starts the copy of missing files from StreamingAssets folder to the device's internal storage.
- UIBootstrapper now requires you to manually inject the components from the scene graph
- Added alot of logic to gridmanager for saving and loading custom grids, but it seems quite repeated and convoluted.
- some disparities between our Raindrop core and Radegast core is noticed.
- start to use color in the chat printer.
- add loading screen to UIService
- add the canvas type of POP so that the ButtonTriggerViewTransition can trigger a pop-only.
2022-02-06 13:39:16 +08:00

410 lines
13 KiB
C#

//
// Radegast Metaverse Client
// Copyright (c) 2009-2014, Radegast Development Team
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the application "Radegast", nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// $Id$
//
using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
//using System.Drawing;
//using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.ComponentModel;
using System.Runtime.Serialization;
using UnityEngine;
using Formatting = System.Xml.Formatting;
//using Font = Catnip.Drawing.Font;
using Logger = OpenMetaverse.Logger;
namespace Raindrop
{
public class Settings : IDictionary<string, OSD>
{
private string SettingsFile;
private OSDMap SettingsData;
public delegate void SettingChangedCallback(object sender, SettingsEventArgs e);
public event SettingChangedCallback OnSettingChanged;
private static Color DarkBlue => new Color(0.2f,0.2f,1f);
private static Color LightBlue => new Color(0.5f,0.7f,1f);
private static Color DarkRed => new Color(0.6f,0f,0f);
private static Color DarkGoldenrod => new Color(0.72f,0.52f,4.0f);
private static Color DarkCyan => new Color(0f,1f,1f);
public static readonly Dictionary<string, FontSetting> DefaultFontSettings = new Dictionary<string, FontSetting>()
{
{"Normal", new FontSetting {
Name = "Normal",
ForeColor = Color.black,
}},
{"StatusBlue", new FontSetting {
Name = "StatusBlue",
ForeColor = LightBlue,
}},
{"StatusDarkBlue", new FontSetting {
Name = "StatusDarkBlue",
ForeColor = DarkBlue,
}},
{"LindenChat", new FontSetting {
Name = "LindenChat",
ForeColor = Color.green,
}},
{"ObjectChat", new FontSetting {
Name = "ObjectChat",
ForeColor = DarkCyan,
}},
{"StartupTitle", new FontSetting {
Name = "StartupTitle",
ForeColor = Color.black,
}},
{"Alert", new FontSetting {
Name = "Alert",
ForeColor = DarkRed,
}},
{"Error", new FontSetting {
Name = "Error",
ForeColor = Color.red,
}},
{"OwnerSay", new FontSetting {
Name = "OwnerSay",
ForeColor = DarkGoldenrod,
}},
{"Timestamp", new FontSetting {
Name = "Timestamp",
ForeColor = Color.gray,
}},
{"Name", new FontSetting {
Name = "Name",
ForeColor = Color.black,
}},
{"Notification", new FontSetting {
Name = "Notification",
ForeColor = Color.black,
}},
{"IncomingIM", new FontSetting {
Name = "IncomingIM",
ForeColor = Color.black,
}},
{"OutgoingIM", new FontSetting {
Name = "OutgoingIM",
ForeColor = Color.black,
}},
{"Emote", new FontSetting {
Name = "Emote",
ForeColor = Color.black,
}},
{"Self", new FontSetting {
Name = "Self",
ForeColor = Color.black,
}},
};
public class FontSetting
{
//[JsonIgnore]
//public static readonly Font DefaultFont = new Font(FontFamily.GenericSansSerif, 8.0f);
//[JsonIgnore]
//public Font Font;
[IgnoreDataMember]
public Color ForeColor;
public String Name;
//
// public string ForeColorString
// {
// get
// {
// if (ForeColor != null)
// {
// return ColorTranslator.ToHtml(ForeColor);
// }
// else
// {
// return ColorTranslator.ToHtml(Color.Black);
// }
// }
// set
// {
// ForeColor = ColorTranslator.FromHtml(value);
// }
// }
//
// public string BackColorString
// {
// get
// {
// if (BackColor != null)
// {
// return ColorTranslator.ToHtml(BackColor);
// }
// else
// {
// return ColorTranslator.ToHtml(Color.Black);
// }
// }
// set
// {
// BackColor = ColorTranslator.FromHtml(value);
// }
// }
// public string FontString
// {
// get
// {
// if (this.Font != null)
// {
// TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));
// return converter.ConvertToString(this.Font);
// }
// else
// {
// return null;
// }
// }
// set
// {
// try
// {
// TypeConverter converter = TypeDescriptor.GetConverter(typeof(Font));
// this.Font = converter.ConvertFromString(value) as Font;
// }
// catch (Exception)
// {
// this.Font = DefaultFont;
// }
//
// }
// }
public override string ToString()
{
return Name;
}
}
public Settings(string fileName)
{
SettingsFile = fileName;
try
{
string xml = File.ReadAllText(SettingsFile);
SettingsData = (OSDMap)OSDParser.DeserializeLLSDXml(xml);
}
catch
{
Logger.DebugLog("Failed opening settings file: " + fileName);
SettingsData = new OSDMap();
Save();
}
}
public void Save()
{
try
{
File.WriteAllText(SettingsFile, SerializeLLSDXmlStringFormated(SettingsData));
}
catch (Exception ex)
{
Logger.Log("Failed saving settings", Helpers.LogLevel.Warning, ex);
}
}
public static string SerializeLLSDXmlStringFormated(OSD data)
{
using (StringWriter sw = new StringWriter())
{
using (XmlTextWriter writer = new XmlTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
writer.WriteStartElement(String.Empty, "llsd", String.Empty);
OSDParser.SerializeLLSDXmlElement(writer, data);
writer.WriteEndElement();
writer.Close();
return sw.ToString();
}
}
}
private void FireEvent(string key, OSD val)
{
if (OnSettingChanged != null)
{
try { OnSettingChanged(this, new SettingsEventArgs(key, val)); }
catch (Exception) { }
}
}
#region IDictionary Implementation
public int Count { get { return SettingsData.Count; } }
public bool IsReadOnly { get { return false; } }
public ICollection<string> Keys { get { return SettingsData.Keys; } }
public ICollection<OSD> Values { get { return SettingsData.Values; } }
public OSD this[string key]
{
get
{
return SettingsData[key];
}
set
{
if (string.IsNullOrEmpty(key))
{
Logger.DebugLog("Warning: trying to set an emprty setting: " + Environment.StackTrace.ToString());
}
else
{
SettingsData[key] = value;
FireEvent(key, value);
Save();
}
}
}
public bool ContainsKey(string key)
{
return SettingsData.ContainsKey(key);
}
public void Add(string key, OSD llsd)
{
SettingsData.Add(key, llsd);
FireEvent(key, llsd);
Save();
}
public void Add(KeyValuePair<string, OSD> kvp)
{
SettingsData.Add(kvp.Key, kvp.Value);
FireEvent(kvp.Key, kvp.Value);
Save();
}
public bool Remove(string key)
{
bool ret = SettingsData.Remove(key);
FireEvent(key, null);
Save();
return ret;
}
public bool TryGetValue(string key, out OSD llsd)
{
return SettingsData.TryGetValue(key, out llsd);
}
public void Clear()
{
SettingsData.Clear();
Save();
}
public bool Contains(KeyValuePair<string, OSD> kvp)
{
// This is a bizarre function... we don't really implement it
// properly, hopefully no one wants to use it
return SettingsData.ContainsKey(kvp.Key);
}
public void CopyTo(KeyValuePair<string, OSD>[] array, int index)
{
throw new NotImplementedException();
}
public bool Remove(KeyValuePair<string, OSD> kvp)
{
bool ret = SettingsData.Remove(kvp.Key);
FireEvent(kvp.Key, null);
Save();
return ret;
}
public System.Collections.IDictionaryEnumerator GetEnumerator()
{
return SettingsData.GetEnumerator();
}
IEnumerator<KeyValuePair<string, OSD>> IEnumerable<KeyValuePair<string, OSD>>.GetEnumerator()
{
return null;
}
IEnumerator IEnumerable.GetEnumerator()
{
return SettingsData.GetEnumerator();
}
#endregion IDictionary Implementation
}
public class SettingsEventArgs : EventArgs
{
public string Key = string.Empty;
public OSD Value = new OSD();
public SettingsEventArgs(string key, OSD val)
{
Key = key;
Value = val;
}
}
}