Skip to content

Commit 3b54fb3

Browse files
Readme.
1 parent 4e396ae commit 3b54fb3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.org

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,35 @@ This can be used, for example, to draw a static sketch and then disable the draw
549549

550550
Example: [[https://github.com/vydd/sketch/blob/master/examples/control-flow.lisp][control-flow.lisp]].
551551

552+
*** Sharing behaviour between sketches
553+
Let's say you want all your sketches to have a black background, but you don't want to have to reimplement that feature every time. The answer is to use `defsketchx`, which is like `defsketch` but allows you to specify extra superclasses for your sketch class.
554+
555+
First, define a mixin class that implements the desired behaviour in the `draw` method (with the `:before` method specialiser):
556+
557+
#+BEGIN_SRC lisp
558+
(defclass black-background () ())
559+
(defmethod draw :before ((instance black-background) &key &allow-other-keys)
560+
(background +black+))
561+
#+END_SRC
562+
563+
Then define your sketches using `defsketchx`:
564+
565+
#+BEGIN_SRC lisp
566+
(defsketchx moon (black-background)
567+
((width 200)
568+
(height 200))
569+
(circle 100 100 50))
570+
#+END_SRC
571+
572+
And it should have a black background. Initialization/setup logic for `black-background`, and sketches inheriting from it, could be put in an `initialize-instance` or `setup` method:
573+
574+
#+BEGIN_SRC lisp
575+
(defmethod initialize-instance :before ((instance black-background) &key &allow-other-keys)
576+
(format t "Initializing black background!"))
577+
(defmethod setup :before ((instance black-background) &key &allow-other-keys)
578+
(format t "Setting up black background!"))
579+
#+END_SRC
580+
552581
** Made with Sketch
553582
- [[https://vydd.itch.io/qelt][QELT]]
554583
- [[https://github.com/sjl/coding-math][sjl's implementation of coding math videos]]

0 commit comments

Comments
 (0)