1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*
interface.cc
(c) Richard Thrippleton
Licensing terms are in the 'LICENSE' file
If that file is not included with this source then permission is not given to use this source in any way whatsoever.
*/
#include <SDL.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "graphic.h"
#include "error.h"
#include "protocol.h"
#include "calc.h"
#include "interface.h"
void interface::init()
{
cons=NULL;
for(int i=0;i<8;i++)
mesg[i]=NULL;
edit[0]='\0';
inp=false;
ent=false;
cspr=NULL;
}
void interface::setup()
{
sbox tot; //Total screen area
tot=graphic::dimension();
viewb.x=tot.x;
viewb.y=tot.y;
viewb.w=tot.h;
viewb.h=tot.h;
panelb.x=viewb.x+viewb.w;
panelb.y=tot.y;
panelb.w=(tot.x+tot.w)-panelb.x;
panelb.h=400;
consb.x=panelb.x+5;
consb.y=panelb.y+200;
consb.w=(panelb.x+panelb.w)-consb.x-5;
consb.h=panelb.h-214;
radarb.x=panelb.x+5;
radarb.y=panelb.y+25;
radarb.w=150;
radarb.h=radarb.w;
barsb.x=panelb.x+panelb.w-38;
barsb.y=panelb.y+75;
barsb.w=32;
barsb.h=100;
editb.x=consb.x;
editb.y=consb.y+consb.h+1;
editb.w=consb.w;
editb.h=10;
mesgb.x=viewb.x+7;
mesgb.y=viewb.y+viewb.h-70;
mesgb.w=viewb.w-20;
mesgb.h=viewb.y+viewb.h-mesgb.y-8;
if(cons)
delete[] cons;
cons=new char[(consb.w/7)*(consb.h/7)];
cons[0]='\0';
SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
mtmo=0;
}
void interface::poll()
{
SDL_Event evnt; //Event to be polled
int n; //Unused area for the argument for SDL_GetKeyState
lkey=-1;
lasc=0;
SDL_PumpEvents();
keys=SDL_GetKeyState(&n);
while(lkey==-1 && SDL_PollEvent(&evnt))
{
if(evnt.type==SDL_KEYDOWN)
{
lkey=evnt.key.keysym.sym;
lasc=evnt.key.keysym.unicode;
if(inp)
lineedit();
}
if(evnt.type==SDL_QUIT)
throw error("User requested quit");
}
}
void interface::printtocons(char* fmt,...)
{
va_list fmts; //To help parsing the vargs
inp=false;
edit[0]='\0';
cspr=NULL;
va_start(fmts,fmt);
vsprintf(cons,fmt,fmts);
va_end(fmts);
}
void interface::spritetocons(graphic* spr)
{
inp=false;
edit[0]='\0';
cspr=spr;
}
void interface::printtomesg(char* fmt,...)
{
va_list fmts; //To help parsing the vargs
char txt[1024]; //Temporary text buffer
if(fmt)
{
mtmo=100;
if(mesg[0])
delete mesg[0];
mesg[0]=mesg[1];
mesg[1]=mesg[2];
mesg[2]=mesg[3];
mesg[3]=mesg[4];
mesg[4]=mesg[5];
mesg[5]=mesg[6];
mesg[6]=mesg[7];
mesg[7]=NULL;
va_start(fmts,fmt);
vsprintf(txt,fmt,fmts);
va_end(fmts);
mesg[7]=new char[strlen(txt)+1];
strcpy(mesg[7],txt);
if((int)strlen(txt)>(mesgb.w/6))
printtomesg("%s",txt+(mesgb.w/6));
}
else
{
for(int i=0;i<8;i++)
{
if(mesg[i])
{
delete[] mesg[i];
mesg[i]=NULL;
}
}
}
}
void interface::render()
{
char* p1;
char* p2; //Pointers for parsing text into console
int y; //Y co-ordinate for writing to console
char line[256]; //Line of console for scratchpad operations
int elen; //String length of edit buffer
int cwid; //Console width (in characters)
graphic::clip(&mesgb);
if(mtmo>0)
{
mtmo--;
for(int i=0,y=mesgb.y;i<8;i++,y+=7)
if(mesg[i])
graphic::string(mesg[i],mesgb.x,y,false);
}
graphic::clip(&consb);
if(cspr)
cspr->draw(consb.x+consb.w/2,consb.y+consb.h/2,0,1,0,false);
cwid=consb.w/6;
y=consb.y;
p1=cons;
p2=cons;
while(*p2)
{
if(*p2=='\0' || *p2=='\n' || p2-p1==cwid || p2-p1>=255)
{
memcpy(line,p1,p2-p1+1);
line[p2-p1+1]='\0';
graphic::string(line,consb.x,y,true);
y+=7;
p1=p2+1;
if(*p2=='\0')
break;
}
p2++;
}
graphic::clip(&editb);
if(inp)
graphic::box(&editb,graphic::RED);
else
graphic::box(&editb,graphic::BLACK);
elen=strlen(edit);
if(pwd)
{
for(int i=0;i<elen;i++)
line[i]='*';
line[elen]='\0';
graphic::string(line,editb.x+2,editb.y+2,false);
}
else
{
if(elen>cwid)
graphic::string(edit+(elen-cwid),editb.x+2,editb.y+2,false);
else
graphic::string(edit,editb.x+2,editb.y+2,false);
}
}
sbox interface::consb;
bool interface::getline(char* put,bool hide)
{
pwd=hide;
if(!inp)
{
inp=true;
ent=false;
edit[0]='\0';
}
if(ent)
{
strcpy(put,edit);
inp=false;
ent=false;
return true;
}
else
return false;
}
sbox interface::viewb,interface::radarb,interface::barsb,interface::panelb;
bool interface::inp;
int interface::lkey;
unsigned char interface::lasc;
unsigned char* interface::keys;
void interface::lineedit() //Function to handle line-editing
{
if(lkey==SDLK_RETURN)
{
ent=true;
return;
}
if(lkey==SDLK_BACKSPACE)
{
for(int i=1;i<65;i++)
{
if(edit[i]=='\0')
{
edit[i-1]='\0';
break;
}
}
}
if(lasc>=32 && lasc<127)
{
for(int i=0;i<64;i++)
{
if(edit[i]=='\0')
{
edit[i]=lasc;
edit[i+1]='\0';
break;
}
}
}
}
sbox interface::mesgb;
sbox interface::editb;
char* interface::cons;
char* interface::mesg[8];
char interface::edit[65];
bool interface::pwd;
bool interface::ent;
graphic* interface::cspr;
int interface::mtmo;
|