--Переменные--
local key_pressed = false
local non_mod_udate = utils_ui.UIInfoItem.Update
local displacement1 = 0
local displacement2 = 0

function utils_ui.UIInfoItem:Update(obj, sec, flags)
	non_mod_udate(self, obj, sec, flags)
	if not self:Pass(obj, sec) then
		self:Show(false)
		return
	end
	local non_mod = self.dialog
	local parameter = self.frame
	local bias_x, bias_y = 0,0
	cursor_position = GetCursorPosition()
	
	local tooltip_position  = vector2():set(key_pressed and cursor_position.x+displacement1 or cursor_position.x+displacement2,cursor_position.y)
	local w = parameter and parameter:GetWidth() or non_mod:GetWidth()
	local h = parameter and parameter:GetHeight() or non_mod:GetHeight()
	if (1024 - tooltip_position.x < w) then
		tooltip_position.x = tooltip_position.x - w
	end
	if (768 - tooltip_position.y < h) then
		local y = tooltip_position.y - h
		tooltip_position.y = y < 0 and 0 or y
	end
	if cursor_position.x <= 514 and cursor_position.x >= 510 and cursor_position.y <= 386 and cursor_position.y >= 382 then
		non_mod:Show(false)
	else
		non_mod:SetWndPos(vector2():set( tooltip_position.x + (bias_x or 0) , tooltip_position.y + (bias_y or 0) ))
	end
end


--Функция режима скрытия описания--
function on_mode_change()
	if hide_mode then
		displacement2 = 10000
	else
		displacement2 = 0
	end
end

--Функции нажатия клавиш--

local function on_key_press(key)
	on_mode_change()
	if hide_mode and key == KEY_HIDE then
		key_pressed = true
		displacement2 = 0
	elseif not hide_mode and key == KEY_HIDE then
		key_pressed = true
		displacement1 = 10000
	end
end

local function on_key_release(key)
	on_mode_change()
	if hide_mode and key == KEY_HIDE then
		key_pressed = false
		displacement2 = 10000
	elseif not hide_mode and key == KEY_HIDE then
		key_pressed = false
		displacement1 = 0
	end
end

function on_option_change()
	KEY_HIDE = zzzz_hide_tooltip_mcm.get_config("key_hide")
	hide_mode = zzzz_hide_tooltip_mcm.get_config("key_mode")
end




--Определение функций на старте игры--
function on_game_start()
	RegisterScriptCallback("on_option_change",on_option_change)
	RegisterScriptCallback("on_key_release",on_key_release)
    RegisterScriptCallback("on_key_press",on_key_press)
	RegisterScriptCallback("on_mode_change",on_mode_change)
	on_mode_change()
	on_option_change()
end