Skip to content

Commit

Permalink
Converted the 2D sketch preview to a drawn 2D node instead of using SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwright committed Jun 1, 2021
1 parent dcfe0a9 commit c36ffbc
Show file tree
Hide file tree
Showing 15 changed files with 207 additions and 115 deletions.
100 changes: 60 additions & 40 deletions ActionPopupPanel.gd
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func _ready():
action_tree_root = action_tree.create_item()
action_tree_root.set_text(0, "Actions")

# The sketch control does not need to be taking up space by default
$VBoxContainer/HBoxContainer/CanvasMarginContainer.hide()


"""
Sets the Action control based on what is selected in the option button.
Expand Down Expand Up @@ -294,23 +297,6 @@ func _on_VBoxContainer_resized():
hide()


"""
Allows an image to be loaded into the 2D preview.
"""
func _load_image(path, globalize):
var texture = ImageTexture.new()
var image = Image.new()

# Static images will not be exported correctly unless the path is globalized
if globalize:
image.load(ProjectSettings.globalize_path(path))
else:
image.load(path)

texture.create_from_image(image)
$VBoxContainer/HBoxContainer/Preview.set_texture(texture)


"""
Called when the user clicks the 3D button and toggles it.
"""
Expand Down Expand Up @@ -368,8 +354,7 @@ func _on_SketchButton_toggled(_button_pressed):
# Show preview controls
$VBoxContainer/HBoxContainer/ActionContainer/ActionButtonContainer/AddButton.show()
$VBoxContainer/HBoxContainer/ActionContainer/ActionTree.show()
$VBoxContainer/HBoxContainer/Preview.show()
_load_image("res://assets/samples/sample_2D_render.svg", true)
$VBoxContainer/HBoxContainer/CanvasMarginContainer.show()

# Make sure the dialog is sized correctly
_on_VBoxContainer_resized()
Expand Down Expand Up @@ -440,7 +425,7 @@ any previously displayed sketch controls.
func _hide_sketch_controls():
$VBoxContainer/HBoxContainer/ActionContainer/ActionButtonContainer/AddButton.hide()
$VBoxContainer/HBoxContainer/ActionContainer/ActionTree.hide()
$VBoxContainer/HBoxContainer/Preview.hide()
$VBoxContainer/HBoxContainer/CanvasMarginContainer.hide()
$VBoxContainer/HBoxContainer/ActionContainer/ActionButtonContainer/ItemSelectedContainer.hide()


Expand Down Expand Up @@ -476,7 +461,7 @@ func _on_AddButton_button_down():

"""
Collects all of the completed templates in the Action tree and
renders them as an SVG image.
renders them on the 2D canvas.
"""
func _render_action_tree():
# Start to build the preview string based on what is in the actions list
Expand All @@ -494,22 +479,28 @@ func _render_action_tree():

script_text += ".consolidateWires()\nshow_object(result)"

# Protect against edge cases that will result in misleading errors
var rgx = RegEx.new()
rgx.compile("\\.pushPoints\\(\\[.*\\]\\)\\.consolidateWires")
var res = rgx.search(script_text)
if res:
return

# Export the file to the user data directory temporarily
var ret = cqgipy.export(script_text, "svg", OS.get_user_data_dir(), "width:400;height:400;marginLeft:50;marginTop:50;showAxes:False;projectionDir:(0,0,1);strokeWidth:0.5;strokeColor:(255,255,255);hiddenColor:(0,0,255);showHidden:False;")
var json_string = cqgipy.build(script_text)

if ret.begins_with("error~"):
if json_string.begins_with("error~"):
# Let the user know there was an error
var err = ret.split("~")[1]
var err = json_string.split("~")[1]
emit_signal("error", err)
else:
_load_image(ret, false)
var component_json = JSON.parse(json_string).result

for component in component_json["components"]:
# If we've found a larger dimension, save the safe scaling, which is the maximum dimension of any component
var max_dim = component["largestDim"]
$VBoxContainer/HBoxContainer/CanvasMarginContainer/Canvas2D.set_max_dim(max_dim)

# Add the edge representations
for edge in component["cqEdges"]:
# Add the current line
$VBoxContainer/HBoxContainer/CanvasMarginContainer/Canvas2D.lines.append([Vector2(edge[0], edge[1]), Vector2(edge[3], edge[4])])

# Have the 2D canvas re-render the lines that are set for it
$VBoxContainer/HBoxContainer/CanvasMarginContainer/Canvas2D.update()


"""
Expand All @@ -521,9 +512,9 @@ func _on_ActionTree_item_activated():
var action_key = item_text.split(".")[1].split("(")[0]

