Planeshift

pawsilluminationwindow.h

Go to the documentation of this file.
00001 /*
00002  * pawsSketchWindow.h - Author: Keith Fulton
00003  *
00004  * Copyright (C) 2006 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 
00020 #ifndef PAWS_SKETCH_WINDOW_HEADER
00021 #define PAWS_SKETCH_WINDOW_HEADER
00022 
00023 #include "net/cmdbase.h"
00024 #include "paws/pawswidget.h"
00025 #include "paws/pawscolorpromptwindow.h"
00026 #include "paws/pawsstringpromptwindow.h"
00027 
00028 #define SKETCHBEZIER_SEGMENTS 20
00029 
00032 class pawsSketchWindow : public pawsWidget, public psClientNetSubscriber,public iOnStringEnteredAction,public iOnColorEnteredAction
00033 {
00034     class BezierWeights
00035     {
00036     private:
00037         float *precomputedBezierWeights; //[SKETCHBEZIER_SEGMENTS+1][4];
00038     public:
00039         BezierWeights();
00040         ~BezierWeights();
00041 
00042         // this is SLOWER than Get(int offset)
00043         inline float Get(int segmentIndex, int controlpointNum)
00044         {
00045             //CS_ASSERT(segmentIndex <= SKETCHBEZIER_SEGMENTS && segmentIndex >= 0);
00046             //CS_ASSERT(controlpointNum >= 0 && controlpointNum <= 3);
00047             return precomputedBezierWeights[segmentIndex * SKETCHBEZIER_SEGMENTS + controlpointNum];
00048         }
00049 
00050         // returns the precomputed value of "segmentIndex*SKETCHBEZIER_SEGMENTS + controlpointNum"
00051         inline float Get(int offset)
00052         {
00053             //CS_ASSERT(offset <= (SKETCHBEZIER_SEGMENTS+1)*4 && offset >= 0);
00054             return precomputedBezierWeights[offset];
00055         }
00056     };
00057     struct SketchObject
00058     {
00059         bool selected;
00060         int x,y;
00061         csString str;
00062         int frame;    
00063         int color;    
00064         bool colorSet;
00065 
00066         pawsSketchWindow *parent;
00067 
00068         const char *GetStr() { return str; }
00069         virtual void SetStr(const char *s) { str=s; }
00070         SketchObject()  { x=0; y=0; selected=false; color=-1; colorSet=false; }
00071         virtual ~SketchObject() {};
00072 
00073         virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent)=0;
00074         virtual void WriteXml(csString& xml) = 0;
00075         virtual void Draw()=0;
00076         virtual bool IsHit(int mouseX, int mouseY)=0;
00077         virtual void Select(bool _selected)         { selected=_selected; colorSet=false; frame=0; }
00078         // update the RELATIVE position
00079         virtual void UpdatePosition(int _x, int _y) { x = _x; y = _y;     }
00080         virtual void SetColor(int _color) { color = _color; colorSet=true; }
00081     };
00082     struct SketchIcon : public SketchObject
00083     {
00084         csRef<iPawsImage> iconImage;
00085         SketchIcon() : SketchObject() {}
00086         SketchIcon(int _x, int _y, const char *icon, pawsSketchWindow *parent);
00087         virtual ~SketchIcon() {};
00088         bool Init(int _x, int _y, const char *icon, pawsSketchWindow *parent);
00089         virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
00090         virtual void WriteXml(csString& xml);
00091         virtual void Draw();
00092         virtual bool IsHit(int mouseX, int mouseY);
00093         virtual void SetStr(const char *s);
00094         // update the RELATIVE position
00095         virtual void UpdatePosition(int _x, int _y);
00096     };
00097     struct SketchText : public SketchObject
00098     {
00099         csString font;
00100 
00101         SketchText(int _x,int _y,const char *value,pawsSketchWindow *_parent)
00102         {
00103             x = _x;
00104             y = _y;
00105             str = value;
00106             parent = _parent;
00107         }
00108         SketchText() {}
00109         virtual ~SketchText() {};
00110         virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
00111         virtual void WriteXml(csString& xml);
00112         virtual void Draw();
00113         virtual bool IsHit(int mouseX, int mouseY);
00114         // update the RELATIVE position
00115         virtual void UpdatePosition(int _x, int _y);
00116     };
00117     struct SketchLine : public SketchObject
00118     {
00119         int x2,y2;
00120         int offsetX, offsetY;
00121         int dragMode;
00122 
00123         SketchLine(int _x,int _y, int _x2, int _y2,pawsSketchWindow *_parent)
00124         {
00125             x=_x;    y=_y;
00126             x2=_x2; y2=_y2;
00127             parent = _parent;
00128             dragMode = 0;
00129         }
00130         SketchLine() {};
00131         virtual ~SketchLine() {};
00132         virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
00133         virtual void WriteXml(csString& xml);
00134         virtual void Draw();
00135         virtual bool IsHit(int mouseX, int mouseY);
00136         // update the RELATIVE position
00137         virtual void UpdatePosition(int _x, int _y);
00138     };
00139     struct SketchBezier : public SketchObject
00140     {
00141         /* Cubic Bézier Curve
00142             p1        p2 (control points)
00143            /            \_
00144         p0-----------------p3
00145 
00146         2 lines would actually be enough to handle the bezier curve.
00147         Connecting p0-p1 and p3-p2: However, the usability would suffer since we can't move the "whole line" then.
00148         Connecting p1-p2 and p0-p3: Displaying this doesn't look good and usability suffers.
00149         */
00150 
00151         //BezierHelper *bezierHelper;
00152 
00153         SketchLine p0p1;
00154         SketchLine p0p3;
00155         SketchLine p3p2;
00156 
00157         SketchBezier(int x, int y, int x1, int y1, int x2, int y2, int x3, int y3, pawsSketchWindow *_parent)
00158         {
00159             this->parent = _parent;
00160             //this->bezierHelper = &_parent->bezierHelper;
00161 
00162             p0p1.parent = _parent;
00163             p0p1.x = x; // point 0 (also start point)
00164             p0p1.y = y;
00165             p0p1.x2 = x1; // point 1 (control point 1)
00166             p0p1.y2 = y1;
00167 
00168             p0p3.parent = _parent;
00169             p0p3.x = x; // point 0
00170             p0p3.y = y;
00171             p0p3.x2 = x3; // point 3 (also end point)
00172             p0p3.y2 = y3;
00173 
00174             p3p2.parent = _parent;
00175             p3p2.x2 = x2; // point 3 (also end point)
00176             p3p2.y2 = y2;
00177             p3p2.x = x3; // point 2 (control point 2)
00178             p3p2.y = y3;
00179         }
00180         SketchBezier() {};
00181         virtual ~SketchBezier() {};
00182         virtual bool Load(iDocumentNode *node, pawsSketchWindow *parent);
00183         virtual void WriteXml(csString& xml);
00184         virtual void Draw();
00185         virtual bool IsHit(int mouseX, int mouseY);
00186         // update the RELATIVE position
00187         virtual void UpdatePosition(int _x, int _y);
00188     };
00189 
00190 protected:
00191     csPDelArray<SketchObject> objlist;
00192     size_t selectedIndex;
00193     int dirty;
00194     uint32_t currentItemID;
00195     csRef<iPawsImage> blackBox;
00196     bool editMode;
00197     bool mouseDown;
00198     int mouseDownX; // used to calculate the updatePosition diff. this value is updated OnMouseDown
00199     int mouseDownY;
00200     csStringArray iconList;
00201     int primCount;
00202     bool readOnly;
00203     bool stringPending;
00204     bool colorPending;
00205     BezierWeights bezierWeights;
00206 
00207     int frame; // incremented each frame till 10 (to update selection without clicking)
00208 
00209     pawsWidget* FeatherTool;
00210     pawsWidget* TextTool;
00211     pawsWidget* LineTool;
00212     pawsWidget* BezierTool;
00213     pawsWidget* PlusTool;
00214     pawsWidget* LeftArrowTool;
00215     pawsWidget* RightArrowTool;
00216     pawsWidget* DeleteTool;
00217     pawsWidget* NameTool;
00218     pawsWidget* SaveButton;
00219     pawsWidget* LoadButton;
00220     pawsWidget* ColorTool;
00221 
00222     void DrawSketch();
00223     bool ParseSketch(const char *xml, bool checklimits = false);
00224     bool ParseLimits(const char *xmlstr);
00225     void SetToolbarButtons();  //sets the toolbar buttons hide/show status
00226 
00227     void AddSketchText();
00228     void AddSketchIcon();
00229     void AddSketchLine();
00230     void AddSketchBezier();
00231     void RemoveSelected();
00232     void NextPrevIcon(int delta);
00233     void MoveObject(int dx, int dy);
00234     void ChangeSketchName();
00235     void SaveSketch();
00236     void LoadSketch();
00237     bool isBadChar(char c);
00238 
00239     csString toXML();
00240 
00241     csString filenameSafe(const csString &original);
00242 
00243     csString sketchName;
00244     
00246     csRef<iPawsImage> sketchBgImage;
00247 
00248 public:
00249     pawsSketchWindow();
00251     pawsSketchWindow(const pawsSketchWindow& origin);
00252     virtual ~pawsSketchWindow();
00253 
00254     bool PostSetup();
00255 
00256     void HandleMessage( MsgEntry* message );
00257 
00259     void OnStringEntered(const char *name, int param, const char *value);
00260 
00262     void OnColorEntered(const char *name,int param,int color);
00263 
00265     double CalcFunction(MathEnvironment* env, const char* functionName, const double* params);
00266 
00267 
00268     virtual bool OnMouseDown( int button, int modifiers, int x, int y );
00269     virtual bool OnMouseUp( int button, int modifiers, int x, int y );
00270     virtual bool OnKeyDown( utf32_char keyCode, utf32_char key, int modifiers );
00271     virtual void Hide();
00272     virtual void Draw();
00273 
00274     // SketchObject helper functions
00275     iGraphics2D *GetG2D();
00276     void DrawBlackBox(int x, int y);
00277     void DrawColorWidgetText(const char *text, int x, int y, int color);
00278     bool IsMouseDown() { return mouseDown; }
00279 
00280     virtual bool GetFocusOverridesControls() const { return true; }
00281 };
00282 
00283 CREATE_PAWS_FACTORY( pawsSketchWindow );
00284 
00285 
00286 #endif
00287 
00288