source: abuse/trunk/src/gui.cpp @ 39

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