示例#1
0
        /// <summary>
        /// Draw the icon
        /// </summary>
        protected virtual void Render(DrawArgs drawArgs, Icon icon, Vector3 projectedPoint)
        {
            if (!icon.isInitialized)
            {
                icon.Initialize(drawArgs);
            }

            if (!drawArgs.WorldCamera.ViewFrustum.ContainsPoint(icon.Position))
            {
                return;
            }

            // Check icons for within "visual" range
            double distanceToIcon = Vector3.Length(icon.Position - drawArgs.WorldCamera.Position);

            if (distanceToIcon > icon.MaximumDisplayDistance)
            {
                return;
            }
            if (distanceToIcon < icon.MinimumDisplayDistance)
            {
                return;
            }

            IconTexture iconTexture = GetTexture(icon);
            bool        isMouseOver = icon == mouseOverIcon;

            if (isMouseOver)
            {
                // Mouse is over
                isMouseOver = true;

                if (icon.isSelectable)
                {
                    DrawArgs.MouseCursor = CursorType.Hand;
                }

                string description = icon.Description;
                if (description == null)
                {
                    description = icon.ClickableActionURL;
                }
                if (description != null)
                {
                    // Render description field
                    DrawTextFormat format = DrawTextFormat.NoClip | DrawTextFormat.WordBreak | DrawTextFormat.Bottom;
                    int            left   = 10;
                    if (World.Settings.showLayerManager)
                    {
                        left += World.Settings.layerManagerWidth;
                    }
                    Rectangle rect = Rectangle.FromLTRB(left, 10, drawArgs.screenWidth - 10, drawArgs.screenHeight - 10);

                    // Draw outline
                    drawArgs.defaultDrawingFont.DrawText(
                        m_sprite, description,
                        rect,
                        format, 0xb0 << 24);

                    rect.Offset(2, 0);
                    drawArgs.defaultDrawingFont.DrawText(
                        m_sprite, description,
                        rect,
                        format, 0xb0 << 24);

                    rect.Offset(0, 2);
                    drawArgs.defaultDrawingFont.DrawText(
                        m_sprite, description,
                        rect,
                        format, 0xb0 << 24);

                    rect.Offset(-2, 0);
                    drawArgs.defaultDrawingFont.DrawText(
                        m_sprite, description,
                        rect,
                        format, 0xb0 << 24);

                    // Draw description
                    rect.Offset(1, -1);
                    drawArgs.defaultDrawingFont.DrawText(
                        m_sprite, description,
                        rect,
                        format, descriptionColor);
                }
            }

            int color = isMouseOver ? hotColor : normalColor;

            if (iconTexture == null || isMouseOver || icon.NameAlwaysVisible)
            {
                // Render label
                if (icon.Name != null)
                {
                    // Render name field
                    const int labelWidth = 1000;                     // Dummy value needed for centering the text
                    if (iconTexture == null)
                    {
                        // Center over target as we have no bitmap
                        Rectangle rect = new Rectangle(
                            (int)projectedPoint.X - (labelWidth >> 1),
                            (int)(projectedPoint.Y - (drawArgs.defaultDrawingFont.Description.Height >> 1)),
                            labelWidth,
                            drawArgs.screenHeight);

                        drawArgs.defaultDrawingFont.DrawText(m_sprite, icon.Name, rect, DrawTextFormat.Center, color);
                    }
                    else
                    {
                        // Adjust text to make room for icon
                        int spacing = (int)(icon.Width * 0.3f);
                        if (spacing > 10)
                        {
                            spacing = 10;
                        }
                        int offsetForIcon = (icon.Width >> 1) + spacing;

                        Rectangle rect = new Rectangle(
                            (int)projectedPoint.X + offsetForIcon,
                            (int)(projectedPoint.Y - (drawArgs.defaultDrawingFont.Description.Height >> 1)),
                            labelWidth,
                            drawArgs.screenHeight);

                        drawArgs.defaultDrawingFont.DrawText(m_sprite, icon.Name, rect, DrawTextFormat.WordBreak, color);
                    }
                }
            }

            if (iconTexture != null)
            {
                // Render icon
                float xscale = (float)icon.Width / iconTexture.Width;
                float yscale = (float)icon.Height / iconTexture.Height;
                m_sprite.Transform = Matrix.Scaling(xscale, yscale, 0);

                if (icon.IsRotated)
                {
                    m_sprite.Transform *= Matrix.RotationZ((float)icon.Rotation.Radians - (float)drawArgs.WorldCamera.Heading.Radians);
                }

                m_sprite.Transform *= Matrix.Translation(projectedPoint.X, projectedPoint.Y, 0);
                m_sprite.Draw(iconTexture.Texture,
                              new Vector3(iconTexture.Width >> 1, iconTexture.Height >> 1, 0),
                              Vector3.Empty,
                              color);

                // Reset transform to prepare for text rendering later
                m_sprite.Transform = Matrix.Identity;
            }
        }
