### Author Topic: Type functions (another brick in the wall).  (Read 935 times)

#### Galileo

• Guest
##### Type functions (another brick in the wall).
« on: February 07, 2019, 06:36:25 pm »
A year ago I presented an example of how to simulate type functions with Yabasic. Now I show a much simpler way.

Code: [Select]
`// Yabasic 2.78, by Galileo, 02/2019// Complex type and functions type// Type defined: TMyBoxCX = 1 : CY = 2 : WIDTH = 3 : HEIGHT = 4 : COL = 5 : MODE = 6dim field\$(6)TMyBox\$ = "0#0#0#0#0,0,0#0" // Prototype with default valuessub get(item\$)    local void        void = token(item\$, field\$(), "#")        cx = val(field\$(CX))    cy = val(field\$(CY))    width = val(field\$(WIDTH))    height = val(field\$(HEIGHT))    col\$ = field\$(COL)    mode = val(field\$(MODE))end subsub set\$()    return str\$(cx) + "#" + str\$(cy) + "#" + str\$(width) + "#" + str\$(height) + "#" + col\$ + "#" + str\$(mode)end subsub draw(item\$)        get(item\$)        color col\$    if mode then        fill box cx, cy, cx + width, cy + height    else        box cx, cy, cx + width, cy + height    end ifend subsub move\$(item\$, x, y)        get(item\$)        cx = x : cy = y        return set\$()end subsub resize\$(item\$, w, h)    get(item\$)        width = w : height = h        return set\$()end subsub colorize\$(item\$, c\$, m)    get(item\$)        col\$ = c\$    if numparams = 3 mode = m        return set\$()end sub// ========== Test ==========open window 640,480myBox\$ = TMyBox\$myBox\$ = move\$(myBox\$, 10, 10)myBox\$ = resize\$(myBox\$, 100, 100)draw(myBox\$)pause 2myBox\$ = move\$(myBox\$, 100, 100)clear windowdraw(myBox\$)pause 2myBox\$ = colorize\$(resize\$(myBox\$, 200, 200), "255, 0, 0", 1)clear windowdraw(myBox\$)drawOtherBox()anotherBox\$ = colorize\$(resize\$(move\$(TMyBox\$, 10, 100), 50, 50), "0, 255, 0", 1)draw(anotherBox\$)sub drawOtherBox()    local otherBox\$        otherBox\$ = move\$(TMyBox\$, 100, 10)    otherBox\$ = colorize\$(resize\$(otherBox\$, 50, 50), "0, 0, 255", 1)    draw(otherBox\$)end sub`