#ifndef BOB_GUI_H #define BOB_GUI_H #include "bob_gfx.h" #include "bob_event.h" #include "bob_wm.h" /* Rectangles are x, y, width, height; drag state is active, offset-x, offset-y. */ int bob_gui_window_hit(int x,int y,const int *rect) { return x>=rect[0]&&y>=rect[1]&&x=0;i--) { int id=order[i]; if(id>=0&&id=rect[0]+rect[2]||y!=rect[1])return 0; drag[0]=1;drag[1]=x-rect[0];drag[2]=y-rect[1];return 1; } int bob_gui_drag_update(int *rect,int *drag,int x,int y,int screen_width,int screen_height) { int max_x,max_y; if(!drag[0])return 0; max_x=screen_width-rect[2];max_y=screen_height-rect[3]; if(max_x<0)max_x=0;if(max_y<0)max_y=0; rect[0]=x-drag[1];rect[1]=y-drag[2]; if(rect[0]<0)rect[0]=0;if(rect[0]>max_x)rect[0]=max_x; if(rect[1]<0)rect[1]=0;if(rect[1]>max_y)rect[1]=max_y; return 1; } int bob_gui_drag_stop(int *drag) { int was_dragging=drag[0];drag[0]=0;return was_dragging; } /* Button state is hovered, pressed; returns one only for an inside release. */ int bob_gui_button_event(const int *rect,int *state,const int *event) { int inside=bob_gui_window_hit(event[2],event[3],rect); if(event[0]==BOB_EVENT_MOUSE_MOVE)state[0]=inside; else if(event[0]==BOB_EVENT_MOUSE_BUTTON_DOWN) { state[0]=inside;state[1]=inside&&((event[1]&BOB_MOUSE_BUTTON_LEFT)!=0); } else if(event[0]==BOB_EVENT_MOUSE_BUTTON_UP) { state[0]=inside; if(state[1]&&inside&&!(event[1]&BOB_MOUSE_BUTTON_LEFT)){state[1]=0;return 1;} state[1]=0; } return 0; } int bob_gui_button_draw(int x,int y,int width,int height,const char *label, int hovered,int pressed) { int color=pressed?14:(hovered?11:7); int label_length=0,label_start=0,label_x,label_width; if(width<3||height<3)return -1; while(label[label_length])label_length++; bob_gfx_fill_rect(x,y,width,height,' ',color); bob_gfx_line(x+1,y,x+width-2,y,pressed?'=':'-',color); bob_gfx_line(x+1,y+height-1,x+width-2,y+height-1,pressed?'=':'-',color); bob_gfx_line(x,y+1,x,y+height-2,'|',color); bob_gfx_line(x+width-1,y+1,x+width-1,y+height-2,'|',color); bob_gfx_pixel(x,y,'+',color);bob_gfx_pixel(x+width-1,y,'+',color); bob_gfx_pixel(x,y+height-1,'+',color);bob_gfx_pixel(x+width-1,y+height-1,'+',color); label_width=width-2; if(label_length>label_width){label_start=(label_length-label_width)/2;label_length=label_width;} label_x=x+1+(label_width-label_length)/2; for(int i=0;i80||rect[3]!=count+2|| rect[0]<0||rect[1]<0||rect[0]+rect[2]>80||rect[1]+rect[3]>25)return -1; bob_gfx_fill_rect(rect[0],rect[1],rect[2],rect[3],' ',7); bob_gfx_line(rect[0]+1,rect[1],rect[0]+rect[2]-2,rect[1],'-',7); bob_gfx_line(rect[0]+1,rect[1]+rect[3]-1,rect[0]+rect[2]-2,rect[1]+rect[3]-1,'-',7); bob_gfx_line(rect[0],rect[1]+1,rect[0],rect[1]+rect[3]-2,'|',7); bob_gfx_line(rect[0]+rect[2]-1,rect[1]+1,rect[0]+rect[2]-1,rect[1]+rect[3]-2,'|',7); bob_gfx_pixel(rect[0],rect[1],'+',7);bob_gfx_pixel(rect[0]+rect[2]-1,rect[1],'+',7); bob_gfx_pixel(rect[0],rect[1]+rect[3]-1,'+',7);bob_gfx_pixel(rect[0]+rect[2]-1,rect[1]+rect[3]-1,'+',7); for(int row=0;row':' ',color); while(lengthscreen_width)rect[0]=screen_width-width; if(rect[0]<0)rect[0]=0; if(rect[1]+height>screen_height)rect[1]=screen_height-height; if(rect[1]<0)rect[1]=0; return 0; } /* Returns the activated row, -1 for no action, or -2 when Escape closes it. */ int bob_gui_menu_event(const int *rect,int count,int *state,const int *event) { int row=-1; if(count<1||rect[3]!=count+2)return -1; if(event[2]>=rect[0]+1&&event[2]=rect[1]+1&&event[3]=0&&(event[1]&BOB_MOUSE_BUTTON_LEFT)?row:-1; } else if(event[0]==BOB_EVENT_MOUSE_BUTTON_UP) { int activated=row>=0&&state[1]==row&&!(event[1]&BOB_MOUSE_BUTTON_LEFT)?row:-1; state[0]=row;state[1]=-1;return activated; } else if(event[0]==BOB_EVENT_KEY_DOWN) { if(event[1]==256)state[0]=state[0]<=0?count-1:state[0]-1; else if(event[1]==257)state[0]=state[0<0||state[0]>=count-1?0:state[0]+1]; } else if(event[0]==BOB_EVENT_CHAR) { if(event[1]==27)return -2; if((event[1]==10||event[1]==13)&&state[0]>=0&&state[0]length)state[1]=length; } } else if(state[0]&&event[0]==BOB_EVENT_CHAR) { if(event[1]>=32&&event[1]<127&&lengthstate[1];i--)buffer[i]=buffer[i-1]; buffer[state[1]++]=event[1];buffer[++length]=0;changed=1; } else if((event[1]==8||event[1]==127)&&state[1]>0) { for(int i=state[1]-1;i0)state[1]--; else if(event[1]==259&&state[1]=state[2]+visible)state[2]=state[1]-visible+1; return changed; } int bob_gui_text_field_draw(const int *rect,const char *buffer,const int *state) { int visible=rect[2]-2; char shown[80]; int i=0; if(rect[2]<3||rect[2]>80||rect[3]<3)return -1; bob_gfx_fill_rect(rect[0],rect[1],rect[2],rect[3],' ',state[0]?15:8); bob_gfx_line(rect[0]+1,rect[1],rect[0]+rect[2]-2,rect[1],'-',state[0]?15:8); bob_gfx_line(rect[0]+1,rect[1]+rect[3]-1,rect[0]+rect[2]-2,rect[1]+rect[3]-1,'-',state[0]?15:8); bob_gfx_line(rect[0],rect[1]+1,rect[0],rect[1]+rect[3]-2,'|',state[0]?15:8); bob_gfx_line(rect[0]+rect[2]-1,rect[1]+1,rect[0]+rect[2]-1,rect[1]+rect[3]-2,'|',state[0]?15:8); for(i=0;i=0&&state[1]-state[2]=capacity-1)return 0; for(int i=*length;i>*cursor;i--)buffer[i]=buffer[i-1]; buffer[*cursor]=value;(*cursor)++;(*length)++;buffer[*length]=0; return 1; } static void bob_gui_text_area_position(const char *buffer,int cursor,int *line,int *column) { *line=0;*column=0; for(int i=0;i=state[2]+visible)state[2]=line-visible+1; } int bob_gui_text_area_reveal(const char *buffer,int *state,const int *rect) { bob_gui_text_area_scroll(buffer,state[1],state,rect); return 0; } int bob_gui_text_area_event(const int *rect,char *buffer,int *state,int capacity,const int *event) { int length=0,changed=0,line,column,start,end,next_start,next_end,target,visible,total_lines,max_scroll,old_scroll; while(length0&&state[2]>0)state[2]--; else if(event[1]<0&&state[2]0){start++;column--;} state[1]=start; } } else if(state[0]&&event[0]==BOB_EVENT_CHAR) { if(event[1]==8||event[1]==127) { if(state[1]>0) { for(int i=state[1]-1;i=32&&event[1]<127) { changed=bob_gui_text_area_insert(buffer,&length,&state[1],capacity,(char)event[1]); } } else if(state[0]&&event[0]==BOB_EVENT_KEY_DOWN) { bob_gui_text_area_position(buffer,state[1],&line,&column); if(event[1]==258&&state[1]>0)state[1]--; else if(event[1]==259&&state[1]0&&buffer[start-1]!='\n')start--;state[1]=start; } else if(event[1]==261) { end=state[1];while(end0&&buffer[start-1]!='\n')start--; end=state[1];while(end0) { next_end=start-1;next_start=next_end; while(next_start>0&&buffer[next_start-1]!='\n')next_start--; target=next_start+column;if(target>next_end)target=next_end;state[1]=target; } else if(event[1]==257&&endnext_end)target=next_end;state[1]=target; } } } bob_gui_text_area_scroll(buffer,state[1],state,rect); return changed; } int bob_gui_text_area_draw(const int *rect,const char *buffer,const int *state) { int width=rect[2]-2,height=rect[3]-2,length=0,line=0,column=0; int position=0,row,row_start,row_end,offset,shown_length,cursor_line,cursor_column; char shown[80]; if(rect[2]<3||rect[2]>80||rect[3]<3)return -1; while(buffer[length])length++; bob_gfx_fill_rect(rect[0],rect[1],rect[2],rect[3],' ',state[0]?15:8); bob_gfx_line(rect[0]+1,rect[1],rect[0]+rect[2]-2,rect[1],'-',state[0]?15:8); bob_gfx_line(rect[0]+1,rect[1]+rect[3]-1,rect[0]+rect[2]-2,rect[1]+rect[3]-1,'-',state[0]?15:8); bob_gfx_line(rect[0],rect[1]+1,rect[0],rect[1]+rect[3]-2,'|',state[0]?15:8); bob_gfx_line(rect[0]+rect[2]-1,rect[1]+1,rect[0]+rect[2]-1,rect[1]+rect[3]-2,'|',state[0]?15:8); bob_gui_text_area_position(buffer,state[1],&cursor_line,&cursor_column); while(position=width)offset=cursor_column-width+1; shown_length=0; while(shown_length=offset&&cursor_column-offset=length)break; position=row_end+1;line++; } return 0; } /* Reusable character-cell window frame and content area. */ int bob_gui_window(int x,int y,int width,int height,const char *title, const char *body,const char *message,int focused) { int edge=focused?14:8; int title_color=focused?15:7; if(width<4||height<4)return -1; bob_gfx_fill_rect(x,y,width,height,' ',edge); bob_gfx_line(x+1,y,x+width-2,y,'-',edge); bob_gfx_line(x+1,y+height-1,x+width-2,y+height-1,'-',edge); bob_gfx_line(x,y+1,x,y+height-2,'|',edge); bob_gfx_line(x+width-1,y+1,x+width-1,y+height-2,'|',edge); bob_gfx_pixel(x,y,'+',edge);bob_gfx_pixel(x+width-1,y,'+',edge); bob_gfx_pixel(x,y+height-1,'+',edge);bob_gfx_pixel(x+width-1,y+height-1,'+',edge); bob_gfx_fill_rect(x+1,y+1,width-2,1,' ',focused?4:8); bob_gfx_text(x+2,y+1,title,title_color); bob_gfx_text(x+2,y+3,body,edge); if(message)bob_gfx_text(x+2,y+height-2,message,focused?10:7); return 0; } #endif