' Shapes Editor 0.4
' Copyright (c) 2012 Nonki Takahashi. All rights reserved.
'
' History :
' 0.4 2012/09/09 Changed to add function of selecting shapes. (XFZ657-2)
' 0.3 2012/09/08 Changed to output shape array. (XFZ657-1)
' 0.2 2012/09/07 Bug fixed. Supported offset scale, and angle of shapes. (XFZ657-0)
' 0.1 2012/09/06 Created. (XFZ657)
'
title = "Shapes Editor 0.4"
GraphicsWindow.Title = title
WQ = Text.GetCharacter(34)
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
pw = "1=2;2=4;3=8;4=16;5=0;6=1;"
pen = 1
nPen = 6
pwidth = GraphicsWindow.PenWidth
pcolor = GraphicsWindow.PenColor
bcolor = GraphicsWindow.BrushColor
DrawMenu()
cont = "True" ' continue
clicked = "False"
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = DoNothing
GraphicsWindow.MouseMove = OnMouseMove
While cont
If clicked Then
SearchClickedObject()
DoObject()
clicked = "False"
Else
Program.Delay(100)
EndIf
EndWhile
' end of program
Sub DoMenu
While Text.StartsWith(obj, "menu")
i = Text.GetSubTextToEnd(obj, 5)
obj = ""
func = menu[i]["func"]
select = "True"
ItemSelect()
If func = "rect" Then ' rectangle
GetClickedPoint()
SearchClickedObject()
If Text.StartsWith(obj, "menu") = "False" Then
GetReleasedPoint()
nShapes = nShapes + 1
obj = "shape" + nShapes
shape[nShapes]["func"] = "rect"
shape[nShapes]["bc"] = bcolor
shape[nShapes]["pw"] = pwidth
GraphicsWindow.BrushColor = bcolor
GraphicsWindow.PenWidth = pwidth
If pwidth > 0 Then
shape[nShapes]["pc"] = pcolor
GraphicsWindow.PenColor = pcolor
EndIf
xmin = Math.Min(x0, x1)
ymin = Math.Min(y0, y1)
xmax = Math.Max(x0, x1)
ymax = Math.Max(y0, y1)
w = xmax - xmin
h = ymax - ymin
shape[nShapes]["x"] = xmin
shape[nShapes]["y"] = ymin
shape[nShapes]["width"] = w
shape[nShapes]["height"] = h
shape[nShapes]["obj"] = Shapes.AddRectangle(w, h)
Shapes.Move(shape[nShapes]["obj"], xmin, ymin)
EndIf
ElseIf func = "ell" Then ' ellipse
GetClickedPoint()
SearchClickedObject()
If Text.StartsWith(obj, "menu") = "False" Then
GetReleasedPoint()
nShapes = nShapes + 1
obj = "shape" + nShapes
shape[nShapes]["func"] = "ell"
shape[nShapes]["bc"] = bcolor
shape[nShapes]["pw"] = pwidth
GraphicsWindow.BrushColor = bcolor
GraphicsWindow.PenWidth = pwidth
If pwidth > 0 Then
shape[nShapes]["pc"] = pcolor
GraphicsWindow.PenColor = pcolor
EndIf
xmin = Math.Min(x0, x1)
ymin = Math.Min(y0, y1)
xmax = Math.Max(x0, x1)
ymax = Math.Max(y0, y1)
w = xmax - xmin
h = ymax - ymin
shape[nShapes]["x"] = xmin
shape[nShapes]["y"] = ymin
shape[nShapes]["width"] = w
shape[nShapes]["height"] = h
shape[nShapes]["obj"] = Shapes.AddEllipse(w, h)
Shapes.Move(shape[nShapes]["obj"], xmin, ymin)
EndIf
ElseIf func = "tri" Then ' triangle
GetClickedPoint()
SearchClickedObject()
If Text.StartsWith(obj, "menu") = "False" Then
GetReleasedPoint()
nShapes = nShapes + 1
obj = "shape" + nShapes
shape[nShapes]["func"] = "tri"
shape[nShapes]["bc"] = bcolor
shape[nShapes]["pw"] = pwidth
shape[nShapes]["pc"] = pcolor
GraphicsWindow.BrushColor = bcolor
GraphicsWindow.PenWidth = pwidth
If pwidth > 0 Then
shape[nShapes]["pc"] = pcolor
GraphicsWindow.PenColor = pcolor
EndIf
xmin = Math.Min(x0, x1)
ymin = Math.Min(y0, y1)
xmax = Math.Max(x0, x1)
ymax = Math.Max(y0, y1)
xt = Math.Floor((xmax - xmin) / 2) ' x top
w = xmax - xmin ' width
h = ymax - ymin ' height
shape[nShapes]["x"] = xmin
shape[nShapes]["y"] = ymin
shape[nShapes]["width"] = w
shape[nShapes]["height"] = h
If y1 <= y0 Then
x1 = xt
y1 = h
x2 = 0
y2 = 0
x3 = w
y3 = 0
ElseIf y0 < y1 Then
x1 = xt
y1 = 0
x2 = 0
y2 = h
x3 = w
y3 = h
EndIf
shape[nShapes]["x1"] = x1
shape[nShapes]["y1"] = y1
shape[nShapes]["x2"] = x2
shape[nShapes]["y2"] = y2
shape[nShapes]["x3"] = x3
shape[nShapes]["y3"] = y3
shape[nShapes]["obj"] = Shapes.AddTriangle(x1, y1, x2, y2, x3, y3)
Shapes.Move(shape[nShapes]["obj"], xmin, ymin)
EndIf
ElseIf func = "line" Then ' line
GetClickedPoint()
SearchClickedObject()
If Text.StartsWith(obj, "menu") = "False" Then
GetReleasedPoint()
nShapes = nShapes + 1
obj = "shape" + nShapes
shape[nShapes]["func"] = "line"
shape[nShapes]["pw"] = pwidth
GraphicsWindow.PenWidth = pwidth
If pwidth > 0 Then
shape[nShapes]["pc"] = pcolor
GraphicsWindow.PenColor = pcolor
EndIf
xmin = Math.Min(x0, x1)
ymin = Math.Min(y0, y1)
xmax = Math.Max(x0, x1)
ymax = Math.Max(y0, y1)
shape[nShapes]["x"] = xmin
shape[nShapes]["y"] = ymin
shape[nShapes]["width"] = xmax - xmin
shape[nShapes]["height"] = ymax - ymin
shape[nShapes]["x1"] = x0 - xmin
shape[nShapes]["y1"] = y0 - ymin
shape[nShapes]["x2"] = x1 - xmin
shape[nShapes]["y2"] = y1 - ymin
shape[nShapes]["obj"] = Shapes.AddLine(x0 - xmin, y0 - ymin, x1 - xmin, y1 - ymin)
Shapes.Move(shape[nShapes]["obj"], xmin, ymin)
EndIf
ElseIf func = "pc" Then ' pen color
color = pcolor
Color_Slider()
pcolor = color
DrawMenuItem()
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = DoNothing
GraphicsWindow.MouseMove = OnMouseMove
ElseIf func = "pw" Then ' pen width
If pen = nPen Then
pen = 1
Else
pen = pen + 1
EndIf
pwidth = pw[pen]
x = menu[i]["x0"]
y = menu[i]["y0"]
size = menu[i]["x1"] - x
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(x, y, size, size)
DrawMenuItem()
ElseIf func = "bc" Then ' brush color
color = bcolor
Color_Slider()
bcolor = color
DrawMenuItem()
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = DoNothing
GraphicsWindow.MouseMove = OnMouseMove
ElseIf func = "save" Then ' save shapes
DumpShapes()
EndIf
select = "False"
ItemSelect()
EndWhile
EndSub
Sub DoObject
' param obj
While obj <> ""
DoMenu()
If obj <> "" Then
DoShape()
EndIf
EndWhile
EndSub
Sub DoShape
While Text.StartsWith(obj, "shape")
i = Text.GetSubTextToEnd(obj, 6)
obj = ""
func = shape[i]["func"]
select = "True"
ShapeSelect()
Program.Delay(800)
select = "False"
ShapeSelect()
EndWhile
EndSub
Sub DrawMenu
cx = 10
cy = 10
size = 40
nMenu = 8
x = cx
For i = 1 To nMenu
y = cy + (i - 1) * (size + 2)
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(x, y, size, size)
menu[i]["x0"] = x
menu[i]["y0"] = y
menu[i]["x1"] = x + size
menu[i]["y1"] = y + size
DrawMenuItem()
EndFor
EndSub
Sub DrawMenuItem
' param i - item number
' param pwidth - pen width
' param pcolor - pen color
' param bcolor - brush color
margin = 4
x = menu[i]["x0"]
y = menu[i]["y0"]
size = menu[i]["x1"] - x
GraphicsWindow.PenColor = "Black"
GraphicsWindow.PenWidth = 2
If i = 1 Then
menu[i]["func"] = "rect"
GraphicsWindow.DrawRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
ElseIf i = 2 Then
menu[i]["func"] = "ell"
GraphicsWindow.DrawEllipse(x + margin, y + margin, size - margin * 2, size - margin * 2)
ElseIf i = 3 Then
menu[i]["func"] = "tri"
x1 = x + size / 2
y1 = y + margin
x2 = x + margin
y2 = y + size - margin
x3 = x + size - margin
y3 = y + size - margin
GraphicsWindow.DrawTriangle(x1, y1, x2, y2, x3, y3)
ElseIf i = 4 Then
menu[i]["func"] = "line"
x1 = x + margin
y1 = y + margin
x2 = x + size - margin
y2 = y + size - margin
GraphicsWindow.DrawLine(x1, y1, x2, y2)
ElseIf i = 5 Then
menu[i]["func"] = "pw"
GraphicsWindow.PenWidth = pwidth
x1 = x + margin
y1 = y + size / 2
x2 = x + size - margin
y2 = y + size / 2
GraphicsWindow.DrawLine(x1, y1, x2, y2)
ElseIf i = 6 Then
menu[i]["func"] = "pc"
margin = 6
GraphicsWindow.PenWidth = 4
GraphicsWindow.PenColor = pcolor
GraphicsWindow.DrawRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
ElseIf i = 7 Then
menu[i]["func"] = "bc"
GraphicsWindow.BrushColor = bcolor
GraphicsWindow.FillRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
GraphicsWindow.PenColor = "Black"
GraphicsWindow.PenWidth = 2
GraphicsWindow.DrawRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
ElseIf i = 8 Then
menu[i]["func"] = "save"
GraphicsWindow.BrushColor = "SteelBlue"
GraphicsWindow.FillRectangle(x + margin, y + margin, size - margin * 2, size - margin * 2)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(x + margin + 4, y + margin, size - (margin + 4) * 2, (size - margin * 2) / 3)
GraphicsWindow.BrushColor = "Gray"
GraphicsWindow.FillRectangle(x + margin + 6, y + size - 15, size - (margin + 8) * 2, 15 - margin)
EndIf
EndSub
Sub GetClickedPoint
clicked = "False"
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = DoNothing
While clicked = "False"
Program.Delay(100)
EndWhile
x0 = mx
y0 = my
x1 = mx
y1 = my
EndSub
Sub GetReleasedPoint
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "Black"
GraphicsWindow.BrushColor = "White"
GraphicsWindow.MouseMove = OnMouseMove
released = "False"
While released = "False"
If moved Then
GraphicsWindow.MouseMove = DoNothing
x1 = mx
y1 = my
If func = "rect" Then
If oRect <> "" Then
Shapes.Remove(oRect)
EndIf
xmin = Math.Min(x0, x1)
ymin = Math.Min(y0, y1)
xmax = Math.Max(x0, x1)
ymax = Math.Max(y0, y1)
oRect = Shapes.AddRectangle(xmax - xmin, ymax - ymin)
Shapes.Move(oRect, xmin, ymin)
Shapes.SetOpacity(oRect, 50)
ElseIf func = "ell" Then
If oEll <> "" Then
Shapes.Remove(oEll)
EndIf
xmin = Math.Min(x0, x1)
ymin = Math.Min(y0, y1)
xmax = Math.Max(x0, x1)
ymax = Math.Max(y0, y1)
oEll = Shapes.AddEllipse(xmax - xmin, ymax - ymin)
Shapes.Move(oEll, xmin, ymin)
Shapes.SetOpacity(oEll, 50)
ElseIf func = "tri" Then
If oTri <> "" Then
Shapes.Remove(oTri)
EndIf
oTri = Shapes.AddTriangle((x1 - x0) / 2, 0, 0, y1 - y0, x1 - x0, y1 - y0)
Shapes.Move(oTri, x0, y0)
Shapes.SetOpacity(oTri, 50)
ElseIf func = "line" Then
If oLine <> "" Then
Shapes.Remove(oLine)
EndIf
oLine = Shapes.AddLine(x0, y0, x1, y1)
Shapes.SetOpacity(oLine, 50)
EndIf
moved = "False"
GraphicsWindow.MouseMove = OnMouseMove
Else
Program.Delay(100)
EndIf
EndWhile
GraphicsWindow.MouseMove = DoNothing
If oRect <> "" Then
Shapes.Remove(oRect)
EndIf
If oEll <> "" Then
Shapes.Remove(oEll)
EndIf
If oTri <> "" Then
Shapes.Remove(oTri)
EndIf
If oLine <> "" Then
Shapes.Remove(oLine)
EndIf
EndSub
Sub ItemSelect
' i - menu index
' select - "True" if selected
If select Then
GraphicsWindow.PenColor = "Gray"
Else
GraphicsWindow.PenColor = "White"
EndIf
GraphicsWindow.PenWidth = 2
x = menu[i]["x0"] - 1
y = menu[i]["y0"] - 1
width = menu[i]["x1"] - x + 1
height = menu[i]["y1"] - y + 1
GraphicsWindow.DrawRectangle(x, y, width, height)
EndSub
Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
clicked = "True"
GraphicsWindow.MouseUp = OnMouseUp
EndSub
Sub DoNothing
EndSub
Sub OnMouseMove
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
moved = "True"
EndSub
Sub OnMouseUp
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
released = "True"
EndSub
Sub SearchClickedObject
' return obj
Stack.PushValue("local", i)
obj = ""
For i = 1 To nMenu
If menu[i]["x0"] <= mx And mx <= menu[i]["x1"] And menu[i]["y0"] <= my And my <= menu[i]["y1"] Then
obj = "menu" + i
Goto sco_exit
EndIf
EndFor
For i = nShapes To 1 Step -1
_x0 = shape[i]["x"]
_x1 = shape[i]["x"] + shape[i]["width"]
_y0 = shape[i]["y"]
_y1 = shape[i]["y"] + shape[i]["height"]
If _x0 <= mx And mx <= _x1 And _y0 <= my And my <= _y1 Then
If shape[i]["func"] = "rect" Then
obj = "shape" + i
Goto sco_exit
ElseIf shape[i]["func"] = "ell" Then
width = shape[i]["width"]
height = shape[i]["height"]
x = (mx - (_x0 + width / 2)) / width * 2
y = (my - (_y0 + height / 2)) / height * 2
r = Math.SquareRoot(x * x + y * y)
If r <= 1 Then
obj = "shape" + i
Goto sco_exit
EndIf
ElseIf shape[i]["func"] = "tri" Then
width = shape[i]["width"]
height = shape[i]["height"]
x = (mx - (_x0 + width / 2)) / width * 2
If shape[i]["y1"] = 0 Then
y = (my - _y1) / height
r = Math.Abs(x) + Math.Abs(y)
If r <= 1 And y <= 0 Then
obj = "shape" + i
Goto sco_exit
EndIf
Else
y = (my - _y0) / height
r = Math.Abs(x) + Math.Abs(y)
If r <= 1 And y >= 0 Then
obj = "shape" + i
Goto sco_exit
EndIf
EndIf
ElseIf shape[i]["func"] = "line" Then
_x1 = _x0 + shape[i]["x1"]
_y1 = _y0 + shape[i]["y1"]
_x2 = _x0 + shape[i]["x2"]
_y2 = _y0 + shape[i]["y2"]
r1 = Math.SquareRoot(Math.Power(_x1 - mx, 2) + Math.Power(_y1 - my, 2))
r2 = Math.SquareRoot(Math.Power(_x2 - mx, 2) + Math.Power(_y2 - my, 2))
r = Math.SquareRoot(Math.Power(_x2 - _x1, 2) + Math.Power(_y2 - _y1, 2))
If r1 + r2 <= (r * 1.0001) Then
obj = "shape" + i
Goto sco_exit
EndIf
EndIf
EndIf
EndFor
sco_exit:
i = Stack.PopValue("local")
EndSub
Sub CS_DrawColorRect
' param color - color of rectangle
' param x, y - position of rectangle
' param width, height - size of rectangle
' return oRect - rectangle object
GraphicsWindow.BrushColor = color
GraphicsWindow.PenColor = BORDERCOLOR
If oRect <> "" Then
Shapes.Remove(oRect)
EndIf
oRect = Shapes.AddRectangle(width, height)
Shapes.Move(oRect, x, y)
EndSub
Sub CS_GetColor
' return color
oSlider = oRed
Slider_GetLevel()
red = level
oSlider = oGreen
Slider_GetLevel()
green = level
oSlider = oBlue
Slider_GetLevel()
blue = level
color = GraphicsWindow.GetColorFromRGB(red, green, blue)
EndSub
Sub CS_Init
' Initialize sliders
width = 256
min = 0
max = 255
left = 200
' add red slider
top = TOPY
caption = "R"
Slider_Add()
oRed = oSlider
' add green slider
top = top + DELTAY
caption = "G"
Slider_Add()
oGreen = oSlider
' add blue slider
top = top + DELTAY
caption = "B"
Slider_Add()
oBlue = oSlider
' add hue slider
width = 360
top = top + DELTAY
max = 360
caption = "H"
Slider_Add()
oHue = oSlider
' add saturation slider
width = 100
top = top + DELTAY
max = 100
caption = "S"
Slider_Add()
oSaturation = oSlider
' add lightness slider
width = 100
top = top + DELTAY
max = 100
caption = "L"
Slider_Add()
oLightness = oSlider
CS_AdjustSlider()
' draw color rectangle
CS_GetColor()
CS_ShowColor()
x = LEFTX
y = TOPY + DELTAY * 4
width = 100
height = 100
CS_DrawColorRect()
' add text box
GraphicsWindow.BrushColor = CAPTIONCOLOR
top = y + height + 4
oNewColor = Shapes.AddText("")
Shapes.Move(oNewColor, LEFTX, top)
GraphicsWindow.MouseUp = Slider_OnMouseUp
GraphicsWindow.MouseDown = Slider_OnMouseDown
EndSub
Sub CS_OnButtonClicked
cont = "False"
If Controls.LastClickedButton = oCancel Then
cancel = "True"
Else
cancel = "False"
EndIf
EndSub