comparison GUI/UI/Spellbook.cpp @ 2549:3121fb54a110

Rest & Spelbook refactored into class
author a.parshin
date Tue, 12 May 2015 11:07:32 +0200
parents
children d87bfbd3bb3b
comparison
equal deleted inserted replaced
2548:87e5590d034b 2549:3121fb54a110
1 #include "Engine/Engine.h"
2 #include "Engine/Party.h"
3 #include "Engine/LOD.h"
4 #include "Engine/Timer.h"
5 #include "Engine/texts.h"
6 #include "Engine/Graphics/Render.h"
7 #include "Engine/Graphics/Viewport.h"
8
9 #include "IO/Mouse.h"
10
11 #include "GUI/UI/Spellbook.h"
12 #include "GUI/GUIFont.h"
13
14 #include "Media/Audio/AudioPlayer.h"
15
16
17
18 void InitializeSpellBookTextures();
19 void OnCloseSpellBookPage();
20 void OnCloseSpellBook();
21 void LoadSpellbook(unsigned int spell_school);
22 void BookUI_Spellbook_DrawCurrentSchoolBackground();
23
24
25 std::array<char *, 9> spellbook_texture_filename_suffices = {{"f", "a", "w", "e", "s", "m", "b", "l", "d"}};
26
27 std::array<std::array<unsigned char, 12>, 9> pSpellbookSpellIndices = // 4E2430 from pSpellbookSpellIndices[9][12]
28 {
29 { //0 1 2 3 4 5 6 7 8 9 10 11
30 { 0, 3, 1, 8, 11, 7, 4, 10, 6, 2, 5, 9 },
31 { 0, 11, 2, 9, 6, 8, 5, 10, 3, 7, 1, 4 },
32 { 0, 4, 8, 9, 1, 10, 3, 11, 7, 6, 2, 5 },
33 { 0, 7, 10, 8, 2, 11, 1, 5, 3, 6, 4, 9 },
34 { 0, 5, 10, 11, 7, 2, 8, 1, 4, 9, 3, 6 },
35 { 0, 5, 9, 8, 3, 7, 6, 4, 1, 11, 2, 10 },
36 { 0, 1, 6, 9, 3, 5, 8, 11, 7, 10, 4, 2 },
37 { 0, 1, 10, 11, 9, 4, 3, 6, 5, 7, 8, 2 },
38 { 0, 9, 3, 7, 1, 5, 2, 10, 11, 8, 6, 4 }
39 }
40 };
41
42
43
44
45 GUIWindow_Spellbook::GUIWindow_Spellbook() :
46 GUIWindow(0, 0, window->GetWidth(), window->GetHeight(), 0, nullptr)
47 {
48 // ------------------------------------
49 // 004304E7 void Game_EventLoop -- part
50 current_screen_type = SCREEN_SPELL_BOOK;
51 pEventTimer->Pause();
52
53 InitializeSpellBookTextures();
54 OpenSpellbook();
55
56 // ------------------------------------
57 // 004304E7 void Game_EventLoop -- part
58 pAudioPlayer->PlaySound(SOUND_48, 0, 0, -1, 0, 0, 0, 0);
59 viewparams->field_48 = 1;
60 }
61
62
63 void GUIWindow_Spellbook::OpenSpellbookPage(int page)
64 {
65 // ------------------------------------
66 // 004304E7 void Game_EventLoop -- part
67
68 OnCloseSpellBookPage();
69 pPlayers[uActiveCharacter]->lastOpenedSpellbookPage = page;
70 OpenSpellbook();
71 pAudioPlayer->PlaySound((SoundID)(SOUND_TurnPageU + rand() % 2), 0, 0, -1, 0, 0, 0, 0);
72 }
73
74 //----- (00411621) --------------------------------------------------------
75 void GUIWindow_Spellbook::OpenSpellbook()
76 {
77 Player *pPlayer; // edi@1
78 //GUIWindow *pWindow; // esi@1
79 //unsigned int v3; // ebp@1
80 int v4; // eax@3
81 ///GUIButton *result; // eax@25
82 int a2; // [sp+10h] [bp-8h]@1
83 //int v7; // [sp+14h] [bp-4h]@1
84
85 pPlayer = pPlayers[uActiveCharacter];
86 //pWindow = this;
87 LoadSpellbook(pPlayer->lastOpenedSpellbookPage);
88 //v3 = 0;
89 a2 = 0;
90
91 PlayerSpellbookChapter* chapter = &pPlayer->spellbook.pChapters[pPlayer->lastOpenedSpellbookPage];
92 for (uint i = 0; i < 11; ++i)
93 {
94 if (!chapter->bIsSpellAvailable[i])
95 continue;
96 v4 = pPlayer->lastOpenedSpellbookPage;
97 //v4 = (12 * pPlayer->lastOpenedSpellbookPage + pSpellbookSpellIndices[pPlayer->lastOpenedSpellbookPage][i + 1]);
98 CreateButton(pViewport->uViewportTL_X + pIconPos[v4][pSpellbookSpellIndices[v4][i + 1]].Xpos,
99 pViewport->uViewportTL_Y + pIconPos[v4][pSpellbookSpellIndices[v4][i + 1]].Ypos, //dword_4E20D0
100 SBPageSSpellsTextureList[i + 1]->uTextureWidth,
101 SBPageSSpellsTextureList[i + 1]->uTextureHeight,
102 1, 79, UIMSG_SelectSpell, i, 0, "", 0);
103 ++a2;
104 //++v3;
105 }
106 //while ( (signed int)v3 < 11 );
107
108 CreateButton(0, 0, 0, 0, 1, 0, UIMSG_SpellBook_PressTab, 0, '\t', "", 0);
109 if (a2)
110 _41D08F_set_keyboard_control_group(a2, 0, 0, 0);
111
112 if (pPlayer->pActiveSkills[PLAYER_SKILL_FIRE]) CreateButton(399, 10, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 0, 0, aSpellSchoolNames[0], 0);
113 if (pPlayer->pActiveSkills[PLAYER_SKILL_AIR]) CreateButton(399, 46, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 1, 0, aSpellSchoolNames[1], 0);
114 if (pPlayer->pActiveSkills[PLAYER_SKILL_WATER]) CreateButton(399, 83, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 2, 0, aSpellSchoolNames[2], 0);
115 if (pPlayer->pActiveSkills[PLAYER_SKILL_EARTH]) CreateButton(399, 121, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 3, 0, aSpellSchoolNames[3], 0);
116 if (pPlayer->pActiveSkills[PLAYER_SKILL_SPIRIT]) CreateButton(399, 158, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 4, 0, aSpellSchoolNames[4], 0);
117 if (pPlayer->pActiveSkills[PLAYER_SKILL_MIND]) CreateButton(400, 196, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 5, 0, aSpellSchoolNames[5], 0);
118 if (pPlayer->pActiveSkills[PLAYER_SKILL_BODY]) CreateButton(400, 234, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 6, 0, aSpellSchoolNames[6], 0);
119 if (pPlayer->pActiveSkills[PLAYER_SKILL_LIGHT]) CreateButton(400, 271, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 7, 0, aSpellSchoolNames[7], 0);
120 if (pPlayer->pActiveSkills[PLAYER_SKILL_DARK]) CreateButton(400, 307, 50, 36, 1, 0, UIMSG_OpenSpellbookPage, 8, 0, aSpellSchoolNames[8], 0);
121
122 CreateButton(476, 450, pSBClickQuickSpellBtnTextr->uTextureWidth, pSBClickQuickSpellBtnTextr->uTextureHeight, 1, 78, UIMSG_ClickInstallRemoveQuickSpellBtn, 0, 0, "", 0);
123 pBtn_InstallRemoveSpell = CreateButton(476, 450, 48, 32, 1, 78, UIMSG_ClickInstallRemoveQuickSpellBtn, 0, 0, "", pSBClickQuickSpellBtnTextr, 0);
124 CreateButton(561, 450, pSpellBookClickCloseBtnTextr->uTextureWidth, pSpellBookClickCloseBtnTextr->uTextureHeight, 1, 0, UIMSG_Escape, 0, 0, pGlobalTXT_LocalizationStrings[79], 0);
125 pBtn_CloseBook = CreateButton(561, 450, 48, 32, 1, 0, UIMSG_Escape, 0, 0, pGlobalTXT_LocalizationStrings[79], pSpellBookClickCloseBtnTextr, 0);
126 }
127
128 void GUIWindow_Spellbook::Update()
129 {
130 // -----------------------------------
131 // 004156F0 GUI_UpdateWindows --- part
132 // {
133 // DrawSpellBookContent(pPlayers[uActiveCharacter]);
134 // }
135 //
136 // --------------------------------------------------
137 // 00412B58 void DrawSpellBookContent(Player *player)
138 auto player = pPlayers[uActiveCharacter];
139
140 Texture *PendingTexture; // edi@1
141 Texture *pTexture; // edx@5
142 int v10; // eax@13
143 unsigned int pX_coord; // esi@18
144 unsigned int pY_coord; // edi@18
145 Texture *pPageTexture; // eax@21
146 // signed int v22; // [sp-4h] [bp-24h]@22
147 POINT a2; // [sp+18h] [bp-8h]@13
148
149 static unsigned int texture_tab_coord1[9][2] =
150 { { 406, 9 }, { 406, 46 }, { 406, 84 }, { 406, 121 }, { 407, 158 }, { 405, 196 }, { 405, 234 }, { 405, 272 }, { 405, 309 } };
151
152 static unsigned int texture_tab_coord0[9][2] =
153 { { 415, 10 }, { 415, 46 }, { 415, 83 }, { 415, 121 }, { 415, 158 }, { 416, 196 }, { 416, 234 }, { 416, 271 }, { 416, 307 } };
154
155 BookUI_Spellbook_DrawCurrentSchoolBackground();
156
157 PendingTexture = pIcons_LOD->GetTexture(pIcons_LOD->FindTextureByName("Pending"));
158 pRenderer->ClearZBuffer(0, 479);
159 if ((11 * player->lastOpenedSpellbookPage) || ((11 * player->lastOpenedSpellbookPage) + 11))//??? maybe structure need fix
160 {
161 for (uint i = 1; i <= 11; ++i)
162 {
163 if (player->_achieved_awards_bits[(11 * player->lastOpenedSpellbookPage) + i + 63])
164 {
165 if (SBPageSSpellsTextureList[i] != PendingTexture)
166 {
167 if (quick_spell_at_page == i)
168 pTexture = SBPageCSpellsTextureList[i];
169 else
170 pTexture = SBPageSSpellsTextureList[i];
171 if (pTexture->pLevelOfDetail0_prolly_alpha_mask)
172 {
173 pX_coord = pViewport->uViewportTL_X + pIconPos[player->lastOpenedSpellbookPage][pSpellbookSpellIndices[player->lastOpenedSpellbookPage][i]].Xpos;
174 pY_coord = pViewport->uViewportTL_Y + pIconPos[player->lastOpenedSpellbookPage][pSpellbookSpellIndices[player->lastOpenedSpellbookPage][i]].Ypos;
175 if (pTexture->pBits & 0x200)
176 pRenderer->DrawTextureIndexedAlpha(pX_coord, pY_coord, pTexture);
177 else
178 pRenderer->DrawTextureIndexed(pX_coord, pY_coord, pTexture);
179 pRenderer->DrawMaskToZBuffer(pIconPos[player->lastOpenedSpellbookPage][pSpellbookSpellIndices[player->lastOpenedSpellbookPage][i]].Xpos,
180 pIconPos[player->lastOpenedSpellbookPage][pSpellbookSpellIndices[player->lastOpenedSpellbookPage][i]].Ypos, pTexture, i);
181 }
182 }
183 }
184 }
185 }
186
187 pMouse->GetCursorPos(&a2);
188 v10 = pRenderer->pActiveZBuffer[a2.x + pSRZBufferLineOffsets[a2.y]] & 0xFFFF;
189 if (v10)
190 {
191 if (SBPageCSpellsTextureList[v10]->pLevelOfDetail0_prolly_alpha_mask)
192 {
193 pX_coord = pViewport->uViewportTL_X + pIconPos[player->lastOpenedSpellbookPage][pSpellbookSpellIndices[player->lastOpenedSpellbookPage][v10]].Xpos;
194 pY_coord = pViewport->uViewportTL_Y + pIconPos[player->lastOpenedSpellbookPage][pSpellbookSpellIndices[player->lastOpenedSpellbookPage][v10]].Ypos;
195 if (SBPageCSpellsTextureList[v10]->pBits & 0x200)
196 pRenderer->DrawTextureIndexedAlpha(pX_coord, pY_coord, SBPageCSpellsTextureList[v10]);
197 else
198 pRenderer->DrawTextureIndexed(pX_coord, pY_coord, SBPageCSpellsTextureList[v10]);
199 }
200 }
201 //pX_coord = (unsigned int)&player->pActiveSkills[PLAYER_SKILL_FIRE];
202 //pY_coord = (unsigned int)&player->pActiveSkills[PLAYER_SKILL_FIRE];
203 for (uint i = 0; i < 9; i++)
204 {
205 if (player->pActiveSkills[PLAYER_SKILL_FIRE + i])
206 {
207 if (player->lastOpenedSpellbookPage == i)
208 {
209 pPageTexture = pTextures_tabs[i][1];
210 pX_coord = texture_tab_coord1[i][0];
211 pY_coord = texture_tab_coord1[i][1];
212 }
213 else
214 {
215 pPageTexture = pTextures_tabs[i][0];
216 pX_coord = texture_tab_coord0[i][0];
217 pY_coord = texture_tab_coord0[i][1];
218 }
219 pRenderer->DrawTextureIndexedAlpha(pX_coord, pY_coord, pPageTexture);
220 }
221 }
222 }
223
224
225 void GUIWindow_Spellbook::Release()
226 {
227 // -----------------------------------------
228 // 0041C26A void GUIWindow::Release --- part
229 OnCloseSpellBookPage();
230 OnCloseSpellBook();
231
232 GUIWindow::Release();
233 }
234
235
236 //----- (00411300) --------------------------------------------------------
237 void LoadSpellbook(unsigned int spell_school)
238 {
239 char pContainer[20]; // [sp+Ch] [bp-1Ch]@7
240
241 byte_506550 = 0;
242 if ( pPlayers[uActiveCharacter]->uQuickSpell && (unsigned __int8)pPlayers[uActiveCharacter]->uQuickSpell / 11 == spell_school )
243 quick_spell_at_page = (unsigned __int8)pPlayers[uActiveCharacter]->uQuickSpell - 11 * spell_school;
244 else
245 quick_spell_at_page = 0;
246
247 for (uint i = 1; i <= 11; ++i)
248 {
249 if (pPlayers[uActiveCharacter]->spellbook.pChapters[spell_school].bIsSpellAvailable[i - 1])
250 {
251 sprintf(pContainer, "SB%sS%02d", spellbook_texture_filename_suffices[spell_school], pSpellbookSpellIndices[spell_school][i]);
252 SBPageSSpellsTextureList[i] = pIcons_LOD->LoadTexturePtr(pContainer, TEXTURE_16BIT_PALETTE);
253
254 sprintf(pContainer, "SB%sC%02d", spellbook_texture_filename_suffices[spell_school], pSpellbookSpellIndices[spell_school][i]);
255 SBPageCSpellsTextureList[i] = pIcons_LOD->LoadTexturePtr(pContainer, TEXTURE_16BIT_PALETTE);
256 }
257 }
258 }
259
260 //----- (00412AF9) --------------------------------------------------------
261 static void BookUI_Spellbook_DrawCurrentSchoolBackground()
262 {
263 int pTexID = 0;
264 if ( uActiveCharacter )
265 pTexID = pParty->pPlayers[uActiveCharacter - 1].lastOpenedSpellbookPage;
266 pRenderer->DrawTextureIndexed(8, 8, pSpellBookPagesTextr[pTexID]);
267 pRenderer->DrawTextureIndexed(476, 450, pSBQuickSpellBtnTextr);
268 pRenderer->DrawTextureIndexed(561, 450, pSpellBookCloseBtnTextr);
269 }
270
271
272
273
274
275 //----- (0041192C) --------------------------------------------------------
276 void InitializeSpellBookTextures()
277 {
278 pAudioPlayer->StopChannels(-1, -1);
279 ++pIcons_LOD->uTexturePacksCount;
280 if (!pIcons_LOD->uNumPrevLoadedFiles)
281 pIcons_LOD->uNumPrevLoadedFiles = pIcons_LOD->uNumLoadedFiles;
282 pAudioPlayer->PlaySound(SOUND_openbook, 0, 0, -1, 0, 0, 0, 0);
283 pSpellBookPagesTextr_9 = pIcons_LOD->LoadTexturePtr("book", TEXTURE_16BIT_PALETTE);
284 pTexture_pagemask = pIcons_LOD->LoadTexturePtr("pagemask", TEXTURE_16BIT_PALETTE);
285 pSpellBookCloseBtnTextr = pIcons_LOD->LoadTexturePtr("ib-m5-u", TEXTURE_16BIT_PALETTE);
286 pSpellBookClickCloseBtnTextr = pIcons_LOD->LoadTexturePtr("ib-m5-d", TEXTURE_16BIT_PALETTE);
287 pSBQuickSpellBtnTextr = pIcons_LOD->LoadTexturePtr("ib-m6-u", TEXTURE_16BIT_PALETTE);
288
289 static const char *texNames[9] = // 004E24EC
290 {
291 "SBFB00", "SBAB00", "SBWB00", "SBEB00",
292 "SBSB00", "SBMB00", "SBBB00", "SBLB00", "SBDB00"
293 };
294
295 pSBClickQuickSpellBtnTextr = pIcons_LOD->LoadTexturePtr("ib-m6-d", TEXTURE_16BIT_PALETTE);
296 for (uint i = 0; i < 9; ++i)
297 {
298 pSpellBookPagesTextr[i] = pIcons_LOD->LoadTexturePtr(texNames[i], TEXTURE_16BIT_PALETTE);
299 sprintf(pTmpBuf.data(), "tab%da", i + 1);
300 pTextures_tabs[i][0] = pIcons_LOD->LoadTexturePtr(pTmpBuf.data(), TEXTURE_16BIT_PALETTE);
301 sprintf(pTmpBuf.data(), "tab%db", i + 1);
302 pTextures_tabs[i][1] = pIcons_LOD->LoadTexturePtr(pTmpBuf.data(), TEXTURE_16BIT_PALETTE);
303 }
304 }
305
306
307 //----- (00411473) --------------------------------------------------------
308 void OnCloseSpellBook()
309 {
310 pTexture_pagemask->Release();
311 pSpellBookCloseBtnTextr->Release();
312 pSBQuickSpellBtnTextr->Release();
313 for (uint i = 0; i < 9; ++i)
314 {
315 pSpellBookPagesTextr[i]->Release();
316 pTextures_tabs[i][0]->Release();
317 pTextures_tabs[i][1]->Release();
318 }
319 pAudioPlayer->PlaySound(SOUND_openbook, 0, 0, -1, 0, 0, 0, 0);
320 pIcons_LOD->RemoveTexturesPackFromTextureList();
321 }