aboutsummaryrefslogtreecommitdiff
path: root/logic/stage.gd
blob: dc4227364628f427d55094c19e9454ef65a61974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
extends ScrollContainer
class_name Stage

signal save		# (database_entry: Dictionary)
signal discard	# ()

const FILTERS_FILE_PATH: String = "user://filters.csv"
const POINTER_VELOCITY_DECAYING_FACTOR: float = 2.5

var staged_entry := {}
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

onready var process_id		:= get_node("controls/process_id") as LineEdit
onready var surgery_id		:= get_node("controls/surgery_id") as LineEdit
onready var date			:= get_node("controls/date_picker") as DatePicker
onready var place			:= get_node("controls/place") as LineEdit
onready var anesthesia		:= get_node("controls/anesthesia") as LineEdit
onready var first_assistant	:= get_node("controls/first_assistant") as LineEdit
onready var type			:= get_node("controls/type") as LineEdit
onready var sub_type		:= get_node("controls/sub_type") as LineEdit
onready var sub_sub_type	:= get_node("controls/sub_sub_type") as LineEdit
onready var pathology		:= get_node("controls/pathology") as LineEdit
onready var intervention	:= get_node("controls/intervention") as LineEdit
onready var is_urgency		:= get_node("controls/is_urgency") as Button
onready var notes			:= get_node("controls/notes") as LineEdit
onready var save_button		:= get_node("controls/save") as Button
onready var discard_button	:= get_node("controls/discard") as Button
onready var v_scroll_bar	:= get_v_scrollbar() as VScrollBar


func _ready():
	save_button.connect("pressed", self, "save_action")
	discard_button.connect("pressed", self, "discard_action")
	
	for it in get_node("controls").get_children():
		it = it as Control
		match it.name:
			"date_picker", "save", "discard":
				pass
			
			_:
				var drag_sensor = PointerInputSensor.new()
				it.add_child(drag_sensor)
				
				drag_sensor.name = "drag_sensor"
				drag_sensor.anchor_right = 1.0
				drag_sensor.anchor_bottom = 1.0
				
				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")
				
				it.connect("focus_entered", drag_sensor, "set_mouse_filter", [Control.MOUSE_FILTER_IGNORE])
				it.connect("focus_entered", drag_sensor, "mouse_default_cursor_shape", [Control.CURSOR_IBEAM])
				it.connect("focus_exited", drag_sensor, "set_mouse_filter", [Control.MOUSE_FILTER_STOP])
				it.connect("focus_exited", drag_sensor, "mouse_default_cursor_shape", [Control.CURSOR_ARROW])
				
				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):
	# 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 save_action():
	self.visible = false
	emit_signal("save", get_stage())


func discard_action():
	self.visible = false
	emit_signal("discard")


func set_stage(entry: Dictionary):
	staged_entry = entry.duplicate(true)
	process_id.text			= staged_entry.process_id
	surgery_id.text			= staged_entry.surgery_id
	date.set_date(staged_entry.date_year, staged_entry.date_month, staged_entry.date_day)
	place.text				= staged_entry.place
	anesthesia.text			= staged_entry.anesthesia
	first_assistant.text	= staged_entry.first_assistant
	type.text				= staged_entry.type
	sub_type.text			= staged_entry.sub_type
	sub_sub_type.text		= staged_entry.sub_sub_type
	pathology.text			= staged_entry.pathology
	intervention.text		= staged_entry.intervention
	is_urgency.pressed		= staged_entry.is_urgency
	notes.text 				= staged_entry.notes
	self.scroll_vertical 	= 0


func get_stage() -> Dictionary:
	staged_entry.process_id		= process_id.text
	staged_entry.surgery_id		= surgery_id.text
	staged_entry.date_year		= date.get_year()
	staged_entry.date_month		= date.get_month()
	staged_entry.date_day		= date.get_day()
	staged_entry.place			= place.text
	staged_entry.anesthesia		= anesthesia.text
	staged_entry.first_assistant= first_assistant.text
	staged_entry.type			= type.text
	staged_entry.sub_type		= sub_type.text
	staged_entry.sub_sub_type	= sub_sub_type.text
	staged_entry.pathology		= pathology.text
	staged_entry.intervention	= intervention.text
	staged_entry.is_urgency		= is_urgency.pressed
	staged_entry.notes			= notes.text
	return staged_entry


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:
			
			# @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
			
			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)



func load_filters(file_path: String = FILTERS_FILE_PATH):
	var file := File.new()
	file.open(file_path, File.READ_WRITE)
	var headers: PoolStringArray
	var is_first_line := true
#	while file.eof_reached() == false: 								# @DAM Why this?
	while file.get_position() < file.get_len():
		var csv_entry := file.get_csv_line()
		if is_first_line:
			is_first_line = false
			headers = csv_entry
			continue
#		var entry = DatabaseEntry.instance_entry()
#		for idx in headers.size():
#			var field_name := headers[idx]
#			var field_value := csv_entry[idx]
#			match field_name:
#				"date_year", "date_month", "date_day":
#					entry[field_name] = int(field_value)
#				"is_urgency":
#					entry[field_name] = true if field_value.strip_edges().to_lower() == "true" else false
#				_:
#					entry[field_name] = field_value
#		filters.append(entry)


func store_filters(file_path: String = FILTERS_FILE_PATH):
	pass # @DAM TODO


func clear_filters(save_changes: bool = false):
	pass # @DAM TODO
	if save_changes:
		store_filters()