-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModCursor.py
90 lines (78 loc) · 1.87 KB
/
ModCursor.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
import TermUI as tui
import TermIntr as ti
import TermCanvas as tc
import Befunge as bfg
import Funge as fng
cursor=fng.Vect2d(0,0)
cursorDelta=fng.Vect2d(1,0)
instrs=bfg.befunge2d
def updateInfo(delta,coords):
statusText.setLingering(f"At ({coords.x},{coords.y}) = {plane[coords]}, moving ({delta.x},{delta.y})")
try:
statusText.queueText("\033"+instrs[plane[coords]].description+"\033")
except KeyError:
statusText.clear()
def goto(c,d):
global cursorDelta, cursor
cursorDelta=d
cursor=c
updateInfo(cursorDelta,cursor)
for callback in callbacks:
callback(c,d)
def step(certain=False):
places=list(instrs[plane[cursor]].transforms(cursorDelta.copy(),cursor.copy(),plane))
if len(places)==0:
return True
if len(places)>1 and certain:
return True
d,c=places[0]
moved=c!=cursor
goto(c,d)
return moved
movement=ti.Listener()
@movement.handle
def key(key):
global cursor
if key==cfg["Step"]:
step(certain=True)
root.frames.schedule(
1,tui.sched.framesLater
)
return
elif key==cfg["Up"]:
d=fng.Vect2d(0,-1)
elif key==cfg["Down"]:
d=fng.Vect2d(0,1)
elif key==cfg["Left"]:
d=fng.Vect2d(-1,0)
elif key==cfg["Right"]:
d=fng.Vect2d(1,0)
else:
return
root.frames.schedule(
1,tui.sched.framesLater
)
c=d+cursor
goto(c,d)
callbacks=[]
def addCallback(cb):
global callbacks
callbacks.append(cb)
def removeCallback(cb):
callbacks.remove(cb)
def cursorCenter(x,y,ph,pw): #align canvas so cursor is in middle
return (cursor.x-(pw//2),cursor.y-(ph//2))
def cursorMarker():
def cursorMod(char):
char.flags|={'r'}
yield (cursor.x,cursor.y,cursorMod)
def modInit(m,config,lock):
global modules,statusText,plane,cfg,root
root=m.ui.root
cfg=config
modules=m
plane=modules.load.funge.plane
statusText=modules.statustext
modules.fungescreen.display.where=cursorCenter
modules.fungescreen.display.markers.append(cursorMarker)
modules.ui.addIntr(movement)