Microsoft Small Basic

Program Listing: BGF911-2
' Multi-Language Sample
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Using: Drop Down Menu System - (under construction) by Jibba Jabba
' Drop Down Menu BGF911-1

GraphicsWindow.Title = "Multi-Language Sample"

Initialise()

While "True"
UpdateProgram()
UpdateFrame()
FrameRate()
EndWhile

'=======================================================
'FIRST ORDER SUBROUTINES
'=======================================================

Sub UpdateProgram
start = Clock.ElapsedMilliseconds

If mouseMove Then
GetMousePosition()
Menu_UpdateOnMouseMove()

mouseMove = "False"
EndIf

If mouseDown Then
Menu_UpdateOnMouseDown() 'creates a non async OnMenuClicked Event
'GetMousePosition() 'not used here for menu

mouseDown = "False"
EndIf

If menuClicked Then 'OnMenuClicked
' For Multi-Language
For i = 1 To nLang
If subMenu[menuSelection][subMenuSelection] = lang1[ixLang[i]] Then
lang = ixLang[i]
i = nLang ' exit For
EndIf
EndFor
If lang <> langLast Then
Shapes.SetText(hello, msg[1][lang])
Shapes.SetText(l1, lang1[lang])
Shapes.SetText(l2, lang2[lang])
langLast = lang
EndIf

menuSelection = 0
subMenuSelection = 0
menuClicked = "False"
EndIf
EndSub

Sub UpdateFrame
Menu_UpdateFrame()
EndSub

Sub FrameRate
delay = 1000 / fps - (Clock.ElapsedMilliseconds - start)
If delay > 0 Then
'TextWindow.WriteLine("ms = " + (Clock.ElapsedMilliseconds - start)) 'speed
Program.Delay(delay)
Else
'TextWindow.WriteLine("delay = " + delay) 'lag
EndIf
EndSub

'============================================================
'SECOND ORDER SUBROUTINES - except for Menu Module
'============================================================
Sub GetMousePosition
mseX = GraphicsWindow.MouseX
mseY = GraphicsWindow.MouseY
EndSub

'===========================================================
'INITIALISE
'===========================================================
Sub Initialise
'DEBUG
'TextWindow.Top = 20
'TextWindow.Left = GraphicsWindow.Width + 50
GraphicsWindow.Left = 10

'Main Loop
fps = 50 '*6 drags > 300fps

'Get &/Or Set Stuff
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
GetMousePosition()

'For Multi-Language
lang1 = "ar=العربية;cs=čeština;de=Deutsch;en=English;es=español;"
lang1 = lang1 + "fr=français;he=מילת עברית;hr=hrvatski;"
lang1 = lang1 + "is=íslenska;it=italiano;ja=日本語;ko=한국어;"
lang1 = lang1 + "nl=Nederlands;pl=polski;pt-BR=Português (Brasil);"
lang1 = lang1 + "pt-PT=Português (Portugal);ru=русский;tr=Türkçe;"
lang1 = lang1 + "zh-CN=中文(简体);zh-TW=中文(繁體);"
nLang = Array.GetItemCount(lang1)
ixLang = Array.GetAllIndices(lang1)
lang2 = "ar=Arabic;cs=Czech;de=German;en=English;"
lang2 = lang2 + "es=Spanish;fr=French;he=Hebrew;hr=Croatian;"
lang2 = lang2 + "is=Icelandic;it=Italian;ja=Japanese;ko=Korean;"
lang2 = lang2 + "nl=Dutch;pl=Polish;pt-BR=Portuguese (Brazil);"
lang2 = lang2 + "pt-PT=Portuguese (Portugal);ru=Russian;"
lang2 = lang2 + "tr=Turkish;zh-CN=Chinese (Simplified);"
lang2 = lang2 + "zh-TW=Chinese (Traditional);"
lang = "en"
langLast = lang
msg[1] = "ar=!مرحبا بالعالم;cs=Hello World!;"
msg[1] = msg[1] + "de=Hallo Welt!;en=Hello World!;"
msg[1] = msg[1] + "es=¡Hola Mundo!;fr=Bonjour le monde!;"
msg[1] = msg[1] + "he=!שלום לך עולם;hr=Hello World!;"
msg[1] = msg[1] + "is=Halló heimur!;it=Ciao mondo!;"
msg[1] = msg[1] + "ja=こんにちは世界!;ko=세계 안녕하세요!;"
msg[1] = msg[1] + "nl=Hallo Wereld!;pl=Witaj świecie!;"
msg[1] = msg[1] + "pt-BR=Olá Mundo!;pt-PT=Olá Mundo!;"
msg[1] = msg[1] + "ru=Здравствуй, мир!;tr=Merhaba Dünya!;"
msg[1] = msg[1] + "zh-CN=你好世界!;zh-TW=你好世界!;"
GraphicsWindow.FontName = "Segoe UI"
GraphicsWindow.FontSize = 60
GraphicsWindow.FontBold = "True"
GraphicsWindow.BrushColor = "DimGray"
hello = Shapes.AddText(msg[1][lang])
Shapes.Move(hello, 60, 60)
GraphicsWindow.FontSize = 14
GraphicsWindow.FontBold = "False"
l1 = Shapes.AddText(lang1[lang])
Shapes.Move(l1, 60, 160)
l2 = Shapes.AddText(lang2[lang])
Shapes.Move(l2, 60, 180)