# Make sure the correct item is selected
Common.set_option_btn_by_text($VBoxContainer/ActionOptionButton, action_key)
# Common.set_option_btn_by_text($VBoxContainer/ActionOptionButton, action_key)

_set_action_control()
# _set_action_control()


"""
Expand All @@ -540,13 +531,33 @@ func _on_UpdateButton_button_down():
Common.update_tree_item(action_tree, orig_text, new_text)

# Re-render everything in the action tree
_render_action_tree()
_update_preview()


"""
Called when an item is selected in the Action tree.
"""
func _on_ActionTree_item_selected():
var selected = action_tree.get_selected()

# Make sure there is an item to work with
if selected == null:
return

# Figure out which control needs to be loaded from the operations list
var selected_text = selected.get_text(0)
var ctrl_text = "(" + selected_text.split(".")[1].split("(")[0] + ")"

# Set the control in the operations drop down based on our partial text
Common.set_option_btn_by_partial_text($VBoxContainer/ActionOptionButton, ctrl_text)

# Make sure the matching control is loaded
_on_ActionOptionButton_item_selected(0)

# Repopulate the control with the previous settings
var cur_control = $VBoxContainer/HBoxContainer/ActionContainer/DynamicContainer.get_children()[0]
cur_control.set_values_from_string(selected_text)

# Unhide the item editing controls so the user can change the selected tree item
$VBoxContainer/HBoxContainer/ActionContainer/ActionButtonContainer/ItemSelectedContainer.show()

Expand All @@ -570,14 +581,14 @@ func _on_DeleteButton_button_down():
return

# Remove the item from the history tree
action_tree_root.remove_child(selected)
selected.free()

# Make sure there is something left to render
if action_tree_root.get_children() == null:
_load_image("res://assets/samples/sample_2D_render.svg", true)
else:
self._render_action_tree()
# Force an update of the tree
action_tree.update()

# Updated the 2D preview
_update_preview()

"""
Called when the move action item up button is pressed.
Expand Down Expand Up @@ -607,6 +618,15 @@ func _on_MoveDownButton_button_down():
Common.move_tree_item_down(action_tree, selected)


"""
Called whenever the 2D preview needs to be updated.
"""
func _update_preview():
# Reset and update the 2D preview
$VBoxContainer/HBoxContainer/CanvasMarginContainer/Canvas2D.reset()
$VBoxContainer/HBoxContainer/CanvasMarginContainer/Canvas2D.update()
self._render_action_tree()

