1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2013 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 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 | |
---|
22 | void AIconButton::set_act_id(int id) |
---|
23 | { |
---|
24 | activate_id=id; |
---|
25 | } |
---|
26 | |
---|
27 | AIconSwitchButton::AIconSwitchButton(ivec2 pos, int id, int start_on, AWidgetList const &buttons) |
---|
28 | : AWidget(pos, id) |
---|
29 | { |
---|
30 | m_buttons = buttons; |
---|
31 | m_current = start_on < m_buttons.Count() ? start_on : -1; |
---|
32 | act = 0; |
---|
33 | for (int i = 0; i < m_buttons.Count(); ++i) |
---|
34 | m_buttons[i]->m_pos = m_pos; |
---|
35 | } |
---|
36 | |
---|
37 | ibox2 AIconSwitchButton::GetArea() |
---|
38 | { |
---|
39 | if (!m_buttons.Count()) |
---|
40 | return ibox2(m_pos, m_pos); |
---|
41 | |
---|
42 | ibox2 ret(10000, 10000, -10000, -10000); |
---|
43 | |
---|
44 | for (int i = 0; i < m_buttons.Count(); ++i) |
---|
45 | { |
---|
46 | ibox2 tmp = m_buttons[i]->GetArea(); |
---|
47 | ret.A = lol::min(ret.A, tmp.A); |
---|
48 | ret.B = lol::max(ret.B, tmp.B); |
---|
49 | } |
---|
50 | |
---|
51 | return ret; |
---|
52 | } |
---|
53 | |
---|
54 | AWidget *AIconSwitchButton::unlink(int id) |
---|
55 | { |
---|
56 | for (int i = 0; i < m_buttons.Count(); ++i) |
---|
57 | { |
---|
58 | if (m_buttons[i]->m_id == id) |
---|
59 | { |
---|
60 | AWidget *ret = m_buttons[i]; |
---|
61 | m_buttons.Remove(i); |
---|
62 | if (m_current >= m_buttons.Count()) |
---|
63 | m_current = 0; |
---|
64 | return ret; |
---|
65 | } |
---|
66 | AWidget *x = m_buttons[i]->unlink(id); |
---|
67 | if (x) |
---|
68 | return x; |
---|
69 | } |
---|
70 | return nullptr; |
---|
71 | } |
---|
72 | |
---|
73 | void AIconSwitchButton::handle_event(Event &ev, AImage *screen, InputManager *im) |
---|
74 | { |
---|
75 | if ((ev.type == EV_KEY && ev.key == 13) |
---|
76 | || (ev.type == EV_MOUSE_BUTTON && ev.mouse_button)) |
---|
77 | { |
---|
78 | ++m_current; |
---|
79 | if (m_current >= m_buttons.Count()) |
---|
80 | m_current = 0; |
---|
81 | m_buttons[m_current]->Draw(act, screen); |
---|
82 | m_buttons[m_current]->handle_event(ev, screen, im); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | void AIconButton::Draw(int active, AImage *screen) |
---|
87 | { |
---|
88 | ibox2 area = GetArea(); |
---|
89 | |
---|
90 | if (active != act && activate_id != -1 && active) |
---|
91 | wm->Push(Event(activate_id, NULL)); |
---|
92 | |
---|
93 | screen->PutImage(cache.img((up && !active) ? u : |
---|
94 | (up && active) ? ua : |
---|
95 | (!up && !active) ? d : da), area.A); |
---|
96 | |
---|
97 | if (act != active && active && activate_id != -1) |
---|
98 | wm->Push(Event(activate_id, NULL)); |
---|
99 | act = active; |
---|
100 | |
---|
101 | if (active && key[0]) |
---|
102 | { |
---|
103 | int g = 80; |
---|
104 | screen->Bar(ivec2(0, 0), ivec2(144, 20), 0); |
---|
105 | wm->font()->PutString(screen, ivec2::zero, symbol_str(key), |
---|
106 | color_table->Lookup(u8vec3(g >> 3, g >> 3, g >> 3))); |
---|
107 | } |
---|
108 | else if (!active && key[0]) |
---|
109 | { |
---|
110 | screen->Bar(ivec2(0, 0), ivec2(144, 20), 0); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | extern long S_BUTTON_PRESS_SND; |
---|
115 | extern int sfx_volume; |
---|
116 | |
---|
117 | void AIconButton::handle_event(Event &ev, AImage *screen, InputManager *im) |
---|
118 | { |
---|
119 | if ((ev.type == EV_KEY && ev.key == 13) |
---|
120 | || (ev.type == EV_MOUSE_BUTTON && ev.mouse_button)) |
---|
121 | { |
---|
122 | ibox2 area = GetArea(); /* FIXME: was this always unused? */ |
---|
123 | up = !up; |
---|
124 | Draw(act, screen); |
---|
125 | wm->Push(Event(m_id, (char *)this)); |
---|
126 | if (S_BUTTON_PRESS_SND) |
---|
127 | cache.sfx(S_BUTTON_PRESS_SND)->play(sfx_volume); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | ibox2 AIconButton::GetArea() |
---|
132 | { |
---|
133 | return ibox2(m_pos, m_pos + cache.img(u)->Size() - ivec2(1)); |
---|
134 | } |
---|
135 | |
---|
136 | AIconButton::AIconButton(ivec2 pos, int id, int Up, int down, int upa, int downa, int act_id, char const *help_key) |
---|
137 | : AWidget(pos, id) |
---|
138 | { |
---|
139 | if (help_key) |
---|
140 | { |
---|
141 | strncpy(key,help_key,15); |
---|
142 | key[15]=0; |
---|
143 | } |
---|
144 | else key[0]=0; |
---|
145 | |
---|
146 | up=1; |
---|
147 | u=Up; d=down; |
---|
148 | ua=upa; da=downa; |
---|
149 | activate_id=act_id; |
---|
150 | act = 0; |
---|
151 | } |
---|
152 | |
---|
153 | AIconSwitchButton::~AIconSwitchButton() |
---|
154 | { |
---|
155 | for (int i = 0; i < m_buttons.Count(); ++i) |
---|
156 | delete m_buttons[i]; |
---|
157 | m_buttons.Empty(); |
---|
158 | } |
---|
159 | |
---|