Planeshift
pawsilluminationwindow.h
Go to the documentation of this file.
1 /*
2  * pawsSketchWindow.h - Author: Keith Fulton
3  *
4  * Copyright (C) 2006 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef PAWS_SKETCH_WINDOW_HEADER
21 #define PAWS_SKETCH_WINDOW_HEADER
22 
23 #include "net/cmdbase.h"
24 #include "paws/pawswidget.h"
27 
28 #define SKETCHBEZIER_SEGMENTS 20
29 
34 {
35  class BezierWeights
36  {
37  private:
38  float *precomputedBezierWeights; //[SKETCHBEZIER_SEGMENTS+1][4];
39  public:
40  BezierWeights();
41  ~BezierWeights();
42 
43  // this is SLOWER than Get(int offset)
44  inline float Get(int segmentIndex, int controlpointNum)
45  {
46  //CS_ASSERT(segmentIndex <= SKETCHBEZIER_SEGMENTS && segmentIndex >= 0);
47  //CS_ASSERT(controlpointNum >= 0 && controlpointNum <= 3);
48  return precomputedBezierWeights[segmentIndex * SKETCHBEZIER_SEGMENTS + controlpointNum];
49  }
50 
51  // returns the precomputed value of "segmentIndex*SKETCHBEZIER_SEGMENTS + controlpointNum"
52  inline float Get(int offset)
53  {
54  //CS_ASSERT(offset <= (SKETCHBEZIER_SEGMENTS+1)*4 && offset >= 0);
55  return precomputedBezierWeights[offset];
56  }
57  };
58  struct SketchObject
59  {
60  bool selected;
61  int x,y;
62  csString str;
63  int frame;
64  int color;
65  bool colorSet;
66 
68 
69  const char *GetStr() { return str; }
70  virtual void SetStr(const char *s) { str=s; }
71  SketchObject() { x=0; y=0; selected=false; color=-1; colorSet=false; }
72  virtual ~SketchObject() {};
73 
74  virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent)=0;
75  virtual void WriteXml(csString& xml) = 0;
76  virtual void Draw()=0;
77  virtual bool IsHit(int mouseX, int mouseY)=0;
78  virtual void Select(bool _selected) { selected=_selected; colorSet=false; frame=0; }
79  // update the RELATIVE position
80  virtual void UpdatePosition(int _x, int _y) { x = _x; y = _y; }
81  virtual void SetColor(int _color) { color = _color; colorSet=true; }
82  };
83  struct SketchIcon : public SketchObject
84  {
85  csRef<iPawsImage> iconImage;
86  SketchIcon() : SketchObject() {}
87  SketchIcon(int _x, int _y, const char *icon, pawsSketchWindow *parent);
88  virtual ~SketchIcon() {};
89  bool Init(int _x, int _y, const char *icon, pawsSketchWindow *parent);
90  virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
91  virtual void WriteXml(csString& xml);
92  virtual void Draw();
93  virtual bool IsHit(int mouseX, int mouseY);
94  virtual void SetStr(const char *s);
95  // update the RELATIVE position
96  virtual void UpdatePosition(int _x, int _y);
97  };
98  struct SketchText : public SketchObject
99  {
100  csString font;
101 
102  SketchText(int _x,int _y,const char *value,pawsSketchWindow *_parent)
103  {
104  x = _x;
105  y = _y;
106  str = value;
107  parent = _parent;
108  }
109  SketchText() {}
110  virtual ~SketchText() {};
111  virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
112  virtual void WriteXml(csString& xml);
113  virtual void Draw();
114  virtual bool IsHit(int mouseX, int mouseY);
115  // update the RELATIVE position
116  virtual void UpdatePosition(int _x, int _y);
117  };
118  struct SketchLine : public SketchObject
119  {
120  int x2,y2;
121  int offsetX, offsetY;
122  int dragMode;
123 
124  SketchLine(int _x,int _y, int _x2, int _y2,pawsSketchWindow *_parent)
125  {
126  x=_x; y=_y;
127  x2=_x2; y2=_y2;
128  parent = _parent;
129  dragMode = 0;
130  }
131  SketchLine() {};
132  virtual ~SketchLine() {};
133  virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
134  virtual void WriteXml(csString& xml);
135  virtual void Draw();
136  virtual bool IsHit(int mouseX, int mouseY);
137  // update the RELATIVE position
138  virtual void UpdatePosition(int _x, int _y);
139  };
140  struct SketchBezier : public SketchObject
141  {
142  /* Cubic Bézier Curve
143  p1 p2 (control points)
144  / \_
145  p0-----------------p3
146 
147  2 lines would actually be enough to handle the bezier curve.
148  Connecting p0-p1 and p3-p2: However, the usability would suffer since we can't move the "whole line" then.
149  Connecting p1-p2 and p0-p3: Displaying this doesn't look good and usability suffers.
150  */
151 
152  //BezierHelper *bezierHelper;
153 
154  SketchLine p0p1;
155  SketchLine p0p3;
156  SketchLine p3p2;
157 
158  SketchBezier(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3, pawsSketchWindow *_parent)
159  {
160  this->parent = _parent;
161  //this->bezierHelper = &_parent->bezierHelper;
162 
163  p0p1.parent = _parent;
164  p0p1.x = x; // point 0 (also start point)
165  p0p1.y = y;
166  p0p1.x2 = x1; // point 1 (control point 1)
167  p0p1.y2 = y1;
168 
169  p0p3.parent = _parent;
170  p0p3.x = x; // point 0
171  p0p3.y = y;
172  p0p3.x2 = x3; // point 3 (also end point)
173  p0p3.y2 = y3;
174 
175  p3p2.parent = _parent;
176  p3p2.x2 = x2; // point 3 (also end point)
177  p3p2.y2 = y2;
178  p3p2.x = x3; // point 2 (control point 2)
179  p3p2.y = y3;
180  }
181  SketchBezier() {};
182  virtual ~SketchBezier() {};
183  virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
184  virtual void WriteXml(csString& xml);
185  virtual void Draw();
186  virtual bool IsHit(int mouseX, int mouseY);
187  // update the RELATIVE position
188  virtual void UpdatePosition(int _x, int _y);
189  };
190 
191 protected:
192  csPDelArray<SketchObject> objlist;
194  int dirty;
195  uint32_t currentItemID;
196  csRef<iPawsImage> blackBox;
197  bool editMode;
198  bool mouseDown;
199  int mouseDownX; // used to calculate the updatePosition diff. this value is updated OnMouseDown
201  csStringArray iconList;
203  bool readOnly;
206  BezierWeights bezierWeights;
207 
208  int frame; // incremented each frame till 10 (to update selection without clicking)
209 
222 
223  void DrawSketch();
224  bool ParseSketch(const char *xml, bool checklimits = false);
225  bool ParseLimits(const char *xmlstr);
226  void SetToolbarButtons(); //sets the toolbar buttons hide/show status
227 
228  void AddSketchText();
229  void AddSketchIcon();
230  void AddSketchLine();
231  void AddSketchBezier();
232  void RemoveSelected();
233  void NextPrevIcon(int delta);
234  void MoveObject(int dx, int dy);
235  void ChangeSketchName();
236  void SaveSketch();
237  void LoadSketch();
238  bool isBadChar(char c);
239 
240  csString toXML();
241 
242  csString filenameSafe(const csString &original);
243 
244  csString sketchName;
245 
247  csRef<iPawsImage> sketchBgImage;
248 
249 public:
252  pawsSketchWindow(const pawsSketchWindow& origin);
253  virtual ~pawsSketchWindow();
254 
255  bool PostSetup();
256 
257  void HandleMessage( MsgEntry* message );
258 
260  void OnStringEntered(const char *name, int param, const char *value);
261 
263  void OnColorEntered(const char *name,int param,int color);
264 
266  double CalcFunction(MathEnvironment* env, const char* functionName, const double* params);
267 
268 
269  virtual bool OnMouseDown( int button, int modifiers, int x, int y );
270  virtual bool OnMouseUp( int button, int modifiers, int x, int y );
271  virtual bool OnKeyDown( utf32_char keyCode, utf32_char key, int modifiers );
272  virtual void Hide();
273  virtual void Draw();
274 
275  // SketchObject helper functions
276  iGraphics2D *GetG2D();
277  void DrawBlackBox(int x, int y);
278  void DrawColorWidgetText(const char *text, int x, int y, int color);
279  bool IsMouseDown() { return mouseDown; }
280 
281  virtual bool GetFocusOverridesControls() const { return true; }
282 };
283 
285 
286 
287 #endif
288 
289 
csString toXML()
The main base widget that all other widgets should inherit from.
Definition: pawswidget.h:116
void HandleMessage(MsgEntry *message)
void MoveObject(int dx, int dy)
A specific MathEnvironment to be used in a MathScript.
Definition: mathscript.h:188
csRef< iPawsImage > blackBox
bool isBadChar(char c)
csString name
The name of this widget.
Definition: pawswidget.h:217
csString filenameSafe(const csString &original)
The structure of 1 queue entry (pointer to a message)
Definition: message.h:143
iGraphics2D * GetG2D()
void DrawBlackBox(int x, int y)
void OnColorEntered(const char *name, int param, int color)
inherited from iOnColorEnteredAction
void ChangeSketchName()
void NextPrevIcon(int delta)
bool ParseLimits(const char *xmlstr)
void OnStringEntered(const char *name, int param, const char *value)
inherited from iOnStringEnteredAction
virtual void Hide()
Makes widget invisible and removes focus if widget has current focus.
pawsWidget * parent
This widget&#39;s parent.
Definition: pawswidget.h:130
void AddSketchBezier()
virtual bool GetFocusOverridesControls() const
Test if the widget should intercept all key presses.
virtual bool OnMouseDown(int button, int modifiers, int x, int y)
Manage mouse down event to test for and apply window changes.
double CalcFunction(MathEnvironment *env, const char *functionName, const double *params)
inherited from iScriptableVar from pawsWidget
csRef< iPawsImage > sketchBgImage
Background image.
csPDelArray< SketchObject > objlist
virtual ~pawsSketchWindow()
bool PostSetup()
This is called after the widget and all of it&#39;s children have been created.
#define SKETCHBEZIER_SEGMENTS
void DrawColorWidgetText(const char *text, int x, int y, int color)
virtual bool Load(iDocumentNode *node)
Load a widget based on its <widget></widget> tag.
This interface defines the callback used by pawsStringPromptWindow to notify another window of a supp...
virtual bool OnMouseUp(int button, int modifiers, int x, int y)
Manage mouse up event.
void SetToolbarButtons()
virtual void Draw()
Draws the widget and all of it&#39;s children.
CREATE_PAWS_FACTORY(pawsSketchWindow)
virtual bool OnKeyDown(utf32_char keyCode, utf32_char key, int modifiers)
Process keydown messages.
bool ParseSketch(const char *xml, bool checklimits=false)
A window that shows a map or picture.