started abstracting the UI

This commit is contained in:
lewd-alt
2025-09-18 08:43:54 -07:00
parent 459e9d04a6
commit 7f0489b214
23 changed files with 428 additions and 903192 deletions

View File

@@ -7,39 +7,26 @@ namespace _2DGAMELIB
public class Are : Rect
{
public Bitmap Dis;
public Bitmap Hit;
protected Graphics gd;
protected Bitmap Hit;
protected Graphics gh;
protected double unit;
protected double disUnit;
protected double hitUnit;
protected Size WH = System.Drawing.Size.Empty;
protected Size WHH = System.Drawing.Size.Empty;
protected Size WHA = System.Drawing.Size.Empty;
private Color ClearColor = Color.Transparent;
public Vector2D BasePoint = Dat.Vec2DZero;
public Vector2D Position = Dat.Vec2DZero;
protected Vector2D p;
public Graphics GD => gd;
public Graphics GH => gh;
public double Unit => unit;
public double DisUnit => disUnit;
public Are() { }
@@ -132,15 +119,28 @@ namespace _2DGAMELIB
}
}
public void Draw(Graphics GD)
public void Draw(Are Are)
{
Vector2D p = Are.GetPosition();
GD.DrawImage(Are.Dis, (float)(p.X * Are.disUnit), (float)(p.Y * Are.disUnit), Are.WHA.Width, Are.WHA.Height);
if (Are.gh != null && GH != null)
{
GH.DrawImage(Are.Hit, (int)(p.X * Are.hitUnit), (int)(p.Y * Are.hitUnit), Are.WHH.Width, Are.WHH.Height);
}
}
public void DrawTo(Graphics GD)
{
p = GetPosition();
Vector2D p = GetPosition();
GD.DrawImage(Dis, (int)(p.X * unit), (int)(p.Y * unit), WH.Width, WH.Height);
}
public void Draw(Graphics GD, Graphics GH)
public void DrawTo(Graphics GD, Graphics GH)
{
p = GetPosition();
Vector2D p = GetPosition();
GD.DrawImage(Dis, (int)(p.X * unit), (int)(p.Y * unit), WH.Width, WH.Height);
if (gh != null)
{
@@ -148,15 +148,6 @@ namespace _2DGAMELIB
}
}
public void Draw(Are Are)
{
p = Are.GetPosition();
GD.DrawImage(Are.Dis, (float)(p.X * Are.disUnit), (float)(p.Y * Are.disUnit), Are.WHA.Width, Are.WHA.Height);
if (Are.gh != null && GH != null)
{
GH.DrawImage(Are.Hit, (int)(p.X * Are.hitUnit), (int)(p.Y * Are.hitUnit), Are.WHH.Width, Are.WHH.Height);
}
}
@@ -164,10 +155,10 @@ namespace _2DGAMELIB
public void Clear()
{
gd.Clear(ClearColor);
gd.Clear(Color.Transparent);
if (gh != null)
{
gh.Clear(ClearColor);
gh.Clear(Color.Transparent);
}
}
@@ -176,7 +167,7 @@ namespace _2DGAMELIB
gd.Clear(Color);
if (gh != null)
{
gh.Clear(ClearColor);
gh.Clear(Color.Transparent);
}
}

View File

