how to center text in maxgui

Started by wadmixfm, August 17, 2023, 12:21:47

Previous topic - Next topic

wadmixfm

hello peeps

what are the commands for centering text in a maxgui window

"~n" this i know moves it down 1 line

what other commands are there ??

cheers

Yes its really me :)

_PJ_

There is no "escape character" for centred text. Closest is probably using ~t to tabulate

I don't think there is a single, definitive command for this either, since "text on a window" is somewhat ambiguous.

One way to achieve this, may be to use a (or many) "labels" which can be aligned to the centre -I think-, depending on the requirements) and use the flags on creation to CENTER these labels to the parent window...

What, specifically  are you trying to achieve, is the text to be shown a known, constant literal or can it change length or contents etc. ?

Midimaster

you cannot write any text in a MaxGui-window. You need a Gadget as container for a text. And those text-gadgets know centering-commands.

As PJ already said: a Label would do the job for short terms. A textfield for more text, etc...

But also a creating a Canvas gadget is a possibity. In a Canvas you can use the DrawText() as in good old BlitzMax windows.

...crossing the alps with my bike at the moment

wadmixfm

hi thanks for the replies

i have opened another window with createwindow (etc)

i have an array reading data in to display certain parts of the array at certain times using a timecode

but what i am trying to do is center the 1 line of text thats all i need to display but each line is different

lengths for example

I know you loved him
A long time ago
Even now in my arms
You still want him, I know
But darling this time
Let your memories die
When you hold me tonight
Don't close your eyes


but i want it to show like this
I know you loved him
A long time ago
Even now in my arms
You still want him, I know
But darling this time
Let your memories die
When you hold me tonight
Don't close your eyes
but only one line at a time , the project i am working on is a lyric display for a singer
and when the drum machine plays the drums the timecode from the song tells the lyric bit to display
a certain line at a specified time but i just cant get it to center unless i manually edit the lyric page
to center the text which is tedious for me and the end user :)
This is my code for displaying the window and a test text
Import brl.D3D9Max2D
Import brl.Max2D
Import MaxGui.Drivers

Graphics 1024,768

Global lyr$[1,1]
lyr$[1,1]="This is just a test text"


Global window:TGadget = CreateWindow( "Lyric Window", 1130, 0, 1280, 1024 )

Global textarea:TGadget = CreateTextArea( 0, 0, ClientWidth(window), ClientHeight(window), window )
Global MyGuiFont:TGuiFont = LookupGuiFont( GUIFONT_SYSTEM, 50,  8 )
SetGadgetFont textarea, MyGuiFont
SetGadgetLayout( textarea, EDGE_ALIGNED,EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED)
Local AllLyr:String
AllLyr = AllLyr +"~n" + "~n"+ "~n"+ "~n"+ "~n"+ "~n"+ lyr[1,1] ' <---- this is my data line

  

SetGadgetTextColor( textarea, 255,255,120 )
SetGadgetText textarea, AllLyr

ActivateGadget( textarea )
WaitKey()
 
when run there will be 2 windows
the one thats blank is where my drum software will be and the maxgui window opens on a second monitor and displays the lyrics
if you click the black screen one it will focus and then all you need to do is hit any key and the program will end
lee
Yes its really me :)

Derron

You need a rich text area (which allows styling individual characters -- just think of wordpad or this very wysiwig input area of this forum).
MaxIDE - the editor you might be using - uses a textarea to stylize source code (syntax highlighting) maybe have a look how it is done.

Another approach is - and maybe the "better" (but less lightweight) idea is to use a max2d-canvas as output. This way you can make the lyrics output more ... visually pleasing. I mean smoothly growing/shrinking of the highlighted line, adding effects, scrolling smoothly, fading out old text ...
All the stuff you know from "karaoke software".


bye
Ron

Midimaster

#5

Here is a runnable example of a CANVAS with centered text
CanvasScroll.png
SuperStrict
Import MaxGui.Drivers
Local width:Int = 1000
Local height:Int =  600

