Last active
July 27, 2020 09:55
-
-
Save nonkit/0c5a8ccaf3d175405db41ffc58a579b4 to your computer and use it in GitHub Desktop.
Small Basic Pseudo Sprite Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Sprite_Add | |
' Sprite | add shapes to a sprite | |
' param name - sprite name | |
' param shX, shY - origin of shape array | |
' param scale - to resize | |
' param shape[] - shape array | |
' param nSprite - number of sprite | |
' return nSprite - updated number of sprite | |
' return sprite[] - sprite array | |
Stack.PushValue("local", i) | |
Stack.PushValue("local", x) | |
Stack.PushValue("local", y) | |
nSprite = nSprite + 1 | |
spr = "" | |
spr["name"] = name | |
spr["x"] = shX | |
spr["y"] = shY | |
spr["angle"] = 0 | |
spr["dir"] = 1 | |
Shapes_CalcWidthAndHeight() | |
spr["width"] = shWidth | |
spr["height"] = shHeight | |
spr["cx"] = shWidth / 2 | |
spr["cy"] = shHeight / 2 | |
If scale = "" Then | |
scale = 1 | |
EndIf | |
s = scale | |
spr["scale"] = s | |
For i = 1 To Array.GetItemCount(shape) | |
shp = shape[i] | |
GraphicsWindow.PenWidth = shp["pw"] * s | |
If shp["pw"] > 0 Then | |
GraphicsWindow.PenColor = shp["pc"] | |
EndIf | |
If Text.IsSubText("rect|ell|tri|text|btn", shp["func"]) Then | |
GraphicsWindow.BrushColor = shp["bc"] | |
EndIf | |
If Text.IsSubText("text|btn", shp["func"]) Then | |
If silverlight Then | |
fs = Math.Floor(shp["fs"] * 0.9) | |
Else | |
fs = shp["fs"] | |
EndIf | |
GraphicsWindow.FontSize = fs * s | |
GraphicsWindow.FontName = shp["fn"] | |
If shp["fb"] = "False" Then | |
GraphicsWindow.FontBold = "False" | |
Else | |
GraphicsWindow.FontBold = "True" | |
EndIf | |
EndIf | |
If shp["func"] = "rect" Then | |
shp["obj"] = Shapes.AddRectangle(shp["width"] * s, shp["height"] * s) | |
ElseIf shp["func"] = "ell" Then | |
shp["obj"] = Shapes.AddEllipse(shp["width"] * s, shp["height"] * s) | |
ElseIf shp["func"] = "tri" Then | |
shp["obj"] = Shapes.AddTriangle(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s, shp["x3"] * s, shp["y3"] * s) | |
ElseIf shp["func"] = "line" Then | |
shp["obj"] = Shapes.AddLine(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s) | |
ElseIf shp["func"] = "text" Then | |
shp["obj"] = Shapes.AddText(shp["text"]) | |
EndIf | |
x = shp["x"] | |
y = shp["y"] | |
shp["rx"] = x | |
shp["ry"] = y | |
If sbd And (shp["func"] = "line") Then | |
shp["wx"] = x | |
shp["wy"] = y | |
ElseIf silverlight And Text.IsSubText("tri|line", shp["func"]) Then | |
_alpha = Math.GetRadians(shp["angle"]) | |
SB_RotateWorkaround() | |
shp["wx"] = x | |
shp["wy"] = y | |
EndIf | |
If shp["func"] = "btn" Then | |
shp["obj"] = Controls.AddButton(shp["caption"], shX + x * s, shY + y * s) | |
Else | |
Shapes.Move(shp["obj"], shX + x * s, shY + y * s) | |
EndIf | |
If Text.IsSubText("rect|ell|tri|text", shp["func"]) And (shp["angle"] <> 0) And (shp["angle"] <> "") Then | |
Shapes.Rotate(shp["obj"], shp["angle"]) | |
EndIf | |
shape[i] = shp | |
EndFor | |
spr["shape"] = shape | |
sprite[nSprite] = spr | |
y = Stack.PopValue("local") | |
x = Stack.PopValue("local") | |
i = Stack.PopValue("local") | |
EndSub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Sprite_Flip | |
' Sprite | flip a sprite | |
' param sprite[i] - sprite to flip | |
' param angle - to flip | |
' return sprite[i] - flipped sprite | |
Stack.PushValue("local", angle) | |
spr = sprite[i] | |
gx = spr["x"] | |
gy = spr["y"] | |
shape = spr["shape"] | |
n = Array.GetItemCount(shape) | |
angle = Math.Remainder(angle, 360) | |
If angle < 0 Then | |
angle = angle + 360 | |
EndIf | |
If (angle <= 90) Or (270 < angle) Then | |
sign = 1 | |
Else | |
sign = -1 | |
EndIf | |
If (spr["flip"] <= 90) Or (270 < spr["flip"]) Then | |
lastSign = 1 | |
Else | |
lastSign = -1 | |
EndIf | |
_a = Math.GetRadians(angle) | |
scaleX = Math.Cos(_a) | |
For j = 1 To n | |
shp = shape[j] | |
Shapes.Zoom(shp["obj"], Math.Abs(scaleX), 1) | |
cx = shp["x"] + shp["width"] / 2 | |
fx = (cx - spr["cx"]) * scaleX + spr["cx"] | |
Shapes.Move(shp["obj"], fx - shp["width"] / 2 + gx, shp["y"] + gy) | |
If sign <> lastSign Then | |
If shp["angle"] <> "" Then | |
shp["angle"] = -shp["angle"] | |
Shapes.Rotate(shp["obj"], shp["angle"]) | |
EndIf | |
_x = Math.Floor((cx - shp["width"] / 2) * 100) / 100 | |
shp["rx"] = _x | |
shp["x"] = _x | |
shape[j] = shp | |
EndIf | |
EndFor | |
spr["flip"] = angle | |
spr["shape"] = shape | |
sprite[i] = spr | |
angle = Stack.PopValue("local") | |
EndSub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Sprite_GetIndexOf | |
' Sprite | get index of a sprite | |
' param name - a sprite name | |
' return i - index of the sprite | |
i = 0 ' not found | |
For _i = 1 To nSprite | |
spr = sprite[_i] | |
If spr["name"] = name Then | |
i = _i | |
_i = nSprite ' exit For | |
EndIf | |
EndFor | |
EndSub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Sprite_Move | |
' Sprite | move a sprite | |
' param sprite[i] - sprite to move | |
' param x, y - position to move | |
' return sprite[i] - updated sprite | |
Stack.PushValue("local", j) | |
spr = sprite[i] | |
s = spr["scale"] | |
spr["x"] = x | |
spr["y"] = y | |
shape = spr["shape"] | |
n = Array.GetItemCount(shape) | |
For j = 1 To n | |
shp = shape[j] | |
If sbd And (shp["func"] = "line") Then | |
_x = shp["wx"] | |
_y = shp["wy"] | |
ElseIf silverlight And Text.IsSubText("tri|line", shp["func"]) Then | |
_x = shp["wx"] | |
_y = shp["wy"] | |
Else | |
_x = shp["rx"] | |
_y = shp["ry"] | |
EndIf | |
Shapes.Move(shp["obj"], spr["x"] + _x * s, spr["y"] + _y * s) | |
EndFor | |
sprite[i] = spr | |
j = Stack.PopValue("local") | |
EndSub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Sprite_Remove | |
' Sprite | remove a sprite | |
' param sprite[i] - sprite to remove | |
' return nSprite - updated number of sprites | |
If (0 < i) And (i <= nSprite) Then | |
spr = sprite[i] | |
shape = spr["shape"] | |
For _i = 1 To Array.GetItemCount(shape) | |
Shapes.Remove(shape[_i]["obj"]) | |
EndFor | |
For _i = i + 1 To nSprite | |
sprite[_i - 1] = sprite[_i] | |
EndFor | |
nSprite = nSprite - 1 | |
EndIf | |
EndSub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Sprite_Rotate | |
' Sprite | rotate a sprite | |
' param sprite[i] - sprite to rotate | |
' param cx, cy - rotation center (if given) | |
' param angle - to rotate | |
Stack.PushValue("local", x) | |
Stack.PushValue("local", y) | |
Stack.PushValue("local", n) | |
spr = sprite[i] | |
shape = spr["shape"] | |
moved = "False" | |
If cx <> "" Then | |
moved = "True" | |
Else | |
cx = "" ' to avoid syntax error | |
EndIf | |
If cy <> "" Then | |
moved = "True" | |
Else | |
cy = "" ' to avoid syntax error | |
EndIf | |
s = spr["scale"] | |
If moved Then | |
param["cx"] = (cx - spr["x"]) / s | |
param["cy"] = (cy - spr["y"]) / s | |
Else | |
param["cx"] = spr["width"] / 2 | |
param["cy"] = spr["height"] / 2 | |
EndIf | |
param["scale"] = 1 | |
spr["angle"] = angle | |
param["angle"] = spr["angle"] | |
n = Array.GetItemCount(shape) | |
Stack.PushValue("local", i) | |
For i = 1 To n | |
shp = shape[i] | |
param["x"] = shp["x"] | |
param["y"] = shp["y"] | |
param["width"] = shp["width"] | |
param["height"] = shp["height"] | |
Shapes_CalcRotateZoomPos() | |
shp["rx"] = x | |
shp["ry"] = y | |
alpha = shp["angle"] + spr["angle"] | |
If sbd And (shp["func"] = "line") And (alpha <> 0) Then | |
x1 = shp["x1"] | |
y1 = shp["y1"] | |
x2 = shp["x2"] | |
y2 = shp["y2"] | |
pw = shp["pw"] | |
SB_LineWorkaround() | |
shp["wx"] = x | |
shp["wy"] = y | |
ElseIf silverlight And Text.IsSubText("tri|line", shp["func"]) Then | |
_alpha = Math.GetRadians(alpha) | |
SB_RotateWorkAround() | |
shp["wx"] = x | |
shp["wy"] = y | |
EndIf | |
Shapes.Move(shp["obj"], spr["x"] + x * s, spr["y"] + y * s) | |
Shapes.Rotate(shp["obj"], shp["angle"] + spr["angle"]) | |
shape[i] = shp | |
EndFor | |
i = Stack.PopValue("local") | |
spr["shape"] = shape | |
sprite[i] = spr | |
n = Stack.PopValue("local") | |
y = Stack.PopValue("local") | |
x = Stack.PopValue("local") | |
EndSub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Sprite_Zoom | |
' Sprite | zoom a sprite | |
' param sprite[i] - sprite to zoom | |
' param scaleX - to zoom | |
' param scaleY - to zoom | |
' return sprite[i] - zoomed sprite | |
Stack.PushValue("local", angle) | |
spr = sprite[i] | |
gx = spr["x"] | |
gy = spr["y"] | |
shape = spr["shape"] | |
n = Array.GetItemCount(shape) | |
For j = 1 To n | |
shp = shape[j] | |
Shapes.Zoom(shp["obj"], scaleX, scaleY) | |
cx = shp["x"] + shp["width"] / 2 | |
cy = shp["y"] + shp["height"] / 2 | |
fx = (cx - spr["cx"]) * scaleX + spr["cx"] | |
fy = (cy - spr["cy"]) * scaleY + spr["cy"] | |
Shapes.Move(shp["obj"], fx - shp["width"] / 2 + gx, fy - shp["height"] / 2 + gy) | |
EndFor | |
spr["scaleX"] = scaleX | |
spr["scaleY"] = scaleY | |
spr["shape"] = shape | |
sprite[i] = spr | |
angle = Stack.PopValue("local") | |
EndSub |
Pseudo Group object has renamed to pseudo Sprite object.
Consider to support https://github.com/nonkit/Form/blob/master/Form.frm.sb.
Two comments for Sprite_Flip()
subroutine:
spr["flip"]
should be removed.spr["scaleX"]
andspr["scaleY"]
(=1) should be used instead ofspr["flip"]
.spr["scale"]
should be considered.
Sprite_Rotate()
doesn't rotate
- shapes correctly when scale <> 1.
- a line correctly when the line is slant.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Subroutine
Sprite_Rotate()
has a problem when rotating 90 degrees with lines in the sprite.