Rev 1416 |
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 System.ComponentModel;
using System.Globalization;
using System.Threading;
using BauzoidNET.graphics;
using BauzoidNET.graphics.sprite;
using BauzoidNET.math;
using OrderedPropertyGrid;
namespace ShapeEditor
.file.shapes
{
[TypeConverter
(typeof(PropertySorter
))]
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;
[Category
("Coordinates"), PropertyOrder
(10)]
public float Left
{
get
{ return x
; }
set
{ x
= value
; }
}
[Category
("Coordinates"), PropertyOrder
(11)]
public float Top
{
get
{ return y
; }
set
{ y
= value
; }
}
[Category
("Coordinates"), PropertyOrder
(12)]
public float Width
{
get
{ return w
; }
set
{ w
= value
; }
}
[Category
("Coordinates"), PropertyOrder
(13)]
public float Height
{
get
{ return h
; }
set
{ h
= value
; }
}
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;
if (IsInFrameRange
(mDocument
.CurrentFrame))
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* z, y
* z,
(x
+ w
) * z,
(y
+ h
) * z, transform, Document
.SHAPE_COLOR);
else
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* z, y
* z,
(x
+ w
) * z,
(y
+ h
) * z, transform, Document
.SHAPE_COLOR_NOT_IN_FRAME_RANGE);
}
public override void RenderSelected
(SpriteTransform transform,
bool drawHandles
= true)
{
float z
= mDocument
.Owner.ZoomFactor;
if (IsInFrameRange
(mDocument
.CurrentFrame))
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* z, y
* z,
(x
+ w
) * z,
(y
+ h
) * z, transform, Document
.SHAPE_COLOR_SELECTED);
else
RenderUtil
.drawQuad(MainForm
.App.getGraphics(), x
* z, y
* z,
(x
+ w
) * z,
(y
+ h
) * z, transform, Document
.SHAPE_COLOR_NOT_IN_FRAME_RANGE);
if (drawHandles
)
{
for (int i
= 0; i
< NUM_HANDLES
; i
++)
mDocument
.RenderHandle(mHandles
[i
].x, mHandles
[i
].y, transform
);
/*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
)
{
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
)));
}
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 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;
}
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();
UpdateHandles
();
UpdateName
();
}
public override void DragMove
(float dx,
float dy
)
{
x
+= dx
/ mDocument
.Owner.ZoomFactor;
y
+= dy
/ mDocument
.Owner.ZoomFactor;
mDocument
.SetDirty();
UpdateHandles
();
UpdateName
();
}
public override string GetName
()
{
return GetFrameRangeStr
() + "rect " + Math
.Min(x, x
+ w
) + ", " + Math
.Min(y, y
+ h
) + ", " + Math
.Abs(w
) + ", " + Math
.Abs(h
);
}
public override void UpdateName
()
{
ShapeName
= GetName
();
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
;
}
}
public override void WriteFile
(System.IO.TextWriter tw
)
{
Thread
.CurrentThread.CurrentCulture = CultureInfo
.InvariantCulture;
base.WriteFrameRange(tw
);
tw
.WriteLine("rect " + x
.ToString() + ", " + y
.ToString() + ", " + w
.ToString() + ", " + h
.ToString() + ";");
/*tw.WriteLine("rect " + x.ToString(NumberFormatInfo.InvariantInfo) + ", " + y.ToString(NumberFormatInfo.InvariantInfo) + ", " +
w.ToString(NumberFormatInfo.InvariantInfo) + ", " + h.ToString(NumberFormatInfo.InvariantInfo) + ";");*/
}
}
}