forked from nbcraft-org/nbcraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIngameBlockSelectionScreen.cpp
More file actions
203 lines (159 loc) · 4.83 KB
/
IngameBlockSelectionScreen.cpp
File metadata and controls
203 lines (159 loc) · 4.83 KB
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
/********************************************************************
Minecraft: Pocket Edition - Decompilation Project
Copyright (C) 2023 iProgramInCpp
The following code is licensed under the BSD 1 clause license.
SPDX-License-Identifier: BSD-1-Clause
********************************************************************/
#include "IngameBlockSelectionScreen.hpp"
#include "PauseScreen.hpp"
#include "client/app/Minecraft.hpp"
#include "client/renderer/entity/ItemRenderer.hpp"
std::string g_sNotAvailableInDemoVersion = "Not available in the demo version";
IngameBlockSelectionScreen::IngameBlockSelectionScreen() :
m_btnPause(0, "Pause")
{
m_selectedSlot = 0;
}
Inventory* IngameBlockSelectionScreen::getInventory()
{
return m_pMinecraft->m_pLocalPlayer->m_pInventory;
}
int IngameBlockSelectionScreen::getBottomY()
{
// -1 for some reason, -2 to make it align between top of screen and top of hotbar instead
return (m_height - 22 * (getSlotsHeight() - 2)) / 2;
}
int IngameBlockSelectionScreen::getSelectedSlot(int x, int y)
{
int slotsHeight = getSlotsHeight();
int bottom = m_height - getBottomY();
int top = bottom - slotsHeight * 22;
int left = m_width / 2 - 87;
if (y < top)
return -1;
if (x < left)
return -1;
int idx = (x - left) / 20;
if (idx > 8)
return -1;
return idx + 9 * slotsHeight - 9 * ((y - top) / 22);
}
int IngameBlockSelectionScreen::getSlotPosX(int x)
{
return m_width / 2 - 88 + 20 * x;
}
int IngameBlockSelectionScreen::getSlotPosY(int y)
{
return m_height - getBottomY() - 22 * y;
}
int IngameBlockSelectionScreen::getSlotsHeight()
{
return (getInventory()->getNumSlots() + 8) / 9;
}
bool IngameBlockSelectionScreen::isAllowed(int slot)
{
return slot >= 0 && slot < getInventory()->getNumSlots();
}
void IngameBlockSelectionScreen::init()
{
m_btnPause.m_width = 40;
m_btnPause.m_xPos = 0;
m_btnPause.m_yPos = 0;
#if TARGET_OS_IPHONE != 0
if (m_pMinecraft->isTouchscreen())
m_buttons.push_back(&m_btnPause);
#endif
Inventory* pInv = getInventory();
int nItems = pInv->getNumItems();
for (int i = 0; i < nItems; i++)
{
if (pInv->getItem(i)->m_itemID == pInv->getSelectedItemId())
{
m_selectedSlot = i;
break;
}
}
if (!isAllowed(m_selectedSlot))
m_selectedSlot = 0;
}
void IngameBlockSelectionScreen::renderSlot(int index, int x, int y, float f)
{
ItemInstance* pItem = getInventory()->getItem(index);
if (ItemInstance::isNull(pItem))
return;
ItemRenderer::renderGuiItem(m_pMinecraft->m_pFont, m_pMinecraft->m_pTextures, pItem, x, y, true);
ItemRenderer::renderGuiItemOverlay(m_pMinecraft->m_pFont, m_pMinecraft->m_pTextures, pItem, x, y);
}
void IngameBlockSelectionScreen::renderSlots()
{
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
m_pMinecraft->m_pTextures->loadAndBindTexture("gui/gui.png");
for (int y = 0; y != -22 * getSlotsHeight(); y -= 22)
blit(m_width / 2 - 182 / 2, m_height - 3 - getBottomY() + y, 0, 0, 182, 22, 0, 0);
if (m_selectedSlot >= 0)
blit(m_width / 2 - 92 + 20 * (m_selectedSlot % 9), m_height - 4 - getBottomY() - 22 * (m_selectedSlot / 9), 0, 22, 24, 22, 0, 0);
for (int y = 0, index = 0; y < getSlotsHeight(); y++)
{
int posY = getSlotPosY(y);
for (int x = 0; x < 9; x++)
{
int posX = getSlotPosX(x);
renderSlot(index++, posX, posY, 0.0f);
}
}
}
void IngameBlockSelectionScreen::renderDemoOverlay()
{
fill(getSlotPosX(0) - 3, getSlotPosY(1) - 3, getSlotPosX(9), getSlotPosY(-1), 0xA0000000);
int x = (getSlotPosX(4) + getSlotPosX(5)) / 2;
int y = (getSlotPosY(0) + getSlotPosY(1)) / 2 + 5;
drawCenteredString(m_pMinecraft->m_pFont, g_sNotAvailableInDemoVersion, x, y, 0xFFFFFFFF);
}
void IngameBlockSelectionScreen::render(int x, int y, float f)
{
Screen::render(x, y, f);
glDisable(GL_DEPTH_TEST);
fill(0, 0, m_width, m_height, 0x80000000);
glEnable(GL_BLEND);
renderSlots();
#ifdef DEMO
renderDemoOverlay();
#endif
glEnable(GL_DEPTH_TEST);
}
void IngameBlockSelectionScreen::buttonClicked(Button* pButton)
{
if (pButton->m_buttonId == m_btnPause.m_buttonId)
m_pMinecraft->setScreen(new PauseScreen);
}
void IngameBlockSelectionScreen::mouseClicked(int x, int y, int type)
{
Screen::mouseClicked(x, y, type);
// not a left click
if (type != 1)
return;
int slot = getSelectedSlot(x, y);
if (isAllowed(slot))
m_selectedSlot = slot;
}
void IngameBlockSelectionScreen::mouseReleased(int x, int y, int type)
{
Screen::mouseReleased(x, y, type);
// not a left click
if (type != 1)
return;
int slot = getSelectedSlot(x, y);
if (isAllowed(slot) && slot == m_selectedSlot)
selectSlotAndClose();
}
void IngameBlockSelectionScreen::removed()
{
m_pMinecraft->m_gui.inventoryUpdated();
}
void IngameBlockSelectionScreen::selectSlotAndClose()
{
Inventory* pInv = getInventory();
pInv->selectItem(m_selectedSlot, m_pMinecraft->m_gui.getNumUsableSlots());
m_pMinecraft->m_pSoundEngine->playUI("random.click");
m_pMinecraft->setScreen(nullptr);
}