| 226 | } |
| 227 | |
| 228 | void TitleScreen::initMainMenu() { |
| 229 | m_mainMenu = make_shared<Pane>(); |
| 230 | auto backMenu = make_shared<Pane>(); |
| 231 | |
| 232 | auto assets = Root::singleton().assets(); |
| 233 | auto config = assets->json("/interface/windowconfig/title.config"); |
| 234 | |
| 235 | StringMap<WidgetCallbackFunc> buttonCallbacks; |
| 236 | buttonCallbacks["singleplayer"] = [=](Widget*) { switchState(TitleState::SinglePlayerSelectCharacter); }; |
| 237 | buttonCallbacks["multiplayer"] = [=](Widget*) { switchState(TitleState::MultiPlayerSelectCharacter); }; |
| 238 | buttonCallbacks["options"] = [=](Widget*) { switchState(TitleState::Options); }; |
| 239 | buttonCallbacks["quit"] = [=](Widget*) { switchState(TitleState::Quit); }; |
| 240 | buttonCallbacks["back"] = [=](Widget*) { back(); }; |
| 241 | buttonCallbacks["mods"] = [=](Widget*) { switchState(TitleState::Mods); }; |
| 242 | |
| 243 | for (auto buttonConfig : config.getArray("mainMenuButtons")) { |
| 244 | String key = buttonConfig.getString("key"); |
| 245 | String image = buttonConfig.getString("button"); |
| 246 | String imageHover = buttonConfig.getString("hover"); |
| 247 | Vec2I offset = jsonToVec2I(buttonConfig.get("offset")); |
| 248 | WidgetCallbackFunc callback = buttonCallbacks.get(key); |
| 249 | bool rightAnchored = buttonConfig.getBool("rightAnchored", false); |
| 250 | |
| 251 | auto button = make_shared<ButtonWidget>(callback, image, imageHover, "", ""); |
| 252 | button->setPosition(offset); |
| 253 | |
| 254 | if (rightAnchored) |
| 255 | m_rightAnchoredButtons.append({button, offset}); |
| 256 | |
| 257 | if (key == "back") |
| 258 | backMenu->addChild(key, button); |
| 259 | else |
| 260 | m_mainMenu->addChild(key, button); |
| 261 | } |
| 262 | |
| 263 | m_mainMenu->setAnchor(PaneAnchor::BottomLeft); |
| 264 | m_mainMenu->lockPosition(); |
| 265 | |
| 266 | backMenu->determineSizeFromChildren(); |
| 267 | backMenu->setAnchor(PaneAnchor::BottomLeft); |
| 268 | backMenu->lockPosition(); |
| 269 | |
| 270 | m_backgroundMenu = make_shared<Pane>(); |
| 271 | m_backgroundMenu->setAnchor(PaneAnchor::BottomLeft); |
| 272 | m_backgroundMenu->lockPosition(); |
| 273 | m_backgroundMenu->addChild("canvas", make_shared<CanvasWidget>()); |
| 274 | m_backgroundMenu->show(); |
| 275 | |
| 276 | m_paneManager.registerPane("mainMenu", PaneLayer::Hud, m_mainMenu); |
| 277 | m_paneManager.registerPane("backMenu", PaneLayer::Hud, backMenu); |
| 278 | |
| 279 | m_scriptComponent = make_shared<ScriptComponent>(); |
| 280 | m_scriptComponent->setLuaRoot(make_shared<LuaRoot>()); |
| 281 | m_scriptComponent->addCallbacks("background", LuaBindings::makeWidgetCallbacks(m_backgroundMenu.get())); |
| 282 | m_scriptComponent->addCallbacks("widget", LuaBindings::makeWidgetCallbacks(m_mainMenu.get())); |
| 283 | m_scriptComponent->setScripts(jsonToStringList(config.getArray("scripts", JsonArray()))); |
| 284 | m_scriptComponent->init(); |
| 285 | } |
nothing calls this directly
no test coverage detected