Util.gd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. extends Object
  2. class_name Util
  3. # Logging
  4. static func PrintLog(logGroup : String, logString : String):
  5. print("[%d.%03d][%s] %s" % [Time.get_ticks_msec() / 1000.0, Time.get_ticks_msec() % 1000, logGroup, logString])
  6. static func PrintInfo(_logGroup : String, _logString : String):
  7. # print("[%d.%03d][%s] %s" % [Time.get_ticks_msec() / 1000.0, Time.get_ticks_msec() % 1000, logGroup, logString])
  8. pass
  9. # Object/Node management
  10. static func DuplicateObject(from : Object, to : Object):
  11. for prop in from.get_property_list():
  12. var name : StringName = prop.name
  13. if prop.usage & PROPERTY_USAGE_SCRIPT_VARIABLE and name in to:
  14. var value : Variant = from.get(name)
  15. if typeof(value) in [TYPE_ARRAY, TYPE_DICTIONARY]:
  16. to.set(name, value.duplicate(true))
  17. elif value is Resource:
  18. to.set(name, value.duplicate(true))
  19. else:
  20. to.set(name, value)
  21. static func RemoveNode(node : Node, parent : Node):
  22. if node != null:
  23. if parent != null:
  24. parent.remove_child(node)
  25. node.queue_free()
  26. # Screenshot
  27. static func GetScreenCapture() -> Image:
  28. return Launcher.get_viewport().get_texture().get_image()
  29. # Math
  30. static func UnrollPathLength(path : PackedVector2Array) -> float:
  31. var pathSize : int = path.size()
  32. if pathSize < 2:
  33. return INF
  34. var unrolledPos : Vector2 = Vector2.ZERO
  35. for i in (pathSize-1):
  36. unrolledPos += (path[i] - path[i+1]).abs()
  37. return unrolledPos.length_squared()
  38. static func IsReachableSquared(pos1 : Vector2, pos2 : Vector2, threshold : float) -> bool:
  39. return pos1.distance_squared_to(pos2) < threshold
  40. # Fade
  41. static func FadeInOutRatio(value : float, maxValue : float, fadeIn : float, fadeOut : float) -> float:
  42. var ratio : float = 1.0
  43. if value < fadeIn:
  44. ratio = min(value / fadeIn, ratio)
  45. if value > maxValue - fadeOut:
  46. ratio = min((fadeOut - (value - (maxValue - fadeOut))) / fadeOut, ratio)
  47. return ratio
  48. # Text
  49. static func GetFormatedText(value : String, numberAfterComma : int = 0) -> String:
  50. var commaLocation : int = value.find(".")
  51. var charCounter : int = 0
  52. if commaLocation > 0:
  53. charCounter = commaLocation - 3
  54. else:
  55. charCounter = value.length() - 3
  56. while charCounter > 0:
  57. value = value.insert(charCounter, ",")
  58. charCounter = charCounter - 3
  59. commaLocation = value.find(".")
  60. if commaLocation == -1:
  61. commaLocation = value.length()
  62. if numberAfterComma > 0:
  63. value += "."
  64. if numberAfterComma > 0:
  65. for i in range(value.length() - 1, commaLocation + numberAfterComma):
  66. value += "0"
  67. else:
  68. value = value.substr(0, commaLocation)
  69. return value
  70. # Dictionary
  71. static func DicCheckOrAdd(dic : Dictionary[Variant, Variant], key : Variant, value : Variant):
  72. if not key in dic:
  73. dic[key] = value
  74. elif dic[key] == null:
  75. dic[key] = value
  76. # Platforms
  77. static func IsMobile() -> bool:
  78. return OS.has_feature("mobile") or OS.has_feature("web_android") or OS.has_feature("web_ios")
  79. static func GetPlatformName() -> String:
  80. if LauncherCommons.isWeb and IsMobile():
  81. return "Android"
  82. return OS.get_name()