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

Added Timer GUI element #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions entities/ui/Timer.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
extends Node2D

# Time formatting stolen from
# https://godotengine.org/qa/32785/is-there-simple-way-to-convert-seconds-to-hh-mm-ss-format-godot

enum TimeFormat {
FORMAT_HOURS = 1 << 0,
FORMAT_MINUTES = 1 << 1,
FORMAT_SECONDS = 1 << 2,
}

func format_time(time, format, digit_format = "%02d"):
var digits = []

if format & TimeFormat.FORMAT_HOURS:
var hours = digit_format % [time / 3600]
digits.append(hours)

if format & TimeFormat.FORMAT_MINUTES:
var minutes = digit_format % [time / 60]
digits.append(minutes)

if format & TimeFormat.FORMAT_SECONDS:
var seconds = digit_format % [int(ceil(time)) % 60]
digits.append(seconds)

var formatted = String()
var colon = " : "

for digit in digits:
formatted += digit + colon

if not formatted.empty():
formatted = formatted.rstrip(colon)

return formatted

var time_elapsed = 0

func _on_Timer_timeout():
time_elapsed += 1
$Label.text = format_time(time_elapsed, TimeFormat.FORMAT_MINUTES | TimeFormat.FORMAT_SECONDS)
21 changes: 21 additions & 0 deletions entities/ui/Timer.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://entities/ui/Timer.gd" type="Script" id=1]

[node name="Node2D" type="Node2D"]
script = ExtResource( 1 )

[node name="Timer" type="Timer" parent="."]
autostart = true

[node name="Label" type="Label" parent="."]
margin_left = 184.887
margin_top = 114.274
margin_right = 224.887
margin_bottom = 128.274
text = "0"
__meta__ = {
"_edit_use_anchors_": false
}

[connection signal="timeout" from="Timer" to="." method="_on_Timer_timeout"]