2021-01-24 01:15:52 +10:30
|
|
|
extends Control
|
2019-11-10 15:09:14 +10:30
|
|
|
|
2019-11-19 00:17:48 +10:30
|
|
|
var buttons_pressed := PoolByteArray()
|
|
|
|
var touchbuttons_pressed := PoolByteArray()
|
2021-01-24 16:18:10 +10:30
|
|
|
signal button_pressed(column) # Add int type to these once Godot supports typed signals
|
|
|
|
signal button_released(column)
|
|
|
|
signal touchbutton_pressed(column)
|
|
|
|
signal touchbutton_released(column)
|
|
|
|
signal column_pressed(column) # This will trigger when either is pressed
|
|
|
|
signal column_released(column) # This will trigger when both are released
|
2019-11-19 00:17:48 +10:30
|
|
|
const TOUCHBUTTON_MIN_DIST := 0.8
|
2020-12-28 15:24:37 +10:30
|
|
|
const TOUCHBUTTON_MAX_DIST := 1.075
|
2021-01-03 22:56:20 +10:30
|
|
|
const BUTTON_MIN_DIST := 0.925
|
2019-11-19 00:17:48 +10:30
|
|
|
const BUTTON_MAX_DIST := 1.25
|
2019-11-13 00:48:06 +10:30
|
|
|
|
2021-01-24 02:49:08 +10:30
|
|
|
func _ready():
|
|
|
|
$'/root/main/TouchInput'.connect('touch_positions_updated', self, '_check_buttons')
|
2019-12-17 15:05:27 +10:30
|
|
|
|
2019-11-13 00:48:06 +10:30
|
|
|
func _init():
|
2019-11-19 00:17:48 +10:30
|
|
|
buttons_pressed.resize(Rules.COLS)
|
|
|
|
touchbuttons_pressed.resize(Rules.COLS)
|
|
|
|
for i in Rules.COLS:
|
|
|
|
buttons_pressed[i] = 0
|
|
|
|
touchbuttons_pressed[i] = 0
|
2021-01-24 16:18:10 +10:30
|
|
|
connect('button_pressed', self, '_on_button_pressed')
|
|
|
|
connect('touchbutton_pressed', self, '_on_button_pressed')
|
|
|
|
connect('button_released', self, '_on_button_released')
|
|
|
|
connect('touchbutton_released', self, '_on_touchbutton_released')
|
2019-11-13 00:48:06 +10:30
|
|
|
|
2019-11-19 00:17:48 +10:30
|
|
|
func print_pressed(col: int):
|
2021-01-14 20:35:30 +10:30
|
|
|
print('Pressed %d'%col)
|
2019-11-13 00:48:06 +10:30
|
|
|
|
2021-01-24 02:49:08 +10:30
|
|
|
func _check_buttons(touch_positions):
|
2019-11-19 00:17:48 +10:30
|
|
|
var buttons_pressed_temp := []
|
|
|
|
var touchbuttons_pressed_temp := []
|
|
|
|
for i in Rules.COLS:
|
|
|
|
buttons_pressed_temp.append(false)
|
|
|
|
touchbuttons_pressed_temp.append(false)
|
|
|
|
|
2021-01-24 02:49:08 +10:30
|
|
|
var global_center = rect_global_position + rect_size*0.5
|
2019-11-19 00:17:48 +10:30
|
|
|
for pos in touch_positions:
|
2021-01-24 02:49:08 +10:30
|
|
|
pos -= global_center
|
2021-01-29 22:11:14 +10:30
|
|
|
pos /= rect_size.x * 0.5
|
2019-11-19 00:17:48 +10:30
|
|
|
var pol = cartesian2polar(pos.x, pos.y)
|
2021-01-29 22:11:14 +10:30
|
|
|
var dist = pol.x/GameTheme.receptor_ring_radius_normalized
|
2019-11-19 00:17:48 +10:30
|
|
|
var angle = rad2deg(pol.y)
|
2021-01-03 22:56:20 +10:30
|
|
|
if dist < TOUCHBUTTON_MIN_DIST: # Short circuit out to save some logic
|
2019-11-19 00:17:48 +10:30
|
|
|
continue
|
|
|
|
# bin the angle
|
|
|
|
angle -= Rules.FIRST_COLUMN_ANGLE_DEG - Rules.COLS_TOUCH_ARC_DEG/2.0
|
|
|
|
if fmod(angle, Rules.COLS_ANGLE_DEG) > Rules.COLS_TOUCH_ARC_DEG:
|
|
|
|
continue
|
|
|
|
var col := int(floor(angle/Rules.COLS_ANGLE_DEG))
|
2021-01-03 22:56:20 +10:30
|
|
|
touchbuttons_pressed_temp[col] = touchbuttons_pressed_temp[col] or (dist < TOUCHBUTTON_MAX_DIST) # min dist already checked
|
|
|
|
buttons_pressed_temp[col] = buttons_pressed_temp[col] or (dist >= BUTTON_MIN_DIST) and (dist < BUTTON_MAX_DIST)
|
2019-11-19 00:17:48 +10:30
|
|
|
|
|
|
|
for i in Rules.COLS:
|
|
|
|
set_button_state(i, buttons_pressed_temp[i])
|
|
|
|
set_touchbutton_state(i, touchbuttons_pressed_temp[i])
|
|
|
|
|
|
|
|
func set_button_state(index: int, state: bool):
|
|
|
|
var new_state = int(state)
|
|
|
|
match new_state - buttons_pressed[index]:
|
|
|
|
1:
|
2021-01-14 20:35:30 +10:30
|
|
|
emit_signal('button_pressed', index)
|
2019-11-19 00:17:48 +10:30
|
|
|
-1:
|
2021-01-14 20:35:30 +10:30
|
|
|
emit_signal('button_released', index)
|
2019-11-19 00:17:48 +10:30
|
|
|
buttons_pressed[index] = new_state
|
|
|
|
|
|
|
|
func set_touchbutton_state(index: int, state: bool):
|
|
|
|
var new_state = int(state)
|
|
|
|
match new_state - touchbuttons_pressed[index]:
|
|
|
|
1:
|
2021-01-14 20:35:30 +10:30
|
|
|
emit_signal('touchbutton_pressed', index)
|
2019-11-19 00:17:48 +10:30
|
|
|
-1:
|
2021-01-14 20:35:30 +10:30
|
|
|
emit_signal('touchbutton_released', index)
|
2019-11-19 00:17:48 +10:30
|
|
|
touchbuttons_pressed[index] = new_state
|
2021-01-24 16:18:10 +10:30
|
|
|
|
|
|
|
func _on_button_pressed(column: int):
|
|
|
|
emit_signal('column_pressed', column)
|
|
|
|
|
|
|
|
func _on_button_released(column: int):
|
|
|
|
if touchbuttons_pressed[column] == 0:
|
|
|
|
emit_signal('column_released', column)
|
|
|
|
|
|
|
|
func _on_touchbutton_released(column: int):
|
|
|
|
if buttons_pressed[column] == 0:
|
|
|
|
emit_signal('column_released', column)
|