' Shapes Editor 0.2
' Copyright (c) 2012 Nonki Takahashi. All rights reserved.
'
' History :
' 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.2"
GraphicsWindow.Title = title
WQ = Text.GetCharacter(34)
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 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 DumpShapes
Stack.PushValue("local", i)
lastpw = ""
lastpc = ""
lastbc = ""
TextWindow.WriteLine("' " + Clock.Date + " " + Clock.Time + " Generated")
TextWindow.WriteLine("' by " + title)
TextWindow.WriteLine("'")
If nShapes > 0 Then
xmin = shape[1]["x"]
ymin = shape[1]["y"]
xman = shape[1]["x"]
yman = shape[1]["y"]
EndIf
For i = 2 To nShapes
If shape[i]["x"] < xmin Then
xmin = shape[i]["x"]
EndIf
If shape[i]["y"] < ymin Then
ymin = shape[i]["y"]
EndIf
If xmax < shape[i]["x"] Then
xmax = shape[i]["x"]
EndIf
If ymax < shape[i]["y"] Then
ymax = shape[i]["y"]
EndIf
EndFor
TextWindow.WriteLine("x = " + xmin + " ' x offset")
TextWindow.WriteLine("y = " + ymin + " ' y offset")
TextWindow.WriteLine("s = 1 ' scale")
TextWindow.WriteLine("a = 10 ' angle")
TextWindow.WriteLine("DrawShapes()")
TextWindow.WriteLine("")
TextWindow.WriteLine("Sub DrawShapes")
TextWindow.WriteLine(" Stack.PushValue(" + WQ + "local" + WQ + ", x)")
TextWindow.WriteLine(" Stack.PushValue(" + WQ + "local" + WQ + ", y)")
TextWindow.WriteLine(" Stack.PushValue(" + WQ + "local" + WQ + ", a)")
TextWindow.WriteLine(" Stack.PushValue(" + WQ + "local" + WQ + ", x0)")
TextWindow.WriteLine(" Stack.PushValue(" + WQ + "local" + WQ + ", y0)")
TextWindow.WriteLine(" x0 = x")
TextWindow.WriteLine(" y0 = y")
TextWindow.WriteLine(" a0 = a")
TextWindow.WriteLine(" cx = (" + xmax + " - x0) / 2")
TextWindow.WriteLine(" cy = (" + ymax + " - y0) / 2")
For i = 1 To nShapes
pw = shape[i]["pw"]
If lastpw <> pw Then
TextWindow.WriteLine(" GraphicsWindow.PenWidth = " + pw + " * s")
lastpw = pw
EndIf
pc = shape[i]["pc"]
If lastpc <> pc Then
TextWindow.WriteLine(" GraphicsWindow.PenColor = " + WQ + pc + WQ)
lastpc = pc
EndIf
If shape[i]["func"] = "rect" Then
bc = shape[i]["bc"]
If lastbc <> bc Then
TextWindow.WriteLine(" GraphicsWindow.BrushColor = " + WQ + bc + WQ)
lastbc = bc
EndIf
TextWindow.WriteLine(" x2 = " + (shape[i]["x"] - xmin))
TextWindow.WriteLine(" y2 = " + (shape[i]["y"] - ymin))
TextWindow.WriteLine(" w2 = " + shape[i]["width"])
TextWindow.WriteLine(" h2 = " + shape[i]["height"])
TextWindow.Write(" oRect" + i + " = ")
TextWindow.WriteLine("Shapes.AddRectangle(w2 * s, h2 * s)")
TextWindow.WriteLine(" RotatePos()")
TextWindow.Write(" Shapes.Move(oRect" + i)
TextWindow.WriteLine(", x0 + x2 * s, y0 + y2 * s)")
TextWindow.WriteLine(" Shapes.Rotate(oRect" + i + ", a0)")
ElseIf shape[i]["func"] = "ell" Then
bc = shape[i]["bc"]
If lastbc <> bc Then
TextWindow.WriteLine(" GraphicsWindow.BrushColor = " + WQ + bc + WQ)
lastbc = bc
EndIf
TextWindow.WriteLine(" x2 = " + (shape[i]["x"] - xmin))
TextWindow.WriteLine(" y2 = " + (shape[i]["y"] - ymin))
TextWindow.WriteLine(" w2 = " + shape[i]["width"])
TextWindow.WriteLine(" h2 = " + shape[i]["height"])
TextWindow.Write(" oEll" + i + " = ")
TextWindow.WriteLine("Shapes.AddEllipse(w2 * s, h2 * s)")
TextWindow.WriteLine(" RotatePos()")
TextWindow.Write(" Shapes.Move(oEll" + i)
TextWindow.WriteLine(", x0 + x2 * s, y0 + y2 * s)")
TextWindow.WriteLine(" Shapes.Rotate(oEll" + i + ", a0)")
ElseIf shape[i]["func"] = "tri" Then
bc = shape[i]["bc"]
If lastbc <> bc Then
TextWindow.WriteLine(" GraphicsWindow.BrushColor = " + WQ + bc + WQ)
lastbc = bc
EndIf
TextWindow.WriteLine(" x2 = " + (shape[i]["x"] - xmin))
TextWindow.WriteLine(" y2 = " + (shape[i]["y"] - ymin))
TextWindow.WriteLine(" w2 = " + shape[i]["width"])
TextWindow.WriteLine(" h2 = " + shape[i]["height"])
TextWindow.Write(" oTri" + i + " = Shapes.AddTriangle(")
TextWindow.Write(shape[i]["x1"] + " * s, ")
TextWindow.Write(shape[i]["y1"] + " * s, ")
TextWindow.Write(shape[i]["x2"] + " * s, ")
TextWindow.Write(shape[i]["y2"] + " * s, ")
TextWindow.Write(shape[i]["x3"] + " * s, ")
TextWindow.WriteLine(shape[i]["y3"] + " * s)")
TextWindow.WriteLine(" RotatePos()")
TextWindow.Write(" Shapes.Move(oTri" + i)
TextWindow.WriteLine(", x0 + x2 * s, y0 + y2 * s)")
TextWindow.WriteLine(" Shapes.Rotate(oTri" + i + ", a0)")
ElseIf shape[i]["func"] = "line" Then
bc = shape[i]["bc"]
TextWindow.WriteLine(" x2 = " + (shape[i]["x"] - xmin))
TextWindow.WriteLine(" y2 = " + (shape[i]["y"] - ymin))
TextWindow.WriteLine(" w2 = " + shape[i]["width"])
TextWindow.WriteLine(" h2 = " + shape[i]["height"])
TextWindow.Write(" oLine" + i + " = Shapes.AddLine(")
TextWindow.Write(shape[i]["x1"] + " * s, ")
TextWindow.Write(shape[i]["y1"] + " * s, ")
TextWindow.Write(shape[i]["x2"] + " * s, ")
TextWindow.WriteLine(shape[i]["y2"] + " * s)")
TextWindow.WriteLine(" RotatePos()")
TextWindow.Write(" Shapes.Move(oLine" + i)
TextWindow.WriteLine(", x0 + x2 * s, y0 + y2 * s)")
TextWindow.WriteLine(" Shapes.Rotate(oLine" + i + ", a0)")
EndIf
EndFor
TextWindow.WriteLine(" y0 = Stack.PopValue(" + WQ + "local" + WQ + ")")
TextWindow.WriteLine(" x0 = Stack.PopValue(" + WQ + "local" + WQ + ")")
TextWindow.WriteLine(" a = Stack.PopValue(" + WQ + "local" + WQ + ")")
TextWindow.WriteLine(" y = Stack.PopValue(" + WQ + "local" + WQ + ")")
TextWindow.WriteLine(" x = Stack.PopValue(" + WQ + "local" + WQ + ")")
TextWindow.WriteLine("EndSub")
TextWindow.WriteLine("")
TextWindow.WriteLine("Sub RotatePos")
TextWindow.WriteLine(" ' param x2, y2 - position of a shape")
TextWindow.WriteLine(" ' param w2, h2 - size of a shape")
TextWindow.WriteLine(" ' param cx, cy - center of shapes")
TextWindow.WriteLine(" ' param a0 - rotate angle")
TextWindow.WriteLine(" ' return x2, y2 - rotated position of a shape")
TextWindow.WriteLine(" cx2 = x2 + w2 / 2")
TextWindow.WriteLine(" cy2 = y2 + h2 / 2")
TextWindow.WriteLine(" x = cx2 - cx")
TextWindow.WriteLine(" y = cy2 - cy")
TextWindow.WriteLine(" Math_CartesianToPolar()")
TextWindow.WriteLine(" a2 = a + a0")
TextWindow.WriteLine(" x = r * Math.Cos(a2 * Math.Pi / 180)")
TextWindow.WriteLine(" y = r * Math.Sin(a2 * Math.Pi / 180)")
TextWindow.WriteLine(" cx2 = x + cx")
TextWindow.WriteLine(" cy2 = y + cy")
TextWindow.WriteLine(" x2 = cx2 - w2 / 2")
TextWindow.WriteLine(" y2 = cy2 - h2 / 2")
TextWindow.WriteLine("EndSub")
TextWindow.WriteLine("")
TextWindow.WriteLine("Sub Math_CartesianToPolar")
TextWindow.WriteLine(" ' param x, y - cartesian coordinate")
TextWindow.WriteLine(" ' return r, a - polar coordinate")
TextWindow.WriteLine(" r = Math.SquareRoot(x * x + y * y)")
TextWindow.WriteLine(" If x = 0 And y > 0 Then ")
TextWindow.WriteLine(" a = 90 ' [degree]")
TextWindow.WriteLine(" ElseIf x = 0 And y < 0 Then ")
TextWindow.WriteLine(" a = -90")
TextWindow.WriteLine(" Else")
TextWindow.WriteLine(" a = Math.ArcTan(y / x) * 180 / Math.Pi")
TextWindow.WriteLine(" EndIf")
TextWindow.WriteLine(" If x < 0 Then")
TextWindow.WriteLine(" a = a + 180")
TextWindow.WriteLine(" ElseIf x > 0 And y < 0 Then")
TextWindow.WriteLine(" a = a + 360")
TextWindow.WriteLine(" EndIf")
TextWindow.WriteLine("EndSub")
i = Stack.PopValue("local")
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
sco_exit:
i = Stack.PopValue("local")
EndSub
Sub Color_ColorToRGB
' Color | Convert Color to RGB
' param sColor - "#rrggbb"
' return iR, iG, iB - [0, 255]
sR = Text.GetSubText(sColor, 2, 2)
sG = Text.GetSubText(sColor, 4, 2)
sB = Text.GetSubText(sColor, 6, 2)
sHex = sR
Math_Hex2Dec()
iR = iDec
sHex = sG
Math_Hex2Dec()
iG = iDec
sHex = sB
Math_Hex2Dec()
iB = iDec
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