print("init Creativity Window begin");


local menu = game:getSceneManager():getCreativityWindow()
local vault = resourceManager:getVault(ResourceManager.COMMON)

local fadeInDelay = 0
local fadeInTime = 1
local fadeOutTime = 1

local windowLeft = 390
local windowTop = 273

local frameW = 260
local frameH = 180

pi = 3.1415926535897932384626433832795

-- Создание бакграунда
-- Должен добавляться раньше всех. Ибо в противном случае он накрывает
-- остальные контролы и не дает им мышь


-- local sprite = KrSprite(vault:GetSpriteResource("hatch"))
local sprite = KrSprite(vault:GetSpriteResource("hatch_black"))
setNodeScale(sprite, 256, 192);
-- aniFadeInFadeOut(sprite,0,.3,.3,60)
aniFadeInFadeOut(sprite,0,.3,.3,127)
menu:setBackground(sprite)


--[[
local sprite = KrSprite(vault:GetSpriteResource("hatch"))
sprite:SetColor(KrColorTransform(KrRGBA(0, 0, 0, 96)), -1);
--sprite:SetPos(-windowLeft, -windowTop, -1)
menu:setBackground(sprite)
menu:setBackgroundSize(game:screenWidth(), game:screenHeight())
]]--

windowLeft = 356
windowTop = 273


local windowBack = KrSprite(vault:GetSpriteResource("book_bg"))
aniFadeInFadeOut(windowBack, fadeInDelay, fadeInTime, fadeOutTime, 255)

local pageX = 60;
local pageY = 130;

windowBack:SetPos(pageX, pageY, -1)
menu:setImageFrame(windowBack)

-- Pages

pageX = pageX - 6;
pageY = pageY - 39;

local page1 = KrImNode()

local x1 = 250
local x2 = 629
local y1 = 95

local x = x1
local y = y1

local width = 300
local height = 30
local sensX = -47
local sensY = -15
local textY = sensY + 3;

local rText = 0;
local gText = 0;
local bText = 0;
local aText = 216;

local rHText = 255;
local gHText = 255;
local bHText = 255;
local aHText = 216;

local rowsInPage = 5;

menu:setRowsPerPage(rowsInPage);

local pageSideFlag = 0;
local j = 0;

local mList = game:getMelodyList();
local mCount = mList:getCount();

for i=0, mCount-1 do
	local mItem = mList:getItem(i);


	local button = XButton();

	initXBtn(button, "tochki", x, y, -175, -45, 370, 70, vault)
	initXButtonTextUnshifted(button, mItem:getTitleID(),
					-185, -45, 
					370, 60, 
						"occ24b", KrTextBox.CENTER)

	button:setTextColor(163, 50, 9, 255);
	button:setTextColorBType(XButton.NORMAL_HIGHLIGHTED, 50, 21, 10, 255);
	button:setTextColorBType(XButton.NORMAL_PRESSED, 211, 87, 41, 255);
	button:setTextColorBType(XButton.NORMAL_DISABLED, 163, 50, 9, 127);

	addNode(button:getRoot(), page1);
	menu:registerMelodyButton(button);


--[[

	local button = XButton();
	initXBtn(button, "Btn_game", x, y, sensX, sensY, width, height, vault)
	initXButtonText(button, mItem:getTitleID(),
					sensX, textY, 
					width, height, 
					"occ24bg", KrTextBox.CENTER)


	button:setTextColor(163, 50, 9, 255);
	button:setTextColorBType(XButton.NORMAL_HIGHLIGHTED, 251, 247, 156, 255);
	button:setTextColorBType(XButton.NORMAL_PRESSED, 255, 255, 255, 255);

	addNode(button:getRoot(), page1);
	menu:registerMelodyButton(button);
]]--

	y = y + 80;

	j = j + 1;

	if( j == rowsInPage ) then
		j = 0;
		pageSideFlag = pageSideFlag + 1;
		if( pageSideFlag == 2 ) then
			pageSideFlag = 0;
			x = x1;
			y = y1;
			menu:addPage(page1);
                        page1 = KrImNode();

		else
			x = x2;
			y = y1;
		end

	end
end;

if( j > 0 ) then 
	menu:addPage(page1);
end

---------------------------------------------------

	local button = XButton();

	local x = 1024 - 68
	local y = 18

	local width = 94
	local height = 30
	local sensX = -47
	local sensY = -15
	local textY = sensY + 3;

	local rText = 245;
	local gText = 203;
	local bText = 99;
	local aText = 216;

	local rHText = 255;
	local gHText = 255;
	local bHText = 255;
	local aHText = 216;

	initXBtn(button, "Btn_game", x, y, sensX, sensY, width, height, vault)
	initXButtonText(button, "GAME_SCREEN.MENU",
						sensX, textY, 
						width, height, 
						"occ24bg", KrTextBox.CENTER)

	aniFadeInFadeOut(button:getRoot(),fadeInDelay, fadeInTime, fadeOutTime, 255)
	button:setTextColor(rText, gText, bText, aText);
	button:setTextColorBType(XButton.NORMAL_HIGHLIGHTED, rHText, gHText, bHText, aHText);
	button:setTextColorBType(XButton.NORMAL_PRESSED, rHText, gHText, bHText, aHText);
	button:setAccelerator(XButton.ESCAPE);
	menu:setButton(button);



	button = XButton();
	initXBtn(button, "Btn_arrow_l", 194, 623, -47, -43, 54, 62, vault)
	aniFadeInFadeOut(button:getRoot(),fadeInDelay, fadeInTime, fadeOutTime, 255)
	menu:setLeftArrowButton(button);

	button = XButton();
	initXBtn(button, "Btn_arrow_r", 829, 624, -8, -32, 49, 60, vault)
	aniFadeInFadeOut(button:getRoot(),fadeInDelay, fadeInTime, fadeOutTime, 255)
	menu:setRightArrowButton(button);

-- create and init textBox

local width = 250;
local height= 150;

local textBox = KrTextBox(game:getFontResource("occ24"), width, height, 2, KrTextBox.CENTER);
aniFadeInFadeOut(textBox, fadeInDelay, fadeInTime , fadeOutTime)

textBox:SetPos(windowLeft + 40 + width/2, windowTop + 35 + height/2, -1);

menu:setTextBox(textBox);

-- end crate text box

print("init Creativity Window end");


