Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. extends Control
  2.  
  3. export var text = "" setget setText
  4.  
  5. export var wordSpacing = 6
  6. signal wordSpacingChanged
  7.  
  8. const line_scriptLocation = "res://staging/interactiveText2/line.gd"
  9.  
  10. func _ready():
  11.    
  12.     $paragraph.connect("resized",self,"refreshMinSize")
  13.     #test
  14.     setText("1 2 3 4 5 6 7 8 9\n10  11 12 13 14 15 165")
  15.     pass
  16.  
  17. func refreshMinSize():
  18.     $paragraph.rect_size = Vector2(0,0)
  19.     rect_min_size.y = $paragraph.rect_size.y
  20.     pass
  21.  
  22. func setText(newText):
  23.     #analyzing session
  24.     var paragraph = []
  25.     var words = []
  26.    
  27.     var processedWord = ""
  28.    
  29.     for letter in newText:
  30.        
  31.         if letter == "\n":
  32.             words.append(processedWord)
  33.             paragraph.append(words.duplicate(false))
  34.             words.clear()
  35.             processedWord = ""
  36.         elif letter == " ":
  37.             #processedWord += " "
  38.             words.append(processedWord)
  39.             processedWord = ""
  40.         else:
  41.             processedWord += letter
  42.    
  43.     words.append(processedWord)
  44.     paragraph.append(words.duplicate(false))
  45.    
  46.     text = newText
  47.    
  48.     printText(paragraph)
  49.     return paragraph
  50.  
  51. func printText(paragraph):
  52.     var width = rect_size.x
  53.     var paragraphNode = get_node("paragraph")
  54.    
  55.     print(paragraphNode)
  56.     for i in paragraphNode.get_children():
  57.         i.free()
  58.    
  59.     for lines in paragraph:
  60.         var linesNode = VBoxContainer.new()
  61.         paragraphNode.add_child(linesNode)
  62.        
  63.         var currentLine = _createLine()
  64.         linesNode.add_child(currentLine)
  65.         var lineWidth = 0.0
  66.        
  67.         for word in lines:
  68.             var wordNode = _createWord(word)
  69.            
  70.             currentLine.add_child(wordNode)
  71.            
  72. #           printt(
  73. #           currentLine.rect_size.x,"+",wordNode.rect_size.x,
  74. #           "=",currentLine.rect_size.x + wordNode.rect_size.x,
  75. #           ">",width)
  76.            
  77.             printt(lineWidth ,"+", wordNode.rect_size.x,"=", lineWidth + wordNode.rect_size.x,">", width)
  78.            
  79.             if (lineWidth + wordNode.rect_size.x > width):
  80.                
  81.                 var newLine = _createLine()
  82.                 currentLine.next = newLine
  83.                
  84.                 currentLine.remove_child(wordNode)
  85.                 newLine.add_child(wordNode)
  86.                
  87.                 linesNode.add_child(newLine)
  88.                
  89.                 currentLine = newLine
  90.                 lineWidth = 0.0 + wordNode.rect_size.x + wordSpacing
  91.             else:
  92.                 lineWidth += wordNode.rect_size.x + wordSpacing
  93.                
  94.             pass
  95.  
  96. func _createLine():
  97.     var newLine = HBoxContainer.new()
  98.     newLine.set_script( load(line_scriptLocation) )
  99.     newLine["size_flags_horizontal"] = false
  100.     newLine.set('custom_constants/separation', wordSpacing)
  101.     connect("wordSpacingChanged",newLine,"setSpacing")
  102.     return newLine
  103.  
  104. func _createWord(word):
  105.     var label = Label.new()
  106.     label.text = word
  107.     label.name = word
  108.     return label
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement