Author Topic: Type functions (another brick in the wall).  (Read 1597 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: TMyBox


CX = 1 : CY = 2 : WIDTH = 3 : HEIGHT = 4 : COL = 5 : MODE = 6

dim field$(6)

TMyBox$ = "0#0#0#0#0,0,0#0" // Prototype with default values


sub 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 sub


sub set$()
    return str$(cx) + "#" + str$(cy) + "#" + str$(width) + "#" + str$(height) + "#" + col$ + "#" + str$(mode)
end sub


sub 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 if
end sub


sub move$(item$, x, y)
   
    get(item$)
   
    cx = x : cy = y
   
    return set$()
end sub


sub resize$(item$, w, h)

    get(item$)
   
    width = w : height = h
   
    return set$()
end sub


sub colorize$(item$, c$, m)

    get(item$)
   
    col$ = c$
    if numparams = 3 mode = m
   
    return set$()
end sub


// ========== Test ==========


open window 640,480

myBox$ = TMyBox$

myBox$ = move$(myBox$, 10, 10)
myBox$ = resize$(myBox$, 100, 100)
draw(myBox$)
pause 2
myBox$ = move$(myBox$, 100, 100)
clear window
draw(myBox$)
pause 2
myBox$ = colorize$(resize$(myBox$, 200, 200), "255, 0, 0", 1)
clear window
draw(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