aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/UIScene_TradingMenu.cpp
blob: bb9e30af4c21a6fc2c46afff0eb43b6b46203d0d (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
#include "stdafx.h"
#include "UI.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.inventory.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.item.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.item.trading.h"
#include "..\..\..\Minecraft.World\net.minecraft.world.level.tile.entity.h"
#include "MultiPlayerLocalPlayer.h"
#include "..\..\Minecraft.h"
#include "UIScene_TradingMenu.h"

UIScene_TradingMenu::UIScene_TradingMenu(int iPad, void *_initData, UILayer *parentLayer) : UIScene(iPad, parentLayer)
{
	// Setup all the Iggy references we need for this scene
	initialiseMovie();
	
	m_showingLeftArrow = true;
	m_showingRightArrow = true;

	// 4J-PB - "Villager" appears for a short time on opening the trading menu
	//m_labelTrading.init( app.GetString(IDS_VILLAGER) );
	m_labelTrading.init( L"" );
	m_labelInventory.init( app.GetString(IDS_INVENTORY) );
	m_labelRequired.init( app.GetString(IDS_REQUIRED_ITEMS_FOR_TRADE) );

	m_labelRequest1.init(L"");
	m_labelRequest2.init(L"");

	TradingScreenInput *initData = static_cast<TradingScreenInput *>(_initData);
	m_merchant = initData->trader;

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

	m_menu = new MerchantMenu( initData->inventory, initData->trader, initData->level );
	
	Minecraft::GetInstance()->localplayers[iPad]->containerMenu = m_menu;

	m_slotListRequest1.addSlots(BUY_A,1);
	m_slotListRequest2.addSlots(BUY_B,1);

	m_slotListTrades.addSlots(TRADES_START,DISPLAY_TRADES_COUNT);

	m_slotListInventory.addSlots(MerchantMenu::INV_SLOT_START, 27);
	m_slotListHotbar.addSlots(MerchantMenu::USE_ROW_SLOT_START, 9);

	if(initData) delete initData;

	// in this scene, we override the press sound with our own for crafting success or fail
	ui.OverrideSFX(m_iPad,ACTION_MENU_A,true);
	ui.OverrideSFX(m_iPad,ACTION_MENU_OK,true);
#ifdef __ORBIS__
	ui.OverrideSFX(m_iPad,ACTION_MENU_TOUCHPAD_PRESS,true);
#endif
	ui.OverrideSFX(m_iPad,ACTION_MENU_LEFT_SCROLL,true);
	ui.OverrideSFX(m_iPad,ACTION_MENU_RIGHT_SCROLL,true);
	ui.OverrideSFX(m_iPad,ACTION_MENU_LEFT,true);
	ui.OverrideSFX(m_iPad,ACTION_MENU_RIGHT,true);
	ui.OverrideSFX(m_iPad,ACTION_MENU_UP,true);
	ui.OverrideSFX(m_iPad,ACTION_MENU_DOWN,true);

	app.SetRichPresenceContext(iPad, CONTEXT_GAME_STATE_TRADING);
}

wstring UIScene_TradingMenu::getMoviePath()
{
	if(app.GetLocalPlayerCount() > 1)
	{
		return L"TradingMenuSplit";
	}
	else
	{
		return L"TradingMenu";
	}
}

void UIScene_TradingMenu::updateTooltips()
{
	ui.SetTooltips(m_iPad, IDS_TOOLTIPS_TRADE, IDS_TOOLTIPS_BACK);
}

void UIScene_TradingMenu::handleDestroy()
{
	app.DebugPrintf("UIScene_TradingMenu::handleDestroy\n");
	Minecraft *pMinecraft = Minecraft::GetInstance();
	if( pMinecraft->localgameModes[m_iPad] != nullptr )
	{
		TutorialMode *gameMode = static_cast<TutorialMode *>(pMinecraft->localgameModes[m_iPad]);
		if(gameMode != nullptr) gameMode->getTutorial()->changeTutorialState(m_previousTutorialState);
	}

	// 4J Stu - Fix for #11302 - TCR 001: Network Connectivity: Host crashed after being killed by the client while accessing a chest during burst packet loss.
	// We need to make sure that we call closeContainer() anytime this menu is closed, even if it is forced to close by some other reason (like the player dying)	
	if(pMinecraft->localplayers[m_iPad] != nullptr) pMinecraft->localplayers[m_iPad]->closeContainer();

	ui.OverrideSFX(m_iPad,ACTION_MENU_A,false);
	ui.OverrideSFX(m_iPad,ACTION_MENU_OK,false);
#ifdef __ORBIS__
	ui.OverrideSFX(m_iPad,ACTION_MENU_TOUCHPAD_PRESS,false);
#endif
	ui.OverrideSFX(m_iPad,ACTION_MENU_LEFT_SCROLL,false);
	ui.OverrideSFX(m_iPad,ACTION_MENU_RIGHT_SCROLL,false);
	ui.OverrideSFX(m_iPad,ACTION_MENU_LEFT,false);
	ui.OverrideSFX(m_iPad,ACTION_MENU_RIGHT,false);
	ui.OverrideSFX(m_iPad,ACTION_MENU_UP,false);
	ui.OverrideSFX(m_iPad,ACTION_MENU_DOWN,false);
}

void UIScene_TradingMenu::handleReload()
{
	m_slotListRequest1.addSlots(BUY_A,1);
	m_slotListRequest2.addSlots(BUY_B,1);

	m_slotListTrades.addSlots(TRADES_START,DISPLAY_TRADES_COUNT);

	m_slotListInventory.addSlots(MerchantMenu::INV_SLOT_START, 27);
	m_slotListHotbar.addSlots(MerchantMenu::USE_ROW_SLOT_START, 9);

	updateDisplay();
	
	IggyDataValue result;
	IggyDataValue value[1];

	value[0].type = IGGY_DATATYPE_number;
	value[0].number = m_selectedSlot;
	IggyResult out = IggyPlayerCallMethodRS ( getMovie() , &result, IggyPlayerRootPath( getMovie() ), m_funcSetActiveSlot , 1 , value );
}

void UIScene_TradingMenu::tick()
{
	UIScene::tick();
	handleTick();
}

void UIScene_TradingMenu::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled)
{
	//app.DebugPrintf("UIScene_InventoryMenu handling input for pad %d, key %d, down- %s, pressed- %s, released- %s\n", iPad, key, down?"TRUE":"FALSE", pressed?"TRUE":"FALSE", released?"TRUE":"FALSE");
	ui.AnimateKeyPress(m_iPad, key, repeat, pressed, released);

	switch(key)
	{
	default:
		if(pressed)
		{
			handled = handleKeyDown(m_iPad, key, repeat);
		}
		break;
	};
}

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

	shared_ptr<ItemInstance> item = nullptr;
	int slotId = -1;
	swscanf(static_cast<wchar_t *>(region->name),L"slot_%d",&slotId);

	if(slotId < MerchantMenu::USE_ROW_SLOT_END)
	{			
		Slot *slot = m_menu->getSlot(slotId);
		item = slot->getItem();
	}
	else if(slotId >= TRADES_START)
	{
		int tradeId = (slotId - TRADES_START) + m_offersStartIndex;
		if(tradeId < m_activeOffers.size())
		{
			item = m_activeOffers.at(tradeId).first->getSellItem();
		}
	}
	else
	{
		int tradeId = m_selectedSlot + m_offersStartIndex;
		if( tradeId < m_activeOffers.size() )
		{
			switch(slotId)
			{
			case BUY_A:
				item = m_activeOffers.at(tradeId).first->getBuyAItem();
				break;
			case BUY_B:
				item = m_activeOffers.at(tradeId).first->getBuyBItem();
				break;
			};
		}
	}
	if(item != nullptr) customDrawSlotControl(region,m_iPad,item,1.0f,item->isFoil(),true);
}

