guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
ganim.hpp
1 //
2 // Animations
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_Anim_hpp
9 #define Guit_Anim_hpp
10 #include <gtimer.hpp>
11 namespace guit {
12 
14 class GAnimStep {
15 public:
16  GAnimStep() = default;
17  GAnimStep& operator<<(GString const& str) ;
18  GAnimStep& operator<<(GProp& p) {list_.push_back(p); return *this;}
19  GAnimStep& operator<<(GProp* p) {list_.push_back(p); return *this;}
20  bool empty() const {return list_.empty();}
21  std::vector<gptr<GProp>> list_;
22 };
23 
24 inline GAnimStep& AnimStep() {return *new GAnimStep();}
25 
26 
29 class GAnim : public GProp {
30 public:
31  enum {Infinite = -1, OneShot = -2};
32 
33  ~GAnim();
34  GAnim(GTime duration = 0, int repetitions = Infinite);
35  GAnim(GString const& spec);
36 
37  GAnim* clone(bool) const override {return nullptr;}
38  GAnim* toAnim() override {return this;}
39 
41  void start(bool yes = true);
42  void stop() {start(false);}
43 
45  virtual void restart();
46 
47  GAnim& operator<<(GAnimStep& prop);
48  GAnim& operator<<(GString const& proplist);
49  GAnim& operator<<(GProp& prop);
50  GAnim& operator<<(GProp* prop);
51  GAnim& operator<<(GFunction const& fun);
52 
53  int repetitions() const {return count_;}
54  GAnim& setRepetitions(int count);
55 
56  bool isRunning() const;
57  GTime duration() const {return duration_;}
58  GTimer* timer() {return timer_;}
59  GString stringValue() const override;
60 
61  using GType = GPropType_<GAnim,GProp>;
62  static GType& Type();
63  GType& type() const override {return Type();}
64 
65  bool isEquivalent(GProp const& other) const override {return false;}
66  void apply(Gadget*, GRender&, Specif) override;
67  void addPropsTo(Gadget&);
68  void removePropsFrom(Gadget&);
69 
70 protected:
71  friend class GVarProp;
72  virtual void init();
73  void timeoutCB();
74  void fireCallbacks(GEvent&);
75  GTime duration_{}, elapsed_{}, timeout_{};
76  int count_{1};
77  gptr<GTimer> timer_;
78  GPropList calls_;
79  GAnimStep props_;
80  std::vector<GAnimStep*> steps_;
81  void updateProps(GAnimStep& from, GAnimStep& to, float alpha);
82 };
83 
85 inline GAnim& Anim(GTime duration = 0, int count = GAnim::Infinite) {
86  return *new GAnim(duration, count);
87 }
88 
89 }
90 #endif