Remove text input and type directly into the playfield

This commit is contained in:
Terry Hearst 2022-05-13 17:11:04 -04:00
parent c687a4e6af
commit 88bb18eecd
3 changed files with 59 additions and 51 deletions

View file

@ -16,6 +16,7 @@ var letters := []
var target_word: String var target_word: String
var current_guess: int var current_guess: int
var ended: bool var ended: bool
var input_guess: String
onready var error_text_color_default = $C/V/V/ErrorText.get("custom_colors/font_color") onready var error_text_color_default = $C/V/V/ErrorText.get("custom_colors/font_color")
@ -45,27 +46,53 @@ func _ready() -> void:
current_guess = 0 current_guess = 0
ended = false ended = false
$C/V/V/H/GuessText.grab_focus()
func type_letter(letter: String) -> void:
if ended or input_guess.length() >= 5:
return
var index := input_guess.length()
letter = letter[0]
input_guess += letter
var letter_instance := letters[current_guess][index] as ColorRect
letter_instance.get_node("Label").text = letter
func backspace() -> void:
if ended or input_guess.length() <= 0:
return
input_guess = input_guess.substr(0, input_guess.length() - 1)
var index := input_guess.length()
var letter_instance := letters[current_guess][index] as ColorRect
letter_instance.get_node("Label").text = ""
func _unhandled_key_input(event: InputEventKey) -> void:
if event.is_pressed():
var scancode = event.get_scancode()
if scancode >= KEY_A and scancode <= KEY_Z:
var letter := OS.get_scancode_string(scancode)
type_letter(letter)
get_tree().set_input_as_handled()
elif scancode == KEY_BACKSPACE:
backspace()
get_tree().set_input_as_handled()
elif scancode == KEY_ENTER:
guess_entered()
get_tree().set_input_as_handled()
func _on_GuessButton_pressed() -> void: func _on_GuessButton_pressed() -> void:
var guess_text_node := find_node("GuessText") as LineEdit guess_entered()
var guess := guess_text_node.text.to_upper()
guess_entered(guess)
func _on_GuessText_text_entered(new_text: String) -> void: func guess_entered() -> void:
guess_entered(new_text)
func guess_entered(guess: String) -> void:
if current_guess >= guess_count: if current_guess >= guess_count:
return return
guess = guess.to_upper() input_guess = input_guess.to_upper()
if guess.length() != letter_count: if input_guess.length() != letter_count:
show_error("Word must be five characters.") show_error("Word must be five characters.")
return return
if not Global.is_valid_word(guess): if not Global.is_valid_word(input_guess):
show_error("Not a recognized word.") show_error("Not a recognized word.")
return return
@ -77,7 +104,7 @@ func guess_entered(guess: String) -> void:
# Mark all greens # Mark all greens
for i in range(letter_count): for i in range(letter_count):
var guess_letter := guess[i] var guess_letter := input_guess[i]
var target_letter := target_word[i] var target_letter := target_word[i]
var letter_instance := letters[current_guess][i] as ColorRect var letter_instance := letters[current_guess][i] as ColorRect
@ -96,7 +123,7 @@ func guess_entered(guess: String) -> void:
if letter_instance.color == color_correct: if letter_instance.color == color_correct:
# Ignore things that are already green # Ignore things that are already green
continue continue
var guess_letter := guess[i] var guess_letter := input_guess[i]
var found := false var found := false
for j in range(letters_remaining.size()): for j in range(letters_remaining.size()):
if letters_remaining[j] == guess_letter: if letters_remaining[j] == guess_letter:
@ -109,20 +136,19 @@ func guess_entered(guess: String) -> void:
letter_instance.color = color_incorrect letter_instance.color = color_incorrect
current_guess += 1 current_guess += 1
var guess_text_node := find_node("GuessText") as LineEdit
guess_text_node.text = ""
var won := false var won := false
if guess == target_word: if input_guess == target_word:
ended = true ended = true
won = true won = true
elif current_guess >= guess_count: elif current_guess >= guess_count:
ended = true ended = true
input_guess = ""
if ended: if ended:
find_node("GuessButton").disabled = true find_node("GuessButton").disabled = true
find_node("GuessText").editable = false
if won: if won:
show_error("Congrats! You won!", Color(0.4, 0.9, 0.6)) show_error("Congrats! You won!", Color(0.4, 0.9, 0.6))
else: else:

