summaryrefslogtreecommitdiff
path: root/Runtime/Graphics/Polygon2D.h
blob: 76af1cf3ed40b56285543b0c6251e103224a7f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include "Configuration/UnityConfigure.h"

#if ENABLE_SPRITES

#include "Runtime/Math/Vector2.h"
#include "Runtime/Utilities/dynamic_array.h"
#include "Runtime/Serialize/SerializeUtility.h"

class Sprite;

class Polygon2D
{
public:
	DECLARE_SERIALIZE_NO_PPTR (Polygon2D)
	
	typedef dynamic_array<Vector2f> TPath;
	typedef std::vector<TPath> TPaths;

	Polygon2D();

	bool IsEmpty() const { return (m_Paths.size() == 0 || m_Paths[0].size() < 3); }
	void Reset();
	void Clear() { m_Paths.clear(); }

	// Simple API
	void SetPoints (const Vector2f* points, size_t count);
	const Vector2f* GetPoints() const { return (m_Paths.size() > 0) ? m_Paths[0].data() : NULL; }
	size_t GetPointCount() const { return (m_Paths.size() > 0) ? m_Paths[0].size() : 0; }

	// Advanced API
	void SetPathCount(int pathCount);
	int GetPathCount() const { return m_Paths.size(); }
	
	void SetPath(int index, const TPath& path);
	const TPath& GetPath(int index) const { return m_Paths[index]; }
	TPath& GetPath(int index) { return m_Paths[index]; }

	void CopyFrom (const Polygon2D& paths);

	size_t GetTotalPointCount() const
	{
		size_t count = 0;
		for (size_t i = 0; i < m_Paths.size(); ++i)
			count += m_Paths[i].size();
		return count;
	}

	// Generation
	void GenerateFrom(Sprite* sprite, const Vector2f& offset, float detail, unsigned char alphaTolerance, bool holeDetection);

	// Math
	bool GetNearestPoint(const Vector2f& point, int& pathIndex, int& pointIndex, float& distance) const;
	bool GetNearestEdge(const Vector2f& point, int& pathIndex, int& pointIndex0, int& pointIndex1, float& distance, bool loop) const;

private:
	TPaths m_Paths;
};

template<class TransferFunction>
void Polygon2D::Transfer(TransferFunction& transfer)
{
	TRANSFER(m_Paths);
}

#endif //ENABLE_SPRITES