Page 1 of 1

Better handling of Custom Events/EventTypes

Posted: Thu Apr 15, 2021 10:07 am
by jacdelad
Hello,
I may be wrong, but I feel like we need a better handling for custom events/eventtypes. The problem is using more than one include/module... which uses #PB_Event[Type]_FirstCustomValue, because of using a value multiple times.

I think a good approach would be either
- a function for registering the constants (like RegisterEvent[Type](count), returning the first event (you can iterate the next ones) and raising #PB_Event[Type]_FirstCustomValue each time by "count")
- or a new type of enumeration, like "EnumerateEvent[Type] #PB_Event[Type]_FirstCustomValue" with the same effect plus setting the constants.

I'd prefer the second one, it would make like a lot easier. The next time it is called #PB_Event[Type]_FirstCustomValue would have already the higher value.

Re: Better handling of Custom Events/EventTypes

Posted: Thu Apr 15, 2021 10:16 am
by NicTheQuick
Usually you are meant to use named enumerations for that:

Code: Select all

; Include1
Enumeration CustomEvent
	#first = #PB_Event_FirstCustomValue
	#second
EndEnumeration

; Include2
Enumeration CustomEvent
	#third
EndEnumeration

; Main

Debug #first
Debug #second
Debug #third
And if you are not sure where to put the first named enumeration that begins with #PB_Event_FirstCustomValue, just use a CompilerIf Defined:

Code: Select all

; Include1
CompilerIf Not Defined(CustomEvent, #PB_Enumeration)
	Enumeration CustomEvent
		#CustomEvent_Base = #PB_Event_FirstCustomValue - 1
	EndEnumeration
CompilerEndIf
Enumeration CustomEvent
	#first
	#second
EndEnumeration

; Include2
CompilerIf Not Defined(CustomEvent, #PB_Enumeration)
	Enumeration CustomEvent
		#CustomEvent_Base = #PB_Event_FirstCustomValue - 1
	EndEnumeration
CompilerEndIf
Enumeration CustomEvent
	#third
EndEnumeration

; Main

Debug #PB_Event_FirstCustomValue
Debug #first
Debug #second
Debug #third

Re: Better handling of Custom Events/EventTypes

Posted: Thu Apr 15, 2021 10:48 am
by Demivec
jacdelad wrote: Thu Apr 15, 2021 10:07 am- or a new type of enumeration, like "EnumerateEvent[Type] #PB_Event[Type]_FirstCustomValue" with the same effect plus setting the constants.

I'd prefer the second one, it would make like a lot easier. The next time it is called #PB_Event[Type]_FirstCustomValue would have already the higher value.
Named enumerations already exist in PureBasic. They can also be placed in a 'Common' module so that they can be accessed from different modules that are included.

Code: Select all

DeclareModule Common
  Enumeration enumCustomEvents #PB_Event_FirstCustomValue
    #Common_Event_0
  EndEnumeration
  
  Enumeration enumFonts 1
  EndEnumeration
  
  Enumeration enumGadgets 1
  EndEnumeration
  
  Enumeration enumWindows 1
  EndEnumeration
  
  Enumeration enumSprites 1
  EndEnumeration
  
  Enumeration enumMenus 1
  EndEnumeration
  
  ;and so on
EndDeclareModule

Module Common
EndModule



DeclareModule thing_1
  UseModule Common
  
  Enumeration enumCustomEvents
    #special_event_big
    #Special_event_small
  EndEnumeration
EndDeclareModule

Module thing_1
  ;code
EndModule

DeclareModule thing_2
  UseModule Common
  
  Enumeration enumCustomEvents
    #Special_event_wide
    #Special_event_thin
  EndEnumeration
EndDeclareModule

Module thing_2
  ;code
EndModule

Debug Common::#Common_Event_0
Debug thing_1::#special_event_big
Debug thing_1::#Special_event_small
Debug thing_2::#Special_event_wide
Debug thing_2::#Special_event_thin 

Re: Better handling of Custom Events/EventTypes

Posted: Mon Apr 19, 2021 12:02 pm
by jacdelad
Yes, I know. But that's a thing everyone has to apply. The other one would be foolproof.

Re: Better handling of Custom Events/EventTypes

Posted: Tue Jul 20, 2021 8:06 am
by collectordave
I use modules and named constants.

I give each module a number, the common module starts at 0 then for me a step of 100 has been enough.

When others are helping they are mostly writing a module to be included which means that they have a new module number.

As here:

Code: Select all

DeclareModule APP
  
Enumeration CustomEvents #PB_Event_FirstCustomValue
  #QuitApplication
  #NextEvent 
EndEnumeration

EndDeclareModule

Module APP
  
  
EndModule


DeclareModule Testvals

  Declare Showvals()

EndDeclareModule

Module Testvals
  
  
  #ModuleNumber = 100 ;This modules number
  
  
  Enumeration CustomEvents  #PB_Event_FirstCustomValue + #ModuleNumber 
    #QuitModule
  EndEnumeration

  Procedure ShowVals()
    
    ;Global Events available in all portions of the application
    Debug APP::#QuitApplication
    
    ;Only available inside Module TestVals
    Debug ""
    Debug #QuitModule

  EndProcedure
  
  
EndModule


TestVals::Showvals()
APP is the global module and is accessible to all others.


It can be applied when not using modules each one programming can have a number assigned which is added to the first custom value.

Re: Better handling of Custom Events/EventTypes

Posted: Wed Mar 06, 2024 5:32 am
by jacdelad
Ok, so I stumbled over this again. I'm sure the error lies by me, but I need someone to explain this, please.
I did the thing with the common module, like suggested by some people:

Code: Select all

;The Module
DeclareModule APP
  
  CompilerIf Not Defined(CustomEvent, #PB_Enumeration)
    Enumeration CustomEvent
      #CustomEvent_Base = #PB_Event_FirstCustomValue - 1
    EndEnumeration
  CompilerEndIf
  Enumeration CustomEvent
    #PB_Event_DoSomething
  EndEnumeration
  
EndDeclareModule

Module APP
  
  
EndModule

;The Main Program
CompilerIf Not Defined(CustomEvent, #PB_Enumeration)
  Enumeration CustomEvent
    #CustomEvent_Base = #PB_Event_FirstCustomValue - 1
  EndEnumeration
CompilerEndIf
Enumeration CustomEvent
  #PB_Event_DoSomethingElse
EndEnumeration

Debug APP::#PB_Event_DoSomething
Debug #PB_Event_DoSomethingElse
Yet, both events get the same value, which surely has something to do, that one is declared in the module. But how, do I solve this without adding dummy values here and there???

Re: Better handling of Custom Events/EventTypes

Posted: Wed Mar 06, 2024 6:46 am
by Demivec
You need to do all of the enumerations through the common module, whether inside or outside of a module.

Modules can't access the enumerations in the main scope but the main scope can access the enumerations in the 'public' scope of a module.

Re: Better handling of Custom Events/EventTypes

Posted: Wed Mar 06, 2024 10:58 pm
by jacdelad
Ok, so I used the search function in the forum and also googled it. The only thing I found out, is that I stumbled across the common module several times. I still don't know how to do this, nor did the search results help. Is there a really good thread to explain this? And why is it done this way? Wouldn't it be more logical if the whole program uses ONE #PB_Event_FirstCustomValue? I don't see why every module needs its own...

Re: Better handling of Custom Events/EventTypes

Posted: Thu Mar 07, 2024 2:00 am
by Demivec
jacdelad wrote: Wed Mar 06, 2024 10:58 pm Wouldn't it be more logical if the whole program uses ONE #PB_Event_FirstCustomValue? I don't see why every module needs its own...
There is only one #PB_Event_FirstCustomValue. It's a constant. That is why the solution is a named enumeration. A named enumeration generates sequential values.

The next part of the solution is for the enumeration to be accessible from the needed scopes.

If access is needed:
  • only in the main scope -- the enumeration is placed in either the main scope or in a DeclareModule's scope.
  • only in a single module's scope -- the enumeration is placed in either the module's own DeclareModule's scope or it's private module scope or separately in external module's DeclareModule scope (i.e. a common module).
  • in the main scope and within at least one module -- the enumeration is placed in a single module's DeclareModule's scope (i.e. a common module).
  • only in modules but more than one module -- the enumeration is placed in a single module's DeclareModule's scope (i.e. a common module).
Enumerations placed in a DeclareModule need the module's prefix or UseModule to access the named enumeration without the prefix.

Re: Better handling of Custom Events/EventTypes

Posted: Thu Mar 07, 2024 5:09 pm
by mk-soft
You only need one global module which is attached everywhere.

Example:

Code: Select all

;-TOP

DeclareModule GlobalCommon
  
  Enumeration Windows
    ;
  EndEnumeration
  
  Enumeration MenuBar
    ;
  EndEnumeration
  
  Enumeration MenuItems
    ;
  EndEnumeration
  
  Enumeration Gadgets
    
  EndEnumeration
  
  Enumeration StatusBar
    ;
  EndEnumeration
  
  Enumeration CustomEvent #PB_Event_FirstCustomValue
    ;
  EndEnumeration
  
EndDeclareModule

Module GlobalCommon
  ;
EndModule

; ****

DeclareModule Dialog
  
  UseModule GlobalCommon
  
  Enumeration Windows
    #Dialog
  EndEnumeration
  
  Enumeration Gadgets
    #DialogButton
  EndEnumeration
  
  Enumeration CustomEvent
    #MyEventDialog_XYZ
  EndEnumeration
  
EndDeclareModule

Module Dialog
  Debug "Dialog"
  Debug "Window: " + #Dialog
  Debug "Gadget: " + #DialogButton
  Debug "CustomEvent: " +#MyEventDialog_XYZ
  Debug "----"

EndModule

; ****

DeclareModule Dialog2
  
EndDeclareModule

Module Dialog2
  
  UseModule GlobalCommon
  
  Enumeration Windows
    #Dialog
  EndEnumeration
  
  Enumeration Gadgets
    #DialogButton
  EndEnumeration
  
  Enumeration CustomEvent
    #MyEventDialog_XYZ
  EndEnumeration
  
  Debug "Dialog2"
  Debug "Window: " + #Dialog
  Debug "Gadget: " + #DialogButton
  Debug "CustomEvent: " +#MyEventDialog_XYZ
  Debug "----"
  
EndModule

; ****

; Main Scope

UseModule GlobalCommon

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainButton
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Enumeration CustomEvent
  #MyEventMain_XYZ
EndEnumeration

Debug "MainScope"
Debug "Window: " + #Main
Debug "Gadget: " + #MainButton
Debug "CustomEvent: " +#MyEventMain_XYZ
Debug "----"