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

Last change on this file since 668 was 668, checked in by Sam Hocevar, 9 years ago

imlib: make JCFont use vec2i classes.

File size: 3.8 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  m_pos = vec2i(X, Y); id=ID;
30  next=Next;
31  blist=cur_but=butts;
32  act=0;
33  for (ifield *b=blist; b; b=b->next)
34      b->m_pos = m_pos;
35  while (cur_but && start_on--) cur_but=cur_but->next;
36  if (!cur_but) cur_but=blist;
37}
38
39void ico_switch_button::area(int &x1, int &y1, int &x2, int &y2)
40{
41  x1=10000;
42  y1=10000;
43  x2=-10000;
44  y2=-10000;
45  int X1,Y1,X2,Y2;
46  for (ifield *b=blist; b; b=b->next)
47  {
48    b->area(X1,Y1,X2,Y2);
49    if (X1<x1) x1=X1;
50    if (Y1<y1) y1=Y1;
51    if (X2>x2) x2=X2;
52    if (Y2>y2) y2=Y2;
53  }
54  if (!blist) { x1=x2=m_pos.x; y1=y2=m_pos.y; }
55}
56
57ifield *ico_switch_button::unlink(int id)
58{
59  ifield *last=NULL;
60  for (ifield *b=blist; b; b=b->next)
61  {
62    if (b->id==id)
63    {
64      if (last) last->next=b->next;
65      else blist=b->next;
66      if (cur_but==b) cur_but=blist;
67      return b;
68    }
69    ifield *x=b->unlink(id);
70    if (x) return x;
71    last=b;
72  }
73  return NULL;
74}
75
76void ico_switch_button::handle_event(Event &ev, image *screen, InputManager *im)
77{
78  if ((ev.type==EV_KEY && ev.key==13) || (ev.type==EV_MOUSE_BUTTON &&
79                                         ev.mouse_button))
80  {
81    cur_but=cur_but->next;
82    if (!cur_but) cur_but=blist;
83    cur_but->draw(act,screen);
84    cur_but->handle_event(ev,screen,im);
85  }
86
87}
88
89void ico_button::draw(int active, image *screen)
90{
91    int x1, y1, x2, y2;
92    area(x1, y1, x2, y2);
93
94    if (active != act && activate_id != -1 && active)
95        wm->Push(new Event(activate_id, NULL));
96
97    screen->PutImage(cache.img((up && !active) ? u :
98                               (up && active) ? ua :
99                               (!up && !active) ? d : da), vec2i(x1, y1));
100
101    if (act != active && active && activate_id != -1)
102        wm->Push(new Event(activate_id, NULL));
103    act = active;
104
105    if (active && key[0])
106    {
107        int g=80;
108        screen->Bar(vec2i(0, 0), vec2i(144, 20), 0);
109        wm->font()->PutString(screen, vec2i(0), symbol_str(key),
110                              color_table->Lookup(g>>3, g>>3, g>>3));
111    }
112    else if (!active && key[0])
113    {
114        screen->Bar(vec2i(0, 0), vec2i(144, 20), 0);
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(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=m_pos.x; y1=m_pos.y;
139  x2=m_pos.x+cache.img(u)->Size().x-1;
140  y2=m_pos.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  m_pos = vec2i(X, 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.