Discussion Native Menu System

  • Wednesday, August 22, 2012 9:39 AM
     
      Has Code

    Hi Everyone,

    As far as I am aware no extensions (the ones I use anyway) have the ability to add a File Menu to the graphics window so I have just started writing my own in native SmallBasic.

    It is pretty basic at the moment as it only draws the menu, but I will be looking at adding the ability to add sub menus, events on click and resizing of the window.

    If you have any suggestions of how I can improve on what I've done so far please let me know.

    InitializeWindow()
    Menu()
    Sub InitializeWindow
      GfxWindowX = 800
      GfxWindowY = 600
      
      GraphicsWindow.Width = GfxWindowX
      GraphicsWindow.Height = GfxWindowY
      
      GraphicsWindow.Left = (Desktop.Width / 2) - (GfxWindowX / 2)
      GraphicsWindow.Top = (Desktop.Height / 2) - (GfxWindowY / 2)
      
      GraphicsWindow.CanResize = "False"
      GraphicsWindow.Title = "Menu System"
    EndSub
    Sub Menu
      'Menu List
      MenuItem[1] = "File"
      MenuItem[2] = "Exit"
      
      'Menu Variables
      ItemCount = Array.GetItemCount(MenuItem)
      MenuItemWidth = GfxWindowX / ItemCount
      MenuItemHeight = 20
      MenuItemX = 0
      MenuItemY = 0
      
      'Draw Menu
      For I = 1 To ItemCount
        
        'Buttons
        GraphicsWindow.BrushColor = "DarkGray"
        
        MenuItem[I + "Button"] = Shapes.AddRectangle(MenuItemWidth, MenuItemHeight)
        Shapes.Move(MenuItem[I + "Button"], MenuItemX, MenuItemY)
        
        'Text
        GraphicsWindow.BrushColor = "Black"
        
        MenuItem[I + "Text"] = Shapes.AddText(MenuItem[I])
        Shapes.Move(MenuItem[I + "Text"], ((MenuItemWidth / 2) + MenuItemX) - (Text.GetLength(MenuItem[I]) + 12), MenuItemY + 2)
        MenuItemX = MenuItemX + MenuItemWidth
      EndFor
    EndSub  

    To add addtional menus simply add another variable to the MenuItem Array.

All Replies

  • Wednesday, August 22, 2012 10:52 AM
     
      Has Code

    Menu now updates when resizing the window.

    InitializeWindow()
    Menu()
    'Events
    Timer.Tick = OnTick
    Timer.Interval = 100
    Sub InitializeWindow
      GfxWindowX = 800
      GfxWindowY = 600
      
      GraphicsWindow.Width = GfxWindowX
      GraphicsWindow.Height = GfxWindowY
      
      GraphicsWindow.Left = (Desktop.Width / 2) - (GfxWindowX / 2)
      GraphicsWindow.Top = (Desktop.Height / 2) - (GfxWindowY / 2)
      
      GraphicsWindow.CanResize = "True"
      GraphicsWindow.Title = "Menu System"
    EndSub
    Sub Menu
      'Menu List
      MenuItem[1] = "File"
      MenuItem[2] = "Settings"
      MenuItem[3] = "Exit"
      
      'Menu Variables
      ItemCount = Array.GetItemCount(MenuItem)
      MenuItemWidth = GfxWindowX / ItemCount
      MenuItemHeight = 20
      MenuItemX = 0
      MenuItemY = 0
      
      'If resizing remove old menu
      If RedrawMenu = 1 Then
        For I = 1 To ItemCount
          Shapes.Remove(MenuButton[I])
          Shapes.Remove(MenuText[I])
        EndFor
      EndIf
      
      'Draw Menu
      For I = 1 To ItemCount
        'Buttons
        GraphicsWindow.BrushColor = "DarkGray"
        
        MenuButton[I] = Shapes.AddRectangle(MenuItemWidth, MenuItemHeight)
        Shapes.Move(MenuButton[I], MenuItemX, MenuItemY)
        
        'Text
        GraphicsWindow.BrushColor = "Black"
        
        MenuText[I] = Shapes.AddText(MenuItem[I])
        Shapes.Move(MenuText[I], ((MenuItemWidth / 2) + MenuItemX) - (Text.GetLength(MenuItem[I]) + 12), MenuItemY + 2)
        MenuItemX = MenuItemX + MenuItemWidth
        
        'Set redraw to false
        RedrawMenu = 0
      EndFor
    EndSub  
    sub OnTick
      
      'Check If Resize 
      If GraphicsWindow.Width <> GfxWindowX Then
        RedrawMenu = 1
        GfxWindowX = GraphicsWindow.Width
        GfxWindowY = GraphicsWindow.Height
        
        Menu()
      EndIf
    EndSub
        
    If you have any better suggestions on how to implement anything please let me know.
  • Wednesday, August 22, 2012 5:56 PM
    Answerer
     
     
    Nice! is it supposed to do anything yet?

    One thing that is impossible is impossible no matter if it is proven so first.

  • Wednesday, August 22, 2012 10:07 PM
     
     
    Haha, no not yet. I didn't get enough time this morning to work on it as I've been to work all day. I will hopfully get more done tonight.