ColorRequester gets stuck in a loop.

Just starting out? Need help? Post your questions and find answers here.
SiggeSvahn
User
User
Posts: 40
Joined: Wed Oct 06, 2010 9:37 pm

ColorRequester gets stuck in a loop.

Post by SiggeSvahn »

Hi forum! I'm a newbie. I want to make a colored square. Clicking it shows the colorpicker. Then i gets stuck in a loop. Please show how I can get out of the loop. Best regards!

Code: Select all

  ; A bad colorpicker routine that gets stuck in a loop.
 #StringGadget=0
  If OpenWindow(0, 0, 0, 300, 200, "colorPicker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    StringGadget(#StringGadget, 125,  100, 50, 50, "", #PB_String_ReadOnly)
    SetGadgetColor(#StringGadget,#PB_Gadget_BackColor,#Black)

    Repeat
      
      If WaitWindowEvent()=#PB_Event_Gadget
        If EventGadget()=#StringGadget
          Fcolor = ColorRequester(Fcolor)
          SetGadgetColor(#StringGadget,#PB_Gadget_BackColor,Fcolor);Gets stuck in a loop. Why?
        EndIf
      EndIf
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
Newbie
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: ColorRequester gets stuck in a loop.

Post by firace »

Works better with a TextGadget :)

Code: Select all

#TGadget=0

OpenWindow(0, 0, 0, 300, 200, "colorPicker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

TextGadget(#TGadget, 125,  100, 50, 50, "", 256)
SetGadgetColor(#TGadget,#PB_Gadget_BackColor,#Black)

Repeat
  e= WaitWindowEvent()
  
  If e =#PB_Event_Gadget
    If EventGadget()=#TGadget
      Fcolor = ColorRequester(Fcolor)
      SetGadgetColor(#TGadget,#PB_Gadget_BackColor,Fcolor)
    EndIf
  EndIf
Until e = #PB_Event_CloseWindow

User avatar
robertfern
User
User
Posts: 38
Joined: Fri Feb 02, 2018 10:33 pm
Location: New Jersey

Re: ColorRequester gets stuck in a loop.

Post by robertfern »

I used a CanvasGadget since text gadget cant get mouse clicks.
Also you were missing a EventType call, which is why you were getting so many events for the gadget.

Code: Select all

#CanvasGadget = 0
If OpenWindow(0, 0, 0, 300, 200, "colorPicker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ;StringGadget(#StringGadget, 125,  100, 50, 50, "", #PB_String_ReadOnly)
  CanvasGadget(#CanvasGadget, 125,  100, 50, 50, #PB_Canvas_Border)
  SetGadgetColor(#CanvasGadget,#PB_Gadget_BackColor,#Black)
  
  Repeat
    event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      EventType = EventType()
      If EventType = #PB_EventType_LeftClick
        If EventGadget() = #CanvasGadget
          Fcolor = ColorRequester(Fcolor)
          If Fcolor > -1 ; if user chooses "Cancel" then ColorRequester returns -1
            If StartDrawing(CanvasOutput(#CanvasGadget))
              Box(0, 0, 50, 50, Fcolor)
              StopDrawing()
            EndIf
          EndIf
        EndIf
      EndIf
    EndIf
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Mac OSX Ventura & Windows 10, PB 6.12
BarryG
Addict
Addict
Posts: 4126
Joined: Thu Apr 18, 2019 8:17 am

Re: ColorRequester gets stuck in a loop.

Post by BarryG »

[Edit] Don't flush the extra events out.
Last edited by BarryG on Thu Dec 12, 2019 8:36 am, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7577
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ColorRequester gets stuck in a loop.

Post by infratec »

The last version from BarryG is ... a bad thing.

Never throw away events! You never know what you are throwing away.

Golden rule in a event driven programming: use only one event loop.
BarryG
Addict
Addict
Posts: 4126
Joined: Thu Apr 18, 2019 8:17 am

Re: ColorRequester gets stuck in a loop.

Post by BarryG »

infratec wrote:Never throw away events! You never know what you are throwing away.
Edited my post so as not to mislead newbies.
SiggeSvahn
User
User
Posts: 40
Joined: Wed Oct 06, 2010 9:37 pm

Re: ColorRequester gets stuck in a loop.

Post by SiggeSvahn »

Thankyou all! I was raised with VisualBasic and tought to prefer the simpler gadgets not to spend too much memory (the old days). That way the code from Firace seems to be the fittest.
#TGadget=0

OpenWindow(0, 0, 0, 300, 200, "colorPicker", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

TextGadget(#TGadget, 125, 100, 50, 50, "", 256)
SetGadgetColor(#TGadget,#PB_Gadget_BackColor,#Black)

Repeat
e= WaitWindowEvent()

If e =#PB_Event_Gadget
If EventGadget()=#TGadget
Fcolor = ColorRequester(Fcolor)
SetGadgetColor(#TGadget,#PB_Gadget_BackColor,Fcolor)
EndIf
EndIf
Until e = #PB_Event_CloseWindow
Newbie
Post Reply