Selection graphique
Publié : mar. 29/mai/2012 21:59
Bonsoir,
Pour mon petit projet , j'ai besoin de "dessiner" dans un canvasgadget et de pouvoir sélectionner les divers éléments pour pouvoir effacer , redimensionner etc ...
Pour la sélection , je crée une région pour chaque éléments dessiné puis enregistre le handle dans une liste chainée . Ensuite je parcours ma liste et vérifie avec PtInRegion si ma souris se trouve sur mon élément . Le souci c'est que çà fonctionne pour le dernier élément dessiné mais par pour les autres . Ca fait 2 jours que je cherche mais je ne vois pas
Si vous avez une idée , je suis preneurs
. Si vous avez des critiques sur mon code , ou des idées d'amélioration n'hésitez pas comme je suis débutant , je dois coder des choses pas très catholique .
@+ David
Pour mon petit projet , j'ai besoin de "dessiner" dans un canvasgadget et de pouvoir sélectionner les divers éléments pour pouvoir effacer , redimensionner etc ...
Pour la sélection , je crée une région pour chaque éléments dessiné puis enregistre le handle dans une liste chainée . Ensuite je parcours ma liste et vérifie avec PtInRegion si ma souris se trouve sur mon élément . Le souci c'est que çà fonctionne pour le dernier élément dessiné mais par pour les autres . Ca fait 2 jours que je cherche mais je ne vois pas

Code : Tout sélectionner
;{- Enumerations / DataSections
Enumeration
#Window
EndEnumeration
Enumeration
#ToolBar
#StatusBar
EndEnumeration
Enumeration
#Canvas
#Image
EndEnumeration
Enumeration
#ToolBar_New
#ToolBar_Open
#ToolBar_Save
#ToolBar_Propiete
#Toolbar_Info
#Toolbar_Help
EndEnumeration
;DataSection
; new : IncludeBinary "res\Icons\new.png"
; open : IncludeBinary "res\Icons\open.png"
; save : IncludeBinary "res\Icons\save.png"
; propriete : IncludeBinary "res\Icons\propriete.png"
; info : IncludeBinary "res\Icons\info.png"
; help : IncludeBinary "res\Icons\help.png"
;EndDataSection
Structure Objet
x.i
y.i
x1.i
y1.i
hRgn.i
EndStructure
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
Procedure OpenWindow_MainWindow()
If OpenWindow(#Window, 0, 00, 700, 600, "Report Creator", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
If CreateToolBar(#ToolBar,WindowID(#Window))
;ToolBarImageButton(#ToolBar_New,ImageID(CatchImage(#PB_Any,?new)))
;ToolBarImageButton(#ToolBar_Open,ImageID(CatchImage(#PB_Any,?open)))
;ToolBarImageButton(#ToolBar_Save,ImageID(CatchImage(#PB_Any,?save)))
;ToolBarSeparator()
;ToolBarImageButton(#ToolBar_Propiete,ImageID(CatchImage(#PB_Any,?propriete)))
;ToolBarSeparator()
;ToolBarImageButton(#Toolbar_Info,ImageID(CatchImage(#PB_Any,?info)))
;ToolBarImageButton(#Toolbar_Help,ImageID(CatchImage(#PB_Any,?help)))
EndIf
If CreateStatusBar(#StatusBar, WindowID(#Window))
AddStatusBarField(#PB_Ignore)
AddStatusBarField(#PB_Ignore)
EndIf
CanvasGadget(#Canvas, 0, ToolBarHeight(#ToolBar), 700, 600 - ToolBarHeight(#ToolBar) - StatusBarHeight(#StatusBar),#PB_Canvas_ClipMouse|#PB_Canvas_Keyboard)
CreateImage(#Image,700,600 - StatusBarHeight(#StatusBar))
EndIf
EndProcedure
Procedure DrawCellTxt(x.i,y.i,width.i,height.i,txt.s)
x_txt.i = (width-TextWidth(txt))/2
y_txt.i = (height-TextHeight(txt))/2
DrawingMode(#PB_2DDrawing_Outlined)
Line(x,y,width,1,RGB(0,0,0)) ; haut
Line(x+width,y,1,height,RGB(0,0,0)) ;droite
Line(x+width,y+height,-width,1,RGB(0,0,0)) ;bas
Line(x,y+height,1,-height,RGB(0,0,0)) ;gauche
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(x_txt+x,y_txt+y,txt,RGB(0,0,0))
EndProcedure
UsePNGImageDecoder()
OpenWindow_MainWindow()
NewList Obj.Objet()
snapX=10
snapY=10
StartDrawing(CanvasOutput(#Canvas))
For i = 0 To (700/snapX)-1
For j = 0 To ((600 - ToolBarHeight(#ToolBar) - StatusBarHeight(#StatusBar))/snapY)
Plot(i*snapX,j*snapY,$ff0000)
Next j
Next i
StopDrawing()
;{- Event loop
Repeat
Event = WaitWindowEvent()
Select Event
; ///////////////////
Case #PB_Event_Gadget
EventGadget = EventGadget()
EventType = EventType()
If EventGadget = #Canvas
If EventType = #PB_EventType_LeftButtonDown
AddElement(Obj())
Drag.i = 1
XStart = GetGadgetAttribute(#Canvas,#PB_Canvas_MouseX)
YStart = GetGadgetAttribute(#Canvas,#PB_Canvas_MouseY)
XStart=(XStart/snapX)*snapX
YStart=(YStart/snapY)*snapY
Obj()\x = XStart
Obj()\y = YStart
StartDrawing(ImageOutput(#Image))
DrawImage(GetGadgetAttribute(#Canvas,#PB_Canvas_Image),0,0)
StopDrawing()
EndIf
If EventType = #PB_EventType_LeftButtonUp
Drag.i = 0
Obj()\hRgn=CreateRectRgn_(Obj()\x,Obj()\y,Obj()\x1,Obj()\y1)
ForEach Obj()
Debug Obj()\hRgn
Next
EndIf
If EventType = #PB_EventType_MouseMove
x = GetGadgetAttribute(#Canvas,#PB_Canvas_MouseX) ; coordonnées x sur le canvas
y = GetGadgetAttribute(#Canvas,#PB_Canvas_MouseY) ; coordonnées y sur le canvas
x=(x/snapX)*snapX
y=(y/snapY)*snapY
StatusBarText(#StatusBar,0,StrF(x,0) + ";" +StrF(y,0),#PB_StatusBar_Center) ; affiche les coordonées en pixel
If Drag
Obj()\x1=x
Obj()\y1=y
StartDrawing(CanvasOutput(#Canvas))
DrawingMode(#PB_2DDrawing_Outlined)
DrawImage(ImageID(#Image),0,0)
DrawCellTxt(XStart,YStart,x-XStart,y-YStart,"Azerty")
StopDrawing()
EndIf
ForEach Obj()
If PtInRegion_(Obj()\hRgn,x,y)
StatusBarText(#StatusBar,1,"OK")
Else
StatusBarText(#StatusBar,1,"NO OK")
EndIf
Next
EndIf
EndIf
; ////////////////////////
Case #PB_Event_CloseWindow
EventWindow = EventWindow()
If EventWindow = #Window
CloseWindow(#Window)
Break
EndIf
EndSelect
ForEver
;
;}

@+ David