using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using BauzoidNET.graphics;
using BauzoidNET.graphics.sprite;
using BauzoidNET.math;
namespace ShapeEditor
.file.shapes
{
public class RectangleElement
: BaseShapeElement
{
public const int NUM_HANDLES
= 4;
private Vector2
[] mHandles
= new Vector2
[NUM_HANDLES
];
public float x
= 0;
public float y
= 0;
public float w
= 0;
public float h
= 0;
public RectangleElement
(Document doc,
string name,
float _x,
float _y,
float _w,
float _h
) : base(doc, name
)
{
x
= _x
; y
= _y
; w
= _w
; h
= _h
;
for (int i
= 0; i
< NUM_HANDLES
; i
++)
mHandles
[i
] = new Vector2
();
}
public override void Render
(SpriteTransform transform,
float zoom
)
{
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* zoom, y
* zoom,
(x
+ w
) * zoom,
(y
+ h
) * zoom, transform, Document
.SHAPE_COLOR);
}
public override void RenderSelected
(SpriteTransform transform,
float zoom,
bool drawHandles
= true)
{
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* zoom, y
* zoom,
(x
+ w
) * zoom,
(y
+ h
) * zoom, transform, Document
.SHAPE_COLOR_SELECTED);
if (drawHandles
)
{
mDocument
.RenderHandle(x
* zoom, y
* zoom, transform
);
mDocument
.RenderHandle((x
+ w
) * zoom, y
* zoom, transform
);
mDocument
.RenderHandle(x
* zoom,
(y
+ h
) * zoom, transform
);
mDocument
.RenderHandle((x
+ w
) * zoom,
(y
+ h
) * zoom, transform
);
}
}
public override int GetNumHandles
() { return NUM_HANDLES
; }
public override int FindHandleAt
(int px,
int py
)
{
UpdateHandles
();
for (int i
= 0; i
< NUM_HANDLES
; i
++)
{
if (IsInsideHandle
(px, py, mHandles
[i
].x * mDocument
.Owner.ZoomFactor, mHandles
[i
].y * mDocument
.Owner.ZoomFactor))
return i
;
}
return -1;
}
public override bool IsInside
(int px,
int py
)
{
SpriteTransform t
= mDocument
.Sprite.transform;
Vector2 topLeft
= t
.spriteToWorld(x
* mDocument
.Owner.ZoomFactor, y
* mDocument
.Owner.ZoomFactor);
Vector2 bottomRight
= t
.spriteToWorld((x
+w
) * mDocument
.Owner.ZoomFactor,
(y
+h
) * mDocument
.Owner.ZoomFactor);
if ((px
>= topLeft
.x) && (px
<= bottomRight
.x) &&
(py
>= topLeft
.y) && (py
<= bottomRight
.y))
return true;
return false;
}
public void UpdateHandles
()
{
mHandles
[0].set(x, y
);
mHandles
[1].set(x
+ w, y
);
mHandles
[2].set(x, y
+ h
);
mHandles
[3].set(x
+ w, y
+ h
);
}
/*
public override bool IsInsideHandle(int testX, int testY, SpriteTransform transform, float zoom)
{
if (IsInsideHandle(testX, testY, x * zoom, y * zoom, transform))
return true;
if (IsInsideHandle(testX, testY, (x + w) * zoom, y * zoom, transform))
return true;
if (IsInsideHandle(testX, testY, x * zoom, (y + h) * zoom, transform))
return true;
if (IsInsideHandle(testX, testY, (x + w) * zoom, (y + h) * zoom, transform))
return true;
return false;
}*/
public bool IsInsideHandle
(float x,
float y,
float handleX,
float handleY
)
{
SpriteTransform t
= mDocument
.Sprite.transform;
Vector2 p
= t
.spriteToWorld(handleX, handleY
);
if ((Math
.Abs(p
.x - x
) < Document
.HANDLE_SIZE / 2) && (Math
.Abs(p
.y - y
) < Document
.HANDLE_SIZE / 2))
return true;
return false;
}
public override void DragHandle
(int handle,
float dx,
float dy
)
{
UpdateHandles
();
if (handle
== 0)
{
// upper left
x
+= dx
; w
-= dx
;
y
+= dy
; h
-= dy
;
}
else if (handle
== 1)
{
// upper right
w
+= dx
;
y
+= dy
; h
-= dy
;
}
else if (handle
== 2)
{
// lower left
x
+= dx
; w
-= dx
;
h
+= dy
;
}
else if (handle
== 3)
{
// lower right
w
+= dx
;
h
+= dy
;
}
UpdateName
();
}
public override void DragMove
(float dx,
float dy
)
{
x
+= dx
;
y
+= dy
;
UpdateName
();
}
public void UpdateName
()
{
ShapeName
= "rect " + Math
.Min(x, x
+ w
) + ", " + Math
.Min(y, y
+ h
) + ", " + Math
.Abs(w
) + ", " + Math
.Abs(h
);
//mDocument.Owner.ElementsListBox.RefreshItems();
typeof(ListBox
).InvokeMember("RefreshItems", BindingFlags
.NonPublic | BindingFlags
.Instance | BindingFlags
.InvokeMethod,
null, mDocument
.Owner.ElementsListBox,
new object[] { });
}
public override void FixCoordinates
()
{
if (w
< 0)
{
x
= x
+ w
;
w
= -w
;
}
if (h
< 0)
{
y
= y
+ h
;
h
= -h
;
}
}
}
}