copy paste this code into a new txt document and name it gui.cpp
#include "gui.h"
#include "cvar.h"
#include "bind.h"
newgui gui;
// 0 nothing clicked
// 1 clicked on titlebar
// 2 clicked on close box
// 3 clicked on minimize box
// 4 clicked on an item
int window::handleClick()
{
ox = x;
oy = y;
cx = gui.x;
cy = gui.y;
rx = cx - x;
ry = cy - y;
if( isHiding )
{
if( !pointRect(rx,ry,getTitleBox()) )
return -1; // not in window
}
else
{
if( !pointRect(rx,ry,getWindowBox()) )
return -1; // not in window
}
if( pointRect(rx,ry,getCloseBox()) && hasClose )
{
if(!canClose)
{
isVisible = !isVisible;
return 0;
}
return 2; // destroy item
}
if( pointRect(rx,ry,getHideBox()) && hasHide )
{
isHiding = !isHiding;
return 3; // hide window
}
if( pointRect(rx,ry,getTitleBox()) && hasTitle && hasDrag )
{
isDragging = true;
return 1; // start dragging
}
for(int i=0;i<elements.size();i++)
if( pointRect(rx,ry,elements.bounds) )
{
switch(elements.type)
{
case 0: //PushButton
elements.flags[0] = 1; // set to pressed
clickedElement = i;
if(elements.callback)
(elements.callback)(i); // send to outside
break;
case 1: //CheckBox
elements.flags[0] = !elements.flags[0]; // toggle
clickedElement = i; // like it matters
if(elements.callback)
(elements.callback)(i); // send to outside
break;
case 2: //TextArea
//Don't do anything
break;
case 3: //InputField
gui.clearFocus();
elements.flags[0] = 1; //has focus now
elements.flags[3] = 0; //move start pointer
gui.acceptKeys = i;
clickedElement = i;
if(elements.callback)
(elements.callback)(i); // send to outside
return 5; //special case
break;
case 4: //ScrollBar
//Do something eventually
break;
default:
clickedElement = -1; // do we even need this?
break;
}
return 4; // ok we clicked on a element
}
return 0;
}
void newgui::clearFocus()
{
acceptKeys = -1;
for(int j=0;j<gui.sWin.size();j++)
for(int i=0;i<gui.sWin[j].elements.size();i++)
if(gui.sWin[j].elements.type == eINPUT)
gui.sWin[j].elements.flags[0] = 0;
}
void window::updateDrag()
{
x = ox + (gui.x - cx); // add the distant moved to original
y = oy + (gui.y - cy); // add the distant moved to original
}
// a button creater
element newgui::makeButton( int x, int y, int w, char* label, void (*call)(int),int r,int g,int b)
{
element ret;
ret.bounds.left = x;
strcpy(ret.data,label);
//gEngfuncs.pfnDrawConsoleStringLen(label,&ret.bounds.right,&ret.bounds.bottom);
ret.bounds.right = x + w;
ret.bounds.top = y;
ret.bounds.bottom = y + 16;
ret.type = eBUTTON;
ret.flags[0] = 0; // we dont set any other flags, lets hope thats okay
ret.flags[1] = r;
ret.flags[2] = g;
ret.flags[3] = b;
ret.callback = call;
return ret;
}
element newgui::makeCheck( int x, int y, char* label, void (*call)(int),int value)
{
element ret;
ret.bounds.left = x;
ret.bounds.right = x + 10;
ret.bounds.top = y;
ret.bounds.bottom = y + 10;
ret.type = eCHECK;
ret.flags[0] = value;
strcpy(ret.data,label);
ret.callback = call;
return ret;
}
// a text creater
element newgui::makeText( int x, int y, char* label,void (*call)(int), int flag, int r, int g, int b )
{
element ret;
ret.bounds.left = x;
ret.bounds.top = y;
ret.bounds.right = x;
ret.bounds.bottom = y;
strcpy(ret.data,label);
ret.type = eTEXT;
ret.flags[0] = flag; // for wrapping etc.
ret.flags[1] = r;
ret.flags[2] = g;
ret.flags[3] = b;
ret.callback = call;
return ret;
}
element newgui::makeInput( int x, int y, char* label,void (*call)(int), int focus,int max,int num)
{
element ret;
ret.bounds.left = x;
strcpy(ret.data,label);
//gEngfuncs.pfnDrawConsoleStringLen(label,&ret.bounds.right,&ret.bounds.bottom);
ret.bounds.right = x + (max * 10);
ret.bounds.top = y;
ret.bounds.bottom = y + 16;
ret.type = eINPUT;
ret.flags[0] = focus;
ret.flags[1] = max;
ret.flags[2] = num;
ret.flags[3] = max;
ret.callback = call;
//if(ret.data = '.') { ret.data=0.0; }
return ret;
}
element newgui::makeExternal( int x, int y )
{
element ret;
ret.bounds.left = x;
ret.bounds.right = x;
ret.bounds.top = y;
ret.bounds.bottom = y;
ret.type = eEXT;
return ret;
}
// a window creater
void newgui::CreateSWindow(int xx, int yy, int w, int h, const char* c, bool f1, bool f2, bool f3, bool f4)
{
window theWindow;
theWindow.x = xx;
theWindow.y = yy;
theWindow.w = w;
theWindow.h = h;
theWindow.title = c;
theWindow.hasClose = f1;
theWindow.hasTitle = f2;
theWindow.hasDrag = f3;
theWindow.hasHide = f4;
theWindow.isHiding = false;
theWindow.isDragging = false;
theWindow.isVisible = true;
theWindow.canClose = false; // won't be destroyed
sWin.push_back(theWindow);
moveToTop(sWin.size()-1);
}
// a window creater
void newgui::CreateTWindow(int x, int y, int w, int h, const char* c, bool f1, bool f2, bool f3, bool f4)
{
window theWindow;
theWindow.x = x;
theWindow.y = y;
theWindow.w = w;
theWindow.h = h;
theWindow.title = c;
theWindow.hasClose = f1;
theWindow.hasTitle = f2;
theWindow.hasDrag = f3;
theWindow.hasHide = f4;
theWindow.isHiding = false;
theWindow.isDragging = false;
theWindow.isVisible = true;
theWindow.canClose = true; // can be destroyed
sWin.push_back(theWindow);
}
int newgui::click()
{
for(int j=0;j<10;j++)
{
int i = gui.windowOrder[j];
if(i>-1)
{
gui.clickType = gui.sWin.handleClick();
gui.mouseState = true;
if( gui.clickType>-1 && gui.clickType!= 5)
{
moveToTop(i);
gui.clearFocus();
}
if( gui.clickType == 0 ) // click in window
return i;
if( gui.clickType == 1 ) // start dragging timer
return i;
if( gui.clickType == 2 ) // clicked on close
{
gui.sWin.erase(&gui.sWin);
return -1;
}
if( gui.clickType == 3 ) // clicked on minimize
return i;
if( gui.clickType == 4 || gui.clickType == 5) // clicked on object
return i;
}
}
return -1;
}
void newgui::mouseUp()
{
if(gui.clickedWindow>=0)
{
gui.sWin[gui.clickedWindow].isDragging = false;
int e = gui.sWin[gui.clickedWindow].clickedElement;
if(e>-1)
switch(gui.sWin[gui.clickedWindow].elements[e].type)
{
case eBUTTON:
gui.sWin[gui.clickedWindow].elements[e].flags[0] = 0;
break;
default:
break;
}
gui.sWin[gui.clickedWindow].clickedElement = -1;
}
gui.mouseState = false;
}
void newgui::mouseDown()
{
if(gui.clickedWindow = click())
{
if( gui.clickedWindow == -1 )
{
gui.mouseState = false; //lets just forget about the click
gui.clearFocus();
}
}
gui.mouseState = true;
}
void newgui::moveToTop(int index)
{
if(sWin[index].order == 0)
return; // already in front, don't do anything
int realIndex = sWin[index].order;
for(int i=realIndex;i>0;i--)
{
windowOrder = windowOrder[i-1];
if(windowOrder>-1)
sWin[windowOrder].order = i;
}
windowOrder[0] = index;
sWin[index].order = 0;
}
bool pointRect(int x,int y,wrect_t r)
{
return ( x>=r.left && x<=r.right && y>=r.top && y<=r.bottom );
}