/// <summary>
    /// RenderCallback for the style "Image".
    /// </summary>
    private void RenderImageControl(UIControl control, UIRenderContext context)
    {
      // Background images.
      RenderImages(control, context, false);

      // Render image.
      var image = control as Image;
      if (image != null && image.Texture != null)
      {
        Color foreground = GetForeground(control, GetState(context), context.Opacity);

        context.RenderTransform.Draw(SpriteBatch, image.Texture, GetContentBoundsRounded(image), image.SourceRectangle, foreground);
#else
        context.RenderTransform.Draw(
          SpriteBatch, 
          image.Texture, 
          GetContentBoundsRounded(image), 
          image.SourceRectangle != Rectangle.Empty ? image.SourceRectangle : image.Texture.Bounds,
          foreground);

      }

      // Visual children.
      foreach (var child in control.VisualChildren)
        child.Render(context);

      // Overlay images.
      RenderImages(control, context, true);
    }
Beispiel #2
0
 public virtual void Prerender(UIRenderContext context)
 {
     foreach (var child in Children)
     {
         child.Prerender(context.SubContext(child.Position));
     }
 }
 private static void SetState(UIRenderContext context, ThemeState state)
 {
   if (state != null)
     context.Data[StateId] = state;
   else
     context.Data.Remove(StateId);
 }
Beispiel #4
0
        public override void Render(UIRenderContext context)
        {
            //
            var spriteBatch = context.SpriteBatch;

            var screenPosition = context.RenderPort;

            switch (Background)
            {
            case null:
                break;

            case ColourBackgroundStyling colourBackground:
                var colour = !string.IsNullOrEmpty(colourBackground.ColourResource)
                        ? context.Colours.Get(colourBackground.ColourResource)
                        : colourBackground.Colour;
                colour = colour ?? colourBackground.Colour ?? Color.Purple;
                spriteBatch.Begin(transformMatrix: context.Transform);
                spriteBatch.Draw(
                    context.RenderPrimitives.WhiteTexture,
                    screenPosition,
                    null,
                    colour.Value,
                    0,
                    Vector2.Zero,
                    SpriteEffects.None,
                    0f
                    );
                spriteBatch.End();
                break;

            case ImageBackgroundStyling imageBackground:
                var texture = !string.IsNullOrEmpty(imageBackground.ImageResource)
                        ? context.ContentManager.Load <Texture2D>(imageBackground.ImageResource)
                        : imageBackground.Image;
                if (texture != null)
                {
                    spriteBatch.Begin(transformMatrix: context.Transform);
                    spriteBatch.Draw(
                        texture,
                        screenPosition,
                        null,
                        imageBackground.Colour,
                        0,
                        Vector2.Zero,
                        SpriteEffects.None,
                        0f
                        );
                    spriteBatch.End();
                }
                break;
            }

            // Render children.
            foreach (var child in Children)
            {
                child.Render(context.SubContext(child.Position));
            }
        }
 public void OnLayout(UIRenderContext rctx, ref UIElementLayout elementLayout)
 {
     m_fill.ResolveTextures(rctx);
     m_fill.OnLayout(rctx, elementLayout.x0, elementLayout.y0, elementLayout.x1, elementLayout.y1);
     if (m_element.Knob != null)
     {
         m_knobTexture = rctx.TextureManager.ResolveTexture(m_element.Knob, rctx.LayoutScale, 0, 0, 1, 1);
         m_knobWidth = m_element.Knob.SourceWidth;
         m_knobHeight = m_element.Knob.SourceHeight;
     }
 }
 public void Render(UIRenderContext rctx, ref UIElementLayout layout)
 {
     UIRenderer.RColor rc, old;
     old = UIRenderer.GetColor();
     rc.r = (float)m_element.color.r / 255.0f;
     rc.g = (float)m_element.color.g / 255.0f;
     rc.b = (float)m_element.color.b / 255.0f;
     rc.a = (float)m_element.color.a / 255.0f;
     UIRenderer.SetColor(rc);
     m_font.Render(rctx, m_x0, m_y0, m_fmted);
     UIRenderer.SetColor(old);
 }
    /// <summary>
    /// RenderCallback for the style "UIControl".
    /// </summary>
    private void RenderUIControl(UIControl control, UIRenderContext context)
    {
      // Background images.
      RenderImages(control, context, false);

      // Visual children.
      foreach (var child in control.VisualChildren)
        child.Render(context);

      // Overlay images.
      RenderImages(control, context, true);
    }
        public void Render(UIRenderContext rctx, ref UIElementLayout layout)
        {
            //UIRenderer.Rect(x0, y0, x1, y1);
            if (m_texture != null)
            {
                UIRenderer.DrawTexture(m_texture, layout.x0, layout.y0, layout.x1, layout.y1);
            }
            else
            {

            }
        }
    private void RenderConsole(UIControl control, UIRenderContext context)
    {
      // Background images.
      RenderImages(control, context, false);

      // Render console text and caret.
      var console = control as Controls.Console;
      if (console != null)
      {
        RectangleF contentBounds = GetContentBoundsRounded(console);

        // Draw all visual lines. A fixed-width font is assumed and the text was already prepared -
        // no clipping necessary.
        var font = GetFont(console.Font);
        Color foreground = GetForeground(control, GetState(context), context.Opacity);
        for (int i = 0; i < console.VisualLines.Count; i++)
        {
          if (console.VisualLines[i] != null)
            context.RenderTransform.DrawString(
              SpriteBatch,
              font,
              console.VisualLines[i],
              new Vector2F(contentBounds.X, contentBounds.Y + i * font.LineSpacing),
              foreground);
        }

        // Draw blinking caret rectangle.
        if (console.VisualCaretX >= 0
            && console.VisualCaretY >= 0
            && console.IsFocused
            && IsCaretVisible(new Vector2F(console.VisualCaretX, console.VisualCaretY)))
        {
          float charWidth = font.MeasureString("A").X;
          Vector4 padding = console.Padding;
          RectangleF caretRectangle = new RectangleF(
            console.ActualX + padding.X + console.VisualCaretX * charWidth - 1,  // minus 1 pixel
            console.ActualY + padding.Y + console.VisualCaretY * font.LineSpacing - 1,
            1,
            font.LineSpacing + 2);

          context.RenderTransform.Draw(SpriteBatch, WhiteTexture, caretRectangle, null, foreground);
        }
      }

      // Visual children.
      foreach (var child in control.VisualChildren)
        child.Render(context);

      // Overlay images.
      RenderImages(control, context, true);
    }
