View Single Post
  #1  
Old 02-01-2009, 04:01 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
Status Bars! Oh my goodness..

Thought I would create something useful for other developers. You know, we have every kind of different GUI controls, just nothing useful for status bars (like health bars). So I created exactly that. Well, can't be used like a GUI control, but it's created using a GuiShowImg control, so you can easily add it to a GUI control based interface. A little inspiration for this came from WoW's Lua API.

So, anyway, here goes. I suggest you read everything in this thread if you want to use this.

class statusbar:
PHP Code:
//#CLIENTSIDE
public function CreateBar(barNamebarParent) {
  if (
this.barName != nil || makevar(barName) != nil)
    return 
false;
  
  
this.barName barName;
  
this.barTexture "white.png";
  
this.barColors = { 111};
  
this.barWidth 16;
  
this.barHeight 16;
  
this.barValue 1;
  
this.barRange = { 0};
  
  new 
GuiShowImgCtrl(makevar(barName)) {
    
0;
    
0;
    
width thiso.barWidth;
    
height thiso.barHeight;
    
    
polygon = { 00,
                
thiso.barWidth0,
                
thiso.barWidththiso.barHeight,
                
0thiso.barHeight };
    
    
red thiso.barColors[0];
    
green thiso.barColors[1];
    
blue thiso.barColors[2];
    
alpha thiso.barColors[3];
    
mode 1;
  }
  
  if (
makevar(barParent) != nil)
    
makevar(barParent).addControl(makevar(barName));
  
  
makevar("z_" barName) = this;
  return 
true;
}



//update functions
//update the poly of the status bar
function updatePoly() {
  
temp.= (this.barValue this.barRange[1]);
  
with (makevar(this.barName)) {
    
width thiso.barWidth;
    
height thiso.barHeight;
    
polygon = { 00,
                
width temp.w0,
                
width temp.wheight,
                
0height };
    
    
partx 0;
    
party 0;
    
partw getImgWidth(thiso.barTexture) * temp.w;
    
parth getImgHeight(thiso.barTexture);
  }
}


//update the value of the status bar
function updateValue() {
  if (
this.barValue this.barRange[1])
    
this.barValue this.barRange[1];
  else if (
this.barValue this.barRange[0])
    
this.barValue this.barRange[0];

  
updatePoly();
}

//update the texture of the status bar
function updateTexture() {
  
with (makevar(this.barName))
    
image thiso.barTexture;
}

//update the color of the status bar
function updateColor() {
  
with (makevar(this.barName)) {
    
red thiso.barColor[0];
    
green thiso.barColor[1];
    
blue thiso.barColor[2];
    
alpha thiso.barColor[3];
  }
}
//updates end



//get and set the parent
public function GetParent()
  return 
makevar(this.barName).getparent();

public function 
SetParent(np) {
  if (
makevar(np) == nil)
    return 
false;
  
  
makevar(np).addControl(makevar(this.barName));
  return 
true;
}
//parent funcs end


//get and set the value
public function GetValue()
  return 
this.barValue;

public function 
SetValue(nv) {
  
this.barValue = (nv nv);
  
updateValue();
  return 
true;
}
//value funcs end


//get and set the range
public function GetRange()
  return 
this.barRange;

public function 
SetRange(mirmar) {
  
this.barRange = { (mir int(mir)), (mar int(mar)) };
  
updateValue();
  return 
true;
}
//range funcs end


//get and set the texture
public function GetTexture()
  return 
this.barTexture;

public function 
SetTexture(nt) {
  
this.barTexture nt;
  
updateTexture();
  return 
true;
}
//texture funcs end


//get and set the color
public function GetColor()
  return 
this.barColor;

public function 
SetColor(rgba) {
  
this.barColor = { rgb};
  
updateColor();
  return 
true;
}
//color funcs end


//get and set the dimensions
public function GetWidth()
  return 
this.barWidth;

public function 
GetHeight()
  return 
this.barHeight;

public function 
SetWidth(nw) {
  
this.barWidth int(nw);
  
updatePoly();
  return 
true;
}

public function 
SetHeight(nh) {
  
this.barHeight int(nh);
  
updatePoly();
  return 
true;
}
//dimension funcs end


//get and set the coordinates
public function GetX()
  return 
makevar(this.barName).x;

