imtk

annotate src/button.c @ 6:38609a9f7586

reorganizing ...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Apr 2011 14:22:42 +0300
parents
children 10604ff95527
rev   line source
nuclear@6 1 #include <assert.h>
nuclear@6 2 #include "imtk.h"
nuclear@6 3 #include "state.h"
nuclear@6 4 #include "draw.h"
nuclear@6 5
nuclear@6 6 static void calc_button_size(const char *label, int *wret, int *hret);
nuclear@6 7 static void draw_button(int id, const char *label, int x, int y);
nuclear@6 8
nuclear@6 9 int imtk_button(int id, const char *label, int x, int y)
nuclear@6 10 {
nuclear@6 11 int w, h, res = 0;
nuclear@6 12 int over = 0;
nuclear@6 13
nuclear@6 14 assert(id >= 0);
nuclear@6 15
nuclear@6 16 calc_button_size(label, &w, &h);
nuclear@6 17
nuclear@6 18 if(imtk_hit_test(x, y, w, h)) {
nuclear@6 19 imtk_set_hot(id);
nuclear@6 20 over = 1;
nuclear@6 21 }
nuclear@6 22
nuclear@6 23 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
nuclear@6 24 if(over) {
nuclear@6 25 imtk_set_active(id);
nuclear@6 26 }
nuclear@6 27 } else { /* mouse button up */
nuclear@6 28 if(imtk_is_active(id)) {
nuclear@6 29 imtk_set_active(-1);
nuclear@6 30 if(imtk_is_hot(id) && over) {
nuclear@6 31 res = 1;
nuclear@6 32 }
nuclear@6 33 }
nuclear@6 34 }
nuclear@6 35
nuclear@6 36 draw_button(id, label, x, y);
nuclear@6 37 return res;
nuclear@6 38 }
nuclear@6 39
nuclear@6 40 static void draw_button(int id, const char *label, int x, int y)
nuclear@6 41 {
nuclear@6 42 int width, height;
nuclear@6 43
nuclear@6 44 calc_button_size(label, &width, &height);
nuclear@6 45
nuclear@6 46 glBegin(GL_QUADS);
nuclear@6 47 if(imtk_hit_test(x, y, width, height)) {
nuclear@6 48 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
nuclear@6 49 } else {
nuclear@6 50 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
nuclear@6 51 }
nuclear@6 52 glVertex2f(x, y);
nuclear@6 53 glVertex2f(x + width, y);
nuclear@6 54 glVertex2f(x + width, y + height);
nuclear@6 55 glVertex2f(x, y + height);
nuclear@6 56 glEnd();
nuclear@6 57
nuclear@6 58 imtk_draw_frame(x, y, width, height, imtk_is_active(id) ? FRAME_INSET : FRAME_OUTSET);
nuclear@6 59
nuclear@6 60 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
nuclear@6 61 imtk_draw_string(x + 20, y + 15, label);
nuclear@6 62 }
nuclear@6 63
nuclear@6 64 static void calc_button_size(const char *label, int *wret, int *hret)
nuclear@6 65 {
nuclear@6 66 int strsz = imtk_string_size(label);
nuclear@6 67 if(wret) *wret = strsz + 40;
nuclear@6 68 if(hret) *hret = 20;
nuclear@6 69 }