Beispiel #10
0
        protected override void OnRender(UIRenderContext context)
        {
            _sampleTime += context.DeltaTime;
            _numberOfFrames++;

            if (_sampleTime > SampleInterval)
            {
                Text            = string.Format("FPS: {0}", (int)(_numberOfFrames / (float)_sampleTime.TotalSeconds + 0.5f));
                _sampleTime     = TimeSpan.Zero;
                _numberOfFrames = 0;
            }

            base.OnRender(context);
        }
Beispiel #11
0
        public override void Render(UIRenderContext context)
        {
            var screenPosition = context.RenderPort;

            if (context.RenderPort.Width == 0 ||
                context.RenderPort.Height == 0 ||
                _renderTarget == null)
            {
                return;
            }

            context.SpriteBatch.Begin();
            context.SpriteBatch.Draw(_renderTarget, screenPosition, Color.White);
            context.SpriteBatch.End();
        }
    /// <summary>
    /// RenderCallback for the style "TextBlock".
    /// </summary>
    private void RenderTextBlock(UIControl control, UIRenderContext context)
    {
      // Background images.
      RenderImages(control, context, false);

      // Visual children.
      foreach (var child in control.VisualChildren)
        child.Render(context);

      // Render text.
      var textBlock = control as TextBlock;
      if (textBlock != null && textBlock.VisualText.Length > 0)
      {
        RectangleF contentBounds = GetContentBoundsRounded(textBlock);
        Rectangle originalScissorRectangle = GraphicsDevice.ScissorRectangle;
        if (textBlock.VisualClip)
        {
          // If clipping is enabled - set scissors rectangle.
          EndBatch();

          Rectangle scissorRectangle = context.RenderTransform.Transform(contentBounds).ToRectangle(true);

          GraphicsDevice.ScissorRectangle = Rectangle.Intersect(scissorRectangle, originalScissorRectangle);
#else
          GraphicsDevice.ScissorRectangle = RectangleIntersect(scissorRectangle, originalScissorRectangle);


          BeginBatch();
        }

        // Render text.
        Vector2F position = new Vector2F(contentBounds.X, contentBounds.Y);
        Color foreground = GetForeground(control, GetState(context), context.Opacity);
        context.RenderTransform.DrawString(SpriteBatch, GetFont(textBlock.Font), textBlock.VisualText, position, foreground);

        if (textBlock.VisualClip)
        {
          // If clipping is enabled - remove scissors rectangle.
          EndBatch();
          GraphicsDevice.ScissorRectangle = originalScissorRectangle;
          BeginBatch();
        }
      }

      // Overlay images.
      RenderImages(control, context, true);
    }
    /// <inheritdoc/>
    public void Render(UIControl control, UIRenderContext context)
    {
      if (context == null)
        throw new ArgumentNullException("context");
      if (control == null)
        return;
      if (string.IsNullOrEmpty(control.Style))
        return;

      // Find style and visual state.
      ThemeStyle style;
      Theme.Styles.TryGet(control.Style, out style);
      var state = GetState(control, style);

      // Determine effective opacity.
      float opacity = GetOpacity(control, context, state);
      if (Numeric.IsZero(opacity))
        return;

      // Store current visual state in render context.
      var originalState = GetState(context);
      SetState(context, state);

      // Store absolute opacity and render transformation in render context.
      float originalOpacity = context.Opacity;
      context.Opacity = opacity;
      var originalRenderTransform = context.RenderTransform;
      if (control.HasRenderTransform)
        context.RenderTransform *= control.RenderTransform;

      // Make sure that the sprite batch is active. BeginBatch() safely handles redundant calls.
      BeginBatch();

      // Clear background if necessary.
      Color background = GetBackground(control, state, context.Opacity);
      if (background.A != 0)
        context.RenderTransform.Draw(SpriteBatch, WhiteTexture, GetActualBoundsRounded(control), null, background);

      // The rest is done by the render callback.
      Action<UIControl, UIRenderContext> callback = GetRenderCallback(control, style);
      callback(control, context);

      // Restore original render context.
      SetState(context, originalState);
      context.Opacity = originalOpacity;
      context.RenderTransform = originalRenderTransform;
    }
    /// <summary>
    /// RenderCallback for the style "ProgressBar".
    /// </summary>
    private void RenderProgressBar(UIControl control, UIRenderContext context)
    {
      // See comments of RenderSlider above.

      ThemeImage indicatorImage = null;
      ThemeState state = GetState(context);
      if (state != null)
      {
        foreach (var image in state.Images)
        {
          if (image.Name == "Indicator")
            indicatorImage = image;
          else if (!image.IsOverlay)
            RenderImage(GetActualBoundsRounded(control), image, context.Opacity, context.RenderTransform);
        }
      }

      ProgressBar bar = control as ProgressBar;
      if (indicatorImage != null && bar != null)
      {
        RectangleF indicatorBounds = GetContentBoundsRounded(bar);

        // Render indicator.
        if (!bar.IsIndeterminate)
        {
          indicatorBounds.Width = (int)((bar.Value - bar.Minimum) / (bar.Maximum - bar.Minimum) * indicatorBounds.Width);
        }
        else
        {
          // In indeterminate mode the indicator is 1/4 wide and moves left and right.
          float width = indicatorBounds.Width / 4.0f;
          float range = width * 3;
          float center = indicatorBounds.X + width / 2 + (bar.Value - bar.Minimum) / (bar.Maximum - bar.Minimum) * range;
          indicatorBounds.X = (int)(center - width / 2);
          indicatorBounds.Width = (int)(width);
        }

        if (indicatorBounds.Width > 0 && indicatorBounds.Height > 0)
          RenderImage(indicatorBounds, indicatorImage, context.Opacity, context.RenderTransform);
      }

      foreach (var child in control.VisualChildren)
        child.Render(context);

      RenderImages(control, context, true);
    }
