bendn 2023-04-22
commit c407769
-rw-r--r--.github/image.pngbin0 -> 10658 bytes
-rw-r--r--.gitignore1
-rw-r--r--README.md17
-rw-r--r--addons/@bendn/timecontrol/README.md16
-rw-r--r--addons/@bendn/timecontrol/hour.gd8
-rw-r--r--addons/@bendn/timecontrol/minute.gd8
-rw-r--r--addons/@bendn/timecontrol/package.json23
-rw-r--r--addons/@bendn/timecontrol/scroll.gd62
-rw-r--r--addons/@bendn/timecontrol/timebox.gd37
-rw-r--r--addons/@bendn/timecontrol/timebox.tscn28
-rw-r--r--addons/@bendn/timecontrol/timebutton.gd35
-rw-r--r--dots.pngbin0 -> 152 bytes
-rw-r--r--dots.png.import34
-rw-r--r--project.godot13
-rw-r--r--test.tscn37
-rw-r--r--them.tres17
16 files changed, 336 insertions, 0 deletions
diff --git a/.github/image.png b/.github/image.png
new file mode 100644
index 0000000..4c61575
--- /dev/null
+++ b/.github/image.png
Binary files differ
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..16293bd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+.godot
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..74a2560
--- /dev/null
+++ b/README.md
@@ -0,0 +1,17 @@
+# timecontrol
+
+[![version](https://img.shields.io/badge/4.x-blue?logo=godot-engine&logoColor=white&label=godot&style=for-the-badge)](https://godotengine.org "Made with godot")
+[![package](https://img.shields.io/npm/v/@bendn/timecontrol?label=version&style=for-the-badge)](https://www.npmjs.com/package/@bendn/timecontrol)
+<a href='https://ko-fi.com/bendn' title='Buy me a coffee' target='_blank'><img height='28' src='https://storage.ko-fi.com/cdn/brandasset/kofi_button_red.png' alt='Buy me a coffee'> </a>
+
+time is a illusion
+
+[![image](https://raw.githubusercontent.com/bend-n/timecontrol/main/.github/image.png)](_blank "Picture!")
+
+## Usage
+
+Instanstiate the `timebox.tscn` scene or add a `timebutton.gd`.
+
+## Installation
+
+See [gpm#using-packages](https://github.com/godot-package-manager#using-packages-quickstart).
diff --git a/addons/@bendn/timecontrol/README.md b/addons/@bendn/timecontrol/README.md
new file mode 100644
index 0000000..cf8f595
--- /dev/null
+++ b/addons/@bendn/timecontrol/README.md
@@ -0,0 +1,16 @@
+# timecontrol
+
+[![package](https://img.shields.io/npm/v/@bendn/timecontrol?label=version&style=for-the-badge)](https://www.npmjs.com/package/@bendn/timecontrol)
+<a href='https://ko-fi.com/bendn' title='Buy me a coffee' target='_blank'><img height='28' src='https://storage.ko-fi.com/cdn/brandasset/kofi_button_red.png' alt='Buy me a coffee'> </a>
+
+time is a illusion
+
+[![image](https://raw.githubusercontent.com/bend-n/timecontrol/main/.github/image.png)](_blank "Picture!")
+
+## Usage
+
+Instanstiate the `timebox.tscn` scene or add a `timebutton.gd`.
+
+## Installation
+
+See [gpm#using-packages](https://github.com/godot-package-manager#using-packages-quickstart).
diff --git a/addons/@bendn/timecontrol/hour.gd b/addons/@bendn/timecontrol/hour.gd
new file mode 100644
index 0000000..4e148b1
--- /dev/null
+++ b/addons/@bendn/timecontrol/hour.gd
@@ -0,0 +1,8 @@
+@tool
+extends Scroll
+
+func make_items() -> Array[String]:
+ var i: Array[String] = []
+ for n in range(0, 24):
+ i.append("%02d" % n)
+ return i
diff --git a/addons/@bendn/timecontrol/minute.gd b/addons/@bendn/timecontrol/minute.gd
new file mode 100644
index 0000000..6fd8636
--- /dev/null
+++ b/addons/@bendn/timecontrol/minute.gd
@@ -0,0 +1,8 @@
+@tool
+extends Scroll
+
+func make_items() -> Array[String]:
+ var i: Array[String] = []
+ for n in range(0, 61):
+ i.append("%02d" % n)
+ return i
diff --git a/addons/@bendn/timecontrol/package.json b/addons/@bendn/timecontrol/package.json
new file mode 100644
index 0000000..019b9cb
--- /dev/null
+++ b/addons/@bendn/timecontrol/package.json
@@ -0,0 +1,23 @@
+{
+ "name": "@bendn/timecontrol",
+ "version": "1.0.1",
+ "description": "adds a control node for selecting times",
+ "main": "timebox.gd",
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/bend-n/timecontrol.git"
+ },
+ "keywords": [
+ "godot",
+ "godot-engine",
+ "time",
+ "picker",
+ "ui"
+ ],
+ "author": "bendn",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/bend-n/timecontrol/issues"
+ },
+ "homepage": "https://github.com/bend-n/timecontrol#readme"
+} \ No newline at end of file
diff --git a/addons/@bendn/timecontrol/scroll.gd b/addons/@bendn/timecontrol/scroll.gd
new file mode 100644
index 0000000..8e9ac94
--- /dev/null
+++ b/addons/@bendn/timecontrol/scroll.gd
@@ -0,0 +1,62 @@
+@tool
+extends VBoxContainer
+class_name Scroll
+
+@export var label_color: Color = Color("#eee8d5")
+@export var label_color_unselected: Color = Color("#93a1a1")
+@export var lookahead := 1:
+ set(lok):
+ if lookahead != lok:
+ lookahead = lok
+ make_children()
+
+@onready var items: Array[String] = make_items()
+
+signal selection_changed(item: int)
+var children: Array[Label]
+
+@export var selection: int:
+ set(sel):
+ if selection != sel:
+ selection = sel
+ make_children()
+ selection_changed.emit(selection)
+
+func make_children():
+ if not is_inside_tree():
+ return
+ for child in children:
+ child.queue_free()
+ children.clear()
+ for n in range(lookahead, 0, -1):
+ make_label(items[selection + -n])
+ make_label(items[selection], true)
+ for n in lookahead:
+ make_label(items[wrapi(selection + n + 1, 0, len(items))])
+
+func _ready() -> void:
+ make_children()
+ var font := get_theme_default_font()
+ for item in items:
+ var string_size := font.get_string_size(item).x
+ if custom_minimum_size.x < string_size:
+ custom_minimum_size.x = string_size
+
+## overridable
+func make_label(text: String, selected: bool = false) -> void:
+ var label := Label.new()
+ label.text = text
+ label.name = text
+ label.add_theme_color_override("font_color", label_color if selected else label_color_unselected)
+ add_child(label)
+ children.append(label)
+
+## virtual
+func make_items() -> Array[String]:
+ return []
+
+func _gui_input(event: InputEvent) -> void:
+ if event is InputEventMouseButton and event.is_pressed():
+ match event.button_index:
+ MOUSE_BUTTON_WHEEL_UP: selection = wrapi(selection - 1, 0, len(items))
+ MOUSE_BUTTON_WHEEL_DOWN: selection = wrapi(selection + 1, 0, len(items))
diff --git a/addons/@bendn/timecontrol/timebox.gd b/addons/@bendn/timecontrol/timebox.gd
new file mode 100644
index 0000000..c2db907
--- /dev/null
+++ b/addons/@bendn/timecontrol/timebox.gd
@@ -0,0 +1,37 @@
+extends HBoxContainer
+class_name TimeBox
+
+var time := {
+ "hour": 12,
+ "minute": 00,
+}
+
+func _changed():
+ time_changed.emit()
+
+
+signal time_changed(time: Dictionary)
+
+func _on_hour_selection_changed(hour: int) -> void:
+ time.hour = hour
+ _changed()
+
+func _on_minute_selection_changed(minute: int) -> void:
+ time.minute = minute
+ _changed()
+
+func fmt_24h() -> String:
+ return TimeBox.fmt_dict_24h(time)
+
+func fmt_12h() -> String:
+ return TimeBox.fmt_dict_12h(time)
+
+static func fmt_dict_12h(dict: Dictionary) -> String:
+ return "%02d:%02d %s" % [
+ (dict.hour + 11) % 12 + 1,
+ dict.minute,
+ "PM" if dict.hour >= 12 else "AM"
+ ]
+
+static func fmt_dict_24h(dict: Dictionary) -> String:
+ return "%02d:%02d" % [dict.hour, dict.minute]
diff --git a/addons/@bendn/timecontrol/timebox.tscn b/addons/@bendn/timecontrol/timebox.tscn
new file mode 100644
index 0000000..a96ee37
--- /dev/null
+++ b/addons/@bendn/timecontrol/timebox.tscn
@@ -0,0 +1,28 @@
+[gd_scene load_steps=4 format=3 uid="uid://drw0w44wonai8"]
+
+[ext_resource type="Script" path="res://addons/@bendn/timecontrol/timebox.gd" id="1_x0q77"]
+[ext_resource type="Script" path="res://addons/@bendn/timecontrol/hour.gd" id="2_chwoo"]
+[ext_resource type="Script" path="res://addons/@bendn/timecontrol/minute.gd" id="3_2xfs8"]
+
+[node name="timebox" type="HBoxContainer"]
+script = ExtResource("1_x0q77")
+
+[node name="hour" type="VBoxContainer" parent="."]
+custom_minimum_size = Vector2(19, 0)
+layout_mode = 2
+script = ExtResource("2_chwoo")
+selection = 12
+
+[node name="sep" type="VSeparator" parent="."]
+layout_mode = 2
+
+[node name="minute" type="VBoxContainer" parent="."]
+custom_minimum_size = Vector2(19, 0)
+layout_mode = 2
+script = ExtResource("3_2xfs8")
+
+[node name="Label" type="Label" parent="."]
+layout_mode = 2
+
+[connection signal="selection_changed" from="hour" to="." method="_on_hour_selection_changed"]
+[connection signal="selection_changed" from="minute" to="." method="_on_minute_selection_changed"]
diff --git a/addons/@bendn/timecontrol/timebutton.gd b/addons/@bendn/timecontrol/timebutton.gd
new file mode 100644
index 0000000..b188e45
--- /dev/null
+++ b/addons/@bendn/timecontrol/timebutton.gd
@@ -0,0 +1,35 @@
+@tool
+extends Button
+
+@export var time := {
+ "hour": 12,
+ "minute": 00,
+}: set = set_time
+
+var timebox: TimeBox
+var panel: PopupPanel
+
+func set_time(t: Dictionary = time) -> void:
+ text = TimeBox.fmt_dict_12h(time)
+ time = t
+
+## this is a required internal object
+func get_timebox() -> TimeBox:
+ return timebox
+
+func _ready() -> void:
+ text = TimeBox.fmt_dict_12h(time)
+ if Engine.is_editor_hint():
+ return
+ panel = PopupPanel.new()
+ timebox = preload("./timebox.tscn").instantiate()
+ timebox.time = time
+ timebox.time_changed.connect(set_time)
+ panel.add_child(timebox)
+ add_child(panel)
+ panel.hide()
+
+func _pressed() -> void:
+ panel.size = Vector2()
+ panel.position = global_position
+ panel.popup()
diff --git a/dots.png b/dots.png
new file mode 100644
index 0000000..08b86a1
--- /dev/null
+++ b/dots.png
Binary files differ
diff --git a/dots.png.import b/dots.png.import
new file mode 100644
index 0000000..ccfa347
--- /dev/null
+++ b/dots.png.import
@@ -0,0 +1,34 @@
+[remap]
+
+importer="texture"
+type="CompressedTexture2D"
+uid="uid://cgv1h1nr73h70"
+path="res://.godot/imported/dots.png-503527815ff6050c9a751263768a95eb.ctex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://dots.png"
+dest_files=["res://.godot/imported/dots.png-503527815ff6050c9a751263768a95eb.ctex"]
+
+[params]
+
+compress/mode=0
+compress/high_quality=false
+compress/lossy_quality=0.7
+compress/hdr_compression=1
+compress/normal_map=0
+compress/channel_pack=0
+mipmaps/generate=false
+mipmaps/limit=-1
+roughness/mode=0
+roughness/src_normal=""
+process/fix_alpha_border=true
+process/premult_alpha=false
+process/normal_map_invert_y=false
+process/hdr_as_srgb=false
+process/hdr_clamp_exposure=false
+process/size_limit=0
+detect_3d/compress_to=1
diff --git a/project.godot b/project.godot
new file mode 100644
index 0000000..4c0cac9
--- /dev/null
+++ b/project.godot
@@ -0,0 +1,13 @@
+; Engine configuration file.
+; It's best edited using the editor UI and not directly,
+; since the parameters that go here are not all obvious.
+;
+; Format:
+; [section] ; section goes between []
+; param=value ; assign values to parameters
+
+config_version=5
+
+[application]
+
+config/features=PackedStringArray("4.1")
diff --git a/test.tscn b/test.tscn
new file mode 100644
index 0000000..197f242
--- /dev/null
+++ b/test.tscn
@@ -0,0 +1,37 @@
+[gd_scene load_steps=4 format=3 uid="uid://p3gdhr2kd0m8"]
+
+[ext_resource type="PackedScene" uid="uid://drw0w44wonai8" path="res://addons/@bendn/timecontrol/timebox.tscn" id="1_gfxw5"]
+[ext_resource type="Theme" uid="uid://b4km62j3u3meo" path="res://them.tres" id="1_n048j"]
+[ext_resource type="Script" path="res://addons/@bendn/timecontrol/timebutton.gd" id="3_rj8cl"]
+
+[node name="Control" type="Control"]
+layout_mode = 3
+anchors_preset = 15
+anchor_right = 1.0
+anchor_bottom = 1.0
+grow_horizontal = 2
+grow_vertical = 2
+theme = ExtResource("1_n048j")
+
+[node name="CenterContainer" type="CenterContainer" parent="."]
+layout_mode = 1
+anchors_preset = 15
+anchor_right = 1.0
+anchor_bottom = 1.0
+grow_horizontal = 2
+grow_vertical = 2
+
+[node name="HBoxContainer" type="HBoxContainer" parent="CenterContainer"]
+layout_mode = 2
+
+[node name="PanelContainer" type="PanelContainer" parent="CenterContainer/HBoxContainer"]
+layout_mode = 2
+
+[node name="timebox" parent="CenterContainer/HBoxContainer/PanelContainer" instance=ExtResource("1_gfxw5")]
+layout_mode = 2
+
+[node name="timebutton" type="Button" parent="CenterContainer/HBoxContainer"]
+layout_mode = 2
+size_flags_vertical = 0
+text = "12:00 PM"
+script = ExtResource("3_rj8cl")
diff --git a/them.tres b/them.tres
new file mode 100644
index 0000000..fdd1727
--- /dev/null
+++ b/them.tres
@@ -0,0 +1,17 @@
+[gd_resource type="Theme" load_steps=4 format=3 uid="uid://b4km62j3u3meo"]
+
+[ext_resource type="Texture2D" uid="uid://cgv1h1nr73h70" path="res://dots.png" id="1_tqco8"]
+
+[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_uccrj"]
+content_margin_left = 5.0
+content_margin_right = 5.0
+texture = ExtResource("1_tqco8")
+
+[sub_resource type="SystemFont" id="SystemFont_jr147"]
+font_names = PackedStringArray("Ubuntu Mono")
+subpixel_positioning = 0
+
+[resource]
+default_font = SubResource("SystemFont_jr147")
+default_font_size = 50
+VSeparator/styles/separator = SubResource("StyleBoxTexture_uccrj")