#define ALFONT_DLL
#include <alfont.h>
#include <allegro.h>
#include <iostream>
using namespace std;
#define WHITE makecol(255, 255, 255)
#define SM_MOUSEMOVE 1
#define SM_MOUSEDOWN 2
#define SM_MOUSEUP 4
#define SM_KEYDOWN 8
#define SM_KEYUP 16
#define BTN_LEFT 1
#define BTN_RIGHT 2
#define BTN_MID 4
enum T_BORDER
{
T_FULL, T_DASHED, T_POINTED, T_NONE
};
class cgstyle
{
public:
T_BORDER bordertype;
int back,fore;
cgstyle(T_BORDER bordertype, int back, int fore);
};
cgstyle::cgstyle(T_BORDER bordertype, int back, int fore)
{
this->bordertype=bordertype;
this->back=back;
this->fore=fore;
}
class cgmouse
{
public:
short omouse_x,omouse_y,omouse_b,msg;
cgmouse();
void process();
void answer(int val);
} mouse;
cgmouse::cgmouse()
{
this->omouse_x=this->omouse_y=this->omouse_b=this->msg=0;
}
void cgmouse::process()
{
this->msg=0x0;
if (mouse_x!=this->omouse_x || mouse_y!=this->omouse_y) { this->msg|=SM_MOUSEMOVE; }
if (mouse_b>0 && mouse_b!=this->omouse_b) { this->msg|=SM_MOUSEDOWN; }
if (mouse_b==0 && mouse_b!=this->omouse_b) { this->msg|=SM_MOUSEUP; }
this->omouse_x = mouse_x;
this->omouse_y = mouse_y;
this->omouse_b = mouse_b;
}
void cgmouse::answer(int val)
{
this->msg -= val;
}
class cgbutton
{
public:
int posx,posy,w; // cgedit position
bool clicked,down; // if clicked
bool visible; // if cgbutton is visible
cgstyle *style; // style
char *text; // displayed text
ALFONT_FONT *czc; // pointer to used font
cgbutton(int posx, int posy, int width, cgstyle *style, char *value, ALFONT_FONT *fnt, bool visible);
bool process();
void draw(BITMAP *dest);
};
cgbutton::cgbutton(int posx, int posy, int width, cgstyle *style, char *value, ALFONT_FONT *fnt, bool visible)
{
this->posx=posx;
this->posy=posy;
this->czc=fnt;
this->w=width;
this->style=style;
this->text=value;
this->visible=visible;
}
bool cgbutton::process()
{
this->down=0;
this->clicked=0;
if (mouse_x>=this->posx && mouse_x<=this->posx+this->w && mouse_y>=this->posy && mouse_y<=this->posy+alfont_text_height(this->czc)+8)
{
if (mouse.omouse_b&BTN_LEFT || mouse.omouse_b&BTN_RIGHT || mouse.omouse_b&BTN_MID)
{
this->down=1;
if (mouse.msg&SM_MOUSEUP/*mouse_b==0 && mouse.omouse_b>0*/)
{
//mouse.answer(SM_MOUSEUP);
this->clicked=1;
cout<<"yea\n";
}
else this->clicked=0;
}
}
return this->clicked;
}
void cgbutton::draw(BITMAP *dest)
{
if (this->visible)
{
if (this->down)
{
hline(dest, this->posx+1, this->posy, this->posx+this->w+7, makecol(getr(this->style->back)-64,getg(this->style->back)-64,getb(this->style->back)-64));
hline(dest, this->posx+1, this->posy+alfont_text_height(this->czc)+8, this->posx+this->w+7, makecol(getr(this->style->back)+64,getg(this->style->back)+64,getb(this->style->back)+64));
vline(dest, this->posx, this->posy+1, this->posy+alfont_text_height(this->czc)+7, makecol(getr(this->style->back)-64,getg(this->style->back)-64,getb(this->style->back)-64));
vline(dest, this->posx+this->w+8, this->posy+1, this->posy+alfont_text_height(this->czc)+7, makecol(getr(this->style->back)+64,getg(this->style->back)+64,getb(this->style->back)+64));
}
else
{
hline(dest, this->posx+1, this->posy, this->posx+this->w+7, makecol(getr(this->style->back)+64,getg(this->style->back)+64,getb(this->style->back)+64));
hline(dest, this->posx+1, this->posy+alfont_text_height(this->czc)+8, this->posx+this->w+7, makecol(getr(this->style->back)-64,getg(this->style->back)-64,getb(this->style->back)-64));
vline(dest, this->posx, this->posy+1, this->posy+alfont_text_height(this->czc)+7, makecol(getr(this->style->back)+64,getg(this->style->back)+64,getb(this->style->back)+64));
vline(dest, this->posx+this->w+8, this->posy+1, this->posy+alfont_text_height(this->czc)+7, makecol(getr(this->style->back)-64,getg(this->style->back)-64,getb(this->style->back)-64));
}
floodfill(dest, this->posx+2, this->posy+2, this->style->back);
//textout_ex(dest, this->czc, this->text, this->posx+5+this->w/2-text_length(this->czc, this->text)/2, this->posy+5, this->style->fore, -1);
alfont_textout_aa_ex(dest, this->czc, this->text, this->posx+5+this->w/2-alfont_text_length(this->czc, this->text)/2, this->posy+5, this->style->fore, this->style->back);
}
}
class cgedit
{
public:
int posx,posy,w,focus; // cgedit position
bool visible; // if cgedit is visible
cgstyle *style; // style
string edittext; // an empty string for editting
string::iterator iter; // string iterator
int caret; // tracks the text caret
bool insert; // true if text should be inserted
ALFONT_FONT *czc; // pointer to used font
int newkey; // -,
char ASCII; // | keys' info
char scancode; // -'
cgedit(int posx, int posy, int width, cgstyle *style, ALFONT_FONT *fnt, bool visible);
bool capture();
void draw(BITMAP *dest);
};
cgedit::cgedit(int posx, int posy, int width, cgstyle *style, ALFONT_FONT *fnt, bool visible)
{
this->iter=edittext.begin();
this->caret=0;
this->insert=true;
this->posx=posx;
this->posy=posy;
this->czc=fnt;
this->w=width;
this->style=style;
this->visible=visible;
}
bool cgedit::capture()
{
if (keypressed() /*&& this->focus==1*/)
{
this->newkey = readkey();
this->ASCII = this->newkey & 0xff;
this->scancode = this->newkey >> 8;
clear_keybuf();
if(this->ASCII >= 32 && this->ASCII <= 126 /*&& !keypressed()*/ && this->focus==1 )
{
// add the new char, inserting or replacing as need be
if(this->insert || this->iter == this->edittext.end())
this->iter = this->edittext.insert(this->iter, this->ASCII);
else
this->edittext.replace(this->caret, 1, 1, this->ASCII);
// increment both the caret and the iterator
this->caret++;
this->iter++;
}
else
{
switch(this->scancode)
{
case KEY_DEL:
if(this->iter != this->edittext.end()) this->iter = this->edittext.erase(this->iter);
break;
case KEY_BACKSPACE:
if(this->iter != this->edittext.begin())
{
this->caret--;
this->iter--;
this->iter = this->edittext.erase(this->iter);
}
break;
case KEY_RIGHT:
if(this->iter != this->edittext.end()) this->caret++, this->iter++;
break;
case KEY_LEFT:
if(this->iter != this->edittext.begin()) this->caret--, this->iter--;
break;
case KEY_INSERT:
this->insert = !this->insert;
break;
case KEY_ESC:
this->focus = 0;
break;
default:
(1==2)?cout<<".\n":cout<<"!\n";
break;
}
}
return true;
}
else return false;
}
void cgedit::draw(BITMAP *dest)
{
if (this->visible)
{
hline(dest, this->posx+1, this->posy, this->posx+this->w+7, makecol(getr(this->style->back)-64,getg(this->style->back)-64,getb(this->style->back)-64));
hline(dest, this->posx+1, this->posy+alfont_text_height(this->czc)+8, this->posx+this->w+7, makecol(getr(this->style->back)+64,getg(this->style->back)+64,getb(this->style->back)+64));
vline(dest, this->posx, this->posy+1, this->posy+alfont_text_height(this->czc)+7, makecol(getr(this->style->back)-64,getg(this->style->back)-64,getb(this->style->back)-64));
vline(dest, this->posx+this->w+8, this->posy+1, this->posy+alfont_text_height(this->czc)+7, makecol(getr(this->style->back)+64,getg(this->style->back)+64,getb(this->style->back)+64));
floodfill(dest, this->posx+2, this->posy+2, this->style->back);
if (this->focus)
if (this->insert)
vline(dest, this->posx+this->caret*8+4, this->posy+3, this->posy+13, makecol(getr(this->style->back)-64,getg(this->style->back)-64,ge