Beispiel #15
0
        public void RenderTreeViewItem(UIControl control, UIRenderContext context)
        {
            var treeViewItem = control as TreeViewItem;

            if (treeViewItem != null && treeViewItem.IsSelected && treeViewItem.Header != null)
            {
                // Draw a blue rectangle behind selected tree view items.
                context.RenderTransform.Draw(
                    SpriteBatch,
                    WhiteTexture,
                    treeViewItem.Header.ActualBounds,
                    null,
                    Color.CornflowerBlue);
            }

            // Call the default render callback to draw all the rest.
            RenderCallbacks["UIControl"](control, context);
        }
        public void OnLayout(UIRenderContext rctx, ref UIElementLayout elementLayout)
        {
            if (m_f0 != null)
            {
                m_f0.ResolveTextures(rctx);
                m_f1.ResolveTextures(rctx);
                m_f2.ResolveTextures(rctx);
            }

            if (m_font != null)
                m_formattedText = m_font.FormatText(rctx, m_element.Text, m_element.Style.FontStyle.PixelSize);
            else
                m_formattedText = null;

            if (m_element.TextureNormal != null)
                m_normal = rctx.TextureManager.ResolveTexture(m_element.TextureNormal, rctx.LayoutScale, 0, 0, 1, 1);

            if (m_element.TexturePressed != null)
                m_pressed = rctx.TextureManager.ResolveTexture(m_element.TexturePressed, rctx.LayoutScale, 0, 0, 1, 1);
        }
    /// <summary>
    /// RenderCallback for the style "ContentControl".
    /// </summary>
    private void RenderContentControl(UIControl control, UIRenderContext context)
    {
      var contentControl = control as ContentControl;
      if (contentControl == null || contentControl.Content == null || !contentControl.ClipContent)
      {
        // No content or no clipping - render as normal "UIControl".
        RenderUIControl(control, context);
        return;
      }

      // Background images.
      RenderImages(control, context, false);

      EndBatch();

      // Render Content and clip with scissor rectangle.
      Rectangle originalScissorRectangle = GraphicsDevice.ScissorRectangle;
      Rectangle scissorRectangle = context.RenderTransform.Transform(contentControl.ContentBounds).ToRectangle(true);

      GraphicsDevice.ScissorRectangle = Rectangle.Intersect(scissorRectangle, originalScissorRectangle);
#else
      GraphicsDevice.ScissorRectangle = RectangleIntersect(scissorRectangle, originalScissorRectangle);


      BeginBatch();
      contentControl.Content.Render(context);
      EndBatch();

      GraphicsDevice.ScissorRectangle = originalScissorRectangle;

      BeginBatch();

      // Visual children except Content.
      foreach (var child in control.VisualChildren)
        if (contentControl.Content != child)
          child.Render(context);

      // Overlay images.
      RenderImages(control, context, true);
    }
        public void Render(UIRenderContext rctx, ref UIElementLayout layout)
        {
            if (UIElementLogic.Button(rctx.InputManager, this, layout.x0, layout.y0, layout.x1, layout.y1, m_touchInteraction))
            {
                // clicked.
                if (m_element.Event.Length > 0)
                    rctx.EventHandler.OnEvent(m_element.Event);
            }

            // behold the prettiness.
            switch (UIElementLogic.GetButtonVisualState(rctx.InputManager, this, m_touchInteraction))
            {
                case UIElementLogic.ButtonVisualStates.NORMAL:
                    UIRenderer.SetColor(new UIRenderer.RColor(1,1,1,1));
                    if (m_f0 != null)
                        m_f0.Draw(rctx, layout.x0, layout.y0, layout.x1, layout.y1);
                    if (m_normal != null)
                        UIRenderer.DrawTexture(m_normal, layout.x0, layout.y0, layout.x1, layout.y1);
                    break;
                case UIElementLogic.ButtonVisualStates.MOUSEOVER:
                    UIRenderer.SetColor(new UIRenderer.RColor(1,1,1,1));
                    if (m_f1 != null)
                        m_f1.Draw(rctx, layout.x0, layout.y0, layout.x1, layout.y1);
                    if (m_normal != null)
                        UIRenderer.DrawTexture(m_normal, layout.x0, layout.y0, layout.x1, layout.y1);
                    break;
                case UIElementLogic.ButtonVisualStates.PRESSED:
                    UIRenderer.SetColor(new UIRenderer.RColor(1,1,1,1));
                    if (m_f2 != null)
                        m_f2.Draw(rctx, layout.x0, layout.y0, layout.x1, layout.y1);
                    if (m_pressed != null)
                        UIRenderer.DrawTexture(m_pressed, layout.x0, layout.y0, layout.x1, layout.y1);
                    break;
            }

            UIRenderer.SetColor(new UIRenderer.RColor(1,1,1,1));

            if (m_font != null && m_formattedText != null)
                m_font.Render(rctx, (layout.x0 + layout.x1 - m_formattedText.x1) / 2, (layout.y0 + layout.y1 - (m_formattedText.y1 - m_formattedText.y0)) / 2 - m_formattedText.y0, m_formattedText);
        }
Beispiel #19
0
        public override void Render(UIRenderContext context)
        {
            var screenPosition = context.RenderPort;


            var font = context.Fonts.Get(Font ?? "Default");

            if (font == null)
            {
                font = context.Fonts.Get("Default");
            }
            var size = font.MeasureString(Value);

            context.SpriteBatch.Begin(transformMatrix: context.Transform);

            var pos = screenPosition.Location.ToVector2();

            switch (TextAlign)
            {
            case Positioning.Center:
                pos -= new Vector2(size.X / 2f, 0);
                break;

            case Positioning.Left:
                break;

            case Positioning.Right:
                pos -= new Vector2(size.X, 0);
                break;
            }

            if (VerticalCenter)
            {
                pos -= new Vector2(0, size.Y / 2);
            }

            context.SpriteBatch.DrawString(font, Value, pos, Colour ?? Color.White);
            context.SpriteBatch.End();
        }
    /// <summary>
    /// RenderCallback for the style "Slider".
    /// </summary>
    private void RenderSlider(UIControl control, UIRenderContext context)
    {
      // Special: An image with the name "Indicator" is drawn from the left up to the slider
      // position.

      ThemeImage indicatorImage = null;
      ThemeState state = GetState(context);
      if (state != null)
      {
        // Background images - except the image called "Indicator".
        foreach (var image in state.Images)
        {
          if (image.Name == "Indicator")
            indicatorImage = image;
          else if (!image.IsOverlay)
            RenderImage(GetActualBoundsRounded(control), image, context.Opacity, context.RenderTransform);
        }
      }

      // Render indicator image.
      Slider slider = control as Slider;
      if (indicatorImage != null && slider != null)
      {
        RectangleF indicatorBounds = GetContentBoundsRounded(slider);

        // Size of indicator image depends on the slider value.
        indicatorBounds.Width = (int)((slider.Value - slider.Minimum)/ (slider.Maximum - slider.Minimum) * indicatorBounds.Width);
        if (indicatorBounds.Width > 0 && indicatorBounds.Height > 0)
          RenderImage(indicatorBounds, indicatorImage, context.Opacity, context.RenderTransform);
      }

      // Visual children.
      foreach (var child in control.VisualChildren)
        child.Render(context);

      // Overlay images.
      RenderImages(control, context, true);
    }
    /// <summary>
    /// Renders the <see cref="ThemeImage"/>s of the current visual state of the given context.
    /// </summary>
    /// <param name="control">The control.</param>
    /// <param name="context">The render context.</param>
    /// <param name="drawOverlays">
    /// If set to <see langword="true"/> only overlay images are rendered; otherwise, only
    /// background images are rendered. See <see cref="ThemeImage.IsOverlay"/>.
    /// </param>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="context"/> is <see langword="null"/>.
    /// </exception>
    public void RenderImages(UIControl control, UIRenderContext context, bool drawOverlays)
    {
      if (context == null)
        throw new ArgumentNullException("context");

      var state = GetState(context);
      if (state == null)
        return;

      int numberOfImages = state.Images.Count;
      if (numberOfImages == 0)
        return;

      RectangleF bounds = GetActualBoundsRounded(control);
      for (int i = 0; i < numberOfImages; i++)
      {
        var image = state.Images[i];
        if (image.IsOverlay != drawOverlays)
          continue;

        RenderImage(bounds, image, context.Opacity, context.RenderTransform);
      }
    }
