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

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

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

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