Eto.Forms.Drawable - DrawText - Text Not Showing?

Hello,

I was using Label controls on top of drawable buttons until I realize the label controls steal cursor focus from the drawable button.

I now am attempting to draw the “label text” within the drawable itself but cannot get it to show the text despite it seemingly being correct (to me) in code.

Here’s a little pill shape drawable:
image

            e.Graphics.DrawImage(self.icon_hover if self._hover else (self.icon_light if dark_mode else self.icon_dark), self._i_rect)  # Draw Icon
            e.Graphics.DrawPath(self._b_pen_2 if self._state and not self._hover else (self._h_pen if self._hover else self._b_pen), self._h_path)  # Draws The Outline Graphics
            label_font = Eto.Drawing.Font("Arial", 10, Eto.Drawing.FontStyle.NONE, Eto.Drawing.FontDecoration.NONE)
            Rhino.RhinoApp.WriteLine(label_font)
            e.Graphics.DrawText((Eto.Drawing.Font(label_font, fg_color_light if dark_mode else fg_color_dark, self.c_text)), Eto.Drawing.PointF(self.Bounds.Width / 2, self.Bounds.Height / 2))

You can see I am drawing the image icon to the left, the “color hover” path around the pill shape and then I am expecting to see text showing up as well but no luck there.

Could someone please help with some guidance on how to use DrawText in a drawable?

When I print out the “label_font” I do get a valid text object as expected but I cannot get to visually show up.

Thank you for your response!

1 Like

Hi @michaelvollrath, hard to say without beeing able to test the code. Two things stand out though:

Eto.Drawing.FontStyle.NONE, Eto.Drawing.FontDecoration.NONE

should be:

Eto.Drawing.FontStyle.None, Eto.Drawing.FontDecoration.None

_
c.

1 Like

I’ll see if I can excerpt some of the code or just quick create a simplified example.
It’s pretty length overall in a much larger file so I tried to isolate.

Here’s why I had chosen NONE it appeared to be an enum option of FontStyle?

Screen Snip:

image

Thanks @clement , I’ll upload some code soon!

Hi @michaelvollrath, is this python 3 or 2 ?

_
c.

@clement Python 3, about to share some code

Here’s the method I’m trying to use ^

Set the “font” as in “arial” “times new roman” or whatever font name it is.

Set the font color.

Set the location to say 0,0 which would be the upper left corner of the drawable bounds aka self.bounds in this class.

Lastly, set the “string” of what the text actually says.

EDIT:

Okay I finally got it working!!

Ugly prototype test:
image

Code I needed:

    def OnPaint(self, e):
        try:

            self._b_pen = Eto.Drawing.Pen(Eto.Drawing.Colors.DarkCyan, 1)
            self._b_pen_2 = Eto.Drawing.Pen(Eto.Drawing.Colors.Cyan, 1)

            if self._hover:
                # draws the shape outline on hover
                # e.Graphics.FillPath(self._rg_brush, self._path)
                pass

            if self._state:
                # draws the enabled squircle
                e.Graphics.FillPath(Eto.Drawing.Colors.Black, self._b_path)

            else:
                # draws the disabled squircle
                e.Graphics.FillPath(Eto.Drawing.Colors.DarkGray, self._b_path)

            e.Graphics.DrawPath(self._b_pen_2 if self._state and not self._hover else (self._h_pen if self._hover else self._b_pen), self._h_path)  # Draws The Outline Graphics

            # Set Up Font For DrawText
            label_font = Eto.Drawing.Font("Arial", 10, Eto.Drawing.FontStyle.Bold, Eto.Drawing.FontDecoration.Underline)
            # label_font = Eto.Drawing.SystemFont.Default
            # Rhino.RhinoApp.WriteLine(str(label_font))

            # Draw Text On Top Of Drawable Background Graphics
            e.Graphics.DrawText(label_font, Eto.Drawing.Colors.Red, Eto.Drawing.PointF(0, 0), "Test Drawable Text")

        except Exception as ex:
            print(ex)

I was trying to set the text inside the Eto.Drawing.Font instead of the DrawText method. Oddly it didn’t give me any errors…

So this…

e.Graphics.DrawText((Eto.Drawing.Font(label_font, fg_color_light if dark_mode else fg_color_dark, self.c_text)), Eto.Drawing.PointF(self.Bounds.Width / 2, self.Bounds.Height / 2))

Should simply be:

e.Graphics.DrawText(label_font, Eto.Drawing.Colors.Red, Eto.Drawing.PointF(0, 0), "Test Drawable Text")

Thanks for your help @clement !

3 Likes

I tried custom drawing strings, but my code doesn’t seem to work. I borrowed some code from your font, but it doesn’t seem to work. I’m not sure what’s wrong.
my code

import Eto.Forms as ef
import Eto.Drawing as ed
form = ef.Form()
form.Width = 300
form.Height = 300
drawable = ef.Drawable()
drawable.Width = 200
drawable.Height = 200
darw_font = ed.Font("Arial", 10, ed.FontStyle.Bold, ed.FontDecoration.Underline)
darw_pen = ed.Pen(ed.Colors.Red,3)
darw_location = ed.PointF(0,0)
darw_Txet = "Hello"
def draw(sender, e):
    e.Graphics.DrawText(darw_font,darw_pen,darw_location,darw_Txet)
drawable.Paint += draw
Layout_0 = ef.DynamicLayout()
Layout_0.AddRow(drawable)
form.Content = Layout_0
form.Show()

I see the problem. I’m passing the wrong parameter, I shouldn’t pass pen, I should pass color.Thank you for sharing, wish you a happy day.