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