xref: /haiku/src/libs/icon/shape/VectorPath.h (revision 1564b19c80637a2ba13949db0056215699b7b418)
1 /*
2  * Copyright 2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #ifndef VECTOR_PATH_H
10 #define VECTOR_PATH_H
11 
12 #include <Rect.h>
13 #include <String.h>
14 
15 #include <agg_path_storage.h>
16 
17 #ifdef ICON_O_MATIC
18 # include <Archivable.h>
19 # include <List.h>
20 
21 # include "IconObject.h"
22 #endif // ICON_O_MATIC
23 
24 class BBitmap;
25 class BMessage;
26 class BView;
27 
28 struct control_point {
29 	BPoint		point;		// actual point on path
30 	BPoint		point_in;	// control point for incomming curve
31 	BPoint		point_out;	// control point for outgoing curve
32 	bool		connected;	// if all 3 points should be on one line
33 };
34 
35 #ifdef ICON_O_MATIC
36 class PathListener {
37  public:
38 								PathListener();
39 	virtual						~PathListener();
40 
41 	virtual	void				PointAdded(int32 index) = 0;
42 	virtual	void				PointRemoved(int32 index) = 0;
43 	virtual	void				PointChanged(int32 index) = 0;
44 	virtual	void				PathChanged() = 0;
45 	virtual	void				PathClosedChanged() = 0;
46 	virtual	void				PathReversed() = 0;
47 };
48 
49 class VectorPath : public BArchivable,
50 				   public IconObject {
51 #else
52 class VectorPath {
53 #endif // ICON_O_MATIC
54  public:
55 
56 	class Iterator {
57 	 public:
58 								Iterator() {}
59 		virtual					~Iterator() {}
60 
61 		virtual	void			MoveTo(BPoint point) = 0;
62 		virtual	void			LineTo(BPoint point) = 0;
63 	};
64 
65 								VectorPath();
66 								VectorPath(const VectorPath& from);
67 #ifdef ICON_O_MATIC
68 								VectorPath(BMessage* archive);
69 #endif
70 	virtual						~VectorPath();
71 
72 #ifdef ICON_O_MATIC
73 	// IconObject
74 	virtual	status_t			Archive(BMessage* into,
75 										bool deep = true) const;
76 
77 	virtual	PropertyObject*		MakePropertyObject() const;
78 	virtual	bool				SetToPropertyObject(
79 									const PropertyObject* object);
80 #else
81 	inline	void				Notify() {}
82 #endif // ICON_O_MATIC
83 
84 	// VectorPath
85 			VectorPath&			operator=(const VectorPath& from);
86 //			bool				operator==(const VectorPath& frrom) const;
87 
88 			void				MakeEmpty();
89 
90 			bool				AddPoint(BPoint point);
91 			bool				AddPoint(const BPoint& point,
92 										 const BPoint& pointIn,
93 										 const BPoint& pointOut,
94 										 bool connected);
95 			bool				AddPoint(BPoint point, int32 index);
96 
97 			bool				RemovePoint(int32 index);
98 
99 								// modify existing points position
100 			bool				SetPoint(int32 index, BPoint point);
101 			bool				SetPoint(int32 index, BPoint point,
102 													  BPoint pointIn,
103 													  BPoint pointOut,
104 													  bool connected);
105 			bool				SetPointIn(int32 index, BPoint point);
106 			bool				SetPointOut(int32 index, BPoint point,
107 										   bool mirrorDist = false);
108 
109 			bool				SetInOutConnected(int32 index, bool connected);
110 
111 								// query existing points position
112 			bool				GetPointAt(int32 index, BPoint& point) const;
113 			bool				GetPointInAt(int32 index, BPoint& point) const;
114 			bool				GetPointOutAt(int32 index, BPoint& point) const;
115 			bool				GetPointsAt(int32 index,
116 											BPoint& point,
117 											BPoint& pointIn,
118 											BPoint& pointOut,
119 											bool* connected = NULL) const;
120 
121 			int32				CountPoints() const;
122 
123 #ifdef ICON_O_MATIC
124 								// iterates over curve segments and returns
125 								// the distance and index of the point that
126 								// started the segment that is closest
127 			bool				GetDistance(BPoint point,
128 											float* distance, int32* index) const;
129 
130 								// at curve segment indicated by "index", this
131 								// function looks for the closest point
132 								// directly on the curve and returns a "scale"
133 								// that indicates the distance on the curve
134 								// between [0..1]
135 			bool				FindBezierScale(int32 index, BPoint point,
136 												double* scale) const;
137 								// this function can be used to get a point
138 								// directly on the segment indicated by "index"
139 								// "scale" is on [0..1] indicating the distance
140 								// from the start of the segment to the end
141 			bool				GetPoint(int32 index, double scale,
142 										 BPoint& point) const;
143 #endif // ICON_O_MATIC
144 
145 			void				SetClosed(bool closed);
146 			bool				IsClosed() const
147 									{ return fClosed; }
148 
149 			BRect				Bounds() const;
150 			BRect				ControlPointBounds() const;
151 
152 			void				Iterate(Iterator* iterator,
153 										float smoothScale = 1.0) const;
154 
155 			void				CleanUp();
156 			void				Reverse();
157 
158 			void				PrintToStream() const;
159 
160 			bool				GetAGGPathStorage(agg::path_storage& path) const;
161 
162 #ifdef ICON_O_MATIC
163 			bool				AddListener(PathListener* listener);
164 			bool				RemoveListener(PathListener* listener);
165 			int32				CountListeners() const;
166 			PathListener*		ListenerAtFast(int32 index) const;
167 #endif // ICON_O_MATIC
168 
169 
170  private:
171 			BRect				_Bounds() const;
172 			void				_SetPoint(int32 index, BPoint point);
173 			void				_SetPoint(int32 index,
174 										  const BPoint& point,
175 										  const BPoint& pointIn,
176 										  const BPoint& pointOut,
177 										  bool connected);
178 			bool				_SetPointCount(int32 count);
179 
180 #ifndef ICON_O_MATIC
181 	inline	void				_NotifyPointAdded(int32 index) const {}
182 	inline	void				_NotifyPointChanged(int32 index) const {}
183 	inline	void				_NotifyPointRemoved(int32 index) const {}
184 	inline	void				_NotifyPathChanged() const {}
185 	inline	void				_NotifyClosedChanged() const {}
186 	inline	void				_NotifyPathReversed() const {}
187 #else
188 			void				_NotifyPointAdded(int32 index) const;
189 			void				_NotifyPointChanged(int32 index) const;
190 			void				_NotifyPointRemoved(int32 index) const;
191 			void				_NotifyPathChanged() const;
192 			void				_NotifyClosedChanged() const;
193 			void				_NotifyPathReversed() const;
194 
195 			BList				fListeners;
196 #endif // ICON_O_MATIC
197 
198 			control_point*		fPath;
199 			bool				fClosed;
200 
201 			int32				fPointCount;
202 			int32				fAllocCount;
203 
204 	mutable	BRect				fCachedBounds;
205 };
206 
207 #endif // VECTOR_PATH_H
208 
209