Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Control
- export var text = "" setget setText
- export var wordSpacing = 6
- signal wordSpacingChanged
- const line_scriptLocation = "res://staging/interactiveText2/line.gd"
- func _ready():
- $paragraph.connect("resized",self,"refreshMinSize")
- #test
- setText("1 2 3 4 5 6 7 8 9\n10 11 12 13 14 15 165")
- pass
- func refreshMinSize():
- $paragraph.rect_size = Vector2(0,0)
- rect_min_size.y = $paragraph.rect_size.y
- pass
- func setText(newText):
- #analyzing session
- var paragraph = []
- var words = []
- var processedWord = ""
- for letter in newText:
- if letter == "\n":
- words.append(processedWord)
- paragraph.append(words.duplicate(false))
- words.clear()
- processedWord = ""
- elif letter == " ":
- #processedWord += " "
- words.append(processedWord)
- processedWord = ""
- else:
- processedWord += letter
- words.append(processedWord)
- paragraph.append(words.duplicate(false))
- text = newText
- printText(paragraph)
- return paragraph
- func printText(paragraph):
- var width = rect_size.x
- var paragraphNode = get_node("paragraph")
- print(paragraphNode)
- for i in paragraphNode.get_children():
- i.free()
- for lines in paragraph:
- var linesNode = VBoxContainer.new()
- paragraphNode.add_child(linesNode)
- var currentLine = _createLine()
- linesNode.add_child(currentLine)
- var lineWidth = 0.0
- for word in lines:
- var wordNode = _createWord(word)
- currentLine.add_child(wordNode)
- # printt(
- # currentLine.rect_size.x,"+",wordNode.rect_size.x,
- # "=",currentLine.rect_size.x + wordNode.rect_size.x,
- # ">",width)
- printt(lineWidth ,"+", wordNode.rect_size.x,"=", lineWidth + wordNode.rect_size.x,">", width)
- if (lineWidth + wordNode.rect_size.x > width):
- var newLine = _createLine()
- currentLine.next = newLine
- currentLine.remove_child(wordNode)
- newLine.add_child(wordNode)
- linesNode.add_child(newLine)
- currentLine = newLine
- lineWidth = 0.0 + wordNode.rect_size.x + wordSpacing
- else:
- lineWidth += wordNode.rect_size.x + wordSpacing
- pass
- func _createLine():
- var newLine = HBoxContainer.new()
- newLine.set_script( load(line_scriptLocation) )
- newLine["size_flags_horizontal"] = false
- newLine.set('custom_constants/separation', wordSpacing)
- connect("wordSpacingChanged",newLine,"setSpacing")
- return newLine
- func _createWord(word):
- var label = Label.new()
- label.text = word
- label.name = word
- return label
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement