Example #1
0
        /// <summary>
        /// 仅在第一次初始化时执行
        /// </summary>
        public async void OnFirstLoad()
        {
            Geoposition Position;

            try
            {
                Position = await GetPositionAsync();
            }
            catch (InvalidOperationException)
            {
                WeatherCtr.Error(ErrorReason.Location);
                return;
            }
            catch (Exception)
            {
                WeatherCtr.Error(ErrorReason.NetWork);
                return;
            }

            //将获取到的GPS位置发送至百度地图逆地址解析服务
            float  lat    = (float)Position.Coordinate.Point.Position.Latitude;
            float  lon    = (float)Position.Coordinate.Point.Position.Longitude;
            string URL    = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=qrTMQKoNdBj3H6N7ZTdIbRnbBOQjcDGQ&output=json&coordtype=wgs84ll&location=" + lat + "," + lon;
            string Result = await GetWebResponseAsync(URL);

            if (Result != "")
            {
                //异步运行以在完成诸多解析任务的同时保持UI响应能力
                await Task.Run(async() =>
                {
                    var WeatherInfo = await GetWeatherInfoAsync(GetDistrictByAnalysisJSON(Result));
                    if (WeatherInfo == null)
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            WeatherCtr.Error(ErrorReason.APIError);
                        });
                        return;
                    }

                    //status=200时表示请求成功
                    if (WeatherInfo.status == 200)
                    {
                        WeatherDataGenarated?.Invoke(null, new WeatherData(WeatherInfo.data, jp.result.addressComponent.city + jp.result.addressComponent.district));
                    }
                    else
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            WeatherCtr.Error(ErrorReason.APIError);
                        });
                    }
                });
            }
        }
Example #2
0
        /// <summary>
        /// 向URL指定的网络服务器发出HTTP请求,获取返回服务器返回信息
        /// </summary>
        /// <param name="url">网络服务器地址</param>
        /// <returns>网络服务器返回的信息</returns>
        private async Task <string> GetWebResponseAsync(string url)
        {
            string     strBuff = string.Empty;
            Uri        HttpURL = new Uri(url);
            WebRequest httpReq = WebRequest.Create(HttpURL);

            try
            {
                WebResponse httpResp = await httpReq.GetResponseAsync();

                Stream       respStream       = httpResp.GetResponseStream();
                StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);
                strBuff = await respStreamReader.ReadToEndAsync();
            }
            catch (WebException)
            {
                WeatherCtr.Error(ErrorReason.NetWork);
            }
            return(strBuff);
        }
Example #3
0
        static void UpdateGradients()
        {
            if (WeatherModule.Self == null)
            {
                return;
            }
            WeatherCtr weatherCtr = (WeatherCtr)typeof(WeatherModule).GetField("weatherCtr", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(WeatherModule.Self);

            if (weatherCtr == null)
            {
                return;
            }
            Executor executor = (Executor)typeof(WeatherCtr).GetField("executor", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(weatherCtr);

            if (defaultGradients == null)
            {
                Dbgl("setting defaults");
                defaultGradients = new List <Dictionary <string, Gradient> >();
                foreach (BiomoTheme bt in executor.BiomoThemes)
                {
                    Dictionary <string, Gradient> dic = new Dictionary <string, Gradient>();
                    foreach (string grad in gradientMasks)
                    {
                        dic.Add(grad, (Gradient)typeof(BiomoTheme).GetField(grad).GetValue(bt));
                    }
                    defaultGradients.Add(dic);
                }
            }
            if (defaultGradients == null)
            {
                return;
            }

            GradientColorKey[] gc  = defaultGradients[executor.BiomoIndex]["AmbientSkyColorChange"].colorKeys;
            string             str = "";

            foreach (GradientColorKey g in gc)
            {
                str += g.color + "\r\n";
            }
            Dbgl(str);

            for (int k = 0; k < executor.BiomoThemes.Length; k++)
            {
                foreach (string name in gradientMasks)
                {
                    Gradient gradient = new Gradient();
                    gradient.colorKeys = defaultGradients[k][name].colorKeys;
                    gradient.alphaKeys = defaultGradients[k][name].alphaKeys;

                    //Dbgl("checking " + name);
                    if (enabled && (bool)typeof(Settings).GetField(name + "GradientMaskEnable").GetValue(settings))
                    {
                        //Dbgl("setting " + name);
                        GradientColorKey[] gradientColorKeys = gradient.colorKeys;
                        for (int i = 0; i < gradientColorKeys.Length; i++)
                        {
                            Color color = gradientColorKeys[i].color;
                            if ((bool)typeof(Settings).GetField(name + "GradientMaskForceMonochrome").GetValue(settings))
                            {
                                float mask = (float)typeof(Settings).GetField(name + "GradientMaskColorMonochrome").GetValue(settings);
                                float orig = (color.r + color.g + color.b) / 3;
                                float grey;
                                if (mask >= 0)
                                {
                                    grey = orig + (1f - orig) * mask;
                                }
                                else
                                {
                                    grey = orig + orig * mask;
                                }
                                gradientColorKeys[i].color.r = grey;
                                gradientColorKeys[i].color.g = grey;
                                gradientColorKeys[i].color.b = grey;
                            }
                            else
                            {
                                float r = (float)typeof(Settings).GetField(name + "GradientMaskR").GetValue(settings);
                                float g = (float)typeof(Settings).GetField(name + "GradientMaskG").GetValue(settings);
                                float b = (float)typeof(Settings).GetField(name + "GradientMaskB").GetValue(settings);
                                if (r >= 0)
                                {
                                    gradientColorKeys[i].color.r = color.r + (1f - color.r) * r;
                                }
                                else
                                {
                                    gradientColorKeys[i].color.r = color.r + color.r * r;
                                }
                                if (g >= 0)
                                {
                                    gradientColorKeys[i].color.g = color.g + (1f - color.g) * g;
                                }
                                else
                                {
                                    gradientColorKeys[i].color.g = color.g + color.g * g;
                                }
                                if (b >= 0)
                                {
                                    gradientColorKeys[i].color.b = color.b + (1f - color.b) * b;
                                }
                                else
                                {
                                    gradientColorKeys[i].color.b = color.b + color.b * b;
                                }
                            }
                            float a = (float)typeof(Settings).GetField(name + "GradientMaskA").GetValue(settings);
                            if (a >= 0)
                            {
                                gradientColorKeys[i].color.a = color.a + (1f - color.a) * a;
                            }
                            else
                            {
                                gradientColorKeys[i].color.a = color.a + color.a * a;
                            }
                        }
                        gradient.colorKeys = gradientColorKeys;
                        //Dbgl("setting " + name + " " + gradient.colorKeys[0].color);
                    }
                    GradientAlphaKey[] gradientAlphaKeys = gradient.alphaKeys;
                    float a2 = (float)typeof(Settings).GetField(name + "GradientMaskA").GetValue(settings);
                    for (int i = 0; i < gradientAlphaKeys.Length; i++)
                    {
                        float alpha = gradientAlphaKeys[i].alpha;
                        if (a2 >= 0)
                        {
                            gradientAlphaKeys[i].alpha = alpha + (1f - alpha) * a2;
                        }
                        else
                        {
                            gradientAlphaKeys[i].alpha = alpha + alpha * a2;
                        }
                    }
                    gradient.alphaKeys = gradientAlphaKeys;
                    typeof(BiomoTheme).GetField(name).SetValue(executor.BiomoThemes[k], gradient);
                }
            }
        }
Example #4
0
        private static void SetColorSetting(string name, float min = 0, float max = 1f, bool isG = false)
        {
            FieldInfo mie = typeof(Settings).GetField(name + "Enable");
            FieldInfo mif = typeof(Settings).GetField(name + "ForceMonochrome");
            FieldInfo mir = typeof(Settings).GetField(name + "R");
            FieldInfo mig = typeof(Settings).GetField(name + "G");
            FieldInfo mib = typeof(Settings).GetField(name + "B");
            FieldInfo mia = typeof(Settings).GetField(name + "A");
            FieldInfo mim = typeof(Settings).GetField(name + "ColorMonochrome");

            bool  ev = (bool)mie.GetValue(settings);
            float rv = (float)mir.GetValue(settings);
            float gv = (float)mig.GetValue(settings);
            float bv = (float)mib.GetValue(settings);
            float av = (float)mia.GetValue(settings);
            float mv = 0;

            if (isG)
            {
                mv = (float)mim.GetValue(settings);
            }

            GUILayout.BeginHorizontal();
            mie.SetValue(settings, GUILayout.Toggle((bool)mie.GetValue(settings), $"<b>{name}</b>", new GUILayoutOption[0]));
            if (isG)
            {
                mif.SetValue(settings, GUILayout.Toggle((bool)mif.GetValue(settings), $"<b>Force Monochrome</b>", new GUILayoutOption[] { GUILayout.Width(labelWidth * 2f) }));
            }
            GUILayout.EndHorizontal();

            if ((bool)mie.GetValue(settings))
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(indentSpace);
                GUILayout.BeginVertical();
                if (isG && (bool)mif.GetValue(settings))
                {
                    GUILayout.Label(string.Format("Grey: <b>{0:F2}</b> ", mim.GetValue(settings)), new GUILayoutOption[] { GUILayout.Width(labelWidth) });
                    mim.SetValue(settings, GUILayout.HorizontalSlider((float)mim.GetValue(settings) * 100f, 100f * min, 100f * max) / 100f);
                }
                else
                {
                    GUILayout.Label(string.Format("Red: <b>{0:F2}</b> ", mir.GetValue(settings)), new GUILayoutOption[] { GUILayout.Width(labelWidth) });
                    mir.SetValue(settings, GUILayout.HorizontalSlider((float)mir.GetValue(settings) * 100f, 100f * min, 100f * max) / 100f);
                    GUILayout.Label(string.Format("Green: <b>{0:F2}</b> ", mig.GetValue(settings)), new GUILayoutOption[] { GUILayout.Width(labelWidth) });
                    mig.SetValue(settings, GUILayout.HorizontalSlider((float)mig.GetValue(settings) * 100f, 100f * min, 100f * max) / 100f);
                    GUILayout.Label(string.Format("Blue: <b>{0:F2}</b> ", mib.GetValue(settings)), new GUILayoutOption[] { GUILayout.Width(labelWidth) });
                    mib.SetValue(settings, GUILayout.HorizontalSlider((float)mib.GetValue(settings) * 100f, 100f * min, 100f * max) / 100f);
                }
                GUILayout.Label(string.Format("Alpha: <b>{0:F2}</b> ", mia.GetValue(settings)), new GUILayoutOption[] { GUILayout.Width(labelWidth) });
                mia.SetValue(settings, GUILayout.HorizontalSlider((float)mia.GetValue(settings) * 100f, 100f * min, 100f * max) / 100f);
                if (isG && defaultGradients != null)
                {
                    List <string>      defColorStrings = new List <string>();
                    WeatherCtr         weatherCtr      = (WeatherCtr)typeof(WeatherModule).GetField("weatherCtr", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(WeatherModule.Self);
                    Executor           executor        = (Executor)typeof(WeatherCtr).GetField("executor", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(weatherCtr);
                    GradientColorKey[] defColors       = defaultGradients[executor.BiomoIndex][name.Replace("GradientMask", "")].colorKeys;
                    foreach (GradientColorKey gc in defColors)
                    {
                        defColorStrings.Add($"({Math.Round(gc.color.r*100)/100f},{Math.Round(gc.color.g * 100) / 100f},{Math.Round(gc.color.b * 100) / 100f},{Math.Round(gc.color.a * 100) / 100f})");
                    }
                    GUILayout.Label("Defaults: " + string.Join(",", defColorStrings.ToArray()), new GUILayoutOption[0]);
                }
                GUILayout.EndVertical();
                GUILayout.EndHorizontal();
            }
            GUILayout.Space(20f);

            if (isG && (ev != (bool)mie.GetValue(settings) || rv != (float)mir.GetValue(settings) || gv != (float)mig.GetValue(settings) || bv != (float)mib.GetValue(settings) || av != (float)mia.GetValue(settings) || mv != (float)mim.GetValue(settings)))
            {
                UpdateGradients();
            }
        }