local mad = require "MoonAdditions"

-- Constants
local MENU_TITLE_SIZE = 1.40
local MENU_ITEM_HEIGHT = 17
local MENU_ITEM_WIDTH = 125
local MENU_WIDTH = 190
local MENU_ITEM_SPACING = 1  -- Additional space between items
local MENU_COLOR = {r = 153, g = 238, b = 153, a = 255}
local TEXT_COLOR = {r = 255, g = 255, b = 255, a = 255}
local CURSOR_HOVER_COLOR = {r = 255, g = 170, b = 255, a = 255}
local DESCRIPTION_COLOR = {r = 255, g = 230, b = 100, a = 255} -- Yellow color for descriptions
local DESCRIPTION_FONT_SIZE = 0.75  -- Smaller font size for descriptions

-- Static menu coordinates
local MENU_X = 45
local MENU_Y = 240

local currentMenu = "MainMenu"             -- Current active menu
local wasLMBDown = false                   -- Track state of the left mouse button
local wasRMBDown = false                   -- Track state of the right mouse button

local textAnimation = "..."
local lastUpdateTime = os.time()
local updateInterval = 0.5

local camhack_status	= true 
local cam_slot			= 0

-- Define each menu with items, actions, descriptions, and parent menu
local menus = function() return {
    MainMenu = {
        title = "Main Menu",
        parent = nil,  -- No parent for the main menu
        items = {
            {label = "Positions", action = function() currentMenu = 'PosMenu' printStringNow("Positions action triggered!", 500) end, description = "Configure camera positions"},
            {label = "Playback", action = function() printStringNow("Playback action triggered!", 500) end, description = "Play recorded audio"},
            {label = "Pause", action = function() printStringNow("Pause action triggered!", 500) end, description = "Pause current action"},
            {label = "Car slot", action = function() printStringNow("Car slot action triggered!", 500) end, description = "Manage car storage"},
            {label = "Save/Load", action = function() printStringNow("Save/Load action triggered!", 500) end, description = "Save or load settings"},
            {label = "Triggers", action = function() printStringNow("Triggers action triggered!", 500) end, description = "Configure trigger actions"},
            {label = "Options", action = function() printStringNow("Options action triggered!", 500) end, description = "Adjust settings"},
        }
    },
	PosMenu = {
		title = "Positions",
		parent = "MainMenu",  -- Specify the parent menu
		items = {
			{label = "Camhack: " .. (camhack_status and "~g~~h~~h~~h~ON" or "~r~~h~~h~~h~OFF"), action = function() printStringNow("Toggle CamHack!", 500) camhack_status = not camhack_status end, description = "Toggle Camhack"},
			{label = "Cam Slot: " .. cam_slot, action = function() printStringNow("Display Current Camera Slot!", 500) end, description = "Select slot to Load or Save"},
			{label = "Preview", action = function() printStringNow("Preview Camera Position!", 500) end, description = "Preview current camera"},
			{label = "Save", action = function() printStringNow("Save Camera Position!", 500) end, description = "Save current camera position to slot"},
			{label = "Load", action = function() printStringNow("Load Camera Position!", 500) end, description = "Load camera position from slot"},
			{label = "", action = function() end, description = ""},
			{label = "Back", action = function() currentMenu = 'MainMenu' printStringNow("Back to Main Menu!", 500) end, description = "Return to the main menu"},
		}
	}
} end

-- Draw a text with given attributes
function drawText(text, x, y, size, color, isHovered)
    local style = mad.font_style.FONT_SUBTITLES
    local width = size * 0.58
    local height = size * 1.02
    local align = mad.font_align.LEFT
    local wrap = MENU_WIDTH
    local proportional = true
    local justify = false  

    local textColor = isHovered and CURSOR_HOVER_COLOR or color
    mad.draw_text(text, x, y, style, width, height, align, wrap, proportional, justify, 
                  textColor.r, textColor.g, textColor.b, textColor.a, 
                  1, 0, 0, 0, 0, 255, 0, 0, 0, 0)
end

-- Check if a point is within a rectangular area
function isPointInArea(pointX, pointY, rectX, rectY, rectWidth, rectHeight)
    return pointX >= rectX and pointX <= rectX + rectWidth and
           pointY >= rectY and pointY <= rectY + rectHeight
end

-- Display and manage the current menu
function displayMenu(menu)
    local cursorX, cursorY = getCursorPos()
    local isLMBDown = isKeyDown(1)  -- Check if the left mouse button is pressed
    local isRMBDown = isKeyDown(2)  -- Check if the right mouse button is pressed
    local currentDescription = ""

    drawText(menu.title, MENU_X, MENU_Y, MENU_TITLE_SIZE, MENU_COLOR, false)

    for i, item in ipairs(menu.items) do
        local itemY = MENU_Y + 10 + (MENU_ITEM_HEIGHT + MENU_ITEM_SPACING) * i
        local isHovered = isPointInArea(cursorX, cursorY, MENU_X, itemY, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT)

        -- Draw the menu item
        drawText(item.label, MENU_X, itemY, 1, TEXT_COLOR, isHovered)

        -- Update the current description based on the hovered item
        if isHovered then
            currentDescription = item.description
        end

        if isHovered and isLMBDown and not wasLMBDown then
            item.action()
        end
    end

    -- Draw the current description under the last menu item with smaller size
    local descriptionY = MENU_Y + 15 + (MENU_ITEM_HEIGHT + MENU_ITEM_SPACING) * (#menu.items + 1)
    drawText(currentDescription, MENU_X, descriptionY, DESCRIPTION_FONT_SIZE, DESCRIPTION_COLOR, false)

    -- Switch to parent menu if right mouse button is pressed
    if isRMBDown and not wasRMBDown and menu.parent then
        currentMenu = menu.parent
        printStringNow("Switching to parent menu", 500)
    end

    wasLMBDown = isLMBDown  -- Update the LMB state
    wasRMBDown = isRMBDown  -- Update the RMB state
end

function updateText()
    local currentTime = os.time()

    if currentTime - lastUpdateTime >= updateInterval then
        if textAnimation == "." then
            textAnimation = ".."
        elseif textAnimation == ".." then
            textAnimation = "..."
        else
            textAnimation = "."
        end

        print(textAnimation)

        lastUpdateTime = currentTime
    end
end


-- Main function
function main()
    while not isPlayerPlaying(PLAYER_HANDLE) do wait(100) end
    wait(100)

    while true do wait(0)
		updateText()
        -- Display the current active menu
        local menu = menus()[currentMenu]
        if menu then
            displayMenu(menu)
        end
    end
end






дай гайд как добавить в этот код поддержку действий не только на лкм но и на колесико вверх вниз