Exemplo n.º 1
0
        /// <summary>
        /// Queues a line of text to be drawn on the screen at the specified location during the next frame.
        /// </summary>
        /// <param name="Text">Text to be displayed</param>
        /// <param name="x">X screen coordinate to draw at</param>
        /// <param name="y">Y screen coordinate to draw at</param>
        /// <param name="size">Size of text</param>
        public static void DrawText(object Text, int x, int y, int size)
        {
            if (Text == null)
            {
                return;
            }
            string text = Text.ToString();

            if (instance == null)
            {
                instance = new Debug();
            }
            if (!Enabled)
            {
                return;
            }
            DrawTextJob j = new DrawTextJob
            {
                Text = text,
                Size = size,
                X    = x,
                Y    = y
            };

            lock (instance.textJobs)
            {
                instance.textJobs.Enqueue(j);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draws the in-game console, debug overlay, and any text queued by the DrawText() method.
        /// Also manages keyboard handling for enabling or disabling the Debug interface and interacting with the in-game console.
        /// </summary>
        public static void Draw()
        {
            if (instance == null)
            {
                instance = new Debug();
            }

            if (IsKeyPressed(KeyboardKey.KEY_F1))
            {
                if (Enabled)
                {
                    Enabled       = false;
                    ConsoleIsOpen = false;
                }
                else
                {
                    Enabled = true;
                }
            }
            if (IsKeyPressed(KeyboardKey.KEY_GRAVE) && Enabled)
            {
                ConsoleIsOpen = !ConsoleIsOpen;
            }
            if (instance == null)
            {
                return;
            }
            if (!Enabled)
            {
                if (instance.consoleLines.Count > instance.consoleMaxLines * 2)
                {
                    for (int i = 0; i < instance.consoleMaxLines; i++)
                    {
                        instance.consoleLinesBuffer.Push(instance.consoleLines.Pop());
                    }
                    instance.consoleLines.Clear();
                    while (instance.consoleLinesBuffer.Count > 0)
                    {
                        instance.consoleLines.Push(instance.consoleLinesBuffer.Pop());
                    }
                    Debug.WriteLine("Trimmed debug console");
                }
            }
            if (!instance.initialized)
            {
                instance.font        = ResourceManager.Get <FontResource>(@"fonts\Perfect_DOS_VGA_437_Win").Font;
                instance.initialized = true;
            }
            // ------------------------------------------------------------------------------------
            framerate += Math.Max(GetFPS() + 0.5, 0);
            framerate  = Math.Round(framerate / 2);
            Debug.WriteOverlay("FPS: " + Convert.ToString((int)framerate, null));

            Vector2 p = new Vector2();

            lock (instance.shapeJobs)
            {
                while (instance.shapeJobs.Count > 0)
                {
                    DrawShapeJob j = instance.shapeJobs.Dequeue();
                    if (j.ShapeType == Shapes.Line)
                    {
                        Raylib.DrawLine(j.A, j.B, j.C, j.D, j.Color);
                    }
                    else if (j.ShapeType == Shapes.Rectangle)
                    {
                        Raylib.DrawRectangle(j.A, j.B, j.C, j.D, j.Color);
                    }
                }
            }

            if (instance.consoleSize > 0)
            {
                UpdateTerminal();

                DrawRectangle(0, 0, GetScreenWidth(), instance.consoleSize, instance.backgroundColor);
                DrawLineEx(new Vector2(0, instance.consoleSize + 1), new Vector2(GetScreenWidth(), instance.consoleSize + 1), 2, Color.WHITE);
                DrawLineEx(new Vector2(0, instance.consoleSize - instance.consoleFontSize - 1), new Vector2(GetScreenWidth(), instance.consoleSize - instance.consoleFontSize - 1), 1, Color.GRAY);
                DrawTextEx(instance.font, terminalBuffer.Insert(terminalCursor, showCursor ? "_" : " "), new Vector2(4, instance.consoleSize - instance.consoleFontSize - 1), instance.consoleFontSize, 1.0f, instance.foregroundColor);

                lock (instance.consoleLines)
                {
                    p.X = 4;
                    p.Y = instance.consoleSize - (instance.consoleFontSize * 2) - 2;
                    int count = instance.consoleLines.Count;
                    for (int i = 0; i < Math.Min(instance.consoleMaxLines, count); i++)
                    {
                        string line         = instance.consoleLines.Pop();
                        string originalLine = line;
                        if (i >= consoleLinesOffset)
                        {
                            string tag = "";
                            if (line.StartsWith("%", StringComparison.Ordinal))
                            {
                                string[] tokens = line.Split('%');
                                if (tokens.Length > 2)
                                {
                                    line = "";
                                    for (int j = 2; j < tokens.Length; j++)
                                    {
                                        line += ((j > 2) ? "%" : "") + tokens[j];
                                    }

                                    tag = tokens[1].ToUpper(System.Globalization.CultureInfo.InvariantCulture);
                                }
                            }

                            if (p.Y > -instance.consoleFontSize)
                            {
                                if (!string.IsNullOrEmpty(tag))
                                {
                                    Color c = instance.backgroundColorAlt;
                                    if (tag == "SUCCESS")
                                    {
                                        c = new Color(0, 255, 0, 64);
                                    }
                                    else if (tag == "WARNING")
                                    {
                                        c = new Color(255, 255, 0, 64);
                                    }
                                    else if (tag == "ERROR")
                                    {
                                        c = new Color(255, 0, 0, 64);
                                    }
                                    DrawRectangle((int)p.X - 2, (int)p.Y + 1, GetScreenWidth() - 2, instance.consoleFontSize - 1, c);
                                }
                                DrawTextEx(instance.font, line, p, instance.consoleFontSize, 1.0f, instance.foregroundColor);
                            }

                            p.Y -= instance.consoleFontSize;
                        }
                        instance.consoleLinesBuffer.Push(originalLine);
                    }
                    instance.consoleLines.Clear();
                    while (instance.consoleLinesBuffer.Count > 0)
                    {
                        instance.consoleLines.Push(instance.consoleLinesBuffer.Pop());
                    }
                }
            }

            if (ConsoleIsOpen && instance.consoleSize < instance.consoleMaxSize)
            {
                instance.consoleSize += 8;
            }
            if (!ConsoleIsOpen && instance.consoleSize > 0)
            {
                instance.consoleSize -= 8;
            }

            lock (instance.textJobs)
            {
                while (instance.textJobs.Count > 0)
                {
                    DrawTextJob j = instance.textJobs.Dequeue();
                    if (j.Y > instance.consoleSize)
                    {
                        p.X = j.X - 1;
                        p.Y = j.Y - 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);
                        p.X = j.X + 1;
                        p.Y = j.Y + 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);
                        p.X = j.X - 1;
                        p.Y = j.Y + 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);
                        p.X = j.X + 1;
                        p.Y = j.Y - 1;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, instance.outlineColor);

                        p.X = j.X;
                        p.Y = j.Y;
                        DrawTextEx(instance.font, j.Text, p, j.Size, 1.0f, Color.BLACK);
                    }
                }
            }

            lock (instance.overlayLines)
            {
                p.Y = instance.consoleSize + (instance.consoleFontSize * 0.25f);
                p.X = 4;
                while (instance.overlayLines.Count > 0)
                {
                    string line = instance.overlayLines.Dequeue();
                    DrawRectangle((int)p.X - 2, (int)p.Y - 1, (int)((line.Length * instance.consoleFontSize * 0.66f) - 2), instance.consoleFontSize - 1, instance.outlineColor);
                    DrawTextEx(instance.font, line, p, instance.consoleFontSize, 1.0f, Color.BLACK);
                    p.Y += instance.consoleFontSize;
                }
            }

            if (instance.logLines.Count > 0)
            {
                using StreamWriter stream = new FileInfo(logfileName.Replace("#", "0")).AppendText();
                while (instance.logLines.Count > 0)
                {
                    stream.WriteLine(instance.logLines.Dequeue());
                }
            }
        }