/// <summary> /// Called when we've to draw the image. /// </summary> /// <param name="graphics">The graphics.</param> private void OnDraw(Graphics2D graphics) { if (IsDisposed) { throw new ObjectDisposedException("ImageDataFlowHelper"); } if (this.SyncBuffer == null) { return; } // Change bitmap contents if needed if (this.SyncBufferChanged) { lock (this.SyncBufferLock) { // Recreate bitmap on need if ((this.BitmapSize != this.SyncBufferSize) || (this.Bitmap == null)) { CommonTools.SafeDispose(ref this.Bitmap); this.Bitmap = new WriteableBitmapResource( new Size2(this.SyncBufferSize.Width, this.SyncBufferSize.Height), BitmapFormat.Bgra, AlphaMode.Ignore); this.BitmapSize = this.SyncBufferSize; } // Write data from SyncBuffer to bitmap this.Bitmap.SetBitmapContent( graphics, this.SyncBuffer.Pointer, this.SyncBuffer.Pitch); } } // Draw the bitmap on the screen if ((this.Bitmap != null) && (this.BitmapSize.Width > 0) && (this.BitmapSize.Height > 0)) { graphics.Clear(Color4.Transparent); // Draw the current contents of the stream RectangleF viewBounds = new RectangleF( 0f, 0f, graphics.ScreenSize.Width, graphics.ScreenSize.Height); graphics.DrawBitmap(this.Bitmap, viewBounds); // Draw the stream's name RectangleF titleBounds = new RectangleF( viewBounds.Width / 2f - 100f, 10f, 200f, 30f); graphics.FillRoundedRectangle(titleBounds, 5f, 5f, this.TextBackground); graphics.DrawText( this.StreamName, this.TextFormat, titleBounds, this.TextForeground); } }
/// <inheritdoc /> protected override void Draw2D(Graphics2D graphics) { base.Draw2D(graphics); if (!_enabled) { return; } RectangleF targetRectFull; switch (_currentType) { case PerformanceDrawingLayerType.FramesPerSecond: // Get display text var fpsText = "-"; if (_lastRender != DateTime.MinValue) { _lastTimeSpans.Add(DateTime.UtcNow - _lastRender); while (_lastTimeSpans.Count > 30) { _lastTimeSpans.RemoveAt(0); } var averageTime = _lastTimeSpans .Select(actTimeSpan => actTimeSpan.TotalMilliseconds) .Average(); fpsText = Math.Round(1000f / averageTime, 0).ToString(CultureInfo.InvariantCulture); } _lastRender = DateTime.UtcNow; // Render display text targetRectFull = new RectangleF( graphics.ScreenWidth - 120f, 10f + _verticalPadding, 100f, 22f); graphics.FillRectangle(targetRectFull, _backBrush); graphics.DrawRectangle(targetRectFull, _borderBrush); graphics.DrawText($"FPS: {fpsText}", _textFormatCentered, targetRectFull, _foreBrush, DrawTextOptions.Clip); break; case PerformanceDrawingLayerType.PressedKeys: // Build display text var stringBuilder = new StringBuilder(16); foreach (var actKeyDown in _keysDown) { if (stringBuilder.Length > 0) { stringBuilder.Append(", "); } stringBuilder.Append(actKeyDown.ToString()); } // Render background for display text targetRectFull = new RectangleF( graphics.ScreenWidth - 220f, 10f + _verticalPadding, 200f, 66f); graphics.FillRectangle(targetRectFull, _backBrush); graphics.DrawRectangle(targetRectFull, _borderBrush); // Render header var targetRectHeader = targetRectFull; targetRectHeader.Height -= 44f; targetRectHeader.Inflate(-3f, -3f); graphics.DrawText("Keys down:", _textFormatLeftAligned, targetRectHeader, _foreBrush); // Render content area var targetRectContent = targetRectFull; targetRectContent.Height -= 22f; targetRectContent.Y += 22f; targetRectContent.Inflate(-3f, -3f); graphics.DrawText(stringBuilder.ToString(), _textFormatLeftAligned, targetRectContent, _foreBrush); break; } }