Skip to content

Instantly share code, notes, and snippets.

@DynamicField
Created May 12, 2018 09:02

Revisions

  1. DynamicField created this gist May 12, 2018.
    50 changes: 50 additions & 0 deletions UIElement.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    using Cosmos.System.Graphics;
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace DongOSEvolved.UI
    {
    public abstract class UIElement
    {
    // The problem with this technique is that if it's the same color,
    // it doesn't recognize the shape.
    public virtual List<Pixel> Draw(Canvas c)
    {
    var before = GetAllPixels(c);
    DrawShape(c);
    var after = GetAllPixels(c);
    var pixelsChanged = new List<Pixel>();
    // Normally i won't have to check the length of both.
    for (int i = 0; i < before.Length; i++)
    {
    if (before[i] != after[i])
    {
    pixelsChanged.Add(after[i]);
    }
    }
    return pixelsChanged;
    }

    private static Pixel[] GetAllPixels(Canvas c)
    {
    var totalPixels = c.Mode.Columns * c.Mode.Rows;
    Pixel[] allPoints = new Pixel[totalPixels];
    int x = 0, y = 0;
    for (int i = 0; i < totalPixels; i++)
    {
    allPoints[i] = new Pixel(c.GetPointColor(x, y), new Point(x, y));
    y++;
    if (y >= c.Mode.Columns - 1) // Go to the line under it
    {
    y = 0;
    ++x; // it's better than x++ for some reason.
    }
    }
    return allPoints;
    }

    protected abstract void DrawShape(Canvas c);
    public int Layer { get; set; } = 0;
    }
    }