GetPlayerMapBoundingBox: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
(Added example, finally)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
{{Needs Example}}
This function gets the GUI bounding box of the radar map texture.
This function gets the GUI bounding box of the radar map texture.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">int,int,int,int getPlayerMapBoundingBox ()</syntaxhighlight>
<syntaxhighlight lang="lua">int, int, int, int getPlayerMapBoundingBox ()</syntaxhighlight>


===Returns===
===Returns===
* If the player's map is showing, it returns four integers: ''minX'', ''minY'', ''maxX'', ''maxY''.  These are '''absolute''' position coordinates of where the player's map is drawn on the screen
* If the player's map is showing, it returns four integers: ''minX'', ''minY'', ''maxX'', ''maxY''.  These are '''absolute''' position coordinates of where the player's map is drawn on the screen.
** ''minX,minY'' represent the world coordinate ''-3000,-3000''  
** ''minX, minY'' represent the world coordinates ''-3000, -3000''.
** ''maxX,maxY'' represent the world coordinate ''3000,3000''.
** ''maxX, maxY'' represent the world coordinates ''3000, 3000''.
** Negative values may be returned if these coordinates are off screen.
** Negative values may be returned if these coordinates are off screen.
* If the map was not showing, a ''false'' boolean value is returned.
* If the map is not showing, a ''false'' boolean value is returned.
 


==Example==
==Example==
This example draws the text "Center of SA map" in the F11 map screen where the center of world coordinates is.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--Needs an example
local centerPos = Vector2(0, 0) -- Somewhere in Blueberry Acres
local screenSize = Vector2(guiGetScreenSize())
 
local function drawCenterPosMapText()
    local mapMin, mapMax
    do
        -- We do these operations inside a block to delete intermediate variables automatically
        local mx, my, Mx, My = getPlayerMapBoundingBox()
        if mx then
            mapMin = Vector2(mx, my)
            mapMax = Vector2(Mx, My)
        else
            -- Do not continue if the screen coordinates could not be calculated (i.e. map is not showing)
            return
        end
    end
       
    -- Calculate a factor in range [0, 1] which represents the relative distance to the max coords point from the min coords point of the given coordinates (centerPos)
    -- 0 means that the given point is in mapMin in a certain direction
    -- 1 means that the given point is in mapMax in a certain direction
    -- Assumes the world map is a square whose side is 6000 units long
    local fMx, fMy = (centerPos.x + 3000) / 6000, (centerPos.y + 3000) / 6000
    -- Do the opposite thing for the min coords point
    local fmx, fmy = 1 - fMx, 1 - fMy
       
    -- Use the factors and given screen points to calculate the final screen coords of our world position in the map
    local screenMapPos = Vector2((fmx * mapMin.x) + (fMx * mapMax.x), (fmy * mapMin.y) + (fMy * mapMax.y))
   
    -- Check whether our screen position is within the viewable screen area (this is not necessary when working with dxDrawText anyway)
    if screenMapPos.x > 0 and screenMapPos.y > 0 and screenMapPos.x <= screenSize.x and screenMapPos.y <= screenSize.y then
        -- Finally, draw our desired text
        local width = dxGetTextWidth("Center of SA map")
        dxDrawText("Center of SA map", screenMapPos.x - (width / 2), screenMapPos.y, screenMapPos.x + (width / 2), screenMapPos.y, tocolor(255, 255, 0), 1, "default", "center")
    end
end
addEventHandler("onClientRender", root, drawCenterPosMapText)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See also==
{{Client player functions}}
{{Client player functions}}

Revision as of 15:12, 30 June 2016

This function gets the GUI bounding box of the radar map texture.

Syntax

int, int, int, int getPlayerMapBoundingBox ()

Returns

  • If the player's map is showing, it returns four integers: minX, minY, maxX, maxY. These are absolute position coordinates of where the player's map is drawn on the screen.
    • minX, minY represent the world coordinates -3000, -3000.
    • maxX, maxY represent the world coordinates 3000, 3000.
    • Negative values may be returned if these coordinates are off screen.
  • If the map is not showing, a false boolean value is returned.

Example

This example draws the text "Center of SA map" in the F11 map screen where the center of world coordinates is.

local centerPos = Vector2(0, 0) -- Somewhere in Blueberry Acres
local screenSize = Vector2(guiGetScreenSize())

local function drawCenterPosMapText()
    local mapMin, mapMax
    do
        -- We do these operations inside a block to delete intermediate variables automatically
        local mx, my, Mx, My = getPlayerMapBoundingBox()
        if mx then
            mapMin = Vector2(mx, my)
            mapMax = Vector2(Mx, My)
        else
            -- Do not continue if the screen coordinates could not be calculated (i.e. map is not showing)
            return
        end
    end
         
    -- Calculate a factor in range [0, 1] which represents the relative distance to the max coords point from the min coords point of the given coordinates (centerPos)
    -- 0 means that the given point is in mapMin in a certain direction
    -- 1 means that the given point is in mapMax in a certain direction
    -- Assumes the world map is a square whose side is 6000 units long
    local fMx, fMy = (centerPos.x + 3000) / 6000, (centerPos.y + 3000) / 6000
    -- Do the opposite thing for the min coords point
    local fmx, fmy = 1 - fMx, 1 - fMy
         
    -- Use the factors and given screen points to calculate the final screen coords of our world position in the map
    local screenMapPos = Vector2((fmx * mapMin.x) + (fMx * mapMax.x), (fmy * mapMin.y) + (fMy * mapMax.y))
    
    -- Check whether our screen position is within the viewable screen area (this is not necessary when working with dxDrawText anyway)
    if screenMapPos.x > 0 and screenMapPos.y > 0 and screenMapPos.x <= screenSize.x and screenMapPos.y <= screenSize.y then
        -- Finally, draw our desired text
        local width = dxGetTextWidth("Center of SA map")
        dxDrawText("Center of SA map", screenMapPos.x - (width / 2), screenMapPos.y, screenMapPos.x + (width / 2), screenMapPos.y, tocolor(255, 255, 0), 1, "default", "center")
    end
end
addEventHandler("onClientRender", root, drawCenterPosMapText)

See also