Menu_Initialise()

'Register Events
GraphicsWindow.MouseMove = OnMouseMove
GraphicsWindow.MouseDown = OnMouseDown
EndSub

'=========================================================
'Event Handlers
'=========================================================
Sub OnMouseMove
mouseMove = "True"
EndSub

Sub OnMouseDown
mouseDown = "True"
EndSub

'************************************************************************************************************
'============================================================
'DROP DOWN MENU SUBROUTINES
'============================================================
'Customise & Dynamic Subs - Second Order
'-----------------------------------------------------------------------------------------------------
Sub Menu_DataTable 'USE THIS SUB TO CUSTOMISE MENU
'MENU
'Data Table
menu = "1=Language;" 'ADD DATA
menuWidth = "1=100;" 'MATCH WIDTH WITH DATA
For i = 1 To nLang
subMenu[1][i] = lang1[ixLang[i]]
subMenuWidth[i] = 150 'MATCH WIDTH WITH DATA
EndFor
EndSub

Sub Menu_DrawMenuBar 'USE THIS SUB TO REDRAW MENU BAR on Resize 'UNTESTED
'Add Menu Bar - Sub {redraw for new window size}
If weHaveSprites Then
GraphicsWindow.DrawResizedImage(menuBarTexture, 0, 0, gw, menuHeight)
Else
GraphicsWindow.BrushColor = "LightGray"
GraphicsWindow.FillRectangle(0, 0, gw, menuHeight)
EndIf
EndSub

'-----------------------------------------------------------------------------------------------------------
'Initialise Menu - First Order
'-----------------------------------------------------------------------------------------------------------
Sub Menu_Initialise
Menu_DataTable() 'USED TO CUSTOMISE THE MENU

'Load Menu Sprites
menuFileName[1] = "\Focus Texture.png"
menuFocusSprite = ImageList.LoadImage(Program.Directory + menuFileName[1])
menuFileName[2] = "\subMenu Focus Sprite.png"
subMenuFocusSprite = ImageList.LoadImage(Program.Directory + menuFileName[2])
menuFileName[3] = "\Sub Menu Background Sprite 2.png"
subMenuBackgroundSprite = ImageList.LoadImage(Program.Directory + menuFileName[3])
menuFileName[4] = "\Menu Bar Texture.png"
menuBarTexture = ImageList.LoadImage(Program.Directory + menuFileName[4])

getMenuFiles = ""

'If there are no PNG files then we don't have sprites
weHaveSprites = "True"
For i = Array.GetItemCount(menuFileName) To 1 Step -1
If Text.IsSubText(getMenuFiles, menuFileName[i]) <> "True" Then
weHaveSprites = "False"
EndIf
EndFor

menuFocusOpacityIndex = 0

'Get posX for menuFocus
For i = 1 To Array.GetItemCount(menuWidth) 'to be reversed
totalMenuWidth = totalMenuWidth + menuWidth[i]
menuFocusX[i] = totalMenuWidth - menuWidth[i]
EndFor

If totalMenuWidth > gw Then
TextWindow.WriteLine("ERROR: total menu widths exceeds GW")
TextWindow.Pause()
EndIf

'Set Parameters
menuHeight = 20 '25
menuFocusWidth = 50 'original width before Zoom scale X
menuTextOffset = 8
menuTextOffsetY = 0 '4
isMenuSelected = -1 'toggle init OFF
menuFocusOpacity = "1=100;-1=30;0=0;"
subMenuTextOffset = 16

