the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "XUI_Ctrl_4JList.h"
3
4static bool TimeSortFn(const void *a, const void *b);
5
6HRESULT CXuiCtrl4JList::OnInit(XUIMessageInit *pInitData, BOOL& bHandled)
7{
8 InitializeCriticalSection(&m_AccessListData);
9
10 m_hSelectionChangedHandlerObj = NULL;
11
12 return S_OK;
13}
14
15void CXuiCtrl4JList::AddData( const LIST_ITEM_INFO& ItemInfo , int iSortListFromIndex, int iSortFunction)
16{
17 // need to allocate memory for the structure and its strings
18 // and remap the string pointers
19 DWORD dwBytes=0;
20 DWORD dwLen1=0;
21 DWORD dwLen2=0;
22
23 if(ItemInfo.pwszText)
24 {
25 dwLen1=(int)wcslen(ItemInfo.pwszText)*sizeof(WCHAR);
26 dwBytes+=dwLen1+sizeof(WCHAR);
27 }
28
29 if(ItemInfo.pwszImage)
30 {
31 dwLen2=(int)(wcslen(ItemInfo.pwszImage))*sizeof(WCHAR);
32 dwBytes+=dwLen2+sizeof(WCHAR);
33 }
34
35 dwBytes+=sizeof( LIST_ITEM_INFO );
36 LIST_ITEM_INFO *pItemInfo = (LIST_ITEM_INFO *)new BYTE[dwBytes];
37 ZeroMemory(pItemInfo,dwBytes);
38
39 XMemCpy( pItemInfo, &ItemInfo, sizeof( LIST_ITEM_INFO ) );
40 if(dwLen1!=0)
41 {
42 XMemCpy( &pItemInfo[1], ItemInfo.pwszText, dwLen1 );
43 pItemInfo->pwszText=(LPCWSTR)&pItemInfo[1];
44 if(dwLen2!=0)
45 {
46 BYTE *pwszImage = ((BYTE *)&pItemInfo[1])+dwLen1+sizeof(WCHAR);
47 XMemCpy( pwszImage, ItemInfo.pwszImage, dwLen2 );
48 pItemInfo->pwszImage=(LPCWSTR)pwszImage;
49 }
50 }
51 else if(dwLen2!=0)
52 {
53 XMemCpy( &pItemInfo[1], ItemInfo.pwszImage, dwLen2 );
54 pItemInfo->pwszImage=(LPCWSTR)&pItemInfo[1];
55 }
56
57 EnterCriticalSection(&m_AccessListData);
58
59 // need to remember the original index of this addition before it gets sorted - this will get used to load the game
60 if(iSortListFromIndex!=-1)
61 {
62 pItemInfo->iIndex=(int)m_vListData.size()-iSortListFromIndex;
63 }
64 else
65 {
66 pItemInfo->iIndex=(int)m_vListData.size();
67 }
68
69 // added to force a sort order for DLC
70 //pItemInfo->iSortIndex=iSortIndex;
71
72 m_vListData.push_back(pItemInfo);
73
74#ifdef _DEBUG
75
76 int iCount=0;
77 for (AUTO_VAR(it, m_vListData.begin()); it != m_vListData.end(); it++)
78 {
79 PLIST_ITEM_INFO pInfo=(PLIST_ITEM_INFO)*it;
80 app.DebugPrintf("%d. ",iCount++);
81 OutputDebugStringW(pInfo->pwszText);
82 app.DebugPrintf(" - %d\n",pInfo->iSortIndex);
83
84 }
85#endif
86
87
88 if(iSortListFromIndex!=-1)
89 {
90 switch(iSortFunction)
91 {
92 case eSortList_Date:
93 // sort from the index passed (to leave create world and tutorial in the saves list)
94 sort(m_vListData.begin()+iSortListFromIndex, m_vListData.end(),CXuiCtrl4JList::TimeSortFn);
95 break;
96 case eSortList_Alphabetical:
97 // alphabetical sort
98 sort(m_vListData.begin()+iSortListFromIndex, m_vListData.end(),CXuiCtrl4JList::AlphabeticSortFn);
99 break;
100 case eSortList_Index:
101 sort(m_vListData.begin()+iSortListFromIndex, m_vListData.end(),CXuiCtrl4JList::IndexSortFn);
102 break;
103 }
104 }
105 LeaveCriticalSection(&m_AccessListData);
106// #ifdef _DEBUG
107//
108// iCount=0;
109// for (AUTO_VAR(it, m_vListData.begin()); it != m_vListData.end(); it++)
110// {
111// PLIST_ITEM_INFO pInfo=(PLIST_ITEM_INFO)*it;
112// app.DebugPrintf("After Sort - %d. ",iCount++);
113// OutputDebugStringW(pInfo->pwszText);
114// app.DebugPrintf(" - %d\n",pInfo->iSortIndex);
115//
116// }
117// #endif
118 InsertItems( 0, 1 );
119}
120
121void CXuiCtrl4JList::RemoveAllData( )
122{
123 EnterCriticalSection(&m_AccessListData);
124
125 int iSize=(int)m_vListData.size();
126 for(int i=0;i<iSize;i++)
127 {
128 LIST_ITEM_INFO *pBack = m_vListData.back();
129 if( pBack->hXuiBrush )
130 {
131 XuiDestroyBrush(pBack->hXuiBrush);
132 }
133 m_vListData.pop_back();
134 DeleteItems( 0, 1 );
135 }
136
137 LeaveCriticalSection(&m_AccessListData);
138}
139
140void CXuiCtrl4JList::SelectByUserData(int iData)
141{
142 for(unsigned int i = 0; i < m_vListData.size(); ++i)
143 {
144 if(m_vListData.at(i)->iData == iData)
145 {
146 SetCurSel(i);
147 SetTopItem(i); // scroll the item into view if it's not visible
148 break;
149 }
150 }
151}
152
153int CXuiCtrl4JList::GetIndexByUserData(int iData)
154{
155 for(unsigned int i = 0; i < m_vListData.size(); ++i)
156 {
157 if(m_vListData.at(i)->iData == iData)
158 {
159 return i;
160 }
161 }
162 return 0;
163}
164
165CXuiCtrl4JList::LIST_ITEM_INFO& CXuiCtrl4JList::GetData(DWORD dw)
166{
167 return *m_vListData[dw];
168}
169
170CXuiCtrl4JList::LIST_ITEM_INFO& CXuiCtrl4JList::GetDataiData(int iData)
171{
172 LIST_ITEM_INFO info;
173
174 for(unsigned int i=0;i<m_vListData.size();i++)
175 {
176 info=*m_vListData[i];
177 if(info.iData==iData)
178 {
179 return *m_vListData[i];
180 }
181 }
182
183 return *m_vListData[0];
184}
185
186CXuiCtrl4JList::LIST_ITEM_INFO& CXuiCtrl4JList::GetData(FILETIME *pFileTime)
187{
188 LIST_ITEM_INFO info;
189
190 for(unsigned int i=0;i<m_vListData.size();i++)
191 {
192 info=*m_vListData[i];
193 if((info.fTime.dwHighDateTime==pFileTime->dwHighDateTime)&&(info.fTime.dwLowDateTime==pFileTime->dwLowDateTime))
194 {
195 return *m_vListData[i];
196 }
197 }
198
199 return *m_vListData[0];
200}
201
202bool CXuiCtrl4JList::TimeSortFn(const void *a, const void *b)
203{
204 CXuiCtrl4JList::LIST_ITEM_INFO *SaveDetailsA=(CXuiCtrl4JList::LIST_ITEM_INFO *)a;
205 CXuiCtrl4JList::LIST_ITEM_INFO *SaveDetailsB=(CXuiCtrl4JList::LIST_ITEM_INFO *)b;
206
207 if(SaveDetailsA->fTime.dwHighDateTime > SaveDetailsB->fTime.dwHighDateTime)
208 {
209 return true;
210 }
211 else if(SaveDetailsA->fTime.dwHighDateTime == SaveDetailsB->fTime.dwHighDateTime)
212 {
213 if(SaveDetailsA->fTime.dwLowDateTime > SaveDetailsB->fTime.dwLowDateTime)
214 {
215 return true;
216 }
217 else
218 {
219 return false;
220 }
221 }
222 else
223 {
224 return false;
225 }
226
227}
228
229bool CXuiCtrl4JList::AlphabeticSortFn(const void *a, const void *b)
230{
231 CXuiCtrl4JList::LIST_ITEM_INFO *SaveDetailsA=(CXuiCtrl4JList::LIST_ITEM_INFO *)a;
232 CXuiCtrl4JList::LIST_ITEM_INFO *SaveDetailsB=(CXuiCtrl4JList::LIST_ITEM_INFO *)b;
233
234 wstring wstr1=SaveDetailsA->pwszText;
235 wstring wstr2=SaveDetailsB->pwszText;
236 if(wstr1.compare(wstr2)<0)
237 {
238 return true;
239 }
240
241 return false;
242}
243
244bool CXuiCtrl4JList::IndexSortFn(const void *a, const void *b)
245{
246 CXuiCtrl4JList::LIST_ITEM_INFO *SaveDetailsA=(CXuiCtrl4JList::LIST_ITEM_INFO *)a;
247 CXuiCtrl4JList::LIST_ITEM_INFO *SaveDetailsB=(CXuiCtrl4JList::LIST_ITEM_INFO *)b;
248
249 int iA=SaveDetailsA->iSortIndex;
250 int iB=SaveDetailsB->iSortIndex;
251 if(iA>iB)
252 {
253 return true;
254 }
255
256 return false;
257}
258
259void CXuiCtrl4JList::UpdateGraphic(int iItem,HXUIBRUSH hXuiBrush )
260{
261 // need to update the one with the matching filetime
262 EnterCriticalSection(&m_AccessListData);
263 if( GetData(iItem).hXuiBrush )
264 {
265 XuiDestroyBrush( GetData(iItem).hXuiBrush );
266 }
267 GetData(iItem).hXuiBrush=hXuiBrush;
268 LeaveCriticalSection(&m_AccessListData);
269}
270
271void CXuiCtrl4JList::UpdateText(int iItem,LPCWSTR pwszText )
272{
273 // need to update the one with the matching filetime
274 EnterCriticalSection(&m_AccessListData);
275 GetData(iItem).pwszText=pwszText;
276 LeaveCriticalSection(&m_AccessListData);
277}
278
279void CXuiCtrl4JList::UpdateGraphicFromiData(int iData,HXUIBRUSH hXuiBrush )
280{
281 // need to update the one with the matching iData
282 EnterCriticalSection(&m_AccessListData);
283 if( GetDataiData(iData).hXuiBrush )
284 {
285 XuiDestroyBrush( GetDataiData(iData).hXuiBrush );
286 }
287 GetDataiData(iData).hXuiBrush=hXuiBrush;
288 LeaveCriticalSection(&m_AccessListData);
289}
290
291void CXuiCtrl4JList::UpdateGraphic(FILETIME *pfTime,HXUIBRUSH hXuiBrush )
292{
293 // need to update the one with the matching filetime
294 EnterCriticalSection(&m_AccessListData);
295 if( GetData(pfTime).hXuiBrush )
296 {
297 XuiDestroyBrush( GetData(pfTime).hXuiBrush );
298 }
299 GetData(pfTime).hXuiBrush=hXuiBrush;
300 LeaveCriticalSection(&m_AccessListData);
301}
302
303// Gets called every frame
304HRESULT CXuiCtrl4JList::OnGetSourceDataText(XUIMessageGetSourceText *pGetSourceTextData,BOOL& bHandled)
305{
306 if( ( 0 == pGetSourceTextData->iData ) && ( ( pGetSourceTextData->bItemData ) ) )
307 {
308 EnterCriticalSection(&m_AccessListData);
309 pGetSourceTextData->szText =
310 GetData(pGetSourceTextData->iItem).pwszText;
311 LeaveCriticalSection(&m_AccessListData);
312 bHandled = TRUE;
313 }
314 return S_OK;
315}
316
317HRESULT CXuiCtrl4JList::OnGetItemCountAll(XUIMessageGetItemCount *pGetItemCountData,BOOL& bHandled)
318{
319 pGetItemCountData->cItems = (int)m_vListData.size();
320 bHandled = TRUE;
321 return S_OK;
322}
323
324HRESULT CXuiCtrl4JList::OnGetSourceDataImage(XUIMessageGetSourceImage *pGetSourceImageData,BOOL& bHandled)
325{
326 if( ( 0 == pGetSourceImageData->iData ) && ( pGetSourceImageData->bItemData ) )
327 {
328 // Check for a brush
329 EnterCriticalSection(&m_AccessListData);
330 if(GetData(pGetSourceImageData->iItem).hXuiBrush!=NULL)
331 {
332 pGetSourceImageData->hBrush=GetData(pGetSourceImageData->iItem).hXuiBrush;
333 }
334 else
335 {
336 pGetSourceImageData->szPath =
337 GetData(pGetSourceImageData->iItem).pwszImage;
338 }
339 LeaveCriticalSection(&m_AccessListData);
340 bHandled = TRUE;
341 }
342 return S_OK;
343}
344
345HRESULT CXuiCtrl4JList::OnGetItemEnable(XUIMessageGetItemEnable *pGetItemEnableData,BOOL& bHandled)
346{
347 if(m_vListData.size()!=0)
348 {
349 EnterCriticalSection(&m_AccessListData);
350 pGetItemEnableData->bEnabled =
351 GetData(pGetItemEnableData->iItem).fEnabled;
352 LeaveCriticalSection(&m_AccessListData);
353 }
354 bHandled = TRUE;
355 return S_OK;
356}
357
358
359HRESULT CXuiCtrl4JList::SetBorder(DWORD dw,BOOL bShow)
360{
361 CXuiControl Control;
362 HXUIOBJ hVisual,hBorder;
363 GetItemControl(dw,&Control);
364 Control.GetVisual(&hVisual);
365 XuiElementGetChildById(hVisual,L"Border",&hBorder);
366 return XuiElementSetShow(hBorder,bShow);
367}
368
369void CXuiCtrl4JList::SetSelectionChangedHandle(HXUIOBJ hObj)
370{
371 m_hSelectionChangedHandlerObj = hObj;
372}
373
374HRESULT CXuiCtrl4JList::OnDestroy()
375{
376 DeleteCriticalSection(&m_AccessListData);
377
378 if(m_vListData.size()!=0)
379 {
380 for (unsigned i = 0; i < m_vListData.size(); ++i)
381 {
382 if( m_vListData[i]->hXuiBrush )
383 {
384 XuiDestroyBrush( m_vListData[i]->hXuiBrush );
385 }
386 delete [] (BYTE *)m_vListData[i];
387 }
388 }
389 return S_OK;
390}
391
392HRESULT CXuiCtrl4JList::OnNotifySelChanged( HXUIOBJ hObjSource, XUINotifySelChanged* pNotifySelChangedData, BOOL& bHandled )
393{
394 if(m_hSelectionChangedHandlerObj)
395 {
396 XUIMessage xuiMsg;
397 XUINotify xuiNotify;
398 XUINotifySelChanged xuiNotifySel;
399 XuiNotifySelChanged( &xuiMsg, &xuiNotify, &xuiNotifySel, hObjSource, pNotifySelChangedData->iItem, pNotifySelChangedData->iOldItem );
400 XuiSendMessage( m_hSelectionChangedHandlerObj, &xuiMsg );
401
402 bHandled = xuiMsg.bHandled;
403 }
404 return S_OK;
405}