-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui.c
243 lines (176 loc) · 4.87 KB
/
gui.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
#include "gui.h"
#include "utils.h"
#include "client.h"
static GLuint gui_texture=0;
void drawBackground(Vec2f pos, Vec2f size, int background)
{
Vec2f border={4,4};
Vec2f texBorder={0.25,0.25};
Vec2f texBase={background%16,background/16};
Vec2f vertexCoord[4]=
{
pos,
pos+border,
pos+size-border,
pos+size,
};
Vec2f texCoord[4]=
{
(texBase)/(Vec2f){16,16},
(texBase+texBorder)/(Vec2f){16,16},
(texBase+(Vec2f){1,1}-texBorder)/(Vec2f){16,16},
(texBase+(Vec2f){1,1})/(Vec2f){16,16},
};
glBegin(GL_QUADS);
for(int y=0;y<3;y++)
for(int x=0;x<3;x++)
{
glTexCoord2f(texCoord[x ][0],texCoord[y ][1]);
glVertex2f(vertexCoord[x ][0],vertexCoord[y ][1]);
glTexCoord2f(texCoord[x+1][0],texCoord[y ][1]);
glVertex2f(vertexCoord[x+1][0],vertexCoord[y ][1]);
glTexCoord2f(texCoord[x+1][0],texCoord[y+1][1]);
glVertex2f(vertexCoord[x+1][0],vertexCoord[y+1][1]);
glTexCoord2f(texCoord[x ][0],texCoord[y+1][1]);
glVertex2f(vertexCoord[x ][0],vertexCoord[y+1][1]);
}
glEnd();
}
void drawSlot(const Slot* slot)
{
drawBackground(slot->pos,slot->size,2);
}
void drawLabel(const Label* label)
{
}
void drawIcon(const Icon* icon)
{
}
/*
public bool equipmentEvent(Client* client, SDL_Event* event)
{
return true;
}
public void equipmentDraw(Client* client)
{
glPushMatrix();
glTranslatef(equipmentFrame.pos[0],equipmentFrame.pos[1],0);
drawBackground((Vec2f){0,0},equipmentFrame.size,1);
for(int i=0;i<lengthof(equipmentSlots);i++)
drawSlot(&equipmentSlots[i]);
drawLabel(&equipmentCraftingLabel);
drawIcon(&equipmentCraftingIcon);
drawBackground(equipmentPlayerView.pos,equipmentPlayerView.size,3);
glPopMatrix();
}
*/
/*
*/
private void componentDraw(Window* window, Component* component)
{
switch(component->type)
{
case SLOT:
drawBackground(component->pos,component->size,2);
break;
case BUTTON:
{
int background;
if(!((Button*)component)->enabled)
background=8;
else if(window->focus==component)
background=6;
else
background=7;
drawBackground(component->pos,component->size,background);
}
break;
default:
panic("unable to draw component type='%i'",component->type);
}
}
private void containerDraw(Window* window, Container* container)
{
int child=container->firstChild;
while(child>=0)
{
componentDraw(window,&window->component[child]);
child=window->component[child].nextChild;
}
}
public void windowDraw(Window* window)
{
if(gui_texture==0)
{
gui_texture=loadTexture("gui.png");
assert(gui_texture!=0);
}
glBindTexture(GL_TEXTURE_2D,gui_texture);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glPushMatrix();
glTranslatef(window->pos[0],window->pos[1],0);
if(window->background)
drawBackground((Vec2f){0,0},window->size,1);
containerDraw(window,window);
glPopMatrix();
}
private bool componentContains(Component* component, Vec2f point)
{
return
point[0] >= component->pos[0] &&
point[1] >= component->pos[1] &&
point[0] < component->pos[0]+component->size[0] &&
point[1] < component->pos[1]+component->size[1];
}
private bool buttonEvent(Window* window, int id, Event* event)
{
Button* button=&window->component[id].button;
window->focus=button;
if(event->type==MOUSE_DOWN && button->enabled)
printf("%i\n",id);
return false;
}
private bool componentEvent(Window* window, int id, Event* event)
{
switch(window->component[id].type)
{
case BUTTON:
return buttonEvent(window,id,event);
default:
return true;
}
return true;
}
private bool containerEvent(Window* window, Container* container, Event* event)
{
int child=container->firstChild;
while(child>=0)
{
Component* component=&window->component[child];
if(componentContains(component,event->mouse))
{
Event tmpEvent=*event;
tmpEvent.mouse-=container->pos;
componentEvent(window,child,&tmpEvent);
}
child=component->nextChild;
}
return false;
}
public bool windowEvent(Window* window, Event* event)
{
//printf("event: %i\n",event->type);
if(event->type==MOUSE_MOTION)
window->focus=NULL;
if(event->type==MOUSE_MOTION || event->type==MOUSE_UP || event->type==MOUSE_DOWN)
{
Event tmpEvent=*event;
tmpEvent.mouse-=window->pos;
return containerEvent(window,window,&tmpEvent);
}
else
{
return true;
}
}