示例#2
0
        protected override void Render(DrawArgs drawArgs, Icon icon, Vector3 projectedPoint)
        {
            if (!icon.Initialized)
            {
                icon.Initialize(drawArgs);
                return;
            }
            if (!drawArgs.WorldCamera.ViewFrustum.ContainsPoint(icon.Position))
            {
                return;
            }

            // check whether in icon's visual range.
            float distanceToIcon = Vector3.Length(icon.Position - drawArgs.WorldCamera.Position);

            if (distanceToIcon > icon.MaximumDisplayDistance)
            {
                return;
            }
            if (distanceToIcon < icon.MinimumDisplayDistance)
            {
                return;
            }

            IconTexture iconTexture = this.GetTexture(icon);
            // check is mouse over.
            bool isMouseOver = (icon == this.mouseOverIcon);

            if (isMouseOver)
            {
                if (icon.IsSelectable)
                {
                    DrawArgs.MouseCursor = CursorType.Hand;
                }

                // get icon's description.
                string description = icon.Description;
                if (description == null)
                {
                    description = icon.ClickableActionURL;
                }

                if (description != null)
                {
                    // Render the description field.
                    DrawTextFormat descTextFormat = DrawTextFormat.NoClip;
                    descTextFormat |= DrawTextFormat.WordBreak;
                    descTextFormat |= DrawTextFormat.Bottom;

                    int left = 10;
                    //if (World.Settings.ShowLayerManager) {
                    //   left += World.Settings.LayerManagerWidth;
                    //}
                    Rectangle descRectangle = Rectangle.FromLTRB(left, 10, drawArgs.ScreenWidth - 10, drawArgs.ScreenHeight - 10);

                    // Draw outline
                    this.DrawOutline(drawArgs.DefaultDrawingFont, this.m_sprite, description, ref descRectangle, descTextFormat);

                    // Draw description
                    drawArgs.DefaultDrawingFont.DrawText(this.m_sprite, description, descRectangle, descTextFormat, descriptionColor);
                }
            }

            int color = isMouseOver ? hotColor : normalColor;
            // calculate scale
            double scale = (drawArgs.WorldCamera.WorldRadius + icon.Altitude) / (drawArgs.WorldCamera.WorldRadius + distanceToIcon);

            scale *= drawArgs.WorldCamera.TargetDistance / distanceToIcon;
            //
            // render name field.
            if (icon.Name != null)
            {
                Rectangle nameRectangle = drawArgs.DefaultDrawingFont.MeasureString(this.m_sprite, icon.Name, DrawTextFormat.Center, color);
                nameRectangle.X = (int)projectedPoint.X - (nameRectangle.Width >> 1);
                if (iconTexture == null)
                {
                    // by zzm start
                    nameRectangle.Y = (int)projectedPoint.Y - (drawArgs.DefaultDrawingFont.Description.Height >> 1);
                    // by zzm end
                    this.DrawOutline(drawArgs.DefaultDrawingFont, this.m_sprite, icon.Name, ref nameRectangle, DrawTextFormat.Center);
                    drawArgs.DefaultDrawingFont.DrawText(this.m_sprite, icon.Name, nameRectangle, DrawTextFormat.Center, color);
                }
                else
                {
                    // adjust text to make room for icon.
                    int spacing       = 10;
                    int offsetForIcon = (int)(icon.Height * scale) + spacing;
                    nameRectangle.Y = (int)projectedPoint.Y - offsetForIcon - (drawArgs.DefaultDrawingFont.Description.Height >> 1);
                    this.DrawOutline(drawArgs.DefaultDrawingFont, this.m_sprite, icon.Name, ref nameRectangle, DrawTextFormat.Center);
                    drawArgs.DefaultDrawingFont.DrawText(this.m_sprite, icon.Name, nameRectangle, DrawTextFormat.Center, color);
                }
            }

            if (iconTexture != null)
            {
                // render icon
                // get icon's current scale
                float xScale = (float)scale;                  //icon.Width / iconTexture.Width;
                float yScale = (float)scale;                  //icon.Height / iconTexture.Height;
                this.m_sprite.Transform  = Matrix.Scaling(xScale, yScale, 0);
                this.m_sprite.Transform *= Matrix.Translation(projectedPoint.X, projectedPoint.Y, 0);
                this.m_sprite.Draw(iconTexture.Texture, new Vector3(iconTexture.Width, iconTexture.Height, 0), Vector3.Empty, color);
                this.m_sprite.Transform = Matrix.Identity;
            }
        }
