diff options
| author | dam <dam@gudinoff> | 2022-01-09 01:25:55 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2022-01-09 01:25:55 +0000 |
| commit | 5ba40857cdc770841d216a27e2a9e8bb3ebf3186 (patch) | |
| tree | 2212f9685067270285265c81208fd5122a760d5d | |
| parent | 431f042390ad36297a5ec986772c77da23b7fb67 (diff) | |
| download | surgery-log-5ba40857cdc770841d216a27e2a9e8bb3ebf3186.tar.zst surgery-log-5ba40857cdc770841d216a27e2a9e8bb3ebf3186.zip | |
Split touch logic from database. Prototype of popup with list of options.
| -rw-r--r-- | logic/database.gd | 67 | ||||
| -rw-r--r-- | logic/popup_list.gd | 24 | ||||
| -rw-r--r-- | logic/stage.gd | 75 | ||||
| -rw-r--r-- | main.tscn | 39 | ||||
| -rw-r--r-- | pointer_input_sensor.gd | 22 | ||||
| -rw-r--r-- | project.godot | 8 | ||||
| -rw-r--r-- | readme.md | 2 | ||||
| -rw-r--r-- | touch_item_list/touch_item_list.gd | 70 | ||||
| -rw-r--r-- | touch_item_list/touch_item_list.tscn | 23 |
9 files changed, 240 insertions, 90 deletions
diff --git a/logic/database.gd b/logic/database.gd index 044681f..de978b7 100644 --- a/logic/database.gd +++ b/logic/database.gd @@ -1,21 +1,16 @@ -extends ItemList +extends TouchItemList class_name Database const DATABASE_FILE_PATH: String = "user://database.csv" -const POINTER_VELOCITY_DECAYING_FACTOR: float = 2.5 var db: Array var selected_idx: int var staged_idx: int -var is_pointer_dragging := false -var pointer_drag_velocity := 0.0 onready var stage := get_node("/root/main/stage") as Stage onready var delete_button := get_node("actions/delete") as Button onready var edit_button := get_node("actions/edit") as Button onready var add_button := get_node("actions/add") as Button -onready var v_scroll_bar := get_v_scroll() as ScrollBar -onready var drag_sensor := get_node("drag_sensor") as PointerInputSensor func _init(): @@ -36,24 +31,13 @@ func _ready(): stage.connect("save", self, "save_stage") stage.connect("discard", self, "discard_stage") - drag_sensor.connect("on_press", self, "pointer_input_handler") - drag_sensor.connect("on_drag", self, "pointer_input_handler") - drag_sensor.connect("on_end_drag", self, "pointer_input_handler") - drag_sensor.connect("on_click", self, "pointer_input_handler") - for it in db: self.add_item(get_entry_view(it)) -func _process(delta: float): - # Apply drag movement inertia. - if is_pointer_dragging == false && abs(pointer_drag_velocity) > 0.5: - pointer_drag_velocity *= clamp((1.0 - POINTER_VELOCITY_DECAYING_FACTOR * delta), 0.0, 1.0) - v_scroll_bar.value -= pointer_drag_velocity * delta - - func _notification(what: int): - # @DAM Yet to be polished. + # @DAM This code should be moved into the main.gd which should check which node was currently + # active and above, and send the signal there. if visible == false || has_focus() == false: return @@ -77,10 +61,6 @@ func clear_selection(): unselect_all() -func scroll_to_selected(): - v_scroll_bar.value = float(selected_idx)/float(db.size()) * v_scroll_bar.max_value - (v_scroll_bar.page * 0.5) - - func delete_action(): if selected_idx < 0: return @@ -125,7 +105,7 @@ func save_stage(entry: Dictionary): select(next_selected_idx) emit_signal("item_selected", next_selected_idx) # Calling "select" does not trigger the "item_selected" signal. set_item_text(selected_idx, get_entry_view(db[selected_idx])) - call_deferred("scroll_to_selected") + ensure_current_is_visible() store_database() @@ -203,42 +183,3 @@ func fake_database(save_changes: bool = false): store_database() -func pointer_input_handler(pointer: PointerInputSensor.PointerInputData): - match pointer.action: - PointerInputSensor.PointerInputAction.ON_PRESS: - is_pointer_dragging = true - - PointerInputSensor.PointerInputAction.ON_DRAG: - is_pointer_dragging = true - pointer_drag_velocity = pointer.velocity.y - v_scroll_bar.value -= pointer.relative_position.y - - PointerInputSensor.PointerInputAction.ON_END_DRAG: - is_pointer_dragging = false - - PointerInputSensor.PointerInputAction.ON_CLICK: - var target := self - var position := target.get_global_mouse_position() - target.rect_global_position - - var event_touch := InputEventScreenTouch.new() - event_touch.index = 0 - event_touch.position = position - - var event_mouse := InputEventMouseButton.new() - event_mouse.button_index = BUTTON_LEFT - event_mouse.button_mask = BUTTON_MASK_LEFT - event_mouse.position = position - - event_mouse.pressed = true - event_touch.pressed = true - target._gui_input(event_mouse) - target._gui_input(event_touch) - - target.grab_focus() - - event_mouse.pressed = false - event_touch.pressed = false - target._gui_input(event_mouse) - target._gui_input(event_touch) - - diff --git a/logic/popup_list.gd b/logic/popup_list.gd new file mode 100644 index 0000000..1dd156c --- /dev/null +++ b/logic/popup_list.gd @@ -0,0 +1,24 @@ +extends Popup + +signal item_selected + +onready var item_list := get_node("list") as TouchItemList + + +func _ready(): + item_list.connect("item_selected", self, "selected") + + +func popup_options(options: Array): + item_list.v_scroll_bar.value = 0.0 + item_list.clear() + for it in options: + item_list.add_item(it) + self.popup_centered_ratio(0.9) + + +func selected(index: int): + self.hide() + emit_signal("item_selected", index) + + diff --git a/logic/stage.gd b/logic/stage.gd index f6f6e47..dc42273 100644 --- a/logic/stage.gd +++ b/logic/stage.gd @@ -8,7 +8,42 @@ const FILTERS_FILE_PATH: String = "user://filters.csv" const POINTER_VELOCITY_DECAYING_FACTOR: float = 2.5 var staged_entry := {} -var filters := {} +var filters := { + "place": { + "bloco central": null, + "tondela": null, + "xpto_00": null, + "xpto_01": null, + "xpto_02": null, + "xpto_03": null, + "xpto_04": null, + "xpto_05": null, + "xpto_06": null, + "xpto_07": null, + "xpto_08": null, + "xpto_09": null, + "xpto_10": null, + "xpto_11": null, + "xpto_12": null, + "xpto_13": null, + "xpto_14": null, + "xpto_15": null, + "xpto_16": null, + "xpto_17": null, + "xpto_18": null, + "xpto_19": null, + "xpto_20": null, + "xpto_21": null, + "xpto_22": null, + "xpto_23": null, + "xpto_24": null, + "xpto_25": null, + "xpto_26": null, + "xpto_27": null, + "xpto_28": null, + "xpto_29": null, + }, +} var is_pointer_dragging := false var pointer_drag_velocity := 0.0 @@ -60,6 +95,22 @@ func _ready(): if it is LineEdit: it.connect("focus_exited", it, "deselect") + + var auto_place := place.get_node("auto") as Button + auto_place.connect("pressed", self, "auto_populate", ["place"]) + + +func auto_populate(field: String): + var stage_options = get_node("/root/main/popup_list") as Popup + stage_options.connect("item_selected", self, "auto_selected", [field], CONNECT_ONESHOT) + stage_options.popup_options(filters[field].keys()) + + +func auto_selected(index: int, field: String): + var field_input: LineEdit = self[field] + field_input.text = filters[field].keys()[index] + field_input.caret_position = field_input.text.length() + func _process(delta: float): @@ -116,11 +167,6 @@ func get_stage() -> Dictionary: return staged_entry -func _notification(what: int): - if what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST: - discard_action() - - func pointer_input_handler(pointer: PointerInputSensor.PointerInputData): match pointer.action: PointerInputSensor.PointerInputAction.ON_PRESS: @@ -135,9 +181,23 @@ func pointer_input_handler(pointer: PointerInputSensor.PointerInputData): is_pointer_dragging = false PointerInputSensor.PointerInputAction.ON_CLICK: + + # @DAM Try this approach on the TouchItemList + var target: Control = pointer.target.get_parent() var position := target.get_global_mouse_position() - target.rect_global_position + var button: Button = target.get_node("auto") + if button != null && button.get_rect().has_point(position): + button.grab_focus() +# button.emit_signal("button_down") + button.emit_signal("pressed") +# button.emit_signal("button_up") + else: + target.grab_focus() + return + + var event_touch := InputEventScreenTouch.new() event_touch.index = 0 event_touch.position = position @@ -153,13 +213,14 @@ func pointer_input_handler(pointer: PointerInputSensor.PointerInputData): target._gui_input(event_touch) target.grab_focus() - + event_mouse.pressed = false event_touch.pressed = false target._gui_input(event_mouse) target._gui_input(event_touch) + func load_filters(file_path: String = FILTERS_FILE_PATH): var file := File.new() file.open(file_path, File.READ_WRITE) @@ -1,4 +1,4 @@ -[gd_scene load_steps=11 format=2] +[gd_scene load_steps=12 format=2] [ext_resource path="res://main.gd" type="Script" id=1] [ext_resource path="res://date_picker/date_picker.tscn" type="PackedScene" id=2] @@ -9,7 +9,8 @@ [ext_resource path="res://icons/add.png" type="Texture" id=7] [ext_resource path="res://icons/delete.png" type="Texture" id=8] [ext_resource path="res://logic/stage.gd" type="Script" id=9] -[ext_resource path="res://pointer_input_sensor.gd" type="Script" id=10] +[ext_resource path="res://logic/popup_list.gd" type="Script" id=10] +[ext_resource path="res://touch_item_list/touch_item_list.tscn" type="PackedScene" id=11] [node name="main" type="Control"] anchor_right = 1.0 @@ -20,24 +21,8 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="database" type="ItemList" parent="."] -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_top = 60.0 -mouse_filter = 2 +[node name="database" parent="." instance=ExtResource( 11 )] script = ExtResource( 5 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="drag_sensor" type="Control" parent="database"] -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_right = -8.0 -script = ExtResource( 10 ) -__meta__ = { -"_edit_use_anchors_": false -} [node name="actions" type="VBoxContainer" parent="database"] anchor_left = 1.0 @@ -133,6 +118,12 @@ placeholder_text = "Local" caret_blink = true caret_blink_speed = 0.5 +[node name="auto" type="Button" parent="stage/controls/place"] +anchor_left = 0.85 +anchor_right = 1.0 +anchor_bottom = 1.0 +text = "X" + [node name="anesthesia" type="LineEdit" parent="stage/controls"] margin_top = 363.0 margin_right = 1080.0 @@ -243,6 +234,7 @@ anchor_bottom = 1.0 size_flags_horizontal = 3 size_flags_vertical = 3 window_title = "PICK FILE" +mode_overrides_title = false access = 2 filters = PoolStringArray( "*.csv" ) show_hidden_files = true @@ -261,6 +253,15 @@ window_title = "CONFIRM ACTION" dialog_text = "Are you sure you want to delete all filters?" dialog_autowrap = true +[node name="popup_list" type="Popup" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +popup_exclusive = true +script = ExtResource( 10 ) + +[node name="list" parent="popup_list" instance=ExtResource( 11 )] +margin_top = 0.0 + [node name="debug" type="Label" parent="."] visible = false anchor_right = 1.0 diff --git a/pointer_input_sensor.gd b/pointer_input_sensor.gd index 27f257c..ee5e390 100644 --- a/pointer_input_sensor.gd +++ b/pointer_input_sensor.gd @@ -12,6 +12,7 @@ signal on_exit signal on_begin_drag signal on_drag signal on_end_drag +signal on_scroll enum PointerInputAction { UNDEFINED, @@ -25,10 +26,12 @@ enum PointerInputAction { ON_BEGIN_DRAG, ON_DRAG, ON_END_DRAG, + ON_SCROLL, } class PointerInputData: var target: PointerInputSensor + var event: InputEvent var index := -1 var initial_position := Vector2.ZERO var current_position := Vector2.ZERO @@ -36,8 +39,10 @@ class PointerInputData: var velocity := Vector2.ZERO var was_dragged := false var is_pressed := false + var scroll := 0.0 var action: int = PointerInputAction.UNDEFINED + export var drag_threshold_cm: float = 0.250 var pointer: PointerInputData @@ -64,6 +69,12 @@ func _gui_input(event: InputEvent): if event.index != 0: return + pointer.event = event +# pointer.index = -1 +# pointer.scroll = 0.0 +# pointer.action = PointerInputAction.UNDEFINED +# pointer.velocity = Vector2.ZERO + if event is InputEventScreenTouch && (pointer.is_pressed == false || pointer.index == event.index): var touch := event as InputEventScreenTouch pointer.is_pressed = event.pressed @@ -84,6 +95,10 @@ func _gui_input(event: InputEvent): emit_signal("on_end_drag", pointer) pointer.index = -1 pointer.was_dragged = false + # @DAM Maybe add this. +# pointer.initial_position = Vector2.ZERO +# pointer.current_position = Vector2.ZERO +# pointer.relative_position = Vector2.ZERO if event is InputEventScreenDrag && event.index == pointer.index: var drag := event as InputEventScreenDrag @@ -96,5 +111,12 @@ func _gui_input(event: InputEvent): emit_signal("on_begin_drag", pointer) pointer.action = PointerInputAction.ON_DRAG emit_signal("on_drag", pointer) + + # @DAM Clean this code please. + if event is InputEventMouseButton && event.is_pressed(): + if event.button_index == BUTTON_WHEEL_UP || event.button_index == BUTTON_WHEEL_DOWN: + pointer.scroll = -1.0 if event.button_index == BUTTON_WHEEL_UP else 1.0 + pointer.action = PointerInputAction.ON_SCROLL + emit_signal("on_scroll", pointer) diff --git a/project.godot b/project.godot index cfdcbb8..d630b8d 100644 --- a/project.godot +++ b/project.godot @@ -9,7 +9,7 @@ config_version=4 _global_script_classes=[ { -"base": "ItemList", +"base": "TouchItemList", "class": "Database", "language": "GDScript", "path": "res://logic/database.gd" @@ -34,6 +34,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://logic/stage.gd" }, { +"base": "ItemList", +"class": "TouchItemList", +"language": "GDScript", +"path": "res://touch_item_list/touch_item_list.gd" +}, { "base": "Control", "class": "ValuePicker", "language": "GDScript", @@ -45,6 +50,7 @@ _global_script_class_icons={ "DatePicker": "", "PointerInputSensor": "", "Stage": "", +"TouchItemList": "", "ValuePicker": "" } @@ -16,6 +16,8 @@ Surgery Log - [x] load/store database CSV file; - [x] export database to CSV file; - [x] allow to reset database; +- [x] split touch logic from database (create TouchItemList); +- [ ] split touch logic from stage (create TouchVerticalContainer); - [ ] load/store filters CSV file; - [ ] import/export filters to CSV file; - [ ] Implement file access permission check on Android: diff --git a/touch_item_list/touch_item_list.gd b/touch_item_list/touch_item_list.gd new file mode 100644 index 0000000..d2d0d37 --- /dev/null +++ b/touch_item_list/touch_item_list.gd @@ -0,0 +1,70 @@ +extends ItemList +class_name TouchItemList + +const POINTER_VELOCITY_DECAYING_FACTOR: float = 2.5 + +var is_pointer_dragging := false +var pointer_drag_velocity := 0.0 + +onready var drag_sensor := get_node("drag_sensor") as PointerInputSensor +onready var v_scroll_bar := get_v_scroll() as ScrollBar + + +func _ready(): + drag_sensor.connect("on_press", self, "pointer_input_handler") + drag_sensor.connect("on_drag", self, "pointer_input_handler") + drag_sensor.connect("on_end_drag", self, "pointer_input_handler") + drag_sensor.connect("on_click", self, "pointer_input_handler") + drag_sensor.connect("on_scroll", self, "pointer_input_handler") + + +func _process(delta: float): + # Apply drag movement inertia. + if is_pointer_dragging == false && abs(pointer_drag_velocity) > 0.5: + pointer_drag_velocity *= clamp((1.0 - POINTER_VELOCITY_DECAYING_FACTOR * delta), 0.0, 1.0) + v_scroll_bar.value -= pointer_drag_velocity * delta + + +func pointer_input_handler(pointer: PointerInputSensor.PointerInputData): + match pointer.action: + PointerInputSensor.PointerInputAction.ON_PRESS: + is_pointer_dragging = true + + PointerInputSensor.PointerInputAction.ON_DRAG: + is_pointer_dragging = true + pointer_drag_velocity = pointer.velocity.y + v_scroll_bar.value -= pointer.relative_position.y + + PointerInputSensor.PointerInputAction.ON_END_DRAG: + is_pointer_dragging = false + + PointerInputSensor.PointerInputAction.ON_SCROLL: + var target := self + target._gui_input(pointer.event) + + PointerInputSensor.PointerInputAction.ON_CLICK: + var target := self + var position := target.get_global_mouse_position() - target.rect_global_position + + var event_touch := InputEventScreenTouch.new() + event_touch.index = 0 + event_touch.position = position + + var event_mouse := InputEventMouseButton.new() + event_mouse.button_index = BUTTON_LEFT + event_mouse.button_mask = BUTTON_MASK_LEFT + event_mouse.position = position + + event_mouse.pressed = true + event_touch.pressed = true + target._gui_input(event_mouse) + target._gui_input(event_touch) + + target.grab_focus() + + event_mouse.pressed = false + event_touch.pressed = false + target._gui_input(event_mouse) + target._gui_input(event_touch) + + diff --git a/touch_item_list/touch_item_list.tscn b/touch_item_list/touch_item_list.tscn new file mode 100644 index 0000000..6022ed4 --- /dev/null +++ b/touch_item_list/touch_item_list.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://pointer_input_sensor.gd" type="Script" id=1] +[ext_resource path="res://touch_item_list/touch_item_list.gd" type="Script" id=2] + +[node name="item_list" type="ItemList"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_top = 60.0 +mouse_filter = 2 +script = ExtResource( 2 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="drag_sensor" type="Control" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -8.0 +script = ExtResource( 1 ) +__meta__ = { +"_edit_use_anchors_": false +} |
