aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/UIScene_InventoryMenu.cpp
blob: 4e217c772029ddf64025c50ff836eb7151b5eb03 (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
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
#include "stdafx.h"
#include "UI.h"
#include "UIScene_InventoryMenu.h"

#include "..\..\..\Minecraft.World\net.minecraft.world.inventory.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.item.h"
#include "..\..\..\Minecraft.World\net.minecraft.stats.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.effect.h"
#include "..\..\MultiplayerLocalPlayer.h"
#include "..\..\Minecraft.h"
#include "..\..\Options.h"
#include "..\..\EntityRenderDispatcher.h"
#include "..\..\Lighting.h"
#include "..\Tutorial\Tutorial.h"
#include "..\Tutorial\TutorialMode.h"
#include "..\Tutorial\TutorialEnum.h"

#define INVENTORY_UPDATE_EFFECTS_TIMER_ID (10)
#define INVENTORY_UPDATE_EFFECTS_TIMER_TIME (1000) // 1 second

UIScene_InventoryMenu::UIScene_InventoryMenu(int iPad, void *_initData, UILayer *parentLayer) : UIScene_AbstractContainerMenu(iPad, parentLayer)
{
	// Setup all the Iggy references we need for this scene
	initialiseMovie();

	InventoryScreenInput *initData = static_cast<InventoryScreenInput *>(_initData);

	Minecraft *pMinecraft = Minecraft::GetInstance();
	if( pMinecraft->localgameModes[initData->iPad] != nullptr )
	{
		TutorialMode *gameMode = static_cast<TutorialMode *>(pMinecraft->localgameModes[initData->iPad]);
		m_previousTutorialState = gameMode->getTutorial()->getCurrentState();
		gameMode->getTutorial()->changeTutorialState(e_Tutorial_State_Inventory_Menu, this);
	}

	InventoryMenu *menu = static_cast<InventoryMenu *>(initData->player->inventoryMenu);

	initData->player->awardStat(GenericStats::openInventory(),GenericStats::param_openInventory());

	Initialize( initData->iPad, menu, false, InventoryMenu::INV_SLOT_START, eSectionInventoryUsing, eSectionInventoryMax, initData->bNavigateBack );

	m_slotListArmor.addSlots(InventoryMenu::ARMOR_SLOT_START, InventoryMenu::ARMOR_SLOT_END - InventoryMenu::ARMOR_SLOT_START);

	if(initData) delete initData;

	for(unsigned int i = 0; i < MobEffect::NUM_EFFECTS; ++i)
	{
		m_bEffectTime[i] = 0;
	}

	updateEffectsDisplay();
	addTimer(INVENTORY_UPDATE_EFFECTS_TIMER_ID,INVENTORY_UPDATE_EFFECTS_TIMER_TIME);
}

wstring UIScene_InventoryMenu::getMoviePath()
{
	if(app.GetLocalPlayerCount() > 1)
	{
		return L"InventoryMenuSplit";
	}
	else
	{
		return L"InventoryMenu";
	}
}

void UIScene_InventoryMenu::handleReload()
{
	Initialize( m_iPad, m_menu, false, InventoryMenu::INV_SLOT_START, eSectionInventoryUsing, eSectionInventoryMax, m_bNavigateBack );

	m_slotListArmor.addSlots(InventoryMenu::ARMOR_SLOT_START, InventoryMenu::ARMOR_SLOT_END - InventoryMenu::ARMOR_SLOT_START);

	for(unsigned int i = 0; i < MobEffect::NUM_EFFECTS; ++i)
	{
		m_bEffectTime[i] = 0;
	}
}

int UIScene_InventoryMenu::getSectionColumns(ESceneSection eSection)
{
	int cols = 0;
	switch( eSection )
	{
	case eSectionInventoryArmor:
		cols = 1;
		break;
	case eSectionInventoryInventory:
		cols = 9;
		break;
	case eSectionInventoryUsing:
		cols = 9;
		break;
	default:
		assert( false );
		break;
	}
	return cols;
}

int UIScene_InventoryMenu::getSectionRows(ESceneSection eSection)
{
	int rows = 0;
	switch( eSection )
	{
	case eSectionInventoryArmor:
		rows = 4;
		break;
	case eSectionInventoryInventory:
		rows = 3;
		break;
	case eSectionInventoryUsing:
		rows = 1;
		break;
	default:
		assert( false );
		break;
	}
	return rows;
}

void UIScene_InventoryMenu::GetPositionOfSection( ESceneSection eSection, UIVec2D* pPosition )
{
	switch( eSection )
	{
	case eSectionInventoryArmor:
		pPosition->x = m_slotListArmor.getXPos();
		pPosition->y = m_slotListArmor.getYPos();
		break;
	case eSectionInventoryInventory:
		pPosition->x = m_slotListInventory.getXPos();
		pPosition->y = m_slotListInventory.getYPos();
		break;
	case eSectionInventoryUsing:
		pPosition->x = m_slotListHotbar.getXPos();
		pPosition->y = m_slotListHotbar.getYPos();
		break;
	default:
		assert( false );
		break;
	}
}

void UIScene_InventoryMenu::GetItemScreenData( ESceneSection eSection, int iItemIndex, UIVec2D* pPosition, UIVec2D* pSize )
{
	UIVec2D sectionSize;

	switch( eSection )
	{
	case eSectionInventoryArmor:
		sectionSize.x = m_slotListArmor.getWidth();
		sectionSize.y = m_slotListArmor.getHeight();
		break;
	case eSectionInventoryInventory:
		sectionSize.x = m_slotListInventory.getWidth();
		sectionSize.y = m_slotListInventory.getHeight();
		break;
	case eSectionInventoryUsing:
		sectionSize.x = m_slotListHotbar.getWidth();
		sectionSize.y = m_slotListHotbar.getHeight();
		break;
	default:
		assert( false );
		break;
	}

	int rows = getSectionRows(eSection);
	int cols = getSectionColumns(eSection);

	pSize->x = sectionSize.x/cols;
	pSize->y = sectionSize.y/rows;

	int itemCol = iItemIndex % cols;
	int itemRow = iItemIndex/cols;

	pPosition->x = itemCol * pSize->x;
	pPosition->y = itemRow * pSize->y;
}

void UIScene_InventoryMenu::setSectionSelectedSlot(ESceneSection eSection, int x, int y)
{
	int cols = getSectionColumns(eSection);

	int index = (y * cols) + x;

	UIControl_SlotList *slotList = nullptr;
	switch( eSection )
	{
	case eSectionInventoryArmor:
		slotList = &m_slotListArmor;
		break;
	case eSectionInventoryInventory:
		slotList = &m_slotListInventory;
		break;
	case eSectionInventoryUsing:
		slotList = &m_slotListHotbar;
		break;
	}

	slotList->setHighlightSlot(index);
}

UIControl *UIScene_InventoryMenu::getSection(ESceneSection eSection)
{
	UIControl *control = nullptr;
	switch( eSection )
	{
	case eSectionInventoryArmor:
		control = &m_slotListArmor;
		break;
	case eSectionInventoryInventory:
		control = &m_slotListInventory;
		break;
	case eSectionInventoryUsing:
		control = &m_slotListHotbar;
		break;
	}
	return control;
}

void UIScene_InventoryMenu::customDraw(IggyCustomDrawCallbackRegion *region)
{
	Minecraft *pMinecraft = Minecraft::GetInstance();
	if(pMinecraft->localplayers[m_iPad] == nullptr || pMinecraft->localgameModes[m_iPad] == nullptr) return;

	if(wcscmp((wchar_t *)region->name,L"player")==0)
	{
		// Setup GDraw, normal game render states and matrices
		CustomDrawData *customDrawRegion = ui.setupCustomDraw(this,region);
		delete customDrawRegion;

		m_playerPreview.render(region);

		// Finish GDraw and anything else that needs to be finalised
		ui.endCustomDraw(region);
	}
	else
	{
		UIScene_AbstractContainerMenu::customDraw(region);
	}
}

void UIScene_InventoryMenu::handleTimerComplete(int id)
{
	if(id == INVENTORY_UPDATE_EFFECTS_TIMER_ID)
	{
		updateEffectsDisplay();
	}
}

void UIScene_InventoryMenu::updateEffectsDisplay()
{
	// Update with the current effects
	Minecraft *pMinecraft = Minecraft::GetInstance();
	shared_ptr<MultiplayerLocalPlayer> player = pMinecraft->localplayers[m_iPad];

	if(player == nullptr) return;

	vector<MobEffectInstance *> *activeEffects = player->getActiveEffects();

	// 4J - TomK setup time update value array size to update the active effects
	int iValue = 0;
	IggyDataValue *UpdateValue = new IggyDataValue[activeEffects->size()*2];

	for(auto& effect : *activeEffects)
	{
		if(effect->getDuration() >= m_bEffectTime[effect->getId()])
		{
			wstring effectString = app.GetString( effect->getDescriptionId() );//I18n.get(effect.getDescriptionId()).trim();
			if (effect->getAmplifier() > 0)
			{
				wstring potencyString = L"";
				switch(effect->getAmplifier())
				{
				case 1:
					potencyString = L" ";
					potencyString += app.GetString( IDS_POTION_POTENCY_1 );
					break;
				case 2:
					potencyString = L" ";
					potencyString += app.GetString( IDS_POTION_POTENCY_2 );
					break;
				case 3:
					potencyString = L" ";
					potencyString += app.GetString( IDS_POTION_POTENCY_3 );
					break;
				default:
					potencyString = app.GetString( IDS_POTION_POTENCY_0 );
					break;
				}
				effectString += potencyString;
			}
			int icon = 0;
			MobEffect *mobEffect = MobEffect::effects[effect->getId()];
			if (mobEffect->hasIcon())
			{
				icon = mobEffect->getIcon();
            }
			IggyDataValue result;
			IggyDataValue value[3];
			value[0].type = IGGY_DATATYPE_number;
			value[0].number = icon;

			IggyStringUTF16 stringVal;
			stringVal.string = (IggyUTF16*)effectString.c_str();
			stringVal.length = effectString.length();
			value[1].type = IGGY_DATATYPE_string_UTF16;
			value[1].string16 = stringVal;

			int seconds = effect->getDuration() / SharedConstants::TICKS_PER_SECOND;
			value[2].type = IGGY_DATATYPE_number;
			value[2].number = seconds;
			IggyResult out = IggyPlayerCallMethodRS ( getMovie() , &result, IggyPlayerRootPath( getMovie() ), m_funcAddEffect , 3 , value );
		}

		if(MobEffect::effects[effect->getId()]->hasIcon())
		{
			// 4J - TomK set ids and remaining duration so we can update the timers accurately in one call! (this prevents performance related timer sync issues, especially on PSVita)
			UpdateValue[iValue].type = IGGY_DATATYPE_number;
			UpdateValue[iValue].number = MobEffect::effects[effect->getId()]->getIcon();
			UpdateValue[iValue + 1].type = IGGY_DATATYPE_number;
			UpdateValue[iValue + 1].number = (int)(effect->getDuration() / SharedConstants::TICKS_PER_SECOND);
			iValue+=2;
		}

		m_bEffectTime[effect->getId()] = effect->getDuration();
	}

	IggyDataValue result;
	IggyResult out = IggyPlayerCallMethodRS ( getMovie() , &result, IggyPlayerRootPath( getMovie() ), m_funcUpdateEffects , activeEffects->size()*2 , UpdateValue );

	delete activeEffects;
}