rayfract

annotate 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
rev   line source
nuclear@10 1 #include <string.h>
nuclear@10 2 #include <assert.h>
nuclear@10 3 #include "imtk.h"
nuclear@10 4 #include "state.h"
nuclear@10 5 #include "draw.h"
nuclear@10 6
nuclear@10 7 static void calc_button_size(const char *label, int *wret, int *hret);
nuclear@10 8 static void draw_button(int id, const char *label, int x, int y, int over);
nuclear@10 9
nuclear@10 10 int imtk_button(int id, const char *label, int x, int y)
nuclear@10 11 {
nuclear@10 12 int w, h, res = 0;
nuclear@10 13 int over = 0;
nuclear@10 14
nuclear@10 15 assert(id >= 0);
nuclear@10 16
nuclear@10 17 if(x == IMTK_AUTO || y == IMTK_AUTO) {
nuclear@10 18 imtk_layout_get_pos(&x, &y);
nuclear@10 19 }
nuclear@10 20
nuclear@10 21 calc_button_size(label, &w, &h);
nuclear@10 22
nuclear@10 23 if(imtk_hit_test(x, y, w, h)) {
nuclear@10 24 imtk_set_hot(id);
nuclear@10 25 over = 1;
nuclear@10 26 }
nuclear@10 27
nuclear@10 28 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
nuclear@10 29 if(over) {
nuclear@10 30 imtk_set_active(id);
nuclear@10 31 }
nuclear@10 32 } else { /* mouse button up */
nuclear@10 33 if(imtk_is_active(id)) {
nuclear@10 34 imtk_set_active(-1);
nuclear@10 35 if(imtk_is_hot(id) && over) {
nuclear@10 36 res = 1;
nuclear@10 37 }
nuclear@10 38 }
nuclear@10 39 }
nuclear@10 40
nuclear@10 41 draw_button(id, label, x, y, over);
nuclear@10 42 imtk_layout_advance(w, h);
nuclear@10 43 return res;
nuclear@10 44 }
nuclear@10 45
nuclear@10 46 static void draw_button(int id, const char *label, int x, int y, int over)
nuclear@10 47 {
nuclear@10 48 float tcol[4], bcol[4];
nuclear@10 49 int width, height, active = imtk_is_active(id);
nuclear@10 50 unsigned int attr = 0;
nuclear@10 51
nuclear@10 52 if(over) attr |= IMTK_FOCUS_BIT;
nuclear@10 53 if(active) attr |= IMTK_PRESS_BIT;
nuclear@10 54
nuclear@10 55 calc_button_size(label, &width, &height);
nuclear@10 56
nuclear@10 57 memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol);
nuclear@10 58 memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol);
nuclear@10 59
nuclear@10 60 imtk_draw_rect(x, y, width, height, tcol, bcol);
nuclear@10 61 imtk_draw_frame(x, y, width, height, active ? FRAME_INSET : FRAME_OUTSET);
nuclear@10 62
nuclear@10 63 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
nuclear@10 64 imtk_draw_string(x + 20, y + 15, label);
nuclear@10 65 }
nuclear@10 66
nuclear@10 67 static void calc_button_size(const char *label, int *wret, int *hret)
nuclear@10 68 {
nuclear@10 69 int strsz = imtk_string_size(label);
nuclear@10 70 if(wret) *wret = strsz + 40;
nuclear@10 71 if(hret) *hret = 20;
nuclear@10 72 }