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

Last change on this file since 579 was 579, checked in by Sam Hocevar, 8 years ago

imlib: refactor the Filter and ColorFilter? classes.

File size: 3.7 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "cache.h"
18#include "gui.h"
19#include "dev.h"
20#include "loader2.h"
21
22void ico_button::set_act_id(int id)
23{
24  activate_id=id;
25}
26
27ico_switch_button::ico_switch_button(int X, int Y, int ID, int start_on, ifield *butts, ifield *Next)
28{
29  x=X; y=Y; id=ID;
30  next=Next;
31  blist=cur_but=butts;
32  act=0;
33  for (ifield *b=blist; b; b=b->next) { b->x=x; b->y=y; }
34  while (cur_but && start_on--) cur_but=cur_but->next;
35  if (!cur_but) cur_but=blist;
36}
37
38void ico_switch_button::area(int &x1, int &y1, int &x2, int &y2)
39{
40  x1=10000;
41  y1=10000;
42  x2=-10000;
43  y2=-10000;
44  int X1,Y1,X2,Y2;
45  for (ifield *b=blist; b; b=b->next)
46  {
47    b->area(X1,Y1,X2,Y2);
48    if (X1<x1) x1=X1;
49    if (Y1<y1) y1=Y1;
50    if (X2>x2) x2=X2;
51    if (Y2>y2) y2=Y2;
52  }
53  if (!blist) { x1=x2=x; y1=y2=y; }
54}
55
56ifield *ico_switch_button::unlink(int id)
57{
58  ifield *last=NULL;
59  for (ifield *b=blist; b; b=b->next)
60  {
61    if (b->id==id)
62    {
63      if (last) last->next=b->next;
64      else blist=b->next;
65      if (cur_but==b) cur_but=blist;
66      return b;
67    }
68    ifield *x=b->unlink(id);
69    if (x) return x;
70    last=b;
71  }
72  return NULL;
73}
74
75void ico_switch_button::handle_event(event &ev, image *screen, InputManager *im)
76{
77  if ((ev.type==EV_KEY && ev.key==13) || (ev.type==EV_MOUSE_BUTTON &&
78                                         ev.mouse_button))
79  {
80    cur_but=cur_but->next;
81    if (!cur_but) cur_but=blist;
82    cur_but->draw(act,screen);
83    cur_but->handle_event(ev,screen,im);
84  }
85
86}
87
88void ico_button::draw(int active, image *screen)
89{
90  int x1,y1,x2,y2;
91  area(x1,y1,x2,y2);
92
93  if (active!=act  && activate_id!=-1 && active)
94    wm->push_event(new event(activate_id,NULL));
95
96  if (up && !active)
97    cache.img(u)->put_image(screen,x1,y1);
98  else if (up && active)
99    cache.img(ua)->put_image(screen,x1,y1);
100  else if (!up && !active)
101    cache.img(d)->put_image(screen,x1,y1);
102  else cache.img(da)->put_image(screen,x1,y1);
103
104  if (act!=active && active && activate_id!=-1)
105    wm->push_event(new event(activate_id,NULL));
106  act=active;
107
108  if (active && key[0])
109  {
110    int g=80;
111    screen->bar(0,0,144,20,0);
112    wm->font()->put_string(screen,0,0,symbol_str(key),color_table->Lookup(g>>3,g>>3,g>>3));
113  } else if (!active && key[0])
114  {
115    screen->bar(0,0,144,20,0);
116  }
117
118}
119
120extern long S_BUTTON_PRESS_SND;
121extern int sfx_volume;
122
123void ico_button::handle_event(event &ev, image *screen, InputManager *im)
124{
125  if ((ev.type==EV_KEY && ev.key==13) || (ev.type==EV_MOUSE_BUTTON &&
126                                         ev.mouse_button))
127  {
128    int  x1,y1,x2,y2;
129    area(x1,y1,x2,y2);
130    up=!up;
131    draw(act,screen);
132    wm->push_event(new event(id,(char *)this));
133    if (S_BUTTON_PRESS_SND)
134      cache.sfx(S_BUTTON_PRESS_SND)->play(sfx_volume);
135  }
136}
137
138void ico_button::area(int &x1, int &y1, int &x2, int &y2)
139{
140  x1=x; y1=y;
141  x2=x+cache.img(u)->Size().x-1;
142  y2=y+cache.img(u)->Size().y-1;
143}
144
145ico_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)
146{
147  if (help_key)
148  {
149    strncpy(key,help_key,15);
150    key[15]=0;
151  }
152  else key[0]=0;
153
154  up=1;
155  x=X; y=Y; id=ID;
156  u=Up; d=down;
157  ua=upa; da=downa;
158  next=Next;
159  activate_id=act_id;
160  act = 0;
161}
162
163ico_switch_button::~ico_switch_button()
164{
165  while (blist)
166  {
167    ifield *i=blist;
168    blist=blist->next;
169    delete i;
170  }
171}
Note: See TracBrowser for help on using the repository browser.