-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
extends Node2D | ||
|
||
const line_width = 10 | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.