Rev 1408 |
Rev 1414 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
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 z
= mDocument
.Owner.ZoomFactor;
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* z, y
* z,
(x
+ w
) * z,
(y
+ h
) * z, transform, Document
.SHAPE_COLOR);
}
public override void RenderSelected
(SpriteTransform transform,
bool drawHandles
= true)
{
float z
= mDocument
.Owner.ZoomFactor;
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* z, y
* z,
(x
+ w
) * z,
(y
+ h
) * z, transform, Document
.SHAPE_COLOR_SELECTED);
if (drawHandles
)
{
mDocument
.RenderHandle(x, y, transform
);
mDocument
.RenderHandle((x
+ w
), y, transform
);
mDocument
.RenderHandle(x,
(y
+ h
), transform
);
mDocument
.RenderHandle((x
+ w
),
(y
+ h
), transform
);
}
}
public override int GetNumHandles
() { return NUM_HANDLES
; }
public override int FindHandleAt
(float px,
float py
)
{
UpdateHandles
();
for (int i
= 0; i
< NUM_HANDLES
; i
++)
{
if (IsInsideHandle
(px, py, mHandles
[i
].x, mHandles
[i
].y))
return i
;
}
return -1;
}
public override bool IsInside
(float px,
float py
)
{
/*SpriteTransform t = mDocument.Sprite.transform;
Vector2 topLeft = t.spriteToWorld(x, y);
Vector2 bottomRight = t.spriteToWorld((x+w), (y+h));*/
px
-= mDocument
.Sprite.transform.x;
py
-= mDocument
.Sprite.transform.y;
px
/= mDocument
.Owner.ZoomFactor;
py
/= mDocument
.Owner.ZoomFactor;
return ((px
>= x
) && (px
<= (x
+ w
)) && (py
>= y
) && (py
<= (y
+ h
)));
/*if ((px >= topLeft.x) && (px <= bottomRight.x) &&
(py >= topLeft.y) && (py <= bottomRight.y))
return true;*/
}
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 Cursor GetCursor
(int handle
)
{
if (handle
== 0)
return Cursors
.SizeNWSE;
else if (handle
== 1)
return Cursors
.SizeNESW;
else if (handle
== 2)
return Cursors
.SizeNESW;
else if (handle
== 3)
return Cursors
.SizeNWSE;
return Cursors
.Arrow;
}
/*
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 px,
float py,
float handleX,
float handleY
)
{
px
-= mDocument
.Sprite.transform.x;
py
-= mDocument
.Sprite.transform.y;
px
/= mDocument
.Owner.ZoomFactor;
py
/= mDocument
.Owner.ZoomFactor;
if ((Math
.Abs(handleX
- px
) < Document
.HANDLE_SIZE / 2) && (Math
.Abs(handleY
- py
) < Document
.HANDLE_SIZE / 2))
return true;
return false;
/*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
();
dx
/= mDocument
.Owner.ZoomFactor;
dy
/= mDocument
.Owner.ZoomFactor;
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
;
}
mDocument
.SetDirty();
UpdateName
();
}
public override void DragMove
(float dx,
float dy
)
{
x
+= dx
/ mDocument
.Owner.ZoomFactor;
y
+= dy
/ mDocument
.Owner.ZoomFactor;
mDocument
.SetDirty();
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
;
}
}
}
}