UIMapOptionsWindow::UIMapOptionsWindow (IFrontend *frontend, ServiceProvider& serviceProvider) :
		UIWindow(UI_WINDOW_OPTIONS, frontend, WINDOW_FLAG_MODAL), _serviceProvider(serviceProvider)
{
	_onPush = _onPop = CMD_MAP_PAUSE;

	UINodeSettingsBackground *background = new UINodeSettingsBackground(frontend, "");
	background->setAmountHorizontal(1);
	add(background);

	UINodeBackToRootButton* backToRoot = new UINodeBackToRootButton(frontend);
	const float gap = std::max(0.01f, getScreenPadding());
	backToRoot->alignTo(background, NODE_ALIGN_CENTER | NODE_ALIGN_TOP, gap);
	add(backToRoot);

	_restartMap = new UINodeButtonImage(frontend, "icon-reload");
	_restartMap->setId("restart-map");
	_restartMap->putUnder(backToRoot, 0.02f);
	_restartMap->setOnActivate(CMD_UI_POP ";" CMD_MAP_RESTART);
	add(_restartMap);

	if (!wantBackButton()) {
		_backButton = nullptr;
		return;
	}

	_backButton = new UINodeBackButton(frontend);
	const float gapBack = std::max(0.01f, getScreenPadding());
	_backButton->alignTo(background, NODE_ALIGN_BOTTOM | NODE_ALIGN_LEFT, gapBack);
	add(_backButton);
}
UINodeBackgroundScene::UINodeBackgroundScene (IFrontend *frontend) :
		UINode(frontend), _reason(&MapFailedReasons::FAILED_NO), _theme(&ThemeTypes::ROCK)
{
	setPadding(getScreenPadding());
	setSize(1.0, 1.0f);
	setBackgroundColor(colorBlack);
	_caveOffIce = loadTexture("ui-scene-cave-ice");
	_caveArtIce = loadTexture("ui-scene-caveart-ice");
	_caveOffRock = loadTexture("ui-scene-cave-rock");
	_caveArtRock = loadTexture("ui-scene-caveart-rock");
	_imageWidth = _caveOffIce->getWidth();
	_imageHeight = _caveOffIce->getHeight();
	_amountHorizontal = getRenderWidth(false) / _imageWidth + 1;
	_amountVertical = getRenderHeight(false) / _imageHeight + 1;

	_tilesIce.push_back(loadTexture("ui-scene-tile1-ice"));
	_tilesIce.push_back(loadTexture("ui-scene-tile2-ice"));
	_groundsIce.push_back(loadTexture("ui-scene-ground1-ice"));
	_groundsIce.push_back(loadTexture("ui-scene-ground2-ice"));
	_tilesRock.push_back(loadTexture("ui-scene-tile1-rock"));
	_tilesRock.push_back(loadTexture("ui-scene-tile2-rock"));
	_groundsRock.push_back(loadTexture("ui-scene-ground1-rock"));
	_groundsRock.push_back(loadTexture("ui-scene-ground2-rock"));

	_failed[&MapFailedReasons::FAILED_WATER_HEIGHT] = loadTexture("dead-waterheight");
	_failed[&MapFailedReasons::FAILED_HITPOINTS] = loadTexture("dead-hitpoints");
	_failed[&MapFailedReasons::FAILED_SIDESCROLL] = loadTexture("dead-hitpoints");
	_failed[&MapFailedReasons::FAILED_NPC_FLYING] = loadTexture("dead-npc-flying");
	_failed[&MapFailedReasons::FAILED_NPC_WALKING] = loadTexture("dead-npc-walking");
	_failed[&MapFailedReasons::FAILED_NPC_FISH] = loadTexture("dead-npc-fish");
	_failed[&MapFailedReasons::FAILED_NPC_MAMMUT] = loadTexture("dead-npc-mammut");
	_failed[&MapFailedReasons::FAILED_NO_MORE_PLAYERS] = loadTexture("dead-hitpoints");
}
UINodeBackground::UINodeBackground (IFrontend *frontend, const std::string& title, bool showVehicle) :
		UINode(frontend), _showVehicle(showVehicle), _title(title)
{
	setPadding(getScreenPadding());
	setSize(1.0, 1.0f);
	setBackgroundColor(colorBlack);
	_cave = loadTexture("ui-scene-cave-ice");
	_vehicle = loadTexture("ui-scene-player");
	_caveArt = loadTexture("ui-scene-caveart-ice");
	_imageWidth = _cave->getWidth();
	_imageHeight = _cave->getHeight();
	_amountHorizontal = _imageWidth <= 0 ? 1 : getRenderWidth(false) / _imageWidth + 1;
	_amountVertical = _imageHeight <= 0 ? 1 : getRenderHeight(false) / _imageHeight + 1;

	const TexturePtr& tile1 = loadTexture("ui-scene-tile1-ice");
	if (tile1)
		_tiles.push_back(tile1);
	const TexturePtr& tile2 = loadTexture("ui-scene-tile2-ice");
	if (tile2)
		_tiles.push_back(tile2);

	_font = getFont(LARGE_FONT);
	Vector4Set(colorWhite, _fontColor);
	_textWidth = _font->getTextWidth(_title);
	_textHeight = _font->getTextHeight(_title);
}
IUIMapWindow::IUIMapWindow (IFrontend *frontend, ServiceProvider& serviceProvider, CampaignManager& campaignManager, IUINodeMap* nodeMap, bool continuousMovement) :
		UIWindow(UI_WINDOW_MAP, frontend, WINDOW_FLAG_MODAL | WINDOW_FLAG_FULLSCREEN), _nodeMap(nodeMap), _waitLabel(nullptr), _mapControl(nullptr),
		_startButton(nullptr), _serviceProvider(serviceProvider), _panel(nullptr), _lastFingerPressEvent(0L), _cursorActive(false), _continuousMovement(continuousMovement) {
	const float screenPadding = getScreenPadding();
	setPadding(screenPadding);
	_playClickSound = false;
	_musicFile = "music-2";
	_onPop = CMD_CL_DISCONNECT;
	_nodeMap->setId(UINODE_MAP);
	add(_nodeMap);
}
UIMainWindow::UIMainWindow (IFrontend *frontend, ServiceProvider& serviceProvider) :
		UIWindow(UI_WINDOW_MAIN, frontend, WINDOW_FLAG_ROOT)
{
	add(new UINodeMainBackground(frontend, false));
	const SpritePtr& mammutSprite = UI::get().loadSprite("ui-npc-mammut");
	_mammut = new UINodeSprite(frontend, mammutSprite->getMaxWidth(), mammutSprite->getMaxHeight());
	_mammut->addSprite(mammutSprite);

	const SpritePtr& grandPaSprite = UI::get().loadSprite("ui-npc-grandpa");
	_grandpa = new UINodeSprite(frontend, grandPaSprite->getMaxWidth(), grandPaSprite->getMaxHeight());
	_grandpa->addSprite(grandPaSprite);

	const SpritePtr& playerSprite = UI::get().loadSprite("ui-player");
	_player = new UINodeSprite(frontend, playerSprite->getMaxWidth(), playerSprite->getMaxHeight());
	_player->addSprite(playerSprite);

	add(_mammut);
	add(_grandpa);
	add(_player);

	const float padding = 10.0f / static_cast<float>(_frontend->getHeight());
	UINode *panel = new UINode(_frontend, "panelMain");
	UIVBoxLayout *layout = new UIVBoxLayout(padding, true);
	panel->setLayout(layout);
	panel->setAlignment(NODE_ALIGN_MIDDLE | NODE_ALIGN_CENTER);
	panel->setPadding(padding);

	UINodeMainButton *campaign = new UINodeMainButton(_frontend, tr("Campaign"));
	campaign->addListener(UINodeListenerPtr(new OpenWindowListener(UI_WINDOW_CAMPAIGN)));
	panel->add(campaign);

#ifndef NONETWORK
	if (Config.isNetwork()) {
		UINodeMainButton *multiplayer = new UINodeMainButton(_frontend, tr("Multiplayer"));
		multiplayer->addListener(UINodeListenerPtr(new OpenWindowListener(UI_WINDOW_MULTIPLAYER)));
		panel->add(multiplayer);
	}
#endif

	UINodeMainButton *settings = new UINodeMainButton(_frontend, tr("Settings"));
	settings->addListener(UINodeListenerPtr(new OpenWindowListener(UI_WINDOW_SETTINGS)));
	panel->add(settings);

	if (System.supportPayment()) {
		UINodeMainButton *payment = new UINodeMainButton(_frontend, tr("Extras"));
		payment->addListener(UINodeListenerPtr(new OpenWindowListener(UI_WINDOW_PAYMENT)));
		panel->add(payment);
	}

	if (System.supportGooglePlay()) {
		UINodeButtonImage *googlePlay = new UINodeGooglePlayButton(_frontend);
		googlePlay->setPadding(padding);
		add(googlePlay);
	}

#ifndef STEAMLINK
	if (System.supportsUserContent()) {
		UINodeMainButton *editor = new UINodeMainButton(_frontend, tr("Editor"));
		editor->addListener(UINodeListenerPtr(new OpenWindowListener(UI_WINDOW_EDITOR)));
		panel->add(editor);
	}

	UINodeMainButton *twitter = new UINodeMainButton(_frontend, tr("Twitter"));
	twitter->addListener(UINodeListenerPtr(new OpenURLListener(_frontend, "https://twitter.com/MartinGerhardy")));
	panel->add(twitter);

	UINodeMainButton *homepage = new UINodeMainButton(_frontend, tr("Homepage"));
	homepage->addListener(UINodeListenerPtr(new OpenURLListener(_frontend, "http://caveproductions.org/")));
	panel->add(homepage);
#endif

	UINodeMainButton *help = new UINodeMainButton(_frontend, tr("Help"));
	help->addListener(UINodeListenerPtr(new OpenWindowListener(UI_WINDOW_HELP)));
	panel->add(help);

#if 0
#ifdef __EMSCRIPTEN__
	UINodeMainButton *fullscreen = new UINodeMainButton(_frontend, tr("Fullscreen"));
	fullscreen->addListener(UINodeListenerPtr(new EmscriptenFullscreenListener()));
	panel->add(fullscreen);
#endif
#endif

	UINodeMainButton *quit = new UINodeMainButton(_frontend, tr("Quit"));
#ifdef __EMSCRIPTEN__
	quit->addListener(UINodeListenerPtr(new OpenURLListener(_frontend, "http://caveproductions.org/", false)));
#else
	quit->addListener(UINodeListenerPtr(new QuitListener()));
#endif
	panel->add(quit);

	add(panel);

	Application& app = Singleton<Application>::getInstance();
	UINodeLabel *versionLabel = new UINodeLabel(_frontend, app.getPackageName() + " " + app.getVersion());
	versionLabel->setAlignment(NODE_ALIGN_BOTTOM|NODE_ALIGN_RIGHT);
	versionLabel->setColor(colorWhite);
	versionLabel->setPadding(getScreenPadding());
	add(versionLabel);
}