Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Draw bitmap numbers (https://forums.graalonline.com/forums/showthread.php?t=134258865)

DustyPorViva 04-21-2010 11:19 PM

Draw bitmap numbers
 
1 Attachment(s)
Made these simple functions to display numbers as images instead of text. Details such as these can make a lot of difference in presentation :)

PHP Code:

function DrawNumber(ind,num,ix,iy,center) {
  if (
this.("oldindex_" ind) > num.length()) hideimgs(ind,ind this.("oldindex_" ind));

  
temp.charmap "0123456789/:";
  
temp.numimg  "state.png";
  
temp.numx    0;
  
temp.numy    81;
  
temp.numw    14
  
temp.numh    16;
  
temp.offset  3;
  
temp.coffset center * ((num.length()*(numw-offset))/2);

  for (
temp.i=0;i<num.length();i++) {
    
with (findimg(ind+i)) {
      
image num.substring(i,1) == " " "" numimg;
      
x     ix coffset + (numw offset)*i;
      
y     iy;
      
partx numx charmap.pos(num.substring(i,1))*numw;
      
party numy;
      
partw numw;
      
parth numh;
      
layer 4;
    }
  }
  
this.("oldindex_" ind) = num.length();
}

function 
DrawCustomNumbers(ind,num,ix,iy,img,chars,numx,numy,numw,numh,offx,center) {
  if (
this.("oldindex_" ind) > num.length()) hideimgs(ind,ind this.("oldindex_" ind));

  
temp.coffset center * ((num.length()*(numw-offx))/2);
  if (
temp.numw == nulltemp.numw 16;
  if (
temp.numh == nulltemp.numh 16;

  for (
temp.i=0;i<num.length();i++) {
    
with (findimg(ind+i)) {
      
image num.substring(i,1) == " " "" img;
      
x     ix coffset + (numw offx)*i;
      
y     iy;
      
partx numx chars.pos(num.substring(i,1))*numw;
      
party numy;
      
partw numw;
      
parth numh;
      
layer 4;
    }
  }
  
this.("oldindex_" ind) = num.length();


There are two functions. One is simple, one is not-so-simple.

DrawNumber: This is the simple function. What it does is display numbers using state.png. Thus, it is limited to the characters defined in that, "0123456789/:" and spaces. If you'd like, you could simply take this function and customize the variables defined in it. Parameters are:

DrawNumber(index,text,x,y[,center?]);

Index is the starting index to display the images. text is what you wish to display. I recommend sending it as a string to assure gscript doesn't mess up the values. And the last parameter is whether you want the text centered on the x-position or not. You can omit this if you'd like, and it just won't center it. An example of using it:

DrawNumber(200,"0123 456",20,150);
DrawNumber(200,player.mp,20,150,true);
DrawNumber(200,03242,20,150);




DrawCustomNumbers: This is the complicated version. This lets you define all the parameters so that you can use a customized image instead of state.png. The parameters are:

DrawNumber(index,text,x,y,charactermap,partx,party ,partw,parth,spacing[,center?]);

The character map is the layout of all the characters as they are in the image. This must be a string. For example, the character map for state.png is "0123456789/:". Partx/y/h/w should be fairly self-explanatory. These will define the startx/y of the number map in an image(typically 0,0 but may be different if in a gui template). Partw/h define the width and height of the characters. As precautionary, these will default to 16,16 if they are not defined. Lastly is the spacing. This will offset the space between the images when they are drawn. Example of usage:

DrawCustomNumbers(200,draw,20,150,"dusty_defaultnu mbers.png","0123456789/:.",0,0,14,16,3,true);


Oh yes, and both functions already account for spaces. You do not need to account for them in your character maps.

edit: obligatory screenshot attached. Also, I may eventually add the ability to perhaps center the text...

fowlplay4 04-21-2010 11:57 PM

I like that, any chance of get functions for the width and height? I imagine those will come if you make the center function though.

DustyPorViva 04-22-2010 12:19 AM

Quote:

Originally Posted by fowlplay4 (Post 1571141)
I like that, any chance of get functions for the width and height? I imagine those will come if you make the center function though.

What do you mean get functions? Performing the centering and such is actually fairly simple. x - ((text.length()*(partw-offset))/2). I was just thinking of how I could easily add the ability to define such an option as the complex function is already quiet long with parameters.

fowlplay4 04-22-2010 01:09 AM

I meant.. getStateTextWidth(text) + getStateTextHeight(text)

DustyPorViva 04-22-2010 01:11 AM

Quote:

Originally Posted by fowlplay4 (Post 1571156)
I meant.. getStateTextWidth(text) + getStateTextHeight(text)

Hmmmm... I could possibly allow something like:
text = "012345";
DrawNumber(index,text,x + DrawCenter(text),y);

DustyPorViva 04-22-2010 01:41 AM

Okay, I've updated it and just added a parameter for centering. Also fixed a major bug where it wasn't hiding left-over images after being updated.

adam 04-22-2010 05:42 AM

Not bad, not bad at all.


All times are GMT +2. The time now is 12:45 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.