Beispiel #22
0
        public void OnLayout(UIRenderContext rctx, ref UIElementLayout elementLayout)
        {
            float wrapLength = 0;
            if (m_element.WordWrap)
                wrapLength = elementLayout.x1 - elementLayout.x0;

            m_fmted = m_font.FormatText(rctx, m_element.Text, m_element.pixelSize, wrapLength);
            if (m_fmted == null)
                return;

            switch (m_element.HorizontalAlignment)
            {
                case outki.UIHorizontalAlignment.UIHorizontalAlignment_Center:
                    m_x0 = (elementLayout.x0 + elementLayout.x1 - (m_fmted.x1 - m_fmted.x0)) / 2 - m_fmted.x0;
                    break;
                case outki.UIHorizontalAlignment.UIHorizontalAlignment_Right:
                    m_x0 = elementLayout.x1 - m_fmted.x1;
                    break;
                default: // left align
                    m_x0 = elementLayout.x0 - m_fmted.x0;
                    break;
            }

            switch (m_element.VerticalAlignment)
            {
                case outki.UIVerticalAlignment.UIVerticalAlignment_Top:
                    m_y0 = elementLayout.y0 - m_fmted.facey0;
                    break;
                case outki.UIVerticalAlignment.UIVerticalAlignment_Bottom:
                    m_y0 = elementLayout.y1 - m_fmted.facey1;
                    break;
                default: // center
                    // glyph actual sizes method
                    m_y0 = (elementLayout.y0 + elementLayout.y1 - (m_fmted.y1 - m_fmted.y0)) / 2 - m_fmted.y0;
                    break;
            }
        }
Beispiel #23
0
        public override void Prerender(UIRenderContext context)
        {
            var screenPosition = context.RenderPort;

            if (View == null)
            {
                View = Matrix.CreateLookAt(new Vector3(1f, 1f, 1f), Vector3.Zero, Vector3.Up);
            }
            if (Projection == null)
            {
                Projection = Matrix.CreateOrthographicOffCenter(-1, 1, -1, 1, 0.001f, 10000f);
                //Matrix.CreatePerspective(context.GraphicsDevice.Viewport.Width, context.GraphicsDevice.Viewport.Height, 0.001f, 10000f);
            }
            if (_renderTarget == null ||
                _renderTarget.Height != screenPosition.Height ||
                _renderTarget.Width != screenPosition.Width)
            {
                _rendered     = false;
                _renderTarget = new RenderTarget2D(context.GraphicsDevice, screenPosition.Width, screenPosition.Height, false, SurfaceFormat.Vector4, DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents);
            }
            if (!SingleRender || !_rendered)
            {
                context.GraphicsDevice.SetRenderTarget(_renderTarget);
                context.GraphicsDevice.Clear(Colour);

                var renderContext = new RenderContext(context.GameTime, context.SpriteBatch, context.GraphicsDevice)
                {
                    Projection = Projection,
                    View       = View,
                };
                Renderable?.Render(renderContext);
                RenderFunc?.Invoke(renderContext);
                context.GraphicsDevice.SetRenderTarget(null);
                _rendered = true;
            }
        }
