Created
April 26, 2024 13:47
-
-
Save treytomes/eb55a2d265bbaae29a0829ef6ea56514 to your computer and use it in GitHub Desktop.
Building a simple scene graph, part 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "events" | |
import "json" | |
import "listUtil" | |
import "qa" | |
import "stringUtil" | |
import "tc" | |
import "textUtil" | |
Shape = {} | |
Shape.make = function(x, y) | |
self.className = "Shape" | |
self.x = x | |
self.y = y | |
end function | |
Shape.fromJSON = function(obj) | |
return (new Shape).make(obj.x, obj.y) | |
end function | |
Shape.toJSON = function | |
return { | |
"class": self.className, | |
"x": self.x, | |
"y": self.y, | |
} | |
end function | |
Shape.moveTo = function(x, y) | |
self.x = x | |
self.y = y | |
end function | |
Shape.draw = function; end function | |
Ellipse = new Shape | |
Ellipse.make = function(x, y, width, height, color=null, penSize=1) | |
self.className = "Ellipse" | |
self.width = width | |
self.height = height | |
self.penSize = penSize | |
if color == null then color == gfx.color | |
self.color = color | |
self.x = x | |
self.y = y | |
return self | |
end function | |
Ellipse.fromJSON = function(obj) | |
return (new Ellipse).make(obj.x, obj.y, obj.width, obj.height, obj.color, obj.penSize) | |
end function | |
Ellipse.toJSON = function | |
return { | |
"class": self.className, | |
"x": self.x, | |
"y": self.y, | |
"width": self.width, | |
"height": self.height, | |
"color": self.color, | |
"penSize": self.penSize, | |
} | |
end function | |
Ellipse.draw = function | |
left = self.x - self.width / 2 | |
bottom = self.y - self.height / 2 | |
gfx.drawEllipse left, bottom, self.width, self.height, self.color, self.penSize | |
end function | |
Circle = new Shape | |
Circle.make = function(x, y, radius, color=null, penSize=1) | |
self.className = "Circle" | |
self.x = x | |
self.y = y | |
self.radius = radius | |
if color == null then color == gfx.color | |
self.color = color | |
self.penSize = penSize | |
return self | |
end function | |
Circle.fromJSON = function(obj) | |
return (new Circle).make(obj.x, obj.y, obj.radius, obj.color, obj.penSize) | |
end function | |
Circle.toJSON = function | |
return { | |
"class": self.className, | |
"x": self.x, | |
"y": self.y, | |
"radius": self.radius, | |
"color": self.color, | |
"penSize": self.penSize, | |
} | |
end function | |
Circle.draw = function | |
diameter = self.radius * 2 | |
left = self.x - self.radius | |
bottom = self.y - self.radius | |
gfx.drawEllipse left, bottom, diameter, diameter, self.color, 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 | |
openFile = function | |
d = textUtil.FileDialog.make | |
hit = d.show | |
if hit == d.okBtn then | |
globals.filename = d.selection | |
globals.scene = loadState(filename) | |
end if | |
end function | |
events.eventLoop.onKey["o"] = function | |
openFile | |
end function | |
events.eventLoop.onUpdate = function | |
tc.flip | |
gfx.clear | |
for obj in scene | |
obj.draw | |
end for | |
if mouse.button then | |
buttons = [globals.saveButton, globals.openButton] | |
btnHit = null | |
for btn in buttons | |
if not btn.visible or not btn.contains(mouse) then continue | |
if btn.trackHit then | |
btnHit = btn | |
break | |
end if | |
end for | |
if btnHit == globals.openButton then | |
openFile | |
else if btnHit == globals.saveButton then | |
print "Save" | |
end if | |
end if | |
// circle.moveTo mouse.x, mouse.y | |
// scrollDelta = key.axis("Mouse ScrollWheel") | |
// if scrollDelta != 0 then circle.radius = circle.radius + scrollDelta | |
end function | |
text.clear | |
clear | |
filename = null | |
scene = [] | |
// 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] | |
text.color = "#CCCCCC" // (button color) | |
saveButton = new textUtil.DialogButton | |
saveButton.caption = "Save" | |
saveButton.visible = true | |
saveButton.key = "S" | |
saveButton.x = 0 | |
saveButton.y = 25 | |
saveButton.draw | |
openButton = new textUtil.DialogButton | |
openButton.caption = "Open" | |
openButton.visible = true | |
openButton.key = "O" | |
openButton.x = saveButton.x + saveButton.width | |
openButton.y = 25 | |
openButton.draw | |
events.eventLoop.run | |
if filename != null then saveState scene, filename | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment