1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "encyclopedia_manager.h"
25 #include "interface_manager.h"
26 #include "nel/gui/group_tree.h"
27 #include "nel/gui/view_text_id.h"
28 #include "nel/gui/view_bitmap.h"
29 #include "action_handler_misc.h"
30 #include "../sheet_manager.h"
32 // ***************************************************************************
34 using namespace NLMISC
;
37 // ***************************************************************************
39 CEncyclopediaManager
*CEncyclopediaManager::_Instance
= NULL
;
41 // ***************************************************************************
42 void CEncyclopediaManager::releaseInstance()
49 // ***************************************************************************
50 CEncyclopediaManager::CEncyclopediaManager()
52 _AlbumNameSelected
= 0;
53 _ThemaNameSelected
= 0;
54 _Initializing
= false;
55 _CheckAllFrame
= false;
58 // ***************************************************************************
59 void CEncyclopediaManager::updateAllFrame()
63 if (!isStringWaiting())
66 _CheckAllFrame
= false;
71 // ***************************************************************************
72 void CEncyclopediaManager::update(const CEncyclopediaUpdateMsg
&msg
)
74 // Update local albums
78 case CEncyclopediaUpdateMsg::UpdateInit
:
80 for (uint32 i
= 0; i
< msg
.AllAlbums
.size(); ++i
)
81 updateAlbum(msg
.AllAlbums
[i
]);
83 if (!isStringWaiting())
86 _CheckAllFrame
= true;
88 _Initializing
= false;
90 case CEncyclopediaUpdateMsg::UpdateAlbum
:
91 updateAlbum(msg
.Album
);
93 if (!isStringWaiting())
96 _CheckAllFrame
= true;
99 case CEncyclopediaUpdateMsg::UpdateThema
:
100 updateThema(msg
.AlbumName
, msg
.Thema
);
104 if ((_AlbumNameSelected
!= 0) && (_ThemaNameSelected
== 0))
105 rebuildAlbumPage(_AlbumNameSelected
);
107 if ((_AlbumNameSelected
!= 0) && (_ThemaNameSelected
!= 0))
108 rebuildThemaPage(_ThemaNameSelected
);
111 // ***************************************************************************
112 void CEncyclopediaManager::clickOnAlbum(uint32 albumName
)
114 if ((_AlbumNameSelected
!= albumName
) || (_ThemaNameSelected
!= 0))
116 rebuildAlbumPage(albumName
);
117 _AlbumNameSelected
= albumName
;
118 _ThemaNameSelected
= 0;
122 // ***************************************************************************
123 void CEncyclopediaManager::clickOnThema(uint32 themaName
)
125 if (_ThemaNameSelected
!= themaName
)
127 rebuildThemaPage(themaName
);
128 _ThemaNameSelected
= themaName
;
132 // ***************************************************************************
133 void CEncyclopediaManager::updateAlbum(const CEncyMsgAlbum
&a
)
135 // Search for album...
136 CEncyMsgAlbum
*pA
= getAlbum(a
.Name
);
139 uint32 nBack
= (uint32
)_Albums
.size();
140 _Albums
.push_back(CEncyMsgAlbum());
141 pA
= &_Albums
[nBack
];
143 nlassert(pA
!= NULL
);
145 pA
->RewardBrick
= a
.RewardBrick
;
146 for (uint32 i
= 0; i
< a
.Themas
.size(); ++i
)
147 updateThema(a
.Name
, a
.Themas
[i
]);
150 // ***************************************************************************
151 void CEncyclopediaManager::updateThema(uint32 nAlbumName
, const CEncyMsgThema
&t
)
153 CEncyMsgAlbum
*pA
= getAlbum(nAlbumName
);
154 nlassert(pA
!= NULL
);
155 CEncyMsgThema
*pT
= pA
->getThema(t
.Name
);
156 // Thema not found add it !
159 uint32 nBack
= (uint32
)pA
->Themas
.size();
160 pA
->Themas
.push_back(CEncyMsgThema());
161 pT
= &pA
->Themas
[nBack
];
163 // If we are not initializing so we must open the encyclopedia on the new thema
164 if ( ! _Initializing
)
166 _AlbumNameSelected
= nAlbumName
;
167 _ThemaNameSelected
= t
.Name
;
168 CInterfaceManager
*pIM
= CInterfaceManager::getInstance();
169 CInterfaceElement
*pContainer
= dynamic_cast<CInterfaceElement
*>(CWidgetManager::getInstance()->getElementFromId(CONT_ENCY
));
170 if (pContainer
!= NULL
)
171 pContainer
->setActive(true);
174 nlassert(pT
!= NULL
);
178 // ***************************************************************************
179 CEncyMsgAlbum
*CEncyclopediaManager::getAlbum(uint32 nName
)
181 for (uint32 i
= 0; i
< _Albums
.size(); ++i
)
182 if (_Albums
[i
].Name
== nName
)
187 // ***************************************************************************
188 void CEncyclopediaManager::rebuildAlbumList()
190 CInterfaceManager
*pIM
= CInterfaceManager::getInstance();
191 STRING_MANAGER::CStringManagerClient
*pSMC
= STRING_MANAGER::CStringManagerClient::instance();
193 CGroupTree
*pTree
= dynamic_cast<CGroupTree
*>(CWidgetManager::getInstance()->getElementFromId(LIST_ENCY_ALBUM
));
194 nlassert(pTree
!= NULL
);
196 CGroupTree::SNode
*pRoot
= new CGroupTree::SNode
;
200 for (uint32 i
= 0; i
< _Albums
.size(); ++i
)
202 CGroupTree::SNode
*pAlb
= new CGroupTree::SNode
;
203 pAlb
->Id
= "a_" + toString(i
);
204 pAlb
->AHName
= "ency_click_album";
205 pAlb
->AHParams
= toString(_Albums
[i
].Name
);
206 if (_Albums
[i
].Name
== _AlbumNameSelected
)
208 if (pSMC
->getDynString(_Albums
[i
].Name
, res
))
211 nlwarning("try to construct album without name");
214 for (uint32 j
= 0; j
< _Albums
[i
].Themas
.size(); ++j
)
216 CGroupTree::SNode
*pThm
= new CGroupTree::SNode
;
217 pThm
->Id
= "t_" + toString(i
) + "_" + toString(j
);
218 pThm
->AHName
= "ency_click_thema";
219 pThm
->AHParams
= toString(_Albums
[i
].Themas
[j
].Name
);
220 if (pSMC
->getDynString(_Albums
[i
].Themas
[j
].Name
, res
))
223 nlwarning("try to construct thema without name");
225 pAlb
->addChild(pThm
);
228 pRoot
->addChild(pAlb
);
231 pTree
->setRootNode(pRoot
);
233 // if previously selected album
234 if ((_AlbumNameSelected
!= 0) && (_ThemaNameSelected
== 0))
236 for (uint32 i
= 0; i
< _Albums
.size(); ++i
)
238 if (_Albums
[i
].Name
== _AlbumNameSelected
)
240 pTree
->selectNodeById("a_" + toString(i
));
245 // if previously selected thema
246 if ((_AlbumNameSelected
!= 0) && (_ThemaNameSelected
!= 0))
248 for (uint32 i
= 0; i
< _Albums
.size(); ++i
)
250 if (_Albums
[i
].Name
== _AlbumNameSelected
)
252 for (uint32 j
= 0; j
< _Albums
[i
].Themas
.size(); ++j
)
254 if (_Albums
[i
].Themas
[j
].Name
== _ThemaNameSelected
)
256 pTree
->selectNodeById("t_" + toString(i
) + "_" + toString(j
));
265 // ***************************************************************************
266 void CEncyclopediaManager::rebuildAlbumPage(uint32 albumName
)
269 CEncyMsgAlbum
*pAlbum
= NULL
;
270 // Select the right album
271 for (i
= 0; i
< _Albums
.size(); ++i
)
273 if (_Albums
[i
].Name
== albumName
)
275 _AlbumNameSelected
= _Albums
[i
].Name
;
276 pAlbum
= &_Albums
[i
];
284 // Update the right page
285 CInterfaceManager
*pIM
= CInterfaceManager::getInstance();
287 // Hide and show good group
288 CInterfaceElement
*pIE
= CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_ALBUM
);
289 nlassert(pIE
!= NULL
);
290 pIE
->setActive(true);
291 pIE
= CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_HELP
);
292 pIE
->setActive(false);
293 pIE
= CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_THEMA
);
294 pIE
->setActive(false);
297 CViewTextID
*pVT
= dynamic_cast<CViewTextID
*>(CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_ALBUM
":title"));
298 nlassert(pVT
!= NULL
);
299 pVT
->setTextId(pAlbum
->Name
);
301 // Setup brick reward
302 NLGUI::CDBManager::getInstance()->getDbProp("UI:VARIABLES:ENCY:ALBUMBRICK:SHEET")->setValue32(pAlbum
->RewardBrick
);
303 CViewText
*pRBVT
= dynamic_cast<CViewText
*>(CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_ALBUM
":reward:desc"));
306 STRING_MANAGER::CStringManagerClient
*pSMC
= STRING_MANAGER::CStringManagerClient::instance();
307 CUtfStringView
desc(pSMC
->getSBrickLocalizedDescription(CSheetId(pAlbum
->RewardBrick
)));
308 pRBVT
->setText(desc
.toUtf8());
312 // ***************************************************************************
313 void CEncyclopediaManager::rebuildThemaPage(uint32 themaName
)
316 CEncyMsgThema
*pThema
= NULL
;
317 // Select the right album
318 for (i
= 0; i
< _Albums
.size(); ++i
)
320 pThema
= _Albums
[i
].getThema(themaName
);
323 _AlbumNameSelected
= _Albums
[i
].Name
;
331 // Update the right page
332 CInterfaceManager
*pIM
= CInterfaceManager::getInstance();
334 // Hide and show good group
335 CInterfaceElement
*pIE
= CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_ALBUM
);
336 nlassert(pIE
!= NULL
);
337 pIE
->setActive(false);
338 pIE
= CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_HELP
);
339 pIE
->setActive(false);
340 pIE
= CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_THEMA
);
341 pIE
->setActive(true);
344 CViewTextID
*pVT
= dynamic_cast<CViewTextID
*>(CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_THEMA
":title"));
345 nlassert(pVT
!= NULL
);
346 pVT
->setTextId(pThema
->Name
);
349 pVT
= dynamic_cast<CViewTextID
*>(CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_THEMA
":reward_text:desc"));
350 nlassert(pVT
!= NULL
);
351 pVT
->setTextId(pThema
->RewardText
);
353 // Setup brick reward
354 NLGUI::CDBManager::getInstance()->getDbProp("UI:VARIABLES:ENCY:REWARDBRICK:SHEET")->setValue32(pThema
->RewardSheet
);
355 CViewText
*pRBVT
= dynamic_cast<CViewText
*>(CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_THEMA
":reward:desc"));
356 nlassert(pRBVT
!= NULL
);
357 STRING_MANAGER::CStringManagerClient
*pSMC
= STRING_MANAGER::CStringManagerClient::instance();
358 CEntitySheet
*pES
= SheetMngr
.get(CSheetId(pThema
->RewardSheet
));
361 if (pES
->type() == CEntitySheet::ITEM
)
363 pRBVT
->setText(pSMC
->getItemLocalizedDescription(CSheetId(pThema
->RewardSheet
)));
365 else if (pES
->type() == CEntitySheet::SBRICK
)
367 pRBVT
->setText(pSMC
->getSBrickLocalizedDescription(CSheetId(pThema
->RewardSheet
)));
369 else if (pES
->type() == CEntitySheet::SPHRASE
)
371 pRBVT
->setText(pSMC
->getSPhraseLocalizedDescription(CSheetId(pThema
->RewardSheet
)));
375 // Setup the total number of steps
376 uint32 nNbSteps
= pThema
->NbTask
- 1; // 0th is the rite
377 NLGUI::CDBManager::getInstance()->getDbProp("UI:VARIABLES:ENCY:STEPS")->setValue32(nNbSteps
);
379 // Count number of tasks done
380 uint32 nNbTaskDone
= 0;
381 for (i
= 0; i
< pThema
->NbTask
; ++i
)
382 if (pThema
->getTaskState((uint8
)i
) == 2) // 2 == finished
384 NLGUI::CDBManager::getInstance()->getDbProp("UI:VARIABLES:ENCY:DONE")->setValue32(nNbTaskDone
);
386 // setup rite & tasks
387 for (i
= 0; i
< pThema
->NbTask
; ++i
)
391 sTmp
= PAGE_ENCY_THEMA
":todo2:rite";
393 sTmp
= PAGE_ENCY_THEMA
":todo:task" + toString(i
);
395 // setup task description
396 CViewTextID
*pText
= dynamic_cast<CViewTextID
*>(CWidgetManager::getInstance()->getElementFromId(sTmp
+":desc"));
397 nlassert(pText
!= NULL
);
398 pText
->setTextId(pThema
->TaskName
[i
]);
400 // setup task NPC name
401 CStringPostProcessNPCRemoveTitle
*pSPPRT
= new CStringPostProcessNPCRemoveTitle
;
402 pIM
->addServerID (sTmp
+":npc:hardtext", pThema
->TaskNPCName
[i
], pSPPRT
);
404 // If the task is not known gray it
405 if (pThema
->getTaskState((uint8
)i
) == 0)
408 pText
->setAlpha(160);
410 // If the task is finished toggle it
411 CViewBitmap
*pBitmap
= dynamic_cast<CViewBitmap
*>(CWidgetManager::getInstance()->getElementFromId(sTmp
+":done"));
412 nlassert(pBitmap
!= NULL
);
413 if (pThema
->getTaskState((uint8
)i
) == 2)
414 pBitmap
->setActive(true);
416 pBitmap
->setActive(false);
421 // ***************************************************************************
422 // For the group tree we have to check the incoming phrase because no view id stored
423 bool CEncyclopediaManager::isStringWaiting()
425 STRING_MANAGER::CStringManagerClient
*pSMC
= STRING_MANAGER::CStringManagerClient::instance();
427 for (uint32 i
= 0; i
< _Albums
.size(); ++i
)
430 if (!pSMC
->getDynString(_Albums
[i
].Name
, res
))
432 for (uint32 j
= 0; j
< _Albums
[i
].Themas
.size(); ++j
)
434 if (!pSMC
->getDynString(_Albums
[i
].Themas
[j
].Name
, res
))
442 // ***************************************************************************
444 // ***************************************************************************
446 // ***************************************************************************
447 class CAHEncyClickAlbum
: public IActionHandler
449 virtual void execute (CCtrlBase
* /* pCaller */, const string
&Params
)
451 nlinfo("ency_click_album called");
454 fromString(Params
, albumName
);
455 CEncyclopediaManager::getInstance()->clickOnAlbum(albumName
);
458 REGISTER_ACTION_HANDLER( CAHEncyClickAlbum
, "ency_click_album" );
460 // ***************************************************************************
461 class CAHEncyClickThema
: public IActionHandler
463 virtual void execute (CCtrlBase
* /* pCaller */, const string
&Params
)
465 nlinfo("ency_click_thema called");
468 fromString(Params
, themaName
);
469 CEncyclopediaManager::getInstance()->clickOnThema(themaName
);
472 REGISTER_ACTION_HANDLER( CAHEncyClickThema
, "ency_click_thema" );
474 // ***************************************************************************
476 // ***************************************************************************
478 NLMISC_COMMAND(testEncyclopedia
, "Temp : Simulate the server message","")
480 CEncyclopediaUpdateMsg msg
;
481 msg
.Type
= CEncyclopediaUpdateMsg::UpdateAlbum
;
483 msg
.Album
.Name
= 654645;
484 msg
.Album
.RewardBrick
= CSheetId("bmov00030.sbrick").asInt();
489 thm
.RewardSheet
= CSheetId("bmor00065.sbrick").asInt();
490 thm
.RewardText
= 2002;
493 thm
.TaskName
[0] = 65455;
494 thm
.TaskNPCName
[0] = 321;
495 thm
.TaskName
[1] = 65466;
496 thm
.TaskNPCName
[1] = 335;
497 thm
.TaskName
[2] = 65488;
498 thm
.TaskNPCName
[2] = 348;
501 msg
.Album
.Themas
.push_back(thm
);
503 CEncyclopediaManager::getInstance()->update(msg
);