guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gpos.hpp
1 //
2 // Position and Layer of a Gadget.
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef GuitPos_hpp
9 #define GuitPos_hpp
10 #include <gvarprop.hpp>
11 namespace guit {
12 
13 class GPosValue : public GPoint {
14 public:
15  GPosValue() = default;
16  GPosValue(float x, float y, int layer);
17  GPosValue(GPoint const&, int layer);
18  GPosValue& operator=(GPoint const&);
19  bool operator==(GPosValue const&) const;
20  bool operator!=(GPosValue const&) const;
21  static GPosValue blend(GPosValue const& a, GPosValue const& b, float coeff);
22  GString stringValue() const;
23  int layer{};
24  //uint8_t layer{};
25 };
26 
27 inline bool gconvert(GPosValue const& from, GPosValue& to) {to = from; return true;}
28 bool gconvert(GString const& from, GPosValue& to);
29 bool gconvert(GPosValue const& from, GString& to);
30 
31 
51 class GPos : public GVarProp_<GPosValue> {
52 public:
54  using Layer = uint8_t;
55  enum : Layer {MinLayer = 0, MaxLayer = 250};
56 
57  GPos();
58  GPos(GString const& value);
59  GPos(float x, float y, Layer layer = 1);
60  GPos(GPoint const& pos, Layer layer = 1);
61  GPos(GPos const& value);
62  GPos* clone(bool copy_value) const override;
63 
64  GPos* toPos() override {return this;}
65  GPos const* toPos() const override {return this;}
66 
67  GPos& operator=(GPos const& pos) {set(pos); return *this;}
68  GPos& operator=(GPoint const& pos) {set(pos); return *this;}
69  GPos& operator=(GString const& pos) {set(pos); return *this;}
70 
71  using Super::set;
72  void set(float x, float y);
73  void set(float x, float y, Layer layer);
74  void set(GPoint const& pos);
75  void set(GProp const&) override;
76 
77  virtual void blend(GPos const& a, GPos const& b, float mix);
78  void blend(GProp const& a, GProp const& b, float mix) override;
79 
80  void setX(float);
81  void setY(float);
82  void setLayer(int);
83 
84  virtual GPoint const& pos() const {return value_;}
85  virtual float x() const {return value_.x;}
86  virtual float y() const {return value_.y;}
87  virtual int layer() const {return value_.layer;}
88 
93  auto xField() {return GNumPropField<GPos,float>(*this, value_.x);}
94  auto yField() {return GNumPropField<GPos,float>(*this, value_.y);}
95  auto layerField() {return GNumPropField<GPos,int>(*this, value_.layer);}
97 
99  GPos& operator<<=(GTextExpr&);
100 
101  using GType = GPropType_<GPos,GProp>;
102  static GType& Type();
103  GType& type() const override {return Type();}
104 
107  bool onAdd(Gadget*) override;
108  void onRemove(Gadget*) override;
109  void updateGadget(Gadget&) override;
110  void apply(Gadget*, GRender&, Specif) final;
112 };
113 
116 GPos& Pos();
117 GPos& Pos(GString const& value);
118 GPos& Pos(float x, float y, GPos::Layer layer = 1);
120 
121 
123 class GDragPos : public GPos {
124 public:
125  GDragPos(float x, float y, Layer layer = 1) : GPos(x,y,layer) {}
126  GDragPos(GPoint const& pos = {}, Layer layer = 1) : GPos(pos,layer) {}
127  GDragPos(GDragPos const&);
128  GDragPos(GString const&);
129  GDragPos* clone(bool copy_value) const override;
130 
131  void fixX(bool val = true) {fixx_ = val;}
132  void fixY(bool val = true) {fixy_ = val;}
133  bool isXFixed() const {return fixx_;}
134  bool isYFixed() const {return fixy_;}
135 
137  static GType& Type();
138  GType& type() const override {return Type();}
139 
142  bool fixx_{}, fixy_{};
143  bool onAdd(Gadget*) override;
144  void onRemove(Gadget*) override;
145  void fire(GEvent&) override;
147 };
148 
149 
151 class GRelativePos : public GPos {
152 public:
153  GRelativePos(Gadget& related, float xoffset, float yoffset, Layer = 1);
154  GRelativePos(Gadget& related, GPoint const& offset = {}, Layer = 1);
155  GRelativePos(GRelativePos const&);
156  GRelativePos(GString const& = "");
157  ~GRelativePos();
158  GRelativePos* clone(bool copy_value) const override;
159 
160  Gadget* relatedGadget() const {return related_;}
161  GPoint const& relatedPos() const {return relatedpos_;}
162  virtual void updatePos();
163 
165  static GType& Type();
166  GType& type() const override {return Type();}
167 
170  gptr<Gadget> related_;
171  gptr<GProp> cond_;
172  GPoint relatedpos_;
174 };
175 
176 }
177 #endif