Better handling of Custom Events/EventTypes

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Better handling of Custom Events/EventTypes

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Better handling of Custom Events/EventTypes

Post 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
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Better handling of Custom Events/EventTypes

Post 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 
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Better handling of Custom Events/EventTypes

Post by jacdelad »

Yes, I know. But that's a thing everyone has to apply. The other one would be foolproof.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Better handling of Custom Events/EventTypes

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Better handling of Custom Events/EventTypes

Post 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???
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Better handling of Custom Events/EventTypes

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Better handling of Custom Events/EventTypes

Post 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...
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Better handling of Custom Events/EventTypes

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Better handling of Custom Events/EventTypes

Post 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 "----"
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply