guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gshape.hpp
1 //
2 // Gadgets for drawing interactively (and for creating and moving gadgets)
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_Shape_hpp
9 #define Guit_Shape_hpp
10 #include <vector>
11 #include <gadget.hpp>
12 #include <ggraphics.hpp>
13 namespace guit {
14 
15 class GShape : public Gadget {
16 protected:
17  friend GadgetType_<GShape,Gadget>;
18  GShape(GadgetType&, GString const& css);
19  GShape(GString const& css);
20 
21 public:
22  ~GShape();
23  Gadget& clone(GClone const&) override;
24 
25  GShape* toShape() override {return this;}
26 
27  using Gadget::findProp;
28  GProp* findProp(GPropType&) const override;
29 
30  using Gadget::prop;
31  GProp& prop(GPropType&, bool compute_style = false) override;
32 
36  GPos& posProp();
37 
41  GSize& sizeProp();
42 
43  void move(float x, float y) override;
44  void move(GPoint const& p) override {move(p.x, p.y);}
45 
46  void resize(float w, float h) override;
47  void resize(GDim const&) override;
48 
49  using GType = GadgetType_<GShape,Gadget>;
50  static GType& Type();
51  GType& type() const override {return Type();}
52 
55  gptr<GPos> pos_;
56  gptr<GSize> size_;
57  void onAdd(GBox* parent) override;
58  void addProp(GProp& prop, bool add_to_list) override;
60 };
61 
62 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63 
65 class GRectShape : public GShape {
66 protected:
67  GRectShape(GadgetType&, GString const& css);
68 
69 public:
70  GRectShape(GString const& css = "");
71  GRectShape& clone(GClone const&) override;
72 
74  static GType& Type();
75  GType& type() const override {return Type();}
76 
79  void doPaint(GRenderGraphics&, GRender* parent_render) override;
81 };
82 
84 GRectShape& RectShape(GString const& title = "");
85 
86 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87 
89 class GOvalShape : public GRectShape {
90 protected:
91  GOvalShape(GadgetType&, GString const& css);
92 
93 public:
94  GOvalShape(GString const& css = "");
95  GOvalShape& clone(GClone const&) override;
96 
98  static GType& Type();
99  GType& type() const override {return Type();}
100 
103  void doPaint(GRenderGraphics&, GRender* parent_render) override;
104  Gadget* doPick(GPoint const&, GPickMode&) override;
106 
107 };
108 
110 GOvalShape& OvalShape(GString const& title = "");
111 
112 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113 
115 class GArcShape : public GOvalShape {
116 protected:
117  GArcShape(GadgetType&, GString const& css);
118 
119 public:
120  GArcShape(GString const& css = "");
121  GArcShape& clone(GClone const&) override;
122 
123  GArcShape& setStart(float start);
124  GArcShape& setExtent(float extent);
125  float start() const;
126  float extent() const;
127 
128  GFloat& startProp() {return *start_;}
129  GFloat& extentProp() {return *extent_;}
130 
132  static GType& Type();
133  GType& type() const override {return Type();}
134 
138  void doPaint(GRenderGraphics&, GRender* parent_render) override;
139  Gadget* doPick(GPoint const&, GPickMode&) override;
141 };
142 
144 GArcShape& ArcShape(GString const& title = "");
145 
146 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147 
151 class GLineShape : public GShape {
152 protected:
153  GLineShape(GadgetType&, GString const& css);
154 
155 public:
156  GLineShape(GString const& css = "");
157  GLineShape& clone(GClone const&) override;
158 
163  GPos& pos2Prop();
165 
170  GLineShape& changePos2Prop(GPos& pos);
172 
173  void move(GPoint const& p) override {move(p.x, p.y);}
174  void move(float x, float y) override;
175 
176  void reshapeInteractively() override;
177  void reshapeInteractively(GMouseEvent& e) override;
178 
179  using GType = GadgetType_<GLineShape,GShape>;
180  static GType& Type();
181  GType& type() const override {return Type();}
182 
186  void onAdd(GBox* parent) override;
187  void addProp(GProp& prop, bool add_to_list) override;
188  void doPaint(GRenderGraphics&, GRender* parent_render) override;
189  Gadget* doPick(GPoint const&, GPickMode&) override;
190  void packPos(float parentx, float parenty) override;
191  void moveImpl(float x, float y, bool callcb) override;
192  virtual void updateFrame();
193  GHandle handleDown(GPoint const& wpos, bool inter) override;
194  void handleDrag(float dx, float dy, GHandle const&) override;
195  void handleUp(GPoint const& wpos, GHandle const&) override;
196  GHandle findHandle(GPoint const& wpos) override;
197  void readAux(std::istream&, class GIOBuffer&) override;
198  void writeAux(std::ostream&, class GIOBuffer&) const override;
200 };
201 
203 GLineShape& LineShape(GString const& title = "");
204 
205 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206 
208 class GFreehandShape : public GShape {
209 protected:
210  GFreehandShape(GadgetType&, GString const& css);
211 
212 public:
213  GFreehandShape(GString const& css ="");
214  GFreehandShape& clone(GClone const&) override;
215 
216  void addPoint(float x, float y) {addPoint(GPoint(x, y));}
217  void addPoint(GPoint const&);
218  GFreehandShape& operator<<(GPoint const& p) {addPoint(p); return *this;}
219  void getPoints(GPoints&) const;
220 
222  static GType& Type();
223  GType& type() const override {return Type();}
224 
227  GPoint pmin_, pmax_, last_;
228  GPoints points_;
229  void packPos(float parentx, float parenty) override;
230  void moveImpl(float x, float y, bool callcb) override;
231  GHandle handleDown(GPoint const& wpos, bool inter) override;
232  void handleDrag(float dx, float dy, GHandle const&) override;
233  void handleUp(GPoint const& wpos, GHandle const&) override;
234  void doPaint(GRenderGraphics&, GRender* parent_render) override;
235  void readAux(std::istream&, class GIOBuffer&) override;
236  void writeAux(std::ostream&, class GIOBuffer&) const override;
238 };
239 
241 GFreehandShape& FreehandShape(GString const& title = "");
242 
243 }
244 #endif