Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A custom textureprogress node for godot
extends Container
class_name CustomTextureProgress
tool
export(Texture) var textureUnder setget setUnderTexture
export(Texture) var textureProgress setget setProgressTexture
export(Texture) var textureOver setget setOverTexture
export(int) var stretchMarginLeft setget setStretchMarginLeft
export(int) var stretchMarginTop setget setStretchMarginTop
export(int) var stretchMarginRight setget setStretchMarginRight
export(int) var stretchMarginBottom setget setStretchMarginBottom
export(Material) var materialUnder setget setUnderMaterial
export(Material) var materialProgress setget setProgressMaterial
export(Material) var materialOver setget setOverMaterial
export(float) var maxValue = 100 setget setMax
export(float) var minValue = 0 setget setMin
export(float) var value = 50 setget setValue
var under = NinePatchRect.new()
var progress = NinePatchRect.new()
var over = NinePatchRect.new()
var rects = [under, progress, over]
func _init():
for rect in rects:
rect.anchor_left = 0
rect.anchor_right = 1
rect.anchor_top = 0
rect.anchor_bottom = 1
add_child(rect)
updateProgress()
func setStretchMarginLeft(amount):
stretchMarginLeft = amount
for rect in rects: rect.patch_margin_left = amount
updateMinSize()
func setStretchMarginTop(amount):
stretchMarginTop = amount
for rect in rects: rect.patch_margin_top = amount
updateMinSize()
func setStretchMarginRight(amount):
stretchMarginRight = amount
for rect in rects: rect.patch_margin_right = amount
updateMinSize()
func setStretchMarginBottom(amount):
stretchMarginBottom = amount
for rect in rects: rect.patch_margin_bottom = amount
updateMinSize()
func updateMinSize():
rect_min_size.y = stretchMarginTop + stretchMarginBottom
rect_min_size.x = stretchMarginLeft + stretchMarginRight
update()
func setUnderTexture(texture):
textureUnder = texture
under.texture = texture
update()
func setProgressTexture(texture):
textureProgress = texture
progress.texture = texture
update()
func setOverTexture(texture):
textureOver = texture
over.texture = texture
update()
func setUnderMaterial(mat):
materialUnder = mat
under.material = mat
update()
func setProgressMaterial(mat):
materialProgress = mat
progress.material = mat
update()
func setOverMaterial(mat):
materialProgress = mat
over.material = mat
update()
func setMax(amount):
maxValue = amount
updateProgress()
func setMin(amount):
minValue = amount
updateProgress()
func setValue(amount):
value = amount
updateProgress()
func updateProgress():
var fract = (value - minValue)/ (maxValue - minValue)
progress.anchor_right = fract
progress.margin_right = 0
update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.