the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2
3#include "..\..\..\Minecraft.World\net.minecraft.world.inventory.h"
4#include "..\..\..\Minecraft.World\net.minecraft.world.item.trading.h"
5#include "..\..\MultiPlayerLocalPlayer.h"
6#include "..\..\Common\Tutorial\Tutorial.h"
7#include "..\..\Common\Tutorial\TutorialMode.h"
8#include "..\..\Common\Tutorial\TutorialEnum.h"
9#include "..\..\Minecraft.h"
10#include "XUI_Ctrl_SlotList.h"
11#include "XUI_Scene_Trading.h"
12#include "..\..\..\Minecraft.World\StringHelpers.h"
13#include "..\..\..\Minecraft.World\JavaMath.h"
14
15//--------------------------------------------------------------------------------------
16// Name: CXuiSceneTrading::OnInit
17// Desc: Message handler for XM_INIT
18//--------------------------------------------------------------------------------------
19HRESULT CXuiSceneTrading::OnInit( XUIMessageInit* pInitData, BOOL& bHandled )
20{
21 MapChildControls();
22
23 //XuiControlSetText(m_villagerText,app.GetString(IDS_VILLAGER));
24 XuiControlSetText(m_inventoryLabel,app.GetString(IDS_INVENTORY));
25 XuiControlSetText(m_requiredLabel,app.GetString(IDS_REQUIRED_ITEMS_FOR_TRADE));
26
27
28 Minecraft *pMinecraft = Minecraft::GetInstance();
29
30 TradingScreenInput* initData = (TradingScreenInput *)pInitData->pvInitData;
31 m_iPad=initData->iPad;
32 m_bSplitscreen=initData->bSplitscreen;
33 m_merchant = initData->trader;
34
35 // if we are in splitscreen, then we need to figure out if we want to move this scene
36
37 if(m_bSplitscreen)
38 {
39 app.AdjustSplitscreenScene(m_hObj,&m_OriginalPosition,m_iPad);
40 }
41
42 if( pMinecraft->localgameModes[m_iPad] != NULL )
43 {
44 TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
45 m_previousTutorialState = gameMode->getTutorial()->getCurrentState();
46 gameMode->getTutorial()->changeTutorialState(e_Tutorial_State_Trading_Menu, this);
47 }
48
49 m_menu = new MerchantMenu( initData->inventory, initData->trader, initData->level );
50 Minecraft::GetInstance()->localplayers[m_iPad]->containerMenu = m_menu;
51
52 // TODO Inventory dimensions need defined as constants
53 m_inventoryControl->SetData( m_iPad, m_menu, 3, 9, MerchantMenu::INV_SLOT_START, MerchantMenu::INV_SLOT_END );
54
55 // TODO Inventory dimensions need defined as constants
56 m_useRowControl->SetData( m_iPad, m_menu, 1, 9, MerchantMenu::USE_ROW_SLOT_START, MerchantMenu::USE_ROW_SLOT_END );
57
58 delete initData;
59
60 D3DXVECTOR3 vec;
61 // store the slot 0 position
62 m_tradeHSlots[0]->GetPosition(&m_vTradeSlot0Pos);
63 m_tradeHSlots[1]->GetPosition(&vec);
64 m_fSlotSize=vec.x-m_vTradeSlot0Pos.x;
65
66 // store the slot 0 highlight position
67 m_tradingSelector.GetPosition(&m_vSelectorInitialPos);
68
69 //app.SetRichPresenceContextValue(m_iPad,CONTEXT_GAME_STATE_FORGING);
70
71 XuiSetTimer(m_hObj,TRADING_UPDATE_TIMER_ID,TRADING_UPDATE_TIMER_TIME);
72
73 ui.SetTooltips(m_iPad, -1, IDS_TOOLTIPS_EXIT);
74
75 return S_OK;
76}
77
78HRESULT CXuiSceneTrading::OnDestroy()
79{
80 Minecraft *pMinecraft = Minecraft::GetInstance();
81
82 if( pMinecraft->localgameModes[m_iPad] != NULL )
83 {
84 TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
85 if(gameMode != NULL) gameMode->getTutorial()->changeTutorialState(m_previousTutorialState);
86 }
87
88 // 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.
89 // 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)
90 if(Minecraft::GetInstance()->localplayers[m_iPad] != NULL) Minecraft::GetInstance()->localplayers[m_iPad]->closeContainer();
91 return S_OK;
92}
93
94HRESULT CXuiSceneTrading::OnTransitionStart( XUIMessageTransition *pTransition, BOOL& bHandled )
95{
96 if(pTransition->dwTransAction==XUI_TRANSITION_ACTION_DESTROY ) return S_OK;
97
98 if(pTransition->dwTransType == XUI_TRANSITION_TO || pTransition->dwTransType == XUI_TRANSITION_BACKTO)
99 {
100 HXUIOBJ hObj=NULL;
101 HRESULT hr=XuiControlGetVisual(m_offerInfoControl.m_hObj,&hObj);
102 hr=XuiElementGetChildById(hObj,L"text_measurer",&m_hOfferInfoTextMeasurer);
103 hr=XuiElementGetChildById(hObj,L"text_name",&m_hOfferInfoText);
104 hr=XuiElementGetChildById(hObj,L"text_panel",&m_hOfferInfoTextBkg);
105 }
106
107 return S_OK;
108}
109
110HRESULT CXuiSceneTrading::OnKeyDown(XUIMessageInput *pInputData, BOOL& bHandled)
111{
112 bHandled = handleKeyDown(pInputData->UserIndex, mapVKToAction(pInputData->dwKeyCode), (pInputData->dwFlags & XUI_INPUT_FLAG_REPEAT) != 0);
113
114 return S_OK;
115}
116
117HRESULT CXuiSceneTrading::OnCustomMessage_Splitscreenplayer(bool bJoining, BOOL& bHandled)
118{
119 bHandled=true;
120 return app.AdjustSplitscreenScene_PlayerChanged(m_hObj,&m_OriginalPosition,m_iPad,bJoining);
121}
122
123HRESULT CXuiSceneTrading::OnTimer( XUIMessageTimer *pTimer, BOOL& bHandled )
124{
125 if(pTimer->nId == TRADING_UPDATE_TIMER_ID)
126 {
127 handleTick();
128 bHandled = TRUE;
129 }
130 return S_OK;
131}
132
133int CXuiSceneTrading::mapVKToAction(int vk)
134{
135 int action = MINECRAFT_ACTION_MAX;
136 switch(vk)
137 {
138 case VK_PAD_A:
139 action = ACTION_MENU_A;
140 break;
141 case VK_PAD_B:
142 case VK_PAD_START:
143 action = ACTION_MENU_B;
144 break;
145 case VK_PAD_X:
146 action = ACTION_MENU_X;
147 break;
148 case VK_PAD_Y:
149 action = ACTION_MENU_Y;
150 break;
151 case VK_PAD_DPAD_LEFT:
152 case VK_PAD_LTHUMB_LEFT:
153 action = ACTION_MENU_LEFT;
154 break;
155 case VK_PAD_DPAD_RIGHT:
156 case VK_PAD_LTHUMB_RIGHT:
157 action = ACTION_MENU_RIGHT;
158 break;
159 case VK_PAD_LTHUMB_UP:
160 case VK_PAD_DPAD_UP:
161 action = ACTION_MENU_UP;
162 break;
163 case VK_PAD_LTHUMB_DOWN:
164 case VK_PAD_DPAD_DOWN:
165 action = ACTION_MENU_DOWN;
166 break;
167 case VK_PAD_LTRIGGER:
168 action = ACTION_MENU_PAGEUP;
169 break;
170 case VK_PAD_RTRIGGER:
171 action = ACTION_MENU_PAGEDOWN;
172 break;
173 case VK_PAD_LSHOULDER:
174 action = ACTION_MENU_LEFT_SCROLL;
175 break;
176 case VK_PAD_RSHOULDER:
177 action = ACTION_MENU_RIGHT_SCROLL;
178 break;
179 case VK_PAD_RTHUMB_UP:
180 action = ACTION_MENU_OTHER_STICK_UP;
181 break;
182 case VK_PAD_RTHUMB_DOWN:
183 action = ACTION_MENU_OTHER_STICK_DOWN;
184 break;
185 case VK_PAD_RTHUMB_RIGHT:
186 action = ACTION_MENU_OTHER_STICK_RIGHT;
187 break;
188 case VK_PAD_RTHUMB_LEFT:
189 action = ACTION_MENU_OTHER_STICK_LEFT;
190 break;
191 };
192
193 return action;
194}
195
196void CXuiSceneTrading::showScrollRightArrow(bool show)
197{
198 m_scrollRight.SetShow(show?TRUE:FALSE);
199 m_scrollRight.PlayOptionalVisual(L"ScrollMore",L"EndScrollMore");
200}
201
202void CXuiSceneTrading::showScrollLeftArrow(bool show)
203{
204 m_scrollLeft.SetShow(show?TRUE:FALSE);
205 m_scrollLeft.PlayOptionalVisual(L"ScrollMore",L"EndScrollMore");
206}
207
208void CXuiSceneTrading::moveSelector(bool right)
209{
210 D3DXVECTOR3 vec;
211
212 vec.z=0.0f;
213 vec.x=m_vSelectorInitialPos.x + (m_selectedSlot*m_fSlotSize);
214 vec.y=m_vSelectorInitialPos.y;
215 m_tradingSelector.SetPosition(&vec);
216}
217
218void CXuiSceneTrading::setTitle(const wstring &name)
219{
220 XuiControlSetText(m_villagerText,name.c_str());
221}
222
223void CXuiSceneTrading::setRequest1Name(const wstring &name)
224{
225 m_request1Label.SetText(name.c_str());
226}
227
228void CXuiSceneTrading::setRequest2Name(const wstring &name)
229{
230 m_request2Label.SetText(name.c_str());
231}
232
233void CXuiSceneTrading::setRequest1RedBox(bool show)
234{
235 m_request1Control->SetRedBox(show?TRUE:FALSE);
236}
237
238void CXuiSceneTrading::setRequest2RedBox(bool show)
239{
240 m_request2Control->SetRedBox(show?TRUE:FALSE);
241}
242
243void CXuiSceneTrading::setTradeRedBox(int index, bool show)
244{
245 m_tradeHSlots[index]->SetRedBox(show?TRUE:FALSE);
246}
247
248void CXuiSceneTrading::setRequest1Item(shared_ptr<ItemInstance> item)
249{
250 m_request1Control->SetIcon(getPad(), item, 12, 31, true);
251}
252
253void CXuiSceneTrading::setRequest2Item(shared_ptr<ItemInstance> item)
254{
255 m_request2Control->SetIcon(getPad(), item, 12, 31, true);
256}
257
258void CXuiSceneTrading::setTradeItem(int index, shared_ptr<ItemInstance> item)
259{
260 m_tradeHSlots[index]->SetIcon(getPad(), item, 12, 31, true);
261}
262
263void CXuiSceneTrading::setOfferDescription(const wstring &name, vector<wstring> &unformattedStrings)
264{
265 if(name.empty())
266 {
267 m_offerInfoControl.SetText(L"");
268 m_offerInfoControl.SetShow(FALSE);
269 return;
270 }
271
272 bool smallPointer = m_bSplitscreen || (!RenderManager.IsHiDef() && !RenderManager.IsWidescreen());
273 wstring desc = L"<font size=\"" + _toString<int>(smallPointer ? 12 :14) + L"\">" + name + L"</font>";
274
275 XUIRect tempXuiRect, xuiRect;
276 HRESULT hr;
277 xuiRect.right = 0;
278
279 for(AUTO_VAR(it, unformattedStrings.begin()); it != unformattedStrings.end(); ++it)
280 {
281 XuiTextPresenterMeasureText(m_hOfferInfoTextMeasurer, (*it).c_str(), &tempXuiRect);
282 if(tempXuiRect.right > xuiRect.right) xuiRect = tempXuiRect;
283 }
284
285 // Set size with the new width so that the HTML height check is correct
286 XuiElementSetBounds(m_hOfferInfoTextBkg,xuiRect.right+4.0f+4.0f+4.0f,xuiRect.bottom); // edge graphics are 8 pixels, with 4 for the border, extra 4 for the background is fudge
287 XuiElementSetBounds(m_hOfferInfoText,xuiRect.right+4.0f+4.0f,xuiRect.bottom); // edge graphics are 8 pixels, text is centred
288
289 XuiHtmlSetText(m_hOfferInfoText, desc.c_str() );
290
291 // Check if we need to resize the box
292 XUIContentDims contentDims;
293 XuiHtmlGetContentDims(m_hOfferInfoText,&contentDims);
294 xuiRect.bottom = contentDims.nContentHeight;
295
296 // Set the new height
297 float backgroundWidth = xuiRect.right+4.0f+4.0f+4.0f;
298 XuiElementSetBounds(m_hOfferInfoTextBkg,backgroundWidth,xuiRect.bottom+4.0f+4.0f); // edge graphics are 8 pixels, with 4 for the border, extra 4 for the background is fudge
299 XuiElementSetBounds(m_hOfferInfoText,xuiRect.right+4.0f+4.0f,xuiRect.bottom+4.0f+4.0f); // edge graphics are 8 pixels, text is centred
300
301 m_offerInfoControl.SetShow(TRUE);
302
303 D3DXVECTOR3 highlightPos, offerInfoPos;
304 float highlightWidth, highlightHeight;
305 m_tradingSelector.GetPosition(&highlightPos);
306 m_tradingSelector.GetBounds(&highlightWidth,&highlightHeight);
307 m_offerInfoControl.GetPosition(&offerInfoPos);
308
309 if(m_selectedSlot < DISPLAY_TRADES_COUNT/2)
310 {
311 // Display on the right
312 offerInfoPos.x = Math::round(highlightPos.x + highlightWidth * 1.1);
313 }
314 else
315 {
316 // Display on the left
317 offerInfoPos.x = Math::round(highlightPos.x - backgroundWidth - highlightWidth * 0.1);
318 }
319 m_offerInfoControl.SetPosition(&offerInfoPos);
320}