ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
animscript.h
Go to the documentation of this file.
1 #ifndef ANIMSCRIPT_H
2 #define ANIMSCRIPT_H
3 
4 #include <QHash>
5 #include <QObject>
6 #include <QMap>
7 #include <QProcess>
8 #include <QUuid>
9 #include <QVariant>
10 #include "keymap.h"
11 #include "colormap.h"
12 
13 // Class for tracking an animation script. Has a global list of all possible scripts, and can also provide instances to launch the process and communicate with it.
14 // See also: KbAnim, KbLight
15 
16 class AnimScript : public QObject
17 {
18  Q_OBJECT
19 public:
20  // Animation parameters
21  struct Param {
22  // Parameter type
23  enum Type {
24  INVALID = -1,
28  RGB,
35  };
37  // Internal name
38  QString name;
39  // Parameter friendly name/description
40  QString prefix;
41  QString postfix;
42  // Default value (if any)
43  QVariant defaultValue;
44  // Minimum and maximum values (apply to int and double)
45  QVariant minimum;
46  QVariant maximum;
47  };
48 
49  // Global animation path
50  static QString path();
51  // Scan the animation path for scripts
52  static void scan();
53  // Loaded script count and alphabetical list
54  static inline int count() { return scripts.count(); }
55  static QList<const AnimScript*> list();
56 
57  // Script properties
58  typedef QMap<QString, QVariant> PresetValue;
59  inline const QUuid& guid() const { return _info.guid; }
60  inline const QString& name() const { return _info.name; }
61  inline const QString& version() const { return _info.version; }
62  inline QString copyright() const { return "Copyright © " + _info.year + " " + _info.author; }
63  inline const QString& year() const { return _info.year; }
64  inline const QString& author() const { return _info.author; }
65  inline const QString& license() const { return _info.license; }
66  inline const QString& description() const { return _info.description; }
67  inline bool hasKeypress() const { return _info.kpMode != KP_NONE; }
68  inline const QStringList& presets() const { return _presets; }
69  inline const PresetValue& preset(int index) const { return _presetValues[index]; }
70 
71  // Parameters, in the order they were given
72  inline QListIterator<Param> paramIterator() const { return _info.params; }
73  inline Param param(const QString& name) const { QListIterator<Param> i(_info.params); while(i.hasNext()){ Param p = i.next(); if(p.name == name) return p; } return ((Param[]){ { Param::INVALID, "", "", "", 0, 0, 0 } })[0]; }
74  inline bool hasParam(const QString& name) const { QListIterator<Param> i(_info.params); while(i.hasNext()){ if(i.next().name == name) return true; } return false; }
75 
76  // Creates a usable script object with the given parent object. Returns null if no such script exists.
77  static AnimScript* copy(QObject* parent, const QUuid& id);
78 
79  // Initializes or re-initializes a script. Must be called at least once.
80  // paramValues should contain parameter name/value pairs to run the script with.
81  void init(const KeyMap& map, const QStringList& keys, const QMap<QString, QVariant>& paramValues);
82  // Updates parameters without a complete re-initialization. Does nothing if the animation does not support live params.
83  void parameters(const QMap<QString, QVariant>& paramValues);
84  // Starts or restarts the animation (no key).
85  void retrigger(quint64 timestamp, bool allowPreempt = false);
86  // Stops the no-key animation
87  void stop(quint64 timestamp);
88  // Triggers a keypress event.
89  void keypress(const QString& key, bool pressed, quint64 timestamp);
90  // Executes the next frame of the animation.
91  void frame(quint64 timestamp);
92  // Ends the animation.
93  void end();
94 
95  // Whether or not the animation has processed any frames yet.
96  inline bool hasFrame() const { return initialized && readAnyFrame; }
97 
98  // Colors returned from the last executed frame.
99  const ColorMap& colors() const { return _colors; }
100 
101  ~AnimScript();
102 
103 private slots:
104  void readProcess();
105 
106 private:
107  bool load();
108 
109  // Basic info
110  struct {
111  QUuid guid;
112  QString name;
113  QString version;
114  QString year;
115  QString author;
116  QString license;
117  QString description;
118  // Parameter list
119  QList<Param> params;
120  // Playback flags
121  int kpMode :3;
122  bool absoluteTime :1, repeat :1, preempt :1, liveParams :1;
123  } _info;
124  const static int KP_NONE = 0, KP_NAME = 1, KP_POSITION = 2;
125  QStringList _presets;
126  QList<PresetValue> _presetValues;
127  // Script path
128  QString _path;
129  // Key map (positions)
131  int minX, minY;
132  // Keys in use
133  QStringList _keys;
134  // Current colors
137 
138  // Animation state
139  quint64 lastFrame;
142  QProcess* process;
144 
145  // Helper functions
146  void setDuration();
147  void printParams();
148  void begin(quint64 timestamp);
149  void advance(quint64 timestamp);
150 
151  // Global script list
152  static QHash<QUuid, AnimScript*> scripts;
153 
154  AnimScript(QObject* parent, const QString& path);
155  AnimScript(QObject* parent, const AnimScript& base);
156 };
157 
158 #endif // ANIMSCRIPT_H
int repeatMsec
Definition: animscript.h:140
static const int KP_NONE
Definition: animscript.h:124
ColorMap _colorBuffer
Definition: animscript.h:143
static int count()
Definition: animscript.h:54
bool firstFrame
Definition: animscript.h:141
Param param(const QString &name) const
Definition: animscript.h:73
void advance(quint64 timestamp)
Definition: animscript.cpp:445
static QList< const AnimScript * > list()
Definition: animscript.cpp:51
bool load()
Definition: animscript.cpp:77
void retrigger(quint64 timestamp, bool allowPreempt=false)
Definition: animscript.cpp:331
static QString path()
Definition: animscript.cpp:29
ColorMap _colors
Definition: animscript.h:135
QVariant defaultValue
Definition: animscript.h:43
void frame(quint64 timestamp)
Definition: animscript.cpp:430
const PresetValue & preset(int index) const
Definition: animscript.h:69
const QString & author() const
Definition: animscript.h:64
const ColorMap & colors() const
Definition: animscript.h:99
AnimScript(QObject *parent, const QString &path)
Definition: animscript.cpp:11
Definition: keymap.h:49
static const int KP_NAME
Definition: animscript.h:124
QString _path
Definition: animscript.h:128
const QString & year() const
Definition: animscript.h:63
const QString & description() const
Definition: animscript.h:66
bool hasParam(const QString &name) const
Definition: animscript.h:74
static QHash< QUuid, AnimScript * > scripts
Definition: animscript.h:152
QVariant maximum
Definition: animscript.h:46
QString copyright() const
Definition: animscript.h:62
struct AnimScript::@0 _info
void printParams()
Definition: animscript.cpp:273
static const int KP_POSITION
Definition: animscript.h:124
bool stopped
Definition: animscript.h:141
QList< PresetValue > _presetValues
Definition: animscript.h:126
const QString & license() const
Definition: animscript.h:65
const QStringList & presets() const
Definition: animscript.h:68
KeyMap _map
Definition: animscript.h:130
bool readFrame
Definition: animscript.h:141
QMap< QString, QVariant > PresetValue
Definition: animscript.h:58
void init(const KeyMap &map, const QStringList &keys, const QMap< QString, QVariant > &paramValues)
Definition: animscript.cpp:239
void keypress(const QString &key, bool pressed, quint64 timestamp)
Definition: animscript.cpp:354
const QString & name() const
Definition: animscript.h:60
bool hasFrame() const
Definition: animscript.h:96
static void scan()
Definition: animscript.cpp:37
void end()
Definition: animscript.cpp:387
quint64 lastFrame
Definition: animscript.h:139
QVariant minimum
Definition: animscript.h:45
PresetValue _paramValues
Definition: animscript.h:136
QStringList _keys
Definition: animscript.h:133
bool inFrame
Definition: animscript.h:141
Definition: keymap.h:49
static AnimScript * copy(QObject *parent, const QUuid &id)
Definition: animscript.cpp:67
QListIterator< Param > paramIterator() const
Definition: animscript.h:72
QStringList _presets
Definition: animscript.h:125
QProcess * process
Definition: animscript.h:142
const QUuid & guid() const
Definition: animscript.h:59
const QString & version() const
Definition: animscript.h:61
void setDuration()
Definition: animscript.cpp:253
void parameters(const QMap< QString, QVariant > &paramValues)
Definition: animscript.cpp:265
bool readAnyFrame
Definition: animscript.h:141
bool initialized
Definition: animscript.h:141
bool hasKeypress() const
Definition: animscript.h:67
void begin(quint64 timestamp)
Definition: animscript.cpp:287
void readProcess()
Definition: animscript.cpp:397
void stop(quint64 timestamp)
Definition: animscript.cpp:344
int durationMsec
Definition: animscript.h:140