rayfract

diff src/imtk/button.c @ 10:1496aae2e7d4

- simplified build by including dependences in the source tree - added make dep tracking - added mingw cross-build rules - added readme & licence
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 31 Jul 2023 18:58:56 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/imtk/button.c	Mon Jul 31 18:58:56 2023 +0300
     1.3 @@ -0,0 +1,72 @@
     1.4 +#include <string.h>
     1.5 +#include <assert.h>
     1.6 +#include "imtk.h"
     1.7 +#include "state.h"
     1.8 +#include "draw.h"
     1.9 +
    1.10 +static void calc_button_size(const char *label, int *wret, int *hret);
    1.11 +static void draw_button(int id, const char *label, int x, int y, int over);
    1.12 +
    1.13 +int imtk_button(int id, const char *label, int x, int y)
    1.14 +{
    1.15 +	int w, h, res = 0;
    1.16 +	int over = 0;
    1.17 +
    1.18 +	assert(id >= 0);
    1.19 +
    1.20 +	if(x == IMTK_AUTO || y == IMTK_AUTO) {
    1.21 +		imtk_layout_get_pos(&x, &y);
    1.22 +	}
    1.23 +
    1.24 +	calc_button_size(label, &w, &h);
    1.25 +
    1.26 +	if(imtk_hit_test(x, y, w, h)) {
    1.27 +		imtk_set_hot(id);
    1.28 +		over = 1;
    1.29 +	}
    1.30 +
    1.31 +	if(imtk_button_state(IMTK_LEFT_BUTTON)) {
    1.32 +		if(over) {
    1.33 +			imtk_set_active(id);
    1.34 +		}
    1.35 +	} else { /* mouse button up */
    1.36 +		if(imtk_is_active(id)) {
    1.37 +			imtk_set_active(-1);
    1.38 +			if(imtk_is_hot(id) && over) {
    1.39 +				res = 1;
    1.40 +			}
    1.41 +		}
    1.42 +	}
    1.43 +
    1.44 +	draw_button(id, label, x, y, over);
    1.45 +	imtk_layout_advance(w, h);
    1.46 +	return res;
    1.47 +}
    1.48 +
    1.49 +static void draw_button(int id, const char *label, int x, int y, int over)
    1.50 +{
    1.51 +	float tcol[4], bcol[4];
    1.52 +	int width, height, active = imtk_is_active(id);
    1.53 +	unsigned int attr = 0;
    1.54 +
    1.55 +	if(over) attr |= IMTK_FOCUS_BIT;
    1.56 +	if(active) attr |= IMTK_PRESS_BIT;
    1.57 +
    1.58 +	calc_button_size(label, &width, &height);
    1.59 +
    1.60 +	memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
    1.61 +	memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
    1.62 +
    1.63 +	imtk_draw_rect(x, y, width, height, tcol, bcol);
    1.64 +	imtk_draw_frame(x, y, width, height, active ? FRAME_INSET : FRAME_OUTSET);
    1.65 +
    1.66 +	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
    1.67 +	imtk_draw_string(x + 20, y + 15, label);
    1.68 +}
    1.69 +
    1.70 +static void calc_button_size(const char *label, int *wret, int *hret)
    1.71 +{
    1.72 +	int strsz = imtk_string_size(label);
    1.73 +	if(wret) *wret = strsz + 40;
    1.74 +	if(hret) *hret = 20;
    1.75 +}