[2] | 1 | #include "cache.hpp" |
---|
| 2 | #include "gui.hpp" |
---|
| 3 | #include "loader2.hpp" |
---|
| 4 | |
---|
| 5 | extern char *symbol_str(char *name); |
---|
| 6 | |
---|
| 7 | void ico_button::set_act_id(int id) |
---|
| 8 | { |
---|
| 9 | activate_id=id; |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | ico_switch_button::ico_switch_button(int X, int Y, int ID, int start_on, ifield *butts, ifield *Next) |
---|
| 13 | { |
---|
| 14 | x=X; y=Y; id=ID; |
---|
| 15 | next=Next; |
---|
| 16 | blist=cur_but=butts; |
---|
| 17 | act=0; |
---|
| 18 | for (ifield *b=blist;b;b=b->next) { b->x=x; b->y=y; } |
---|
| 19 | while (cur_but && start_on--) cur_but=cur_but->next; |
---|
| 20 | if (!cur_but) cur_but=blist; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | void ico_switch_button::area(int &x1, int &y1, int &x2, int &y2, window_manager *wm) |
---|
| 24 | { |
---|
| 25 | x1=10000; |
---|
| 26 | y1=10000; |
---|
| 27 | x2=-10000; |
---|
| 28 | y2=-10000; |
---|
| 29 | int X1,Y1,X2,Y2; |
---|
| 30 | for (ifield *b=blist;b;b=b->next) |
---|
| 31 | { |
---|
| 32 | b->area(X1,Y1,X2,Y2,wm); |
---|
| 33 | if (X1<x1) x1=X1; |
---|
| 34 | if (Y1<y1) y1=Y1; |
---|
| 35 | if (X2>x2) x2=X2; |
---|
| 36 | if (Y2>y2) y2=Y2; |
---|
| 37 | } |
---|
| 38 | if (!blist) { x1=x2=x; y1=y2=y; } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | ifield *ico_switch_button::unlink(int id) |
---|
| 42 | { |
---|
| 43 | ifield *last=NULL; |
---|
| 44 | for (ifield *b=blist;b;b=b->next) |
---|
| 45 | { |
---|
| 46 | if (b->id==id) |
---|
| 47 | { |
---|
| 48 | if (last) last->next=b->next; |
---|
| 49 | else blist=b->next; |
---|
| 50 | if (cur_but==b) cur_but=blist; |
---|
| 51 | return b; |
---|
| 52 | } |
---|
| 53 | ifield *x=b->unlink(id); |
---|
| 54 | if (x) return x; |
---|
| 55 | last=b; |
---|
| 56 | } |
---|
| 57 | return NULL; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void ico_switch_button::handle_event(event &ev, image *screen, window_manager *wm, input_manager *im) |
---|
| 61 | { |
---|
| 62 | if ((ev.type==EV_KEY && ev.key==13) || (ev.type==EV_MOUSE_BUTTON && |
---|
| 63 | ev.mouse_button)) |
---|
| 64 | { |
---|
| 65 | cur_but=cur_but->next; |
---|
| 66 | if (!cur_but) cur_but=blist; |
---|
| 67 | cur_but->draw(act,screen,wm); |
---|
| 68 | cur_but->handle_event(ev,screen,wm,im); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void ico_button::draw(int active, image *screen, window_manager *wm) |
---|
| 74 | { |
---|
| 75 | int x1,y1,x2,y2; |
---|
| 76 | area(x1,y1,x2,y2,wm); |
---|
| 77 | |
---|
| 78 | if (active!=act && activate_id!=-1 && active) |
---|
| 79 | wm->push_event(new event(activate_id,NULL)); |
---|
| 80 | |
---|
| 81 | if (up && !active) |
---|
| 82 | cash.img(u)->put_image(screen,x1,y1); |
---|
| 83 | else if (up && active) |
---|
| 84 | cash.img(ua)->put_image(screen,x1,y1); |
---|
| 85 | else if (!up && !active) |
---|
| 86 | cash.img(d)->put_image(screen,x1,y1); |
---|
| 87 | else cash.img(da)->put_image(screen,x1,y1); |
---|
| 88 | |
---|
| 89 | if (act!=active && active && activate_id!=-1) |
---|
| 90 | wm->push_event(new event(activate_id,NULL)); |
---|
| 91 | act=active; |
---|
| 92 | |
---|
| 93 | if (active && key[0]) |
---|
| 94 | { |
---|
| 95 | int g=80; |
---|
| 96 | screen->bar(0,0,144,20,0); |
---|
| 97 | wm->font()->put_string(screen,0,0,symbol_str(key),color_table->lookup_color(g>>3,g>>3,g>>3)); |
---|
| 98 | } else if (!active && key[0]) |
---|
| 99 | { |
---|
| 100 | screen->bar(0,0,144,20,0); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | extern long S_BUTTON_PRESS_SND; |
---|
| 106 | extern int sfx_volume; |
---|
| 107 | |
---|
| 108 | void ico_button::handle_event(event &ev, image *screen, window_manager *wm, input_manager *im) |
---|
| 109 | { |
---|
| 110 | if ((ev.type==EV_KEY && ev.key==13) || (ev.type==EV_MOUSE_BUTTON && |
---|
| 111 | ev.mouse_button)) |
---|
| 112 | { |
---|
| 113 | int x1,y1,x2,y2; |
---|
| 114 | area(x1,y1,x2,y2,wm); |
---|
| 115 | up=!up; |
---|
| 116 | draw(act,screen,wm); |
---|
| 117 | wm->push_event(new event(id,(char *)this)); |
---|
| 118 | if (S_BUTTON_PRESS_SND) |
---|
| 119 | cash.sfx(S_BUTTON_PRESS_SND)->play(sfx_volume); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void ico_button::area(int &x1, int &y1, int &x2, int &y2, window_manager *wm) |
---|
| 124 | { |
---|
| 125 | x1=x; y1=y; |
---|
| 126 | x2=x+cash.img(u)->width()-1; |
---|
| 127 | y2=y+cash.img(u)->height()-1; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | ico_button::ico_button(int X, int Y, int ID, int Up, int down, int upa, int downa, ifield *Next, int act_id, char *help_key) |
---|
| 131 | { |
---|
| 132 | if (help_key) |
---|
| 133 | { |
---|
| 134 | strncpy(key,help_key,15); |
---|
| 135 | key[15]=0; |
---|
| 136 | } |
---|
| 137 | else key[0]=0; |
---|
| 138 | |
---|
| 139 | up=1; |
---|
| 140 | x=X; y=Y; id=ID; |
---|
| 141 | u=Up; d=down; |
---|
| 142 | ua=upa; da=downa; |
---|
| 143 | next=Next; |
---|
| 144 | activate_id=act_id; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | ico_switch_button::~ico_switch_button() |
---|
| 148 | { |
---|
| 149 | while (blist) |
---|
| 150 | { |
---|
| 151 | ifield *i=blist; |
---|
| 152 | blist=blist->next; |
---|
| 153 | delete i; |
---|
| 154 | } |
---|
| 155 | } |
---|