void UIScene_TradingMenu::showScrollRightArrow(bool show)
{
	if(m_showingRightArrow != show)
	{
		IggyDataValue result;
		IggyDataValue value[1];

		value[0].type = IGGY_DATATYPE_boolean;
		value[0].boolval = show;
		IggyResult out = IggyPlayerCallMethodRS ( getMovie() , &result, IggyPlayerRootPath( getMovie() ), m_funcShowScrollRightArrow , 1 , value );

		m_showingRightArrow = show;
	}
}

void UIScene_TradingMenu::showScrollLeftArrow(bool show)
{
	if(m_showingLeftArrow != show)
	{
		IggyDataValue result;
		IggyDataValue value[1];

		value[0].type = IGGY_DATATYPE_boolean;
		value[0].boolval = show;
		IggyResult out = IggyPlayerCallMethodRS ( getMovie() , &result, IggyPlayerRootPath( getMovie() ), m_funcShowScrollLeftArrow , 1 , value );

		m_showingLeftArrow = show;
	}
}

void UIScene_TradingMenu::moveSelector(bool right)
{
	IggyDataValue result;
	IggyDataValue value[1];

	value[0].type = IGGY_DATATYPE_boolean;
	value[0].boolval = right;
	IggyResult out = IggyPlayerCallMethodRS ( getMovie() , &result, IggyPlayerRootPath( getMovie() ), m_funcMoveSelector , 1 , value );
}

void UIScene_TradingMenu::setTitle(const wstring &name)
{
	m_labelTrading.setLabel(name);
}

void UIScene_TradingMenu::setRequest1Name(const wstring &name)
{
	m_labelRequest1.setLabel(name);
}

void UIScene_TradingMenu::setRequest2Name(const wstring &name)
{
	m_labelRequest2.setLabel(name);
}

void UIScene_TradingMenu::setRequest1RedBox(bool show)
{
	m_slotListRequest1.showSlotRedBox(0,show);
}

void UIScene_TradingMenu::setRequest2RedBox(bool show)
{
	m_slotListRequest2.showSlotRedBox(0,show);
}

void UIScene_TradingMenu::setTradeRedBox(int index, bool show)
{
	m_slotListTrades.showSlotRedBox(index,show);
}

void UIScene_TradingMenu::setOfferDescription(vector<HtmlString> *description)
{
	wstring descriptionStr = HtmlString::Compose(description);

	IggyDataValue result;
	IggyDataValue value[1];

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

	IggyResult out = IggyPlayerCallMethodRS ( getMovie() , &result, IggyPlayerRootPath( getMovie() ), m_funcSetOfferDescription , 1 , value );
}

void UIScene_TradingMenu::HandleMessage(EUIMessage message, void *data)
{
	switch(message)
	{
	case eUIMessage_InventoryUpdated:
		handleInventoryUpdated(data);
		break;
	};
}

void UIScene_TradingMenu::handleInventoryUpdated(LPVOID data)
{
	HandleInventoryUpdated();
}