XTextdraw [added stuff to textdraws]

Started by karan20000000000, Dec 13, 2015, 06:42 PM

Previous topic - Next topic

karan20000000000

The topic itself is self explanatory; a couple of features have been added to textdraws.
XTextdraw
     .ID         //ID of xtxd instance
     .text      //The text to be displayed
     .pos      //The array containing x,y coord of txd
     .col       //The hex color of txd.
     .obj      //The actual textdraw instance
     .players  //An array containing players to which an instance of xtextdraw is being visible
     .ShowForAll()
     .ShowForPlayer(player)
     .HideFromAll()
     .HideFromPlayer(player)
     .SetRelativeForAll(boolean)
     .Delete()

 CreateXTextdraw(text,x,y,colour)
 FindXTextdraw(id)

An example to illustrate above functions:
CreateXTextdraw("momo",400,500,0xFFFFFFFF);
local hi = FindXTextdraw(0);
hi.pos[1]-=10;
hi.col=0xFF3445FF;

I didn't add  Textdraw.SetPosForAll, Textdraw.SetPosForPlayer, Textdraw.SetColourForAll, Textdraw.SetColourForPlayer because they will require a different approach. In case you use them, try modifying the code yourself.
Since this is the very first release, so there could be a couple of bugs. Just put them down.
Link to snippet: clicky
[spoiler=Main Code]
class XTxd
{
    text = null;
    ID = null;
    pos = null;
    col = null;
    obj = null;
    players = null;
    isvta = false;
    isrfa = false;

    //static for these
    count = [0];
    timer = [null];
    objs = [];

    constructor(str,x,y,colr) {
      count[0]++;
      if(pos==null)
      {
        pos = [0,0];
        players = [];
      }
      text = str;
      pos = [x,y];
      col = colr;
      obj = ::CreateTextdraw(str,x,y,colr);

      local did=false;
      for(local i=0;i<XTxd.objs.len();i++)
      {
        local dat = XTxd.objs[i];
        if(dat==null)
        { XTxd.objs[i] = this; did=true; ID = i; break; }
      }
      if(did!=true)
      {
        XTxd.objs.push(this);
        ID = XTxd.objs.len()-1;
      }

      if(count[0] == 1)
      {
        timer[0] = ::NewTimer("ref",1000,0);
      }

    }

    static function refresh()
    {
      for(local i=0;i<objs.len();i++)
      {
        local o =   objs[i];
        if(o!=null)
        {
            o.obj.Delete();
            o.obj = ::CreateTextdraw(o.text,o.pos[0],o.pos[1],o.col);
            o.obj.SetRelativeForAll(o.isrfa);
            if(o.isvta==true) o.obj.ShowForAll();
            else if(o.players.len()>0)
            {
              foreach (plr in o.players)
               {
                o.obj.ShowForPlayer(plr);
               }
            }
        }
      }
    }

    function ShowForPlayer(player)
    {
      players.push(player);
    }
    function ShowForAll()
    {
      isvta = true;
    }
    function HideFromPlayer(player)
    {
      if(players.find(player)!=null)
      {
        players.remove(players.find(player));
      }
    }
    function HideFromAll()
    {
      isvta = false;
    }
    function SetRelativeForAll(val)
    {
      isrfa = val;
    }
    function Delete()
    {
      obj.Delete();
      objs[ID] = null;
      --count[0];
    }

}

function CreateXTextdraw(str,x,y,col)
{
  return XTxd(str,x,y,col);
}
function FindXTextdraw(id)
{
  for(local i=0;i<XTxd.objs.len();i++)
  {
    if(XTxd.objs[i]!=null && id == XTxd.objs[i].ID) return XTxd.objs[i];
  }
  return null;
}

function ref()
{
  XTxd.refresh();
}
[/spoiler]
With regards,
KP
------------------------------------------

DizzasTeR

So what is the advantage of this over the default functions?

karan20000000000

Quote from: Doom_Kill3R on Dec 14, 2015, 09:18 AMSo what is the advantage of this over the default functions?
a couple of features have been added to textdraws
If you look at the stuff above, you can see what all has been added.
The txd.text was one of feature which most of us wanted to exist in default implementation.
With regards,
KP
------------------------------------------

KAKAN

I can't see that paste for the reason my net is slow, I have one question.
By deafult the ".SetRelativeForAll(boolean)" is set to true or false?
Please set it to true if it's false!

Btw, Awesome work!
oh no

dEaN

not bad but who add this, time lost
I think first impressions are important when i pick up a Main.nut script and I'm sticking to the script, I'm putting that organic feeling back in the game.
-Since 2012-