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 File Reference
#include "../ckb/ckb-anim.h"
#include <math.h>
#include <time.h>
+ Include dependency graph for main.c:

Go to the source code of this file.

Macros

#define ANIM_MAX   (144 * 2)
 

Functions

void ckb_info ()
 
void ckb_init (ckb_runctx *context)
 
void ckb_parameter (ckb_runctx *context, const char *name, const char *value)
 
void anim_add (float x, float y, float width, float height)
 
void anim_remove (float x, float y)
 
void ckb_keypress (ckb_runctx *context, ckb_key *key, int x, int y, int state)
 
void ckb_start (ckb_runctx *context, int state)
 
void ckb_time (ckb_runctx *context, double delta)
 
int ckb_frame (ckb_runctx *context)
 

Variables

float kbsize = 0.f
 
ckb_gradient animcolor = { 0 }
 
int symmetric = 0
 
int kprelease = 0
 
int randomize = 0
 
double animlength = 0.
 
double x_offset = 0
 
double y_offset = 0
 
struct {
int active
 
float x
 
float y
 
float maxsize
 
float cursize
 
float choice
 
anim [(144 *2)] = { }
 

Macro Definition Documentation

#define ANIM_MAX   (144 * 2)

Definition at line 75 of file main.c.

Referenced by anim_add(), anim_remove(), ckb_frame(), and ckb_time().

Function Documentation

void anim_add ( float  x,
float  y,
float  width,
float  height 
)

Definition at line 84 of file main.c.

References anim, ANIM_MAX, animlength, symmetric, x, and y.

84  {
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 }
float y
Definition: main.c:66
float x
Definition: main.c:66
double width
Definition: main.c:52
#define ANIM_MAX
Definition: main.c:75
int symmetric
Definition: main.c:41
double animlength
Definition: main.c:40
struct keyAnim * anim
Definition: main.c:55
void anim_remove ( float  x,
float  y 
)

Definition at line 100 of file main.c.

References anim, and ANIM_MAX.

100  {
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 }
float y
Definition: main.c:66
float x
Definition: main.c:66
#define ANIM_MAX
Definition: main.c:75
struct keyAnim * anim
Definition: main.c:55
int ckb_frame ( ckb_runctx context)

Definition at line 134 of file main.c.

References anim, ANIM_MAX, animlength, ckb_alpha_blend(), ckb_grad_color(), CKB_KEYCLEAR, ckb_runctx::keycount, ckb_runctx::keys, randomize, and symmetric.

134  {
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 }
ckb_gradient animcolor
Definition: main.c:52
Definition: keymap.h:49
#define ANIM_MAX
Definition: main.c:75
unsigned keycount
Definition: ckb-anim.h:141
int symmetric
Definition: main.c:41
int randomize
Definition: main.c:53
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
ckb_key * keys
Definition: ckb-anim.h:140
void ckb_alpha_blend(ckb_key *key, float a, float r, float g, float b)
Definition: ckb-anim.h:283
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

+ Here is the call graph for this function:

void ckb_info ( )

Definition at line 5 of file main.c.

References CKB_COPYRIGHT, CKB_DESCRIPTION, CKB_GUID, CKB_KP_POSITION, CKB_KPMODE, CKB_LICENSE, CKB_LIVEPARAMS, CKB_NAME, CKB_PARAM_AGRADIENT, CKB_PARAM_BOOL, CKB_PARAM_DOUBLE, CKB_PREEMPT, CKB_PRESET_END, CKB_PRESET_PARAM, CKB_PRESET_START, CKB_TIME_DURATION, CKB_TIMEMODE, CKB_VERSION, and TRUE.

5  {
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 }
#define CKB_PRESET_END
Definition: ckb-anim.h:87
#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
#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_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
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
#define CKB_PRESET_START(name)
Definition: ckb-anim.h:85
#define CKB_PREEMPT(enable)
Definition: ckb-anim.h:102
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
#define CKB_PARAM_AGRADIENT(name, prefix, postfix, default)
Definition: ckb-anim.h:80
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
#define CKB_TIME_DURATION
Definition: ckb-anim.h:95
#define CKB_PRESET_PARAM(name, value)
Definition: ckb-anim.h:86
#define CKB_PARAM_BOOL(name, text, default)
Definition: ckb-anim.h:76
void ckb_init ( ckb_runctx context)

Definition at line 55 of file main.c.

References ckb_runctx::height, kbsize, and ckb_runctx::width.

55  {
56  kbsize = sqrt(context->width * context->width / 4.f + context->height * context->height / 4.f);
57  srand((unsigned)time(NULL));
58 }
unsigned height
Definition: ckb-anim.h:143
float kbsize
Definition: main.c:49
unsigned width
Definition: ckb-anim.h:143
void ckb_keypress ( ckb_runctx context,
ckb_key key,
int  x,
int  y,
int  state 
)

Definition at line 107 of file main.c.

References anim_add(), anim_remove(), ckb_runctx::height, kprelease, and ckb_runctx::width.

107  {
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 }
float y
Definition: main.c:66
float x
Definition: main.c:66
unsigned height
Definition: ckb-anim.h:143
void anim_remove(int index)
Definition: main.c:87
void anim_add(int index)
Definition: main.c:81
int kprelease
Definition: main.c:53
unsigned width
Definition: ckb-anim.h:143

+ Here is the call graph for this function:

void ckb_parameter ( ckb_runctx context,
const char *  name,
const char *  value 
)

Definition at line 60 of file main.c.

References animlength, CKB_PARSE_AGRADIENT, CKB_PARSE_BOOL, CKB_PARSE_DOUBLE, ckb_runctx::height, kprelease, randomize, symmetric, ckb_runctx::width, x_offset, and y_offset.

60  {
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 }
ckb_gradient animcolor
Definition: main.c:52
#define CKB_PARSE_AGRADIENT(param_name, gradient_ptr)
Definition: ckb-anim.h:121
unsigned height
Definition: ckb-anim.h:143
int symmetric
Definition: main.c:41
int randomize
Definition: main.c:53
#define CKB_PARSE_BOOL(param_name, value_ptr)
Definition: ckb-anim.h:117
double x_offset
Definition: main.c:43
double y_offset
Definition: main.c:43
int kprelease
Definition: main.c:53
#define CKB_PARSE_DOUBLE(param_name, value_ptr)
Definition: ckb-anim.h:116
unsigned width
Definition: ckb-anim.h:143
double animlength
Definition: main.c:40
void ckb_start ( ckb_runctx context,
int  state 
)

Definition at line 115 of file main.c.

References anim_add(), anim_remove(), ckb_runctx::height, ckb_runctx::width, x_offset, and y_offset.

115  {
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 }
unsigned height
Definition: ckb-anim.h:143
void anim_remove(int index)
Definition: main.c:87
void anim_add(int index)
Definition: main.c:81
double x_offset
Definition: main.c:43
double y_offset
Definition: main.c:43
unsigned width
Definition: ckb-anim.h:143

+ Here is the call graph for this function:

void ckb_time ( ckb_runctx context,
double  delta 
)

Definition at line 123 of file main.c.

References anim, ANIM_MAX, kbsize, and maxsize.

123  {
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 }
double maxsize
Definition: main.c:38
#define ANIM_MAX
Definition: main.c:75
float kbsize
Definition: main.c:49
struct keyAnim * anim
Definition: main.c:55

Variable Documentation

struct { ... } anim[ (144 * 2) ]
ckb_gradient animcolor = { 0 }

Definition at line 50 of file main.c.

double animlength = 0.

Definition at line 52 of file main.c.

float kbsize = 0.f

Definition at line 49 of file main.c.

Referenced by ckb_init(), and ckb_time().

int kprelease = 0

Definition at line 51 of file main.c.

int randomize = 0

Definition at line 51 of file main.c.

int symmetric = 0

Definition at line 51 of file main.c.

double x_offset = 0

Definition at line 53 of file main.c.

double y_offset = 0

Definition at line 53 of file main.c.