Skip to content

Commit

Permalink
added Color property for Image class (default: White), so GDI can use…
Browse files Browse the repository at this point in the history
… it for painting. Perfect solution to paint something like BackgroundImage;

fixed some bugs in ComboBox control and added some more missing features;
fixed ScrollBar behavior for handling input keys, now it's just handing over KeyArgs to it's parent as it should be intended;
  • Loading branch information
Meragon committed Jul 19, 2019
1 parent ddbf07f commit 214e81f
Show file tree
Hide file tree
Showing 19 changed files with 408 additions and 314 deletions.
14 changes: 7 additions & 7 deletions Controls/BitmapText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ public BitmapText(BitmapFont font)
public void Apply()
{
var tSize = TextureSize;
uTexture = Graphics.ApiGraphics.CreateTexture(tSize.Width, tSize.Height);
Texture = Graphics.ApiGraphics.CreateTexture(tSize.Width, tSize.Height);

if (tSize.Width == 0 || tSize.Height == 0)
return;


// Clear texture with transparent color.
var uBackColor = Color.Transparent;
var clearColor = new Color[uTexture.Width * uTexture.Height];
var clearColor = new Color[Texture.Width * Texture.Height];
for (int i = 0; i < clearColor.Length; i++)
clearColor[i] = uBackColor;
uTexture.SetPixels(clearColor);
Texture.SetPixels(clearColor);

int xOffset = 0;
float cursorY = GetCursorY();
float cursorOffset = uTexture.Height - cursorY;
float cursorOffset = Texture.Height - cursorY;

for (int i = 0; i < Text.Length; i++)
{
Expand All @@ -101,7 +101,7 @@ public void Apply()

if (cW > 0 && cH > 0)
{
var charPixels = textC.Texture.uTexture.GetPixels();
var charPixels = textC.Texture.Texture.GetPixels();
var charTexture = Graphics.ApiGraphics.CreateTexture(textC.Texture.Width, textC.Texture.Height);
charTexture.SetPixels(charPixels);
charTexture.Apply();
Expand All @@ -126,13 +126,13 @@ public void Apply()
blendedCharPixels[p] = Color.FromArgb(bA, bR, bG, bB);
}

uTexture.SetPixels((int)cX, (int)(cursorOffset - bellowHeight), charTexture.Width, charTexture.Height, blendedCharPixels);
Texture.SetPixels((int)cX, (int)(cursorOffset - bellowHeight), charTexture.Width, charTexture.Height, blendedCharPixels);
}

xOffset += (int)cA;
}

