-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui_bag.c
354 lines (318 loc) · 8.57 KB
/
gui_bag.c
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#ifndef GUI_GAMEFRAME_C
#define GUI_GAMEFRAME_C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <X11/Xlib.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <pthread.h>
#include <math.h>
#include <signal.h>
#include "types.h"
#include "util.h"
#include "color.h"
#define GUI_WIDGET
#include "gui.h"
#undef GUI_WIDGET
#include "gui_bag.h"
////////////////////////////////////////////////////////////////////////////////
static gem_t* bag_get_nearest_gem(widget_t *w, int x, int y, int *ndx)
{
gem_t *gem = NULL;
float xf,yf,xs,ys,d,md = 1000000.0f;
int i;
// Find the closest gem to the mouse..
*ndx = -1;
for(i=0; i<(sizeof(Statec->player.bag.items)/sizeof(item_t)); i++) {
// Skip the current selection.
if( i != GuiState.mouse_item_ndx ) {
switch(Statec->player.bag.items[i].type) {
case BAG_ITEM_TYPE_GEM:
// Find position of the gem
xf = i%3;
xf = xf/3.0;
xf = xf + (1.0f/6.0f);
yf = i/3;
yf = yf/15.0;
yf = yf + (1.0f/30.0f);
xs = ScaleX(w,xf*w->w+w->x);
ys = ScaleY(w,yf*w->h+w->y);
// Get distance to mouse
d = sqrt((xs-x)*(xs-x) + (ys-y)*(ys-y));
if( d < md && ISCLOSE(d) ) {
*ndx = i;
md = d;
gem = &(Statec->player.bag.items[i].gem);
}
break;
}
}
}
// Return the gem if we got one.
return gem;
}
////////////////////////////////////////////////////////////////////////////////
// Event Callbacks
////////////////////////////////////////////////////////////////////////////////
//
// Mouse callbacks
//
void Bag_Down(widget_t *w, const int x, const int y, const int b)
{
gem_t *gem = NULL;
int ndx;
if( Statec->player.mana < 0 ) {
return;
}
if( (x > ScaleX(w,w->x)) && (x < ScaleX(w,w->x+w->w)) &&
(y > ScaleY(w,w->y)) && (y < ScaleY(w,w->y+w->h)) ) {
switch(b) {
case MOUSE_LEFT:
// If we have a gem selected, and we are close to another gem, mix them.
// Otherwise, add the selected gem back into the bag.
if ( GuiState.mouse_item_ndx != -1 ) {
gem = bag_get_nearest_gem(w,x,y,&ndx);
if ( gem ) {
// Add a game event to mix the gems.
game_event_mix_gems(ndx, GuiState.mouse_item_ndx);
}
// Always deselect the gem, even if we didn't mix
GuiState.mouse_item_ndx = -1;
} else {
// No gem selected, do a regular select: find the closest gem to the mouse..
gem = bag_get_nearest_gem(w,x,y,&ndx);
if( gem ) {
// Pick up gem into mouse (escape key / other event will drop)
GuiState.mouse_item_ndx = ndx;
}
}
break;
case MOUSE_RIGHT:
// Clear the bag mouse selection.
GuiState.mouse_item_ndx = -1;
break;
}
}
}
void Bag_MouseMove(widget_t *w, int x, int y)
{
//gem_t *gem = NULL;
//int ndx;
if( Statec->player.mana < 0 ) {
return;
}
}
//
// Key callbacks
//
void Bag_KeyPress(widget_t *w, char key, unsigned int keycode)
{
if( Statec->player.mana < 0 ) {
return;
}
if( GuiState.mouse_item_ndx != -1 ) {
GuiState.mouse_item_ndx = -1;
}
}
////////////////////////////////////////////////////////////////////////////////
// Draw routines.
////////////////////////////////////////////////////////////////////////////////
static int BuildBagBG()
{
int bgtex,cl,i;
float a,b;
// I use these values to slightly tweak the textures positions.
a = 0.0f;
b = 1.0f;
// I guess we can load this here..
bgtex = LoadTexture("data/bmp/BagBG.bmp");
// Start a list
glNewList(cl=glGenLists(1),GL_COMPILE);
// Draw cloth BG.
glColor3f(0.5f,0.5f,0.5f);
glBindTexture(GL_TEXTURE_2D,bgtex);
glBegin(GL_QUADS);
glTexCoord2f(a,b); glVertex3i(0.0, 1.0, 1.0);
glTexCoord2f(a,a); glVertex3i(0.0, 0.0, 1.0);
glTexCoord2f(b,a); glVertex3i(1.0, 0.0, 1.0);
glTexCoord2f(b,b); glVertex3i(1.0, 1.0, 1.0);
glEnd();
// Draw an item grid.
glDisable(GL_TEXTURE_2D);
glColor4ub(0,0,0,96);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for(i=0; i<2; i++) {
glBegin(GL_QUADS);
glVertex2f((1.0/3.0)*(i+1)+0.015, 0.0);
glVertex2f((1.0/3.0)*(i+1)-0.015, 0.0);
glVertex2f((1.0/3.0)*(i+1)-0.015, 1.0);
glVertex2f((1.0/3.0)*(i+1)+0.015, 1.0);
glEnd();
}
for(i=0; i<14; i++) {
glBegin(GL_QUADS);
glVertex2f(0.0, (1.0/15.0)*(i+1)+0.005);
glVertex2f(0.0, (1.0/15.0)*(i+1)-0.005);
glVertex2f(1.0, (1.0/15.0)*(i+1)-0.005);
glVertex2f(1.0, (1.0/15.0)*(i+1)+0.005);
glEnd();
}
glDisable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
// End the list.
glEndList();
// Return the new call list
return cl;
}
void Gem_Draw(gem_t *gem, double xf, double yf, double ratio)
{
double r;
int j;
// Gem border
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
r = 20 / 255.0;
for(j=0; j<6; ) {
glVertex3f(xf+r*cos(2*3.14159265*(j/5.0)),
yf+r*ratio*sin(2*3.14159265*(j/5.0)), 1.0f );
j++;
glVertex3f(xf+r*cos(2*3.14159265*(j/5.0)),
yf+r*ratio*sin(2*3.14159265*(j/5.0)), 1.0f );
}
glEnd();
glBegin(GL_POLYGON);
// Gem center
glColor3f( (gem->color.a[0])/255.0,
(gem->color.a[1])/255.0,
(gem->color.a[2])/255.0 );
r = 16 / 255.0;
for(j=0; j<6; ) {
glVertex2f(xf+r*cos(2*3.14159265*(j/5.0)),
yf+r*ratio*sin(2*3.14159265*(j/5.0)) );
j++;
glVertex2f(xf+r*cos(2*3.14159265*(j/5.0)),
yf+r*ratio*sin(2*3.14159265*(j/5.0)) );
}
glEnd();
}
void Bag_Draw(widget_t *w)
{
static int bg = 0;
u32b_t i,x,y;
double xf,yf,ratio=w->w/((double)w->h);
gem_t *gem = NULL;
int ndx;
// Init if needed.. I guess this can be here.
if( bg == 0 ) {
glEnable(GL_TEXTURE_2D);
bg = BuildBagBG();
glDisable(GL_TEXTURE_2D);
}
// Draw background.
glEnable(GL_TEXTURE_2D);
White();
glCallList(bg);
glDisable(GL_TEXTURE_2D);
// Draw the items
for(i=0; i<(sizeof(Statec->player.bag.items)/sizeof(item_t)); i++) {
// Find position of items
x = i%3;
xf = x/3.0;
xf = xf + (1.0f/6.0f);
y = i/3;
yf = y/15.0;
yf = yf + (1.0f/30.0f);
// Figure out item type
switch(Statec->player.bag.items[i].type) {
case BAG_ITEM_TYPE_GEM:
if( i != GuiState.mouse_item_ndx ) {
Gem_Draw(&(Statec->player.bag.items[i].gem),xf,yf,ratio);
} else {
}
break;
}
}
// Outline
Yellow();
glBegin(GL_LINE_LOOP);
glVertex2f(0.0f,0.0f);
glVertex2f(0.0f,1.0f);
glVertex2f(1.0f,1.0f);
glVertex2f(1.0f,0.0f);
glEnd();
// Only if no gems selected.
if( GuiState.mouse_item_ndx == -1 ) {
if( (GuiState.hand_pos.s.x > ScaleX(w,w->x)) && (GuiState.hand_pos.s.x < ScaleX(w,w->x+w->w)) &&
(GuiState.hand_pos.s.y > ScaleY(w,w->y)) && (GuiState.hand_pos.s.y < ScaleY(w,w->y+w->h)) ) {
// Find nearest gem.
gem = bag_get_nearest_gem(w,GuiState.hand_pos.s.x,GuiState.hand_pos.s.y,&ndx);
if( gem ) {
// Highlight the gem.
xf = ndx%3;
yf = ndx/3;
glColor3ub(128,128,0);
glBegin(GL_LINE_LOOP);
glVertex2f(xf/3.0f, yf/15.0f);
glVertex2f(xf/3.0f, (yf+1)/15.0f);
glVertex2f((xf+1)/3.0f, (yf+1)/15.0f);
glVertex2f((xf+1)/3.0f, yf/15.0f);
glEnd();
// Draw the hover box for the current hover gem.
glPushMatrix();
glLoadIdentity();
xf = GuiState.hand_pos.s.x;
yf = GuiState.hand_pos.s.y;
if( xf+64 > w->glw->width ) {
xf -= (xf+64) - w->glw->width;
}
if( yf+64 > w->glw->height ) {
yf -= (yf+64) - w->glw->height;
}
glTranslatef(xf, yf, 0.0f);
// Background
glColor4ub(0,0,0,220);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_POLYGON);
glVertex2f(0.0f,0.0f);
glVertex2f(0.0f,64.0f);
glVertex2f(64.0f,64.0f);
glVertex2f(64.0f,0.0f);
glEnd();
glDisable(GL_BLEND);
// Text
Red();
glRasterPos2f(strlen("r: ")*6.0f/2.0f, 16.0f);
printGLf(w->glw->font,"r: %.1lf",gem->color.s.x);
Green();
glRasterPos2f(strlen("g: ")*6.0f/2.0f, 16.0f+16.0f*1);
printGLf(w->glw->font,"g: %.1lf",gem->color.s.y);
Blue();
glRasterPos2f(strlen("b: ")*6.0f/2.0f, 16.0f+16.0f*2);
printGLf(w->glw->font,"b: %.1lf",gem->color.s.z);
// Outline
Yellow();
glBegin(GL_LINE_LOOP);
glVertex2f(0.0f,0.0f);
glVertex2f(0.0f,64.0f);
glVertex2f(64.0f,64.0f);
glVertex2f(64.0f,0.0f);
glEnd();
glPopMatrix();
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
#endif // !GUI_BAG_C