Local window:TGadget=CreateWindow("Karaoke",100,100, width, height ,Null,WINDOW_TITLEBAR)
Local canvas:TGadget=CreateCanvas(0,0, width,height,window)

Global Text:String[] = CreateText()

CreateTimer 60
Local Scroll:Int=600

While WaitEvent()
    Select EventID()
        Case EVENT_TIMERTICK
            RedrawGadget canvas
            scroll= (scroll+599) Mod 600
        Case EVENT_GADGETPAINT
            SetGraphics CanvasGraphics(canvas)
            Cls
            For Local line:Int=0 Until 9
                DrawText Text[line], width/2 - TextWidth(Text[Line])/2  , scroll+line*30
            Next
            Flip
        Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
            End
    End Select
Wend


Function CreateText:String[]()
    Local t:String[9]
    For Local line:Int=0 Until 9
        Local r:Int= Rand(30,100)
        For Local j:Int=0 To r   
            Local ch:Int = Rand(32)+54
            If ch<64 Then ch=32
            t[line] = t[line] + Chr(ch)
        Next
    Next
    Return t
End Function
...crossing the alps with my bike at the moment

wadmixfm

#6
Hi again and thank you for your help :)

i have slightly modified the code you sent as i dont want it scrolling but i do want it in the center but larger

Import MaxGui.Drivers
Local width:Int = 1000
Local height:Int =  600

Local window:TGadget=CreateWindow("Karaoke",100,100, width, height ,Null,WINDOW_TITLEBAR)
Local canvas:TGadget=CreateCanvas(0,0, width,height,window)
Global Text:String[] = CreateText()



While WaitEvent()
    Select EventID()
        Case EVENT_TIMERTICK
            RedrawGadget canvas
        Case EVENT_GADGETPAINT
            SetGraphics CanvasGraphics(canvas)
            Cls
            For Local line:Int=0 Until 9
              DrawText Text[line], width/2 - TextWidth(Text[Line])/2  , line*30
            Next


            Flip
        Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
            End
    End Select
Wend


Function CreateText:String[]()
    Local t:String[9]
    t[1]="This is working but how to increase font size?"
    Return t
End Function


i have tried to put in setscale 2,2 that does work but then it wont center anymore

also i have tried to change the font but for some reason i can't get it to do that either , am i missing something here ??

Lee

ps i am running this on a mac (not windows) if this helps

lee
Yes its really me :)

Midimaster

No, you can simply use a TImageFont inside CANVAS as you would do in BlitzMax:

Select any size you need in LoadImageFont():

This modified code needs a file  ARIAL.TTF next to your  BMX-file
SuperStrict
Import MaxGui.Drivers
Local width:Int = 1000
Local height:Int =  600

Local window:TGadget=CreateWindow("Karaoke",100,100, width, height ,Null,WINDOW_TITLEBAR)
Local canvas:TGadget=CreateCanvas(0,0, width,height,window)
Local font:TimageFont=LoadImageFont("Arial.ttf",40,SMOOTHFONT)
Global Text:String[] = CreateText()

CreateTimer 60


While WaitEvent()
    Select EventID()
        Case EVENT_TIMERTICK
            RedrawGadget canvas
        Case EVENT_GADGETPAINT
            SetGraphics CanvasGraphics(canvas)
            SetImageFont Font
            Cls
            For Local line:Int=0 Until 9
                DrawText Text[line], width/2 - TextWidth(Text[Line])/2  , line*50+50
            Next
            Flip
        Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
            End
    End Select
Wend


Function CreateText:String[]()
    Local t:String[9]
    For Local line:Int=0 Until 9
        Local r:Int= Rand(10,30)
        For Local j:Int=0 To r   
            Local ch:Int = Rand(32)+54
            If ch<64 Then ch=32
            t[line] = t[line] + Chr(ch)
        Next
    Next
    Return t
End Function
...crossing the alps with my bike at the moment

wadmixfm

Thank you so much MidiMaster :) 

Lee 
Yes its really me :)

wadmixfm

hi again midimaster the code you sent to me above works great in windows but not the mac

i have downloaded the latest builds of mac 64 blitzmax ng but the fonts will not change , i have ported the Arial.ttf file next to my bmx file as directed but it still wont change maybe there is something wrong with the mod file for fonts on the mac ???

not my expertise to be fair

cheers

lee
Yes its really me :)

Midimaster

#10
This must be something new on MAC. I never had these problems in former times.

Try this test app. It is without MaxGUI and report back:

SuperStrict
Graphics 800,600
Local font:TimageFont=LoadImageFont("Arial.ttf",40,SMOOTHFONT)
If font=Null Then Notify "font not found"; End

SetImageFont Font

Global Text:String[] = CreateText()

CreateTimer 60

Repeat
    Cls
    For Local line:Int=0 Until 9
        DrawText Text[line], 400 - TextWidth(Text[Line])/2  , line*50+50
    Next
    Flip
Until AppTerminate()


Function CreateText:String[]()
    Local t:String[9]
    For Local line:Int=0 Until 9
        Local r:Int= Rand(10,30)
        For Local j:Int=0 To r  
            Local ch:Int = Rand(32)+54
            If ch<64 Then ch=32
            t[line] = t[line] + Chr(ch)
        Next
    Next
    Return t
End Function


If this does not work try this startup sequence:

SuperStrict
Graphics 800,600
Incbin "Arial.ttf"
Local font:TimageFont=LoadImageFont("incbin::Arial.ttf",40,SMOOTHFONT)
SetImageFont Font
Global Text:String[] = CreateText()

.....


Maybe it is a typo in file name? uppercase <-> lowercase?
...crossing the alps with my bike at the moment

wadmixfm

yes it is to do with the higher and lower case

sorted it

cheers
Yes its really me :)

wadmixfm

actually no it didnt work

when the drum machine bit plays it does open the second window but i cant get the text to show up

because its trying to handle 2 flips and it slows everything down

going back to my first attempt , if i could just get my text to center that would be it , thats all i need

grrrr soooo frustrating that max gui was supposed to be the dogs balls when it came to creating external windows but you are soooo limited to its functionality regarding text position and stuff

if they incorporated something like a locate command so you could say locate"lee was here" ,50,50,(center)  for example

just on another note i did notice that you can align the status text left center or right

could this be possible  ??

lee


Yes its really me :)

Midimaster

#13
I can help!

No need for despair! Of course there is always a solution.


A pure WINDOW has no surface where you can draw show or display anything. They always need Canvases. Each Gadget is a canvas, so your CANVAS is only another one.  CANVASes will not slow down your peorformance.

Each window needs a "Please-draw-me-now" command at the end. Always! Each! Gadget Windows do this also! There are always some dozend of FLIPs in all those windows, which are open on your screen! So you can also add some more FLIPs (one for each CANVAS).

You cannot say  MaxGui has "poor" or "incomplete" windows. Windows are a system thing and per default without content. MaxGui enables only the access to the windows for BlitzMax . Gadgets are something  like pre-designed mini-windows for easy access. 


BUG1: Use Flip 0

There is only one little mistake in the code! Try FLIP 0 instead of FLIP, This zero means "do not wait for the next VSYNC". Two FLIPs in your app  without this zero would wait already 32msec for 2 VSYNCs. This slows down your app.




BUG 2: RedrawGadget()

You can save a lot of time if you think about how often you need to redraw a window!!! I Canvas is re-draw, when a EVENT_GADGETPAINT Event appears. In my example I simply connected Redrawing with EVENT_TIMERTICK. This causes to throw a EVENT_GADGETPAINT 60 times a second. This is pretty to often. Normaly it would be sufficient to call RedrawGadget(), only once when the Karaoke Text changed. All other cases (f.e. user moved other window over karaoke window)  will automatically processed by the system.


...crossing the alps with my bike at the moment