@@ -9,100 +9,84 @@ namespace _2DGAMELIB
{
public class Med
{
public static object obj;
private GlImage baseControl;
private GlImage baseControl;
public double Unit = 1762.4;
public double DisQuality = 1.0;
public double HitAccuracy = 0.5;
private double resMag = 1.0;
private int WidthM;
private int HeightM;
public double Unit = 1762.4;
public bool ShowFPS;
public bool Drive = true;
public Rect Base = new Rect(4.0, 3.0, 0.4);
public Rect Base = new Rect(4.0, 3.0, 0.4);
public double DisQuality = 1.0;
public Bitmap Dis;
public Graphics GD;
public double HitAccuracy = 0.5;
public Bitmap Hit;
public Graphics GH;
public Bitmap BD;
public Sce Sce;
public FPS FPSF = new FPS(60.0);
public Graphics GD;
private Size BaseSize = Size.Empty;
private Vector2D resVector = Dat.Vec2DZero;
public Bitmap BH;
public string UITitle;
public Graphics GH;
private string mode;
public string Modeb;
private Color ClearColor = Color.Transparent;
private Dictionary<string, Module> Modes;
private Func<Med, Dictionary<string, Module>> GetModes;
public bool Drive = true;
public HashSet<Color> HitColors = new HashSet<Color>
{
Color.Transparent,
Color.Black
};
private Size BaseSize = Size.Empty;
private double resMag = 1.0;
//public Control BaseControlC => baseControl;
private Vector2D resVector = Dat.Vec2DZero;
public string Mode
{
get
{
return mode;
}
set
{
Modes[mode].Up(MouseButtons.None, Dat.Vec2DZero, Color.Empty);
Modes[mode].Move(MouseButtons.None, Dat.Vec2DZero, Color.Empty);
Modeb = mode;
mode = value;
Modes[mode].Move(MouseButtons.None, Dat.Vec2DZero, Color.Empty);
Modes[mode].Setting();
}
}
private Sce Sce;
private string mode;
public string Modeb;
private Dictionary<string, Module> Modes;
private Func<Med, Dictionary<string, Module>> GetModes;
private int WidthM;
private int HeightM;
public static double FPS;
public FPS FPSF = new FPS(FPS);
private bool cur = true;
public HashSet<Color> HitColors = new HashSet<Color>
{
Color.Transparent,
Color.Black
};
public static double DpiX;
public static double DpiY;
public string UITitle;
public bool ShowFPS;
//public Control BaseControlC => baseControl;
public string Mode
{
get
{
return mode;
}
set
{
Modes[mode].Up(MouseButtons.None, Dat.Vec2DZero, Color.Empty);
Modes[mode].Move(MouseButtons.None, Dat.Vec2DZero, Color.Empty);
Modeb = mode;
mode = value;
Modes[mode].Move(MouseButtons.None, Dat.Vec2DZero, Color.Empty);
Modes[mode].Setting();
}
}
public Vector2D CursorPosition => ToBasePosition(baseControl.GetCursorPoint()); //ToBasePosition(baseControl.PointToClient(System.Windows.Forms.Cursor.Position));
public Vector2D CursorPosition{
get {
return ToBasePosition(baseControl.GetCursorPoint()); //ToBasePosition(baseControl.PointToClient(System.Windows.Forms.Cursor.Position));
}
set {
baseControl.SetCursorPoint(FromBasePosition(value));
}
}
public Med()
{
//Point dpi = GetDpi();
//dpiX = dpi.X;
//dpiY = dpi.Y;
DpiX = 1.0; // 96.0 / dpiX;
DpiY = 1.0; // 96.0 / dpiY;
}
public void FadeIn(double Rate)
{
Sce.TransformAlpha(GD, Rate);
@@ -113,20 +97,10 @@ namespace _2DGAMELIB
Sce.TransD(GD, Rate);
}
public void DrawStart(Are Are)
{
Sce.DrawStart(Are);
}
public void DrawEnd(Are Are)
{
Sce.DrawEnd(Are);
}
public void ClearSta(Color ClearColor)
{
Sce.ClearStart(ref ClearColor);
}
public void InitializeModes(string Mode, Func<Med, Dictionary<string, Module>> GetModes)
{
@@ -138,24 +112,31 @@ namespace _2DGAMELIB
{
baseControl = BaseControl;
BaseSize = new Size((int)(Base.LocalWidth * Unit), (int)(Base.LocalHeight * Unit));
BD = new Bitmap(BaseSize.Width, BaseSize.Height);
GD = Graphics.FromImage(BD);
GD.InterpolationMode = InterpolationMode.HighQualityBilinear;
BH = new Bitmap((int)((double)BaseSize.Width * HitAccuracy), (int)((double)BaseSize.Height * HitAccuracy));
GH = Graphics.FromImage(BH);
GH.InterpolationMode = InterpolationMode.Bilinear;
WidthM = BH.Width - 1;
HeightM = BH.Height - 1;
Clear();
Dis = new Bitmap(BaseSize.Width, BaseSize.Height);
GD = Graphics.FromImage(Dis);
//GD.InterpolationMode = InterpolationMode.HighQualityBilinear;
GD.SmoothingMode = SmoothingMode.None;
GD.PixelOffsetMode = PixelOffsetMode.HighSpeed;
GD.InterpolationMode = InterpolationMode.NearestNeighbor;
//needed for text or smthn
GD.CompositingMode = CompositingMode.SourceOver;
Hit = new Bitmap((int)((double)BaseSize.Width * HitAccuracy), (int)((double)BaseSize.Height * HitAccuracy));
GH = Graphics.FromImage(Hit);
//GH.InterpolationMode = InterpolationMode.Bilinear;
GH.SmoothingMode = SmoothingMode.None;
GH.PixelOffsetMode = PixelOffsetMode.HighSpeed;
GH.InterpolationMode = InterpolationMode.NearestNeighbor;
GH.CompositingMode = CompositingMode.SourceOver;
WidthM = Hit.Width - 1;
HeightM = Hit.Height - 1;
Sce = new Sce(BaseSize.Width, BaseSize.Height);
Modes = GetModes(this);
BaseControl.Click = delegate (IntPtr window, GLFW.MouseButton button, InputState state, GLFW.ModifierKeys modifiers)
{
double x, y;
Glfw.GetCursorPosition(GlImage.PtrToWindow(window), out x, out y);
Point Position5 = new Point((int)x, (int)y);
MouseButtons arg2 = MouseButtons.None;
switch (button)
{
@@ -176,14 +157,17 @@ namespace _2DGAMELIB
break;
}
(state == InputState.Press ? Modes[mode].Down : Modes[mode].Up)(arg2, ToBasePosition(Position5), GetHitColor(ref Position5));
double x, y;
Glfw.GetCursorPosition(GlImage.PtrToWindow(window), out x, out y);
Vector2D Position5 = new Vector2D(x, y);
(state == InputState.Press ? Modes[mode].Down : Modes[mode].Up)
(arg2, ToBasePosition(Position5), GetHitColor(Position5));
};
BaseControl.Move = delegate (IntPtr window, double x, double y)
{
Point Position3 = new Point((int)x, (int)y);
Modes[mode].Move(BaseControl.GetMouseButtons(), ToBasePosition(Position3), GetHitColor(ref Position3));
Vector2D Position3 = new Vector2D(x, y);
Modes[mode].Move(BaseControl.GetMouseButtons(), ToBasePosition(Position3), GetHitColor(Position3));
};
BaseControl.Leave = delegate (IntPtr window, bool entered)
@@ -192,8 +176,8 @@ namespace _2DGAMELIB
{
double x, y;
Glfw.GetCursorPosition(GlImage.PtrToWindow(window), out x, out y);
Point Position2 = new Point((int)x, (int)y);
Modes[mode].Leave(BaseControl.GetMouseButtons(), ToBasePosition(Position2), GetHitColor(ref Position2));
Vector2D Position2 = new Vector2D(x, y);
Modes[mode].Leave(BaseControl.GetMouseButtons(), ToBasePosition(Position2), GetHitColor(Position2));
}
};
@@ -201,9 +185,9 @@ namespace _2DGAMELIB
{
double x, y;
Glfw.GetCursorPosition(GlImage.PtrToWindow(window), out x, out y);
Point Position = new Point((int)x, (int)y);
Vector2D Position = new Vector2D(x, y);
//Note: yo may be inverted
Modes[mode].Wheel(BaseControl.GetMouseButtons(), ToBasePosition(Position), (int)yo, GetHitColor(ref Position));
Modes[mode].Wheel(BaseControl.GetMouseButtons(), ToBasePosition(Position), (int)yo, GetHitColor(Position));
};
BaseControl.Resize = delegate (IntPtr window, int width, int height)
@@ -246,20 +230,22 @@ namespace _2DGAMELIB
return BaseSize;
}
public Vector2D ToBasePosition(Point Position)
public Vector2D ToBasePosition(Vector2D Position)
{
return new Vector2D(((double)Position.X - resVector.X) / Unit * resMag, ((double)Position.Y - resVector.Y) / Unit * resMag);
}
public Point FromBasePosition(Vector2D Position)
public Vector2D FromBasePosition(Vector2D Position)
{
return new Point((int)(Position.X / resMag * Unit + resVector.X), (int)(Position.Y / resMag * Unit + resVector.Y));
return new Vector2D(Position.X / resMag * Unit + resVector.X, Position.Y / resMag * Unit + resVector.Y);
}
public Color GetHitColor(Point Position)
public Color GetHitColor(Vector2D Position)
{
double num = HitAccuracy * resMag;
Point point = ((Position.ToVector2D() - resVector) * num).ToPoint();
Point point = ((Position - resVector) * num).ToPoint();
if (point.X < 0)
{
point.X = 0;
@@ -276,7 +262,7 @@ namespace _2DGAMELIB
{
point.Y = HeightM;
}
return BH.GetPixel(point.X, point.Y);
return Hit.GetPixel(point.X, point.Y);
}
public Color GetHitColor(ref Point Position)
@@ -299,14 +285,18 @@ namespace _2DGAMELIB
{
point.Y = HeightM;
}
return BH.GetPixel(point.X, point.Y);
return Hit.GetPixel(point.X, point.Y);
}
public void Drawing()
{
baseControl.BitmapSetting(BD);
Modes[mode].Setting();
baseControl.BitmapSetting(Dis);
baseControl.SetTitle(UITitle);
Modes[mode].Setting();
Action action = delegate
{
@@ -320,8 +310,9 @@ namespace _2DGAMELIB
}
}
GD.DrawImage(BH, new Point(0, 0));
baseControl.SetBitmap(BD);
//DEBUG shows the hit lut
//GD.DrawImage(Hit, new Point(0, 0));
baseControl.SetBitmap(Dis);
};
@@ -332,45 +323,30 @@ namespace _2DGAMELIB
}
}
public void Clear()
{
GD.Clear(ClearColor);
GH.Clear(ClearColor);
}
public void Draw(Are Are)
{
Are.Draw(GD, GH);
//Note: this is terribly slow...
//would be better to not copy the entire frame
Are.DrawTo(GD, GH);
}
//TODO fix?
public void CursorHide()
{
if (cur)
{
//TODO fix?
//System.Windows.Forms.Cursor.Hide();
cur = false;
}
//TODO fix?
}
public void CursorShow()
{
if (!cur)
{
//TODO fix?
//System.Windows.Forms.Cursor.Show();
cur = true;
}
//TODO fix?
}
public void InvokeL(Action a)
{
//TODO this look right?
a();
//baseControl.Invoke(a);
}
public Color GetUniqueColor()
//hit color stuff
public Color GetUniqueColor()
{
Oth.GetRandomColor(out var ret);
while (HitColors.Contains(ret))
@@ -434,10 +410,6 @@ namespace _2DGAMELIB
//return result;
}*/
static Med()
{
obj = new object();
FPS = 60.0;
}
static Med() {}
}
}

