-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.jl
More file actions
181 lines (160 loc) · 5.37 KB
/
test.jl
File metadata and controls
181 lines (160 loc) · 5.37 KB
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
using AutumnInterpreter: Interpreter, run_script, step, click, left, right, up, down,
render_all
using JSON, CxxWrap, PlotlyJS, WebIO, Observables, JSExpr
# ------------------------------------------------------------------
const CELL_SIZE = 1.0
const MARGIN = 0.05
const COLOR_MAP = Dict(
"gray"=>"gray","gold"=>"gold","green"=>"green","mediumpurple"=>"mediumpurple",
"purple"=>"purple","white"=>"white","yellow"=>"yellow","blue"=>"blue",
"red"=>"red","orange"=>"orange")
parse_grid(out::CxxWrap.StdLib.StdStringAllocated) = let d=JSON.parse(String(out)); s=pop!(d,"GRID_SIZE",0); (d,s) end
parse_grid(out::AbstractString) = let d=JSON.parse(out); s=pop!(d,"GRID_SIZE",0); (d,s) end
rectangle_trace(x0,y0;color="black",outline=true) = scatter(
x=[x0,x0+CELL_SIZE,x0+CELL_SIZE,x0,x0],
y=[y0,y0,y0+CELL_SIZE,y0+CELL_SIZE,y0],
mode="lines",fill="toself",fillcolor=color,
line=attr(color=outline ? "white" : color,width=1),hoverinfo="none")
mutable struct GridRenderer
interpreter::Interpreter
grid::Dict{String,Any}
gridsize::Int
plot::Union{PlotlyJS.SyncPlot,Nothing}
masking::Bool
masked::Set{Tuple{Int,Int}}
frame::Int
history::Vector{Dict}
running::Bool
lasttick::Float64
function GridRenderer(interp)
new(interp,Dict(),0,nothing,false,Set{Tuple{Int,Int}}(),0,Dict[],true,time())
end
end
build_traces(grid,n) = begin
tr = GenericTrace[]
for objs in values(grid), obj in objs
col=obj["position"]["x"]; row=obj["position"]["y"]
key=lowercase(obj["color"])
x0 = MARGIN + col*(CELL_SIZE+MARGIN)
y0 = MARGIN + (n-1-row)*(CELL_SIZE+MARGIN)
push!(tr, rectangle_trace(x0,y0;color=get(COLOR_MAP,key,key)))
end
tr
end
function render!(renderer::GridRenderer)
render_output = render_all(renderer.interpreter)
grid, gridsize = parse_grid(render_output)
renderer.grid = grid
renderer.gridsize = gridsize
# Build traces using the existing build_traces function
traces = build_traces(grid, gridsize)
# Add frame counter trace
push!(traces, scatter(
x=[0.02 * gridsize],
y=[0.98 * gridsize],
text=["Frame: $(renderer.frame)"],
mode="text",
textfont=attr(color="white", size=10),
hoverinfo="none"
))
# Add masked cells traces
for (col, row) in renderer.masked
x0 = MARGIN + col*(CELL_SIZE+MARGIN)
y0 = MARGIN + (gridsize-1-row)*(CELL_SIZE+MARGIN)
push!(traces, rectangle_trace(x0, y0, color="gray", outline=true))
end
layout = Layout(
width=500,
height=500,
plot_bgcolor="black",
paper_bgcolor="black",
showlegend=false,
xaxis=attr(
range=[-2, gridsize + 2],
showgrid=false,
zeroline=false,
showline=false,
showticklabels=false
),
yaxis=attr(
range=[-2, gridsize + 2],
showgrid=false,
zeroline=false,
showline=false,
showticklabels=false,
scaleanchor="x"
),
margin=attr(l=0, r=0, t=0, b=0)
)
if isnothing(renderer.plot)
# First time rendering - create new plot
plt = PlotlyJS.plot(traces, layout)
# Set up event listeners only once
onimport(plt.scope, @js function(node)
window.addEventListener("keydown", (e) -> WebIO.dispatch(node, "keydown", e.key))
end)
display(plt)
renderer.plot = plt
else
# Update existing plot
PlotlyJS.react!(renderer.plot, traces, layout)
end
end
function handle_click!(r::GridRenderer, x, y)
col = Int(floor((x-MARGIN)/(CELL_SIZE+MARGIN)))
row = Int(floor((r.gridsize - y - MARGIN)/(CELL_SIZE+MARGIN)))
(0<=col<r.gridsize && 0<=row<r.gridsize) || return
if r.masking
coord = (col,row)
if in(coord, r.masked)
delete!(r.masked, coord)
else
push!(r.masked, coord)
end
else
click(r.interpreter, col, row)
step(r.interpreter)
end
store_frame!(r)
render!(r)
end
function handle_key!(r::GridRenderer, key::String)
if key == "ArrowUp"
up(r.interpreter)
elseif key == "ArrowDown"
down(r.interpreter)
elseif key == "ArrowRight"
right(r.interpreter)
elseif key == "ArrowLeft"
left(r.interpreter)
elseif key == "m"
r.masking = !r.masking
println("Masking mode: ", r.masking ? "enabled" : "disabled")
elseif key == "c"
empty!(r.masked)
end
if key in ["ArrowUp", "ArrowDown", "ArrowRight", "ArrowLeft"]
step(r.interpreter)
store_frame!(r)
end
render!(r)
end
store_frame!(r::GridRenderer) = (push!(r.history,Dict("grid"=>deepcopy(r.grid),"masked"=>collect(r.masked))); r.frame+=1)
autorefresh(r::GridRenderer;dt=0.3) = while true
if r.running && (time()-r.lasttick)>=dt
step(r.interpreter); store_frame!(r); render!(r); r.lasttick=time()
end
sleep(0.01)
end
function main()
interp = Interpreter()
script = length(ARGS)>0 ? ARGS[1] : "./tests/ants.sexp"
run_script(interp, read(script,String), "", "")
r = GridRenderer(interp)
render!(r)
println("Controls: click | arrows | m mask | c clear | p pause | s save frames")
autorefresh(r)
end
if abspath(PROGRAM_FILE)==@__FILE__
main()
end