
C'est ce que je vous propose avec ce code commenté.
Code : Tout sélectionner
EnableExplicit
Enumeration window
#mf
EndEnumeration
Enumeration gadget
#mfGrid
EndEnumeration
Structure Plot
; Point de coordonnée haut gauche
x0.i
y0.i
; Point de coordonnée bas droit
x1.i
y1.i
; Le plot est sélectionné (ou pas)
isSelect.b
EndStructure
; Configuration de la grille
Global gnbCol = 20 ; Nombre de colonnes
Global gnbLig = 20 ; Nombre de lignes
; Configuration d'un plot
Global pInnerSize = 20 ; Taille d'un plot
Global pOuterSize = 22 ; Dimension exterieur d'un plot
Global pSpace = 2 ; Espacement avant et aprés le plot
; Construction du grid
Global Dim Grid.Plot(gnbCol, gnbLig)
; Définition des couleurs de plot
Global PlotColorInactif = RGB(105, 105, 105)
Global PlotColorActif = RGB(245, 245, 245)
;Sommaire
Declare Start()
Declare DrawGrid()
Declare onPlot()
Declare Exit()
Start()
Procedure Start()
; Dimension fenetre
Protected ww = 800
Protected wh = 600
; Marge haute et gauche de la grille
Protected MarginLeft = (ww - (gnbCol*pOuterSize))/2, MarginTop = (wh - (gnbLig*pOuterSize))/2
; Ouverture de la fenetre de l'application
OpenWindow(#mf, 0, 0, ww, wh, "Dessiner sur une grille", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SetWindowColor(#mf, RGB(0,0,0))
; Installation de la grille
CanvasGadget(#mfGrid, MarginLeft, MarginTop, gnbCol*pOuterSize, gnbLig*pOuterSize, #PB_Canvas_ClipMouse)
DrawGrid()
; Déclencheurs
BindGadgetEvent(#mfGrid, @onPlot())
BindEvent(#PB_Event_CloseWindow, @Exit())
; Loop
Repeat : WaitWindowEvent(0) : ForEver
EndProcedure
; Dessiner le grid
Procedure DrawGrid()
; Balayage de la grille de plots
Protected x, y
; Fond du grid
StartDrawing(CanvasOutput(#mfGrid))
Box(0, 0, gnbCol*pOuterSize, gnbLig*pOuterSize, RGB(0, 0, 0))
; Dessin des plots
For x = 0 To gnbCol - 1
For y = 0 To gnbLig -1
With Grid(x,y)
\x0 = x*pOuterSize
\y0 = y*pOuterSize
\x1 = \x0 + pInnerSize
\y1 = \y0 + pInnerSize
Box( \x0 + pSpace, \y0 + pSpace, pInnerSize, pInnerSize, PlotColorInactif)
EndWith
Next
Next
StopDrawing()
EndProcedure
; Un plot est sélectionné
; - On passe dessus avec le clic gauche de la souris préssé
; - Quand on le clic
Procedure onPlot()
; Parcours de la grille de plots
Protected x, y
; Position x y de la souris
Protected px = GetGadgetAttribute(#mfGrid, #PB_Canvas_MouseX)
Protected py = GetGadgetAttribute(#mfGrid, #PB_Canvas_MouseY)
; Plot en cours de traitement
Static PreviousPlot.i
; Action a effectuer 0 (Aucune, 1 : Selection, 2 : Déselection)
Static CurrentAction.i
; La Souris est sur la grille : Affichage de la main
If EventType() = #PB_EventType_MouseEnter
SetGadgetAttribute(#mfGrid, #PB_Canvas_Cursor, #PB_Cursor_Hand)
EndIf
; La souris est en mouvement et la touche gauche de la soutis est préssée
If (EventType() = #PB_EventType_MouseMove And
GetGadgetAttribute(#mfGrid, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
; Parcours de la grille
For x = 0 To gnbCol - 1
For y = 0 To gnbLig - 1
With Grid(x,y)
; Le surseur de la souris est sur un plot
; Ce nouveau plot doit etre différent du précédent
If (px > \x0 And py> \y0 And px < \x1 And py < \y1) And (Grid(x,y) <> PreviousPlot)
; C'est un nouveau plot. Mémorisation du plot en cours
PreviousPlot = Grid(x,y)
; Action à effectuer
; 0 : On ne sait pas encore
; 1 : Le plot sera sélectionné
; 2 : Le plot sera déselectionné
If CurrentAction = 0
If \isSelect = #False
CurrentAction = 1
Else
CurrentAction = 2
EndIf
EndIf
If CurrentAction = 1
StartDrawing(CanvasOutput(#mfGrid))
Box(\x0 + pSpace, \y0 + pSpace, pInnerSize, pInnerSize, PlotColorActif)
StopDrawing()
\isSelect = #True
EndIf
If CurrentAction = 2
StartDrawing(CanvasOutput(#mfGrid))
Box(\x0 + pSpace, \y0 + pSpace, pInnerSize, pInnerSize, PlotColorInactif)
StopDrawing()
\isSelect = #False
EndIf
EndIf
EndWith
Next
Next
EndIf
; Le bouton gauche de la souris est relaché
If EventType() = #PB_EventType_LeftButtonUp
PreviousPlot = #False
CurrentAction = 0
EndIf
EndProcedure
Procedure Exit()
End
EndProcedure