"""
Called when the control loaded in the dynamic contrainer
is changed.
Expand Down
45 changes: 45 additions & 0 deletions Canvas2D.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
extends Node2D

var lines = [] # Array of line start-end points to use to draw the line segments
var line_width = 5 # Line width can be adjusted for the size of the object
var max_dim = 1 # Largest dimension of the object being rendered


"""
Called when the node is asked to redraw its contents.
"""
func _draw():
var par_size = get_parent().rect_size
var par_loc = get_parent().rect_position

# Draw all the lines that have been stored for the canvas
for line in lines:
var l0x = (line[0].x / (max_dim * 2)) * 550
var l1x = (line[1].x / (max_dim * 2)) * 550
var l0y = (line[0].y / (max_dim * 2)) * -550
var l1y = (line[1].y / (max_dim * 2)) * -550

# Line start and end points
var v1 = Vector2(l0x + par_loc.x + par_size.x / 4.0, l0y + par_loc.y + par_size.y / 2.0)
var v2 = Vector2(l1x + par_loc.x + par_size.x / 4.0, l1y + par_loc.y + par_size.y / 2.0)

# Draw the line
self.draw_line(v1, v2, Color(255, 255, 255), line_width)


"""
Used to set the maximum dimension for the sketch.
"""
func set_max_dim(dim):
# Only save this if it is bigger than what we had before
if dim > max_dim:
max_dim = dim


"""
Resets the 2D sketch canvas back to the default values
"""
func reset():
self.lines = [] # Array of line start-end points to use to draw the line segments
self.line_width = 5 # Line width can be adjusted for the size of the object
self.max_dim = 1 # Largest dimension of the object being rendered
2 changes: 1 addition & 1 deletion ExportDXFDialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func _on_OkButton_button_down():
for i in range(0, steps):
var file_name = pt.get_text().replace(".dxf", "_" + str(i) + ".dxf")
_export_to_file(file_name, cur_height)
print(str(cur_height) + " : " + str(layer_height))

cur_height += layer_height

# Do a final export right at the end height
Expand Down
34 changes: 18 additions & 16 deletions GUI.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=65 format=2]
[gd_scene load_steps=66 format=2]

[ext_resource path="res://assets/icons/folder_button_flat_ready.svg" type="Texture" id=1]
[ext_resource path="res://GUI.gd" type="Script" id=2]
Expand Down Expand Up @@ -31,6 +31,7 @@
[ext_resource path="res://AddParameterDialog.gd" type="Script" id=29]
[ext_resource path="res://controls/NumberEdit.gd" type="Script" id=30]
[ext_resource path="res://ExportDXFDialog.gd" type="Script" id=31]
[ext_resource path="res://Canvas2D.gd" type="Script" id=32]

[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0.145098, 0.145098, 0.164706, 1 )
Expand Down Expand Up @@ -488,11 +489,11 @@ size_flags_horizontal = 5
size_flags_vertical = 5

[node name="ActionGroupsVBoxContainer" type="VBoxContainer" parent="ActionPopupPanel/VBoxContainer"]
margin_right = 208.0
margin_right = 274.0
margin_bottom = 43.0

[node name="HBoxContainer" type="HBoxContainer" parent="ActionPopupPanel/VBoxContainer/ActionGroupsVBoxContainer"]
margin_right = 208.0
margin_right = 274.0
margin_bottom = 43.0

[node name="WorkplaneButton" type="Button" parent="ActionPopupPanel/VBoxContainer/ActionGroupsVBoxContainer/HBoxContainer"]
Expand Down Expand Up @@ -524,23 +525,23 @@ icon = ExtResource( 23 )

[node name="HSeparator3" type="HSeparator" parent="ActionPopupPanel/VBoxContainer"]
margin_top = 47.0
margin_right = 208.0
margin_right = 274.0
margin_bottom = 51.0

[node name="ActionOptionButton" type="OptionButton" parent="ActionPopupPanel/VBoxContainer"]
margin_top = 55.0
margin_right = 208.0
margin_right = 274.0
margin_bottom = 75.0

[node name="HSeparator" type="HSeparator" parent="ActionPopupPanel/VBoxContainer"]
margin_top = 79.0
margin_right = 208.0
margin_right = 274.0
margin_bottom = 83.0
custom_styles/separator = SubResource( 28 )

[node name="HBoxContainer" type="HBoxContainer" parent="ActionPopupPanel/VBoxContainer"]
margin_top = 87.0
margin_right = 208.0
margin_right = 274.0
margin_bottom = 319.0

[node name="ActionContainer" type="VBoxContainer" parent="ActionPopupPanel/VBoxContainer/HBoxContainer"]
Expand Down Expand Up @@ -595,29 +596,30 @@ margin_bottom = 232.0
rect_min_size = Vector2( 100, 200 )
hide_root = true

[node name="Preview" type="TextureRect" parent="ActionPopupPanel/VBoxContainer/HBoxContainer"]
margin_left = 170.0
margin_right = 170.0
[node name="CanvasMarginContainer" type="MarginContainer" parent="ActionPopupPanel/VBoxContainer/HBoxContainer"]
margin_left = 174.0
margin_right = 274.0
margin_bottom = 232.0
__meta__ = {
"_edit_use_anchors_": false
}
rect_min_size = Vector2( 600, 600 )

[node name="Canvas2D" type="Node2D" parent="ActionPopupPanel/VBoxContainer/HBoxContainer/CanvasMarginContainer"]
script = ExtResource( 32 )

[node name="HSeparator2" type="HSeparator" parent="ActionPopupPanel/VBoxContainer"]
margin_top = 323.0
margin_right = 208.0
margin_right = 274.0
margin_bottom = 327.0
custom_styles/separator = SubResource( 29 )

[node name="OkButton" type="Button" parent="ActionPopupPanel/VBoxContainer"]
margin_top = 331.0
margin_right = 208.0
margin_right = 274.0
margin_bottom = 351.0
text = "OK"

[node name="CancelButton" type="Button" parent="ActionPopupPanel/VBoxContainer"]
margin_top = 355.0
margin_right = 208.0
margin_right = 274.0
margin_bottom = 375.0
text = "Cancel"
__meta__ = {
Expand Down
Loading

0 comments on commit c36ffbc

Please sign in to comment.