Ejemplo n.º 1
0
        public async Task <string> ClampText(Canvas2DContext context, string text, int maxWidth)
        {
            var metrics = await context.MeasureTextAsync(text);

            if (metrics.Width <= maxWidth)
            {
                return(text);
            }

            const string suffix        = "...";
            var          suffixMetrics = await context.MeasureTextAsync(suffix);

            maxWidth = (int)Math.Floor(maxWidth - suffixMetrics.Width);

            var str         = string.Empty;
            var previousStr = string.Empty;

            foreach (var letter in text)
            {
                str += letter;
                var width = await context.MeasureTextAsync(str);

                if (width.Width > maxWidth)
                {
                    return(previousStr);
                }

                previousStr = str;
            }

            throw new FormatterException(text);
        }
Ejemplo n.º 2
0
        internal async Task <double> MeasureDisplayStringWidthAsync(string text, Font font)
        {
            await this.SetFont(font);

            var size = await ctx.MeasureTextAsync(text);

            return((double)size.Width);
        }
Ejemplo n.º 3
0
        public async ValueTask Render(GameContext game, Canvas2DContext context)
        {
            await context.SetFillStyleAsync("#fff").ConfigureAwait(false);

            await context.SetFontAsync("18px verdana").ConfigureAwait(false);

            var hiScore  = Math.Max(_score, _maxScore);
            var text     = $"Score: {_score:###} Hi Score: {hiScore:###}";
            var textSize = await context.MeasureTextAsync(text).ConfigureAwait(false);

            var x = game.Display.Size.Width - textSize.Width - 50;

            await context.FillTextAsync(text, x, 50).ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        public async ValueTask Render()
        {
            await _context.ClearRectAsync(0, 0, _width, _height);

            await _context.BeginBatchAsync();

            await _context.SetFillStyleAsync("white");

            await _context.SetFontAsync("32px consolas");

            var text   = $"Points: {_points.ToString("D3")} High score: {HighScore} {HighScoreName} Lives: {_lives.ToString("D3")}";
            var length = await _context.MeasureTextAsync(text);

            await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 25);

            if (GameOver)
            {
                await RenderGameOver();
            }
            else if (Won)
            {
                await RenderWonGame();
            }
            else if (Started)
            {
                await RenderGameFrame();
            }
            else if (!Started)
            {
                await _context.SetFillStyleAsync("white");

                text   = $"Use arrow keys to move, space to fire";
                length = await _context.MeasureTextAsync(text);

                await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 150);

                text   = $"Press space to start";
                length = await _context.MeasureTextAsync(text);

                await _context.FillTextAsync(text, _width / 2 - (length.Width / 2), 200);
            }

            await _context.EndBatchAsync();
        }