-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextLayer.gd
149 lines (118 loc) · 3.22 KB
/
TextLayer.gd
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
extends TileMap
const layer_name: String = Global.LAYER_TEXT
onready var inputs = $Inputs
var _drag_offset: Vector2 = Vector2.ZERO
var _drag_backup: Dictionary
func set_cell_character(character: String):
set_cellv(Settings.cell, tile_set.find_tile_by_name(character))
func clear_cell(c = null):
if c == null: c = Settings.cell
set_cellv(c, -1)
func clear_left_cell(c = null):
if c == null: c = Settings.cell
set_cell(c.x - 1, c.y, -1)
func push_cells(c = null):
"""
Shift subsequent characters in the current row right
until two emtpy cells are found.
"""
if c == null: c = Settings.cell
var upcoming = get_upcoming_cells(c)
# need to start from the back as n+1 is stored and would overwrite else.
upcoming.invert()
for x in upcoming:
set_cell(x + 1, c.y, get_cell(x, c.y))
set_cell(x, c.y, -1)
func get_upcoming_cells(c = null) -> Array:
"""
returns the x coordinates of upcoming not empty cells in current row
until two empty cells are found.
"""
if c == null: c = Settings.cell
var upcoming = []
var used = get_used_cells()
var x = c.x
while true:
if Vector2(x, c.y) in used:
upcoming.append(float(x))
elif not Vector2(x + 1, c.y) in used and x != c.x:
# Found a two spaced gap, can stop now.
break
x += 1
return upcoming
func get_former_cells(c = null) -> Array:
if c == null: c = Settings.cell
var former = []
var used = get_used_cells()
var x = c.x
while true:
if Vector2(x, c.y) in used:
former.append(float(x))
elif not Vector2(x - 1, c.y) in used and x != c.x:
# Found a two spaced gap, can stop now.
break
x -= 1
return former
func pull_cells(c = null):
"""
Shift subsequent characters in the current row left
until two emtpy cells are found.
"""
if c == null: c = Settings.cell
for x in get_upcoming_cells(c):
set_cell(x - 1, c.y, get_cell(x, c.y))
set_cell(x, c.y, -1)
func get_home() -> int:
"""
Returns the change in x coordinate to reach
the beginning of the current row.
"""
var c = get_former_cells()
if len(c):
return c.min() - Settings.cell.x
return 0
func get_end() -> int:
"""
Returns the change in x coordinate to reach
the end of the current row.
"""
var c = get_upcoming_cells()
if len(c):
return c.max() - Settings.cell.x
return 0
func start_dragging():
_drag_backup = get_used_cells_with_ids()
func get_used_cells_with_ids() -> Dictionary:
var ret = {}
for c in get_used_cells():
ret[c] = get_cellv(c)
return ret
func show_backup():
clear()
for coord in _drag_backup:
set_cellv(coord, _drag_backup[coord])
func drag_cells(cells: PoolVector2Array, offset: Vector2):
"""
Drag the given cells by the given offset.
Also empty cells must be given.
Call this function every time, offset changes.
"""
show_backup()
for c in cells:
# clear all origin and target cells.
# yes, needs to be done before drawing again.
set_cellv(c, -1)
set_cellv(c + offset, -1)
var move_to: Vector2
for c in cells:
move_to = c + offset
# is there a character to move?
if _drag_backup.has(c):
set_cellv(move_to, _drag_backup[c])
func _on_TextMap_switch_layer(to_layer_name):
if to_layer_name == layer_name:
inputs.set_process_unhandled_input(true)
inputs.show()
else:
inputs.set_process_unhandled_input(false)
inputs.hide()