Skip to content

Commit

Permalink
Added comments to the code, and cleaned up a little
Browse files Browse the repository at this point in the history
  • Loading branch information
rossunger committed Dec 27, 2021
1 parent f11aa7b commit 66e414e
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 61 deletions.
45 changes: 30 additions & 15 deletions ExtraGui/Draggable.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends Control
class_name Draggable, "move_icon.png"

# This component makes it's parent draggable, resizeable, and deleteable

var resizing = false
var moving = false
var renaming = false
Expand Down Expand Up @@ -29,22 +31,28 @@ func _ready():

resizeHandle = get_node(resizeHandle)
if resizeHandle:
resizeHandle.connect("gui_input", self, "resize")
resizeHandle.connect("gui_input", self, "resize")

moveHandle = get_node(moveHandle)
moveHandle.mouse_filter = MOUSE_FILTER_STOP
moveHandle.connect("gui_input", self, "click")

#Show [X] (close button) only when the mouse is over the move handle
moveHandle.connect("mouse_entered", self, "showCloseButton")
moveHandle.connect("mouse_exited", self, "showCloseButton")

parent.connect("resized", self, "validate_size")
add_to_group("draggable")

add_to_group("draggable")
parent.add_user_signal("startMoveResize")
parent.add_user_signal("endMoveResize")
parent.add_user_signal("doRemove")

parent.connect("resized", self, "validate_size")
parent.connect("doRemove", self, "doRemove")

grow_parent_as_needed()

#Create a close button
if canClose && !closeButton:
closeButton = Button.new()
closeButton.text = "X"
Expand All @@ -59,6 +67,7 @@ func _ready():
closeButton.call_deferred("connect", "pressed", parent, "emit_signal", ["doRemove"])
#connect("pressed", self, "doRemove")

#called automatically via signals / groups
func doRemove():
parent.get_parent().call_deferred("remove_child", parent)

Expand All @@ -68,6 +77,7 @@ func showCloseButton():
else:
closeButton.visible = false

#called when you click on the move handle
func click(event:InputEvent):
if event as InputEventMouseButton:
if event.button_index == 1 and !event.is_pressed():
Expand All @@ -82,23 +92,25 @@ func click(event:InputEvent):
get_tree().call_group("selectable", "select_one", parent)
get_tree().call_group("draggable", "startMove")

#called when you click on the resize handle
func resize(event):
if !canResizeX && !canResizeY:
return
if event as InputEventMouseButton:
if event.button_index == 1 and event.is_pressed():
if event as InputEventMouseButton && event.button_index == 1:
#on Mouse Down...
if event.is_pressed():
if !get_tree().get_nodes_in_group("selected").has(parent):
if Input.is_key_pressed(KEY_SHIFT):
get_tree().call_group("selectable", "doSelect")
else:
get_tree().call_group("selectable", "select_one", parent)
get_tree().call_group("draggable", "startResizing")
egs.selectionController.interrupt()
#g.undo.add({"Type": "Resize", "Who": parent, "Rect": parent.get_rect()})
if event as InputEventMouseButton:
if event.button_index == 1 and !event.is_pressed():
egs.selectionController.interrupt() #interrupt the selection controller because we're actually resizing, not selecting
#on Mouse Up...
else:
get_tree().call_group("draggable", "endResizing")

#startResizing, endResizing, startMove, and endMove are all called via get_tree().call_group() from elsewhere
func startResizing():
if parent.is_in_group("selected"):
parent.emit_signal("startMoveResize", parent.get_rect())
Expand All @@ -107,7 +119,8 @@ func startResizing():
func endResizing():
parent.emit_signal("endMoveResize")
resizing = false



func startMove():
if parent.is_in_group("selected"):
if canMoveX || canMoveY:
Expand All @@ -123,8 +136,7 @@ func _input(event):
if event is InputEventMouseMotion:
if resizing:
if canResizeX:
parent.rect_size.x = max(parent.rect_size.x+event.relative.x, minWidth)

parent.rect_size.x = max(parent.rect_size.x+event.relative.x, minWidth)
if canResizeY:
parent.rect_size.y = max(parent.rect_size.y+event.relative.y, minHeight)
grow_parent_as_needed()
Expand All @@ -140,7 +152,9 @@ func _input(event):
if parent.is_in_group("selected"):
parent.emit_signal("doRemove")


