Skip to content

Instantly share code, notes, and snippets.

@nedzadarek
Created October 26, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nedzadarek/836adb9fc20c1e2cb1dce6436133a4ab to your computer and use it in GitHub Desktop.
Save nedzadarek/836adb9fc20c1e2cb1dce6436133a4ab to your computer and use it in GitHub Desktop.
Uses `el1/type` instead of index `face/draw/1` for changing draw elements
Red [
author: "Nędza Darek"
version: 0.0.1
license: {
use/edit everywhere
post link to the gist/github
no waranties
}
]
get-draw-element: function [draw] [
parse/part
draw
[
copy set-words any [set-word!]
values-position:
copy values any [any-type!](
; if (length? set-words) = (length? values) [ ]
)
]
find draw (to-set-word 'end)
body: copy [elements:]
append/only body values-position
if (length? set-words) = (length? values) [
repeat i-th (length? values) [
; a: _a/1
append body set-words/:i-th
append body either word? v: values-position/:i-th [to-lit-word v] [v]
; _a: is [elements/( (index? values-position) + index? find elements a): a]
append body to-set-word rejoin ["_" set-words/:i-th]
append body 'is
append/only body copy []
l: back tail body
l: l/1
append l reduce [
to-set-path reduce [
'elements
i-th
]
to-word set-words/:i-th
]
]
]
return make deep-reactor! body
]
comment {
syntax:
<set-word!> <set-word!> ...
<draw-element> <draw-element> ...
end:
<rest of draw>
Where number of <set-word!> must match number of <draw-element>
Parses only one draw element till the `end:` (e.g `box 2x2 3x3`)
<rest of draw> - normal draw code - unchanged
The syntax is made to be easily parsed. Few considerations:
- parse many elements - you just run `get-draw-element` many times with different draw's position in some cases
- set-words between element's arguments (e.g. `b1: box pos: 2x2 end-pos: 3x3`) - not possible with current version of the Red;
- do not use arguments' names - you have to build library of draw elements and their arguments - doable
}
draw-block: [
type: start-position: end-position:
box 20x20 40x40
end:
ellipse 10x10 12x32
]
view [
base draw draw-block on-up [
el1: get-draw-element face/draw
el1/type: random/only [box ellipse line] ; change, randomly, element's type
face/draw: face/draw ; trigger the reactions - to update the draw
? el1/type ; print element's type
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment