-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModGrab.py
146 lines (142 loc) · 4.04 KB
/
ModGrab.py
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
import TermUI as tui
import TermIntr as ti
import Befunge as bf
import Funge as fng
def selectingMod(char,value,subPos,matrix,corners):
global state
mx,my=(subPos[1]+corners[1],subPos[0]+corners[0])
if state=="Selecting":
start=corner.copy()
end=cursor.cursor.copy()
if start.x>end.x:
start.x,end.x=(end.x,start.x)
if start.y>end.y:
start.y,end.y=(end.y,start.y)
if end.y>=my>=start.y and end.x>=mx>=start.x:
char.bcolor="27"
# raise ValueError((mx,my),end,start,subPos,corners)
# raise ValueError
def selectedMarkers():
global state,cursor
if state=="Selected":
root=cursor.cursor
for y in range(len(data)):
for x in range(len(data[0])):
if data[y][x] is not None:
def changer(char):
char.char=chr(data[y][x])
char.bcolor="27"
char.fcolor="232"
# raise TypeError
yield (root.x+x,root.y+y,changer)
state="None"
corner=None
data=None
origData=None
rotation=0
def modInit(m,config,lock):
global cursor
cursor=m.cursor
statusText=m.statustext.queueText
m.fungescreen.display.modifiers.append(selectingMod)
m.fungescreen.display.markers.append(selectedMarkers)
listener=ti.Listener()
@listener.handle
def handle(key):
global state,corner,data,rotation,origData
if key==config["GrabKey"]:
# start selecting
if state=="None":
corner=cursor.cursor.copy()
state="Selecting"
statusText(f"Grabber: *Selecting*")
# select
elif state=="Selecting":
matrix=m.load.funge.plane.matrix
start=corner.copy()
end=cursor.cursor.copy()
if start.x>end.x:
start.x,end.x=(end.x,start.x)
if start.y>end.y:
start.y,end.y=(end.y,start.y)
data=[[None for _ in range(end.x-start.x+1)] for _ in range(end.y-start.y+1)]
toClear=[]
for y in matrix:
if end.y>=y>=start.y:
row=matrix[y]
for x in row:
if end.x>=x>=start.x:
data[y-start.y][x-start.x]=row[x]
toClear.append((x,y))
for coord in toClear:
del m.load.funge.plane[fng.Vect2d(*coord)]
state="Selected"
origData=data
statusText(f"Grabber: *Selected* - use *{config['PlaceKey']}* to place or *{config['DropKey']}* to drop")
elif state=="Selected":
# rotate
rotation+=1
rotation=rotation%4
if rotation==0:
data=origData
statusText(f"Grabber: *Rotated*")
return
else:
data=origData
for _ in range(rotation):
data=list(zip(*data[::-1]))
for row in range(len(data)):
data[row]=list(data[row])
functional=True
for row in range(len(data)):
for col in range(len(data[0])):
val=data[row][col]
if val is not None:
table=None
for line in bf.rotations:
if chr(val) in line:
table=line
if table:
transformed=table[(table.index(chr(val))+rotation)%4]
if transformed is None:
functional=False
else:
data[row][col]=ord(transformed)
statusText(f"Grabber: *Rotated*"+ (" Warn - Could not rotate function" if not functional else ""))
elif key==config["PlaceKey"] or key==config["DropKey"]:
if state=="Selected":
# place & drop
plane=m.load.funge.plane
for y in range(len(data)):
for x in range(len(data[0])):
if data[y][x] is not None:
plane[fng.Vect2d(cursor.cursor.x+x,cursor.cursor.y+y)]=data[y][x]
statusText(f"Grabber: *Placed*")
if key==config["DropKey"]:
state="None"
data=None
statusText(f"Grabber: *Dropped*")
elif state=="Selecting" and key==config["DropKey"]:
# clear
matrix=m.load.funge.plane.matrix
start=corner.copy()
end=cursor.cursor.copy()
if start.x>end.x:
start.x,end.x=(end.x,start.x)
if start.y>end.y:
start.y,end.y=(end.y,start.y)
toClear=[]
for y in matrix:
if end.y>=y>=start.y:
row=matrix[y]
for x in row:
if end.x>=x>=start.x:
toClear.append((x,y))
for coord in toClear:
del m.load.funge.plane[fng.Vect2d(*coord)]
state="None"
data=None
statusText("Grabber: *Erased*")
else:
statusText("Select something with *ctrl c* first!")
m.ui.addIntr(listener)