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

Last change on this file since 650 was 650, checked in by Sam Hocevar, 12 years ago

imlib: make some Image methods use vec2i.

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(new Event(activate_id, NULL));
95
96    screen->PutImage(cache.img((up && !active) ? u :
97                               (up && active) ? ua :
98                               (!up && !active) ? d : da), vec2i(x1, y1));
99
100    if (act != active && active && activate_id != -1)
101        wm->Push(new Event(activate_id, NULL));
102    act = active;
103
104    if (active && key[0])
105    {
106        int g=80;
107        screen->bar(0, 0, 144, 20, 0);
108        wm->font()->put_string(screen, 0, 0, symbol_str(key),
109                               color_table->Lookup(g>>3, g>>3, g>>3));
110    }
111    else if (!active && key[0])
112    {
113        screen->bar(0, 0, 144, 20, 0);
114    }
115}
116
117extern long S_BUTTON_PRESS_SND;
118extern int sfx_volume;
119
120void ico_button::handle_event(Event &ev, image *screen, InputManager *im)
121{
122  if ((ev.type==EV_KEY && ev.key==13) || (ev.type==EV_MOUSE_BUTTON &&
123                                         ev.mouse_button))
124  {
125    int  x1,y1,x2,y2;
126    area(x1,y1,x2,y2);
127    up=!up;
128    draw(act,screen);
129    wm->Push(new Event(id,(char *)this));
130    if (S_BUTTON_PRESS_SND)
131      cache.sfx(S_BUTTON_PRESS_SND)->play(sfx_volume);
132  }
133}
134
135void ico_button::area(int &x1, int &y1, int &x2, int &y2)
136{
137  x1=x; y1=y;
138  x2=x+cache.img(u)->Size().x-1;
139  y2=y+cache.img(u)->Size().y-1;
140}
141
142ico_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)
143{
144  if (help_key)
145  {
146    strncpy(key,help_key,15);
147    key[15]=0;
148  }
149  else key[0]=0;
150
151  up=1;
152  x=X; y=Y; id=ID;
153  u=Up; d=down;
154  ua=upa; da=downa;
155  next=Next;
156  activate_id=act_id;
157  act = 0;
158}
159
160ico_switch_button::~ico_switch_button()
161{
162  while (blist)
163  {
164    ifield *i=blist;
165    blist=blist->next;
166    delete i;
167  }
168}
Note: See TracBrowser for help on using the repository browser.