View file

@ -49,26 +49,26 @@ __meta__ = {
} }
[node name="V" type="VBoxContainer" parent="C"] [node name="V" type="VBoxContainer" parent="C"]
margin_left = 70.0 margin_left = 209.0
margin_top = 377.0 margin_top = 379.0
margin_right = 570.0 margin_right = 430.0
margin_bottom = 582.0 margin_bottom = 580.0
custom_constants/separation = 20 custom_constants/separation = 20
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="Title" type="Label" parent="C/V"] [node name="Title" type="Label" parent="C/V"]
margin_right = 500.0 margin_right = 221.0
margin_bottom = 82.0 margin_bottom = 82.0
custom_fonts/font = SubResource( 1 ) custom_fonts/font = SubResource( 1 )
text = "Gordle" text = "Gordle"
align = 1 align = 1
[node name="LetterGrid" type="GridContainer" parent="C/V"] [node name="LetterGrid" type="GridContainer" parent="C/V"]
margin_left = 250.0 margin_left = 110.0
margin_top = 102.0 margin_top = 102.0
margin_right = 250.0 margin_right = 110.0
margin_bottom = 102.0 margin_bottom = 102.0
grow_horizontal = 2 grow_horizontal = 2
size_flags_horizontal = 4 size_flags_horizontal = 4
@ -78,36 +78,22 @@ columns = 5
[node name="V" type="VBoxContainer" parent="C/V"] [node name="V" type="VBoxContainer" parent="C/V"]
margin_top = 122.0 margin_top = 122.0
margin_right = 500.0 margin_right = 221.0
margin_bottom = 205.0 margin_bottom = 201.0
[node name="ErrorText" type="Label" parent="C/V/V"] [node name="ErrorText" type="Label" parent="C/V/V"]
margin_right = 500.0 margin_right = 221.0
margin_bottom = 28.0 margin_bottom = 28.0
custom_colors/font_color = Color( 1, 0.31, 0.31, 1 ) custom_colors/font_color = Color( 1, 0.31, 0.31, 1 )
custom_fonts/font = SubResource( 2 ) custom_fonts/font = SubResource( 2 )
align = 1 align = 1
[node name="H" type="HBoxContainer" parent="C/V/V"] [node name="GuessButton" type="Button" parent="C/V/V"]
margin_left = 53.0
margin_top = 32.0 margin_top = 32.0
margin_right = 500.0 margin_right = 167.0
margin_bottom = 83.0 margin_bottom = 79.0
rect_min_size = Vector2( 500, 0 )
size_flags_horizontal = 4 size_flags_horizontal = 4
custom_constants/separation = 12
[node name="GuessText" type="LineEdit" parent="C/V/V/H"]
margin_right = 374.0
margin_bottom = 51.0
size_flags_horizontal = 3
max_length = 5
caret_blink = true
caret_blink_speed = 0.5
[node name="GuessButton" type="Button" parent="C/V/V/H"]
margin_left = 386.0
margin_right = 500.0
margin_bottom = 51.0
text = "Guess" text = "Guess"
[node name="MenuButton" type="Button" parent="."] [node name="MenuButton" type="Button" parent="."]
@ -126,6 +112,5 @@ __meta__ = {
[node name="ErrorFadeOut" type="AnimationPlayer" parent="."] [node name="ErrorFadeOut" type="AnimationPlayer" parent="."]
anims/ErrorFadeOut = SubResource( 3 ) anims/ErrorFadeOut = SubResource( 3 )
[connection signal="text_entered" from="C/V/V/H/GuessText" to="." method="_on_GuessText_text_entered"] [connection signal="pressed" from="C/V/V/GuessButton" to="." method="_on_GuessButton_pressed"]
[connection signal="pressed" from="C/V/V/H/GuessButton" to="." method="_on_GuessButton_pressed"]
[connection signal="pressed" from="MenuButton" to="." method="_on_MenuButton_pressed"] [connection signal="pressed" from="MenuButton" to="." method="_on_MenuButton_pressed"]

View file

@ -18,9 +18,6 @@ font_data = ExtResource( 1 )
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
script = ExtResource( 2 ) script = ExtResource( 2 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="C" type="CenterContainer" parent="."] [node name="C" type="CenterContainer" parent="."]
anchor_right = 1.0 anchor_right = 1.0