Page 1 of 1

Use one menu for multiple windows

Posted: Sun Mar 24, 2024 4:26 pm
by Distorted Pixel
Hi,

Is it possible to use the same menu for multiple windows. I have searched for an answer, but have not found an answer for my specific question, all I have found is stuff about managing multiple windows and nothing about the same menu used across multiple windows. I want to use literally one menu for all windows. Only 1 - 2 windows will be loaded or seen at any given time, but I want to use the same menu for all but the first 6 -7 windows (The first 6 -7 windows are the game setup windows. No need for the menu in them.)

After the setup windows I want to have the menu available in all other windows except the game windows itself where the 2 teams will be playing against each other. The game window will have it's own menu.

[Edit] - I would like to not have to create the same menu many times in the code for all windows that need it. I would be senseless, time consuming and a lot of typing. I have even thought about using PureVision for all creating all the windows and the menus (Only 2 menus in the game. One main menu for multiple windows and the other is for during the game action)

Re: Use one menu for multiple windows

Posted: Sun Mar 24, 2024 4:54 pm
by AZJIO

Code: Select all

If OpenWindow(0, 200, 200, 200, 100, "Example Menu")
	If CreateMenu(0, WindowID(0))
		MenuTitle("Project")
		MenuItem(1, "Open" + Chr(9) + "Ctrl+O")
		MenuItem(2, "Save" + Chr(9) + "Ctrl+S")
		MenuItem(3, "Save as" + Chr(9) + "Ctrl+A")
		MenuItem(4, "Close" + Chr(9) + "Ctrl+C")
	EndIf
EndIf

If OpenWindow(1, 400, 200, 200, 100, "Example Menu")
	SetMenu_(WindowID(1), MenuID(0))
EndIf

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			End
		Case #PB_Event_Menu
			Select EventMenu()
				Case 1 : Debug "Menu: Open"
				Case 2 : Debug "Menu: Save"
				Case 3 : Debug "Menu: Save As"
				Case 4 : End
			EndSelect
	EndSelect
ForEver

Re: Use one menu for multiple windows

Posted: Sun Mar 24, 2024 5:18 pm
by Distorted Pixel
AZJIO wrote: Sun Mar 24, 2024 4:54 pm

Code: Select all

If OpenWindow(0, 200, 200, 200, 100, "Example Menu")
	If CreateMenu(0, WindowID(0))
		MenuTitle("Project")
		MenuItem(1, "Open" + Chr(9) + "Ctrl+O")
		MenuItem(2, "Save" + Chr(9) + "Ctrl+S")
		MenuItem(3, "Save as" + Chr(9) + "Ctrl+A")
		MenuItem(4, "Close" + Chr(9) + "Ctrl+C")
	EndIf
EndIf

If OpenWindow(1, 400, 200, 200, 100, "Example Menu")
	SetMenu_(WindowID(1), MenuID(0))
EndIf

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			End
		Case #PB_Event_Menu
			Select EventMenu()
				Case 1 : Debug "Menu: Open"
				Case 2 : Debug "Menu: Save"
				Case 3 : Debug "Menu: Save As"
				Case 4 : End
			EndSelect
	EndSelect
ForEver
Thank you AZJIO,
One more question I have is if the first window where the creation of the menu is done is closed, can you still use the the menu throughout the rest of the game in other windows? I ask this because I will have all windows using the menu in their own "Include" file

Re: Use one menu for multiple windows

Posted: Sun Mar 24, 2024 5:33 pm
by AZJIO
Distorted Pixel wrote: Sun Mar 24, 2024 5:18 pm if the first window where the creation of the menu is done is closed, can you still use the the menu
To do this, you need to close the window and see what happens. You can conduct the tests yourself.

Code: Select all

If OpenWindow(0, 200, 200, 200, 100, "Example Menu")
	If CreateMenu(0, WindowID(0))
		MenuTitle("Project")
		MenuItem(1, "Open" + Chr(9) + "Ctrl+O")
		MenuItem(2, "Save" + Chr(9) + "Ctrl+S")
		MenuItem(3, "Save as" + Chr(9) + "Ctrl+A")
		MenuItem(4, "Close" + Chr(9) + "Ctrl+C")
	EndIf
EndIf

If OpenWindow(1, 400, 200, 200, 100, "Example Menu")
	SetMenu_(WindowID(1), MenuID(0))
EndIf

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			Select EventWindow()
				Case 0 : CloseWindow(0)
				Case 1 : End
			EndSelect
		Case #PB_Event_Menu
			Select EventMenu()
				Case 1 : Debug "Menu: Open"
				Case 2 : Debug "Menu: Save"
				Case 3 : Debug "Menu: Save As"
				Case 4 : End
			EndSelect
	EndSelect
ForEver

Re: Use one menu for multiple windows

Posted: Sun Mar 24, 2024 5:47 pm
by Distorted Pixel
AZJIO wrote: Sun Mar 24, 2024 5:33 pm You can conduct the tests yourself.
Yes, as soon as you posted your example code I started to test things. Currently I have been trying to implement your example into my project and it is failing to show the menu in the second window.

Currently I am using PureVision to create the interface of the game, but so far I have failed to get your example working. I will keep working on it. If I can't get it to work with PureVision files, I will code the entire game manually. I do have a project without using PureVision also.

Re: Use one menu for multiple windows

Posted: Mon Mar 25, 2024 8:44 pm
by boddhi
Distorted Pixel wrote: [...] Currently I have been trying to implement your example into my project and it is failing to show the menu in the second window. [...]
The help manual may be the answer! :wink:

