PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/rcg_editor/client/render/controlpoints_client.lua

http://mtasa-resources-rcg.googlecode.com/
Lua | 185 lines | 165 code | 20 blank | 0 comment | 20 complexity | 6adc8919c1e42a7ee09211f8d2c7ef25 MD5 | raw file
  1. local targetedControlPointAlpha = { controlPoint = false, alpha = controlPointAlphaDefault }
  2. local function unitSizeAtPosition(position)
  3. local camX, camY, camZ = getCameraMatrix()
  4. return 120 / vec3.len(position - vec3.new({ camX, camY, camZ }))
  5. end
  6. local function dxDrawCircle(radius, centerX, centerY, color, width, startAngle, endAngle, postGUI)
  7. startAngle = startAngle or 0
  8. endAngle = endAngle or 360
  9. local n = math.ceil(radius * (endAngle - startAngle) / 500)
  10. local anglePiece = math.rad((endAngle - startAngle) / n)
  11. local angle = math.rad(startAngle)
  12. for i = 1, n do
  13. dxDrawLine(centerX + math.cos(angle) * radius,
  14. centerY + math.sin(angle) * radius,
  15. centerX + math.cos(angle + anglePiece) * radius,
  16. centerY + math.sin(angle + anglePiece) * radius,
  17. color,
  18. width,
  19. postGUI)
  20. angle = angle + anglePiece
  21. end
  22. end
  23. local function getScreenPosition(position)
  24. local x, y = getScreenFromWorldPosition(position[1], position[2], position[3], 100000)
  25. if (x and y) then
  26. return vec2.new({ x, y })
  27. end
  28. return false
  29. end
  30. addEventHandler("onClientRender",
  31. gRootElement,
  32. function()
  33. if (isEditorSuspended()) then
  34. return
  35. end
  36. local targetedControlPointAlphaTo = controlPointAlphaDefault
  37. if (gTargetedControlPoint) then
  38. if (targetedControlPointAlpha.controlPoint ~= gTargetedControlPoint.controlPoint) then
  39. targetedControlPointAlpha = { controlPoint = gTargetedControlPoint.controlPoint, alpha = 100 }
  40. end
  41. targetedControlPointAlphaTo = controlPointAlphaSelectedOrTargeted
  42. end
  43. local cX, cY, cZ = getCameraMatrix()
  44. local cameraPosition = vec3.new({ cX, cY, cZ })
  45. targetedControlPointAlpha.alpha = targetedControlPointAlpha.alpha + (targetedControlPointAlphaTo - targetedControlPointAlpha.alpha) / 10
  46. local controlPoints = getElementsByType("controlPoint")
  47. for k, controlPoint in pairs(controlPoints) do
  48. if (isElementInWorkingDimension(controlPoint)) then
  49. local selectedElement = exports.editor_main:getSelectedElement()
  50. local alpha = controlPointAlphaDefault
  51. if (targetedControlPointAlpha and targetedControlPointAlpha.controlPoint == controlPoint) then
  52. alpha = targetedControlPointAlpha.alpha
  53. end
  54. local images = { controlPointImageDefault, controlPointImageDefault, controlPointImageDefault }
  55. if (gSelectedControlPoint and gSelectedControlPoint.controlPoint == selectedElement and gSelectedControlPoint.controlPoint == controlPoint) then
  56. if (gSelectedControlPoint.subControlPoint == subControlPointLeft) then
  57. images = { controlPointImageSelected, controlPointImageDefault, controlPointImageDefault }
  58. elseif (gSelectedControlPoint.subControlPoint == subControlPointCenter) then
  59. images = { controlPointImageDefault, controlPointImageSelected, controlPointImageDefault }
  60. elseif (gSelectedControlPoint.subControlPoint == subControlPointRight) then
  61. images = { controlPointImageDefault, controlPointImageDefault, controlPointImageSelected }
  62. end
  63. alpha = controlPointAlphaSelectedOrTargeted
  64. elseif (controlPoint == getControlPointFromRepresentObject(selectedElement)) then
  65. images = { controlPointImageDefault, controlPointImageSelected, controlPointImageDefault }
  66. alpha = controlPointAlphaSelectedOrTargeted
  67. end
  68. local centerPosition = getSubControlPointCenterPosition(controlPoint)
  69. if (centerPosition and vec3.len(cameraPosition - centerPosition) <= 300) then
  70. local positions = { getSubControlPointLeftPosition(controlPoint), centerPosition, getSubControlPointRightPosition(controlPoint) }
  71. if (positions[1] and positions[3]) then
  72. local screenPositions = { getScreenPosition(positions[1]), getScreenPosition(positions[2]), getScreenPosition(positions[3]) }
  73. if (screenPositions[1] and screenPositions[2] and screenPositions[3]) then
  74. local unitSizes = { unitSizeAtPosition(positions[1]), unitSizeAtPosition(positions[2]), unitSizeAtPosition(positions[3]) }
  75. local direction = screenPositions[3] - screenPositions[1]
  76. local rotation = math.deg(math.atan2(direction[2], direction[1]))
  77. dxDrawImage(screenPositions[1][1] - unitSizes[1] * controlPointInsideRadius,
  78. screenPositions[1][2] - unitSizes[1] * controlPointInsideRadius,
  79. unitSizes[1] * controlPointInsideRadius * 2,
  80. unitSizes[1] * controlPointInsideRadius * 2,
  81. images[1],
  82. rotation,
  83. 0,
  84. 0,
  85. tocolor(255, 255, 255, alpha))
  86. dxDrawImage(screenPositions[2][1] - unitSizes[2] * controlPointInsideRadius,
  87. screenPositions[2][2] - unitSizes[2] * controlPointInsideRadius,
  88. unitSizes[2] * controlPointInsideRadius * 2,
  89. unitSizes[2] * controlPointInsideRadius * 2,
  90. images[2],
  91. rotation,
  92. 0,
  93. 0,
  94. tocolor(255, 255, 255, alpha))
  95. dxDrawImage(screenPositions[3][1] - unitSizes[3] * controlPointInsideRadius,
  96. screenPositions[3][2] - unitSizes[3] * controlPointInsideRadius,
  97. unitSizes[3] * controlPointInsideRadius * 2,
  98. unitSizes[3] * controlPointInsideRadius * 2,
  99. images[3],
  100. rotation,
  101. 0,
  102. 0,
  103. tocolor(255, 255, 255, alpha))
  104. local angles = { { rotation + controlPointOutsideOpenedAngle / 2, rotation + 360 - controlPointOutsideOpenedAngle / 2 },
  105. { rotation + controlPointOutsideOpenedAngle / 2, rotation + 180 - controlPointOutsideOpenedAngle / 2 },
  106. { rotation - 180 + controlPointOutsideOpenedAngle / 2, rotation - controlPointOutsideOpenedAngle / 2 },
  107. { rotation - 180 + controlPointOutsideOpenedAngle / 2, rotation + 180 - controlPointOutsideOpenedAngle / 2 } }
  108. dxDrawCircle(unitSizes[1] * controlPointOutsideRadius,
  109. screenPositions[1][1],
  110. screenPositions[1][2],
  111. tocolor(255, 255, 255, alpha),
  112. controlPointOutsideWidth,
  113. angles[1][1],
  114. angles[1][2])
  115. dxDrawCircle(unitSizes[2] * controlPointOutsideRadius,
  116. screenPositions[2][1],
  117. screenPositions[2][2],
  118. tocolor(255, 255, 255, alpha),
  119. controlPointOutsideWidth,
  120. angles[2][1],
  121. angles[2][2])
  122. dxDrawCircle(unitSizes[2] * controlPointOutsideRadius,
  123. screenPositions[2][1],
  124. screenPositions[2][2],
  125. tocolor(255, 255, 255, alpha),
  126. controlPointOutsideWidth,
  127. angles[3][1],
  128. angles[3][2])
  129. dxDrawCircle(unitSizes[3] * controlPointOutsideRadius,
  130. screenPositions[3][1],
  131. screenPositions[3][2],
  132. tocolor(255, 255, 255, alpha),
  133. controlPointOutsideWidth,
  134. angles[4][1],
  135. angles[4][2])
  136. dxDrawLine(screenPositions[1][1] + math.cos(math.rad(angles[1][1])) * unitSizes[1] * controlPointOutsideRadius,
  137. screenPositions[1][2] + math.sin(math.rad(angles[1][1])) * unitSizes[1] * controlPointOutsideRadius,
  138. screenPositions[2][1] + math.cos(math.rad(angles[2][2])) * unitSizes[2] * controlPointOutsideRadius,
  139. screenPositions[2][2] + math.sin(math.rad(angles[2][2])) * unitSizes[2] * controlPointOutsideRadius,
  140. tocolor(255, 255, 255, alpha),
  141. controlPointOutsideWidth)
  142. dxDrawLine(screenPositions[1][1] + math.cos(math.rad(angles[1][2])) * unitSizes[1] * controlPointOutsideRadius,
  143. screenPositions[1][2] + math.sin(math.rad(angles[1][2])) * unitSizes[1] * controlPointOutsideRadius,
  144. screenPositions[2][1] + math.cos(math.rad(angles[3][1])) * unitSizes[2] * controlPointOutsideRadius,
  145. screenPositions[2][2] + math.sin(math.rad(angles[3][1])) * unitSizes[2] * controlPointOutsideRadius,
  146. tocolor(255, 255, 255, alpha),
  147. controlPointOutsideWidth)
  148. dxDrawLine(screenPositions[2][1] + math.cos(math.rad(angles[2][1])) * unitSizes[2] * controlPointOutsideRadius,
  149. screenPositions[2][2] + math.sin(math.rad(angles[2][1])) * unitSizes[2] * controlPointOutsideRadius,
  150. screenPositions[3][1] + math.cos(math.rad(angles[4][2])) * unitSizes[3] * controlPointOutsideRadius,
  151. screenPositions[3][2] + math.sin(math.rad(angles[4][2])) * unitSizes[3] * controlPointOutsideRadius,
  152. tocolor(255, 255, 255, alpha),
  153. controlPointOutsideWidth)
  154. dxDrawLine(screenPositions[2][1] + math.cos(math.rad(angles[3][2])) * unitSizes[2] * controlPointOutsideRadius,
  155. screenPositions[2][2] + math.sin(math.rad(angles[3][2])) * unitSizes[2] * controlPointOutsideRadius,
  156. screenPositions[3][1] + math.cos(math.rad(angles[4][1])) * unitSizes[3] * controlPointOutsideRadius,
  157. screenPositions[3][2] + math.sin(math.rad(angles[4][1])) * unitSizes[3] * controlPointOutsideRadius,
  158. tocolor(255, 255, 255, alpha),
  159. controlPointOutsideWidth)
  160. end
  161. end
  162. end
  163. end
  164. end
  165. end)