Form Designer generated event procedure

Just starting out? Need help? Post your questions and find answers here.
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Form Designer generated event procedure

Post by Distorted Pixel »

Hi,

Is this Form Designer tutorial example still relevent with 5.73 LTS version? It was posted back in 2015

https://www.purebasic.fr/english/viewtopic.php?t=64684

I'm trying to learn how to use the form designer and include and use the file in my game, but I'm not sure what to put in the pb file to use the pbf file. I only want to use one window for entire game. My plan is the remove or delete the current stuff in the window and add the next stuff for the next screen, but first I need to learn how to use the generated pbf file and call it in a source file to use it

This is what I have and want to use. I need to add a window timer probably because I want to just show it for 4-5 seconds and then it disappear. I might want to add the event of if the left mouse button is clicked it will end the screen early and move to the next screen in the same window
I'm also needing to do this on my mac and post back soon. I haven't had time to work on the mac LOL

Code: Select all

Global Window_0

Global Image_1

Global Img_Window_0_0

UsePNGImageDecoder()

Img_Window_0_0 = LoadImage(#PB_Any,"C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM.png")


Procedure OpenWindow_0(x = 0, y = 0, width = 1920, height = 1080)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu)
  Image_1 = ImageGadget(#PB_Any, 0, 0, 1920, 1080, ImageID(Img_Window_0_0))
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Form Designer generated event procedure

Post by Distorted Pixel »

I have been able to get my event source "SplashScreen.pb" file figure out I hope, but I haven't ran it yet because I have another issue in my next event source for my next screen created from another form.
This is what I put in my button event source and it accepted it just checking the syntax and went on to next file where I have another problem, but I should be able to figure it out I hope.

Code: Select all

XIncludeFile "SplashScreen.pbf"



Procedure buttonEvent(eventType.i)
  event = #PB_EventType_LeftClick  
EndProcedure
All I want to do is detect for a left mouse button click so you can exit the splash screen quicker and move on. I still need to finish the code so it exits I think.

Edit: Actually I need to change things about the above code. Ouch! I'll figure it out LOL
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Form Designer generated event procedure

Post by Distorted Pixel »

I'm still working on getting events to work. Instead of using the form designer I have decided to manually code things.
I have a question. If I want to use the same window for everything, how do delete what is there from the first windows event stuff? I know there isn't something like DeleteImage(1) or DeleteGadget(1) commands so do I use something like this?

Code: Select all

FreeImage(1)
FreeGadget(1)
And does PureBasic automatically sync or refresh the window? Because I don't see a sync command anywhere.
Also, in every mouse button click detection example I see they create a ButtonGadget, but if all you want to do is detect a mouse click anywhere on screen not needing to click any button so the program will automatically be interupted where it is and moves on to the next thing to be able to skip something then why put a ButtonGadget in?
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Form Designer generated event procedure

Post by spikey »

Distorted Pixel wrote: Sun Jan 09, 2022 2:09 am Is this Form Designer tutorial example still relevent with 5.73 LTS version? It was posted back in 2015
Yes it's still relevant, it isn't complete however. It makes no mention of event binding, this is an alternative method of handling events which is more appropriate for use in a modular program. It won't invalidate your learning but is something to be aware of for future reference if you start using modules at some point. See https://www.purebasic.com/documentation ... event.html
Distorted Pixel wrote: Sun Jan 09, 2022 2:09 am If I want to use the same window for everything, how do delete what is there from the first windows event stuff?
I wouldn't recommend going this way, it will make your code very complicated eventually and difficult to manage. It's easier to have a window for each separate purpose and keep all the code pertaining to each one separate. Then you just open the window for the purpose your user requests.
If you do want to have a multi-function window and not multiple windows, it still makes sense to separate things out into different functional sections and just show/hide the irrelevant bits at the appropriate times.
If you do decide to keep on the path you outline then, yes, the 'Free*' instructions are the ones you are looking for.
Distorted Pixel wrote: Sun Jan 09, 2022 2:09 am And does PureBasic automatically sync or refresh the window?
Yes, it does providing it isn't doing something else in a loop, or a processor intensive task such as loading a large file into memory. Updates will be suspended for the duration of the loop/task but the program should catch up eventually. Beware of infinite loops though.
However not everything has "persistence of data", so for example if you draw directly on the window background these drawing actions will get lost when the window is covered/minimized because a window doesn't have persistence data for its background. Other gadgets do have persistence appropriate to their purposes.
Distorted Pixel wrote: Sun Jan 09, 2022 2:09 amthen why put a ButtonGadget in?
For user feedback and persistence as mentioned above. You know how your program works because you wrote it. Someone else operating your program for the first time won't know anything about how it is supposed to work. If you just present a window with no feedback you'd have to train all users to use every part of your application and they'd still find it difficult to use any feature that they didn't use on a daily basis.
Placing a button gives the user a reasonable expectation that clicking it will cause something specific to happen. Similarly placing a text box gives the user a realistic expectation that they are expected to enter some text. A calendar gadget - a date...
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Form Designer generated event procedure

Post by Distorted Pixel »

spikey wrote: Tue Jan 11, 2022 1:58 pm
Distorted Pixel wrote: Sun Jan 09, 2022 2:09 am Is this Form Designer tutorial example still relevent with 5.73 LTS version? It was posted back in 2015
Yes it's still relevant, it isn't complete however. It makes no mention of event binding, this is an alternative method of handling events which is more appropriate for use in a modular program. It won't invalidate your learning but is something to be aware of for future reference if you start using modules at some point. See https://www.purebasic.com/documentation ... event.html
Distorted Pixel wrote: Sun Jan 09, 2022 2:09 am If I want to use the same window for everything, how do delete what is there from the first windows event stuff?
I wouldn't recommend going this way, it will make your code very complicated eventually and difficult to manage. It's easier to have a window for each separate purpose and keep all the code pertaining to each one separate. Then you just open the window for the purpose your user requests.
If you do want to have a multi-function window and not multiple windows, it still makes sense to separate things out into different functional sections and just show/hide the irrelevant bits at the appropriate times.
If you do decide to keep on the path you outline then, yes, the 'Free*' instructions are the ones you are looking for.
Distorted Pixel wrote: Sun Jan 09, 2022 2:09 am And does PureBasic automatically sync or refresh the window?
Yes, it does providing it isn't doing something else in a loop, or a processor intensive task such as loading a large file into memory. Updates will be suspended for the duration of the loop/task but the program should catch up eventually. Beware of infinite loops though.
However not everything has "persistence of data", so for example if you draw directly on the window background these drawing actions will get lost when the window is covered/minimized because a window doesn't have persistence data for its background. Other gadgets do have persistence appropriate to their purposes.
Distorted Pixel wrote: Sun Jan 09, 2022 2:09 amthen why put a ButtonGadget in?
For user feedback and persistence as mentioned above. You know how your program works because you wrote it. Someone else operating your program for the first time won't know anything about how it is supposed to work. If you just present a window with no feedback you'd have to train all users to use every part of your application and they'd still find it difficult to use any feature that they didn't use on a daily basis.
Placing a button gives the user a reasonable expectation that clicking it will cause something specific to happen. Similarly placing a text box gives the user a realistic expectation that they are expected to enter some text. A calendar gadget - a date...
Thank you for this information spikey,

I will thoroughly read it

I just don't understand why there has to be a gadget if the user of the program just wants to advance through the introduction because maybe they have played the game already and don't want to wait until the intro is done to get started, so if they can just click the left mouse button one time reguardless where the mouse is then they can skip the intro.

So, if I have to have a gadget to do this, do I create a hidden gadget as big as the window so they can click anywhere to get it to skip the intro? If the gadget is visible then they won't be able to see the splash screen.
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
mk-soft
Always Here
Always Here
Posts: 5389
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Form Designer generated event procedure

Post by mk-soft »

CanvasGadget !
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
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Form Designer generated event procedure

Post by spikey »

mk-soft wrote: Tue Jan 11, 2022 9:08 pmCanvasGadget !
Indeed. The CanvasGadget acts as a drawing surface and also supports interaction events:

Code: Select all

#Window = 0
#Canvas = 0
#Font = 0

If OpenWindow(#Window, 10, 10, 600, 500, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  If LoadFont(#Font, "Segoe Script", 14) = 0
    Debug "Font not available - pick one that is!"
    End
  EndIf
  
  CanvasGadget(#Canvas, 10, 10, 580, 480)
  
  StartDrawing(CanvasOutput(#Canvas))
  DrawingFont(FontID(#Font))
  
  Y = 30 
  X = 30 
  D = TextHeight("Py") + 4

  DrawText(X, Y, "Double, double toil and trouble;", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "Fire burn and caldron bubble.", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "Fillet of a fenny snake,", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "In the caldron boil and bake;", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "Eye of newt and toe of frog,", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "Wool of bat and tongue of dog,", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "Adder's fork and blind-worm's sting,", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "Lizard's leg and howlet's wing,", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "For a charm of powerful trouble,", #Black, #White)
  X + 2 : Y + D
  DrawText(X, Y, "Like a hell-broth boil and bubble.", #Black, #White)
  
  StopDrawing()
    
  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_CloseWindow
      Quit = #True
    EndIf    
    
    If Event = #PB_Event_Gadget And EventGadget() = #Canvas And EventType() = #PB_EventType_LeftClick
      Quit = #True
    EndIf    
    
  Until Quit = #True
  
EndIf
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Form Designer generated event procedure

Post by Distorted Pixel »

CanvasGadget !
I'm currently trying to use the CanvasGadget command. So far I have realized I need to put the CanvasGadget above the ImageGadget so that my SplashScreen image shows. Now somehow I need to do the event and event detection stuff. This is what I have so far, it obviously isn't complete, I need to finish the timer too

Code: Select all

Global ImgFile

UsePNGImageDecoder()

Procedure SplashScreen()
  
  If OpenWindow(0, 0, 0, 1920, 1080, "Ultimate Football Manager", #PB_Window_BorderLess | #PB_Window_MinimizeGadget)
    ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png")
    CanvasGadget(0, 0, 0, 1920, 1080, #PB_EventType_LeftClick)
    ImageGadget(0, 0, 0, 1920, 1080, ImageID(ImgFile))
  EndIf
  
  
  AddWindowTimer(0, 1, 3000)
  
  Repeat
   WindowEvent = WaitWindowEvent()
    
  Until WindowEvent = #PB_Event_CloseWindow
  
EndProcedure
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Demivec
Addict
Addict
Posts: 4089
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Form Designer generated event procedure

Post by Demivec »

Distorted Pixel wrote: Fri Jan 14, 2022 12:27 am
CanvasGadget !
I'm currently trying to use the CanvasGadget command. So far I have realized I need to put the CanvasGadget above the ImageGadget so that my SplashScreen image shows. Now somehow I need to do the event and event detection stuff. This is what I have so far, it obviously isn't complete, I need to finish the timer too

You're off to a decent start. I only want to nudge you on a few things as you learn.

You can eliminate the Image gadget as the Canvas gadget can display the image. Here is your WIP code with that minor change. I also removed the incorrect flag for the Canvas gadget.

Code: Select all

Global ImgFile

UsePNGImageDecoder()

Procedure SplashScreen()
  
  If OpenWindow(0, 0, 0, 1920, 1080, "Ultimate Football Manager", #PB_Window_BorderLess | #PB_Window_MinimizeGadget)
    ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png")
    CanvasGadget(0, 0, 0, 1920, 1080) 
    SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(ImgFile)) ;set image as canvas background 
  EndIf
  
  
  AddWindowTimer(0, 1, 3000)
  
  Repeat
   WindowEvent = WaitWindowEvent()
    
  Until WindowEvent = #PB_Event_CloseWindow
  
EndProcedure
@Edit: Added the missing attribute parameter. :oops:
Thanks for the heads up from Paul and Distorted Pixel.
Last edited by Demivec on Fri Jan 14, 2022 6:07 am, edited 3 times in total.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1251
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Form Designer generated event procedure

Post by Paul »

Distorted Pixel wrote: Tue Jan 11, 2022 9:07 pm I just don't understand why there has to be a gadget if the user of the program just wants to advance through the introduction because maybe they have played the game already and don't want to wait until the intro is done to get started, so if they can just click the left mouse button one time reguardless where the mouse is then they can skip the intro.
You could try binding a Left Click to the Window so you don't need a Gadget.

Code: Select all

Procedure DrawBox()
  If StartDrawing(WindowOutput(0))
    Box(0,0,640,480,Random($FFFFFF))
    StopDrawing()
  EndIf
EndProcedure

If OpenWindow(0,0,0,640,480,"Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  DrawBox()
  BindEvent(#PB_Event_LeftClick,@DrawBox(),0)
  Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Image Image
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Form Designer generated event procedure

Post by Distorted Pixel »

You're off to a decent start. I only want to nudge you on a few things as you learn.

You can eliminate the Image gadget as the Canvas gadget can display the image. Here is your WIP code with that minor change. I also removed the incorrect flag for the Canvas gadget.

Code: Select all

Global ImgFile

UsePNGImageDecoder()

Procedure SplashScreen()
  
  If OpenWindow(0, 0, 0, 1920, 1080, "Ultimate Football Manager", #PB_Window_BorderLess | #PB_Window_MinimizeGadget)
    ImgFile = LoadImage(#PB_Any, "C:\Users\Brian\Documents\Pure Basic Projects\Ultimate Football Manager\Media\UFM1.png")
    CanvasGadget(0, 0, 0, 1920, 1080) 
    SetGadgetAttribute(0, ImageID(ImgFile)) ;set image as canvas background 
  EndIf
  
  
  AddWindowTimer(0, 1, 3000)
  
  Repeat
   WindowEvent = WaitWindowEvent()
    
  Until WindowEvent = #PB_Event_CloseWindow
  
EndProcedure
I get the following error with your code. I looked up the command and it doesn't seem to directly support images except maybe including the ButtonImageGadget() command somehow. I could be wrong unless it supports directly the flags from listed commands in help file for SetGadgetAttribute()

[21:28:42] [COMPILER] Line 18: SetGadgetAttribute(): Incorrect number of parameters.


@Paul
I might give your code a try if this doesn't work somehow
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1251
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Form Designer generated event procedure

Post by Paul »

Demivecs line should read

Code: Select all

SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(ImgFile))
Image Image
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Form Designer generated event procedure

Post by Distorted Pixel »

Paul wrote: Fri Jan 14, 2022 3:45 am Demivecs line should read

Code: Select all

SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(ImgFile))
Ok, with that it works, now I just need to add the finish the timer stuff and add detection for the left mouse click so if a user clicks the left mouse button before the timer is done it will end the splashscreen early and move on

Thank you all for your help. Yes, I'm still learning
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Post Reply