示例#3
0
        public override void Initialize(DrawArgs drawArgs)
        {
            if (!isOn)
            {
                return;
            }

            if (m_sprite != null)
            {
                m_sprite.Dispose();
                m_sprite = null;
            }

            m_sprite = new Sprite(drawArgs.device);

            System.TimeSpan smallestRefreshInterval = System.TimeSpan.MaxValue;

            // Load all textures
            foreach (RenderableObject ro in m_children)
            {
                Icon icon = ro as Icon;
                if (icon == null)
                {
                    // Child is not an icon
                    if (ro.IsOn)
                    {
                        ro.Initialize(drawArgs);
                    }
                    continue;
                }

                if (icon.RefreshInterval.TotalMilliseconds != 0 && icon.RefreshInterval != TimeSpan.MaxValue && icon.RefreshInterval < smallestRefreshInterval)
                {
                    smallestRefreshInterval = icon.RefreshInterval;
                }

                // Child is an icon
                icon.Initialize(drawArgs);

                object      key         = null;
                IconTexture iconTexture = null;

                if (icon.TextureFileName != null && icon.TextureFileName.Length > 0)
                {
                    if (icon.TextureFileName.ToLower().StartsWith("http://") && icon.SaveFilePath != null)
                    {
                        //download it
                        try
                        {
                            WorldWind.Net.WebDownload webDownload = new WorldWind.Net.WebDownload(icon.TextureFileName);
                            webDownload.DownloadType = WorldWind.Net.DownloadType.Unspecified;

                            System.IO.FileInfo saveFile = new System.IO.FileInfo(icon.SaveFilePath);
                            if (!saveFile.Directory.Exists)
                            {
                                saveFile.Directory.Create();
                            }

                            webDownload.DownloadFile(saveFile.FullName);
                        }
                        catch {}

                        iconTexture = (IconTexture)m_textures[icon.SaveFilePath];
                        if (iconTexture == null)
                        {
                            key         = icon.SaveFilePath;
                            iconTexture = new IconTexture(drawArgs.device, icon.SaveFilePath);
                        }
                    }
                    else
                    {
                        // Icon image from file
                        iconTexture = (IconTexture)m_textures[icon.TextureFileName];
                        if (iconTexture == null)
                        {
                            key         = icon.TextureFileName;
                            iconTexture = new IconTexture(drawArgs.device, icon.TextureFileName);
                        }
                    }
                }
                else
                {
                    // Icon image from bitmap
                    if (icon.Image != null)
                    {
                        iconTexture = (IconTexture)m_textures[icon.Image];
                        if (iconTexture == null)
                        {
                            // Create new texture from image
                            key         = icon.Image;
                            iconTexture = new IconTexture(drawArgs.device, icon.Image);
                        }
                    }
                }

                if (iconTexture == null)
                {
                    // No texture set
                    continue;
                }

                if (key != null)
                {
                    // New texture, cache it
                    m_textures.Add(key, iconTexture);

                    // Use default dimensions if not set
                    if (icon.Width == 0)
                    {
                        icon.Width = iconTexture.Width;
                    }
                    if (icon.Height == 0)
                    {
                        icon.Height = iconTexture.Height;
                    }
                }
            }

            // Compute mouse over bounding boxes
            foreach (RenderableObject ro in m_children)
            {
                Icon icon = ro as Icon;
                if (icon == null)
                {
                    // Child is not an icon
                    continue;
                }

                if (GetTexture(icon) == null)
                {
                    // Label only
                    icon.SelectionRectangle = drawArgs.defaultDrawingFont.MeasureString(null, icon.Name, DrawTextFormat.None, 0);
                }
                else
                {
                    // Icon only
                    icon.SelectionRectangle = new Rectangle(0, 0, icon.Width, icon.Height);
                }

                // Center the box at (0,0)
                icon.SelectionRectangle.Offset(-icon.SelectionRectangle.Width / 2, -icon.SelectionRectangle.Height / 2);
            }

            if (refreshTimer == null && smallestRefreshInterval != TimeSpan.MaxValue)
            {
                refreshTimer          = new System.Timers.Timer(smallestRefreshInterval.TotalMilliseconds);
                refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(refreshTimer_Elapsed);
                refreshTimer.Start();
            }

            isInitialized = true;
        }