public function 
GetY()
  return 
makevar(this.barName).y;

public function 
SetX(nx) {
  
makevar(this.barName).int(nx);
  return 
true;
}

public function 
SetY(ny) {
  
makevar(this.barName).int(ny);
  return 
true;
}
//coord funcs end


//return the object type
public function objecttype()
  return 
"TStatusBar"
To make use of the code, add the following to a system wnpc or basically any other wnpc players normally have:
PHP Code:
//#CLIENTSIDE 
function onCreated() { 
  
this.sb = new TStaticVar("TStatusBar"); 
  
this.sb.join("statusbar"); 

Then you can do the following anywhere to create a new status bar:
PHP Code:
this.bar = new TStatusBar() 
Of course, it's up to you how you name the variable.


Here's a simple usage example:
PHP Code:
new GuiWindowCtrl("testWindow") {
  
profile GuiBlueWindowProfile;
  
128;
  
width 144;
  
height 64;
}

temp.sb = new TStatusBar();
temp.sb.CreateBar("testBar"nil);

z_testBar.SetX(32);
z_testBar.SetY(32);
z_testBar.SetWidth(96);
z_testBar.SetHeight(16);
z_testBar.SetTexture("crow_bartexture.png");
z_testBar.SetRange(01);
z_testBar.SetValue(1);
z_testBar.SetColor(0.310.41);

temp.z_testBar;
echo(
"x:" SPC z.GetX());
echo(
"y:" SPC z.GetY());
echo(
"width:" SPC z.GetWidth());
echo(
"height:" SPC z.GetHeight());
echo(
"value:" SPC z.GetValue());
echo(
"range:" SPC z.GetRange());
echo(
"color:" SPC z.GetColor());
echo(
"texture:" SPC z.GetTexture());
echo(
"parent:" SPC z.GetParent()); 
What it does is create a window control, then create a new bar, place it inside the window, and change different aspects of the bar. Final result is not very nice, but I just want to demonstrate the usage here.
After the bar creation, it will also echo some information of the newly created status bar.


Here goes a list of all availabe functions, plus return/argument types:

CreateBar(name : string, parent : string)
Creates a new status bar
If parent is nil, it will keep the default parent (GraalControl)


GetParent() : object
Returns the parent of the bar

GetValue() : float
Returns the current value of the bar

GetRange() : array
Returns the value range of the bar (min, max)

GetTexture() : string
Returns the file name of the currently used bar texture

GetColor() : array
Returns the bar color (red, green, blue, alpha)

GetWidth() : int
Returns the bar width (in pixels)

GetHeight() : int
Returns the bar height (in pixels)

GetX() : int
Returns the horizontal bar position (in pixels)

GetY() : int
Returns the vertical bar position (in pixels)


SetParent(parent : string)
Sets the parent of the bar

SetValue(value : float)
Sets the value of the bar (value >= 0)

SetRange(min : int, max : int)
Sets the value range of the bar (min, max >= 0)

SetTexture(filename : string)
Sets the bar texture to the specified file name

SetColor(red : float, green : float, blue : float, alpha : float)
Sets the bar color (0 <= red, green, blue, alpha <= 1)

SetWidth(width : int)
Sets the bar width (in pixels)

SetHeight(height : int)
Sets the bar height (in pixels)

SetX(x : int)
Sets the horizontal bar position (in pixels)

SetY(y : int)
Sets the vertical bar position (in pixels)



Last but not least, some important notes:
  • Although there's a CreateBar() function, you will have to use a new variable for every new bar.
  • CreateBar() will create a GuiShowImgCtrl with the specified name, and a global reference to the statusbar class called z_SPECIFIEDNAME.
  • You could probably use any image as a bar texture, but I suggest using a higher resolution grayscale image (like the example one I have attached).



That's it. I hope somebody will find use for this, and I hope I could help you out with this Attached is an example bar texture which I have created and used while creating this. You may use it, but I suggest not to do so, because it's ugly

If I forgot anything important in this thread, or if you find any bugs or you got any suggestions, post!
Attached Thumbnails
Click image for larger version

Name:	crow_bartexture.png
Views:	179
Size:	6.1 KB
ID:	47278  

Last edited by Crow; 02-01-2009 at 04:32 PM..
Reply With Quote