Skip to content

Instantly share code, notes, and snippets.

@bend-n
Last active June 8, 2022 01:15
Show Gist options
  • Save bend-n/850e9619dc1a42fcbab7174219145a80 to your computer and use it in GitHub Desktop.
Save bend-n/850e9619dc1a42fcbab7174219145a80 to your computer and use it in GitHub Desktop.
extends Node2D
var refs := [] # = [[ node : object, variable : string, (code : string) ]]
var style := StyleBoxFlat.new()
var font: Font
var debug := false
var expr := Expression.new()
const offset := Vector2(10, 10)
const vertical := 15
func _ready() -> void:
# init the themes
var theme_propertys := Control.new()
font = theme_propertys.get_theme_default_font()
style.bg_color = Color("50000000")
theme_propertys.free()
z_index = 5 # put on top
font = font.duplicate()
font.size = vertical * 0.8
debug = is_debug()
visible = debug
set_process(debug)
monitor(Engine, "fps", "get_frames_per_second()") # monitor fps
static func is_debug() -> bool:
for arg in OS.get_cmdline_args():
var keyvalue: PoolStringArray = arg.lstrip("--").to_lower().split("=")
if keyvalue[0] == "debug":
if len(keyvalue) > 1:
return keyvalue[1] in ["true", "1", "on", "yes", "y", ""]
return true
return OS.is_debug_build()
func monitor(node: Object, what: String, code := "") -> void: # `code` doesnt really work well with ternarys
refs.append([node, what, code] if code else [node, what])
func calculate_size() -> Rect2:
var xminsize := 0.0
for set in refs: # find the chonkiest text
var tmp := font.get_string_size(get_string(set)).x
xminsize = tmp if tmp > xminsize else xminsize
return Rect2(Vector2.ZERO, Vector2(xminsize + offset.x, (refs.size()) * vertical) + offset)
func _process(_delta):
if Engine.get_physics_frames() % 4 == 0:
# run slightly less often
update()
func _draw() -> void:
draw_style_box(style, calculate_size())
for i in range(refs.size() - 1, -1, -1): # loop backwards
var pos := Vector2(offset.x, (i + 1) * vertical)
draw_string(font, pos, get_string(refs[i]))
func get_string(set: Array) -> String:
var node: Object = set[0]
if !is_instance_valid(node):
refs.erase(set)
return "invalid!"
var what: String = set[1]
var val: String
if len(set) == 3: # it hath code!
var err := expr.parse(set[2]) # parse the code
if err != OK: # problem with the code
printerr(expr.get_error_text())
return ""
val = str(expr.execute([], node, true)) # execute the code
else:
val = str(node.get(what))
return "%s: %s" % [what, val]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment