Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Lining up text using SharpDx

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Lining up text using SharpDx

    Hi
    Putting the finishing touches to an EcoSystem update and I have an issue.
    I am using SharpDx to write the text for a counter like so
    SharpDX.DirectWrite.TextLayout textLayout1 = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Gl obals.DirectWriteFactory, timeLeft, textFormat1, ChartPanel.X + ChartPanel.W, textFormat1.FontSize);It's always in the format "HH:mm:ss";
    However I am putting some text underneath the first line and I am trying to find the 'centre' of this first line.

    I tried finding the text width via

    TextSize = textLayout1.Metrics.Width;

    and then my new lower string placement would be
    +"\n".PadRight((int)TextSize/2);

    I then realised that of course the text itself changes so I tried using an unused dummy layout

    SharpDX.DirectWrite.TextLayout dummyLayout1 = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Gl obals.DirectWriteFactory, "00:00:00", textFormat1, ChartPanel.X + ChartPanel.W, textFormat1.FontSize);

    TextSize =dummyLayout1.Metrics.Width;


    This also does not work so what am I doing wrong?

    #2
    Hello Mindset,

    What specifically isn't working?

    The SymbolWatermark gets the width of the text and divides this by 2 to center the text, which you may find helpful.
    Simple script that adds a background watermark of the instrument symbol (and expiry if a future) to the chart. (Updated July 10th, 2018 ‐ The opacity needed to be casted as a double and divided by 100 to be between 0 and 1) (Update: August 24th, 2021 – Subtracted the ChartPanel.Y so indicator can be […]
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I am trying to add a second line to an existing string in OnRender.
      But to have the second line of that string centered on the first line - hope that makes sense.
      This is the line that I am writing

      Code:
              CTS = Alternative_Clock_Now.ToString("HH:mm:ss");//.PadRight((int)TextSize/2)
                              if(barTimeLeft.Hours > 0)
                                      {
                                  timeLeft =  ShowPercent ?
                                  CTS +    timePercentage.ToString("N0") + "%":            
                                   CTS +
                             [COLOR=#e74c3c]     "\n"+
                                          barTimeLeft.Hours.ToString("00") +
                                  ":" + barTimeLeft.Minutes.ToString("00") +
                                  ":" + (barTimeLeft.Ticks < 0 ?"00:00:00": barTimeLeft.Seconds.ToString("00"));      [/COLOR]  
                                      }​
      It may be that I have to do it as two separate lines - just wondered if there were a way to do it with one single string that's split with a "\n"?

      Interestingly your symbol watermark is not centered either despite your coding so that makes me wonder if there is something wrong with my install?
      Click image for larger version

Name:	ES 03-24 (21 Second) 2024_02_23 (22_00_23).png
Views:	47
Size:	46.0 KB
ID:	1292822
      Attached Files

      Comment


        #4
        Hello Mindset,

        To confirm you are drawing two text layouts correct?

        One that draws the first line of text at the selected coordinates.
        The second that draws the second line of text at the coordinates adjusted to the center point of the first text layout?


        The SymbolWatermark indicator may be effected by scaling settings. In the Display settings do you have 'Change the size of text, apps, and other items' set to something other than 100%?
        (Note, if you change this setting, you will need to log out and back in to your windows profile (or at least restart NinjaTrader))

        I may be able to do further adjustments with the code in this script to work with Windows scaling.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Chelsea
          Firstly, no it's currently a single line of text but split by the escape "\n".
          The more I get into it the more it might be better to have it as two separate lines but that requires a whole host of other co ordinate coding which is a pain.
          And secondly yes my Display settings were set to 125% as 'recommneded' by Windows. - so that probably accounts for some of it?

          Comment


            #6
            Hello Mindset,

            The \n is not going to change the x coordinate of the second line of text. If you want this centered, you will need two rendered objects with different x coordinates.

            And yes, using a text size setting of 125% would throw off the rendering location.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              ok thanks . I thought you might say that.

              Comment


                #8
                Hi Chelsea

                Ok so I have made 2 texts but I am stumped by one thing.
                The first text is a clock in the format "HH:mm:ss"
                The second text is a number which can be anywhere from 1 to 8 characters long ( its a counter);

                I am trying to center the text using the first text as the sort of point of reference eg

                Click image for larger version  Name:	formatted counter.png Views:	0 Size:	3.6 KB ID:	1294540

                I move the text around the screen so I have a number of Vector points all of which are slightly different of course.
                How can I code this second text string to be always in the center of the first - seems simple but I can only do it by declaring a "dummy string" , grabbing the width and then sending that to a render helper along with my variable string, which I can then call in my SharpDX.Vector2 calls.

                I originally thought I could use the float return but that gave errors so I have to use the method call inside the vector call. Yeugh!

                Seems very inelegant and clumsy.
                Is there a better way?
                I have looked at textalignment but couldn't get it to do what I wanted somehow.



                Dummy string code
                Code:
                                SharpDX.DirectWrite.TextLayout dummystr = new  SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
                    "88:88:88",textFormat1,ChartPanel.X + ChartPanel.W, textFormat1.FontSize);
                            
                            
                             float w = dummystr.Metrics.Width;
                            float maxheight = dummystr.Metrics.Height;​
                Render helper code
                Code:
                private float RenderHelper_Width(string s, float w)// really  inelegant way to do this!
                {
                    
                        if(s.Length  > 5)
                        startPoint = 0;//w/8;
                        if(s.Length == 5)
                        startPoint = w/5;
                        if(s.Length == 4)
                        startPoint = w/4;
                        if(s.Length <= 2)
                        startPoint = w/3;
                
                return startPoint;
                }
                ​
                using the reder helper to formulate the new Vector2 (textlayout1 is the first string of text that I am trying to reference)

                Code:
                textPoint2     = new SharpDX.Vector2(ChartPanel.X +
                            ChartPanel.W/2+RenderHelper_Width(counterstr,maxwidth), ChartPanel.Y +
                            ChartPanel.H- textLayout1.Metrics.Height/2);​
                Hope that all makes sense

                Comment


                  #9
                  Hello Mindset,

                  You should have the X coordinate of the Vector2 used for the DrawTextLayout() call for the first text box.
                  You would be adding the textLayout1.Metrics.Width / 2 to that X value, which would be the center of the first text box.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Ha! you pointed me in the right direction
                    Once I actually applied some thought to it, all became obvious. Find the mid point of the first string, and then start string 2 from the middle of the width of the second string as the text is aligned centrally. Too much screen time.
                    Eventually, the correct line (for middle, bottom) is
                    Code:
                    textPoint2 = new SharpDX.Vector2(textPoint1.X+
                    textLayout1.Metrics.Width/2-textLayout2.Metrics.Width/2,
                    ChartPanel.Y + ChartPanel.H - textLayout1.Metrics.Height/2);​
                    I am finally near the end of the BarTimerHybrid update - hope to post it soon in the EcoSystem - it's been driving my OCD nuts!!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by fx.practic, 10-15-2013, 12:53 AM
                    5 responses
                    5,404 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by Shai Samuel, 07-02-2022, 02:46 PM
                    4 responses
                    95 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by DJ888, Yesterday, 10:57 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    159 views
                    0 likes
                    Last Post loganjarosz123  
                    Started by Belfortbucks, Yesterday, 09:29 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post Belfortbucks  
                    Working...
                    X