Page 1 of 1

Marquee Scrolling Text

Posted: Tue Apr 01, 2025 6:38 pm
by EaxVal
Hello PB Team,
I've created a window that includes a String Text, Button and a Marquee text.
The issue with the marquee starting and ending points.
How can I make this marquee starts from the right window edge and ends at the left edge (better to have 10 pixels margines from each edge)?

Code: Select all

; Marquee variables
Global marqueeText.s = "This is my beautiful Marquee Text ..."
Global marqueePosition.i = 0
Global marqueeDirection.i = 1 
Global marqueeSpeed.i = 1

Procedure UpdateMarquee()
  ; Calculate the visible portion of the marquee text
  marqueeTextLength = Len(marqueeText)
  gadgetWidth = GadgetWidth(9)
  visibleChars = gadgetWidth / 6 ; Approximate number of characters that fit (6 pixels per char)
  
  ; If the text is shorter than the gadget, pad it with spaces to make it scroll smoothly
  If marqueeTextLength < visibleChars
    paddedText.s = marqueeText + Space(visibleChars - marqueeTextLength + 10)
  Else
    paddedText = marqueeText + "    " ; Add some spaces at the end for smoother scrolling
  EndIf
  
  ; Update marquee position
  marqueePosition + marqueeDirection * marqueeSpeed
  
  ; Handle wrapping
  If marqueeDirection = 1 ; Right to left
    If marqueePosition > Len(paddedText)
      marqueePosition = -visibleChars
    EndIf
  Else ; Left to right
    If marqueePosition < -Len(paddedText)
      marqueePosition = visibleChars
    EndIf
  EndIf
  
  ; Build the visible portion of the text
  visibleText.s = ""
  For i = 0 To visibleChars
    pos = (marqueePosition + i) % Len(paddedText)
    If pos < 0
      pos + Len(paddedText)
    EndIf
    visibleText + Mid(paddedText, pos + 1, 1)
  Next
  
  SetGadgetText(9, visibleText)
EndProcedure

If OpenWindow(0, 0, 0, 320, 220, "Marque Text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  StringGadget(1, 10, 138, 300, 22, "Hello PB team!", #PB_String_ReadOnly)
  ButtonGadget(2, 130, 80, 40, 20, "Close") ; Close button
  
  ; Flat marquee text gadget at the bottom of the window
  TextGadget(9, 10, 198, WindowWidth(0) - 20, 20, "")
  SetGadgetAttribute(9, #PB_Text_Border, #True) ; Hide text outside the window
  SetGadgetColor(9, #PB_Gadget_BackColor, RGB(240, 240, 240))
  
  AddWindowTimer(0, 4, 100) ; Timer for marquee animation
  
  Repeat
    Event = WaitWindowEvent()
    
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 2 ; Close button
            Event = #PB_Event_CloseWindow ; Trigger close event
        EndSelect
        
      Case #PB_Event_Timer
        Select EventTimer()
          Case 4
            UpdateMarquee()
        EndSelect
    EndSelect
  Until Event = #PB_Event_CloseWindow
  CloseWindow(0)
EndIf

Re: Marquee Text

Posted: Tue Apr 01, 2025 7:42 pm
by Demivec
You could use a canvas gadget for the marquee instead of a text gadget and simply position the text according to your desires. I may be able to work up an example later today if you have any questions.

Re: Marquee Scrolling Text

Posted: Wed Apr 02, 2025 5:34 am
by AZJIO

Code: Select all

#Window = 0
#cnv = 0
#idTimer = 0
color = GetSysColor_(#COLOR_BTNFACE)

If OpenWindow(#Window, 0, 0, 320, 220, "Moving text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	CanvasGadget(#cnv, 0, 0, 320, 220)
	AddWindowTimer(#Window, #idTimer, 10)

	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Timer
				If EventTimer() = #idTimer
					If StartDrawing(CanvasOutput(#cnv))
						x + 1
						DrawText(x, 0, "Moving text", 0, color)
						If x > 320
							x=-60
						EndIf
						StopDrawing()
					EndIf
				EndIf
			Case #PB_Event_CloseWindow
				CloseWindow(#Window)
				End
		EndSelect
	ForEver
EndIf

Re: Marquee Scrolling Text

Posted: Wed Apr 02, 2025 9:51 am
by ChrisR
And the same from right to left

Code: Select all

#Window = 0
#cnv = 0
#idTimer = 0
color = GetSysColor_(#COLOR_BTNFACE)

If OpenWindow(#Window, 0, 0, 320, 220, "Moving text", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	CanvasGadget(#cnv, 0, 0, 320, 220)
	AddWindowTimer(#Window, #idTimer, 10)
	x = DesktopScaledX(WindowWidth(0) -10)

	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Timer
				If EventTimer() = #idTimer
					If StartDrawing(CanvasOutput(#cnv))
					  x - 1
					  Box(0, 0, OutputWidth(), OutputHeight())
						DrawText(x, 10, "Moving text", #Blue, color)
						If x < -TextWidth("Moving text")
							x = DesktopScaledX(WindowWidth(0) -10)
						EndIf
						StopDrawing()
					EndIf
				EndIf
			Case #PB_Event_CloseWindow
				CloseWindow(#Window)
				End
		EndSelect
	ForEver
EndIf

Re: Marquee Scrolling Text

Posted: Wed Apr 02, 2025 10:51 am
by EaxVal
Many thanks! :)