Laxkit  0.0.7.1
pathinterface.h
1 //
2 //
3 // The Laxkit, a windowing toolkit
4 // Please consult http://laxkit.sourceforge.net about where to send any
5 // correspondence about this software.
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 // Copyright (C) 2004-2007,2010-2012 by Tom Lechner
22 //
23 #ifndef _LAX_PATHINTERFACE_H
24 #define _LAX_PATHINTERFACE_H
25 
26 
27 #include <lax/interfaces/viewportwindow.h>
28 #include <lax/interfaces/coordinate.h>
29 #include <lax/interfaces/somedata.h>
30 #include <lax/interfaces/linestyle.h>
31 #include <lax/interfaces/fillstyle.h>
32 #include <lax/screencolor.h>
33 #include <lax/dump.h>
34 
35 
36 
37 namespace LaxInterfaces {
38 
39 
40 class PathOperator;
41 
42 //These are added to Coordinate::flags, and devs should ensure they do not conflict with normal flags
43 #define BEZ_MASK (255<<16)
44 #define BEZ_STIFF_EQUAL (1<<16)
45 #define BEZ_STIFF_NEQUAL (1<<17)
46 #define BEZ_NSTIFF_EQUAL (1<<18)
47 #define BEZ_NSTIFF_NEQUAL (1<<19)
48 
49  // NOTE that the FILL_* is basically an enum, not a bunch of flags
50 enum PathFillType {
51  FILL_NONE,
52  FILL_NONZERO,
53  FILL_ZERO,
54  FILL_EVEN_ODD,
55  FILL_ODD_EVEN
56 };
57 
58 
59 //-------------------- Path ---------------------------
60 
62 {
63  protected:
64  public:
66 
67  Coordinate *path; // path is not necessarily the head, but is a vertex
68  LineStyle *linestyle;
69 
70  Path();
71  Path(Coordinate *np,LineStyle *nls=NULL);
72  virtual ~Path();
73  virtual Path *duplicate();
74 
75  //building functions
76  virtual void fixpath();
77  virtual Coordinate *lastPoint(int v=0);
78  virtual void append(double x,double y,unsigned long flags=POINT_VERTEX,SegmentControls *ctl=NULL);
79  virtual void append(flatpoint p,unsigned long flags=POINT_VERTEX,SegmentControls *ctl=NULL);
80  virtual void append(Coordinate *coord);
81  virtual void appendBezFromStr(const char *value);
82  virtual void moveTo(flatpoint p);
83  virtual void lineTo(flatpoint p);
84  virtual void curveTo(flatpoint c1, flatpoint c2, flatpoint p2);
85  virtual void close();
86  virtual void clear();
87 
88  //info functions
89  virtual int Intersect(flatpoint p1,flatpoint p2, int isline, double startt, flatpoint *pts,int ptsn, double *t,int tn);
90  virtual int PointAlongPath(double t, int tisdistance, flatpoint *point, flatpoint *tangent);
91  virtual flatpoint ClosestPoint(flatpoint point, double *disttopath, double *distalongpath, double *tdist);
92  virtual int Reverse();
93  virtual double Length(double tstart,double tend);
94  virtual double distance_to_t(double distance, int *err);
95  virtual double t_to_distance(double t, int *err);
96 
97  virtual void dump_out(FILE *f,int indent,int what,Laxkit::anObject *context);
98  virtual void dump_in_atts(LaxFiles::Attribute *att,int flag,Laxkit::anObject *context);
99 };
100 
101 
102 //-------------------- PathsData ---------------------------
103 
104 
105 
106 class PathsData : virtual public SomeData
107 {
108  protected:
109  public:
110  unsigned long style; // contains FILL_* for combining(?)
114 
115  PathsData(unsigned long ns=FILL_NONZERO);
116  virtual ~PathsData();
117  virtual const char *whattype() { return "PathsData"; }
118  virtual void FindBBox();
119  virtual SomeData *duplicate(SomeData *dup);
120 
121  virtual int line(double width,int cap=-1,int join=-1,Laxkit::ScreenColor *color=NULL);
122  virtual int fill(Laxkit::ScreenColor *color);
123 
124  virtual int hasCoord(Coordinate *co);
125  virtual int pathHasCoord(int pathindex,Coordinate *co);
126  virtual void appendCoord(Coordinate *coord,int whichpath=-1);
127  virtual void append(double x,double y,unsigned long flags=POINT_VERTEX,SegmentControls *ctl=NULL,int whichpath=-1);
128  virtual void append(flatpoint p,unsigned long flags=POINT_VERTEX,SegmentControls *ctl=NULL,int whichpath=-1);
129  virtual void appendRect(double x,double y,double w,double h,SegmentControls *ctl=NULL,int whichpath=-1);
130  virtual void appendBezArc(flatpoint center, double angle, int num_vertices);
131  virtual void moveTo(flatpoint p,int whichpath=-1);
132  virtual void lineTo(flatpoint p,int whichpath=-1);
133  virtual void curveTo(flatpoint c1, flatpoint c2, flatpoint p2, int whichpath=-1);
134  virtual void close(int whichpath=-1);
135  virtual Coordinate *LastVertex();
136  virtual void pushEmpty(int where=-1,LineStyle *nls=NULL);
137  virtual void InstallLineStyle(LineStyle *newlinestyle);
138  virtual void fixpath(int index);
139 // virtual int AddAfter(Coordinate *afterwhich,flatpoint p);
140 // virtual int Delete(Coordinate *which); // returns num left in stack
141  virtual void clear(int which=-1);
142  virtual void ApplyTransform();
143  virtual void MatchTransform(Affine &affine);
144  virtual void MatchTransform(const double *mm);
145 
146  virtual flatpoint ClosestPoint(flatpoint point, double *disttopath, double *distalongpath, double *tdist, int *pathi);
147  virtual int Intersect(int pathindex,flatpoint p1,flatpoint p2, int isline, double startt,flatpoint *pts,int ptsn, double *t,int tn);
148  virtual int PointAlongPath(int pathindex, double t, int tisdistance, flatpoint *point, flatpoint *tangent);
149  virtual Coordinate *GetCoordinate(int pathi, double t);
150  virtual int ReversePath(int pathindex);
151  virtual double Length(int pathi, double tstart,double tend);
152  //virtual int NumPoints(int vertices=1);
153 
154  virtual void dump_out(FILE *f,int indent,int what,Laxkit::anObject *context);
155  virtual void dump_in_atts(LaxFiles::Attribute *att,int flag,Laxkit::anObject *context);
156 };
157 
158 
159 //-------------------- PathsData utility
160 
161 PathsData *SvgToPathsData(PathsData *existingpath, const char *d,char **end_ptr);
162 
163 
164 //-------------------- PathOperator ---------------------------
165 
166 
167 class PathInterface;
168 
170 {
171  public:
172  int id;
173 
174  PathOperator(int nid=-1);
175  virtual ~PathOperator();
176  virtual void dumpOut(FILE *f, int indent, SegmentControls *controls, int what, Laxkit::anObject *context) = 0;
177  virtual void dumpIn(Coordinate *attachto, LaxFiles::Attribute *att, Laxkit::anObject *context) = 0;
178 
179  virtual void drawControls(Laxkit::Displayer *dp, int which, SegmentControls *controls,
180  int showdecs, PathInterface *pathi) = 0;
181 
182  //return an interface that can manipulate relevant data
183  virtual anInterface *getInterface(Laxkit::Displayer *dp,PathsData *data) = 0;
184 
185  //return a thing id for yes, there is something at screen coordinate (x,y), or 0 for none
186  virtual int scan(Laxkit::Displayer *dp,SegmentControls *ctl,double x,double y) = 0;
187 
188  // Return a new point and associated points. It must return
189  // the most previous point in the group. This gets appended to whatever.
190  // It must not return closed loops. By comparison, a new bez point would create 3
191  // points: control1--vertex--control2, and would return control1.
192  // The returned segment must start and end with a vertex.
193  virtual Coordinate *newPoint(flatpoint p) = 0; // pop after/before, prefunit=0 means full unit
194 
195  virtual int ShiftPoint(Coordinate *pp,flatpoint d) = 0;
196  virtual int Rotate(flatpoint o,double angle,Coordinate *pp) = 0;
197  virtual int Scale(flatpoint o,double f,int contsrain, Coordinate *pp) = 0;
198 
199  //virtual int UseThis(***);
200 };
201 
202 
203 
204 
205 //-------------------- PathInterface ---------------------------
206 
207 enum PathInterfaceSettings {
208  PATHI_One_Path_Only =(1<<0),
209  PATHI_Two_Point_Minimum=(1<<1),
210  PATHI_Path_Is_Screen =(1<<2),
211  PATHI_Path_Is_M_Screen =(1<<3),
212  PATHI_Path_Is_Real =(1<<4),
213  PATHI_Path_Is_M_Real =(1<<5),
214  PATHI_Esc_Off_Sub =(1<<6),
215  PATHI_Plain_Click_Add =(1<<7)
216 };
217 
218 enum PathInterfaceActions {
219  PATHIA_CurpointOnHandle,
220  PATHIA_CurpointOnHandleR,
221  PATHIA_Pathop,
222  PATHIA_ToggleFillRule,
223  PATHIA_ToggleFill,
224  PATHIA_ToggleStroke,
225  PATHIA_ColorFillOrStroke,
226  PATHIA_RollNext,
227  PATHIA_RollPrev,
228  PATHIA_ToggleAddAfter,
229  PATHIA_TogglePointType,
230  PATHIA_Select,
231  PATHIA_SelectInPath,
232  PATHIA_Close,
233  PATHIA_Decorations,
234  PATHIA_StartNewPath,
235  PATHIA_StartNewSubpath,
236  PATHIA_Wider,
237  PATHIA_Thinner,
238  PATHIA_WidthStep,
239  PATHIA_WidthStepR,
240  PATHIA_MakeCircle,
241  PATHIA_MakeRect,
242  PATHIA_Reverse,
243  PATHIA_Delete,
244  PATHIA_Combine,
245  PATHIA_ExtractPath,
246  PATHIA_ExtractAll,
247  PATHIA_Copy,
248  PATHIA_Cut,
249  PATHIA_Paste,
250 
251  PATHIA_MAX
252 };
253 
255 {
256  protected:
257  class SelectedPoint //prep for actions on multiple pathsdata objects
258  {
259  public:
260  ObjectContext *context;
261  PathsData *paths;
262  Coordinate *point;
263  int pathindex;
264  int pointindex;
265  };
266 
267  //PathInterface potentially non-local state
268  int addmode;
269  int editmode;
270  LineStyle *linestyle,*defaultline;
271  FillStyle *fillstyle,*defaultfill;
272 
273  //other state
274  int constrain;
275  int addafter;
276  int colortofill;
277  double widthstep;
278 
279  Path *curpath;
280  flatpoint curdirv;
281  Coordinate *curdirp;
282  Coordinate defaulthoverp;
283  Coordinate *curvertex; // curvertex points to the relevant vertex point, not to the last selected point
284  Coordinate *lbfound;
285  int lbselected;
286  int drawhover;
287  int hoverdevice;
288  int hoverpointtype;
289  flatpoint hoverpoint;
290  flatpoint hoversegment[4];
291  int lasth; //direction of toggling selected handle
292 
293  //Laxkit::PtrStack<PathOperator> pathops;
294  PathOperator *curpathop;
295 
297  virtual int PerformAction(int action);
298 
299  virtual void clearSelection();
300  virtual PathOperator *getPathOpFromId(int iid);
301  virtual PathOperator *getPathOp(Coordinate *p);
302  virtual void selectPoint(Coordinate *p,char flag);
303  virtual void removeSegment(Coordinate *c);
304  virtual Coordinate *scannear(Coordinate *p,char u,double radius=5);
305  virtual void SetCurvertex(Coordinate *p, int path=-1);
306  virtual void UpdateDir();
307  virtual int ConnectEndpoints(Coordinate *from,int fromi, Coordinate *to,int toi);
308  virtual int MergeEndpoints(Coordinate *from,int fromi, Coordinate *to,int toi);
309 
310  virtual int shiftBezPoint(Coordinate *p,flatpoint d);
311  virtual int shiftSelected(flatpoint d);
312  virtual int scaleSelected(flatpoint center,double f,int constrain);
313  virtual int rotateSelected(flatpoint center,double angle);
314 
315  virtual void Modified(int level);
316  virtual void hoverMessage();
317  virtual void drawNewPathIndicator(flatpoint p,int which);
318  public:
319  // the following three comprise the default PathInterface settings.
320  unsigned long controlcolor;
321  unsigned long creationstyle;
322  unsigned long pathi_style;
323 
324  Laxkit::PtrStack<Coordinate> curpoints; //*** how make this nondestructive/undoable changes?
325 
326  int showdecs;
327  int verbose;
328  PathsData *data;
329  ObjectContext *poc;
330 
331  PathInterface(int nid,Laxkit::Displayer *ndp);
332  virtual ~PathInterface();
333  virtual Laxkit::ShortcutHandler *GetShortcuts();
334  virtual anInterface *duplicate(anInterface *dup);
335  virtual PathsData *newPathsData();
336 
337  // from anInterface:
338  virtual const char *IconId() { return "Path"; }
339  virtual const char *Name();
340  virtual const char *whattype() { return "PathInterface"; }
341  virtual const char *whatdatatype() { return "PathsData"; }
342  virtual void Dp(Laxkit::Displayer *ndp);
343  virtual int DrawDataDp(Laxkit::Displayer *ndp,SomeData *ndata,
344  Laxkit::anObject *a1=NULL,Laxkit::anObject *a2=NULL,int info=0);
345  virtual int DrawData(Laxkit::anObject *ndata, Laxkit::anObject *a1=NULL,Laxkit::anObject *a2=NULL,int info=0);
346  virtual ObjectContext *Context() { return poc; }
347  virtual int Refresh();
348  virtual int UseThisObject(ObjectContext *oc);
349  virtual int UseThis(Laxkit::anObject *newdata,unsigned int mask=0);
350  virtual int InterfaceOn();
351  virtual int InterfaceOff();
352  virtual void Clear(SomeData *d=NULL);
353  virtual int LBDown(int x,int y,unsigned int state,int count,const Laxkit::LaxMouse *d);
354  virtual int LBUp(int x,int y,unsigned int state,const Laxkit::LaxMouse *d);
355  virtual int MouseMove(int x,int y,unsigned int state,const Laxkit::LaxMouse *mouse);
356  virtual int WheelUp(int x,int y,unsigned int state,int count,const Laxkit::LaxMouse *d);
357  virtual int WheelDown(int x,int y,unsigned int state,int count,const Laxkit::LaxMouse *d);
358  virtual int CharInput(unsigned int ch, const char *buffer,int len,unsigned int state,const Laxkit::LaxKeyboard *d);
359  virtual int KeyUp(unsigned int ch,unsigned int state, const Laxkit::LaxKeyboard *kb);
360  virtual Laxkit::MenuInfo *ContextMenu(int x,int y,int deviceid);
361 
362  //virtual void RegisterOp(PathOperator *apathop);
363  virtual int ChangeCurpathop(int newiid);
364 
365  virtual int DeleteCurpoints();
366  virtual int DeletePoint(Coordinate *p);
367  virtual void deletedata();
368  virtual Coordinate *scan(int x,int y,int pmask=0, int *pathindex=NULL);
369  virtual Coordinate *scanEndpoints(int x,int y,int *pathindex,Coordinate *exclude);
370  virtual int scanHover(int x,int y,unsigned int state);
371  virtual flatpoint screentoreal(int x,int y);
372  virtual flatpoint realtoscreen(flatpoint r);
373  virtual int toggleclosed(int c=-1);
374  virtual int AddPoint(flatpoint p);
375  virtual void SetPointType(int newtype);
376  virtual void SetPointType(Coordinate *v,int newtype);
377  virtual void MakeCircle();
378  virtual void MakeRect();
379  virtual int CutNear(flatpoint hoverpoint);
380 };
381 
382 
383 } // namespace LaxInterfaces
384 
385 
386 #endif
387 

Mon Feb 17 2014 11:52:57, Laxkit