-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryGui.cpp
135 lines (123 loc) · 3.75 KB
/
InventoryGui.cpp
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
#include "InventoryGui.h"
#include "DrawFunc.h"
#include "UserSession.h"
InventoryGui::InventoryGui(GuiElement *parent)
{
m_children = 0;
}
void InventoryGui::init()
{
if (m_children != 0)
{
delete[] m_children;
}
User *user = gUserSession->getActiveUser();
int nInventorySize = user->getInventorySize();
m_nNumChildren = nInventorySize;
m_children = new GuiElement*[m_nNumChildren];
float nRadius = 0.4f;
float nBoxSize = 2.0f/nInventorySize;
for (int i=0; i<nInventorySize; i++)
{
float nColorR = 1.0f;
float nColorG = 1.0f;
float nColorB = 1.0f;
m_children[i] = new Button("", this);
m_children[i]->setWidth(nBoxSize);
m_children[i]->setHeight(nBoxSize);
m_children[i]->setColor(nColorR, nColorG, nColorB, 1.0);
float nAngle = 2.0f * 3.1415f * i / nInventorySize;
m_children[i]->setXPos(0.5f - nBoxSize / 2.0f + nRadius * cos(nAngle));
m_children[i]->setYPos(0.5f - nBoxSize / 2.0f + nRadius * sin(nAngle));
Object *invItem = user->getInventoryObject(i);
if (invItem !=0)
{
m_children[i]->setBackground(invItem->getInventoryTexture());
}
}
}
void InventoryGui::drawChildren()
{
for (int i=0; i<m_nNumChildren; i++)
{
GuiElement *child = m_children[i];
child->draw();
}
}
void InventoryGui::draw()
{
User *user = gUserSession->getActiveUser();
int nInventorySize = user->getInventorySize();
glPushMatrix();
float *nPadding = this->getPadding();
glTranslatef(this->getXPos()+this->getWidth()*nPadding[0], this->getYPos()+this->getHeight()*nPadding[2], 0);
glScalef(this->getWidth()*(1.0f-nPadding[0]-nPadding[1]), this->getHeight()*(1.0f-nPadding[2]-nPadding[3]), 0);
drawChildren();
if (m_nClickedChild >= 0 && m_nClickedChild < nInventorySize)
{
float nAngle = 2.0f * 3.1415f * m_nClickedChild / nInventorySize;
float nRadius = 0.15f;
m_options->setXPos(0.5f - m_options->getWidth() / 2.0f + nRadius * cos(nAngle));
m_options->setYPos(0.5f - m_options->getHeight() / 2.0f + nRadius * sin(nAngle));
m_options->draw();
}
glPopMatrix();
}
GuiElement *InventoryGui::getChild(int nIndex)
{
return m_children[nIndex];
}
void InventoryGui::onClick(int nButton, int nState, float nX, float nY)
{
for (int i=0; i<m_nNumChildren; i++)
{
GuiElement *child = m_children[i];
float nChildClickX = (nX - child->getXPos()) / child->getWidth();
float nChildClickY = (nY - child->getYPos()) / child->getHeight();
float *nPadding = child->getPadding();
if (nChildClickX >= nPadding[0] && nChildClickX <= (1.0f - nPadding[1])
&& nChildClickY >= nPadding[2] && nChildClickY <= (1.0f - nPadding[3]))
{
child->onClick(nButton, nState, nChildClickX, nChildClickY);
User *user = gUserSession->getActiveUser();
Object *obj = user->getInventoryObject(i);
if (obj != 0)
{
m_options->getHeaderLabel()->setText(obj->getWorldTile());
m_nClickedChild = i;
}
else
{
m_nClickedChild = -1;
}
return;
}
}
if (m_nClickedChild >= 0)
{
float nChildClickX = (nX - m_options->getXPos()) / m_options->getWidth();
float nChildClickY = (nY - m_options->getYPos()) / m_options->getHeight();
float *nPadding = m_options->getPadding();
if (nChildClickX >= nPadding[0] && nChildClickX <= (1.0f - nPadding[1])
&& nChildClickY >= nPadding[2] && nChildClickY <= (1.0f - nPadding[3]))
{
m_options->onClick(nButton, nState, nChildClickX, nChildClickY);
}
else
{
m_nClickedChild = -1;
}
}
}
void InventoryGui::buttonClicked(Button *button)
{
if (button->getText() == "Cancel")
{
m_nClickedChild = -1;
this->setColorExtra(0, 0, 0, 0);
}
}
void InventoryGui::setOptionsType(OptionsType type)
{
m_options = InventoryOptionsFactory::getOptionsGui(type, this);
}