Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2D : Simple mouse drawing demo #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions 2d/simple_mouse_drawing/MouseDrawing.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extends Node2D

const line_width = 10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about coding conventions for gdscript, but constants should be named in CAPS.


var pos_track = 0
var pos0 = Vector2()
var pos1 = Vector2()

func _input(event):
if (event.type == InputEvent.MOUSE_BUTTON):
if (event.pressed):

var mpos = get_viewport().get_mouse_pos()
if (pos_track == 0):
pos0 = mpos
else:
# on second click, create the "line"
pos1 = mpos
gen_poly_line()
# XOR flips 1 to 0 and 0 to 1, for switching states
pos_track ^= 1


func gen_poly_line():
# create a new Polygon2D node.
var node = Polygon2D.new()

# do maths to produce a line shape (rectangle)
node.set_polygon(create_poly_coords_for_line(pos0, pos1, line_width))

# attach the node to this object, and therfore include it in the scene
add_child(node)

# basic way to keep track of the added lines
node.add_to_group("DrawLines")


func create_poly_coords_for_line(start, end, width):
# Creates 2 points each for start and end point of a line;
# these points make up the corners of the rectangle that is the line representation.

width = width / 2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here should be a short comment, what this line means.


# Take the difference between start and end, make it a unit vector (normalized),
# make it perpendicular (tangent), make it half the line width long (* width) , finally
# offset it by adding it to start or end.
var s1 = start + ((start - end).normalized().tangent() * width)
var s2 = start + ((start - end).normalized().tangent() * -width)
var e1 = end + ((end - start).normalized().tangent() * width)
var e2 = end + ((end - start).normalized().tangent() * -width)

# This array of 2D points will make up the path of the polygon
return Vector2Array([s1, s2, e1, e2])


func _ready():
set_process_input(true)
65 changes: 65 additions & 0 deletions 2d/simple_mouse_drawing/MouseDrawing.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[gd_scene load_steps=3 format=1]

[ext_resource path="res://MouseDrawing.gd" type="Script" id=1]

[sub_resource type="GDScript" id=1]

script/source = "extends Control

# this function is connected to a signal on the button node
func _on_Button_pressed():
for poly in get_tree().get_nodes_in_group(\"DrawLines\"):
poly.queue_free()

"

[node name="Node2D" type="Node2D"]

script/script = ExtResource( 1 )

[node name="GUI" type="Control" parent="."]

focus/ignore_mouse = false
focus/stop_mouse = true
size_flags/horizontal = 2
size_flags/vertical = 2
margin/left = 0.0
margin/top = 0.0
margin/right = 292.0
margin/bottom = 67.0
script/script = SubResource( 1 )

[node name="Label" type="Label" parent="GUI"]

focus/ignore_mouse = true
focus/stop_mouse = true
size_flags/horizontal = 2
size_flags/vertical = 0
margin/left = 0.0
margin/top = 0.0
margin/right = 276.0
margin/bottom = 24.0
text = "Click 2 places to create line between them."
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1

[node name="Button" type="Button" parent="GUI"]

focus/ignore_mouse = false
focus/stop_mouse = true
size_flags/horizontal = 2
size_flags/vertical = 2
margin/left = 7.0
margin/top = 25.0
margin/right = 99.0
margin/bottom = 55.0
toggle_mode = false
enabled_focus_mode = 2
shortcut = null
text = "Clear"
flat = false

[connection signal="pressed" from="GUI/Button" to="GUI" method="_on_Button_pressed"]


9 changes: 9 additions & 0 deletions 2d/simple_mouse_drawing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Simple Mouse Drawing Demo

Click 2 points to create a line between them.

Demonstrates:

* Procedural 2DPolygon generation and Node instancing.

* Mouse input handling.
16 changes: 16 additions & 0 deletions 2d/simple_mouse_drawing/engine.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[application]

name="SimpleMouseDrawing"
main_scene="res://MouseDrawing.tscn"
icon="res://icon.png"

[display]

width=1024
height=768
resizable=true
stretch_aspect="keep"

[physics_2d]

cell_size=64
Binary file added 2d/simple_mouse_drawing/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.