A slightly more complicated example. A number of changes and generalisations are made.
MyBox.yab library:
// Yabasic 2.78, by Galileo, 02/2018
// Complex type and functions type
// Type defined: MyBox
items = 1
type() // Initialize array of type objects
sub type() // Complex type MyCircle
dim used(items) // if true the item is in use. if false the item is free.
dim cx(items) // Coordenate x of the left-top corner of the box
dim cy(items) // Coordenate y of the left-top corner of the box
dim width(items) // Width of the box
dim height(items) // Height of the box
dim col$(items) // Box colour in mode "red, green, blue" (0 to 255 each of)
dim mode(items) // Fill mode (true or false)
end sub
sub copy(dest, orig)
cx(dest) = cx(orig)
cy(dest) = cy(orig)
width(dest) = width(orig)
height(dest) = height(orig)
col$(dest) = col$(orig)
mode(dest) = mode(orig)
end sub
sub create(parent, cx, cy, width, height, col$, mode)
local item
for item = 1 to items
if not used(item) break
next item
if item > items then
items = item
type()
end if
used(item) = true
if parent then
copy(item, parent)
else
cx(item) = cx
cy(item) = cy
width(item) = width
height(item) = height
if col$ = "" col$ = "0, 0, 0"
col$(item) = col$
mode(item) = mode
end if
redrawAll()
return item
end sub
sub destroy(item)
used(item) = false
redrawAll()
end sub
sub draw(item)
color col$(item)
if mode(item) then
fill box cx(item), cy(item), cx(item) + width(item), cy(item) + height(item)
else
box cx(item), cy(item), cx(item) + width(item), cy(item) + height(item)
end if
end sub
sub move(item, cx, cy)
cx(item) = cx : cy(item) = cy
redrawAll()
end sub
sub resize(item, width, height)
width(item) = width
height(item) = height
redrawAll()
end sub
sub colorize(item, col$, mode)
col$(item) = col$
if numparams = 3 mode(item) = mode
redrawAll()
end sub
sub redraw()
local n
for n = 1 to items
if used(n) draw(n)
next n
end sub
MyCircle2.yab library:
// Yabasic 2.78, by Galileo, 02/2018
// Complex type and functions type
// Type defined: MyCircle2
items = 1
type() // Initialize array of type objects
sub type() // Complex type MyCircle
dim used(items) // if true the item is in use. if false the item is free.
dim cx(items) // Coordenate x of the center
dim cy(items) // Coordenate y of the center
dim radius(items) // Circle radius
dim col$(items) // Circle colour in mode "red, green, blue" (0 to 255 each of)
dim mode(items) // Fill mode (true or false)
end sub
sub copy(dest, orig)
cx(dest) = cx(orig)
cy(dest) = cy(orig)
radius(dest) = radius(orig)
col$(dest) = col$(orig)
mode(dest) = mode(orig)
end sub
sub create(parent, cx, cy, radius, col$, mode)
local item
for item = 1 to items
if not used(item) break
next item
if item > items then
items = item
type()
end if
used(item) = true
if parent then
copy(item, parent)
else
cx(item) = cx
cy(item) = cy
radius(item) = radius
if col$ = "" col$ = "0, 0, 0"
col$(item) = col$
mode(item) = mode
end if
redrawAll()
return item
end sub
sub destroy(item)
used(item) = false
redrawAll()
end sub
sub draw(item)
color col$(item)
if mode(item) then
fill circle cx(item), cy(item), radius(item)
else
circle cx(item), cy(item), radius(item)
end if
end sub
sub move(item, cx, cy)
cx(item) = cx : cy(item) = cy
redrawAll()
end sub
sub resize(item, radius)
radius(item) = radius
redrawAll()
end sub
sub colorize(item, col$, mode)
col$(item) = col$
if numparams = 3 mode(item) = mode
redrawAll()
end sub
sub redraw()
local n
for n = 1 to items
if used(n) draw(n)
next n
end sub
General library TShape.yab:
// Yabasic 2.78, by Galileo, 02/2018
// Complex type and functions type
// General library: TShape
import MyBox
import MyCircle2
export sub redrawAll() // common subroutine for redraw all shapes
clear window
MyCircle2.redraw()
MyBox.redraw()
end sub
Main code (you can name it as you wish):
// Yabasic 2.78, by Galileo, 02/2018
// Complex type and functions type
// Test of complex types MyCircle2 and MyBox
import TShape
open window 640, 480
backcolor 255, 255, 255
clear window
circle1 = MyCircle2.create(0, 100, 100, 25) // create a circle in coords cx, cy with radius = 25. By default, color "0,0,0" (black) and no filler mode
box1 = MyBox.create(0, 200, 200, 100, 50) // create a box in coords 200, 200 with width = 100 and height = 50. By default, color "0,0,0" (black) and no filler mode
print "Identification of item circle = ", circle1
print "Identification of item box = ", box1
pause 1
MyCircle2.move(circle1, 200, 200)
pause 1
MyCircle2.resize(circle1, 50)
pause 1
MyCircle2.colorize(circle1, "255, 0, 0", true)
pause 1
MyCircle2.destroy(circle1)
pause 1
circle2 = MyCircle2.create(0, 300, 200, 75, "0, 0, 255", true)
print "Identification of item circle = ", circle2 // "reuse" the identificator
pause 1
circle3 = MyCircle2.create(circle2) // create a new object with attributes of other (is a copy)
print "Identification of item circle = ", circle3 // new identificator
MyCircle2.move(circle3, 100, 100) // Hey! Where is my first circle?
pause 1
MyCircle2.colorize(circle3, "0, 255, 0") // So I can already tell them apart.
MyBox.colorize(box1, "255, 0, 0")