Example #1
0
 public void SafeUnload()
 {
     if (this._renderer != null)
     {
         this._renderer.Dispose();
         this._renderer = null;
     }
     this._canvasControl.Draw -= OnDraw;
     this._canvasControl.RemoveFromVisualTree();
     this._canvasControl = null;
 }
Example #2
0
        private void OnContentChanged(SvgDocument svg)
        {
            if (this._renderer != null)
            {
                this._renderer.Dispose();
                this._renderer = null;
            }

            if (svg != null && this._canvasControl != null)
            {
                this._renderer = new Win2dRenderer(this._canvasControl, svg);
                this._canvasControl?.Invalidate();
            }
        }
Example #3
0
		private void OnContentChanged(SvgDocument svg)
		{
			if (this._renderer != null)
			{
				this._renderer.Dispose();
				this._renderer = null;
			}

			if (svg != null && this._canvasControl != null)
			{
				this._renderer = new Win2dRenderer(this._canvasControl, svg);
				this._canvasControl?.Invalidate();
			}
		}
Example #4
0
		private static void RendererImage(Stream stream, SvgImageRendererSettings settings)
		{
			var svg = settings.Document;
			var viewPort = svg.RootElement.ViewPort;
			if (!viewPort.HasValue) throw new ArgumentException(nameof(settings));

			var width = viewPort.Value.Width;
			var height = viewPort.Value.Height;
			var device = CanvasDevice.GetSharedDevice();
			using (var offScreen = new CanvasRenderTarget(device, width, height, settings.Scaling * 96.0F))
			{
				using (var renderer = new Win2dRenderer(offScreen, svg))
				using (var session = offScreen.CreateDrawingSession())
				{
					session.Clear(Colors.Transparent);
					renderer.Render(width, height, session);
				}
				offScreen.SaveAsync(stream.AsRandomAccessStream(), (CanvasBitmapFileFormat)settings.Format, settings.Quality).AsTask().GetAwaiter().GetResult();
			}
		}
Example #5
0
		public void SafeUnload()
		{
			if (this._renderer != null)
			{
				this._renderer.Dispose();
				this._renderer = null;
			}
			this._canvasControl.Draw -= OnDraw;
			this._canvasControl.RemoveFromVisualTree();
			this._canvasControl = null;
		}
 public async Task LoadResources(Win2dRenderer renderer)
 {
     renderer.init = true;
 }
Example #7
0
        public async Task <Point> DrawWeather(CanvasDrawingSession ds, CanvasDevice device, CanvasBitmap canvasBitmap, Point drawPoint, bool isBackground = false)
        {
            #region Init & Get Weather Informations

            Size   screenSize = SettingManager.GetWindowsResolution();
            var    width      = screenSize.Width;
            var    height     = screenSize.Height;
            float  space      = (float)width - (float)drawPoint.X;
            Rect   textSize;
            var    newWidth     = width - space;
            double tempWidth    = 0;
            var    oldDrawPoint = drawPoint.Y;

            if (drawPoint.Y >= height)
            {
                drawPoint.Y = drawPoint.Y > height ? drawPoint.Y - height - height * 22 / 100 : height - height * 25 / 100;
            }

            if (!SettingsHelper.GetSetting <bool>(SettingKey.IsDisplayWeather.ToString()))
            {
                return(drawPoint);
            }

            //Get Weather condition in the 1st time
            if (CurrentWeatherInfo.Temp == null)
            {
                if (CurrentWeather?.Main == null)
                {
                    //If FixedLocation Mode is ON then get weather with FixedLocation
                    if (IsFixedLocation)
                    {
                        await GetWeather(false);
                    }
                    else
                    {
                        await GetWeather();
                    }
                }
            }
            else
            {
                //If the final state is dif then get new WeatherCondition
                if (IsFixedLocation == IsNormalMode)
                {
                    if (IsFixedLocation)
                    {
                        if (!string.IsNullOrEmpty(UserLocation))
                        {
                            await GetWeather(false);
                        }

                        //Update Mode status & IconSaved
                        StartVm.IsIconSaved = false;
                        IsNormalMode        = false;
                    }
                    else
                    {
                        await GetWeather();

                        StartVm.IsIconSaved = false;
                        IsNormalMode        = true;
                    }
                }
            }

            var isFahrenheit = SettingsHelper.GetSetting <bool>(SettingKey.IsFahrenheit.ToString());

            //If CurrentWeather isn't loaded then draw with temporary template
            //Avoid step out DrawWeather method when haven't drawn and make User think Weather Function is broken
            if (CurrentWeather?.Main == null)
            {
                CurrentWeatherInfo.Temp = isFahrenheit ? "? °F" : "? °C";
                if (string.IsNullOrEmpty(CurrentWeatherInfo.Address))
                {
                    CurrentWeatherInfo.Address       = "???";
                    CurrentWeatherInfo.Condition     = "clouds";
                    CurrentWeatherInfo.MainCondition = "??";
                }

                //Update final state to get WeatherCondition in the next time
                IsNormalMode = IsFixedLocation;
            }
            else
            {
                if (isFahrenheit)
                {
                    CurrentWeatherInfo.Temp = Math.Round(CurrentWeather.Main.Temp * 1.8 + 32) + "°F";
                }
                else
                {
                    CurrentWeatherInfo.Temp = Math.Round(CurrentWeather.Main.Temp) + "°C";
                }

                CurrentWeatherInfo.MainCondition = CurrentWeather.Weather[0].Main;
                CurrentWeatherInfo.Condition     = CurrentWeather.Weather[0].Description;
            }

            var address = CurrentWeatherInfo.Address;

            #endregion

            #region Get Address & Caculate area size, position

            if (address != null)
            {
                var strArr = address.Split(',');

                if (strArr.Count() >= 2)
                {
                    CurrentWeatherInfo.Address = string.Format("{0},{1}", strArr[strArr.Length - 2],
                                                               strArr[strArr.Length - 1]);
                }
                else
                {
                    CurrentWeatherInfo.Address = strArr[0];
                }

                var textFormat = new CanvasTextFormat
                {
                    FontFamily   = "Segoe UI Light",
                    FontSize     = (float)(height * 4 / 100),
                    WordWrapping = CanvasWordWrapping.NoWrap
                };

                textSize = BitmapHelper.TextRect(CurrentWeatherInfo.Address, textFormat, ds);

                if (string.IsNullOrEmpty(CurrentWeatherInfo.MainCondition))
                {
                    CurrentWeatherInfo.Condition     = "clouds";
                    CurrentWeatherInfo.MainCondition = "??";
                }

                //Caculate WeatherCondition text length for drawing AntiBright & Weather
                var conditionSize = BitmapHelper.TextRect(CurrentWeatherInfo.MainCondition, new CanvasTextFormat()
                {
                    FontSize     = (float)(height * 4.5 / 100),
                    FontFamily   = "Segoe UI Light",
                    FontWeight   = FontWeights.Thin,
                    WordWrapping = CanvasWordWrapping.NoWrap
                }, ds);

                var tempSize = BitmapHelper.TextRect(CurrentWeatherInfo.Temp, new CanvasTextFormat()
                {
                    FontSize     = (float)(height * 7.5 / 100),
                    WordWrapping = CanvasWordWrapping.NoWrap,
                    FontFamily   = "Segoe UI Light",
                    FontWeight   = FontWeights.Thin
                }, ds);

                //Check if CenterX + WeatherCondition(or Temperature if Temp>WeatherCon) > Address to update CenterX(use for draw WeatherCondition)
                tempWidth = tempSize.Width > conditionSize.Width ? tempSize.Width : conditionSize.Width;

                DrawPoint = new Point(newWidth - textSize.Width, drawPoint.Y);

                if (DrawPoint.X + textSize.Width + height / 100 >= newWidth)
                {
                    newWidth -= height / 100;
                    DrawPoint = new Point(newWidth - textSize.Width, drawPoint.Y);
                }

                if (drawPoint.X == 0)
                {
                    newWidth = textSize.Width > tempWidth + height * 15 / 100 + width / 100 ? textSize.Width + height * 3 / 100 : tempWidth + height * 15 / 100 + width / 100 + height * 3 / 100;

                    DrawPoint = new Point(newWidth - textSize.Width, drawPoint.Y);
                }


                //Check if WeatherIcon + TempWidth > Address to update DrawPoint
                if (DrawPoint.X + tempWidth + height * 15 / 100 + width / 100 + height / 100 >= newWidth)
                {
                    var x = DrawPoint.X - (tempWidth + DrawPoint.X + width / 100 + height * 15 / 100 + height / 100 - newWidth);
                    DrawPoint = new Point(x, drawPoint.Y);
                }

                if (Math.Abs(drawPoint.X - width / 2) < 0.5)
                {
                    var leftToCenter = width / 2 - DrawPoint.X + height * 2 / 100;
                    var haftWidth    = (newWidth - DrawPoint.X + height * 2 / 100) / 2;
                    var tempSpace    = leftToCenter - haftWidth;
                    DrawPoint = new Point(DrawPoint.X + tempSpace, DrawPoint.Y);
                    newWidth += tempSpace;
                }
            }

            #endregion

            #region Draw Methods

            if ((DrawPoint.X == 0) && (DrawPoint.Y == 0))
            {
                return(drawPoint);
            }
            {
                //Check and draw transparent black rectangle if necessary
                if (BitmapHelper.IsBrightArea(canvasBitmap,
                                              (int)(DrawPoint.X - height * 2 / 100),
                                              (int)(DrawPoint.Y - height / 100),
                                              (int)(newWidth - DrawPoint.X + height * 2 / 100),
                                              (int)height * 22 / 100))
                {
                    ds.FillRoundedRectangle(
                        new Rect(DrawPoint.X - height * 2 / 100,
                                 (int)DrawPoint.Y - height / 100,
                                 newWidth - DrawPoint.X + height * 2 / 100,
                                 height * 22 / 100), 20, 20,
                        new CanvasSolidColorBrush(device, Colors.Black)
                    {
                        Opacity = 0.4F
                    });
                }



                var strArr = CurrentWeatherInfo.Address?.Split(',');
                if (strArr?.Length > 2)
                {
                    CurrentWeatherInfo.Address = strArr[strArr.Length - 2] + "," + strArr[strArr.Length - 1];
                }

                if (CurrentWeatherInfo.Address != null)
                {
                    ds.DrawText(CurrentWeatherInfo.Address, (float)(newWidth - height * 1.5 / 100), (float)DrawPoint.Y,
                                Colors.White,
                                new CanvasTextFormat
                    {
                        FontSize            = (float)(height * 4 / 100),
                        HorizontalAlignment = CanvasHorizontalAlignment.Right,
                        FontFamily          = "Segoe UI Light",
                        FontWeight          = FontWeights.Thin
                    });
                }

                var centerX = (float)(DrawPoint.X + (newWidth - DrawPoint.X) / 2);

                //Config Area position for better Draw design
                if (tempWidth + centerX + width * 2 / 100 >= newWidth)
                {
                    centerX -= (float)tempWidth + centerX + (float)(width * 2 / 100) - (float)newWidth;
                }
                else
                {
                    if ((tempWidth + height * 15 / 100 >= textSize.Width) &&
                        (tempWidth + height * 15 / 100 + (newWidth - (float)width * 2 / 100) - textSize.Width) <= newWidth)
                    {
                        centerX = (float)((newWidth - (float)width * 2 / 100) - textSize.Width);
                    }
                }

                if (CurrentWeatherInfo.Temp != null)
                {
                    ds.DrawText(CurrentWeatherInfo.Temp, centerX, (float)(DrawPoint.Y + height * 4.5 / 100), Colors.White,
                                new CanvasTextFormat
                    {
                        FontSize            = (float)(height * 7.5 / 100),
                        HorizontalAlignment = CanvasHorizontalAlignment.Left,
                        FontFamily          = "Segoe UI Light",
                        FontWeight          = FontWeights.Thin
                    });
                }

                if (CurrentWeatherInfo.MainCondition != null)
                {
                    ds.DrawText(CurrentWeatherInfo.MainCondition, centerX, (float)(DrawPoint.Y + height * 13 / 100),
                                Colors.White,
                                new CanvasTextFormat
                    {
                        FontSize            = (float)(height * 4.5 / 100),
                        HorizontalAlignment = CanvasHorizontalAlignment.Left,
                        FontFamily          = "Segoe UI Light",
                        FontWeight          = FontWeights.Thin
                    });
                }

                if (CurrentWeatherInfo.Condition != null)
                {
                    if (isBackground)
                    {
                        var weatherIcon = PickWeatherIcon(CurrentWeatherInfo.Condition);
                        var iconSizeStr = weatherIcon.Substring(0, 3);
                        var iconSize    = int.Parse(iconSizeStr);
                        var iconBitmap  = new CanvasRenderTarget(device, iconSize, iconSize, 500);
                        using (var ds1 = iconBitmap.CreateDrawingSession())
                        {
                            var file =
                                await
                                StorageFile.GetFileFromApplicationUriAsync(
                                    new Uri("ms-appx:///ShareClass/Assets/WeatherIcon/" + weatherIcon + ".svg"));

                            using (var stream = await file.OpenStreamForReadAsync())
                                using (var reader = new StreamReader(stream))
                                {
                                    var xml = new XmlDocument();
                                    xml.LoadXml(reader.ReadToEnd(), new XmlLoadSettings {
                                        ProhibitDtd = false
                                    });

                                    var svgDocument = SvgDocument.Parse(xml);


                                    using (var renderer = new Win2dRenderer(iconBitmap, svgDocument))
                                        renderer.Render(iconSize, iconSize, ds1);

                                    ds.DrawImage(iconBitmap, new Rect(centerX - height * 15 / 100,
                                                                      DrawPoint.Y + height * 5.5 / 100, height * 13 / 100, height * 13 / 100));
                                }
                        }
                    }
                    else
                    {
                        if (!StartVm.IsIconSaved)
                        {
                            var weatherIcon = PickWeatherIcon(CurrentWeatherInfo.Condition);
                            var iconSizeStr = weatherIcon.Substring(0, 3);
                            var iconSize    = int.Parse(iconSizeStr);
                            var iconBitmap  = new CanvasRenderTarget(device, iconSize, iconSize, 500);
                            using (var ds1 = iconBitmap.CreateDrawingSession())
                            {
                                var file =
                                    await
                                    StorageFile.GetFileFromApplicationUriAsync(
                                        new Uri("ms-appx:///ShareClass/Assets/WeatherIcon/" + weatherIcon + ".svg"));

                                using (var stream = await file.OpenStreamForReadAsync())
                                    using (var reader = new StreamReader(stream))
                                    {
                                        var xml = new XmlDocument();
                                        xml.LoadXml(reader.ReadToEnd(), new XmlLoadSettings {
                                            ProhibitDtd = false
                                        });

                                        var svgDocument = SvgDocument.Parse(xml);


                                        using (var renderer = new Win2dRenderer(iconBitmap, svgDocument))
                                            renderer.Render(iconSize, iconSize, ds1);

                                        StartVm.IconBitmap  = new CanvasRenderTarget(device, iconSize, iconSize, 500);
                                        StartVm.IconBitmap  = iconBitmap;
                                        StartVm.IsIconSaved = true;

                                        ds.DrawImage(iconBitmap, new Rect(centerX - height * 15 / 100,
                                                                          DrawPoint.Y + height * 5.5 / 100, height * 13 / 100, height * 13 / 100));
                                    }
                            }
                        }
                        else
                        {
                            ds.DrawImage(StartVm.IconBitmap,
                                         new Rect(centerX - height * 15 / 100, DrawPoint.Y + height * 5.5 / 100, height * 13 / 100, height * 13 / 100));
                        }
                    }
                }


                if (oldDrawPoint >= screenSize.Height)
                {
                    var temp = DrawPoint.Y - height * 1.5 / 100;
                    if (temp + screenSize.Height <= screenSize.Height * 2)
                    {
                        return(new Point(drawPoint.X, temp + screenSize.Height));
                    }
                    return(new Point(-1, -1));
                }
                else
                {
                    var temp = DrawPoint.Y - height * 2 / 100 + height * 22 / 100;
                    if (temp <= screenSize.Height)
                    {
                        return(new Point(drawPoint.X, temp));
                    }
                    return(new Point(-1, -1));
                }
            }

            #endregion
        }