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

Last change on this file since 512 was 512, checked in by Sam Hocevar, 11 years ago

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

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