Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 67 additions & 23 deletions src/ckb-gradient/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../ckb/ckb-anim.h"
#include <math.h>

#include <time.h>
#include <stdio.h>
void ckb_info(){
// Plugin info
CKB_NAME("Gradient");
Expand All @@ -13,6 +14,7 @@ void ckb_info(){
// Effect parameters
CKB_PARAM_AGRADIENT("color", "Color:", "", "ffffffff");
CKB_PARAM_BOOL("kphold", "Freeze until key is released", TRUE);
CKB_PARAM_BOOL("randomize", "Randomly select from gradient", 0);

// Timing/input parameters
CKB_KPMODE(CKB_KP_NAME);
Expand Down Expand Up @@ -42,73 +44,115 @@ void ckb_info(){
#define NONE -1.f
#define HOLD -2.f

struct keyAnim {
float target;
ckb_gradient gradient;
};

ckb_gradient animcolor = { 0 };
int kphold = 0, kprelease = 0;
float* target = 0;
int kphold = 0, kprelease = 0, randomize = 0;

struct keyAnim* anim;

void ckb_init(ckb_runctx* context){
// Initialize all keys to 100% (animation over)

srand((unsigned)time(NULL));
unsigned count = context->keycount;
target = malloc(count * sizeof(float));
for(unsigned i = 0; i < count; i++)
target[i] = NONE;
anim = malloc(count * sizeof *anim);
for(unsigned i = 0; i < count; i++) {
anim[i].target = NONE;
}
}

void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
CKB_PARSE_AGRADIENT("color", &animcolor){}
CKB_PARSE_BOOL("kphold", &kphold){}
CKB_PARSE_BOOL("kprelease", &kprelease){}
CKB_PARSE_BOOL("randomize", &randomize){}
}

void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
// Start or stop animation on key
int i = key - context->keys;
if(state){
if(kphold)
target[key - context->keys] = HOLD;
else
target[key - context->keys] = 0.f;
if (randomize) {
float a, r, g, b;
float colorChoice = (float)rand()/(float)(RAND_MAX);
ckb_grad_color(&a, &r, &g, &b, &animcolor, colorChoice * 100.);
ckb_gradient newGradient = { 0 };
newGradient.ptcount = 2;
newGradient.pts[0] = 0;
newGradient.pts[1] = 100;
newGradient.a[0] = 255;
newGradient.b[0] = b;
newGradient.g[0] = g;
newGradient.r[0] = r;
newGradient.a[1] = 0;
newGradient.b[1] = b;
newGradient.g[1] = g;
newGradient.r[1] = r;

anim[i].gradient = newGradient;
}

if(kphold) {
anim[i].target = HOLD;
} else {
anim[i].target = 0.f;
}
} else {
if(kprelease)
target[key - context->keys] = NONE;
else if(kphold)
target[key - context->keys] = 0.f;
if(kprelease) {
anim[i].target = NONE;
}
else if(kphold) {
anim[i].target = 0.f;
}
}
}

void ckb_start(ckb_runctx* context, int state){
// Start/stop all keys
unsigned count = context->keycount;
if(state)
memset(target, 0, count * sizeof(float));
else {
for(unsigned i = 0; i < count; i++)
target[i] = NONE;
if(state) {
memset(anim, 0, count * sizeof *anim);
} else {
for(unsigned i = 0; i < count; i++) {
anim[i].target = NONE;
}
}
}

void ckb_time(ckb_runctx* context, double delta){
// Advance animation on each key
unsigned count = context->keycount;
for(unsigned i = 0; i < count; i++){
float phase = target[i];
float phase = anim[i].target;
if(phase > 1.f || phase < 0.f)
continue;
target[i] = phase + delta;
anim[i].target = phase + delta;
}
}

int ckb_frame(ckb_runctx* context){
// Draw key colors
unsigned count = context->keycount;
for(unsigned i = 0; i < count; i++){
float phase = target[i];
float phase = anim[i].target;

if(phase == HOLD)
phase = 0.f;
else if(phase < 0.f)
phase = 1.f;
ckb_key* key = context->keys + i;
float a, r, g, b;
ckb_grad_color(&a, &r, &g, &b, &animcolor, phase * 100.);
ckb_gradient thisGradient;
if (randomize) {
thisGradient = anim[i].gradient;
} else {
thisGradient = animcolor;
}
ckb_grad_color(&a, &r, &g, &b, &thisGradient, phase * 100.);
key->a = a;
key->r = r;
key->g = g;
Expand Down
12 changes: 10 additions & 2 deletions src/ckb-ripple/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../ckb/ckb-anim.h"
#include <math.h>
#include <time.h>

void ckb_info(){
// Plugin info
Expand All @@ -14,6 +15,7 @@ void ckb_info(){
CKB_PARAM_AGRADIENT("color", "Ripple color:", "", "ffffffff");
CKB_PARAM_DOUBLE("length", "Ring length:", "%", 100, 1, 100);
CKB_PARAM_BOOL("symmetric", "Symmetric", 0);
CKB_PARAM_BOOL("randomize", "Randomly select from gradient", 0);

// Timing/input parameters
CKB_KPMODE(CKB_KP_POSITION);
Expand Down Expand Up @@ -44,11 +46,12 @@ void ckb_info(){

float kbsize = 0.f;
ckb_gradient animcolor = { 0 };
int symmetric = 0, kprelease = 0;
int symmetric = 0, kprelease = 0, randomize = 0;
double animlength = 0.;

void ckb_init(ckb_runctx* context){
kbsize = sqrt(context->width * context->width / 4.f + context->height * context->height / 4.f);
srand((unsigned)time(NULL));
}

void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
Expand All @@ -61,6 +64,7 @@ void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
}
CKB_PARSE_BOOL("symmetric", &symmetric){}
CKB_PARSE_BOOL("kprelease", &kprelease){}
CKB_PARSE_BOOL("randomize", &randomize){}
}

#define ANIM_MAX (144 * 2)
Expand All @@ -69,6 +73,7 @@ struct {
float x, y;
float maxsize;
float cursize;
float choice;
} anim[ANIM_MAX] = { };

void anim_add(float x, float y, float width, float height){
Expand All @@ -82,6 +87,7 @@ void anim_add(float x, float y, float width, float height){
float sizey = fmax(y, height - y);
anim[i].maxsize = sqrt(sizex * sizex + sizey * sizey) + animlength;
anim[i].cursize = (symmetric) ? -animlength : 0;
anim[i].choice = (float)rand()/(float)(RAND_MAX);
return;
}
}
Expand Down Expand Up @@ -140,10 +146,12 @@ int ckb_frame(ckb_runctx* context){
if(distance > 1.f && distance <= 1.005f)
// Round values close to 1
distance = 1.f;

// Blend color gradient according to position
if(distance >= 0. && distance <= 1.f){
float a, r, g, b;
ckb_grad_color(&a, &r, &g, &b, &animcolor, distance * 100.);
float gradChoice = randomize ? anim[i].choice : distance;
ckb_grad_color(&a, &r, &g, &b, &animcolor, gradChoice * 100.);
ckb_alpha_blend(key, a, r, g, b);
}
}
Expand Down