# As we move/resize, adjust the parent so that we are always completely inside our parent
# ie. push the parent's boundaries as needed
# p.s. parent = this component's parent, and par is technically the grandparent
func grow_parent_as_needed():
if !parent.get_parent().has_node("Draggable"):
return
Expand All @@ -160,7 +174,8 @@ func grow_parent_as_needed():
c.rect_global_position.y += (deltaY )
par.rect_global_position = newRect.position
par.rect_size = newRect.size


# Hide the children if we're too small
func validate_size():
if parent.rect_size.x < minWidth or parent.rect_size.y < minHeight:
becomeSmall()
Expand Down
20 changes: 13 additions & 7 deletions ExtraGui/Renameable.gd
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
extends Node
class_name Renameable, "edit_icon.png"

export (NodePath) var who
#this component allows you to rename it's parent (and change a corresponding label's text).
#on right-click, it creates a popup asking for text input

export (NodePath) var who #the label whose text will be changed
var popupScene = preload("renamePopup.tscn")
var popup

var parent
var parent #the control to be renamed

func _ready():
parent = get_parent()
who = get_node(who)
who.connect("gui_input", self, "gui_input")
who.connect("gui_input", self, "gui_input") #adding code for the Label's Gui_event
who.text = parent.name
parent.add_user_signal("doneRenaming")

# called from Draggable via by get_tree().call_group()
func start_renaming():
popup = popupScene.instance()
add_child(popup)
popup.popup()
var lineEdit = popup.get_node("NewName")
var lineEdit = popup.get_node("NewName")
lineEdit.connect("text_entered", self, "done_renaming")
lineEdit.connect("focus_exited", self, "done_renaming_focus_lost", [lineEdit])
lineEdit.text = who.text
Expand All @@ -27,21 +31,23 @@ func start_renaming():

func done_renaming(newName):
if parent.name != newName:
# You can add a "validate_name" function to the parent if you wish to make sure
# the name doesn't exist yet, or use some special criterea for validation
if !parent.has_method("validate_name") || parent.validate_name(newName):
parent.emit_signal("doneRenaming", who, parent.name)
parent.name = newName
who.text = newName
else:
popup.get_node("error").visible = true
return

if is_instance_valid(popup):
popup.queue_free()

func done_renaming_focus_lost(lineEdit):
done_renaming(lineEdit.text)
#if is_instance_valid(popup):
# popup.get_node("NewName").grab_click_focus()
done_renaming(lineEdit.text)

func gui_input(event):
#on right-click. Change this if you want a different action to trigger renaming
if event is InputEventMouseButton && event.button_index == 2:
start_renaming()
15 changes: 11 additions & 4 deletions ExtraGui/SaveController.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends Node
class_name SaveController, "save_icon.png"

# SaveController is a singleton that handles saving and loading

var saveFilePath
var saveFile = File.new()
var dialog: FileDialog
Expand All @@ -10,12 +12,14 @@ var autosaveFile = File.new()
var dialogCanvasLayer: CanvasLayer

func _ready():
#register this singleton in the ExtraGui singleton
if !egs.saveController:
egs.saveController = self
else:
queue_free()

#Init the dialogs
#Init the dialogs. Put them on a canavs layer so they float about all the other GUI
#resultDialog is an alert expresses how the save/open went, and if there are any error.
dialog = FileDialog.new()

dialogCanvasLayer = CanvasLayer.new()
Expand Down Expand Up @@ -49,9 +53,9 @@ func _input(event):
func doLoadFrom():
if resultDialog.is_connected("confirmed", self, "doLoadFrom"):
resultDialog.disconnect("confirmed", self, "doLoadFrom")

dialog.mode = FileDialog.MODE_OPEN_FILE
dialog.invalidate()
dialog.invalidate() #refresh the file system
dialog.popup()
if !dialog.is_connected("file_selected", self, "doLoad"):
dialog.connect("file_selected", self, "doLoad")
Expand All @@ -76,6 +80,7 @@ func doLoad(filepath:String = autosaveFilePath):
var errors = false
for d in data:
if !is_instance_valid( get_node_or_null( d.path_to_parent )):
#This should never happen, because we sort the nodes by heirarchy first
print("Error! trying to load a node who's parent doesn't exist yet")

