Skip to content

Instantly share code, notes, and snippets.

@krypt-lynx
Last active November 12, 2016 07:49
Show Gist options
  • Save krypt-lynx/ba92afbc316f63bf887389da47003697 to your computer and use it in GitHub Desktop.
Save krypt-lynx/ba92afbc316f63bf887389da47003697 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRageMath;
namespace Script
{
#region ingame script start
class Charmap : ICloneable
{
public char[][] Lines { get; private set; }
public int Width { get; private set; }
public int Height
{
get
{
return Lines?.Length ?? 0;
}
}
public Point Origin;
private Charmap() { }
public Charmap(Point size) : this(size.X, size.Y) { }
public Charmap(int width, int height)
{
Width = width;
Lines = new char[height][];
for (int i = 0; i < height; i++)
{
Lines[i] = new char[width];
}
}
public void Clean()
{
Fill(' ', 0, 0, Width, Height);
}
public void Text(string str, int X, int Y, int bW = -1, int bH = -1) // todo
{
var lines = str.Split('\n');
int height = lines.Length;
if (bH != -1)
height = Math.Min(height, bH);
int rX = X + Origin.X, rY = Y + Origin.Y;
int cX = Math.Max(rX, 0), cS = Math.Max(-rX, 0);
int cY = Math.Max(rY, 0),
cL = Math.Min(rY + height, Height);
for (int y = cY, ymax = cL; y < ymax; y++)
{
int width = lines[y - rY].Length;
if (bW != -1)
width = Math.Min(width, bW);
int cW = width - cS - Math.Max(rX + width - Width, 0);
Array.Copy(lines[y - rY].ToCharArray(), cS, Lines[y], cX, cW); // todo;
}
}
public void Text(string str, int X, int Y, int bW, int bH, bool lineWrap) // todo
{
if (lineWrap)
{
var lines = str.Split('\n').Select(x => x.Split(' '));
StringBuilder sb = new StringBuilder();
int length = 0;
foreach (var words in lines)
{
foreach (var word in words)
{
if (length + word.Length + 1 > bW)
{
if (length != 0)
{
sb.Append('\n');
}
// todo: correct length overflow by single word
sb.Append(word);
length = word.Length;
}
else
{
if (length != 0)
{
length++;
sb.Append(' ');
}
sb.Append(word);
length += word.Length;
}
}
sb.Append('\n');
length = 0;
}
str = sb.ToString();
}
Text(str, X, Y, bW, bH);
}
public void Put(char ch, int X, int Y)
{
int rX = X + Origin.X, rY = Y + Origin.Y;
if (rX < 0 || rY < 0 || rX >= Width || rY >= Height)
return;
Lines[rY][rX] = ch;
}
public void Fill(char ch, int X, int Y, int width, int height)
{
int rX = X + Origin.X, rY = Y + Origin.Y;
int cX = Math.Max(rX, 0), cW = width + Math.Min(rX, 0) - Math.Max(rX + width - Width, 0);
int cY = Math.Max(rY, 0), cL = Math.Min(rY + height, Height);
if (cW < 1 || cL < 1)
return;
Lines[cY].Fill(cX, cW, ch);
for (int y = cY+1, ymax = cL; y < ymax; y++)
{
Array.Copy(Lines[cY], cX, Lines[y], cX, cW);
}
}
public object Clone()
{
throw new Exception("Not implemented");
}
public void Draw(Charmap cMap)
{
Draw(cMap, 0, 0, 0, 0, cMap.Width, cMap.Height);
}
public void Draw(Charmap cMap, int X, int Y)
{
Draw(cMap, X, Y, 0, 0, cMap.Width, cMap.Height);
}
public void Draw(Charmap cMap, int X, int Y, int xFrom, int yFrom, int width, int height)
{
int rX = X + Origin.X, rY = Y + Origin.Y;
int cX = Math.Max(rX, 0), cS = Math.Max(-rX, 0);
int cY = Math.Max(rY, 0),
cL = Math.Min(rY + height, Height);
for (int y = cY, ymax = cL; y < ymax; y++)
{
int cW = width - cS - Math.Max(rX + width - Width, 0);
Array.Copy(cMap.Lines[y - rY], cS, Lines[y], cX, cW); // todo;
}
}
}
public static class ArrayExtensions
{
public static void Fill<T>(this T[] array, int start, int length, params T[] value)
{
// todo: exceptions
Array.Copy(value, 0, array, start, value.Length);
int hL = length / 2;
int cL;
for (cL = value.Length; cL <= hL; cL <<= 1)
{
Array.Copy(array, start, array, start + cL, cL);
}
Array.Copy(array, start, array, start + cL, length - cL);
}
}
#endregion // ingame script end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment