Page 1 of 1

ColorRequester gets stuck in a loop.

Posted: Thu Dec 12, 2019 12:01 am
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

Re: ColorRequester gets stuck in a loop.

Posted: Thu Dec 12, 2019 12:36 am
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


Re: ColorRequester gets stuck in a loop.

Posted: Thu Dec 12, 2019 12:40 am
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

Re: ColorRequester gets stuck in a loop.

Posted: Thu Dec 12, 2019 8:19 am
by BarryG
[Edit] Don't flush the extra events out.

Re: ColorRequester gets stuck in a loop.

Posted: Thu Dec 12, 2019 8:28 am
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.

Re: ColorRequester gets stuck in a loop.

Posted: Thu Dec 12, 2019 8:37 am
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.

Re: ColorRequester gets stuck in a loop.

Posted: Thu Dec 12, 2019 10:23 am
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