glamtk

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/imtk.c	Thu Dec 30 05:22:14 2010 +0200
     1.3 @@ -0,0 +1,161 @@
     1.4 +#include <stdio.h>
     1.5 +#include <assert.h>
     1.6 +#ifndef __APPLE__
     1.7 +#include <GL/glut.h>
     1.8 +#else
     1.9 +#include <GLUT/glut.h>
    1.10 +#endif
    1.11 +#include "imtk.h"
    1.12 +
    1.13 +static int scr_width = 1, scr_height = 1;
    1.14 +static int mousex, mousey, mouse_bnmask;
    1.15 +static int active = -1, hot = -1;
    1.16 +
    1.17 +static void set_active(int id);
    1.18 +static int set_hot(int id);
    1.19 +static int hit_test(int x, int y, int w, int h);
    1.20 +
    1.21 +static void draw_button(int id, int x, int y, const char *label, int focused);
    1.22 +static void calc_button_size(const char *label, int *wret, int *hret);
    1.23 +static void draw_string(int x, int y, const char *str);
    1.24 +static int string_size(const char *str);
    1.25 +
    1.26 +void imtk_inp_key(int key, int state)
    1.27 +{
    1.28 +	glutPostRedisplay();
    1.29 +}
    1.30 +
    1.31 +void imtk_inp_mouse(int bn, int state)
    1.32 +{
    1.33 +	if(state == IMTK_DOWN) {
    1.34 +		mouse_bnmask |= 1 << bn;
    1.35 +	} else {
    1.36 +		mouse_bnmask &= ~(1 << bn);
    1.37 +	}
    1.38 +	glutPostRedisplay();
    1.39 +}
    1.40 +
    1.41 +void imtk_inp_motion(int x, int y)
    1.42 +{
    1.43 +	mousex = x;
    1.44 +	mousey = y;
    1.45 +
    1.46 +	glutPostRedisplay();
    1.47 +}
    1.48 +
    1.49 +void imtk_inp_reshape(int x, int y)
    1.50 +{
    1.51 +	scr_width = x;
    1.52 +	scr_height = y;
    1.53 +}
    1.54 +
    1.55 +void imtk_begin(void)
    1.56 +{
    1.57 +	glMatrixMode(GL_PROJECTION);
    1.58 +	glPushMatrix();
    1.59 +	glLoadIdentity();
    1.60 +	glTranslatef(-1, 1, 0);
    1.61 +	glScalef(2.0 / scr_width, -2.0 / scr_height, 1.0);
    1.62 +}
    1.63 +
    1.64 +void imtk_end(void)
    1.65 +{
    1.66 +	glMatrixMode(GL_PROJECTION);
    1.67 +	glPopMatrix();
    1.68 +}
    1.69 +
    1.70 +int imtk_button(int id, const char *label, int x, int y)
    1.71 +{
    1.72 +	int w, h, res = 0;
    1.73 +	int over = 0;
    1.74 +
    1.75 +	assert(id >= 0);
    1.76 +
    1.77 +	calc_button_size(label, &w, &h);
    1.78 +
    1.79 +	if(hit_test(x, y, w, h)) {
    1.80 +		if(set_hot(id)) {
    1.81 +			glutPostRedisplay();
    1.82 +		}
    1.83 +		over = 1;
    1.84 +	}
    1.85 +
    1.86 +
    1.87 +	if(mouse_bnmask & (1 << IMTK_LEFT_BUTTON)) {
    1.88 +		if(over) {
    1.89 +			set_active(id);
    1.90 +		}
    1.91 +	} else { /* mouse button up */
    1.92 +		if(active == id) {
    1.93 +			set_active(-1);
    1.94 +			if(hot == id && over) {
    1.95 +				res = 1;
    1.96 +			}
    1.97 +		}
    1.98 +	}
    1.99 +
   1.100 +	draw_button(id, x, y, label, over);
   1.101 +	return res;
   1.102 +}
   1.103 +
   1.104 +static void set_active(int id)
   1.105 +{
   1.106 +	active = id;
   1.107 +}
   1.108 +
   1.109 +static int set_hot(int id)
   1.110 +{
   1.111 +	if(active == -1) {
   1.112 +		hot = id;
   1.113 +		return 1;
   1.114 +	}
   1.115 +	return 0;
   1.116 +}
   1.117 +
   1.118 +static int hit_test(int x, int y, int w, int h)
   1.119 +{
   1.120 +	return mousex >= x && mousex < (x + w) &&
   1.121 +		mousey >= y && mousey < (y + h);
   1.122 +}
   1.123 +
   1.124 +static void draw_button(int id, int x, int y, const char *label, int focused)
   1.125 +{
   1.126 +	int width, height;
   1.127 +
   1.128 +	calc_button_size(label, &width, &height);
   1.129 +
   1.130 +	glBegin(GL_QUADS);
   1.131 +	if(focused) {
   1.132 +		glColor3f(0.85, 0.85, 0.85);
   1.133 +	} else {
   1.134 +		glColor3f(0.7, 0.7, 0.7);
   1.135 +	}
   1.136 +	glVertex2f(x, y);
   1.137 +	glVertex2f(x + width, y);
   1.138 +	glVertex2f(x + width, y + height);
   1.139 +	glVertex2f(x, y + height);
   1.140 +	glEnd();
   1.141 +
   1.142 +	glColor3f(0, 0, 0);
   1.143 +	draw_string(x + 20, y + 15, label);
   1.144 +}
   1.145 +
   1.146 +static void calc_button_size(const char *label, int *wret, int *hret)
   1.147 +{
   1.148 +	int strsz = string_size(label);
   1.149 +	if(wret) *wret = strsz + 40;
   1.150 +	if(hret) *hret = 20;
   1.151 +}
   1.152 +
   1.153 +static void draw_string(int x, int y, const char *str)
   1.154 +{
   1.155 +	glRasterPos2i(x, y);
   1.156 +	while(*str) {
   1.157 +		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *str++);
   1.158 +	}
   1.159 +}
   1.160 +
   1.161 +static int string_size(const char *str)
   1.162 +{
   1.163 +	return glutBitmapLength(GLUT_BITMAP_HELVETICA_10, str);
   1.164 +}