TUTO draw in mm with grid and zoom

Informations pour bien débuter en PureBasic
Avatar de l’utilisateur
microdevweb
Messages : 1802
Inscription : mer. 29/juin/2011 14:11
Localisation : Belgique

TUTO draw in mm with grid and zoom

Message par microdevweb »

Bonjour à tous,
Je travaille actuellement sur un logiciel de plan (2d -> 3d) en Pb, je met à votre disposition ce petit bout de code pour la gestion d'un dessin en mm avec grille magnétique et zoom
  • CTRL + molette pour augmenté ou diminué le zoom


Image

Image

Code : Tout sélectionner

; ********************************************************************************************************
; AUTHOR          : MICRODEVWEB
; PROJECT         : TUTO DRAW
; DESIGNED WITH   : PB 5.70
; ********************************************************************************************************
EnableExplicit
Enumeration 
  #MAIN_FORM
  #CANVAS
EndEnumeration
Structure pos
  x.l
  y.l
  w.l
  h.l
EndStructure
; constantes
#BACK_COLOR = $FFAAE8EE
#GRID_COLOR = $8213458B
#CURSOR_COLOR = $F70000FF
#GRID_SIZE = 50 ; mm
#GRID_SENSITIVE = 20.0
#BOX_WIDTH = 1000 ; mm
#BOX_HEIGHT = 100 ; mm
; Variables globales
Global gZoom.f = 0.2 ; facteur de zoom
Global gMX,gMY ; position de la souris

