Skip to content

Instantly share code, notes, and snippets.

@treytomes
Created April 18, 2024 18:41
Show Gist options
  • Save treytomes/d05ab757462b8d4887651733c4b7a0c7 to your computer and use it in GitHub Desktop.
Save treytomes/d05ab757462b8d4887651733c4b7a0c7 to your computer and use it in GitHub Desktop.
Building a simple scene graph, part 2.
import "events"
import "json"
import "stringUtil"
import "tc"
Ellipse = {}
Ellipse.make = function(x, y, width, height, color=null, penSize=1)
if color == null then color == gfx.color
self.className = "Ellipse"
self.left = x - width / 2
self.bottom = y - height / 2
self.width = width
self.height = height
self.color = color
self.penSize = penSize
return self
end function
Ellipse.fromJSON = function(obj)
return (new Ellipse).make(obj.left, obj.bottom, obj.width, obj.height, obj.color, obj.penSize)
end function
Ellipse.draw = function
gfx.drawEllipse self.left, self.bottom, self.width, self.height, self.color, self.penSize
end function
Ellipse.moveTo = function(x, y)
self.left = x - self.width / 2
self.bottom = y - self.height / 2
end function
Ellipse.toJSON = function
return {
"class": self.className,
"left": self.left,
"bottom": self.bottom,
"width": self.width,
"height": self.height,
"color": self.color,
"penSize": self.penSize,
}
end function
Circle = new Ellipse
Circle.make = function(x, y, radius, color=null, penSize=1)
self.className = "Circle"
diameter = radius * 2
Ellipse.make(x, y, diameter, diameter, color, penSize)
return self
end function
Circle.fromJSON = function(obj)
return (new Circle).make(obj.left, obj.bottom, obj.radius, obj.color, obj.penSize)
end function
Circle.getRadius = function
return self.width / 2
end function
Circle.setRadius = function(r)
self.width = r * 2
self.height = r * 2
end function
Circle.toJSON = function
return {
"class": self.className,
"left": self.left,
"bottom": self.bottom,
"radius": self.getRadius,
"color": self.color,
"penSize": self.penSize,
}
end function
saveState = function(scene, path)
objects = []
for obj in scene
objects.push obj.toJSON
end for
text = json.toJSON(objects)
file.writeLines path, [text]
end function
loadState = function(path)
objects = json.parse(file.open(path).read)
scene = []
for obj in objects
if obj.class == "Ellipse" then
scene.push Ellipse.fromJSON(obj)
else if obj.class == "Circle" then
scene.push Circle.fromJSON(obj)
end if
end for
return scene
end function
events.eventLoop.onKey["escape"] = function
events.eventLoop.running = false
end function
events.eventLoop.onUpdate = function
tc.flip
gfx.clear
for obj in scene
obj.draw
end for
circle.moveTo mouse.x, mouse.y
scrollDelta = key.axis("Mouse ScrollWheel")
circle.setRadius circle.getRadius + scrollDelta
end function
clear
if file.exists("scene.json") then
scene = loadState("scene.json")
else
scene = [
(new Ellipse).make(200, 200, 400, 150, color.red, 3),
(new Circle).make(100, 300, 40, color.blue, 2),
]
end if
circle = scene[1]
events.eventLoop.run
saveState scene, "scene.json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment