/*
* Copyright (C) 2021 P. J. McDermott
*
* This file is part of Maze Fight
*
* Maze Fight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Maze Fight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Maze Fight. If not, see .
*/
#include
#include "../tk.h"
#include "widget.h"
struct mftk_check {
struct mftk_widget parent;
int butn_width;
int butn_padding;
SDL_Color *butn_color;
SDL_Color *mark_color;
int state;
int label_padding;
struct mftk_widget *label;
int (*action)(void *, int);
void *user_data;
};
static void
_mftk_check_layout(struct mftk_widget *w)
{
struct mftk_check *c = (struct mftk_check *) w;
mftk_widget_layout(c->label);
w->w = c->butn_width;
if (c->label != NULL) {
w->w += c->label_padding + c->label->w;
}
w->h = c->butn_width;
if (c->label != NULL && c->label->h > w->h) {
w->h = c->label->h;
}
}
static int
_mftk_check_event(struct mftk_widget *w, SDL_Event *e,
int x __attribute__((__unused__)),
int y __attribute__((__unused__)))
{
struct mftk_check *c = (struct mftk_check *) w;
switch (e->type) {
case SDL_MOUSEBUTTONUP:
if (e->button.button == SDL_BUTTON_LEFT) {
c->state = !c->state;
return c->action(c->user_data, c->state);
}
break;
default:
break;
}
return 0;
}
static int
_mftk_check_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y)
{
struct mftk_check *c = (struct mftk_check *) w;
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = c->butn_width;
rect.h = c->butn_width;
if (SDL_SetRenderDrawColor(renderer,
c->butn_color->r, c->butn_color->g,
c->butn_color->b, c->butn_color->a) < 0 ||
SDL_RenderFillRect(renderer, &rect) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't render widget: %s",
SDL_GetError());
return -1;
}
if (c->state == SDL_TRUE) {
rect.x += c->butn_padding;
rect.y += c->butn_padding;
rect.w -= c->butn_padding * 2;
rect.h -= c->butn_padding * 2;
if (SDL_SetRenderDrawColor(renderer,
c->mark_color->r, c->mark_color->g,
c->mark_color->b, c->mark_color->a) < 0
|| SDL_RenderFillRect(renderer, &rect) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't render widget: %s",
SDL_GetError());
return -1;
}
}
if (c->label != NULL && mftk_widget_render(c->label, renderer,
x + c->butn_width + c->label_padding, y) < 0) {
return -1;
}
return 0;
}
static void
_mftk_check_destroy(struct mftk_widget *w)
{
struct mftk_check *c = (struct mftk_check *) w;
if (c->label != NULL) {
mftk_widget_destroy(&c->label);
}
}
struct mftk_widget *
mftk_check_new(int butn_width, int butn_padding, SDL_Color *butn_color,
SDL_Color *mark_color, int state, int label_padding,
TTF_Font *font, const char *text, SDL_Color *text_color,
int (*action)(void *, int), void *user_data,
SDL_Renderer *renderer)
{
struct mftk_widget *w;
struct mftk_check *c;
mftk_widget_init(w, c, check);
if (font != NULL && text != NULL && text_color != NULL) {
c->label = mftk_label_new(font, text, text_color, renderer);
if (c->label == NULL) {
free(c);
return NULL;
}
}
c->butn_width = butn_width;
c->butn_padding = butn_padding;
c->butn_color = butn_color;
c->mark_color = mark_color;
c->state = state;
c->label_padding = label_padding;
c->action = action;
c->user_data = user_data;
return w;
}