Skip to content

Instantly share code, notes, and snippets.

@DynamicField
Created May 12, 2018 09:02
Show Gist options
  • Save DynamicField/53efb1819b9863de604fd0c588ffe3cb to your computer and use it in GitHub Desktop.
Save DynamicField/53efb1819b9863de604fd0c588ffe3cb to your computer and use it in GitHub Desktop.
UI thing
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment