summaryrefslogtreecommitdiffstats
path: root/menu_demo/button.c
blob: e63143a9f3f270de0251cec619a406b67f911462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
#endif

#include <GL/gl.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

#include "button.h"



struct ButtonStruct ButtonCreate(int f_dXPos, int f_dYPos, int f_dWidth, int f_dHeight, char *f_pcTitle, int f_iType)
{
  struct ButtonStruct l_sButton;

  // copy parameters
  l_sButton.m_dXPos   = f_dXPos;
  l_sButton.m_dYPos   = f_dYPos;
  l_sButton.m_iType   = f_iType;
  l_sButton.m_dHeight = f_dHeight;
  l_sButton.m_dWidth  = f_dWidth;

  // copy title
  memcpy(l_sButton.m_pcTitle, f_pcTitle, sizeof(f_pcTitle));

  switch (f_iType)
  {
    case BUTTON_CLICK:
      l_sButton.m_piImgNormal[0] = BitmapLoad("img/button_click_left.bmp");
      l_sButton.m_piImgNormal[1] = BitmapLoad("img/button_click_center.bmp");
      l_sButton.m_piImgNormal[2] = BitmapLoad("img/button_click_right.bmp");

      l_sButton.m_piImgHover[0] = BitmapLoad("img/button_click_left_hover.bmp");
      l_sButton.m_piImgHover[1] = BitmapLoad("img/button_click_center_hover.bmp");
      l_sButton.m_piImgHover[2] = BitmapLoad("img/button_click_right_hover.bmp");

      BitmapConvertWidth(&l_sButton.m_piImgNormal[0], f_dHeight);
      BitmapConvertWidth(&l_sButton.m_piImgNormal[1], f_dHeight);
      BitmapConvertWidth(&l_sButton.m_piImgNormal[2], f_dHeight);

      BitmapConvertWidth(&l_sButton.m_piImgHover[0], f_dHeight);
      BitmapConvertWidth(&l_sButton.m_piImgHover[1], f_dHeight);
      BitmapConvertWidth(&l_sButton.m_piImgHover[2], f_dHeight);

      // calculate (center)width of image
      l_sButton.m_dCenterWidth = max(f_dWidth - l_sButton.m_piImgNormal[0].m_iWidth - l_sButton.m_piImgNormal[2].m_iWidth, 0);
      l_sButton.m_dWidth = l_sButton.m_dCenterWidth + l_sButton.m_piImgNormal[0].m_iWidth + l_sButton.m_piImgNormal[2].m_iWidth;
      break;

    case BUTTON_RADIO:
      l_sButton.m_piImgNormal[0]  = BitmapLoad("img/button_radio.bmp");
      l_sButton.m_piImgHover[0]   = BitmapLoad("img/button_radio_hover.bmp");
      l_sButton.m_piImgPressed[0] = BitmapLoad("img/button_radio_pressed.bmp");

      BitmapConvertWidth(&l_sButton.m_piImgNormal[0],  f_dHeight);
      BitmapConvertWidth(&l_sButton.m_piImgHover[0],   f_dHeight);
      BitmapConvertWidth(&l_sButton.m_piImgPressed[0], f_dHeight);

      l_sButton.m_dCenterWidth = 0;
      l_sButton.m_dHeight = f_dHeight;
      l_sButton.m_dWidth  = f_dWidth;
      break;

    case BUTTON_LABEL:
    default:
      break;
  }

  l_sButton.m_bPressed = FALSE;
  l_sButton.m_bHover   = FALSE;

  return l_sButton;

} // ButtonCreate



int ButtonGetType(struct ButtonStruct *f_sButton)
{
  return f_sButton->m_iType;

} // ButtonGetType



