-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModPointers.py
264 lines (228 loc) · 5.67 KB
/
ModPointers.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
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
260
261
262
263
264
import Befunge as bfg
import Funge as fng
import TermIntr as ti
import TermUI as tui
funge=None
pointers=None # 0 has highest priority
focused=None
cursor=None
mvmtCallbacks=[]
def pointerMoved(pointer):
if pointer is focused:
cursor.cursor=pointer.pos
cursor.cursorDelta=pointer.delta
for cb in mvmtCallbacks:
cb(pointer)
def focus(pointer):
focused=pointer
def cursorMoved(c,d):
if focused:
focused._pos=c
focused._delta=d
class BfPointerN(bfg.BfPointer):
@property
def pos(self):
return self._pos
@pos.setter
def pos(self,value):
self._pos=value
pointerMoved(self)
@property
def delta(self):
return self._delta
@delta.setter
def delta(self,value):
self._delta=value
def pointer():
return BfPointerN(funge,fng.Vect2d(0,0),fng.Vect2d(1,0),[],[],fng.Vect2d(0,0))
def newPointer():
p=pointer()
pointers.append(p)
return p
def removePointer(p):
if focused is p:
unpossess()
if p in pointers:
pointers.remove(p)
def reorderPointer(p,i):
pointers.remove(p)
pointers.insert(i,p)
def display():
def mod(char):
char.flags|={'s','b','r'}
for pointer in pointers:
yield (pointer.pos.x,pointer.pos.y,mod)
possessCb=[]
def possess(pointer):
global focused
focused=pointer
focused._pos=cursor.cursor
focused._delta=cursor.cursorDelta
for cb in possessCb:
cb(pointer)
def unpossess():
global focused
focused=None
for cb in possessCb:
cb(None)
def pointersAt(pos):
results=[] #left has highest priority
for pointer in pointers:
if pointer.pos==pos:
results.append(pointer)
return results
spawnButton=ti.Button("spawn a pointer","S")
@spawnButton.onPress
def spawn(*_):
new=newPointer()
possess(new)
statusText(f"Spawned pointer #{pointers.index(new)}")
possessCounter=0 #so every possess gives a diff pointer if multiple on same tile
possessButton=ti.Button("un/possess a pointer\nbelow the cursor","P")
@possessButton.onPress
def cursorPossess(*_):
global focused,possessCounter
if focused:
unpossess()
statusText("Unpossessed pointer")
return
found=pointersAt(cursor.cursor)
possessCounter+=1
if found:
focused=found[possessCounter%len(found)]
statusText(f"Possessed pointer #{pointers.index(focused)}")
possess(focused)
else:
statusText("No pointer to possess")
class Stack(tui.GenElement): #todo: have a less crude thread safety solution (stack is one render behind)
def __init__(self,pointer):
self.pointer=pointer
self.stack=pointer.stack
def innards(self):
stack=self.stack
numberString=""
for n in range(len(stack)):
numberString+=f"#{n+1}\n"
numberText=tui.Text(numberString[:-1])
characterString=""
if stack:
for item in stack:
if 32<=item<=126: #ascii normal character
characterString+="\\"+chr(item)
elif item==10:
characterString+="↩" #newline
else:
characterString+="`-`"
characterString+="\n"
else:
characterString="Empty stack "
characterText=tui.Text(characterString[:-1])
valString=""
for item in stack:
valString+=f"{item}\n"
valText=tui.Text(valString)
sep=tui.Seperator("vertical",tui.lines.thin.v,style="`").pad(left=1,right=1)
return tui.HStack(
numberText,
sep,
characterText,
sep,
valText
)
def render(self,cnv,x,y,ph,pw):
self.innards().render(cnv,x,y,ph,pw)
self.stack=self.pointer.stack[:]
class Inspector(tui.GenElement):
def __init__(self,pointer,collection):
self.pointer=pointer
self.collection=collection
self.stack=Stack(pointer)
def innards(self):
alive=self.pointer in self.collection
if alive:
header=tui.Text(f"*Pointer #{self.collection.index(self.pointer)}*")
else:
header=tui.Text("*Dead pointer*")
possessed=(tui.Text("(Possessed)") if focused is self.pointer else tui.Nothing())
physical=tui.Text("At "+str(self.pointer.pos)+", moving "+str(self.pointer.delta))
stack=tui.VStack(
tui.Text("*Stack*").align(alignH="middle"),
self.stack.align(alignH="middle")
)
return tui.VStack(
header,
possessed,
physical.pad(bottom=1),
stack
)
possessedView=None
def mkInspector(pointer):
global possessedView
if pointer==None:
possessedView=None
else:
possessedView=Inspector(pointer,pointers)
possessCb.append(mkInspector)
class InspectorView(tui.GenElement):
def __init__(self):
pass
def innards(self):
showing=bool(focused)
if showing:
return possessedView
else:
return tui.Text("Possess a cursor to view info")
killButton=ti.Button("kill possessed",'K')
@killButton.onPress
def kill(*_):
if focused:
statusText(f"Pointer killed.")
removePointer(focused)
else:
statusText(f"No pointer possessed!")
clearButton=ti.Button("clear stack",'C')
@clearButton.onPress
def clear(*_):
if focused:
statusText(f"Stack cleared.")
focused.stack.clear()
else:
statusText(f"No pointer possessed!")
def modInit(modules,config,lock):
global funge,cursor,pointers,statusText
funge=modules.load.funge
pointers=funge.pointers
cursor=modules.cursor
sidebar=modules.sidebar
statusText=modules.statustext.queueText
cursor.addCallback(cursorMoved)
screen=modules.fungescreen.display
screen.markers.append(display)
# p=newPointer()
# p.stack=list(bytes("hello world!".encode("ascii")))+[1,2,4,8,16,8,4,2,1]
inspectorBar=sidebar.Sidebar(
"Pointers",
tui.VStack(
(scrollView:=tui.ScrollBox(
tui.VStack(
spawnButton,
possessButton,
killButton,
clearButton,
tui.Text("Press w and s to scroll `(w,s)`"),
InspectorView().pad(top=1),
tui.Nothing(height=10)
),
style=[False,False,False,True],
axes=[True,False]
),100)
)
,ti.Group(
killButton,
clearButton,
ti.scrollerInput(scrollView.scroller,modules.ui.root.frames,inputs=["w","s",None,None]),
spawnButton,
possessButton
)
)
sidebar.addSidebar(inspectorBar)