uTexture.Apply();
Texture.Apply();
}
public BitmapChar CharAt(int index)
{
Expand Down
2 changes: 1 addition & 1 deletion Core/API/IApiGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IApiGraphics

void BeginGroup(float x, float y, float width, float height);
void Clear(Color color);
void DrawImage(Image image, Color color, float x, float y, float width, float height, float angle);
void DrawImage(Image image, float x, float y, float width, float height, float angle);
void DrawImage(Image image, float x, float y, float width, float height, object material = null);
void DrawLine(Pen pen, float x1, float y1, float x2, float y2, object material = null);
void DrawPolygon(Pen pen, Point[] points, object material = null);
Expand Down
2 changes: 1 addition & 1 deletion Examples/ControlsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static class ControlsExtensions
{
public static T Create<T>(this Control parent, int margin = 8, int lineHeight = 24) where T : Control, new()
{
return Create<T>(parent, AnchorStylesExtended.LeftTop, null, false, margin, lineHeight);
return Create<T>(parent, AnchorStylesExtended.LeftTop, string.Empty, false, margin, lineHeight);
}
public static T Create<T>(this Control parent, AnchorStylesExtended anchor, bool placeToRight = false, int margin = 8, int lineHeight = 24) where T : Control, new()
{
Expand Down
14 changes: 7 additions & 7 deletions System/Drawing/Bitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public class Bitmap : Image
public Bitmap(Image original)
{
if (original != null)
uTexture = original.uTexture;
Texture = original.Texture;
}
public Bitmap(int width, int height)
{
uTexture = Graphics.ApiGraphics.CreateTexture(width, height);
Texture = Graphics.ApiGraphics.CreateTexture(width, height);
}

private Bitmap()
Expand All @@ -24,30 +24,30 @@ public void ClearColor(Color c, bool apply = true)
var colors = new Color[Width * Height];
for (int i = 0; i < colors.Length; i++)
colors[i] = c;
uTexture.SetPixels(colors);
Texture.SetPixels(colors);
if (apply) this.Apply();
}
public Color GetPixel(int x, int y)
{
return uTexture.GetPixel(x, uTexture.Height - y - 1);
return Texture.GetPixel(x, Texture.Height - y - 1);
}
public Color[] GetPixels(int x, int y, int w, int h)
{
var ucs = uTexture.GetPixels(x, uTexture.Height - y - 1, w, h);
var ucs = Texture.GetPixels(x, Texture.Height - y - 1, w, h);
Color[] cs = new Color[ucs.Length];
for (int i = 0; i < cs.Length; i++)
cs[i] = ucs[i];
return cs;
}
public void SetPixel(int x, int y, Color color)
{
uTexture.SetPixel(x, uTexture.Height - y - 1, color);
Texture.SetPixel(x, Texture.Height - y - 1, color);
}

internal static Bitmap FromTexture(ITexture tex)
{
var bmp = new Bitmap();
bmp.uTexture = tex;
bmp.Texture = tex;

return bmp;
}
Expand Down
12 changes: 8 additions & 4 deletions System/Drawing/Graphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void DrawImage(Image image, int x, int y, int width, int height)
}
public void DrawImage(Image image, float x, float y, float width, float height)
{
ApiGraphics.DrawImage(image, Color.White, x, y, width, height, 0);
ApiGraphics.DrawImage(image, x, y, width, height, 0);
}
public void DrawLine(Pen pen, Point pt1, Point pt2)
{
Expand Down Expand Up @@ -161,13 +161,16 @@ public SizeF MeasureString(string text, Font font)

internal static PointF[] GetBezierApproximation(PointF[] controlPoints, int outputSegmentCount)
{
if (outputSegmentCount <= 0) return null;
if (outputSegmentCount <= 0)
return new PointF[] {};

var points = new PointF[outputSegmentCount + 1];
for (int i = 0; i <= outputSegmentCount; i++)
{
float t = (float)i / outputSegmentCount;
float t = (float) i / outputSegmentCount;
points[i] = GetBezierPoint(t, controlPoints, 0, controlPoints.Length);
}

return points;
}
internal void GroupBegin(Control control)
Expand All @@ -188,7 +191,8 @@ internal void GroupEnd()
#region Not original methods.
internal void uwfDrawImage(Image image, Color color, float x, float y, float width, float height)
{
ApiGraphics.DrawImage(image, color, x, y, width, height, 0);
image.Color = color;
ApiGraphics.DrawImage(image, x, y, width, height, 0);
}
internal string uwfDrawPasswordField(string s, Font font, Color color, float x, float y, float width, float height, HorizontalAlignment alignment)
{
Expand Down
12 changes: 9 additions & 3 deletions System/Drawing/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

public abstract class Image
{
public int Height { get { return uTexture.Height; } }
internal Image()
{
Color = Color.White;
}

public int Height { get { return Texture.Height; } }
public Size Size { get { return new Size(Width, Height); } }
public object Tag { get; set; }
public int Width { get { return uTexture.Width; } }
public int Width { get { return Texture.Width; } }

internal ITexture uTexture { get; set; }
internal Color Color { get; set; }
internal ITexture Texture { get; set; }
}
}
5 changes: 4 additions & 1 deletion System/Drawing/Pen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public Pen(Color color)
Color = color;
Width = 1;
}

public Pen(Color color, float width)
{
Color = color;
Expand All @@ -31,14 +32,16 @@ public object Clone()
pen.Width = Width;
return pen;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

void Dispose(bool disposing)
private void Dispose(bool disposing)
{
// Nothing to dispose.
}
}
}
40 changes: 26 additions & 14 deletions System/Drawing/Rectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ public Rectangle(Point location, Size size) : this()
Width = size.Width;
Height = size.Height;
}

public int Bottom { get { return Y + Height; } }

public int Bottom
{
get { return Y + Height; }
}
public int Height { get; set; }
public bool IsEmpty
{
get { return X == 0 && Y == 0 && Width == 0 && Height == 0; }
}
public int Left { get { return X; } }
public int Left
{
get { return X; }
}
public Point Location
{
get { return new Point(X, Y); }
Expand All @@ -38,7 +44,10 @@ public Point Location
Y = value.Y;
}
}
public int Right { get { return X + Width; } }
public int Right
{
get { return X + Width; }
}
public Size Size
{
get { return new Size(Width, Height); }
Expand All @@ -48,11 +57,14 @@ public Size Size
Height = value.Height;
}
}
public int Top { get { return Y; } }
public int Top
{
get { return Y; }
}
public int Width { get; set; }
public int X { get; set; }
public int Y { get; set; }

public static bool operator !=(Rectangle left, Rectangle right)
{
return !(left == right);
Expand Down Expand Up @@ -84,9 +96,9 @@ public static Rectangle Intersect(Rectangle a, Rectangle b)
int y1 = Math.Max(a.Y, b.Y);
int y2 = Math.Min(a.Y + a.Height, b.Y + b.Height);

if (x2 >= x1 && y2 >= y1)
if (x2 >= x1 && y2 >= y1)
return new Rectangle(x1, y1, x2 - x1, y2 - y1);

return Empty;
}
public static Rectangle Union(Rectangle a, Rectangle b)
Expand All @@ -98,7 +110,7 @@ public static Rectangle Union(Rectangle a, Rectangle b)

return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}

public bool Contains(int x, int y)
{
return X <= x && x < X + Width && Y <= y && y < Y + Height;
Expand All @@ -117,8 +129,8 @@ public bool Equals(Rectangle other)
}
public override bool Equals(object obj)
{
if (obj is Rectangle == false) return false;
var rect = (Rectangle)obj;
if (!(obj is Rectangle)) return false;
var rect = (Rectangle) obj;
return rect.X == X && rect.Y == Y && rect.Width == Width && rect.Height == Height;
}
public override int GetHashCode()
Expand Down Expand Up @@ -163,8 +175,8 @@ public void Offset(int x, int y)
public override string ToString()
{
return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) +
",Width=" + Width.ToString(CultureInfo.CurrentCulture) +
",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}";
",Width=" + Width.ToString(CultureInfo.CurrentCulture) +
",Height=" + Height.ToString(CultureInfo.CurrentCulture) + "}";
}
}
}
}
10 changes: 5 additions & 5 deletions System/Windows/Forms/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void ProcessKeys(KeyEventArgs args, KeyEvents keyEventType)
}

// Raise keys on selected controls if possible.
if (Control.lastSelected != null && Control.lastSelected.IsDisposed == false)
if (Control.lastSelected != null && !Control.lastSelected.IsDisposed)
{
var keyControl = Control.lastSelected;

Expand Down Expand Up @@ -278,7 +278,7 @@ public void Update()

internal static bool ControlIsVisible(Control control)
{
if (control.Visible == false) return false;
if (!control.Visible) return false;

var co = control.uwfOffset;
var controlLocation = control.Location;
Expand Down Expand Up @@ -366,7 +366,7 @@ private static Control FindControlAt(Control currentControl, Point position)
for (int i = currentControl.Controls.Count - 1; i >= 0; i--)
{
var child = currentControl.Controls[i];
if (child.Visible == false || child.Enabled == false) continue;
if (!child.Visible || !child.Enabled) continue;

// Base child bounds.
var childOffset = child.uwfOffset;
Expand Down Expand Up @@ -495,7 +495,7 @@ private void RaiseKeyEvent(KeyEventArgs args, KeyEvents keyEventType, Control ke
keyControl.RaiseOnKeyDown(args);

var lastChar = KeyHelper.GetLastInputChar();
if (args.KeyCode == Keys.Space || args.KeyCode == Keys.Back || char.IsControl(lastChar) == false)
if (args.KeyCode == Keys.Space || args.KeyCode == Keys.Back || !char.IsControl(lastChar))
keyControl.RaiseOnKeyPress(new KeyPressEventArgs(lastChar));

break;
Expand Down Expand Up @@ -597,7 +597,7 @@ private void UpdateHoveredControl()
else
{
hoveredControl = controlAtMouse;
if (controlAtMouse.mouseEntered == false)
if (!controlAtMouse.mouseEntered)
{
var mclient = controlAtMouse.PointToClient(Control.MousePosition);

Expand Down
Loading

0 comments on commit 214e81f

Please sign in to comment.