imtk

annotate src/imtk.c @ 0:b04d49e4599c

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Dec 2010 05:22:14 +0200
parents
children dfbd12d1f566
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <assert.h>
nuclear@0 3 #ifndef __APPLE__
nuclear@0 4 #include <GL/glut.h>
nuclear@0 5 #else
nuclear@0 6 #include <GLUT/glut.h>
nuclear@0 7 #endif
nuclear@0 8 #include "imtk.h"
nuclear@0 9
nuclear@0 10 static int scr_width = 1, scr_height = 1;
nuclear@0 11 static int mousex, mousey, mouse_bnmask;
nuclear@0 12 static int active = -1, hot = -1;
nuclear@0 13
nuclear@0 14 static void set_active(int id);
nuclear@0 15 static int set_hot(int id);
nuclear@0 16 static int hit_test(int x, int y, int w, int h);
nuclear@0 17
nuclear@0 18 static void draw_button(int id, int x, int y, const char *label, int focused);
nuclear@0 19 static void calc_button_size(const char *label, int *wret, int *hret);
nuclear@0 20 static void draw_string(int x, int y, const char *str);
nuclear@0 21 static int string_size(const char *str);
nuclear@0 22
nuclear@0 23 void imtk_inp_key(int key, int state)
nuclear@0 24 {
nuclear@0 25 glutPostRedisplay();
nuclear@0 26 }
nuclear@0 27
nuclear@0 28 void imtk_inp_mouse(int bn, int state)
nuclear@0 29 {
nuclear@0 30 if(state == IMTK_DOWN) {
nuclear@0 31 mouse_bnmask |= 1 << bn;
nuclear@0 32 } else {
nuclear@0 33 mouse_bnmask &= ~(1 << bn);
nuclear@0 34 }
nuclear@0 35 glutPostRedisplay();
nuclear@0 36 }
nuclear@0 37
nuclear@0 38 void imtk_inp_motion(int x, int y)
nuclear@0 39 {
nuclear@0 40 mousex = x;
nuclear@0 41 mousey = y;
nuclear@0 42
nuclear@0 43 glutPostRedisplay();
nuclear@0 44 }
nuclear@0 45
nuclear@0 46 void imtk_inp_reshape(int x, int y)
nuclear@0 47 {
nuclear@0 48 scr_width = x;
nuclear@0 49 scr_height = y;
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 void imtk_begin(void)
nuclear@0 53 {
nuclear@0 54 glMatrixMode(GL_PROJECTION);
nuclear@0 55 glPushMatrix();
nuclear@0 56 glLoadIdentity();
nuclear@0 57 glTranslatef(-1, 1, 0);
nuclear@0 58 glScalef(2.0 / scr_width, -2.0 / scr_height, 1.0);
nuclear@0 59 }
nuclear@0 60
nuclear@0 61 void imtk_end(void)
nuclear@0 62 {
nuclear@0 63 glMatrixMode(GL_PROJECTION);
nuclear@0 64 glPopMatrix();
nuclear@0 65 }
nuclear@0 66
nuclear@0 67 int imtk_button(int id, const char *label, int x, int y)
nuclear@0 68 {
nuclear@0 69 int w, h, res = 0;
nuclear@0 70 int over = 0;
nuclear@0 71
nuclear@0 72 assert(id >= 0);
nuclear@0 73
nuclear@0 74 calc_button_size(label, &w, &h);
nuclear@0 75
nuclear@0 76 if(hit_test(x, y, w, h)) {
nuclear@0 77 if(set_hot(id)) {
nuclear@0 78 glutPostRedisplay();
nuclear@0 79 }
nuclear@0 80 over = 1;
nuclear@0 81 }
nuclear@0 82
nuclear@0 83
nuclear@0 84 if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
nuclear@0 85 if(over) {
nuclear@0 86 set_active(id);
nuclear@0 87 }
nuclear@0 88 } else { /* mouse button up */
nuclear@0 89 if(active == id) {
nuclear@0 90 set_active(-1);
nuclear@0 91 if(hot == id && over) {
nuclear@0 92 res = 1;
nuclear@0 93 }
nuclear@0 94 }
nuclear@0 95 }
nuclear@0 96
nuclear@0 97 draw_button(id, x, y, label, over);
nuclear@0 98 return res;
nuclear@0 99 }
nuclear@0 100
nuclear@0 101 static void set_active(int id)
nuclear@0 102 {
nuclear@0 103 active = id;
nuclear@0 104 }
nuclear@0 105
nuclear@0 106 static int set_hot(int id)
nuclear@0 107 {
nuclear@0 108 if(active == -1) {
nuclear@0 109 hot = id;
nuclear@0 110 return 1;
nuclear@0 111 }
nuclear@0 112 return 0;
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 static int hit_test(int x, int y, int w, int h)
nuclear@0 116 {
nuclear@0 117 return mousex >= x && mousex < (x + w) &&
nuclear@0 118 mousey >= y && mousey < (y + h);
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 static void draw_button(int id, int x, int y, const char *label, int focused)
nuclear@0 122 {
nuclear@0 123 int width, height;
nuclear@0 124
nuclear@0 125 calc_button_size(label, &width, &height);
nuclear@0 126
nuclear@0 127 glBegin(GL_QUADS);
nuclear@0 128 if(focused) {
nuclear@0 129 glColor3f(0.85, 0.85, 0.85);
nuclear@0 130 } else {
nuclear@0 131 glColor3f(0.7, 0.7, 0.7);
nuclear@0 132 }
nuclear@0 133 glVertex2f(x, y);
nuclear@0 134 glVertex2f(x + width, y);
nuclear@0 135 glVertex2f(x + width, y + height);
nuclear@0 136 glVertex2f(x, y + height);
nuclear@0 137 glEnd();
nuclear@0 138
nuclear@0 139 glColor3f(0, 0, 0);
nuclear@0 140 draw_string(x + 20, y + 15, label);
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 static void calc_button_size(const char *label, int *wret, int *hret)
nuclear@0 144 {
nuclear@0 145 int strsz = string_size(label);
nuclear@0 146 if(wret) *wret = strsz + 40;
nuclear@0 147 if(hret) *hret = 20;
nuclear@0 148 }
nuclear@0 149
nuclear@0 150 static void draw_string(int x, int y, const char *str)
nuclear@0 151 {
nuclear@0 152 glRasterPos2i(x, y);
nuclear@0 153 while(*str) {
nuclear@0 154 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *str++);
nuclear@0 155 }
nuclear@0 156 }
nuclear@0 157
nuclear@0 158 static int string_size(const char *str)
nuclear@0 159 {
nuclear@0 160 return glutBitmapLength(GLUT_BITMAP_HELVETICA_10, str);
nuclear@0 161 }