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
main.c
Go to the documentation of this file.
1 #include "../ckb/ckb-anim.h"
2 #include <math.h>
3 
4 void ckb_info(){
5  // Plugin info
6  CKB_NAME("Wave");
7  CKB_VERSION("0.10");
8  CKB_COPYRIGHT("2014-2016", "MSC");
9  CKB_LICENSE("GPLv2");
10  CKB_GUID("{E0BBA19E-C328-4C0E-8E3C-A06D5722B4FC}");
11  CKB_DESCRIPTION("A moving wave effect.");
12 
13  // Effect parameters
14  CKB_PARAM_ANGLE("angle", "Angle:", "", 90);
15  CKB_PARAM_AGRADIENT("color", "Wave color:", "", "ffffffff");
16  CKB_PARAM_DOUBLE("length", "Wave length:", "%", 100, 1., 100.);
17  CKB_PARAM_BOOL("symmetric", "Symmetric", 0);
18 
19  // Timing/input parameters
24 
25  // Presets
26  CKB_PRESET_START("Shimmer");
27  CKB_PRESET_PARAM("duration", "2.0");
28  CKB_PRESET_PARAM("length", "50.0");
29  CKB_PRESET_PARAM("symmetric", "1");
31 
32  CKB_PRESET_START("Rainbow");
33  CKB_PRESET_PARAM("color", "0:ffff0000 17:ffffff00 33:ff00ff00 50:ff00ffff 67:ff0000ff 83:ffff00ff 100:ffff0000");
34  CKB_PRESET_PARAM("duration", "2.0");
36 
37  CKB_PRESET_START("Vertical rainbow");
38  CKB_PRESET_PARAM("color", "0:ffff0000 17:ffffff00 33:ff00ff00 50:ff00ffff 67:ff0000ff 83:ffff00ff 100:ffff0000");
39  CKB_PRESET_PARAM("duration", "2.0");
40  CKB_PRESET_PARAM("angle", "180");
42 }
43 
44 void ckb_init(ckb_runctx* context){
45  // Nothing to do
46 }
47 
49 int symmetric = 0, kprelease = 0;
50 double angle = 0.;
51 double left = 0., top = 0.;
52 double animlength = 0., width = 0.;
53 
54 void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
55  CKB_PARSE_AGRADIENT("color", &animcolor){}
56  double len;
57  CKB_PARSE_DOUBLE("length", &len){
58  animlength = len / 100.;
59  }
60  CKB_PARSE_BOOL("symmetric", &symmetric){}
61  CKB_PARSE_BOOL("kprelease", &kprelease){}
62  long _angle;
63  CKB_PARSE_ANGLE("angle", &_angle){
64  angle = CKB_REAL_ANGLE(_angle);
65  // Get each of the four corners of the keyboard, relative to the center
66  double wOver2 = context->width / 2., hOver2 = context->height / 2.;
67  double x[4] = {
68  -wOver2, wOver2,
69  -wOver2, wOver2
70  };
71  double y[4] = {
72  -hOver2, -hOver2,
73  hOver2, hOver2
74  };
75  // Rotate them in the direction that the animation will travel
76  for(int i = 0; i < 4; i++){
77  double x2 = x[i] * cos(angle) - y[i] * sin(angle);
78  y[i] = x[i] * sin(angle) + y[i] * cos(angle);
79  x[i] = x2;
80  }
81  // Determine the leftmost and rightmost points
82  double min_x = INFINITY, max_x = -INFINITY;
83  for(int i = 0; i < 4; i++){
84  if(x[i] < min_x)
85  min_x = x[i];
86  if(x[i] > max_x)
87  max_x = x[i];
88  }
89  // The leftmost point is where the animation will begin when started with the mode,
90  // and the difference between the two is the animation width.
91  // Animations always travel left to right in their own coordinate system.
92  left = min_x * cos(-angle) + wOver2;
93  top = min_x * sin(-angle) + hOver2;
94  width = max_x - min_x;
95  }
96 }
97 
98 #define ANIM_MAX (144 * 2)
99 struct {
100  int active;
101  float x, y;
102  float curx;
103 } anim[ANIM_MAX] = { };
104 
105 void anim_add(float x, float y){
106  for(int i = 0; i < ANIM_MAX; i++){
107  if(anim[i].active)
108  continue;
109  anim[i].active = 1;
110  anim[i].x = x;
111  anim[i].y = y;
112  anim[i].curx = symmetric ? -animlength * width : 0.f;
113  return;
114  }
115 }
116 
117 void anim_remove(float x, float y){
118  for(int i = 0; i < ANIM_MAX; i++){
119  if(anim[i].active && anim[i].x == x && anim[i].y == y)
120  anim[i].active = 0;
121  }
122 }
123 
124 void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
125  // Start or stop animation on a key
126  if(state)
127  anim_add(x, y);
128  else
129  anim_remove(x, y);
130 }
131 
132 void ckb_start(ckb_runctx* context, int state){
133  // Start or stop animation on the edge of the keyboard
134  if(state)
135  anim_add(left, top);
136  else if(kprelease)
137  anim_remove(left, top);
138 }
139 
140 void ckb_time(ckb_runctx* context, double delta){
141  // Advance all waves, remove them if they pass the edge
142  double length = animlength * width;
143  for(unsigned i = 0; i < ANIM_MAX; i++){
144  if(anim[i].active){
145  anim[i].curx += width * delta;
146  if(anim[i].curx > width + length)
147  anim[i].active = 0;
148  }
149  }
150 }
151 
152 int ckb_frame(ckb_runctx* context){
153  CKB_KEYCLEAR(context);
154  // Draw keys
155  double length = animlength * width;
156  unsigned count = context->keycount;
157  ckb_key* keys = context->keys;
158  for(unsigned i = 0; i < ANIM_MAX; i++){
159  if(anim[i].active){
160  for(ckb_key* key = keys; key < keys + count; key++){
161  // Translate and rotate the key position into the animation's coordinate system
162  float x = key->x - anim[i].x, y = key->y - anim[i].y;
163  // Distance is the current X minus the key's X
164  float distance = anim[i].curx - (x * cos(angle) - y * sin(angle));
165  distance /= length;
166  // If symmetric, use absolute distance
167  if(symmetric)
168  distance = fabs(distance);
169  else if(distance >= -0.005f && distance < 0.f)
170  // If not symmetric, round values close to 0
171  distance = 0.f;
172  if(distance > 1.f && distance <= 1.005f)
173  // Round values close to 1
174  distance = 1.f;
175  // Pick gradient position based on distance
176  if(distance <= 1.f && distance >= 0.){
177  float a, r, g, b;
178  ckb_grad_color(&a, &r, &g, &b, &animcolor, distance * 100.);
179  ckb_alpha_blend(key, a, r, g, b);
180  }
181  }
182  }
183  }
184  return 0;
185 }
#define CKB_PRESET_END
Definition: ckb-anim.h:87
#define ANIM_MAX
Definition: main.c:98
void ckb_keypress(ckb_runctx *context, ckb_key *key, int x, int y, int state)
Definition: main.c:75
void ckb_parameter(ckb_runctx *context, const char *name, const char *value)
Definition: main.c:68
#define CKB_PARAM_DOUBLE(name, prefix, postfix, default, min, max)
Definition: ckb-anim.h:75
#define CKB_VERSION(version)
Definition: ckb-anim.h:64
#define CKB_TIMEMODE(mode)
Definition: ckb-anim.h:97
float y
Definition: main.c:66
#define TRUE
Definition: ckb-anim.h:46
#define CKB_GUID(guid)
Definition: ckb-anim.h:60
#define CKB_NAME(name)
Definition: ckb-anim.h:62
#define CKB_PARAM_ANGLE(name, prefix, postfix, default)
Definition: ckb-anim.h:81
float x
Definition: main.c:66
ckb_gradient animcolor
Definition: main.c:52
double width
Definition: main.c:52
#define CKB_PARSE_AGRADIENT(param_name, gradient_ptr)
Definition: ckb-anim.h:121
#define CKB_LICENSE(license)
Definition: ckb-anim.h:68
#define CKB_KP_POSITION
Definition: ckb-anim.h:92
#define CKB_COPYRIGHT(year, author)
Definition: ckb-anim.h:66
Definition: keymap.h:49
unsigned height
Definition: ckb-anim.h:143
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
double top
Definition: main.c:51
int ckb_frame(ckb_runctx *context)
Definition: main.c:137
unsigned keycount
Definition: ckb-anim.h:141
#define CKB_PRESET_START(name)
Definition: ckb-anim.h:85
void ckb_time(ckb_runctx *context, double delta)
Definition: main.c:126
#define CKB_PREEMPT(enable)
Definition: ckb-anim.h:102
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
double angle
Definition: main.c:50
int symmetric
Definition: main.c:41
#define CKB_PARAM_AGRADIENT(name, prefix, postfix, default)
Definition: ckb-anim.h:80
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
double left
Definition: main.c:51
void anim_remove(int index)
Definition: main.c:87
#define CKB_TIME_DURATION
Definition: ckb-anim.h:95
void ckb_info()
Definition: main.c:5
#define CKB_PARSE_BOOL(param_name, value_ptr)
Definition: ckb-anim.h:117
void ckb_init(ckb_runctx *context)
Definition: main.c:57
void anim_add(int index)
Definition: main.c:81
#define CKB_PARSE_ANGLE(param_name, value_ptr)
Definition: ckb-anim.h:122
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
void ckb_start(ckb_runctx *context, int state)
Definition: main.c:114
#define CKB_PRESET_PARAM(name, value)
Definition: ckb-anim.h:86
ckb_key * keys
Definition: ckb-anim.h:140
int kprelease
Definition: main.c:53
#define CKB_PARSE_DOUBLE(param_name, value_ptr)
Definition: ckb-anim.h:116
#define CKB_PARAM_BOOL(name, text, default)
Definition: ckb-anim.h:76
void ckb_alpha_blend(ckb_key *key, float a, float r, float g, float b)
Definition: ckb-anim.h:283
unsigned width
Definition: ckb-anim.h:143
double animlength
Definition: main.c:40
#define CKB_REAL_ANGLE(angle)
Definition: ckb-anim.h:127
void ckb_grad_color(float *a, float *r, float *g, float *b, const ckb_gradient *grad, float pos)
Definition: ckb-anim.h:254
struct keyAnim * anim
Definition: main.c:55