View File

@@ -276,7 +276,7 @@ namespace _2DGAMELIB
}
if (EditF || EditTS)
{
Font = new Font(font.FontFamily, (float)(us * fontSize * Med.DpiY));
Font = new Font(font.FontFamily, (float)(us * fontSize));
EditF = false;
EditTS = false;
}
@@ -303,7 +303,7 @@ namespace _2DGAMELIB
double num = Unit * base.Size;
if (EditF || EditS || EditPS || EditTS)
{
Font = new Font(font.FontFamily, (float)(num * fontSize * Med.DpiY));
Font = new Font(font.FontFamily, (float)(num * fontSize));
EditF = false;
EditTS = false;
}

View File

@@ -8,11 +8,9 @@ namespace _2DGAMELIB
public class Sce
{
private Bitmap Start;
private Graphics GS;
private Bitmap End;
private Graphics GE;
private int w;
@@ -54,12 +52,12 @@ namespace _2DGAMELIB
public void DrawStart(Are Are)
{
Are.Draw(GS);
Are.DrawTo(GS);
}
public void DrawEnd(Are Are)
{
Are.Draw(GE);
Are.DrawTo(GE);
}
public void ClearStart(ref Color ClearColor)

View File

@@ -40,12 +40,16 @@ namespace _2DGAMELIB
public GlImage() { }
public System.Drawing.Point GetCursorPoint() {
public Vector2D GetCursorPoint() {
double x, y;
Glfw.GetCursorPosition(window, out x, out y);
return new System.Drawing.Point((int)x, (int)y);
return new Vector2D(x, y);
}
public void SetCursorPoint(Vector2D pos) {
Glfw.SetCursorPosition(window, pos.X, pos.Y);
}
public MouseButtons GetMouseButtons() {
MouseButtons btns = 0;
@@ -220,7 +224,7 @@ void main()
gl.TexParameterI(Silk.NET.OpenGL.GLEnum.Texture2D, Silk.NET.OpenGL.GLEnum.TextureMinFilter, new int[] {(int)TextureMinFilter.Nearest});
float[] buf = { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f };
float[] buf = { 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f };
vertex_buf = gl.GenBuffer();
gl.BindBuffer(Silk.NET.OpenGL.GLEnum.ArrayBuffer, vertex_buf);
fixed (float* buf_ = buf) gl.BufferData(Silk.NET.OpenGL.GLEnum.ArrayBuffer, (uint)(sizeof(float) * buf.Length), buf_, Silk.NET.OpenGL.GLEnum.StaticDraw);
@@ -228,102 +232,4 @@ void main()
vao = gl.GenVertexArray();
}
}
/*
public class WPFImage : ElementHost
{
public GlImage gl_img;
private int ByteSize;
private Rectangle rect1;
private Int32Rect rect2;
private BitmapData data;
public WriteableBitmap wb;
private bool HighQuality;
private string ConfigPath = Directory.GetCurrentDirectory() + "\\Config.ini";
public System.Windows.Controls.Image Image => (System.Windows.Controls.Image)base.Child;
public WPFImage()
{
System.Windows.Controls.Image child = new System.Windows.Controls.Image
{
Stretch = Stretch.Uniform
};
base.Child = child;
gl_img = new GlImage();
}
public void PollEvents() {
gl_img.PollEvents();
}
public void ImageSetting()
{
try
{
if (!File.Exists(ConfigPath))
{
HighQuality = false;
}
else
{
string[] source = ConfigPath.ReadLines();
HighQuality = source.First((string s) => s.StartsWith("AntiAliasing:")).Last() == '1';
}
}
catch
{
HighQuality = false;
}
if (HighQuality)
{
RenderOptions.SetBitmapScalingMode(Image, BitmapScalingMode.LowQuality);
}
else
{
RenderOptions.SetBitmapScalingMode(Image, BitmapScalingMode.NearestNeighbor);
}
RenderOptions.SetEdgeMode(Image, EdgeMode.Aliased);
}
[DllImport("Kernel32.dll")]
public static extern void CopyMemory(IntPtr Destination, IntPtr Source, int ByteSize);
//the guy that gets called to update a frame
public void SetBitmap(Bitmap bmp)
{
data = bmp.LockBits(rect1, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
wb.Lock();
CopyMemory(wb.BackBuffer, data.Scan0, ByteSize);
wb.AddDirtyRect(rect2);
wb.Unlock();
bmp.UnlockBits(data);
gl_img.SetBitmap(bmp);
}
public void BitmapSetting(Bitmap bmp)
{
int pixelWidth = bmp.Width;
int num = bmp.Height;
rect1 = new Rectangle(0, 0, pixelWidth, num);
rect2 = new Int32Rect(0, 0, pixelWidth, num);
wb = new WriteableBitmap(pixelWidth, num, bmp.HorizontalResolution, bmp.VerticalResolution, PixelFormats.Bgra32, null);
ByteSize = wb.BackBufferStride * num;
Image.Source = wb;
gl_img.BitmapSetting(bmp);
}
}
*/
}