Linux multi-monitor fullscreen support
[ryzomcore.git] / ryzom / client / src / interface_v3 / encyclopedia_manager.cpp
blob7d435b64fa6ed76d74555fc0d49a75d6a75dbb70
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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>
7 //
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/>.
23 #include "stdpch.h"
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;
35 using namespace std;
37 // ***************************************************************************
39 CEncyclopediaManager *CEncyclopediaManager::_Instance = NULL;
41 // ***************************************************************************
42 void CEncyclopediaManager::releaseInstance()
44 if( _Instance )
45 delete _Instance;
46 _Instance = NULL;
49 // ***************************************************************************
50 CEncyclopediaManager::CEncyclopediaManager()
52 _AlbumNameSelected = 0;
53 _ThemaNameSelected = 0;
54 _Initializing = false;
55 _CheckAllFrame = false;
58 // ***************************************************************************
59 void CEncyclopediaManager::updateAllFrame()
61 if (_CheckAllFrame)
63 if (!isStringWaiting())
65 rebuildAlbumList();
66 _CheckAllFrame = false;
71 // ***************************************************************************
72 void CEncyclopediaManager::update(const CEncyclopediaUpdateMsg &msg)
74 // Update local albums
76 switch (msg.Type)
78 case CEncyclopediaUpdateMsg::UpdateInit:
79 _Initializing = true;
80 for (uint32 i = 0; i < msg.AllAlbums.size(); ++i)
81 updateAlbum(msg.AllAlbums[i]);
83 if (!isStringWaiting())
84 rebuildAlbumList();
85 else
86 _CheckAllFrame = true;
88 _Initializing = false;
89 break;
90 case CEncyclopediaUpdateMsg::UpdateAlbum:
91 updateAlbum(msg.Album);
93 if (!isStringWaiting())
94 rebuildAlbumList();
95 else
96 _CheckAllFrame = true;
98 break;
99 case CEncyclopediaUpdateMsg::UpdateThema:
100 updateThema(msg.AlbumName, msg.Thema);
101 break;
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);
137 if (pA == NULL)
139 uint32 nBack = (uint32)_Albums.size();
140 _Albums.push_back(CEncyMsgAlbum());
141 pA = &_Albums[nBack];
143 nlassert(pA != NULL);
144 pA->Name = a.Name;
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 !
157 if (pT == NULL)
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);
175 *pT = t;
178 // ***************************************************************************
179 CEncyMsgAlbum *CEncyclopediaManager::getAlbum(uint32 nName)
181 for (uint32 i = 0; i < _Albums.size(); ++i)
182 if (_Albums[i].Name == nName)
183 return &_Albums[i];
184 return NULL;
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;
197 string res;
199 // Add all albums
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)
207 pAlb->Opened = true;
208 if (pSMC->getDynString(_Albums[i].Name, res))
209 pAlb->Text = res;
210 else
211 nlwarning("try to construct album without name");
213 // Add all themas
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))
221 pThm->Text = res;
222 else
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));
241 break;
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));
257 break;
265 // ***************************************************************************
266 void CEncyclopediaManager::rebuildAlbumPage(uint32 albumName)
268 uint32 i;
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];
277 break;
281 if (pAlbum == NULL)
282 return;
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);
296 // Setup title
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"));
304 if (pRBVT != NULL)
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)
315 uint32 i;
316 CEncyMsgThema *pThema = NULL;
317 // Select the right album
318 for (i = 0; i < _Albums.size(); ++i)
320 pThema = _Albums[i].getThema(themaName);
321 if (pThema != NULL)
323 _AlbumNameSelected = _Albums[i].Name;
324 break;
328 if (pThema == NULL)
329 return;
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);
343 // Setup title
344 CViewTextID *pVT = dynamic_cast<CViewTextID*>(CWidgetManager::getInstance()->getElementFromId(PAGE_ENCY_THEMA ":title"));
345 nlassert(pVT != NULL);
346 pVT->setTextId(pThema->Name);
348 // Setup rewards
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));
359 if (pES != NULL)
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
383 ++nNbTaskDone;
384 NLGUI::CDBManager::getInstance()->getDbProp("UI:VARIABLES:ENCY:DONE")->setValue32(nNbTaskDone);
386 // setup rite & tasks
387 for (i = 0; i < pThema->NbTask; ++i)
389 string sTmp;
390 if (i == 0)
391 sTmp = PAGE_ENCY_THEMA ":todo2:rite";
392 else
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)
406 pText->setAlpha(80);
407 else
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);
415 else
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)
429 string res;
430 if (!pSMC->getDynString(_Albums[i].Name, res))
431 return true;
432 for (uint32 j = 0; j < _Albums[i].Themas.size(); ++j)
434 if (!pSMC->getDynString(_Albums[i].Themas[j].Name, res))
435 return true;
439 return false;
442 // ***************************************************************************
443 // ACTION HANDLERS
444 // ***************************************************************************
446 // ***************************************************************************
447 class CAHEncyClickAlbum : public IActionHandler
449 virtual void execute (CCtrlBase * /* pCaller */, const string &Params)
451 nlinfo("ency_click_album called");
453 uint32 albumName;
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");
467 uint32 themaName;
468 fromString(Params, themaName);
469 CEncyclopediaManager::getInstance()->clickOnThema(themaName);
472 REGISTER_ACTION_HANDLER( CAHEncyClickThema, "ency_click_thema" );
474 // ***************************************************************************
475 // DEBUG
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();
485 msg.Album.State = 1;
487 CEncyMsgThema thm;
488 thm.Name = 3003;
489 thm.RewardSheet = CSheetId("bmor00065.sbrick").asInt();
490 thm.RewardText = 2002;
491 thm.State = 1;
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;
499 thm.NbTask = 3;
501 msg.Album.Themas.push_back(thm);
503 CEncyclopediaManager::getInstance()->update(msg);
505 return true;