Skip to content

Commit

Permalink
Merge pull request #42 from HotariTobu:feat/HotariTobu-radio-button-e…
Browse files Browse the repository at this point in the history
…xample

Add Radio Button Example
  • Loading branch information
HotariTobu authored Nov 21, 2024
2 parents 8b446f3 + e9b119f commit c625006
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class_name CaseBindingConverter
extends BindingConverter

var _case_value: Variant
var _last_source_value: Variant


func _init(case_value: Variant):
_case_value = case_value


func source_to_target(source_value: Variant) -> Variant:
_last_source_value = source_value
return source_value == _case_value


func target_to_source(target_value: Variant) -> Variant:
return _case_value if target_value else _last_source_value
16 changes: 16 additions & 0 deletions examples/buttons.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func _ready():
_data.bind(&"option_index").using(str).to_label($Col3/DisplayRow/OptionIndexLabel)
_data.bind(&"option_index").to_option_button($Col3/OptionButton)

# gdlint:ignore = constant-name
const Mode = Data.TravelMode
var get_mode_key = func(mode: Mode): return Mode.find_key(mode)
# gdlint:ignore = function-variable-name,
var Case = CaseBindingConverter
_data.bind(&"travel_mode").using(get_mode_key).to_label($Col4/DisplayRow/RadioValueLabel)
_data.bind(&"travel_mode").using(Case.new(Mode.DRIVING)).to_check_box($Col4/DrivingCheckBox)
_data.bind(&"travel_mode").using(Case.new(Mode.TRANSIT)).to_check_box($Col4/TransitCheckBox)
_data.bind(&"travel_mode").using(Case.new(Mode.WALKING)).to_check_box($Col4/WalkingCheckBox)
_data.bind(&"travel_mode").using(Case.new(Mode.CYCLING)).to_check_box($Col4/CyclingCheckBox)
_data.bind(&"travel_mode").using(Case.new(Mode.FLIGHTS)).to_check_box($Col4/FlightsCheckBox)


func _on_on_button_pressed():
_data.binary_state = true
Expand All @@ -44,12 +56,16 @@ func _on_color_button_pressed(color):


class Data:
enum TravelMode { DRIVING, TRANSIT, WALKING, CYCLING, FLIGHTS }

var binary_state = false

var color = Color.RED

var option_index: int = -1

var travel_mode: TravelMode = TravelMode.DRIVING


class BoolToOnOff:
extends BindingConverter
Expand Down
60 changes: 59 additions & 1 deletion examples/buttons.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
[gd_scene load_steps=3 format=3 uid="uid://p7swf5sjivj0"]
[gd_scene load_steps=8 format=3 uid="uid://p7swf5sjivj0"]

[ext_resource type="Script" path="res://examples/buttons.gd" id="1_sfuve"]
[ext_resource type="LabelSettings" uid="uid://bcnt8xhjv02bx" path="res://examples/header_label_settings.tres" id="2_u12hm"]

[sub_resource type="ButtonGroup" id="ButtonGroup_w2qvh"]

[sub_resource type="ButtonGroup" id="ButtonGroup_hari6"]

[sub_resource type="ButtonGroup" id="ButtonGroup_77akt"]

[sub_resource type="ButtonGroup" id="ButtonGroup_x7oww"]

[sub_resource type="ButtonGroup" id="ButtonGroup_4ynul"]

[node name="Buttons" type="HBoxContainer"]
anchors_preset = 8
anchor_left = 0.5
Expand Down Expand Up @@ -173,6 +183,54 @@ text = "Select 3"
layout_mode = 2
text = "Deselect"

[node name="VSeparator3" type="VSeparator" parent="."]
layout_mode = 2

[node name="Col4" type="VBoxContainer" parent="."]
custom_minimum_size = Vector2(200, 0)
layout_mode = 2

[node name="Label" type="Label" parent="Col4"]
layout_mode = 2
text = "Radio Button"
label_settings = ExtResource("2_u12hm")

[node name="DisplayRow" type="HBoxContainer" parent="Col4"]
layout_mode = 2

[node name="Label" type="Label" parent="Col4/DisplayRow"]
layout_mode = 2
size_flags_horizontal = 3
text = "Current Value:"

[node name="RadioValueLabel" type="Label" parent="Col4/DisplayRow"]
layout_mode = 2

[node name="DrivingCheckBox" type="CheckBox" parent="Col4"]
layout_mode = 2
button_group = SubResource("ButtonGroup_w2qvh")
text = "Driving"

[node name="TransitCheckBox" type="CheckBox" parent="Col4"]
layout_mode = 2
button_group = SubResource("ButtonGroup_hari6")
text = "Transit"

[node name="WalkingCheckBox" type="CheckBox" parent="Col4"]
layout_mode = 2
button_group = SubResource("ButtonGroup_77akt")
text = "Walking"

[node name="CyclingCheckBox" type="CheckBox" parent="Col4"]
layout_mode = 2
button_group = SubResource("ButtonGroup_x7oww")
text = "Cycling"

[node name="FlightsCheckBox" type="CheckBox" parent="Col4"]
layout_mode = 2
button_group = SubResource("ButtonGroup_4ynul")
text = "Flights"

[connection signal="pressed" from="Col1/HBoxContainer/OnButton" to="." method="_on_on_button_pressed"]
[connection signal="pressed" from="Col1/HBoxContainer/OffButton" to="." method="_on_off_button_pressed"]
[connection signal="pressed" from="Col2/HBoxContainer/RedButton" to="." method="_on_color_button_pressed" binds= [Color(1, 0, 0, 1)]]
Expand Down

0 comments on commit c625006

Please sign in to comment.