DxDrawCircle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
{{Useful Function}}
{{Client Function}}
<lowercasetitle/>
<lowercasetitle/>
__NOTOC__
__NOTOC__
This function draws a number of 2D lines in order to achieve a circle shape on the screen - rendered for '''one''' frame.  This should be used in conjunction with [[onClientRender]] in order to display continuously.
This function draws a circle shape on the screen - rendered for '''one''' frame.  This should be used in conjunction with [[onClientRender]] in order to display continuously.


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">bool dxDrawCircle ( int posX, int posY [, int radius = 50, int, width = 5, int angleAmount = 1, int startAngle = 0, int stopAngle = 360, int color = white, bool postGUI = false ] )</syntaxhighlight>
<syntaxhighlight lang="lua">bool dxDrawCircle( float posX, float posY, float radius [, float startAngle = 0, float stopAngle = 360, color theColor = white, color theCenterColor = theColor, int segments = 32, int ratio = 1, bPostGUI = false ])</syntaxhighlight>


===Required Arguments===
===Required Arguments===
[[Image:DxDrawCircle_example.png|thumb|An example of how dxDrawCircle function works in practice.]]
[[Image:DxDrawCircle_howworking.png|thumb|An example of how dxDrawCircle function works in practice.]]
 
* '''posX''': An integer representing the '''absolute''' X position of the circle center, represented by pixels on the screen.
* '''posX''': An integer representing the '''absolute''' X position of the circle center, represented by pixels on the screen.
* '''posY''': An integer representing the '''absolute''' Y position of the circle center, represented by pixels on the screen.
* '''posY''': An integer representing the '''absolute''' Y position of the circle center, represented by pixels on the screen.
* '''radius''': An integer representing the radius scale of the circle that is being drawn.


===Optional Arguments===
===Optional Arguments===
{{OptionalArg}}
{{OptionalArg}}
* '''radius''': An integer representing the radius scale of the circle that is being drawn.
* '''width''': An integer representing the width of the line that is being drawn.
* '''angleAmount''': An integer representing the tightness of the circle. Lower amount makes it smoother, higher amount makes it more of a clock looking circle.
* '''startAngle''': An integer representing the angle of the first point of the circle.
* '''startAngle''': An integer representing the angle of the first point of the circle.
* '''stopAngle''': An integer representing the angle of the last point of the circle.
* '''stopAngle''': An integer representing the angle of the last point of the circle.
* '''color''': An integer of the hex color, produced using [[tocolor]] or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
* '''theColor''': An integer of the hex color, produced using [[tocolor]] or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
* '''theCenterColor''': An integer of the hex color, produced using [[tocolor]] or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
* '''segments''': An intiger representing from how many triangles is created circle, more = more smoothly.
* '''ratio''': Ratio between width and height, 2 mean that circle is 2 times widther then higher.
* '''postGUI''': A bool representing whether the line should be drawn on top of or behind any ingame GUI (rendered by CEGUI).
* '''postGUI''': A bool representing whether the line should be drawn on top of or behind any ingame GUI (rendered by CEGUI).


Line 27: Line 29:
==Code==
==Code==
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
This function draw a rectangle with rounded corners.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function dxDrawCircle( posX, posY, radius, width, angleAmount, startAngle, stopAngle, color, postGUI )
function dxDrawRoundedRectangle(x,y,rx,ry,color,radius)
if ( type( posX ) ~= "number" ) or ( type( posY ) ~= "number" ) then
  rx = rx - radius * 2
return false
  ry = ry - radius * 2
end
  x = x + radius
  y = y + radius
local function clamp( val, lower, upper )
  if(rx >= 0 and ry >= 0)then
if ( lower > upper ) then lower, upper = upper, lower end
    dxDrawRectangle(x,y,rx,ry,color)
return math.max( lower, math.min( upper, val ) )
   
end
    dxDrawRectangle(x,y - radius,rx,radius,color)
    dxDrawRectangle(x,y + ry,rx,radius,color)
radius = type( radius ) == "number" and radius or 50
    dxDrawRectangle(x - radius,y,radius,ry,color)
width = type( width ) == "number" and width or 5
    dxDrawRectangle(x + rx,y,radius,ry,color)
angleAmount = type( angleAmount ) == "number" and angleAmount or 1
   
startAngle = clamp( type( startAngle ) == "number" and startAngle or 0, 0, 360 )
    dxDrawCircle(x,y,radius,180,270,color,color,7);
stopAngle = clamp( type( stopAngle ) == "number" and stopAngle or 360, 0, 360 )
    dxDrawCircle(x + rx,y,radius,270,360,color,color,7);
