1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Draw a rectangle to match the string width.

Discussion in 'Programming' started by Gasil, Apr 23, 2018.

  1. Gasil

    Gasil Member

    Joined:
    Mar 18, 2018
    Posts:
    58
    I'm trying to draw a rectangle window in a menu system, which uses an array to store the options. The thing is that such options aren't always going to be the same, they could vary in width and height and I'd like the rectangle to match those measures.

    This is the code.


    Code:
    if (alpha < 1) alpha += 0.08; else alpha = 1;
    
    draw_set_color(c_black);
    draw_rectangle(x + espacio, y, x + string_width(string(diagMenu)), y + 100, 0);
    draw_set_color(c_white);
    
    draw_set_alpha(1);
    
    
    //Dibujar Texto
    var m;
    
    for (m = 0; m < array_length_1d(diagMenu); m += 1)
    {
        draw_text(x + espacio, y + (m * espacio), string(diagMenu[m]));
    }
    
    I tried to use string_width(string(diagMenu)) to get the width of the strings; however it seems to give me a sum of every option. But weirdly enough for me, I did a test to check the width of a single option and it gave me the following result. I used string_width(string(diagMenu[0]))

    https://imgur.com/IJlrF3r
    [​IMG]

    the rectangle doesn't cover all the width of the string.

    Should I use the function in another way?
     
  2. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    1,072
    Loop through the array and compare all the values to find the widest string, storing the result in a variable.
     
    Gasil likes this.
  3. Gasil

    Gasil Member

    Joined:
    Mar 18, 2018
    Posts:
    58
    Sounds simple and good..., would you mind to teach me how, please?
     
  4. BattleRifle BR55

    BattleRifle BR55 Member

    Joined:
    Jun 24, 2016
    Posts:
    1,072
    Code:
    var _maxwidth = 0, _temp;
    
    _temp = string_width(diagMenu[m]);
    if (_temp > _maxwidth)
        _maxwidth = _temp;
     
  5. Gasil

    Gasil Member

    Joined:
    Mar 18, 2018
    Posts:
    58
    Thanks friend, I'll see if I can make it work using your code.
     
  6. mafon2

    mafon2 Member

    Joined:
    Sep 13, 2016
    Posts:
    18
    I'm trying to do the same thing. I didn't touch the project in a year so half of it is mystery to me.

    I have an object (o_textbox) that draws text above the head of character.

    I want to add a black rectangle beside the text.

    Draw Event:

    draw_set_color (c_black);
    draw_rectangle(x-2, y+16, x + 2 + string_width(o_textbox), y, false);

    draw_set_color (c_white);
    draw_text(x,y,text);

    No matter the text, the rectangle is one character wide.

    I tried to add variable that tied to string_width in step event to no result.

    Also I tried to copy-paste previous solution with no effect. And I don't understand it.

    It's like easier to create a black sprite with changing width than to make it properly.
     
  7. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,840
    to both OP and above... why are you passing something that is NOT A STRING to a function that requires a STRING to determine the width in pixels of a string!!!?

    >string_width(o_textbox)
    that's an object... how is gml supposed to know you want to measure that text variable specifically?

    >string_width(string(diagMenu))
    that is a 1d array... how is gml supposed to know to loop through it and find the longest string in the array? string of an array btw returns "{{word1, word2, word3,...}}"


    :)
     
  8. mafon2

    mafon2 Member

    Joined:
    Sep 13, 2016
    Posts:
    18
    > how is gml supposed to know you want to measure that text variable specifically?

    So how does gml suppose to know the length of the text object draws?
     
    BattleRifle BR55 likes this.
  9. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,840
    How about string_width(text) since that is what you are drawing
     

Share This Page