var scene = load(d.scene)
Expand All @@ -92,6 +97,8 @@ func doLoad(filepath:String = autosaveFilePath):
child.rect_position = str2var("Vector2" + d.rect_position)
child.rect_size = str2var("Vector2" + d.rect_size)

#If you have other data to load, this is where you should do that

if errors:
resultDialog.dialog_text = "Some objects didn't have scenes to load. they have been replaced with Panels"
resultDialog.popup()
Expand Down Expand Up @@ -128,7 +135,7 @@ func doSave(filepath:String = autosaveFilePath):
return
filepath += ".json"
saveFilePath = filepath
saveFile.open(saveFilePath, File.READ)

var saveData: Array
var sortedSaveables = get_tree().get_nodes_in_group("saveable")
sortedSaveables.sort_custom(self, "SortByHeirarchy")
Expand Down
2 changes: 2 additions & 0 deletions ExtraGui/Saveable.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends Node
class_name Saveable, "save_icon.png"

#this control makes it's parent's data saveable. Override the getDataToSave function to save other data... you'll have to modify save controller too to tell it what to do with this data

var parent
func _ready():
parent = get_parent()
Expand Down
4 changes: 3 additions & 1 deletion ExtraGui/Scrollable.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends Control
class_name Scrollable, "move_icon.png"

# This component makes the parent a scrollbox, allowing you to scroll it's children, and zoom in and out

export var canScrollX = true
export var canScrollY = true
export var canZoomX = true
Expand Down Expand Up @@ -50,7 +52,7 @@ func _input(event):
if !is_visible_in_tree():
return

#Frame all draggables so you can see them
#Frame all draggables so you can see them (i.e. zoom out/in and scroll as needed so that the bounding box of all the draggales combines is equal to the screen size)
if Input.is_key_pressed(KEY_F):
var topleft
var bottomright
Expand Down
7 changes: 5 additions & 2 deletions ExtraGui/SelectBox.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
extends Control
class_name SelectBox,"select_icon.png"

# This class is instanced automatically by SelectionController as eeded. DO NOT USE MANUALLY

var start
var end
var color = Color.cornflower
var color = Color.cornflower #the color of the selectBox outline
func _input(event):
if event is InputEventMouseMotion:
end += event.relative
update()
#On mouse up

#On mouse up do the selection, deselcting-all first if neededl, and then delete this selectbox
if event is InputEventMouseButton && event.button_index==1 && !event.pressed:
if !Input.is_key_pressed(KEY_SHIFT):
if !egs.selectionController.interrupted:
Expand Down
13 changes: 8 additions & 5 deletions ExtraGui/Selectable.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extends Control
class_name Selectable, "select_icon.png"

# This component makes it's parent selectable

onready var parent = get_parent()
var selected = false

Expand All @@ -10,6 +12,10 @@ func _ready():
parent.connect("resized", self, "resize")
resize()

#resize this component to be the size of it's parent
func resize():
rect_size = parent.rect_size

func select_one(who):
if parent == who:
doSelect()
Expand All @@ -19,7 +25,7 @@ func select_one(who):
func drag_select(box: Rect2):
if !is_visible_in_tree():
return
var r = Rect2(parent.rect_global_position, parent.rect_size)
var r = Rect2(parent.rect_global_position, parent.rect_size)
if box.intersects(r) && !r.encloses(box) && !is_grandparent_selected():
doSelect()

Expand Down Expand Up @@ -52,7 +58,4 @@ func _draw():
sb.border_width_bottom = 2
sb.draw_center = false
draw_style_box(sb, Rect2(rect_position, rect_size))


func resize():
rect_size = parent.rect_size

11 changes: 3 additions & 8 deletions ExtraGui/SelectionController.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
extends Node
class_name SelectionController, "select_icon.png"

# SelectionController singleton. instance a selectBox when you click and drag

signal resizing
signal moving

var parent
var selectBox
export var color = Color.cornflower
var interrupted = false
onready var timer = Timer.new()
onready var timer = Timer.new() #used for interrupting the select box
var startPosition

func _ready():
Expand All @@ -21,14 +23,7 @@ func _ready():
egs.selectionController = self
else:
queue_free()

func startResizing():
pass

func endResizing():
pass


func _input(event):
if event is InputEventMouseButton && event.button_index == 1 && event.pressed:
timer.start(0.01)
Expand Down
Loading

0 comments on commit 66e414e

Please sign in to comment.