void UINode::renderDebug (int x, int y, int textY) const
{
	const int panelX = getRenderX();
	const int panelY = getRenderY();
	const int w = getRenderWidth(false);
	const int h = getRenderHeight(false);

	const Color* color[5];
	color[0] = &colorGreen;
	color[1] = &colorBlue;
	color[2] = &colorRed;
	color[3] = &colorYellow;
	color[4] = &colorCyan;

	const int index = (panelX * 22 * h + panelY * 23 * w) % 5;
	renderRect(x + getRenderX(false), y + getRenderY(false), w, h, *color[index]);
	if (!fequals(_padding, 0.0f)) {
		renderRect(x + panelX, y + panelY, getRenderWidth(), getRenderHeight(), *color[index]);
	}
	renderFilledRect(x + getRenderCenterX(), y + getRenderCenterY(), 4, 4, colorRed);
	renderFilledRect(x + panelX, y + panelY, 4, 4, colorBlue);

	const BitmapFontPtr& font = getFont(MEDIUM_FONT);
	int panelTextY = textY;
	for (UINodeListConstIter i = _nodes.begin(); i != _nodes.end(); ++i) {
		const UINode* nodePtr = *i;
		if (!nodePtr->hasFocus())
			continue;
		nodePtr->renderDebug(x + panelX, y + panelY, panelTextY);
		const std::string debugInfo = "[id=" + nodePtr->getId() + "]";
		_frontend->renderFilledRect(x - 1, panelTextY + 1, font->getTextWidth(debugInfo) + 2, font->getTextHeight(debugInfo) + 2, colorGrayAlpha);
		font->print(debugInfo, colorCyan, x, panelTextY);
		panelTextY += font->getTextHeight(debugInfo);
	}
}
void UINodeBackground::renderMiddle(int x, int y) const {
	const int renderHeight = getRenderHeight(false);
	renderImage(getCave(), x + getRenderX(false), y + getRenderY(false) + renderHeight - _imageHeight);
	if (_amountVertical > 1)
		renderImage(getCaveArt(), x + getRenderX(false), y + getRenderY(false) + renderHeight - 2 * _imageHeight);

	const int tileCnt = (int)_tiles.size();
	if (tileCnt > 0) {
		for (int row = 3; row <= _amountVertical; ++row) {
			renderImage(_tiles[(row * _amountHorizontal) % tileCnt],
					x + getRenderX(false), y + getRenderY(false) + renderHeight - row * _imageHeight);
		}

		for (int row = 1; row <= _amountVertical; ++row) {
			for (int col = 1; col < _amountHorizontal; ++col) {
				renderImage(_tiles[((row * _amountHorizontal) + col) % tileCnt],
						x + getRenderX(false) + _imageWidth * col,
						y + getRenderY(false) + renderHeight - row * _imageHeight);
			}
		}
	}
	if (_showVehicle && _amountHorizontal > 1 && _vehicle)
		renderImage(_vehicle, x + getRenderX(false) + _imageWidth, y + getRenderY(false) + renderHeight - _vehicle->getHeight());

	if (!_title.empty()) {
		x += getRenderCenterX() - _textWidth / 2;
		y += getRenderY() + 10;

		_font->print(_title, _fontColor, x, y);
	}
}
void UINodeBackgroundScene::renderFailedCenter (int x, int y, const MapFailedReason& reason) const
{
	FailedMap::const_iterator i = _failed.find(&reason);
	if (i == _failed.end())
		return;
	const TexturePtr& t = i->second;
	if (!t || !t->isValid())
		return;

	const int playerX = x + getRenderCenterX() - t->getWidth() / 2;
	const int playerY = y + getRenderCenterY() - t->getHeight() / 2;
	renderImage(t, playerX, playerY);
}
void UINodeBackgroundScene::renderFailedOnGround (int x, int y, const MapFailedReason& reason, float offsetY) const
{
	renderCave(x, y);
	const int groundY = renderGround(x, y);
	FailedMap::const_iterator i = _failed.find(&reason);
	if (i == _failed.end())
		return;
	const TexturePtr& t = i->second;
	if (!t || !t->isValid())
		return;

	const int playerX = x + getRenderCenterX() - t->getWidth() / 2;
	const int playerY = groundY - t->getHeight() + offsetY * t->getHeight();
	renderImage(t, playerX, playerY);
}
void UINode::renderTop (int x, int y) const
{
	const int childX = x + getRenderX();
	const int childY = y + getRenderY();

	if (_renderBorder) {
		const int w = getRenderWidth(false);
		const int h = getRenderHeight(false);
		renderRect(x + getRenderX(false), y + getRenderY(false), w, h, _borderColor);
	}

	int textYOffset = 0;
	for (DelayedTextsConstIter i = _texts.begin(); i != _texts.end(); ++i) {
		const UINodeDelayedText& t = *i;
		const int fontX = t.pos.x > 0.00001f ? (t.pos.x * _frontend->getWidth()) : (getRenderCenterX() - t.font->getTextWidth(t.text) / 2.0f);
		int fontY;
		if (t.pos.y > 0.00001f) {
			fontY = t.pos.y * _frontend->getHeight();
		} else {
			const int textHeight = t.font->getTextHeight(t.text);
			fontY = textYOffset + getRenderCenterY() - textHeight / 2.0f;
			textYOffset += textHeight;
		}
		t.font->print(t.text, colorWhite, fontX, fontY);
	}

	const bool debug = Config.isDebugUI();
	const bool focus = hasFocus();
	if (debug && focus)
		renderDebug(x, y, y + 20);

	for (UINodeListConstIter i = _nodes.begin(); i != _nodes.end(); ++i) {
		const UINode* nodePtr = *i;
		if (!nodePtr->isVisible())
			continue;
		nodePtr->render(childX, childY);
	}
}