Menu_DrawMenuBar() 'USED TO REDRAW MENU AFTER WINDOW RESIZE

'Add Menu Focus
If weHaveSprites Then
menuFocus = Shapes.AddImage(menuFocusSprite)
Else
GraphicsWindow.BrushColor = "SkyBlue"
GraphicsWindow.PenWidth = 0
menuFocus = Shapes.AddRectangle(menuFocusWidth, menuHeight) 'WIP ************
EndIf
Shapes.SetOpacity(menuFocus, 0)

'Add Menu
GraphicsWindow.FontName = "Segoe UI"
GraphicsWindow.FontBold = "False"
GraphicsWindow.FontSize = 13
GraphicsWindow.BrushColor = "Black"

For i = Array.GetItemCount(menu) To 1 Step -1
menuText[i] = Shapes.AddText(menu[i])
Shapes.Move(menuText[i], menuFocusX[i] + menuTextOffset, menuTextOffsetY)
EndFor

'Error Check subMenu Width. NEEDS MORE TESTING
If menuFocusX[Array.GetItemCount(menu)] + subMenuWidth[Array.GetItemCount(menu)] > gw Then
TextWindow.WriteLine("ERROR: sum of all subMenu widths exceeds GW")
TextWindow.Pause()
EndIf

'Add Sub Menus - FIXING B/GROUND SPRITE *******************************
If weHaveSprites Then
subMenuBackground = Shapes.AddImage(subMenuBackgroundSprite)
Else
GraphicsWindow.BrushColor = "LightGray"
subMenuBackground = Shapes.AddRectangle(menuFocusWidth, menuHeight)
EndIf
Shapes.SetOpacity(subMenuBackground, 0)

'Add SubMenus
If weHaveSprites Then
subMenuFocus = Shapes.AddImage(subMenuFocusSprite) 'wip sprite ***********
Else
GraphicsWindow.BrushColor = "PaleTurquoise"
GraphicsWindow.PenWidth = 0
subMenuFocus = Shapes.AddRectangle(menuFocusWidth, menuHeight)
EndIf
Shapes.SetOpacity(subMenuFocus, 0)

For j = Array.GetItemCount(subMenu) To 1 Step -1
left = menuFocusX[j] + subMenuTextOffset
For i = Array.GetItemCount(subMenu[j]) To 1 Step -1
GraphicsWindow.BrushColor = "Black"
subMenuText[j][i] = Shapes.AddText(subMenu[j][i])
Shapes.Move(subMenuText[j][i], left, menuHeight * i + menuTextOffsetY)
Shapes.SetOpacity(subMenuText[j][i], 0)
EndFor
EndFor
EndSub

'------------------------------------------------------------------------------------------------------------
'Menu Updates - Second Order
'------------------------------------------------------------------------------------------------------------
Sub Menu_UpdateOnMouseMove
'Update Menu Focus onMouseMove
menuFocusedOn = 0
menuFocusOpacityIndex = 0
If mseY <= menuHeight And mseX <= totalMenuWidth Then
For i = Array.GetItemCount(menu) To 1 Step -1
If mseX >= menuFocusX[i] And mseX <= menuFocusX[i] + menuWidth[i] Then
menuFocusOpacityIndex = isMenuSelected 'isMenuSelected toggles onMouseDown 1/-1
menuFocusZoomX = menuWidth[i] / menuFocusWidth
menuFocusedOn = i 'determines which SubMenu[i] to open If isMenuSelected = 1
nextMenuFocus = menuFocusedOn
EndIf
EndFor

If isMenuSelected = 1 And nextMenuFocus <> currentMenuFocus Then 'change selected Menu focus
priorMenuFocus = currentMenuFocus
currentMenuFocus = nextMenuFocus
EndIf

ElseIf isMenuSelected = 1 Then 'lock the selected Menu and its opacity to show related SubMenu
menuFocusedOn = currentMenuFocus '...so that SubMenu stays open when mouse is off location
menuFocusOpacityIndex = 1
EndIf

'Update SubMenu Focus onMouseMove
subMenuFocusedOn = 0 'WIP
If isMenuSelected = 1 Then
subMenuFocusOpacity = 0