color = color or tocolor( 255, 255, 255, 200 )
    dxDrawCircle(x + rx,y + ry,radius,0,90,color,color,7);
postGUI = type( postGUI ) == "boolean" and postGUI or false
    dxDrawCircle(x,y + ry,radius,90,180,color,color,7);
  end
if ( stopAngle < startAngle ) then
local tempAngle = stopAngle
stopAngle = startAngle
startAngle = tempAngle
end
for i = startAngle, stopAngle, angleAmount do
local startX = math.cos( math.rad( i ) ) * ( radius - width )
local startY = math.sin( math.rad( i ) ) * ( radius - width )
local endX = math.cos( math.rad( i ) ) * ( radius + width )
local endY = math.sin( math.rad( i ) ) * ( radius + width )
dxDrawLine( startX + posX, startY + posY, endX + posX, endY + posY, color, width, postGUI )
end
return true
end
end
dxDrawRoundedRectangle(350,50,100,100,tocolor(0,255,0,255), 20)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
<br />'''Author:''' Socialz
<br />
==Examples==
<section name="Client" class="client" show="true">
This example draws a progressing circle shape in the middle of the screen.
<syntaxhighlight lang="lua">
local screenWidth, screenHeight = guiGetScreenSize( )
local stopAngle = 0
addEventHandler( "onClientRender", root,
function( )
if ( stopAngle < 360 ) then
stopAngle = stopAngle + 5
else
stopAngle = 0
end
dxDrawCircle( screenWidth / 2, screenHeight / 2, nil, nil, nil, nil, stopAngle )
end
)
</syntaxhighlight>
</section>
<section name="Client" class="client" show="true">
This example draws the shape of a circle arc with an angle of 90°.
<syntaxhighlight lang="lua">
addEventHandler( "onClientRender", root,
function( )
-- We're starting to draw the circle at 0° which means that the first point of the arc is ( 200+50 | 200 )
-- Therefore the last point is ( 200 | 200+50 ). > Our arc is the "lower right" quarter of the circle.
dxDrawCircle( 200, 200, 50, 5, 1, 0, 90 )
end
)
</syntaxhighlight>
Example by: Michael89/Trevit
</section>
==See Also==
==See Also==
{{Useful_Functions}}
{{Drawing_functions}}

Revision as of 12:32, 4 August 2018

This function draws a circle shape on the screen - rendered for one frame. This should be used in conjunction with onClientRender in order to display continuously.

Syntax

bool dxDrawCircle( float posX, float posY, float radius [, float startAngle = 0, float stopAngle = 360, color theColor = white, color theCenterColor = theColor, int segments = 32, int ratio = 1, bPostGUI = false ])

Required Arguments

An example of how dxDrawCircle function works in practice.
  • posX: An integer representing the absolute X position of the circle center, represented by pixels on the screen.
  • posY: An integer representing the absolute Y position of the circle center, represented by pixels on the screen.
  • radius: An integer representing the radius scale of the circle that is being drawn.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • startAngle: An integer representing the angle of the first point of the circle.
  • stopAngle: An integer representing the angle of the last point of the circle.
  • theColor: An integer of the hex color, produced using tocolor or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
  • theCenterColor: An integer of the hex color, produced using tocolor or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
  • segments: An intiger representing from how many triangles is created circle, more = more smoothly.
  • ratio: Ratio between width and height, 2 mean that circle is 2 times widther then higher.
  • postGUI: A bool representing whether the line should be drawn on top of or behind any ingame GUI (rendered by CEGUI).

Returns

Returns true if the creation of the 2D circle was successful, false otherwise.

Code

Click to collapse [-]
Client

This function draw a rectangle with rounded corners.

function dxDrawRoundedRectangle(x,y,rx,ry,color,radius)
  rx = rx - radius * 2
  ry = ry - radius * 2
  x = x + radius
  y = y + radius
  if(rx >= 0 and ry >= 0)then
    dxDrawRectangle(x,y,rx,ry,color)
    
    dxDrawRectangle(x,y - radius,rx,radius,color)
    dxDrawRectangle(x,y + ry,rx,radius,color)
    dxDrawRectangle(x - radius,y,radius,ry,color)
    dxDrawRectangle(x + rx,y,radius,ry,color)
    
    dxDrawCircle(x,y,radius,180,270,color,color,7);
    dxDrawCircle(x + rx,y,radius,270,360,color,color,7);
    dxDrawCircle(x + rx,y + ry,radius,0,90,color,color,7);
    dxDrawCircle(x,y + ry,radius,90,180,color,color,7);
  end
end
dxDrawRoundedRectangle(350,50,100,100,tocolor(0,255,0,255), 20)

See Also