[Résolu] Problème de gestion d'événement dans les module

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Ehma
Messages : 26
Inscription : dim. 24/juin/2018 19:01

[Résolu] Problème de gestion d'événement dans les module

Message par Ehma »

Bonjour,

J'ai un petit problème. Avant de transformer mon bouton en module tout fonctionnait bien, mais depuis, il ne reçois plus les événements (Je suis sous macOS, on ne sait jamais qu'il faudrait une option particulière)

Je vous ai mis le code, si vous avez une idée pour m'orienter.


Code : Tout sélectionner

  EnableExplicit

DeclareModule KnobGadget

  Declare CreateKnob(Gadget, X, Y, Width, Height, Min, Max, Options=0)
  Declare Repaint(*Knob)
  Declare EventKnob(*Knob)
  Declare SetSens(*Knob,Sens)

  
EndDeclareModule

Module KnobGadget

  Structure Knob
    Gadget.i
    x.a
    Y.a
    Height.i
    Width.i
    Min.i
    Max.i
    Val.i
    Image.i
    CenterX.i
    CenterY.i
    MouseDown.b
    Sens.b
    oldval.i
  EndStructure
  
Procedure CreateKnob(Gadget, X, Y, Width, Height, Min, Max, Options=0)
  *Knob.Knob= AllocateMemory(SizeOf(Knob))

    *Knob\Gadget=CanvasGadget(Gadget, X, Y, Width, Height)
    *Knob\Image=CreateImage(#PB_Any,Width, Height,24,$FFFFFF)
    Define ix,iy,irad; Création bouton
    ix=Int(Width/2)
    iy=Int(Height/2)
    irad=Int((ix+iy)/2)
    If StartDrawing(ImageOutput(*Knob\Image)) 
      Circle(ix,iy, irad, $0)
      Circle(ix,iy, irad-2,$FFFFFF)
      LineXY(ix,iy,Width,iy,$0)
      StopDrawing()
    EndIf;----------------------------
    *Knob\Val=0  
    *Knob\x=x
    *Knob\y=y
    *Knob\min=Min
    *Knob\Max=Max
    *Knob\Width=Width
    *Knob\Height=Height
    *Knob\CenterX=ix
    *Knob\CenterY=iy
    *Knob\MouseDown=#False
    *Knob\Sens=Height
    ProcedureReturn *Knob
  
  EndProcedure
  Procedure Repaint(*Knob.knob)
    Define Radius.i ;orientation du bouton
    If (0-*Knob\Min+*Knob\Max)<>0
      Radius=Int(280*(0-*Knob\Min+*Knob\Val)/(0-*Knob\Min+*Knob\Max))
    Else
      Radius=0
    EndIf
    Debug "Radius : "+Str(Radius)+" : on x : "+Str(*Knob\x)
    If StartVectorDrawing(CanvasVectorOutput(*Knob\Gadget)) ;rotation et affichage bouton
      RotateCoordinates(*Knob\CenterX, *Knob\CenterY, -235+Radius )
      MovePathCursor(0, 0)
      DrawVectorImage(ImageID(*Knob\Image), 255)
      StopVectorDrawing()
    EndIf
  EndProcedure
  Procedure ChangeValue(*Knob.knob,Val); lorsqu'il y a un mouvement sur le curseur appelé depuis événement
    If *Knob\oldval > Val
      *Knob\Val=*Knob\Val+1
      *Knob\oldval=val+*Knob\Sens
    ElseIf *Knob\oldval < Val
      *Knob\Val=*Knob\Val-1
      *Knob\oldval=val-*Knob\Sens
    EndIf
    
    If *Knob\Val>*Knob\Max
      *Knob\Val=*Knob\Max  
    ElseIf *Knob\Val<*Knob\Min
      *Knob\Val=*Knob\Min
    EndIf
    
    Debug "Mouse : "+Str(val)+" Value : "+Str(*Knob\Val)

    *Knob\oldval=val
    Repaint(*Knob\Gadget)
    
  EndProcedure
  Procedure SetSens(*Knob.knob,Sens); Définir la sensibilité du curseur
    If sens<0
      sens=0
    EndIf
    *Knob\Sens=Sens
    Debug "Sens set "+Str(*Knob\x)
  EndProcedure
  Procedure EventKnob(*Knob.knob) ; gestion des événement sur le bouton
    Define lEventType = EventType()
    Debug "Event set "+Str(*Knob\x)
    Select lEventType
      Case   #PB_EventType_MouseMove       : 
        If *Knob\MouseDown=#True : ChangeValue(*Knob,GetGadgetAttribute(*Knob\Gadget,#PB_Canvas_MouseY)):EndIf
      Case   #PB_EventType_LeftButtonDown  
        *Knob\MouseDown=#True
        *Knob\oldval=GetGadgetAttribute(*Knob\Gadget,#PB_Canvas_MouseY)
        Debug " Le bouton gauche de la souris a été pressé"
      Case   #PB_EventType_LeftButtonUp    
        *Knob\MouseDown=#False
        Debug " Le bouton gauche de la souris a été relâché sur x"+Str(*Knob\x)
   
    EndSelect
    ProcedureReturn lEventType;
  EndProcedure
EndModule



;----------------------------------------------------------
;Programme de test
;----------------------------------------------------------


Define Event, myKnob1

If OpenWindow(0, 0, 0, 400, 200, "Test Knob", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  myKnob1=KnobGadget::CreateKnob(#PB_Any,10,10, 64, 64,0,127)
  KnobGadget::SetSens(myKnob1,0)
  KnobGadget::Repaint(myKnob1)
  
  
 Repeat
    Event = WaitWindowEvent()
  
    If Event = #PB_Event_Gadget
      Select EventGadget() 
        Case myKnob1
          KnobGadget::EventKnob(myKnob1)
          Debug "Event myKnob1"
      EndSelect
    EndIf
    
  Until Event = #PB_Event_CloseWindow
EndIf
   
Je vous remercie d'y avoir jeté un oeil
Dernière modification par Ehma le jeu. 01/août/2019 15:38, modifié 1 fois.
Ehma
Messages : 26
Inscription : dim. 24/juin/2018 19:01

Re: Problème de gestion d'événement dans les module

Message par Ehma »

J'ai trouvé, c'était tout con

Code : Tout sélectionner

Select EventGadget()
        Case myKnob1
          KnobGadget::EventKnob(myKnob1)
          Debug "Event myKnob1"
      EndSelect
myKnob1 n'est pas un gadget mais un pointeur vers la structure.
Répondre