Comment Faire une rotation d'un Texte en son centre

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Shadow
Messages : 1413
Inscription : mer. 04/nov./2015 17:39

Comment Faire une rotation d'un Texte en son centre

Message par Shadow »

Salut,

Savez vous Comment Faire une rotation d'un Texte en son centre ?
Je voudrait que le texte reste centrer quand il tourne, qu'il tourne sur lui même en son centre, comment fait-on cela ?

Code : Tout sélectionner

Enumeration
  
  #Fenetre
  #ButonImageGadget
  #Image
  
EndEnumeration

OpenWindow(#Fenetre, 327, 177, 500, 300, "Exemple Texte Rotation Centré...",  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered | #PB_Window_TitleBar)

TexteBouton.s = "Mon Bouton"
AngleTexte.i = 50

If CreateImage(#Image, 150, 150, 32, #PB_Image_Transparent)
  
  If IsImage(#Image) And StartDrawing(ImageOutput(#Image))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    DrawRotatedText((ImageWidth(#Image) - TextWidth(TexteBouton.s)) / 2, (ImageHeight(#Image) - TextHeight(TexteBouton.s)) / 2, TexteBouton.s, AngleTexte.i, RGBA(150, 75, 200, 255))
    StopDrawing()
  EndIf
  
EndIf

ButtonImageGadget(#ButonImageGadget, 10, 10, 150, 150, ImageID(#Image))

Repeat ; Départ des évènements de boucle.
  
  Event = WaitWindowEvent(1) ; Cette ligne attend pendent (Minuteur) qu'un évènement soit recus par la fenêtre.
  EventGadget = EventGadget(); Pour savoir sur quel gadget c'est produis l'évènement.
  EventType = EventType()    ; Le type d'évènement qui c'est produis sur le gadget.
  
  Select Event 
      
    Case #PB_Event_Gadget
      
      Select EventGadget
          
        Case #ButonImageGadget
          
      EndSelect
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow ; Fin des évènements de boucle.
Processeur: Intel Core I7-4790 - 4 Cœurs - 8 Thread: 3.60 Ghz.
Ram: 32 GB.
Disque: C: SDD 250 GB, D: 3 TB.
Vidéo: NVIDIA GeForce GTX 960: 2 GB DDR5.
Écran: Asus VX248 24 Pouces: 1920 x 1080.
Système: Windows 7 64 Bits.

PureBasic: 5.60 x64 Bits.
Avatar de l’utilisateur
SPH
Messages : 4947
Inscription : mer. 09/nov./2005 9:53

Re: Comment Faire une rotation d'un Texte en son centre

Message par SPH »

Il faut mettre un Sin et un Cos mais je n'ai pas reussi pile poil :idea:

!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
Avatar de l’utilisateur
Ar-S
Messages : 9540
Inscription : dim. 09/oct./2005 16:51
Contact :

Re: Comment Faire une rotation d'un Texte en son centre

Message par Ar-S »

Je n'ai pas la formule sous la main, mais c'est vraie que si on pouvait choisir en parametre des DrawRotatedText un flag #PB_Rotate_fromLeft, #PB_Rotate_fromRight, #PB_Rotate_fromCenter ce serait vraiment pratique.
~~~~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
Shadow
Messages : 1413
Inscription : mer. 04/nov./2015 17:39

Re: Comment Faire une rotation d'un Texte en son centre

Message par Shadow »

Peut être creuser par là ?:

Code : Tout sélectionner

; Par StarGate le ?.
; Faire une rotation d'un sprite en son centre.
Procedure SpriteTransformation(Sprite.i, X.f, Y.f, W.f, H.f, Angle.f)
   Protected Cos.f = Cos(Radian(Angle))
   Protected Sin.f = Sin(Radian(Angle))
   TransformSprite(Sprite, X*Cos-Y*Sin, X*Sin+Y*Cos, (X+W)*Cos-Y*Sin, (X+W)*Sin+Y*Cos, (X+W)*Cos-(Y+H)*Sin, (X+W)*Sin+(Y+H)*Cos, X*Cos-(Y+H)*Sin, X*Sin+(Y+H)*Cos)
EndProcedure
Processeur: Intel Core I7-4790 - 4 Cœurs - 8 Thread: 3.60 Ghz.
Ram: 32 GB.
Disque: C: SDD 250 GB, D: 3 TB.
Vidéo: NVIDIA GeForce GTX 960: 2 GB DDR5.
Écran: Asus VX248 24 Pouces: 1920 x 1080.
Système: Windows 7 64 Bits.

PureBasic: 5.60 x64 Bits.
Avatar de l’utilisateur
JohnJohnsonSHERMAN
Messages : 648
Inscription : dim. 13/déc./2015 11:05
Localisation : Allez, cherche...
Contact :

Re: Comment Faire une rotation d'un Texte en son centre

Message par JohnJohnsonSHERMAN »

Sinon avec VectorDrawing, en jouant sur l'origine et les rotations peut être? :P
"Le bug se situe entre la chaise et le clavier"
Votre expert national en bogage et segfaults.

CPU : AMD A8 Quad core - RAM 8Gb - HDD 2To
  • Windows 10 x64 - PB 5.61 x64
  • Linux Ubuntu 16.04 LTS x64 (dual boot) - PB pas encore réinstallé
vurvur
Messages : 60
Inscription : ven. 29/août/2014 19:52

Re: Comment Faire une rotation d'un Texte en son centre

Message par vurvur »

Un code sympa de firace (forum anglais)... Ca ne fait pas exactement ce que tu cherches mais peut-être que ça te mettra sur la voie ou te donnera des idées :)

Code : Tout sélectionner


InitSprite() :  W=750 : H=540  : C = $324F72 : Cellsize = 50 : LoadFont(99, "Arial", 16)

OpenWindow(0, 35, 35, W+1, H, "E8", #PB_Window_SizeGadget|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,W+1,H,1,0,0)  : AddWindowTimer(0, 1, 1000)

CreateSprite(33, W+200,W+200, #PB_Sprite_AlphaBlending) 
CreateSprite(34, W+200,W+200, #PB_Sprite_AlphaBlending)   

Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow :   Break
    Case #PB_Event_Timer :
      
      StartDrawing(SpriteOutput(33)) : Box(0,0,W*2,H*2,0) : DrawingFont(FontID(99))
        sec$ = RSet(Str(Second(Date())),2,"0")
        For a = 0 To W+200-Cellsize Step 50 : For b = 0 To W+200-Cellsize Step 50 
            If Not Random(Val(sec$)/10+1) :   Box(a, b, Cellsize-1, Cellsize-1, C+(a+b)*6)  : DrawRotatedText(a+3,b+3,Sec$,0) : EndIf
        Next : Next    
      StopDrawing()  
      CopySprite(33,34) 
      
      x = 0 :  Repeat : x+1
        ClearScreen(0) 
        RotateSprite(33, 9,#PB_Relative) : DisplayTransparentSprite(33,-100,-100,134) 
        RotateSprite(34,-9,#PB_Relative) : DisplayTransparentSprite(34,-100,-100,134) 
        FlipBuffers()
      Until x = 8
      
  EndSelect
ForEver

Avatar de l’utilisateur
falsam
Messages : 7324
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Comment Faire une rotation d'un Texte en son centre

Message par falsam »

Bonjour vurvur. Un peu hors-sujet je pense. Shadow souhaite faire une rotation de texte dans un BoutonGadget() et toi tu cite un code source dans un environnement 2D.

Tout ça n'est pas bien grave car une réponse fiable a été donnée sur le forum anglophone.
:arrow: http://www.purebasic.fr/english/viewtop ... 78#p504778

Oui Shadow hante aussi le forum anglophone ^-^
Configuration : Windows 11 Famille 64-bit - PB 6.20 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
vurvur
Messages : 60
Inscription : ven. 29/août/2014 19:52

Re: Comment Faire une rotation d'un Texte en son centre

Message par vurvur »

Hello falsam,
ah oui tu as raison, j'avais pas vu que c'était pour un bouton, désolé. :oops:
Avatar de l’utilisateur
falsam
Messages : 7324
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Comment Faire une rotation d'un Texte en son centre

Message par falsam »

Sois pas désolé. Ca m'arrive aussi de répondre à cotes :wink:
Configuration : Windows 11 Famille 64-bit - PB 6.20 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Shadow
Messages : 1413
Inscription : mer. 04/nov./2015 17:39

Re: Comment Faire une rotation d'un Texte en son centre

Message par Shadow »

Salut,

Merci à vous, oui je vais de temps en temps sur le fofo anglais quand je trouve pas ici :)
Mais je suis sûr que tu aurais trouvée Falsam :)
Merci à infratec du forum anglais:

Code : Tout sélectionner

Enumeration
  
  #Fenetre
  #ButonImageGadget
  #Image
  
EndEnumeration

OpenWindow(#Fenetre, 327, 177, 500, 300, "Exemple Texte Rotation Centré...",  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered | #PB_Window_TitleBar)

TexteBouton.s = "Mon Bouton"
AngleTexte.i = 10

If CreateImage(#Image, 150, 150, 32, #PB_Image_Transparent)
  
  If IsImage(#Image) And StartDrawing(ImageOutput(#Image))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    
    X.i = (ImageWidth(#Image) - TextWidth(TexteBouton.s) * Cos(Radian(AngleTexte)) - TextHeight(TexteBouton) * Sin(Radian(AngleTexte))) / 2
    Y.i = (ImageHeight(#Image) + TextWidth(TexteBouton.s) * Sin(Radian(AngleTexte)) - TextHeight(TexteBouton) * Cos(Radian(AngleTexte))) / 2
    
    DrawRotatedText(X.i, Y.i, TexteBouton.s, AngleTexte.i, RGBA(150, 75, 200, 255))
    StopDrawing()
    
  EndIf
  
EndIf

ButtonImageGadget(#ButonImageGadget, 10, 10, 150, 150, ImageID(#Image))

Repeat ; Départ des évènements de boucle.
  
  Event = WaitWindowEvent(1) ; Cette ligne attend pendent (Minuteur) qu'un évènement soit recus par la fenêtre.
  EventGadget = EventGadget(); Pour savoir sur quel gadget c'est produis l'évènement.
  EventType = EventType()    ; Le type d'évènement qui c'est produis sur le gadget.
  
  Select Event 
      
    Case #PB_Event_Gadget
      
      Select EventGadget
          
        Case #ButonImageGadget
          
      EndSelect
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow ; Fin des évènements de boucle.
Processeur: Intel Core I7-4790 - 4 Cœurs - 8 Thread: 3.60 Ghz.
Ram: 32 GB.
Disque: C: SDD 250 GB, D: 3 TB.
Vidéo: NVIDIA GeForce GTX 960: 2 GB DDR5.
Écran: Asus VX248 24 Pouces: 1920 x 1080.
Système: Windows 7 64 Bits.

PureBasic: 5.60 x64 Bits.
Avatar de l’utilisateur
JohnJohnsonSHERMAN
Messages : 648
Inscription : dim. 13/déc./2015 11:05
Localisation : Allez, cherche...
Contact :

Re: Comment Faire une rotation d'un Texte en son centre

Message par JohnJohnsonSHERMAN »

Ayant de mon coté fait quelques essais avec VectorDrawing, je suis... perplexe :roll:

Code : Tout sélectionner

Enumeration
  #Window
  #Canvas
  #Refresh
EndEnumeration

Procedure DrawCenterRotatedText(Gadget,Text$,Angle.f,BackColor,ForeColor)
  
  StartVectorDrawing(CanvasVectorOutput(Gadget))
  Protected outWidth = VectorOutputWidth(), outHeight = VectorOutputHeight()
  
  
  VectorSourceColor(BackColor)
  FillVectorOutput()
   
  VectorFont(LoadFont(0,"Verdana",20))
  VectorSourceColor(ForeColor)
  
  MovePathCursor(outWidth/2 - VectorTextWidth(Text$)/2, outHeight/2 - VectorTextHeight(Text$)/2)
  AddPathCircle(outWidth/2 - VectorTextWidth(Text$)/2, outHeight/2 - VectorTextHeight(Text$)/2,3)
  
  RotateCoordinates(outWidth/2,outHeight/2,Angle,#PB_Coordinate_User) 
  DrawVectorText(Text$)
  AddPathCircle(outWidth/2,outHeight/2,5)
  
  VectorSourceColor(RGBA(200,220,255,120))
  For i=0 To outWidth Step 20
    MovePathCursor(i,0)
    AddPathLine(i,outHeight)
  Next
  For i=0 To outHeight Step 20
    MovePathCursor(0,i)
    AddPathLine(outWidth,i)
  Next
  
  StrokePath(2)

  StopVectorDrawing()
EndProcedure

  
  
OpenWindow(#Window,0,0,400,400,"Rotation de texte VectorDrawing",#PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget)

CanvasGadget(#Canvas,0,0,400,400)
AddWindowTimer(#Window,#Refresh,50)

Repeat
  event = WaitWindowEvent()
  
  Select event
    Case #PB_Event_Timer
      If EventTimer() = #Refresh
        i +1
        DrawCenterRotatedText(#Canvas,"Ce texte tourne",i,RGBA(0,100,180,255),RGBA(255,255,255,255))
      EndIf
      
    Case #PB_Event_SizeWindow
      ResizeGadget(#Canvas,0,0,WindowWidth(#Window),WindowHeight(#Window))
  EndSelect
  
Until event = #PB_Event_CloseWindow
Il va falloir utiliser des cos() et sin() :?
"Le bug se situe entre la chaise et le clavier"
Votre expert national en bogage et segfaults.

CPU : AMD A8 Quad core - RAM 8Gb - HDD 2To
  • Windows 10 x64 - PB 5.61 x64
  • Linux Ubuntu 16.04 LTS x64 (dual boot) - PB pas encore réinstallé
Avatar de l’utilisateur
kernadec
Messages : 1606
Inscription : ven. 25/avr./2008 11:14

Re: Comment Faire une rotation d'un Texte en son centre

Message par kernadec »

salut
si j'ai bien compris ta demande, alors pour le fun :mrgreen:
j'ai tenté cette idée à l' arrache 8O

[réédition] pour les commentaires et mise à jour du code.

cordialement

Code : Tout sélectionner

 
LoadFont(1, "Arial", 12) 

If OpenWindow(0, 0, 0, 300, 300, "RotatedText centré", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  StartDrawing(WindowOutput(0))
 
  SetGadgetFont(#PB_Default, FontID(1))  
  DrawingFont(FontID(1))                                             ; à placer avant les fonctions TextHeight() et TextWidth()

  Texte$="Hello World!"              
  Hauteur.i = TextHeight(Texte$)
  Longueur.i = TextWidth(Texte$+Space(1))
  ang_hypo.d= Degree(ASin(Hauteur/Longueur))              ; calcul angle du centre de la ligne de caractères
   
  Box(0, 0, 300, 300, RGB(255, 255, 255))
  
  For Angle = -(ang_hypo+5) To 3600 Step 1

    x = 150 + (Longueur/2) * Sin(Angle / 180 * #PI)         ; x position sur le périmètre du cercle
    y = 150 + (Longueur/2) * Cos(Angle / 180 * #PI)        ; y position sur le périmètre  du cercle
    Box(0, 0, 300, 300, RGB(255, 255, 255))                    ; effacement 

    DrawRotatedText(x, y, Texte$, (90+Angle)+ang_hypo+5, RGB(0, 0, 0))  
    Delay(2)

  Next Angle
  
  StopDrawing() 
  
EndIf

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

Dernière modification par kernadec le ven. 24/mars/2017 11:13, modifié 3 fois.
Avatar de l’utilisateur
falsam
Messages : 7324
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: Comment Faire une rotation d'un Texte en son centre

Message par falsam »

Joli ecroix design ^-^

Image
Configuration : Windows 11 Famille 64-bit - PB 6.20 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
Zorro
Messages : 2186
Inscription : mar. 31/mai/2016 9:06

Re: Comment Faire une rotation d'un Texte en son centre

Message par Zorro »

et en changeant le Step 90 par Step 1 ?
en ajoutant un systeme d'effacement, un petit delay et hop :)
pourrai fair l'objet d'une Animation si utilisation d'un Timer

Code : Tout sélectionner


LoadFont(1, "Arial", 12)

If OpenWindow(0, 0, 0, 300, 300, "RotatedTextCentré", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		
		StartDrawing(WindowOutput(0))
				
				Texte$="Hello World!"
				Hauteur.i = TextHeight(Texte$)
				Longueur.i = TextWidth(Texte$+Space(1))
				demi_hypo.d=Sqr(Pow(Hauteur,2)+Pow(Longueur,2))/2
				ang_hypo.d= Degree(ASin(Hauteur/Longueur))
				
				SetGadgetFont(#PB_Default, FontID(1))
				DrawingFont(FontID(1))
				Box(0, 0, 300, 300, RGB(255, 255, 255))
				
				For Angle = -90 To 360-102 Step 1
						
						x = 150 + (Longueur/2) * Sin(Angle / 180 * #PI)
						y = 150 + (Longueur/2) * Cos(Angle / 180 * #PI)
						Box(0,0,300,300,Rgb(255,255,255))
						DrawRotatedText(x, y, Texte$, (90+Angle)+ang_hypo+1, RGB(0, 0, 0))
						Delay(10)
				Next Angle
				
		StopDrawing()
		
EndIf

Repeat
		Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow


Dernière modification par Zorro le jeu. 23/mars/2017 12:53, modifié 3 fois.
Avatar de l’utilisateur
kernadec
Messages : 1606
Inscription : ven. 25/avr./2008 11:14

Re: Comment Faire une rotation d'un Texte en son centre

Message par kernadec »

eh ben le fameux trou noir :mrgreen:

@dobro oui il peut être a 1 :mrgreen: pour cela j'ai mis jour le code et ça tourne :wink:
j 'ajoute qu en utilisant un canvas au lieu de la fenêtre cela pourrait éliminer le scintillement

grillé par Dobro 8O :mrgreen:

Cordialement
Dernière modification par kernadec le jeu. 23/mars/2017 13:09, modifié 3 fois.
Répondre