preparing background images (#69)

This commit is contained in:
Felix Kratz 2021-12-28 19:12:11 +01:00
parent c1a6541052
commit 98b8ef571d
5 changed files with 49 additions and 4 deletions

View file

@ -1,4 +1,5 @@
#include "background.h"
#include "image.h"
#include "misc/helpers.h"
#include "shadow.h"
@ -16,6 +17,7 @@ void background_init(struct background* background) {
background->color = rgba_color_from_hex(0xff000000);
background->border_color = rgba_color_from_hex(0xff000000);
shadow_init(&background->shadow);
image_init(&background->image);
}
bool background_set_color(struct background* background, uint32_t color) {
@ -79,6 +81,9 @@ bool background_set_padding_right(struct background* background, uint32_t pad) {
void background_calculate_bounds(struct background* background, uint32_t x, uint32_t y) {
background->bounds.origin.x = x;
background->bounds.origin.y = y - background->bounds.size.height / 2;
if (background->image.enabled)
image_calculate_bounds(&background->image, x, y);
}
void background_draw(struct background* background, CGContextRef context) {
@ -88,6 +93,9 @@ void background_draw(struct background* background, CGContextRef context) {
draw_rect(context, bounds, &background->shadow.color, background->corner_radius, background->border_width, &background->shadow.color, false);
}
if (background->image.enabled)
image_draw(&background->image, context);
draw_rect(context, background->bounds, &background->color, background->corner_radius, background->border_width, &background->border_color, false);
}
@ -115,6 +123,8 @@ static bool background_parse_sub_domain(struct background* background, FILE* rsp
struct token entry = { key_value_pair.value, strlen(key_value_pair.value) };
if (token_equals(subdom, SUB_DOMAIN_SHADOW))
return shadow_parse_sub_domain(&background->shadow, rsp, entry, message);
else if (token_equals(subdom, SUB_DOMAIN_IMAGE))
return image_parse_sub_domain(&background->image, rsp, entry, message);
else {
fprintf(rsp, "Invalid subdomain: %s \n", subdom.text);
printf("Invalid subdomain: %s \n", subdom.text);

View file

@ -12,6 +12,7 @@ struct background {
int padding_right;
struct rgba_color color;
struct rgba_color border_color;
struct image image;
struct shadow shadow;
};

View file

@ -1,13 +1,30 @@
#include "image.h"
#include "misc/helpers.h"
#include <_types/_uint32_t.h>
void image_init(struct image* image) {
image->enabled = false;
image->image_ref = NULL;
image->data_ref = NULL;
image->bounds = CGRectNull;
}
void image_load(struct image* image, char* path) {
bool image_set_enabled(struct image* image, bool enabled) {
if (image->enabled == enabled) return false;
image->enabled = enabled;
return true;
}
bool image_load(struct image* image, char* path) {
CGDataProviderRef data_provider = CGDataProviderCreateWithFilename(path);
CGImageRef new_image_ref = CGImageCreateWithPNGDataProvider(data_provider, NULL, false, kCGRenderingIntentDefault);
if (data_provider && new_image_ref)
image_set(image, new_image_ref, CGRectNull, true);
else printf("Could find or open image file at: %s!\n", path);
CGDataProviderRelease(data_provider);
free(path);
return true;
}
bool image_data_equals(struct image* image, CFDataRef new_data_ref) {
@ -31,7 +48,6 @@ bool image_data_equals(struct image* image, CFDataRef new_data_ref) {
return equals;
}
bool image_set(struct image* image, CGImageRef new_image_ref, CGRect bounds, bool forced) {
CFDataRef new_data_ref = CGDataProviderCopyData(CGImageGetDataProvider(new_image_ref));
@ -44,9 +60,10 @@ bool image_set(struct image* image, CGImageRef new_image_ref, CGRect bounds, boo
if (image->image_ref) CGImageRelease(image->image_ref);
if (image->data_ref) CFRelease(image->data_ref);
image->bounds = bounds;
image->image_ref = new_image_ref;
image->data_ref = new_data_ref;
image->bounds = bounds;
image->enabled = true;
return true;
}
@ -64,3 +81,16 @@ void image_destroy(struct image* image) {
CGImageRelease(image->image_ref);
if (image->data_ref) CFRelease(image->data_ref);
}
static bool image_parse_sub_domain(struct image* image, FILE* rsp, struct token property, char* message) {
if (token_equals(property, PROPERTY_DRAWING))
return image_set_enabled(image, evaluate_boolean_state(get_token(&message), image->enabled));
else if (token_equals(property, "tmp"))
return image_load(image, token_to_string(get_token(&message)));
else {
fprintf(rsp, "Unknown property: %s \n", property.text);
printf("Unknown property: %s \n", property.text);
}
return false;
}

View file

@ -4,14 +4,17 @@
#include <_types/_uint16_t.h>
#include <_types/_uint32_t.h>
struct image {
bool enabled;
CGImageRef image_ref;
CFDataRef data_ref;
CGRect bounds;
};
void image_init(struct image* image);
bool image_set_enabled(struct image* image, bool enabled);
bool image_data_equals(struct image* image, CFDataRef new_data_ref);
bool image_set(struct image* image, CGImageRef new_image_ref, CGRect bounds, bool forced);
void image_load(struct image* image, char* path);
bool image_load(struct image* image, char* path);
void image_calculate_bounds(struct image* image, uint32_t x, uint32_t y);
void image_draw(struct image* image, CGContextRef context);
void image_destroy(struct image* image);

View file

@ -35,6 +35,7 @@
#define SUB_DOMAIN_GRAPH "graph"
#define SUB_DOMAIN_POPUP "popup"
#define SUB_DOMAIN_SHADOW "shadow"
#define SUB_DOMAIN_IMAGE "image"
#define PROPERTY_FONT "font"
#define PROPERTY_COLOR "color"