void ButtonClickRender(struct ButtonStruct *f_sButton)
{
  char l_pcTitle[BUTTON_MAX_TITLE];
  struct BitmapStruct *l_sImgButton;
  int  l_iImgWidth, l_iImageIdLeft, l_iImageIdCenter, l_iImageIdRight;
  double l_dXOffset, l_dYOffset;

  double l_dYCoordTop = 1;
  double l_dYCoordBot = 0;

  // copy button attributes
  double l_dCnWdt  = f_sButton->m_dCenterWidth;
  double l_dHeight = f_sButton->m_dHeight;
  double l_dXPos   = f_sButton->m_dXPos;
  double l_dYPos   = f_sButton->m_dYPos;

  if (f_sButton->m_bPressed) {
    //l_dYCoordBot += l_dYCoordTop;
    //l_dYCoordTop -= l_dYCoordTop;
    glColor3d(0.8, 0.9, 1);
  }
  else {
    glColor3d(1, 1, 1);
  }

  // get the right image struct
  if (f_sButton->m_bHover) {
    l_sImgButton = f_sButton->m_piImgHover;
  }
  else {
    l_sImgButton = f_sButton->m_piImgNormal;
  }

  // copy buttom images
  l_iImageIdLeft   = l_sImgButton[0].m_iImageId;
  l_iImageIdCenter = l_sImgButton[1].m_iImageId;
  l_iImageIdRight  = l_sImgButton[2].m_iImageId;

  // copy button title
  memset(&l_pcTitle, 0, sizeof(l_pcTitle));
  memcpy(&l_pcTitle, f_sButton->m_pcTitle, sizeof(f_sButton->m_pcTitle));

  // render left side of button
  glBindTexture(GL_TEXTURE_2D, l_iImageIdLeft);

  l_iImgWidth = l_sImgButton[0].m_iWidth;
  l_dXOffset = l_dXPos;
  l_dYOffset = l_dYPos;

  glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2d(0, l_dYCoordTop); glVertex3d(l_dXOffset + 1,           l_dYOffset,             0);
    glTexCoord2d(1, l_dYCoordTop); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset,             0);
    glTexCoord2d(0, l_dYCoordBot); glVertex3d(l_dXOffset + 1,           l_dYOffset + l_dHeight, 0);
    glTexCoord2d(1, l_dYCoordBot); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset + l_dHeight, 0);
  glEnd();

  l_dXOffset = l_dXPos + l_iImgWidth + l_dCnWdt;

  // render right side of button
  glBindTexture(GL_TEXTURE_2D, l_iImageIdRight);

  l_iImgWidth = l_sImgButton[2].m_iWidth;

  glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2d(0, l_dYCoordTop); glVertex3d(l_dXOffset - 1,           l_dYOffset,             0);
    glTexCoord2d(1, l_dYCoordTop); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset,             0);
    glTexCoord2d(0, l_dYCoordBot); glVertex3d(l_dXOffset - 1,           l_dYOffset + l_dHeight, 0);
    glTexCoord2d(1, l_dYCoordBot); glVertex3d(l_dXOffset + l_iImgWidth, l_dYOffset + l_dHeight, 0);
  glEnd();

  l_dXOffset = l_dXOffset - l_dCnWdt;

  // render center of button
  if (l_dCnWdt > 0)
  {
    glBindTexture(GL_TEXTURE_2D, l_iImageIdCenter);

    glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2d(0, l_dYCoordTop); glVertex3d(l_dXOffset - 1,        l_dYOffset,             0);
      glTexCoord2d(1, l_dYCoordTop); glVertex3d(l_dXOffset + l_dCnWdt, l_dYOffset,             0);
      glTexCoord2d(0, l_dYCoordBot); glVertex3d(l_dXOffset - 1,        l_dYOffset + l_dHeight, 0);
      glTexCoord2d(1, l_dYCoordBot); glVertex3d(l_dXOffset + l_dCnWdt, l_dYOffset + l_dHeight, 0);
    glEnd();
  }

  if (f_sButton->m_bPressed)
  {
    glColor3d(1, 1, 1);
  }

} // ButtonClickRender



void ButtonRadioRender(struct ButtonStruct *f_sButton)
{
  struct BitmapStruct *l_sImgButton;
  int  l_iImageId;

  // copy button attributes
  double l_dWidth  = f_sButton->m_dWidth;
  double l_dHeight = f_sButton->m_dHeight;
  double l_dXPos   = f_sButton->m_dXPos;
  double l_dYPos   = f_sButton->m_dYPos;

  // get the right image struct
  if (f_sButton->m_bHover)
  {
    l_sImgButton = f_sButton->m_piImgHover;
  }
  else
  {
    l_sImgButton = f_sButton->m_piImgNormal;
  }

  // copy buttom images
  l_iImageId = l_sImgButton[0].m_iImageId;

  // render left side of button
  glBindTexture(GL_TEXTURE_2D, l_iImageId);

  glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2d(0, 1); glVertex3d(l_dXPos,            l_dYPos,             0);
    glTexCoord2d(1, 1); glVertex3d(l_dXPos + l_dWidth, l_dYPos,             0);
    glTexCoord2d(0, 0); glVertex3d(l_dXPos,            l_dYPos + l_dHeight, 0);
    glTexCoord2d(1, 0); glVertex3d(l_dXPos + l_dWidth, l_dYPos + l_dHeight, 0);
  glEnd();

  if (f_sButton->m_bPressed)
  {
    l_iImageId = f_sButton->m_piImgPressed[0].m_iImageId;
    glBindTexture(GL_TEXTURE_2D, l_iImageId);

    glBegin(GL_TRIANGLE_STRIP);
      glTexCoord2d(0, 1); glVertex3d(l_dXPos,            l_dYPos,             0);
      glTexCoord2d(1, 1); glVertex3d(l_dXPos + l_dWidth, l_dYPos,             0);
      glTexCoord2d(0, 0); glVertex3d(l_dXPos,            l_dYPos + l_dHeight, 0);
      glTexCoord2d(1, 0); glVertex3d(l_dXPos + l_dWidth, l_dYPos + l_dHeight, 0);
    glEnd();
  }

} // ButtonRadioRender




void ButtonRender(struct ButtonStruct *f_sButton)
{
  int l_iType = f_sButton->m_iType;

  switch (l_iType)
  {
    case BUTTON_CLICK:
      ButtonClickRender(f_sButton);
      break;

    case BUTTON_RADIO:
      ButtonRadioRender(f_sButton);
        break;

    case BUTTON_LABEL:
    default:
      break;
  }

} // ButtonRender



void ButtonEnter(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bHover = TRUE;

} // ButtonHover



void ButtonExit(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bHover = FALSE;

} // ButtonHover



void ButtonPress(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bPressed = !f_sButton->m_bPressed;

} // ButtonPress



void ButtonRelease(struct ButtonStruct *f_sButton)
{
  f_sButton->m_bPressed = FALSE;

} // ButtonPress