Element search interface by pressing 'e'

First search is for element names, second search looks at the
description, pressing enter will give you the first result the the left
button selection, left click and right click can also be used to select.
This commit is contained in:
Simon Robertshaw 2012-05-24 19:38:58 +01:00
parent a1b7b02f81
commit 5a117c2d27
3 changed files with 206 additions and 0 deletions

View File

@ -287,6 +287,8 @@ void draw_svf_ui(pixel *vid_buf, int alternate);
void error_ui(pixel *vid_buf, int err, char *txt);
void element_search_ui(pixel *vid_buf, int * sl, int * sr);
void info_ui(pixel *vid_buf, char *top, char *txt);
void copytext_ui(pixel *vid_buf, char *top, char *txt, char *copytxt);

View File

@ -984,6 +984,206 @@ void error_ui(pixel *vid_buf, int err, char *txt)
}
}
typedef struct int_pair
{
int first, second;
} int_pair;
int int_pair_cmp (const void * a, const void * b)
{
int_pair *ap = a;
int_pair *bp = a;
return ( ap->first - bp->first );
}
void element_search_ui(pixel *vid_buf, int * slp, int * srp)
{
int windowHeight = 300, windowWidth = 240;
int x0 = (XRES-windowWidth)/2, y0 = (YRES-windowHeight)/2, b = 1, bq, mx, my;
int toolx = 0, tooly = 0, i, xoff, yoff, c, found;
char tempCompare[512];
char tempString[512];
int_pair tempInts[PT_NUM];
ui_edit ed;
int selectedl = -1;
int selectedr = -1;
int firstResult = -1, hover = -1;
ed.x = x0+12;
ed.y = y0+30;
ed.w = windowWidth - 20;
ed.nx = 1;
ed.def = "[element name]";
ed.focus = 1;
ed.hide = 0;
ed.cursor = 0;
ed.multiline = 0;
ed.str[0] = 0;
while (!sdl_poll())
{
bq = b;
b = mouse_get_state(&mx, &my);
clearrect(vid_buf, x0-2, y0-2, windowWidth+4, windowHeight+4);
drawrect(vid_buf, x0, y0, windowWidth, windowHeight, 192, 192, 192, 255);
drawtext(vid_buf, x0+8, y0+8, "Element Search", 255, 255, 255, 255);
drawrect(vid_buf, ed.x-4, ed.y-5, ed.w+4, 16, 192, 192, 192, 255);
ui_edit_draw(vid_buf, &ed);
ui_edit_process(mx, my, b, &ed);
drawrect(vid_buf, ed.x-4, (ed.y-5)+20, ed.w+4, windowHeight-((ed.y-5)), 192, 192, 192, 255);
xoff = (ed.x-4)+6;
yoff = ((ed.y-5)+20)+4;
toolx = 0;
tooly = 0;
drawtext(vid_buf, xoff+toolx+4, yoff+tooly+3, "Matches:", 255, 255, 255, 180);
draw_line(vid_buf, xoff+toolx+2, yoff+tooly+14, xoff+toolx+5+(ed.w-16), yoff+tooly+14, 180, 180, 180, XRES+BARSIZE);
tooly += 17;
//Covert input to lower case
c = 0;
while (ed.str[c]) { tempString[c] = tolower(ed.str[c]); c++; } tempString[c] = 0;
firstResult = -1;
hover = -1;
for(i = 0; i < PT_NUM; i++)
{
c = 0;
while (ptypes[i].name[c]) { tempCompare[c] = tolower(ptypes[i].name[c]); c++; } tempCompare[c] = 0;
if(strstr(tempCompare, tempString)!=0)
{
if(firstResult==-1)
firstResult = i;
toolx += draw_tool_xy(vid_buf, toolx+xoff, tooly+yoff, i, ptypes[i].pcolors)+5;
if (!bq && mx>=xoff+toolx-32 && mx<xoff+toolx && my>=yoff+tooly && my<yoff+tooly+15)
{
drawrect(vid_buf, xoff+toolx-32, yoff+tooly-1, 29, 17, 255, 55, 55, 255);
hover = i;
}
else if (i==selectedl || i==*slp)
{
drawrect(vid_buf, xoff+toolx-32, yoff+tooly-1, 29, 17, 255, 55, 55, 255);
}
else if (i==selectedr || i==*srp)
{
drawrect(vid_buf, xoff+toolx-32, yoff+tooly-1, 29, 17, 55, 55, 255, 255);
}
if(toolx > ed.w-4)
{
tooly += 18;
toolx = 0;
}
if(tooly>windowHeight-((ed.y-5)+20))
break;;
}
}
if(toolx>0)
{
toolx = 0;
tooly += 18;
}
if(tooly<windowHeight-((ed.y-5)+20))
{
drawtext(vid_buf, xoff+toolx+4, yoff+tooly+3, "Related:", 255, 255, 255, 180);
draw_line(vid_buf, xoff+toolx+2, yoff+tooly+14, xoff+toolx+5+(ed.w-16), yoff+tooly+14, 180, 180, 180, XRES+BARSIZE);
tooly += 17;
found = 0;
for(i = 0; i < PT_NUM; i++)
{
c = 0;
while (ptypes[i].descs[c]) { tempCompare[c] = tolower(ptypes[i].descs[c]); c++; } tempCompare[c] = 0;
if(strstr(tempCompare, tempString)!=0)
{
tempInts[found].first = strstr(tempCompare, tempString);
tempInts[found++].second = i;
}
}
qsort(tempInts, found, sizeof(int_pair), int_pair_cmp);
for(i = 0; i < found; i++)
{
if(firstResult==-1)
firstResult = tempInts[i].second;
toolx += draw_tool_xy(vid_buf, toolx+xoff, tooly+yoff, tempInts[i].second, ptypes[tempInts[i].second].pcolors)+5;
if (!bq && mx>=xoff+toolx-32 && mx<xoff+toolx && my>=yoff+tooly && my<yoff+tooly+15)
{
drawrect(vid_buf, xoff+toolx-32, yoff+tooly-1, 29, 17, 255, 55, 55, 255);
hover = tempInts[i].second;
}
else if (tempInts[i].second==selectedl || tempInts[i].second==*slp)
{
drawrect(vid_buf, xoff+toolx-32, yoff+tooly-1, 29, 17, 255, 55, 55, 255);
}
else if (tempInts[i].second==selectedr || tempInts[i].second==*srp)
{
drawrect(vid_buf, xoff+toolx-32, yoff+tooly-1, 29, 17, 55, 55, 255, 255);
}
if(toolx > ed.w-4)
{
tooly += 18;
toolx = 0;
}
if(tooly>windowHeight-((ed.y-5)+18))
break;
}
}
if(b==1 && hover!=-1)
{
selectedl = hover;
break;
}
if(b==4 && hover!=-1)
{
selectedr = hover;
break;
}
drawtext(vid_buf, x0+5, y0+windowHeight-12, "Dismiss", 255, 255, 255, 255);
drawrect(vid_buf, x0, y0+windowHeight-16, windowWidth, 16, 192, 192, 192, 255);
#ifdef OGLR
clearScreen(1.0f);
#endif
sdl_blit(0, 0, (XRES+BARSIZE), YRES+MENUSIZE, vid_buf, (XRES+BARSIZE));
if (b && !bq && mx>=x0 && mx<x0+windowWidth && my>=y0+windowHeight-16 && my<=y0+windowHeight)
break;
if (sdl_key==SDLK_RETURN)
{
if(selectedl==-1)
selectedl = firstResult;
break;
}
if (sdl_key==SDLK_ESCAPE)
{
selectedl = -1;
selectedr = -1;
break;
}
}
if(selectedl!=-1)
*slp = selectedl;
if(selectedr!=-1)
*srp = selectedr;
while (!sdl_poll())
{
b = mouse_get_state(&mx, &my);
if (!b)
break;
}
}
char *input_ui(pixel *vid_buf, char *title, char *prompt, char *text, char *shadow)
{

View File

@ -1389,6 +1389,10 @@ int main(int argc, char *argv[])
it = 50;
save_mode = 1;
}
if(sdl_key=='e')
{
element_search_ui(vid_buf, &sl, &sr);
}
//TODO: Superseded by new display mode switching, need some keyboard shortcuts
if (sdl_key=='1')
{