'Loop thru sub menu, as required. Sets subMenuFocus Top, Opacity & ZoomX. And sets subMenuBackgroundZoomY
If mseX >= menuFocusX[menuFocusedOn] And mseX <= menuFocusX[menuFocusedOn] + subMenuWidth[menuFocusedOn] Then
If mseY > menuHeight And mseY <= menuHeight * (Array.GetItemCount(subMenu[menuFocusedOn]) + 1) Then
For i = Array.GetItemCount(subMenu[menuFocusedOn]) To 1 Step -1
If mseY >= menuHeight * i And mseY <= menuHeight * (i+1) Then
subMenuFocusTop = menuHeight * i
subMenuFocusedOn = i
EndIf
EndFor
subMenuFocusOpacity = 100
EndIf
subMenuFocusZoomX = subMenuWidth[menuFocusedOn] / menuFocusWidth
subMenuBackgroundZoomY = (Array.GetItemCount(subMenu[menuFocusedOn]) * menuHeight) / menuHeight
EndIf
Else
subMenuFocusOpacity = 0
EndIf
EndSub

Sub Menu_UpdateOnMouseDown
'Update Menu onMouseDown
If menuFocusedOn <> 0 Then
isMenuSelected = -isMenuSelected 'toggle on Click 1/-1
menuFocusOpacityIndex = isMenuSelected
EndIf

priorMenuFocus = 0
If isMenuSelected = -1 Then
If mseY > menuHeight Or mseX > totalMenuWidth Then
menuFocusOpacityIndex = 0
priorMenuFocus = menuFocusedOn
EndIf
EndIf

'Update SubMenu onMouseDown
If isMenuSelected = 1 Then
subMenuOpacity = 100
subMenuFocusZoomX = subMenuWidth[menuFocusedOn] / menuFocusWidth
subMenuBackgroundZoomY = (Array.GetItemCount(subMenu[menuFocusedOn]) * menuHeight) / menuHeight '******
Else
subMenuOpacity = 0
subMenuFocusOpacity = 0
EndIf

'OnMenuClicked event {non async Event} - pass the selection to If menuClicked
If isMenuSelected = -1 And subMenuFocusedOn > 0 Then
menuSelection = menuFocusedOn
subMenuSelection = subMenuFocusedOn

If menuSelection > 0 And subMenuSelection > 0 Then '***OnMenuClicked Event***
menuClicked = "True" 'non async event. Sequential
EndIf

menuFocusedOn = 0
subMenuFocusedOn = 0

'Unlock the selected Menu after no mouseMove between clicks
ElseIf isMenuSelected = -1 And mseY > menuHeight Or isMenuSelected = -1 And mseX > totalMenuWidth Then '****wip here
menuFocusedOn = 0
EndIf
EndSub

Sub Menu_UpdateFrame
'Update Menu Focus - Main Bar
Shapes.SetOpacity(menuFocus, menuFocusOpacity[menuFocusOpacityIndex])
Shapes.Zoom(menuFocus, menuFocusZoomX, 1)
Shapes.Move(menuFocus, menuFocusX[menuFocusedOn] + (menuWidth[menuFocusedOn] - menuFocusWidth) /2, 0)

'Update SubMenu - Got Focus
Shapes.SetOpacity(subMenuBackground, subMenuOpacity)
Shapes.Zoom(subMenuBackground, subMenuFocusZoomX, subMenuBackgroundZoomY)

Y = menuHeight + ((Array.GetItemCount(subMenu[menuFocusedOn]) * menuHeight) - menuHeight) / 2 'wip put in update program
Shapes.Move(subMenuBackground, menuFocusX[menuFocusedOn] + (subMenuWidth[menuFocusedOn] - menuFocusWidth) /2, Y)

For i = Array.GetItemCount(subMenu[menuFocusedOn]) To 1 Step -1
Shapes.SetOpacity(subMenuText[menuFocusedOn][i], subMenuOpacity)
EndFor

'Update SubMenu - Lost Focus
Shapes.SetOpacity(subMenuBackground[priorMenuFocus], 0) 'check if index needed
For i = Array.GetItemCount(subMenu[priorMenuFocus]) To 1 Step -1
Shapes.SetOpacity(subMenuText[priorMenuFocus][i], 0)
EndFor

'Update SubMenu Focus
Shapes.SetOpacity(subMenuFocus, subMenuFocusOpacity)
Shapes.Zoom(subMenuFocus, subMenuFocusZoomX, 1)
Shapes.Move(subMenuFocus, menuFocusX[menuFocusedOn] + (subMenuWidth[menuFocusedOn] - menuFocusWidth) /2, subMenuFocusTop)
EndSub