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 #include <time.h>
4 
5 void ckb_info(){
6  // Plugin info
7  CKB_NAME("Ripple");
8  CKB_VERSION("0.10");
9  CKB_COPYRIGHT("2014-2016", "MSC");
10  CKB_LICENSE("GPLv2");
11  CKB_GUID("{097D69F0-70B2-48B8-AFE2-25A1CDB02C9D}");
12  CKB_DESCRIPTION("An expanding ring effect.");
13 
14  // Effect parameters
15  CKB_PARAM_AGRADIENT("color", "Ripple color:", "", "ffffffff");
16  CKB_PARAM_DOUBLE("length", "Ring length:", "%", 100, 1, 100);
17  CKB_PARAM_DOUBLE("x_offset", "X offset:", "%", 0, -100, 100);
18  CKB_PARAM_DOUBLE("y_offset", "Y offset:", "%", 0, -100, 100);
19  CKB_PARAM_BOOL("symmetric", "Symmetric", 0);
20  CKB_PARAM_BOOL("randomize", "Randomly select from gradient", 0);
21 
22  // Timing/input parameters
27 
28  // Presets
29  CKB_PRESET_START("Shimmer");
30  CKB_PRESET_PARAM("duration", "2.0");
31  CKB_PRESET_PARAM("length", "50.0");
32  CKB_PRESET_PARAM("symmetric", "1");
34 
35  CKB_PRESET_START("Rainbow");
36  CKB_PRESET_PARAM("color", "0:ffff0000 17:ffffff00 33:ff00ff00 50:ff00ffff 67:ff0000ff 83:ffff00ff 100:ffff0000");
37  CKB_PRESET_PARAM("duration", "2.0");
39 
40  CKB_PRESET_START("Type lighting");
41  CKB_PRESET_PARAM("duration", "1.0");
42  CKB_PRESET_PARAM("length", "25.0");
43  CKB_PRESET_PARAM("symmetric", "1");
44  CKB_PRESET_PARAM("trigger", "0");
45  CKB_PRESET_PARAM("kptrigger", "1");
47 }
48 
49 float kbsize = 0.f;
51 int symmetric = 0, kprelease = 0, randomize = 0;
52 double animlength = 0.;
53 double x_offset = 0, y_offset = 0;
54 
55 void ckb_init(ckb_runctx* context){
56  kbsize = sqrt(context->width * context->width / 4.f + context->height * context->height / 4.f);
57  srand((unsigned)time(NULL));
58 }
59 
60 void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
61  CKB_PARSE_AGRADIENT("color", &animcolor){}
62  double len;
63  CKB_PARSE_DOUBLE("length", &len){
64  double sizex = context->width / 2.;
65  double sizey = context->height / 2.;
66  animlength = len / 100. * sqrt(sizex * sizex + sizey * sizey);
67  }
68  CKB_PARSE_BOOL("symmetric", &symmetric){}
69  CKB_PARSE_BOOL("kprelease", &kprelease){}
70  CKB_PARSE_BOOL("randomize", &randomize){}
71  CKB_PARSE_DOUBLE("x_offset", &x_offset) {}
72  CKB_PARSE_DOUBLE("y_offset", &y_offset) {}
73 }
74 
75 #define ANIM_MAX (144 * 2)
76 struct {
77  int active;
78  float x, y;
79  float maxsize;
80  float cursize;
81  float choice;
82 } anim[ANIM_MAX] = { };
83 
84 void anim_add(float x, float y, float width, float height){
85  for(int i = 0; i < ANIM_MAX; i++){
86  if(anim[i].active)
87  continue;
88  anim[i].active = 1;
89  anim[i].x = x;
90  anim[i].y = y;
91  float sizex = fmax(x, width - x);
92  float sizey = fmax(y, height - y);
93  anim[i].maxsize = sqrt(sizex * sizex + sizey * sizey) + animlength;
94  anim[i].cursize = (symmetric) ? -animlength : 0;
95  anim[i].choice = (float)rand()/(float)(RAND_MAX);
96  return;
97  }
98 }
99 
100 void anim_remove(float x, float y){
101  for(int i = 0; i < ANIM_MAX; i++){
102  if(anim[i].active && anim[i].x == x && anim[i].y == y)
103  anim[i].active = 0;
104  }
105 }
106 
107 void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
108  // Add or remove a ring on this key
109  if(state)
110  anim_add(x, y, context->width, context->height);
111  else if(kprelease)
112  anim_remove(x, y);
113 }
114 
115 void ckb_start(ckb_runctx* context, int state){
116  // Add or remove a ring in the center of the keyboard
117  if(state)
118  anim_add((context->width + (context->width * x_offset * 0.01)) / 2.f, (context->height - (context->height * y_offset * 0.01)) / 2.f, context->width, context->height);
119  else
120  anim_remove(context->width / 2.f, context->height / 2.f);
121 }
122 
123 void ckb_time(ckb_runctx* context, double delta){
124  // Advance animation on all rings
125  for(unsigned i = 0; i < ANIM_MAX; i++){
126  if(anim[i].active){
127  anim[i].cursize += kbsize * delta;
128  if(anim[i].cursize > anim[i].maxsize)
129  anim[i].active = 0;
130  }
131  }
132 }
133 
134 int ckb_frame(ckb_runctx* context){
135  CKB_KEYCLEAR(context);
136  // Draw rings
137  unsigned count = context->keycount;
138  ckb_key* keys = context->keys;
139  for(unsigned i = 0; i < ANIM_MAX; i++){
140  if(anim[i].active){
141  for(ckb_key* key = keys; key < keys + count; key++){
142  // Calculate distance between this key and the ring
143  float distance = anim[i].cursize - sqrt(pow(key->x - anim[i].x, 2.f) + pow(key->y - anim[i].y, 2.f));
144  // Divide distance by ring size (use absolute distance if symmetric)
145  distance /= animlength;
146  if(symmetric)
147  distance = fabs(distance);
148  else if(distance >= -0.005f && distance < 0.f)
149  // If not symmetric, round down values very close to zero
150  distance = 0.f;
151  if(distance > 1.f && distance <= 1.005f)
152  // Round values close to 1
153  distance = 1.f;
154 
155  // Blend color gradient according to position
156  if(distance >= 0. && distance <= 1.f){
157  float a, r, g, b;
158  float gradChoice = randomize ? anim[i].choice : distance;
159  ckb_grad_color(&a, &r, &g, &b, &animcolor, gradChoice * 100.);
160  ckb_alpha_blend(key, a, r, g, b);
161  }
162  }
163  }
164  }
165  return 0;
166 }
#define CKB_PRESET_END
Definition: ckb-anim.h:87
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
double maxsize
Definition: main.c:38
#define CKB_GUID(guid)
Definition: ckb-anim.h:60
#define CKB_NAME(name)
Definition: ckb-anim.h:62
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 ANIM_MAX
Definition: main.c:75
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
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
float kbsize
Definition: main.c:49
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
int symmetric
Definition: main.c:41
int randomize
Definition: main.c:53
#define CKB_PARAM_AGRADIENT(name, prefix, postfix, default)
Definition: ckb-anim.h:80
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
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
double x_offset
Definition: main.c:43
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
double y_offset
Definition: main.c:43
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
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