Microsoft Small Basic

Program Listing: ZNR767
' Fonts Sample 0.1
' Copyright (c) 2014 Nonki Takahashi. All rights reserved.
'
' History :
' 0.1 2014-01-03 Created from Ruler 0.61 .
'
GraphicsWindow.Title = "Fonts Sample 0.1"
GraphicsWindow.BackgroundColor = "LightGray"
Not = "True=False;False=True;"
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BrushColor = "Black"
param = "left=10;top=10;caption=FontBold;"
param["checked"] = GraphicsWindow.FontBold
Controls_AddCheckBox()
cboxBold = return
param = "left=10;top=40;caption=FontItalic;"
param["checked"] = GraphicsWindow.FontItalic
Controls_AddCheckBox()
cboxItalic = return
param = "left=10;top=70;caption=FontName;offset=70;length=15;"
Controls_AddTextBox()
tboxName = return
fn = GraphicsWindow.FontName
Controls.SetTextBoxText(tboxName, fn)
param = "left=10;top=100;caption=FontSize;offset=70;length=3;"
Controls_AddTextBox()
tboxSize = return
fs = GraphicsWindow.FontSize
Controls.SetTextBoxText(tboxSize, fs)
param = "left=10;top=130;caption=Text;offset=70;length=30;"
Controls_AddTextBox()
tboxText = return
Controls.SetTextBoxText(tboxText, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Controls.AddButton("Redraw", 10, 160)
Controls.ButtonClicked = OnButtonClicked
Ruler_Add()
RedrawText()
redraw = "False"
Main_Loop()
Sub OnButtonClicked
redraw = "True"
EndSub
Sub Ruler_Add
' Ruler | Add ruler
uImage = "http://www.nonkit.com/smallbasic.files/ruler600.png"
oImage = ImageList.LoadImage(uImage)
ruler["angle"] = 0
ruler["moved"] = "False"
ruler["clicked"] = "False"
ruler["left"] = 0
ruler["top"] = 0
ruler["width"] = ImageList.GetWidthOfImage(oImage)
ruler["height"] = ImageList.GetHeightOfImage(oImage)
If ruler["width"] = 0 Then ' for remote
ruler["width"] = 620
ruler["height"] = 50
EndIf
ruler["cx"] = ruler["left"] + ruler["width"] / 2
ruler["cy"] = ruler["top"] + ruler["height"] / 2
ruler["right"] = ruler["left"] + ruler["width"]
ruler["bottom"] = ruler["top"] + ruler["height"]
ruler["oRuler"] = Shapes.AddImage(uImage)
Shapes.SetOpacity(ruler["oRuler"], 70)
Shapes.Move(ruler["oRuler"], ruler["px"], ruler["py"])
EndSub
Sub Main_Loop
' Main loop
GraphicsWindow.MouseUp = Ruler_OnMouseUp
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.KeyDown = Ruler_OnKeyDown
While "True"
Program.Delay(100)
If ruler["moved"] Then
ruler["moved"] = "False"
x = ruler["movex"] - ruler["mousex"]
y = ruler["movey"] - ruler["mousey"]
Ruler_Move()
EndIf
If ruler["clicked"] Then
ruler["clicked"] = "False"
an = 90 - ruler["angle"]
Ruler_Rotate()
EndIf
If redraw Then
redraw = "False"
RedrawText()
EndIf
EndWhile
EndSub
Sub OnMouseDown
mx = GraphicsWindow.MouseX
my = GraphicsWindow.MouseY
Controls_OnMouseDown()
If Not[clicked] Then
Ruler_OnMouseDown()
EndIf
EndSub
Sub RedrawText
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
param["checkBoxName"] = cboxBold
Controls_IsChecked()
GraphicsWindow.FontBold = return
param["checkBoxName"] = cboxItalic
Controls_IsChecked()
GraphicsWindow.FontItalic = return
return = Controls.GetTextBoxText(tboxName)
GraphicsWindow.FontName = return
return = Controls.GetTextBoxText(tboxSize)
GraphicsWindow.FontSize = return
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(10, 200, gw - 10, return)
txt = Controls.GetTextBoxText(tboxText)
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(10, 200, txt)
EndSub
Sub Ruler_OnKeyDown
' Ruler | On key down
key = GraphicsWindow.LastKey
If key = "Up" Then
ruler["movex"] = ruler["cx"] + ruler["mousex"]
ruler["movey"] = ruler["cy"] + ruler["mousey"] - 1
ruler["moved"] = "True"
ElseIf key = "Down" Then
ruler["movex"] = ruler["cx"] + ruler["mousex"]
ruler["movey"] = ruler["cy"] + ruler["mousey"] + 1
ruler["moved"] = "True"
ElseIf key = "Left" Then
ruler["movex"] = ruler["cx"] + ruler["mousex"] - 1
ruler["movey"] = ruler["cy"] + ruler["mousey"]
ruler["moved"] = "True"
ElseIf key = "Right" Then
ruler["movex"] = ruler["cx"] + ruler["mousex"] + 1
ruler["movey"] = ruler["cy"] + ruler["mousey"]
ruler["moved"] = "True"
EndIf
EndSub
Sub Ruler_OnMouseDown
' Ruler | On mouse down event handler
' param mx, my - mouse x and y co-ordinates
' ruler["clicked"] = "False"
ruler["clicked"] = "N/A"
If ruler["left"] <= mx And mx <= ruler["right"] And ruler["top"] <= my And my <= ruler["bottom"] Then
ruler["mousex"] = mx - ruler["cx"]
ruler["mousey"] = my - ruler["cy"]
ruler["moved"] = "False"
ruler["clicked"] = "N/A"
GraphicsWindow.MouseMove = Ruler_OnMouseMove
EndIf
EndSub
Sub Ruler_OnMouseUp
' Ruler | On mouse up event handler
GraphicsWindow.MouseMove = Ruler_DoNothing
If ruler["clicked"] = "N/A" Then
ruler["clicked"] = "True"
EndIf
EndSub
Ruler_OnMouseMove=""
Sub Ruler_OnMouseMove
' Ruler | On mouse move event handler
GraphicsWindow.MouseMove = Ruler_DoNothing
ruler["clicked"] = "False"
ruler["moved"] = "True"
ruler["movex"] = GraphicsWindow.MouseX
ruler["movey"] = GraphicsWindow.MouseY
GraphicsWindow.MouseMove = Ruler_OnMouseMove
EndSub
Sub Ruler_DoNothing
EndSub
Sub Ruler_Move
' Ruler | Move ruler
' param x, y - center of ruler
ruler["cx"] = x
ruler["cy"] = y
ruler["left"] = x - ruler["width"] / 2
ruler["right"] = x + ruler["width"] / 2
ruler["top"] = y - ruler["height"] / 2
ruler["bottom"] = y + ruler["height"] / 2
If ruler["angle"] = 0 Then
px = ruler["left"]
py = ruler["top"]
ElseIf ruler["angle"] = 90 Then
px = x - ruler["height"] / 2
py = y - ruler["width"] / 2
EndIf
Shapes.Move(ruler["oRuler"], px, py)
EndSub
Sub Ruler_Rotate
' Ruler | Rotate ruler
' param an - angle 0 or 90
If (an = 0 Or an = 90) And an <> ruler["angle"] Then
ruler["angle"] = an
Shapes.Rotate(ruler["oRuler"], an)
height = ruler["width"]
width = ruler["height"]
ruler["width"] = width
ruler["left"] = ruler["cx"] - width / 2
ruler["right"] = ruler["cx"] + width / 2
ruler["height"] = height
ruler["top"] = ruler["cy"] - height / 2
ruler["bottom"] = ruler["cy"] + height / 2
EndIf
EndSub
Sub Controls_AddCheckBox
' Controls | Add check box
' param["left"] - the x co-ordinate of the check box
' param["top"] - the y co-ordinate of the check box
' param["caption"] - caption of the check box
' param["checked"] - optional flag ("True" if default is checked)
' returns return - the check box that was just added to the Graphics Window.
cboxColor = GraphicsWindow.BrushColor
cboxSize = GraphicsWindow.FontSize
cboxFont = GraphicsWindow.FontName
cboxPW = GraphicsWindow.PenWidth
cboxPC = GraphicsWindow.PenColor
cboxNum = cboxNum + 1
cboxX0 = param["left"]
cboxX1 = cboxX0 + cboxSize + 1
cboxY0 = param["top"] + 1
cboxY1 = cboxY0 + cboxSize + 1
GraphicsWindow.PenWidth = 1
GraphicsWindow.PenColor = "Gray"
GraphicsWindow.BrushColor = "White"
cboxRect = Shapes.AddRectangle(cboxSize, cboxSize)
GraphicsWindow.PenWidth = cboxPW
GraphicsWindow.PenColor = cboxPC
Shapes.Move(cboxRect, cboxX0, cboxY0)
cboxObj[cboxNum]["x0"] = cboxX0
cboxObj[cboxNum]["x1"] = cboxX1
cboxObj[cboxNum]["y0"] = cboxY0
cboxObj[cboxNum]["y1"] = cboxY1
GraphicsWindow.BrushColor = cboxColor
GraphicsWindow.FontName = "Arial"
cboxMark = Shapes.AddText("✔")
Shapes.Move(cboxMark, param["left"] + 1, param["top"])
cboxObj[cboxNum]["checkmark"] = cboxMark
If param["checked"] = "" Or Not[param["checked"]] Then
Shapes.HideShape(cboxMark)
cboxObj[cboxNum]["checked"] = "False"
Else
cboxObj[cboxNum]["checked"] = "True"
EndIf
GraphicsWindow.FontName = cboxFont
cboxCaption = Shapes.AddText(param["caption"])
Shapes.Move(cboxCaption, param["left"] + cboxSize * 1.5 , param["top"])
return = "CheckBox" + cboxNum
EndSub
Sub Controls_OnMouseDown
' Controls | On mouse down
' param mx, my - mouse x and y co-ordinates
' return clicked - "True" if any check box clicked
clicked = "False"
For iCheckBox = 1 To cboxNum
x0 = cboxObj[iCheckBox]["x0"]
x1 = cboxObj[iCheckBox]["x1"]
y0 = cboxObj[iCheckBox]["y0"]
y1 = cboxObj[iCheckBox]["y1"]
If x0 <= mx And mx <= x1 And y0 <= my And my <= y1 Then
clicked = "True"
If cboxObj[iCheckBox]["checked"] Then
Shapes.HideShape(cboxObj[iCheckBox]["checkmark"])
cboxObj[iCheckBox]["checked"] = "False"
Else
Shapes.ShowShape(cboxObj[iCheckBox]["checkmark"])
cboxObj[iCheckBox]["checked"] = "True"
EndIf
EndIf
EndFor
EndSub
Sub Controls_IsChecked
' Controls | Is checked
' param["checkBoxName"] - check box name
' return - "True" if checked
iCheckBox = Text.GetSubTextToEnd(param["checkBoxName"], 9)
return = cboxObj[iCheckBox]["checked"]
EndSub
Sub Controls_AddTextBox
' Controls | Add text box with caption
' param["left"] - the x co-ordinate of the text box caption
' param["top"] - the y co-ordinate of the text box
' param["caption"] - caption of the text box
' param["offset"] - optional offset for the text box from the left
' param["length"] - optional character length for the text box
' return - the text box just added
fs = GraphicsWindow.FontSize
len = Text.GetLength(param["caption"])
tboxCaption = Shapes.AddText(param["caption"])
Shapes.Move(tboxCaption, param["left"], param["top"] + fs * 0.2)
offset = param["offset"]
If offset = "" Then
offset = len * fs * 0.9
EndIf
return = Controls.AddTextBox(param["left"] + offset, param["top"])
tboxLen = param["length"]
If tboxLen <> "" Then
tboxWidth = (tboxLen + 1) * fs * 0.9
tboxHeight = fs * 2
Controls.SetSize(return, tboxWidth, tboxHeight)
EndIf
EndSub
Sub SB_Workaround
' Small Basic | Workaround for Silverlight
' returns silverlight - "True" if in remote
color = GraphicsWindow.GetPixel(0, 0)
If Text.GetLength(color) > 7 Then
silverlight = "True"
msWait = 300
Else
silverlight = "False"
EndIf
EndSub