3 Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "guiConfigureWorld.h"
26 #include "guiMessageMenu.h"
27 #include <IGUIButton.h>
28 #include <IGUICheckBox.h>
29 #include <IGUIListBox.h>
30 #include <IGUIStaticText.h>
31 #include <IGUITreeView.h>
33 #include "util/string.h"
39 GUI_ID_MOD_TREEVIEW = 101,
40 GUI_ID_ENABLED_CHECKBOX,
43 GUI_ID_DEPENDS_LISTBOX,
44 GUI_ID_RDEPENDS_LISTBOX,
49 #define QUESTIONMARK_STR L"?"
50 #define CHECKMARK_STR L"\411"
51 #define CROSS_STR L"\403"
53 GUIConfigureWorld::GUIConfigureWorld(gui::IGUIEnvironment* env,
54 gui::IGUIElement* parent, s32 id,
55 IMenuManager *menumgr, WorldSpec wspec):
56 GUIModalMenu(env, parent, id, menumgr),
58 m_gspec(findWorldSubgame(m_wspec.path)),
61 //will be initialized in regenerateGUI()
65 m_gamemods = flattenModTree(getModsInPath(m_gspec.gamemods_path));
68 std::string worldmods_path = wspec.path + DIR_DELIM + "worldmods";
69 m_worldmods = flattenModTree(getModsInPath(worldmods_path));
71 // fill m_addontree with add-on mods
72 std::set<std::string> paths = m_gspec.addon_mods_paths;
73 for(std::set<std::string>::iterator it=paths.begin();
74 it != paths.end(); ++it)
76 std::map<std::string,ModSpec> mods = getModsInPath(*it);
77 m_addontree.insert(mods.begin(), mods.end());
81 m_addonmods = flattenModTree(m_addontree);
83 // collect reverse dependencies
84 for(std::map<std::string, ModSpec>::iterator it = m_addonmods.begin();
85 it != m_addonmods.end(); ++it)
87 std::string modname = (*it).first;
88 ModSpec mod = (*it).second;
89 for(std::set<std::string>::iterator dep_it = mod.depends.begin();
90 dep_it != mod.depends.end(); ++dep_it)
92 m_reverse_depends.insert(std::make_pair((*dep_it),modname));
96 m_settings.readConfigFile((m_wspec.path + DIR_DELIM + "world.mt").c_str());
97 std::vector<std::string> names = m_settings.getNames();
99 // mod_names contains the names of mods mentioned in the world.mt file
100 std::set<std::string> mod_names;
101 for(std::vector<std::string>::iterator it = names.begin();
102 it != names.end(); ++it)
104 std::string name = *it;
105 if (name.compare(0,9,"load_mod_")==0)
106 mod_names.insert(name.substr(9));
109 // find new mods (installed but not mentioned in world.mt)
110 for(std::map<std::string, ModSpec>::iterator it = m_addonmods.begin();
111 it != m_addonmods.end(); ++it)
113 std::string modname = (*it).first;
114 ModSpec mod = (*it).second;
115 // a mod is new if it is not a modpack, and does not occur in
117 if(!mod.is_modpack &&
118 mod_names.count(modname) == 0)
119 m_new_mod_names.insert(modname);
121 if(!m_new_mod_names.empty())
123 GUIMessageMenu *menu =
124 new GUIMessageMenu(Environment, Parent, -1, m_menumgr,
125 wgettext("Warning: Some mods are not configured yet.\n"
126 "They will be enabled by default when you save the configuration. "));
131 // find missing mods (mentioned in world.mt, but not installed)
132 std::set<std::string> missing_mods;
133 for(std::set<std::string>::iterator it = mod_names.begin();
134 it != mod_names.end(); ++it)
136 std::string modname = *it;
137 if(m_addonmods.count(modname) == 0)
138 missing_mods.insert(modname);
140 if(!missing_mods.empty())
142 GUIMessageMenu *menu =
143 new GUIMessageMenu(Environment, Parent, -1, m_menumgr,
144 wgettext("Warning: Some configured mods are missing.\n"
145 "Their setting will be removed when you save the configuration. "));
146 for(std::set<std::string>::iterator it = missing_mods.begin();
147 it != missing_mods.end(); ++it)
148 m_settings.remove("load_mod_"+(*it));
153 void GUIConfigureWorld::drawMenu()
155 gui::IGUISkin* skin = Environment->getSkin();
158 video::IVideoDriver* driver = Environment->getVideoDriver();
160 video::SColor bgcolor(140,0,0,0);
161 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
163 gui::IGUIElement::draw();
167 void GUIConfigureWorld::regenerateGui(v2u32 screensize)
176 Calculate new sizes and positions
178 core::rect<s32> rect(
179 screensize.X/2 - 580/2,
180 screensize.Y/2 - 300/2,
181 screensize.X/2 + 580/2,
182 screensize.Y/2 + 300/2
186 recalculateAbsolutePosition(false);
188 v2s32 size = rect.getSize();
190 v2s32 topleft = v2s32(10, 10);
197 core::rect<s32> rect(0, 0, 200, 20);
199 //proper text is set below, when a mod is selected
200 m_modname_text = Environment->addStaticText(L"Mod: N/A", rect, false,
204 core::rect<s32> rect(0, 0, 200, 20);
205 rect += v2s32(0, 25) + topleft;
207 Environment->addCheckBox(false, rect, this, GUI_ID_ENABLED_CHECKBOX,
208 wgettext("enabled"));
209 m_enabled_checkbox->setVisible(false);
212 core::rect<s32> rect(0, 0, 85, 30);
213 rect = rect + v2s32(0, 25) + topleft;
214 m_enableall = Environment->addButton(rect, this, GUI_ID_ENABLEALL,
215 wgettext("Enable All"));
216 m_enableall->setVisible(false);
219 core::rect<s32> rect(0, 0, 85, 30);
220 rect = rect + v2s32(115, 25) + topleft;
221 m_disableall = Environment->addButton(rect, this, GUI_ID_DISABLEALL,
222 wgettext("Disable All"));
223 m_disableall->setVisible(false);
226 core::rect<s32> rect(0, 0, 200, 20);
227 rect += v2s32(0, 60) + topleft;
228 Environment->addStaticText(wgettext("depends on:"),
229 rect, false, false, this, -1);
232 core::rect<s32> rect(0, 0, 200, 85);
233 rect += v2s32(0, 80) + topleft;
234 m_dependencies_listbox =
235 Environment->addListBox(rect, this, GUI_ID_DEPENDS_LISTBOX, true);
238 core::rect<s32> rect(0, 0, 200, 20);
239 rect += v2s32(0, 175) + topleft;
240 Environment->addStaticText(wgettext("is required by:"),
241 rect, false, false, this, -1);
244 core::rect<s32> rect(0, 0, 200, 85);
245 rect += v2s32(0, 195) + topleft;
246 m_rdependencies_listbox =
247 Environment->addListBox(rect,this, GUI_ID_RDEPENDS_LISTBOX,true);
250 core::rect<s32> rect(0, 0, 340, 250);
251 rect += v2s32(220, 0) + topleft;
252 m_treeview = Environment->addTreeView(rect, this,
253 GUI_ID_MOD_TREEVIEW,true);
254 gui::IGUITreeViewNode* node
255 = m_treeview->getRoot()->addChildBack(L"Add-Ons");
256 buildTreeView(m_addontree, node);
259 core::rect<s32> rect(0, 0, 120, 30);
260 rect = rect + v2s32(330, 270) - topleft;
261 Environment->addButton(rect, this, GUI_ID_CANCEL,
265 core::rect<s32> rect(0, 0, 120, 30);
266 rect = rect + v2s32(460, 270) - topleft;
267 Environment->addButton(rect, this, GUI_ID_SAVE,
272 // at start, none of the treeview nodes is selected, so we select
273 // the first element in the treeview of mods manually here.
274 if(m_treeview->getRoot()->hasChilds())
276 m_treeview->getRoot()->getFirstChild()->setExpanded(true);
277 m_treeview->getRoot()->getFirstChild()->setSelected(true);
278 // Because a manual ->setSelected() doesn't cause an event, we
279 // have to do this here:
284 bool GUIConfigureWorld::OnEvent(const SEvent& event)
287 gui::IGUITreeViewNode* selected_node = NULL;
288 if(m_treeview != NULL)
289 selected_node = m_treeview->getSelected();
291 if(event.EventType==EET_KEY_INPUT_EVENT && event.KeyInput.PressedDown)
293 switch (event.KeyInput.Key) {
298 // irrlicht's built-in TreeView gui has no keyboard control,
299 // so we do it here: up/down to select prev/next node,
300 // left/right to collapse/expand nodes, space to toggle
303 if(selected_node != NULL)
305 gui::IGUITreeViewNode* node = selected_node->getNextVisible();
308 node->setSelected(true);
315 if(selected_node != NULL)
317 gui::IGUITreeViewNode* node = selected_node->getPrevSibling();
320 node->setSelected(true);
325 gui::IGUITreeViewNode* parent = selected_node->getParent();
326 if(selected_node == parent->getFirstChild() &&
327 parent != m_treeview->getRoot())
329 parent->setSelected(true);
337 if(selected_node != NULL && selected_node->hasChilds())
338 selected_node->setExpanded(true);
342 if(selected_node != NULL && selected_node->hasChilds())
343 selected_node->setExpanded(false);
347 if(selected_node != NULL && !selected_node->hasChilds() &&
348 selected_node->getText() != NULL)
350 std::string modname = wide_to_narrow(selected_node->getText());
351 bool checked = m_enabled_checkbox->isChecked();
352 m_enabled_checkbox->setChecked(!checked);
353 setEnabled(modname,!checked);
360 if(event.EventType==EET_GUI_EVENT)
362 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
365 if(!canTakeFocus(event.GUIEvent.Element))
367 dstream<<"GUIConfigureWorld: Not allowing focus change."
369 // Returning true disables focus change
373 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
374 switch(event.GUIEvent.Caller->getID()){
375 case GUI_ID_CANCEL: {
380 for(std::set<std::string>::iterator it = m_new_mod_names.begin();
381 it!= m_new_mod_names.end(); ++it)
383 m_settings.setBool("load_mod_"+(*it),true);
385 std::string worldmtfile = m_wspec.path+DIR_DELIM+"world.mt";
386 m_settings.updateConfigFile(worldmtfile.c_str());
388 // The trailing spaces are because there seems to be a
389 // bug in the text-size calculation. if the trailing
390 // spaces are removed from the message text, the
391 // message gets wrapped and parts of it are cut off:
392 GUIMessageMenu *menu =
393 new GUIMessageMenu(Environment, Parent, -1, m_menumgr,
394 wgettext("Configuration saved. "));
397 ModConfiguration modconf(m_wspec.path);
398 if(!modconf.isConsistent())
400 GUIMessageMenu *menu =
401 new GUIMessageMenu(Environment, Parent, -1, m_menumgr,
402 wgettext("Warning: Configuration not consistent. "));
409 case GUI_ID_ENABLEALL: {
410 if(selected_node != NULL && selected_node->getParent() == m_treeview->getRoot())
412 enableAllMods(m_addonmods,true);
414 else if(selected_node != NULL && selected_node->getText() != NULL)
416 std::string modname = wide_to_narrow(selected_node->getText());
417 ModSpec mod = m_addonmods[modname];
418 enableAllMods(mod.modpack_content,true);
422 case GUI_ID_DISABLEALL: {
423 if(selected_node != NULL && selected_node->getParent() == m_treeview->getRoot())
425 enableAllMods(m_addonmods,false);
427 if(selected_node != NULL && selected_node->getText() != NULL)
429 std::string modname = wide_to_narrow(selected_node->getText());
430 ModSpec mod = m_addonmods[modname];
431 enableAllMods(mod.modpack_content,false);
437 if(event.GUIEvent.EventType==gui::EGET_CHECKBOX_CHANGED &&
438 event.GUIEvent.Caller->getID() == GUI_ID_ENABLED_CHECKBOX)
440 if(selected_node != NULL && !selected_node->hasChilds() &&
441 selected_node->getText() != NULL)
443 std::string modname = wide_to_narrow(selected_node->getText());
444 setEnabled(modname, m_enabled_checkbox->isChecked());
448 if(event.GUIEvent.EventType==gui::EGET_TREEVIEW_NODE_SELECT &&
449 event.GUIEvent.Caller->getID() == GUI_ID_MOD_TREEVIEW)
456 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED &&
457 event.GUIEvent.Caller->getID() == GUI_ID_DEPENDS_LISTBOX)
459 selecting_dep = m_dependencies_listbox->getSelected();
463 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED &&
464 event.GUIEvent.Caller->getID() == GUI_ID_RDEPENDS_LISTBOX)
467 selecting_rdep = m_rdependencies_listbox->getSelected();
471 //double click in a dependency listbox: find corresponding
472 //treeviewnode and select it:
473 if(event.GUIEvent.EventType==gui::EGET_LISTBOX_SELECTED_AGAIN)
475 gui::IGUIListBox* box = NULL;
476 if(event.GUIEvent.Caller->getID() == GUI_ID_DEPENDS_LISTBOX)
478 box = m_dependencies_listbox;
479 if(box->getSelected() != selecting_dep)
482 if(event.GUIEvent.Caller->getID() == GUI_ID_RDEPENDS_LISTBOX)
484 box = m_rdependencies_listbox;
485 if(box->getSelected() != selecting_rdep)
488 if(box != NULL && box->getSelected() != -1 &&
489 box->getListItem(box->getSelected()) != NULL)
491 std::string modname =
492 wide_to_narrow(box->getListItem(box->getSelected()));
493 std::map<std::string, gui::IGUITreeViewNode*>::iterator it =
494 m_nodes.find(modname);
495 if(it != m_nodes.end())
497 // select node and make sure node is visible by
498 // expanding all parents
499 gui::IGUITreeViewNode* node = (*it).second;
500 node->setSelected(true);
501 while(!node->isVisible() &&
502 node->getParent() != m_treeview->getRoot())
504 node = node->getParent();
505 node->setExpanded(true);
514 return Parent ? Parent->OnEvent(event) : false;
517 void GUIConfigureWorld::buildTreeView(std::map<std::string, ModSpec> mods,
518 gui::IGUITreeViewNode* node)
520 for(std::map<std::string,ModSpec>::iterator it = mods.begin();
521 it != mods.end(); ++it)
523 std::string modname = (*it).first;
524 ModSpec mod = (*it).second;
525 gui::IGUITreeViewNode* new_node =
526 node->addChildBack(narrow_to_wide(modname).c_str());
527 m_nodes.insert(std::make_pair(modname, new_node));
529 buildTreeView(mod.modpack_content, new_node);
532 // set icon for node: ? for new mods, x for disabled mods,
533 // checkmark for enabled mods
534 if(m_new_mod_names.count(modname) > 0)
536 new_node->setIcon(QUESTIONMARK_STR);
540 bool mod_enabled = true;
541 if(m_settings.exists("load_mod_"+modname))
542 mod_enabled = m_settings.getBool("load_mod_"+modname);
544 new_node->setIcon(CHECKMARK_STR);
546 new_node->setIcon(CROSS_STR);
553 void GUIConfigureWorld::adjustSidebar()
555 gui::IGUITreeViewNode* node = m_treeview->getSelected();
556 std::wstring modname_w;
557 if(node->getText() != NULL)
558 modname_w = node->getText();
561 std::string modname = wide_to_narrow(modname_w);
563 // if no mods installed, don't show buttons or checkbox on the sidebar
564 if(node->getParent() == m_treeview->getRoot() && !node->hasChilds())
566 m_disableall->setVisible(false);
567 m_enableall->setVisible(false);
568 m_enabled_checkbox->setVisible(false);
572 // if modpack, show enable/disable all buttons. otherwise, show
574 if(node->getParent() == m_treeview->getRoot() ||
575 m_addonmods[modname].is_modpack)
577 m_enabled_checkbox->setVisible(false);
578 m_disableall->setVisible(true);
579 m_enableall->setVisible(true);
580 m_modname_text->setText((L"Modpack: "+modname_w).c_str());
584 m_disableall->setVisible(false);
585 m_enableall->setVisible(false);
586 m_enabled_checkbox->setVisible(true);
587 m_modname_text->setText((L"Mod: "+modname_w).c_str());
590 // the mod is enabled unless it is disabled in the world.mt settings.
591 bool mod_enabled = true;
592 if(m_settings.exists("load_mod_"+modname))
593 mod_enabled = m_settings.getBool("load_mod_"+modname);
594 m_enabled_checkbox->setChecked(mod_enabled);
596 // dependencies of this mod:
597 m_dependencies_listbox->clear();
598 ModSpec mspec = m_addonmods[modname];
599 for(std::set<std::string>::iterator it=mspec.depends.begin();
600 it != mspec.depends.end(); ++it)
602 // check if it is an add-on mod or a game/world mod. We only
603 // want to show add-ons
604 std::string dependency = (*it);
605 if(m_gamemods.count(dependency) > 0)
606 dependency += " (" + m_gspec.id + ")";
607 else if(m_worldmods.count(dependency) > 0)
608 dependency += " (" + m_wspec.name + ")";
609 else if(m_addonmods.count(dependency) == 0)
610 dependency += " (missing)";
611 m_dependencies_listbox->addItem(narrow_to_wide(dependency).c_str());
615 // reverse dependencies of this mod:
616 m_rdependencies_listbox->clear();
617 std::pair< std::multimap<std::string, std::string>::iterator,
618 std::multimap<std::string, std::string>::iterator > rdep =
619 m_reverse_depends.equal_range(modname);
620 for(std::multimap<std::string,std::string>::iterator it = rdep.first;
621 it != rdep.second; ++it)
623 // check if it is an add-on mod or a game/world mod. We only
624 // want to show add-ons
625 std::string rdependency = (*it).second;
626 if(m_addonmods.count(rdependency) > 0)
627 m_rdependencies_listbox->addItem(narrow_to_wide(rdependency).c_str());
631 void GUIConfigureWorld::enableAllMods(std::map<std::string, ModSpec> mods,bool enable)
633 for(std::map<std::string, ModSpec>::iterator it = mods.begin();
634 it != mods.end(); ++it)
636 ModSpec mod = (*it).second;
638 // a modpack, recursively enable all mods in it
639 enableAllMods(mod.modpack_content,enable);
640 else // not a modpack
641 setEnabled(mod.name, enable);
645 void GUIConfigureWorld::enableMod(std::string modname)
647 m_settings.setBool("load_mod_"+modname,true);
648 std::map<std::string,gui::IGUITreeViewNode*>::iterator it =
649 m_nodes.find(modname);
650 if(it != m_nodes.end())
651 (*it).second->setIcon(CHECKMARK_STR);
652 m_new_mod_names.erase(modname);
653 //also enable all dependencies
654 ModSpec mspec = m_addonmods[modname];
655 for(std::set<std::string>::iterator it=mspec.depends.begin();
656 it != mspec.depends.end(); ++it)
658 std::string dependency = *it;
659 // only enable it if it is an add-on mod
660 if(m_addonmods.count(dependency) > 0)
661 enableMod(dependency);
665 void GUIConfigureWorld::disableMod(std::string modname)
667 m_settings.setBool("load_mod_"+modname,false);
668 std::map<std::string,gui::IGUITreeViewNode*>::iterator it =
669 m_nodes.find(modname);
670 if(it != m_nodes.end())
671 (*it).second->setIcon(CROSS_STR);
672 m_new_mod_names.erase(modname);
673 //also disable all mods that depend on this one
674 std::pair<std::multimap<std::string, std::string>::iterator,
675 std::multimap<std::string, std::string>::iterator > rdep =
676 m_reverse_depends.equal_range(modname);
677 for(std::multimap<std::string,std::string>::iterator it = rdep.first;
678 it != rdep.second; ++it)
680 std::string rdependency = (*it).second;
681 // only disable it if it is an add-on mod
682 if(m_addonmods.count(rdependency) > 0)
683 disableMod(rdependency);