Procedure coordinatesTo_MM(x,y,*x,*y)
  PokeL(*x,ConvertCoordinateX(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
  PokeL(*Y,ConvertCoordinateY(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
EndProcedure
Procedure coordinatesTo_MM_bis(x,y,*x,*y)
  StartVectorDrawing(CanvasVectorOutput(#CANVAS,#PB_Unit_Millimeter))
  ScaleCoordinates(gZoom,gZoom) ; le facteur de zoom
  PokeL(*x,ConvertCoordinateX(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
  PokeL(*Y,ConvertCoordinateY(x,y,#PB_Coordinate_Device,#PB_Coordinate_User))
  StopVectorDrawing()
EndProcedure

Procedure drawGrid()
  ; dessin de la grille
  Protected r = #GRID_SIZE,c = #GRID_SIZE
  Protected w,h 
  ; on transform la taille du canvas en mm
  coordinatesTo_MM(GadgetWidth(#CANVAS),GadgetHeight(#CANVAS),@w,@h)
  ; on efface
  VectorSourceColor(#BACK_COLOR)
  FillVectorOutput()
  ; dessin des colonnes
  While c < w
    MovePathCursor(c,0)
    AddPathLine(c,h)
    c + #GRID_SIZE
  Wend
  ; dessin des lignes
  While r < h
    MovePathCursor(0,r)
    AddPathLine(w,r)
    r + #GRID_SIZE
  Wend
  VectorSourceColor(#GRID_COLOR)
  StrokePath(0.2) ; Attention en mm
EndProcedure

Procedure drawCursor()
  Protected w,h 
  ; on transform la taille du canvas en mm
  coordinatesTo_MM(GadgetWidth(#CANVAS),GadgetHeight(#CANVAS),@w,@h)
  ; horizontal
  MovePathCursor(0,gMY)
  AddPathLine(w,gMY)
  ; vertical
  MovePathCursor(gMX,0)
  AddPathLine(gMx,h)
  VectorSourceColor(#CURSOR_COLOR)
  StrokePath(0.3) ; Attention en mm
EndProcedure

Procedure manageMagnet()
  ; gestion de l'aimantation
  Protected x,y,lM.f = #GRID_SIZE  - #GRID_SENSITIVE,LP.f = #GRID_SENSITIVE
    If Mod(gMX,#GRID_SIZE) >= LM
      x =  Round(gMX/#GRID_SIZE,#PB_Round_Up) * #GRID_SIZE
      gMX = x
    ElseIf Mod(gMX,#GRID_SIZE) <= LP
      x =  Round(gMX/#GRID_SIZE,#PB_Round_Nearest) * #GRID_SIZE
      gMX = x
    EndIf
    If Mod(gMY,#GRID_SIZE) >= LM
      Y =  Round(gMY/#GRID_SIZE,#PB_Round_Up) * #GRID_SIZE
      gMY = y
    ElseIf Mod(gMY,#GRID_SIZE) <= LP
      Y =  Round(gMY/#GRID_SIZE,#PB_Round_Nearest) * #GRID_SIZE
      gMY = y
    EndIf
EndProcedure

Procedure draw()
  ; dessin du canvas en mm
  StartVectorDrawing(CanvasVectorOutput(#CANVAS,#PB_Unit_Millimeter))
  ScaleCoordinates(gZoom,gZoom) ; le facteur de zoom
  drawGrid()                    ; dessin de la grille
  drawCursor()                  ; dessin du curseur
  StopVectorDrawing()
EndProcedure

Procedure evExit()
  End
EndProcedure

Procedure evCanvas()
  Select EventType()
    Case #PB_EventType_MouseWheel
      ; gestion du zoom uniquement si touche controle appuyée
      If GetGadgetAttribute(#CANVAS,#PB_Canvas_Modifiers) = #PB_Canvas_Control
        If GetGadgetAttribute(#CANVAS,#PB_Canvas_WheelDelta) < 1
          If gZoom > 0.03
            gZoom -0.01
          EndIf
        Else
          If gZoom <100
            gZoom +0.01
          EndIf
        EndIf
      EndIf
      draw()
    Case #PB_EventType_MouseMove
      ; on mémorise la position de la souris
      coordinatesTo_MM_bis(GetGadgetAttribute(#CANVAS,#PB_Canvas_MouseX),
                           GetGadgetAttribute(#CANVAS,#PB_Canvas_MouseY),
                           @gMX,@gMY)
      manageMagnet()
      draw()
  EndSelect
EndProcedure

Procedure openForm()
  Protected flags = #PB_Window_ScreenCentered|#PB_Window_SystemMenu
  OpenWindow(#MAIN_FORM,0,0,800,600,"Tuto draw in mm",flags)
  CanvasGadget(#CANVAS,0,0,800,600,#PB_Canvas_Keyboard) ; important pour lire le delata wheel
  ; mise en place des callback
  BindEvent(#PB_Event_CloseWindow,@evExit(),#MAIN_FORM)
  BindGadgetEvent(#CANVAS,@evCanvas())
  
  draw()
EndProcedure

openForm()
; main loop
Repeat : WaitWindowEvent() : ForEver
Dernière modification par microdevweb le mar. 19/mars/2019 11:16, modifié 1 fois.
Windows 10 64 bits PB: 5.70 ; 5.72 LST
Work at Centre Spatial de Liège
Avatar de l’utilisateur
Micoute
Messages : 2612
Inscription : dim. 02/oct./2011 16:17
Localisation : 35520 La Mézière

Re: TUTO draw in mm with grid and zoom

Message par Micoute »

Bonjour microdevweb et merci beaucoup pour le partage.
Microsoft Windows 10 Famille 64 bits : Carte mère : ASRock 970 Extreme3 R2.0 : Carte Graphique NVIDIA GeForce RTX 3080 : Processeur AMD FX 6300 6 cœurs 12 threads 3,50 GHz PB 6.20 LTS (x64)
Un homme doit être poli, mais il doit aussi être libre !
Avatar de l’utilisateur
Kwai chang caine
Messages : 6995
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: TUTO draw in mm with grid and zoom

Message par Kwai chang caine »

M'étonnera toujours ce PB du minimum de lignes pour un max d'effets 8O
Merci pour le partage 8)
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel
Avatar de l’utilisateur
SPH
Messages : 5033
Inscription : mer. 09/nov./2005 9:53

Re: TUTO draw in mm with grid and zoom

Message par SPH »

Kwai chang caine a écrit :M'étonnera toujours ce PB du minimum de lignes pour un max d'effets 8O
Merci pour le partage 8)
+1 8) 8O

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Intel Core i7 4770 64 bits - GTX 650 Ti
Version de PB : 6.12LTS- 64 bits
Ollivier
Messages : 4197
Inscription : ven. 29/juin/2007 17:50
Localisation : Encore ?
Contact :

Re: TUTO draw in mm with grid and zoom

Message par Ollivier »

Et encore, la procédure magnet peut être encore considérablement diminuée.
Avatar de l’utilisateur
Kwai chang caine
Messages : 6995
Inscription : sam. 23/sept./2006 18:32
Localisation : Isere

Re: TUTO draw in mm with grid and zoom

Message par Kwai chang caine »

En tout cas pas par moi :oops: :wink:
ImageLe bonheur est une route...
Pas une destination

PureBasic Forum Officiel
Avatar de l’utilisateur
Philippe_GEORGES
Messages : 146
Inscription : mer. 28/janv./2009 13:28

Re: TUTO draw in mm with grid and zoom

Message par Philippe_GEORGES »

Impressionnant !

Là, je me dit qu'il y a des têtes sur ce forum !

Un grand merci pour le partage.
Philippe GEORGES
"La simplicité est la sophistication suprême" (De Vinci)
assistance informatique, création de logiciels
Avatar de l’utilisateur
Ar-S
Messages : 9554
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: TUTO draw in mm with grid and zoom

Message par Ar-S »

Très propre l'interface.
Bonne continuation. C'est du lourd.
~~~~Règles du forum ~~~~
⋅.˳˳.⋅ॱ˙˙ॱ⋅.˳Ar-S ˳.⋅ॱ˙˙ॱ⋅.˳˳.⋅
W11x64 PB 6.x
Section HORS SUJET : ICI
LDV MULTIMEDIA : Dépannage informatique & mes Logiciels PB
UPLOAD D'IMAGES : Uploader des images de vos logiciels
Répondre