Technically, a menu must be attached to a window. Therefore, the same menu cannot be "shared" by several windows. And, consequently, when you close the window, you also release the menu associated with it.

If you want to have the same menu (i.e. with the same items) on several windows, one way is to create, for example, a procedure - which will have the task of creating each menu - with at least two arguments, one of which will receive the menu number (i.e. the PB Id) and the other, the number of the window to which it will be attached.

Re: Use one menu for multiple windows

Posted: Mon Mar 25, 2024 9:28 pm
by Fips
Maybe you don't really need the same menue but could instead create several alike menues via a procedure like this:

Code: Select all

EnableExplicit

Enumeration Windows
  #Win_0
  #Win_1
  #Win_2
EndEnumeration

Enumeration Menues
  #Menue_Close
  #Menue_Test
EndEnumeration


Procedure onCloseWindow()
  Protected.i winNr = EventWindow()
  
  CloseWindow(winNr)
  
  If IsWindow(#Win_0) = #False And IsWindow(#Win_1) = #False And IsWindow(#Win_2) = #False
    End  
  EndIf
  
EndProcedure

Procedure onClickedTest()
  Protected.i winNr = EventWindow()
  
  Debug "Menue-Item 'Test' was clicked on window " + Str(winNr) + ". The MenueNr is " + Str(GetWindowData(winNr)) + "." 
  
EndProcedure


Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure


Procedure OpenTestWindow(winNr.i)
  
  If OpenWindow(winNr, Random(400), Random(400), 400, 400, "Window " + Str (winNr))
    CreateTestMenue(winNr)
    
    BindEvent(#PB_Event_CloseWindow, @onCloseWindow(), winNr)
    
  EndIf
  
  
  
EndProcedure

OpenTestWindow(#Win_0)
OpenTestWindow(#Win_1)
OpenTestWindow(#Win_2)


Repeat
  WaitWindowEvent()
  
ForEver


Re: Use one menu for multiple windows

Posted: Tue Mar 26, 2024 2:45 pm
by Distorted Pixel
Fips wrote: Mon Mar 25, 2024 9:28 pm Maybe you don't really need the same menue but could instead create several alike menues via a procedure like this:

Code: Select all

EnableExplicit

Enumeration Windows
  #Win_0
  #Win_1
  #Win_2
EndEnumeration

Enumeration Menues
  #Menue_Close
  #Menue_Test
EndEnumeration


Procedure onCloseWindow()
  Protected.i winNr = EventWindow()
  
  CloseWindow(winNr)
  
  If IsWindow(#Win_0) = #False And IsWindow(#Win_1) = #False And IsWindow(#Win_2) = #False
    End  
  EndIf
  
EndProcedure

Procedure onClickedTest()
  Protected.i winNr = EventWindow()
  
  Debug "Menue-Item 'Test' was clicked on window " + Str(winNr) + ". The MenueNr is " + Str(GetWindowData(winNr)) + "." 
  
EndProcedure


Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure


Procedure OpenTestWindow(winNr.i)
  
  If OpenWindow(winNr, Random(400), Random(400), 400, 400, "Window " + Str (winNr))
    CreateTestMenue(winNr)
    
    BindEvent(#PB_Event_CloseWindow, @onCloseWindow(), winNr)
    
  EndIf
  
  
  
EndProcedure

OpenTestWindow(#Win_0)
OpenTestWindow(#Win_1)
OpenTestWindow(#Win_2)


Repeat
  WaitWindowEvent()
  
ForEver

Actually I do need the exact same menu for all screens other than the game screen where the 2 teams are playing against each other. If I have to I'll just copy and paste the same menu code into each window and set the correct window and menu numbers

Re: Use one menu for multiple windows

Posted: Tue Mar 26, 2024 3:43 pm
by Quin
Distorted Pixel wrote: Tue Mar 26, 2024 2:45 pm Actually I do need the exact same menu for all screens other than the game screen where the 2 teams are playing against each other. If I have to I'll just copy and paste the same menu code into each window and set the correct window and menu numbers
No, you don't need to do that, the procedure way can work for that. In fact, that's what it was designed for. Look here:
Fips wrote: Mon Mar 25, 2024 9:28 pm

Code: Select all

Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure
This procedure is what you call instead of copy-pasting :wink: just pass the window's number.

Re: Use one menu for multiple windows

Posted: Wed Mar 27, 2024 12:08 am
by Distorted Pixel
Quin wrote: Tue Mar 26, 2024 3:43 pm
Distorted Pixel wrote: Tue Mar 26, 2024 2:45 pm Actually I do need the exact same menu for all screens other than the game screen where the 2 teams are playing against each other. If I have to I'll just copy and paste the same menu code into each window and set the correct window and menu numbers
No, you don't need to do that, the procedure way can work for that. In fact, that's what it was designed for. Look here:
Fips wrote: Mon Mar 25, 2024 9:28 pm

Code: Select all

Procedure CreateTestMenue(winNr.i)
  Protected.i menueNr 
  menueNr = CreateMenu(#PB_Any, WindowID(winNr))
  If menueNr
    MenuTitle("Menue")
    MenuItem(#Menue_Test, "Test")
    MenuItem(#Menue_Close, "Close")
    
    BindEvent(#PB_Event_Menu, @onCloseWindow(), winNr, #Menue_Close)
    BindEvent(#PB_Event_Menu, @onClickedTest(), winNr, #Menue_Test)
    
    ; if you need the menue number later on you could save it in window data
    
    SetWindowData(winNr, menueNr)
    
  EndIf
  
EndProcedure
This procedure is what you call instead of copy-pasting :wink: just pass the window's number.
Thank you Fips, I will give it a try