Beispiel #24
0
 public void Render(UIRenderContext rctx, ref UIElementLayout layout)
 {
     m_fill.Draw(rctx, layout.x0, layout.y0, layout.x1, layout.y1);
 }
Beispiel #25
0
 public void OnLayout(UIRenderContext rctx, ref UIElementLayout elementLayout)
 {
     m_fill.ResolveTextures(rctx);
     m_fill.OnLayout(rctx, elementLayout.x0, elementLayout.y0, elementLayout.x1, elementLayout.y1);
 }
 public void Render(UIRenderContext rctx, ref UIElementLayout elementLayout)
 {
 }
 private static ThemeState GetState(UIRenderContext context)
 {
   object state;
   context.Data.TryGetValue(StateId, out state);
   return state as ThemeState;
 }
Beispiel #28
0
 public abstract void Render(UIRenderContext context);
 /// <summary>
 /// Gets the effective opacity (the product of the opacities of all visual ancestors).
 /// </summary>
 private static float GetOpacity(UIControl control, UIRenderContext context, ThemeState state)
 {
   float opacity = (state != null && state.Opacity.HasValue) ? state.Opacity.Value : control.Opacity;
   return opacity * context.Opacity;
 }
        public void Render(UIRenderContext rctx, ref UIElementLayout layout)
        {
            m_fill.Draw(rctx, layout.x0, layout.y0, layout.x1, layout.y1);
            if (m_knobTexture == null)
                return;

            // Current knob
            float h_width = 0.5f * rctx.LayoutScale * m_knobWidth;
            float h_height = 0.5f * rctx.LayoutScale * m_knobHeight;
            float x = layout.x0 + m_value * (layout.x1 - layout.x0);
            float y = layout.y0 + m_value * (layout.y1 - layout.y0);
            float x0 = x - h_width;
            float y0 = y - h_height;
            float x1 = x + h_width;
            float y1 = y + h_height;
            UIRenderer.DrawTexture(m_knobTexture, x0, y0, x1, y1);

            float displayVal = m_value;

            UIInputManager im = rctx.InputManager;
            float dragX = 0, dragY = 0;

            if (!m_dragMouse)
            {
                bool pr = (m_ti.PressedByTouchId != -1);
                im.TouchHitTest(x - 0.70f * h_width, y - 0.70f * h_height, x + 0.70f * h_width, y + 0.70f * h_height, ref m_ti);
                if (!pr && m_ti.PressedByTouchId != -1)
                {
                    // touched.
                    m_dragTouch = true;
                    m_preDragValue = m_value;
                }
                else if (m_ti.PressedByTouchId == -1)
                {
                    m_dragTouch = false;
                }
                if (m_dragTouch)
                {
                    dragX = m_ti.CurrentLocation.x - m_ti.PressedLocation.x;
                    dragY = m_ti.CurrentLocation.y - m_ti.PressedLocation.y;
                }
            }

            /*
            if (!m_dragTouch)
            {
                if (!im.m_state.MouseDown)
                {
                    if (im.MouseHitTest(x0, y0, x1, y1))
                        im.m_mouseInteraction.ObjectOver = this;
                    else if (im.m_mouseInteraction.ObjectOver == this)
                        im.m_mouseInteraction.ObjectOver = null;
                }

                if (im.m_mouseInteraction.ObjectOver == this)
                {
                    if (im.m_state.MouseDown)
                    {
                        im.m_mouseInteraction.ObjectPressed = this;
                        m_dragStartX = im.m_state.MouseX;
                        m_dragStartY = im.m_state.MouseY;
                        m_preDragValue = m_value;
                        m_dragMouse = true;
                    }
                }

                if (im.m_mouseInteraction.ObjectPressed == this)
                {
                    dragX = im.m_state.MouseX - m_dragStartX;
                    dragY = im.m_state.MouseY - m_dragStartY;
                    im.m_mouseInteraction.ObjectOver = im.MouseHitTest(x0, y0, x1, y1) ? this : null;
                    if (!im.m_state.MouseDown)
                    {
                        im.m_mouseInteraction.ObjectPressed = null;
                        m_dragMouse = false;
                    }
                }
            }
            */

            if (m_dragMouse || m_dragTouch)
            {
                float w = (layout.x1-layout.x0);
                float h = (layout.y1-layout.y0);
                float deltaDot = dragX * w + dragY * h;
                float delta = deltaDot / (w*w + h*h);
                m_value = m_preDragValue + delta;
                if (m_value < 0.0f)
                    m_value = 0.0f;
                else if (m_value > 1.0f)
                    m_value = 1.0f;
            }
        }
    /// <summary>
    /// RenderCallback for the style "TextBox".
    /// </summary>
    private void RenderTextBox(UIControl control, UIRenderContext context)
    {
      // Textbox content is always clipped using the scissors rectangle.
      // TODO: TextBox should determine if clipping is necessary and set a VisualClip flag.

      // Background images.
      RenderImages(control, context, false);

      var textBox = control as TextBox;
      if (textBox != null)
      {
        RectangleF contentBounds = GetContentBoundsRounded(textBox);
        Rectangle originalScissorRectangle = GraphicsDevice.ScissorRectangle;

        EndBatch();
        Rectangle scissorRectangle = context.RenderTransform.Transform(textBox.VisualClip).ToRectangle(true);

        GraphicsDevice.ScissorRectangle = Rectangle.Intersect(scissorRectangle, originalScissorRectangle);
#else
        GraphicsDevice.ScissorRectangle = RectangleIntersect(scissorRectangle, originalScissorRectangle);

        BeginBatch();

        bool hasSelection = (textBox.VisualSelectionBounds.Count > 0);
        bool hasFocus = textBox.IsFocused;
        if (hasSelection && hasFocus)
        {
          // Render selection.
          Color selectionColor = textBox.SelectionColor;

          selectionColor = Color.FromNonPremultiplied(selectionColor.ToVector4() * new Vector4(1, 1, 1, context.Opacity));
#else
          selectionColor = ColorFromNonPremultiplied(selectionColor.ToVector4() * new Vector4(1, 1, 1, context.Opacity));

          foreach (RectangleF selection in textBox.VisualSelectionBounds)
          {
            // The selection rectangle of an empty line has zero width.
            // Show a small rectangle to indicate that the selection covers the line. 
            RectangleF rectangle = selection;
            rectangle.Width = Math.Max(rectangle.Width, 4);

            // Draw rectangle using TextBox.SelectionColor.
            context.RenderTransform.Draw(SpriteBatch, WhiteTexture, rectangle, null, selectionColor);
          }
        }

        // Render text.
        Vector2F position = new Vector2F(contentBounds.X, contentBounds.Y);
        if (!textBox.IsMultiline)
          position.X -= textBox.VisualOffset;
        else
          position.Y -= textBox.VisualOffset;

        var font = GetFont(textBox.Font);
        Color foreground = GetForeground(control, GetState(context), context.Opacity);
        context.RenderTransform.DrawString(SpriteBatch, font, textBox.VisualText, position, foreground);

        if (!hasSelection
            && hasFocus
            && !textBox.VisualCaret.IsNaN
            && IsCaretVisible(textBox.VisualCaret))
        {
          // Render caret.
          RectangleF caret = new RectangleF(textBox.VisualCaret.X, textBox.VisualCaret.Y, 2, font.LineSpacing);
          context.RenderTransform.Draw(SpriteBatch, WhiteTexture, caret, null, foreground);
        }

        EndBatch();
        GraphicsDevice.ScissorRectangle = originalScissorRectangle;
        BeginBatch();
      }

      // Visual children.
      foreach (var child in control.VisualChildren)
        child.Render(context);

      // Overlay images.
      RenderImages(control, context, true);
    }
 public void OnLayout(UIRenderContext rctx, ref UIElementLayout elementLayout)
 {
     if (m_element.texture != null)
         m_texture = rctx.TextureManager.ResolveTexture(m_element.texture, rctx.LayoutScale, 0, 0, 1, 1);
 }