1 | package CFClient::UI; |
1 | package CFClient::UI; |
2 | |
2 | |
|
|
3 | use utf8; |
3 | use strict; |
4 | use strict; |
4 | |
5 | |
5 | use Scalar::Util (); |
6 | use Scalar::Util (); |
6 | use List::Util (); |
7 | use List::Util (); |
7 | |
8 | |
8 | use CFClient; |
9 | use CFClient; |
9 | |
10 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
12 | |
12 | our $ROOT; |
13 | our $ROOT; |
|
|
14 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
|
|
16 | |
|
|
17 | our %WIDGET; # all widgets, weak-referenced |
|
|
18 | |
|
|
19 | sub check_tooltip { |
|
|
20 | if (!$GRAB) { |
|
|
21 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
22 | if (length $widget->{tooltip}) { |
|
|
23 | |
|
|
24 | if ($TOOLTIP->{owner} != $widget) { |
|
|
25 | $TOOLTIP->{owner} = $widget; |
|
|
26 | |
|
|
27 | my $tip = $widget->{tooltip}; |
|
|
28 | |
|
|
29 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
30 | |
|
|
31 | $TOOLTIP->set_tooltip_from ($widget); |
|
|
32 | $TOOLTIP->show; |
|
|
33 | |
|
|
34 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
|
|
35 | |
|
|
36 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) |
|
|
37 | if $x + $TOOLTIP->{w} > $::WIDTH; |
|
|
38 | |
|
|
39 | $TOOLTIP->move ($x, $y); |
|
|
40 | $TOOLTIP->check_size; |
|
|
41 | $TOOLTIP->update; |
|
|
42 | } |
|
|
43 | |
|
|
44 | return; |
|
|
45 | } |
|
|
46 | } |
|
|
47 | } |
|
|
48 | |
|
|
49 | $TOOLTIP->hide; |
|
|
50 | delete $TOOLTIP->{owner}; |
|
|
51 | } |
14 | |
52 | |
15 | # class methods for events |
53 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
54 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
55 | $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) |
|
|
56 | if $FOCUS; |
18 | } |
57 | } |
19 | |
58 | |
20 | sub feed_sdl_key_up_event { |
59 | sub feed_sdl_key_up_event { |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
60 | $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) |
|
|
61 | if $FOCUS; |
22 | } |
62 | } |
23 | |
63 | |
24 | sub feed_sdl_button_down_event { |
64 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
65 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
66 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
67 | |
28 | if (!$BUTTON_STATE) { |
68 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
69 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
70 | |
31 | $GRAB = $widget; |
71 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
72 | $GRAB->update if $GRAB; |
33 | } |
|
|
34 | |
73 | |
|
|
74 | check_tooltip; |
|
|
75 | } |
|
|
76 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
77 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
78 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
79 | if ($GRAB) { |
|
|
80 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
81 | $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y); |
|
|
82 | } |
38 | } |
83 | } |
39 | |
84 | |
40 | sub feed_sdl_button_up_event { |
85 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
86 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
87 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
88 | |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
89 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
90 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
91 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
92 | |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
93 | if ($GRAB) { |
|
|
94 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
95 | $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y); |
|
|
96 | } |
49 | |
97 | |
50 | if (!$BUTTON_STATE) { |
98 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
99 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
100 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
101 | $GRAB->update if $GRAB; |
|
|
102 | |
|
|
103 | check_tooltip; |
54 | } |
104 | } |
55 | } |
105 | } |
56 | |
106 | |
57 | sub feed_sdl_motion_event { |
107 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
108 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
109 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
110 | |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
111 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
112 | |
63 | if ($widget != $HOVER) { |
113 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
114 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
115 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
116 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
117 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
|
|
69 | |
118 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
119 | check_tooltip; |
|
|
120 | } |
|
|
121 | |
|
|
122 | if ($HOVER) { |
|
|
123 | ($x, $y) = $HOVER->coord2local ($x, $y); |
|
|
124 | $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); |
|
|
125 | } |
71 | } |
126 | } |
72 | |
127 | |
73 | # convert position array to integers |
128 | # convert position array to integers |
74 | sub harmonize { |
129 | sub harmonize { |
75 | my ($vals) = @_; |
130 | my ($vals) = @_; |
76 | |
131 | |
77 | my $rem = 0; |
132 | my $rem = 0; |
78 | |
133 | |
79 | for ($vals) { |
134 | for (@$vals) { |
80 | my $i = int $_ + $rem; |
135 | my $i = int $_ + $rem; |
81 | $rem += $_ - $i; |
136 | $rem += $_ - $i; |
82 | $_ = $i; |
137 | $_ = $i; |
83 | } |
138 | } |
84 | } |
139 | } |
85 | |
140 | |
|
|
141 | # call when resolution changes etc. |
|
|
142 | sub rescale_widgets { |
|
|
143 | my ($sx, $sy) = @_; |
|
|
144 | |
|
|
145 | for my $widget (values %WIDGET) { |
|
|
146 | if ($widget->{toplevel}) { |
|
|
147 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
|
|
148 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
|
|
149 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
|
|
150 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
|
|
151 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
|
|
152 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
|
|
153 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
|
|
154 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
|
|
155 | } |
|
|
156 | |
|
|
157 | $widget->reconfigure; |
|
|
158 | } |
|
|
159 | } |
|
|
160 | |
86 | ############################################################################# |
161 | ############################################################################# |
87 | |
162 | |
88 | package CFClient::UI::Base; |
163 | package CFClient::UI::Base; |
89 | |
164 | |
90 | use strict; |
165 | use strict; |
91 | |
166 | |
92 | use SDL::OpenGL; |
167 | use CFClient::OpenGL; |
93 | |
168 | |
94 | sub new { |
169 | sub new { |
95 | my $class = shift; |
170 | my $class = shift; |
96 | |
171 | |
97 | my $self = bless { |
172 | my $self = bless { |
98 | x => 0, |
173 | x => 0, |
99 | y => 0, |
174 | y => 0, |
100 | z => 0, |
175 | z => 0, |
101 | w => -1, |
176 | can_events => 1, |
102 | h => -1, |
|
|
103 | @_ |
177 | @_ |
104 | }, $class; |
178 | }, $class; |
105 | |
179 | |
106 | for (keys %$self) { |
180 | for (keys %$self) { |
107 | if (/^connect_(.*)$/) { |
181 | if (/^connect_(.*)$/) { |
108 | $self->connect ($1 => delete $self->{$_}); |
182 | $self->connect ($1 => delete $self->{$_}); |
109 | } |
183 | } |
110 | } |
184 | } |
111 | |
185 | |
|
|
186 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
187 | |
112 | $self |
188 | $self |
|
|
189 | } |
|
|
190 | |
|
|
191 | sub destroy { |
|
|
192 | my ($self) = @_; |
|
|
193 | |
|
|
194 | $self->hide; |
|
|
195 | %$self = (); |
|
|
196 | } |
|
|
197 | |
|
|
198 | sub show { |
|
|
199 | my ($self) = @_; |
|
|
200 | |
|
|
201 | return if $self->{parent}; |
|
|
202 | |
|
|
203 | $CFClient::UI::ROOT->add ($self); |
|
|
204 | } |
|
|
205 | |
|
|
206 | sub hide { |
|
|
207 | my ($self) = @_; |
|
|
208 | |
|
|
209 | undef $GRAB if $GRAB == $self; |
|
|
210 | undef $HOVER if $HOVER == $self; |
|
|
211 | |
|
|
212 | $self->{parent}->remove ($self) |
|
|
213 | if $self->{parent}; |
113 | } |
214 | } |
114 | |
215 | |
115 | sub move { |
216 | sub move { |
116 | my ($self, $x, $y, $z) = @_; |
217 | my ($self, $x, $y, $z) = @_; |
|
|
218 | |
117 | $self->{x} = $x; |
219 | $self->{x} = int $x; |
118 | $self->{y} = $y; |
220 | $self->{y} = int $y; |
119 | $self->{z} = $z if defined $z; |
221 | $self->{z} = $z if defined $z; |
120 | } |
|
|
121 | |
222 | |
122 | sub needs_redraw { |
223 | $self->update; |
123 | 0 |
224 | } |
|
|
225 | |
|
|
226 | sub set_size { |
|
|
227 | my ($self, $w, $h) = @_; |
|
|
228 | |
|
|
229 | $self->{user_w} = $w; |
|
|
230 | $self->{user_h} = $h; |
|
|
231 | |
|
|
232 | $self->check_size; |
124 | } |
233 | } |
125 | |
234 | |
126 | sub size_request { |
235 | sub size_request { |
127 | require Carp; |
236 | require Carp; |
128 | Carp::confess "size_request is abtract"; |
237 | Carp::confess "size_request is abstract"; |
129 | } |
238 | } |
130 | |
239 | |
131 | sub _size_allocate { |
240 | sub configure { |
132 | my ($self, $x, $y, $w, $h) = @_; |
241 | my ($self, $x, $y, $w, $h) = @_; |
133 | |
242 | |
|
|
243 | if ($self->{aspect}) { |
|
|
244 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
245 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
246 | |
|
|
247 | # use alignment to adjust x, y |
|
|
248 | |
|
|
249 | $x += int +($w - $w2) * 0.5; |
|
|
250 | $y += int +($h - $h2) * 0.5; |
|
|
251 | |
|
|
252 | ($w, $h) = ($w2, $h2); |
|
|
253 | } |
|
|
254 | |
|
|
255 | if ($self->{x} != $x || $self->{y} != $y) { |
134 | $self->{x} = $x; |
256 | $self->{x} = $x; |
135 | $self->{y} = $y; |
257 | $self->{y} = $y; |
|
|
258 | $self->update; |
|
|
259 | } |
136 | |
260 | |
137 | return unless $self->{w} != $w || $self->{h} != $h; |
261 | if ($self->{w} != $w || $self->{h} != $h) { |
138 | |
262 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; |
139 | $self->{w} = $w; |
|
|
140 | $self->{h} = $h; |
|
|
141 | |
|
|
142 | 1 |
263 | } |
143 | } |
264 | } |
144 | |
265 | |
145 | sub size_allocate { |
266 | sub size_allocate { |
146 | my ($self, $x, $y, $w, $h) = @_; |
267 | # nothing to be done |
147 | |
|
|
148 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
149 | } |
268 | } |
150 | |
269 | |
151 | # return top left coordinates |
270 | sub reconfigure { |
152 | sub _topleft { |
271 | my ($self) = @_; |
|
|
272 | |
|
|
273 | $self->check_size (1); |
|
|
274 | $self->update; |
|
|
275 | } |
|
|
276 | |
|
|
277 | sub children { |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub set_max_size { |
153 | my ($self, $x, $y) = @_; |
281 | my ($self, $w, $h) = @_; |
154 | |
282 | |
155 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
283 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
284 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
156 | } |
285 | } |
157 | |
286 | |
158 | # translate global coordinates to local coordinate system |
287 | # translate global coordinates to local coordinate system |
159 | sub coord2local { |
288 | sub coord2local { |
160 | my ($self, $x, $y) = @_; |
289 | my ($self, $x, $y) = @_; |
161 | |
290 | |
162 | my ($X, $Y) = $self->_topleft; |
291 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
163 | ($x - $X, $y - $Y) |
|
|
164 | } |
292 | } |
165 | |
293 | |
166 | # translate local coordinates to global coordinate system |
294 | # translate local coordinates to global coordinate system |
167 | sub coord2global { |
295 | sub coord2global { |
168 | my ($self, $x, $y) = @_; |
296 | my ($self, $x, $y) = @_; |
169 | |
297 | |
170 | my ($X, $Y) = $self->_topleft; |
298 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
171 | ($x + $X, $y + $Y) |
|
|
172 | } |
299 | } |
173 | |
300 | |
174 | sub focus_in { |
301 | sub focus_in { |
175 | my ($self) = @_; |
302 | my ($self) = @_; |
176 | |
303 | |
177 | return if $FOCUS == $self; |
304 | return if $FOCUS == $self; |
178 | return unless $self->{can_focus}; |
305 | return unless $self->{can_focus}; |
179 | |
306 | |
180 | my $focus = $FOCUS; $FOCUS = $self; |
307 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
308 | |
|
|
309 | $self->emit (focus_in => $focus); |
|
|
310 | |
181 | $focus->update if $focus; |
311 | $focus->update if $focus; |
182 | $FOCUS->update; |
312 | $FOCUS->update; |
183 | } |
313 | } |
184 | |
314 | |
185 | sub focus_out { |
315 | sub focus_out { |
186 | my ($self) = @_; |
316 | my ($self) = @_; |
187 | |
317 | |
188 | return unless $FOCUS == $self; |
318 | return unless $FOCUS == $self; |
189 | |
319 | |
190 | my $focus = $FOCUS; undef $FOCUS; |
320 | my $focus = $FOCUS; undef $FOCUS; |
|
|
321 | |
|
|
322 | $self->emit (focus_out => $focus); |
|
|
323 | |
191 | $focus->update if $focus; #? |
324 | $focus->update if $focus; #? |
192 | } |
325 | } |
193 | |
326 | |
194 | sub mouse_motion { } |
327 | sub mouse_motion { } |
195 | sub button_up { } |
328 | sub button_up { } |
… | |
… | |
219 | glPopMatrix; |
352 | glPopMatrix; |
220 | |
353 | |
221 | if ($self == $HOVER && $self->{can_hover}) { |
354 | if ($self == $HOVER && $self->{can_hover}) { |
222 | my ($x, $y) = @$self{qw(x y)}; |
355 | my ($x, $y) = @$self{qw(x y)}; |
223 | |
356 | |
224 | glColor 0, 0, 1, 0.2; |
357 | glColor 1, 0.8, 0.5, 0.2; |
225 | glEnable GL_BLEND; |
358 | glEnable GL_BLEND; |
226 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
359 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
227 | glBegin GL_QUADS; |
360 | glBegin GL_QUADS; |
228 | glVertex $x , $y; |
361 | glVertex $x , $y; |
229 | glVertex $x + $self->{w}, $y; |
362 | glVertex $x + $self->{w}, $y; |
230 | glVertex $x + $self->{w}, $y + $self->{h}; |
363 | glVertex $x + $self->{w}, $y + $self->{h}; |
231 | glVertex $x , $y + $self->{h}; |
364 | glVertex $x , $y + $self->{h}; |
232 | glEnd; |
365 | glEnd; |
233 | glDisable GL_BLEND; |
366 | glDisable GL_BLEND; |
234 | } |
367 | } |
|
|
368 | |
|
|
369 | if ($ENV{PCLIENT_DEBUG}) { |
|
|
370 | glPushMatrix; |
|
|
371 | glColor 1, 1, 0, 1; |
|
|
372 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
|
|
373 | glBegin GL_LINE_LOOP; |
|
|
374 | glVertex 0 , 0; |
|
|
375 | glVertex $self->{w}, 0; |
|
|
376 | glVertex $self->{w}, $self->{h}; |
|
|
377 | glVertex 0 , $self->{h}; |
|
|
378 | glEnd; |
|
|
379 | glPopMatrix; |
|
|
380 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
|
|
381 | } |
235 | } |
382 | } |
236 | |
383 | |
237 | sub _draw { |
384 | sub _draw { |
238 | my ($self) = @_; |
385 | my ($self) = @_; |
239 | |
386 | |
240 | warn "no draw defined for $self\n"; |
387 | warn "no draw defined for $self\n"; |
241 | } |
388 | } |
242 | |
389 | |
243 | sub find_widget { |
390 | sub find_widget { |
244 | my ($self, $x, $y) = @_; |
391 | my ($self, $x, $y) = @_; |
|
|
392 | |
|
|
393 | return () unless $self->{can_events}; |
245 | |
394 | |
246 | return $self |
395 | return $self |
247 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
396 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
248 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
397 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
249 | |
398 | |
… | |
… | |
252 | |
401 | |
253 | sub set_parent { |
402 | sub set_parent { |
254 | my ($self, $parent) = @_; |
403 | my ($self, $parent) = @_; |
255 | |
404 | |
256 | Scalar::Util::weaken ($self->{parent} = $parent); |
405 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
406 | |
|
|
407 | # TODO: req_w _does_change after ->reconfigure |
|
|
408 | $self->check_size |
|
|
409 | unless exists $self->{req_w}; |
|
|
410 | } |
|
|
411 | |
|
|
412 | sub check_size { |
|
|
413 | my ($self, $forced) = @_; |
|
|
414 | |
|
|
415 | $self->{force_alloc} = 1 if $forced; |
|
|
416 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
257 | } |
417 | } |
258 | |
418 | |
259 | sub update { |
419 | sub update { |
260 | my ($self) = @_; |
420 | my ($self) = @_; |
261 | |
421 | |
… | |
… | |
264 | } |
424 | } |
265 | |
425 | |
266 | sub connect { |
426 | sub connect { |
267 | my ($self, $signal, $cb) = @_; |
427 | my ($self, $signal, $cb) = @_; |
268 | |
428 | |
269 | push @{ $self->{cb}{$signal} }, $cb; |
429 | push @{ $self->{signal_cb}{$signal} }, $cb; |
270 | } |
430 | } |
271 | |
431 | |
272 | sub emit { |
432 | sub emit { |
273 | my ($self, $signal, @args) = @_; |
433 | my ($self, $signal, @args) = @_; |
274 | |
434 | |
275 | $_->($self, @args) |
435 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
276 | for @{$self->{cb}{$signal} || []}; |
|
|
277 | } |
436 | } |
278 | |
437 | |
279 | sub DESTROY { |
438 | sub DESTROY { |
280 | my ($self) = @_; |
439 | my ($self) = @_; |
281 | |
440 | |
|
|
441 | delete $WIDGET{$self+0}; |
282 | #$self->deactivate; |
442 | #$self->deactivate; |
283 | } |
443 | } |
284 | |
444 | |
285 | ############################################################################# |
445 | ############################################################################# |
286 | |
446 | |
287 | package CFClient::UI::DrawBG; |
447 | package CFClient::UI::DrawBG; |
288 | |
448 | |
289 | our @ISA = CFClient::UI::Base::; |
449 | our @ISA = CFClient::UI::Base::; |
290 | |
450 | |
291 | use strict; |
451 | use strict; |
292 | use SDL::OpenGL; |
452 | use CFClient::OpenGL; |
293 | |
453 | |
294 | sub new { |
454 | sub new { |
295 | my $class = shift; |
455 | my $class = shift; |
296 | |
456 | |
297 | # range [value, low, high, page] |
457 | # range [value, low, high, page] |
… | |
… | |
326 | |
486 | |
327 | package CFClient::UI::Empty; |
487 | package CFClient::UI::Empty; |
328 | |
488 | |
329 | our @ISA = CFClient::UI::Base::; |
489 | our @ISA = CFClient::UI::Base::; |
330 | |
490 | |
|
|
491 | sub new { |
|
|
492 | my ($class, %arg) = @_; |
|
|
493 | $class->SUPER::new (can_events => 0, %arg); |
|
|
494 | } |
|
|
495 | |
331 | sub size_request { |
496 | sub size_request { |
332 | (0, 0) |
497 | (0, 0) |
333 | } |
498 | } |
334 | |
499 | |
335 | sub draw { } |
500 | sub draw { } |
… | |
… | |
343 | sub new { |
508 | sub new { |
344 | my ($class, %arg) = @_; |
509 | my ($class, %arg) = @_; |
345 | |
510 | |
346 | my $children = delete $arg{children} || []; |
511 | my $children = delete $arg{children} || []; |
347 | |
512 | |
348 | my $self = $class->SUPER::new (children => [], %arg); |
513 | my $self = $class->SUPER::new ( |
|
|
514 | children => [], |
|
|
515 | can_events => 0, |
|
|
516 | %arg, |
|
|
517 | ); |
349 | $self->add ($_) for @$children; |
518 | $self->add ($_) for @$children; |
350 | |
519 | |
351 | $self |
520 | $self |
352 | } |
521 | } |
353 | |
522 | |
354 | sub add { |
523 | sub add { |
355 | my ($self, $child) = @_; |
524 | my ($self, @widgets) = @_; |
356 | |
525 | |
357 | $child->set_parent ($self); |
526 | $_->set_parent ($self) |
|
|
527 | for @widgets; |
358 | |
528 | |
359 | use sort 'stable'; |
529 | use sort 'stable'; |
360 | |
530 | |
361 | $self->{children} = [ |
531 | $self->{children} = [ |
362 | sort { $a->{z} <=> $b->{z} } |
532 | sort { $a->{z} <=> $b->{z} } |
363 | @{$self->{children}}, $child |
533 | @{$self->{children}}, @widgets |
364 | ]; |
534 | ]; |
365 | |
535 | |
366 | $self->{w} = $self->{h} = -1; |
536 | $self->check_size (1); |
367 | $self->update; |
537 | $self->update; |
368 | } |
538 | } |
369 | |
539 | |
|
|
540 | sub children { |
|
|
541 | @{ $_[0]{children} } |
|
|
542 | } |
|
|
543 | |
370 | sub remove { |
544 | sub remove { |
371 | my ($self, $widget) = @_; |
545 | my ($self, $child) = @_; |
372 | |
546 | |
|
|
547 | delete $child->{parent}; |
|
|
548 | $child->hide; |
|
|
549 | |
373 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
550 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
374 | |
551 | |
375 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
552 | $self->check_size; |
|
|
553 | $self->update; |
|
|
554 | } |
|
|
555 | |
|
|
556 | sub clear { |
|
|
557 | my ($self) = @_; |
|
|
558 | |
|
|
559 | my $children = delete $self->{children}; |
|
|
560 | $self->{children} = []; |
|
|
561 | |
|
|
562 | for (@$children) { |
|
|
563 | delete $_->{parent}; |
|
|
564 | $_->hide; |
|
|
565 | } |
|
|
566 | |
|
|
567 | $self->check_size; |
|
|
568 | $self->update; |
376 | } |
569 | } |
377 | |
570 | |
378 | sub find_widget { |
571 | sub find_widget { |
379 | my ($self, $x, $y) = @_; |
572 | my ($self, $x, $y) = @_; |
380 | |
573 | |
… | |
… | |
410 | |
603 | |
411 | $class->SUPER::new (children => [$child], %arg) |
604 | $class->SUPER::new (children => [$child], %arg) |
412 | } |
605 | } |
413 | |
606 | |
414 | sub add { |
607 | sub add { |
415 | my ($self, $widget) = @_; |
608 | my ($self, $child) = @_; |
416 | |
609 | |
417 | $self->{children} = []; |
610 | $self->{children} = []; |
418 | |
611 | |
419 | $self->SUPER::add ($widget); |
612 | $self->SUPER::add ($child); |
420 | } |
613 | } |
421 | |
614 | |
422 | sub remove { |
615 | sub remove { |
423 | my ($self, $widget) = @_; |
616 | my ($self, $widget) = @_; |
424 | |
617 | |
… | |
… | |
433 | sub size_request { |
626 | sub size_request { |
434 | $_[0]{children}[0]->size_request |
627 | $_[0]{children}[0]->size_request |
435 | } |
628 | } |
436 | |
629 | |
437 | sub size_allocate { |
630 | sub size_allocate { |
438 | my ($self, $x, $y, $w, $h) = @_; |
631 | my ($self, $w, $h) = @_; |
439 | |
632 | |
440 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
441 | |
|
|
442 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
633 | $self->{children}[0]->configure (0, 0, $w, $h); |
443 | } |
634 | } |
444 | |
635 | |
445 | ############################################################################# |
636 | ############################################################################# |
446 | |
637 | |
447 | package CFClient::UI::Window; |
638 | package CFClient::UI::Window; |
448 | |
639 | |
449 | our @ISA = CFClient::UI::Bin::; |
640 | our @ISA = CFClient::UI::Bin::; |
450 | |
641 | |
451 | use SDL::OpenGL; |
642 | use CFClient::OpenGL; |
452 | |
643 | |
453 | sub new { |
644 | sub new { |
454 | my ($class, %arg) = @_; |
645 | my ($class, %arg) = @_; |
455 | |
646 | |
456 | my $self = $class->SUPER::new (%arg); |
647 | my $self = $class->SUPER::new (%arg); |
457 | } |
648 | } |
458 | |
649 | |
459 | sub update { |
650 | sub update { |
460 | my ($self) = @_; |
651 | my ($self) = @_; |
461 | |
652 | |
462 | # we want to do this delayed... |
653 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
463 | $self->render_chld; |
|
|
464 | $self->SUPER::update; |
654 | $self->SUPER::update; |
465 | } |
655 | } |
466 | |
656 | |
|
|
657 | sub size_allocate { |
|
|
658 | my ($self, $w, $h) = @_; |
|
|
659 | |
|
|
660 | $self->SUPER::size_allocate ($w, $h); |
|
|
661 | $self->update; |
|
|
662 | } |
|
|
663 | |
|
|
664 | sub _render { |
|
|
665 | $_[0]{children}[0]->draw; |
|
|
666 | } |
|
|
667 | |
467 | sub render_chld { |
668 | sub render_child { |
468 | my ($self) = @_; |
669 | my ($self) = @_; |
469 | |
670 | |
470 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
671 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
471 | glClearColor 0, 0, 0, 1; |
672 | glClearColor 0, 0, 0, 0; |
472 | glClear GL_COLOR_BUFFER_BIT; |
673 | glClear GL_COLOR_BUFFER_BIT; |
473 | $self->child->draw; |
674 | |
|
|
675 | $self->_render; |
|
|
676 | # glColorMask 1, 1, 1, 0; |
|
|
677 | # glEnable GL_BLEND; |
|
|
678 | # glBlendFunc GL_SRC_ALPHA, GL_ZERO; |
|
|
679 | # glRasterPos 0, 0; |
|
|
680 | # glCopyPixels 0, 0, $self->{w}, $self->{h}; |
|
|
681 | # glDisable GL_BLEND; |
|
|
682 | # glColorMask 1, 1, 1, 1; |
474 | }; |
683 | }; |
475 | } |
|
|
476 | |
|
|
477 | sub size_allocate { |
|
|
478 | my ($self, $x, $y, $w, $h) = @_; |
|
|
479 | |
|
|
480 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
481 | |
|
|
482 | $self->child->size_allocate (0, 0, $w, $h); |
|
|
483 | |
|
|
484 | $self->render_chld; |
|
|
485 | } |
684 | } |
486 | |
685 | |
487 | sub _draw { |
686 | sub _draw { |
488 | my ($self) = @_; |
687 | my ($self) = @_; |
489 | |
688 | |
490 | my ($w, $h) = ($self->w, $self->h); |
689 | my ($w, $h) = ($self->w, $self->h); |
491 | |
690 | |
492 | my $tex = $self->{texture} |
691 | my $tex = $self->{texture} |
493 | or return; |
692 | or return; |
494 | |
693 | |
495 | glEnable GL_BLEND; |
|
|
496 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
497 | glEnable GL_TEXTURE_2D; |
694 | glEnable GL_TEXTURE_2D; |
498 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
695 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
696 | glColor 0, 0, 0, 1; |
499 | |
697 | |
500 | $tex->draw_quad (0, 0, $w, $h); |
698 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
501 | |
699 | |
502 | glDisable GL_BLEND; |
|
|
503 | glDisable GL_TEXTURE_2D; |
700 | glDisable GL_TEXTURE_2D; |
504 | } |
701 | } |
505 | |
702 | |
506 | ############################################################################# |
703 | ############################################################################# |
507 | |
704 | |
508 | package CFClient::UI::Frame; |
705 | package CFClient::UI::ViewPort; |
509 | |
706 | |
510 | our @ISA = CFClient::UI::Bin::; |
707 | our @ISA = CFClient::UI::Window::; |
511 | |
|
|
512 | use SDL::OpenGL; |
|
|
513 | |
708 | |
514 | sub size_request { |
709 | sub size_request { |
515 | my ($self) = @_; |
710 | my ($self) = @_; |
516 | my $chld = $self->child |
|
|
517 | or return (0, 0); |
|
|
518 | |
711 | |
519 | $chld->move (2, 2); |
712 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
|
|
713 | $self->child->configure (0, 0, @$self{qw(child_w child_h)}); |
520 | |
714 | |
521 | map { $_ + 4 } $chld->size_request; |
715 | @$self{qw(child_w child_h)} |
522 | } |
716 | } |
523 | |
717 | |
524 | sub size_allocate { |
718 | sub size_allocate { |
525 | my ($self, $x, $y, $w, $h) = @_; |
719 | my ($self, $w, $h) = @_; |
|
|
720 | |
|
|
721 | $self->update; |
|
|
722 | } |
|
|
723 | |
|
|
724 | sub set_offset { |
|
|
725 | my ($self, $x, $y) = @_; |
|
|
726 | |
|
|
727 | $self->{view_x} = int $x; |
|
|
728 | $self->{view_y} = int $y; |
|
|
729 | |
|
|
730 | $self->update; |
|
|
731 | } |
|
|
732 | |
|
|
733 | # hmm, this does not work for topleft of $self... but we should not ask for that |
|
|
734 | sub coord2local { |
|
|
735 | my ($self, $x, $y) = @_; |
|
|
736 | |
|
|
737 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
738 | } |
|
|
739 | |
|
|
740 | sub coord2global { |
|
|
741 | my ($self, $x, $y) = @_; |
|
|
742 | |
|
|
743 | $x = List::Util::min $self->{w}, $x - $self->{view_x}; |
|
|
744 | $y = List::Util::min $self->{h}, $y - $self->{view_y}; |
|
|
745 | |
|
|
746 | $self->SUPER::coord2global ($x, $y) |
|
|
747 | } |
|
|
748 | |
|
|
749 | sub find_widget { |
|
|
750 | my ($self, $x, $y) = @_; |
|
|
751 | |
|
|
752 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
753 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
|
|
754 | ) { |
|
|
755 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
756 | } else { |
|
|
757 | $self->CFClient::UI::Base::find_widget ($x, $y) |
526 | |
758 | } |
527 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
528 | |
|
|
529 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
|
|
530 | } |
759 | } |
531 | |
760 | |
532 | sub _draw { |
761 | sub _render { |
533 | my ($self) = @_; |
762 | my ($self) = @_; |
534 | |
763 | |
535 | my $chld = $self->child; |
764 | CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
536 | |
765 | |
537 | my ($w, $h) = $chld->size_request; |
766 | $self->SUPER::_render; |
538 | |
|
|
539 | glBegin GL_QUADS; |
|
|
540 | glColor 0, 0, 0; |
|
|
541 | glVertex 0 , 0; |
|
|
542 | glVertex 0 , $h + 4; |
|
|
543 | glVertex $w + 4 , $h + 4; |
|
|
544 | glVertex $w + 4 , 0; |
|
|
545 | glEnd; |
|
|
546 | |
|
|
547 | $chld->draw; |
|
|
548 | } |
767 | } |
549 | |
768 | |
550 | ############################################################################# |
769 | ############################################################################# |
551 | |
770 | |
552 | package CFClient::UI::FancyFrame; |
771 | package CFClient::UI::ScrolledWindow; |
553 | |
772 | |
554 | our @ISA = CFClient::UI::Bin::; |
773 | our @ISA = CFClient::UI::HBox::; |
555 | |
|
|
556 | use SDL::OpenGL; |
|
|
557 | |
|
|
558 | my @tex = |
|
|
559 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
560 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
561 | |
774 | |
562 | sub new { |
775 | sub new { |
563 | my $class = shift; |
776 | my $class = shift; |
564 | |
777 | |
|
|
778 | my $self; |
|
|
779 | |
|
|
780 | my $slider = new CFClient::UI::Slider |
|
|
781 | vertical => 1, |
|
|
782 | range => [0, 0, 1, 0.01], # HACK fix |
|
|
783 | connect_changed => sub { |
|
|
784 | $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); |
|
|
785 | }, |
|
|
786 | ; |
|
|
787 | |
|
|
788 | $self = $class->SUPER::new ( |
|
|
789 | vp => (new CFClient::UI::ViewPort), |
|
|
790 | slider => $slider, |
|
|
791 | @_, |
|
|
792 | ); |
|
|
793 | |
|
|
794 | $self->{vp}->add ($self->{scrolled}); |
|
|
795 | $self->add ($self->{vp}); |
|
|
796 | $self->add ($self->{slider}); |
|
|
797 | |
|
|
798 | $self |
|
|
799 | } |
|
|
800 | |
|
|
801 | #TODO# update range on size_allocate depending on child |
|
|
802 | # update viewport offset on scroll |
|
|
803 | |
|
|
804 | ############################################################################# |
|
|
805 | |
|
|
806 | package CFClient::UI::Frame; |
|
|
807 | |
|
|
808 | our @ISA = CFClient::UI::Bin::; |
|
|
809 | |
|
|
810 | use CFClient::OpenGL; |
|
|
811 | |
|
|
812 | sub new { |
|
|
813 | my $class = shift; |
|
|
814 | |
|
|
815 | $class->SUPER::new ( |
|
|
816 | bg => undef, |
|
|
817 | @_, |
|
|
818 | ) |
|
|
819 | } |
|
|
820 | |
|
|
821 | sub _draw { |
|
|
822 | my ($self) = @_; |
|
|
823 | |
|
|
824 | if ($self->{bg}) { |
|
|
825 | my ($w, $h) = @$self{qw(w h)}; |
|
|
826 | |
|
|
827 | glEnable GL_BLEND; |
|
|
828 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
829 | glColor @{ $self->{bg} }; |
|
|
830 | |
|
|
831 | glBegin GL_QUADS; |
|
|
832 | glVertex 0 , 0; |
|
|
833 | glVertex 0 , $h; |
|
|
834 | glVertex $w, $h; |
|
|
835 | glVertex $w, 0; |
|
|
836 | glEnd; |
|
|
837 | |
|
|
838 | glDisable GL_BLEND; |
|
|
839 | } |
|
|
840 | |
|
|
841 | $self->SUPER::_draw; |
|
|
842 | } |
|
|
843 | |
|
|
844 | ############################################################################# |
|
|
845 | |
|
|
846 | package CFClient::UI::FancyFrame; |
|
|
847 | |
|
|
848 | our @ISA = CFClient::UI::Bin::; |
|
|
849 | |
|
|
850 | use CFClient::OpenGL; |
|
|
851 | |
|
|
852 | my @tex = |
|
|
853 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
854 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
855 | |
|
|
856 | sub new { |
|
|
857 | my $class = shift; |
|
|
858 | |
565 | # TODO: user_x, user_y, overwrite moveto? |
859 | # TODO: user_x, user_y, overwrite moveto? |
566 | |
860 | |
567 | $class->SUPER::new ( |
861 | my $self = $class->SUPER::new ( |
568 | bg => [1, 1, 1, 1], |
862 | bg => [1, 1, 1, 1], |
569 | border_bg => [1, 1, 1, 1], |
863 | border_bg => [1, 1, 1, 1], |
570 | border => $::FONTSIZE * 0.8, |
864 | border => 0.6, |
|
|
865 | toplevel => 1, |
|
|
866 | can_events => 1, |
571 | @_ |
867 | @_ |
|
|
868 | ); |
|
|
869 | |
|
|
870 | $self->{title} &&= new CFClient::UI::Label |
|
|
871 | align => 0, |
|
|
872 | valign => 1, |
|
|
873 | text => $self->{title}, |
|
|
874 | fontsize => $self->{border}; |
|
|
875 | |
|
|
876 | $self |
|
|
877 | } |
|
|
878 | |
|
|
879 | sub border { |
|
|
880 | int $_[0]{border} * $::FONTSIZE |
|
|
881 | } |
|
|
882 | |
|
|
883 | sub size_request { |
|
|
884 | my ($self) = @_; |
|
|
885 | |
|
|
886 | my ($w, $h) = $self->SUPER::size_request; |
|
|
887 | |
|
|
888 | ( |
|
|
889 | $w + $self->border * 2, |
|
|
890 | $h + $self->border * 2, |
572 | ) |
891 | ) |
573 | } |
892 | } |
574 | |
893 | |
575 | sub size_request { |
|
|
576 | my ($self) = @_; |
|
|
577 | |
|
|
578 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
579 | |
|
|
580 | my ($w, $h) = $self->SUPER::size_request; |
|
|
581 | |
|
|
582 | ( |
|
|
583 | $w + $self->{border} * 2, |
|
|
584 | $h + $self->{border} * 2, |
|
|
585 | ) |
|
|
586 | } |
|
|
587 | |
|
|
588 | sub size_allocate { |
894 | sub size_allocate { |
589 | my ($self, $x, $y, $w, $h) = @_; |
895 | my ($self, $w, $h) = @_; |
590 | |
896 | |
591 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
592 | |
|
|
593 | $h -= List::Util::max 0, $self->{border} * 2; |
897 | $h -= List::Util::max 0, $self->border * 2; |
594 | $w -= List::Util::max 0, $self->{border} * 2; |
898 | $w -= List::Util::max 0, $self->border * 2; |
595 | |
899 | |
|
|
900 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
|
|
901 | if $self->{title}; |
|
|
902 | |
596 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
903 | $self->child->configure ($self->border, $self->border, $w, $h); |
597 | } |
904 | } |
598 | |
905 | |
599 | sub button_down { |
906 | sub button_down { |
600 | my ($self, $ev, $x, $y) = @_; |
907 | my ($self, $ev, $x, $y) = @_; |
601 | |
908 | |
602 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
909 | my ($w, $h) = @$self{qw(w h)}; |
603 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
910 | my $border = $self->border; |
604 | |
911 | |
|
|
912 | my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); |
|
|
913 | my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); |
|
|
914 | |
|
|
915 | if ($lr & $td) { |
|
|
916 | my ($wx, $wy) = ($self->{x}, $self->{y}); |
605 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
917 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
606 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
918 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
919 | |
|
|
920 | my $mx = $x < $border; |
|
|
921 | my $my = $y < $border; |
607 | |
922 | |
608 | $self->{motion} = sub { |
923 | $self->{motion} = sub { |
609 | my ($ev, $x, $y) = @_; |
924 | my ($ev, $x, $y) = @_; |
610 | |
925 | |
611 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
926 | my $dx = $ev->{x} - $ox; |
|
|
927 | my $dy = $ev->{y} - $oy; |
612 | |
928 | |
613 | $self->{user_w} = $bw + $x - $ox; |
929 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
614 | $self->{user_h} = $bh + $y - $oy; |
930 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
615 | $self->update; |
931 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
|
|
932 | $self->check_size; |
616 | }; |
933 | }; |
617 | |
934 | |
618 | } elsif ($x >= 0 && $x < $self->{w} |
935 | } elsif ($lr ^ $td) { |
619 | && $y >= 0 && $y < $self->{border}) { |
|
|
620 | |
|
|
621 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
936 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
622 | my ($bx, $by) = ($self->{x}, $self->{y}); |
937 | my ($bx, $by) = ($self->{x}, $self->{y}); |
623 | |
938 | |
624 | $self->{motion} = sub { |
939 | $self->{motion} = sub { |
625 | my ($ev, $x, $y) = @_; |
940 | my ($ev, $x, $y) = @_; |
626 | |
941 | |
627 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
942 | ($x, $y) = ($ev->{x}, $ev->{y}); |
628 | |
943 | |
629 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
944 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
630 | $self->update; |
945 | $self->update; |
631 | }; |
946 | }; |
632 | } |
947 | } |
… | |
… | |
648 | my ($self) = @_; |
963 | my ($self) = @_; |
649 | |
964 | |
650 | my ($w, $h ) = ($self->{w}, $self->{h}); |
965 | my ($w, $h ) = ($self->{w}, $self->{h}); |
651 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
966 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
652 | |
967 | |
653 | glEnable GL_BLEND; |
|
|
654 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
655 | glEnable GL_TEXTURE_2D; |
968 | glEnable GL_TEXTURE_2D; |
656 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
969 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
657 | |
970 | |
|
|
971 | my $border = $self->border; |
|
|
972 | |
658 | glColor @{ $self->{border_bg} }; |
973 | glColor @{ $self->{border_bg} }; |
659 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
974 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
660 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
975 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
661 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
976 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
662 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
977 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
663 | |
978 | |
|
|
979 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
664 | my $bg = $tex[0]; |
980 | my $bg = $tex[0]; |
665 | |
981 | |
666 | # TODO: repeat texture not scale |
982 | # TODO: repeat texture not scale |
667 | my $rep_x = $cw / $bg->{w}; |
983 | my $rep_x = $cw / $bg->{w}; |
668 | my $rep_y = $ch / $bg->{h}; |
984 | my $rep_y = $ch / $bg->{h}; |
669 | |
985 | |
670 | glColor @{ $self->{bg} }; |
986 | glColor @{ $self->{bg} }; |
671 | |
987 | |
672 | $bg->{s} = $rep_x; |
988 | $bg->{s} = $rep_x; |
673 | $bg->{t} = $rep_y; |
989 | $bg->{t} = $rep_y; |
674 | $bg->{wrap_mode} = 1; |
990 | $bg->{wrap_mode} = 1; |
675 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
991 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
|
|
992 | } |
676 | |
993 | |
677 | glDisable GL_TEXTURE_2D; |
994 | glDisable GL_TEXTURE_2D; |
678 | glDisable GL_BLEND; |
995 | |
|
|
996 | $self->{title}->draw if $self->{title}; |
679 | |
997 | |
680 | $self->child->draw; |
998 | $self->child->draw; |
681 | } |
999 | } |
682 | |
1000 | |
683 | ############################################################################# |
1001 | ############################################################################# |
… | |
… | |
686 | |
1004 | |
687 | our @ISA = CFClient::UI::Base::; |
1005 | our @ISA = CFClient::UI::Base::; |
688 | |
1006 | |
689 | use List::Util qw(max sum); |
1007 | use List::Util qw(max sum); |
690 | |
1008 | |
691 | use SDL::OpenGL; |
1009 | use CFClient::OpenGL; |
692 | |
1010 | |
693 | sub new { |
1011 | sub new { |
694 | my $class = shift; |
1012 | my $class = shift; |
695 | |
1013 | |
696 | $class->SUPER::new ( |
1014 | $class->SUPER::new ( |
… | |
… | |
703 | my ($self, $x, $y, $child) = @_; |
1021 | my ($self, $x, $y, $child) = @_; |
704 | |
1022 | |
705 | $child->set_parent ($self); |
1023 | $child->set_parent ($self); |
706 | $self->{children}[$y][$x] = $child; |
1024 | $self->{children}[$y][$x] = $child; |
707 | |
1025 | |
708 | $self->{w} = $self->{h} = -1; |
1026 | $child->check_size; |
709 | $self->update; |
1027 | } |
|
|
1028 | |
|
|
1029 | sub children { |
|
|
1030 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
710 | } |
1031 | } |
711 | |
1032 | |
712 | # TODO: move to container class maybe? send childs a signal on removal? |
1033 | # TODO: move to container class maybe? send childs a signal on removal? |
713 | sub clear { |
1034 | sub clear { |
714 | my ($self) = @_; |
1035 | my ($self) = @_; |
715 | |
1036 | |
|
|
1037 | my @children = $self->children; |
716 | delete $self->{children}; |
1038 | delete $self->{children}; |
|
|
1039 | |
|
|
1040 | for (@children) { |
|
|
1041 | delete $_->{parent}; |
|
|
1042 | $_->hide; |
|
|
1043 | } |
|
|
1044 | |
717 | $self->update; |
1045 | $self->update; |
718 | } |
1046 | } |
719 | |
1047 | |
720 | sub get_wh { |
1048 | sub get_wh { |
721 | my ($self) = @_; |
1049 | my ($self) = @_; |
… | |
… | |
727 | or next; |
1055 | or next; |
728 | |
1056 | |
729 | for my $x (0 .. $#$row) { |
1057 | for my $x (0 .. $#$row) { |
730 | my $widget = $row->[$x] |
1058 | my $widget = $row->[$x] |
731 | or next; |
1059 | or next; |
732 | my ($w, $h) = $widget->size_request; |
1060 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
733 | |
1061 | |
734 | $w[$x] = max $w[$x], $w; |
1062 | $w[$x] = max $w[$x], $w; |
735 | $h[$y] = max $h[$y], $h; |
1063 | $h[$y] = max $h[$y], $h; |
736 | } |
1064 | } |
737 | } |
1065 | } |
… | |
… | |
749 | (sum @$hs), |
1077 | (sum @$hs), |
750 | ) |
1078 | ) |
751 | } |
1079 | } |
752 | |
1080 | |
753 | sub size_allocate { |
1081 | sub size_allocate { |
754 | my ($self, $x, $y, $w, $h) = @_; |
1082 | my ($self, $w, $h) = @_; |
755 | |
|
|
756 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
757 | |
1083 | |
758 | my ($ws, $hs) = $self->get_wh; |
1084 | my ($ws, $hs) = $self->get_wh; |
759 | |
1085 | |
760 | my $req_w = sum @$ws; |
1086 | my $req_w = sum @$ws; |
761 | my $req_h = sum @$hs; |
1087 | my $req_h = sum @$hs; |
… | |
… | |
783 | |
1109 | |
784 | for my $c (0 .. $#$row) { |
1110 | for my $c (0 .. $#$row) { |
785 | my $col_w = $ws->[$c]; |
1111 | my $col_w = $ws->[$c]; |
786 | |
1112 | |
787 | if (my $widget = $row->[$c]) { |
1113 | if (my $widget = $row->[$c]) { |
788 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
1114 | $widget->configure ($x, $y, $col_w, $row_h); |
789 | } |
1115 | } |
790 | |
1116 | |
791 | $x += $col_w; |
1117 | $x += $col_w; |
792 | } |
1118 | } |
793 | |
1119 | |
… | |
… | |
838 | (List::Util::max map $_->[1], @alloc), |
1164 | (List::Util::max map $_->[1], @alloc), |
839 | ) |
1165 | ) |
840 | } |
1166 | } |
841 | |
1167 | |
842 | sub size_allocate { |
1168 | sub size_allocate { |
843 | my ($self, $x, $y, $w, $h) = @_; |
1169 | my ($self, $w, $h) = @_; |
844 | |
|
|
845 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
846 | |
1170 | |
847 | ($h, $w) = ($w, $h); |
1171 | ($h, $w) = ($w, $h); |
848 | |
1172 | |
849 | my $children = $self->{children}; |
1173 | my $children = $self->{children}; |
850 | |
1174 | |
851 | my @h = map +($_->size_request)[0], @$children; |
1175 | my @h = map $_->{req_w}, @$children; |
852 | |
1176 | |
853 | my $req_h = List::Util::sum @h; |
1177 | my $req_h = List::Util::sum @h; |
854 | |
1178 | |
855 | if ($req_h > $h) { |
1179 | if ($req_h > $h) { |
856 | # ah well, not enough space |
1180 | # ah well, not enough space |
… | |
… | |
872 | |
1196 | |
873 | my $y = 0; |
1197 | my $y = 0; |
874 | for (0 .. $#$children) { |
1198 | for (0 .. $#$children) { |
875 | my $child = $children->[$_]; |
1199 | my $child = $children->[$_]; |
876 | my $h = $h[$_]; |
1200 | my $h = $h[$_]; |
877 | $child->size_allocate ($y, 0, $h, $w); |
1201 | $child->configure ($y, 0, $h, $w); |
878 | |
1202 | |
879 | $y += $h; |
1203 | $y += $h; |
880 | } |
1204 | } |
|
|
1205 | |
|
|
1206 | 1 |
881 | } |
1207 | } |
882 | |
1208 | |
883 | ############################################################################# |
1209 | ############################################################################# |
884 | |
1210 | |
885 | package CFClient::UI::VBox; |
1211 | package CFClient::UI::VBox; |
… | |
… | |
898 | (List::Util::sum map $_->[1], @alloc), |
1224 | (List::Util::sum map $_->[1], @alloc), |
899 | ) |
1225 | ) |
900 | } |
1226 | } |
901 | |
1227 | |
902 | sub size_allocate { |
1228 | sub size_allocate { |
903 | my ($self, $x, $y, $w, $h) = @_; |
1229 | my ($self, $w, $h) = @_; |
904 | |
1230 | |
905 | $self->_size_allocate ($x, $y, $w, $h); |
1231 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
906 | |
1232 | |
907 | my $children = $self->{children}; |
1233 | my $children = $self->{children}; |
908 | |
1234 | |
909 | my @h = map +($_->size_request)[1], @$children; |
1235 | my @h = map $_->{req_h}, @$children; |
910 | |
1236 | |
911 | my $req_h = List::Util::sum @h; |
1237 | my $req_h = List::Util::sum @h; |
912 | |
1238 | |
913 | if ($req_h > $h) { |
1239 | if ($req_h > $h) { |
914 | # ah well, not enough space |
1240 | # ah well, not enough space |
… | |
… | |
928 | |
1254 | |
929 | my $y = 0; |
1255 | my $y = 0; |
930 | for (0 .. $#$children) { |
1256 | for (0 .. $#$children) { |
931 | my $child = $children->[$_]; |
1257 | my $child = $children->[$_]; |
932 | my $h = $h[$_]; |
1258 | my $h = $h[$_]; |
933 | $child->size_allocate (0, $y, $w, $h); |
1259 | $child->configure (0, $y, $w, $h); |
934 | |
1260 | |
935 | $y += $h; |
1261 | $y += $h; |
936 | } |
1262 | } |
|
|
1263 | |
|
|
1264 | 1 |
937 | } |
1265 | } |
938 | |
1266 | |
939 | ############################################################################# |
1267 | ############################################################################# |
940 | |
1268 | |
941 | package CFClient::UI::Label; |
1269 | package CFClient::UI::Label; |
942 | |
1270 | |
943 | our @ISA = CFClient::UI::Base::; |
1271 | our @ISA = CFClient::UI::Base::; |
944 | |
1272 | |
945 | use SDL::OpenGL; |
1273 | use CFClient::OpenGL; |
946 | |
1274 | |
947 | sub new { |
1275 | sub new { |
948 | my ($class, %arg) = @_; |
1276 | my ($class, %arg) = @_; |
949 | |
1277 | |
950 | my $self = $class->SUPER::new ( |
1278 | my $self = $class->SUPER::new ( |
951 | fg => [1, 1, 1], |
1279 | fg => [1, 1, 1], |
952 | fontsize => $::FONTSIZE, |
1280 | #font => default_font |
953 | text => "", |
1281 | #text => initial text |
|
|
1282 | #markup => initial narkup |
|
|
1283 | layout => (new CFClient::Layout), |
|
|
1284 | fontsize => 1, |
954 | align => -1, |
1285 | align => -1, |
|
|
1286 | valign => -1, |
955 | padding => 2, |
1287 | padding => 2, |
956 | layout => new CFClient::Layout, |
1288 | can_events => 0, |
957 | %arg |
1289 | %arg |
958 | ); |
1290 | ); |
959 | |
1291 | |
|
|
1292 | if (exists $self->{template}) { |
|
|
1293 | my $layout = new CFClient::Layout; |
|
|
1294 | $layout->set_text (delete $self->{template}); |
|
|
1295 | $self->{template} = $layout; |
|
|
1296 | } |
|
|
1297 | |
|
|
1298 | if (exists $self->{markup}) { |
|
|
1299 | $self->set_markup (delete $self->{markup}); |
|
|
1300 | } else { |
960 | $self->set_text ($self->{text}); |
1301 | $self->set_text (delete $self->{text}); |
|
|
1302 | } |
961 | |
1303 | |
962 | $self |
1304 | $self |
963 | } |
1305 | } |
964 | |
1306 | |
965 | sub escape_text { |
1307 | sub escape { |
966 | local $_ = $_[1]; |
1308 | local $_ = $_[1]; |
967 | |
1309 | |
968 | s/&/&/g; |
1310 | s/&/&/g; |
969 | s/>/>/g; |
1311 | s/>/>/g; |
970 | s/</</g; |
1312 | s/</</g; |
971 | |
1313 | |
972 | $_[1] |
1314 | $_[1] |
973 | } |
1315 | } |
974 | |
1316 | |
|
|
1317 | sub update { |
|
|
1318 | my ($self) = @_; |
|
|
1319 | |
|
|
1320 | delete $self->{texture}; |
|
|
1321 | $self->SUPER::update; |
|
|
1322 | } |
|
|
1323 | |
975 | sub set_text { |
1324 | sub set_text { |
976 | my ($self, $text) = @_; |
1325 | my ($self, $text) = @_; |
977 | |
1326 | |
|
|
1327 | return if $self->{text} eq "T$text"; |
978 | $self->{text} = $text; |
1328 | $self->{text} = "T$text"; |
|
|
1329 | |
|
|
1330 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
979 | $self->{layout}->set_markup ($text); |
1331 | $self->{layout}->set_text ($text); |
980 | |
1332 | |
981 | delete $self->{texture}; |
|
|
982 | # $self->{w} = $self->{h} = -1; |
|
|
983 | $self->update; |
1333 | $self->update; |
|
|
1334 | $self->check_size; |
984 | } |
1335 | } |
985 | |
1336 | |
986 | sub get_text { |
1337 | sub set_markup { |
987 | my ($self, $text) = @_; |
1338 | my ($self, $markup) = @_; |
988 | |
1339 | |
989 | $self->{text} |
1340 | return if $self->{text} eq "M$markup"; |
|
|
1341 | $self->{text} = "M$markup"; |
|
|
1342 | |
|
|
1343 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
|
|
1344 | |
|
|
1345 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
|
|
1346 | $self->{layout}->set_markup ($markup); |
|
|
1347 | |
|
|
1348 | $self->update; |
|
|
1349 | $self->check_size; |
990 | } |
1350 | } |
991 | |
1351 | |
992 | sub size_request { |
1352 | sub size_request { |
993 | my ($self) = @_; |
1353 | my ($self) = @_; |
994 | |
1354 | |
995 | $self->{layout}->set_width; |
1355 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1356 | $self->{layout}->set_width ($self->{max_w} || -1); |
996 | $self->{layout}->set_height ($self->{fontsize}); |
1357 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1358 | |
997 | my ($w, $h) = $self->{layout}->size; |
1359 | my ($w, $h) = $self->{layout}->size; |
|
|
1360 | |
|
|
1361 | if (exists $self->{template}) { |
|
|
1362 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1363 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1364 | |
|
|
1365 | my ($w2, $h2) = $self->{template}->size; |
|
|
1366 | |
|
|
1367 | $w = List::Util::max $w, $w2; |
|
|
1368 | $h = List::Util::max $h, $h2; |
|
|
1369 | } |
998 | |
1370 | |
999 | ( |
1371 | ( |
1000 | $w + $self->{padding} * 2, |
1372 | $w + $self->{padding} * 2, |
1001 | $h + $self->{padding} * 2, |
1373 | $h + $self->{padding} * 2, |
1002 | ) |
1374 | ) |
1003 | } |
1375 | } |
1004 | |
1376 | |
1005 | sub size_allocate { |
1377 | sub size_allocate { |
1006 | my ($self, $x, $y, $w, $h) = @_; |
1378 | my ($self, $w, $h) = @_; |
1007 | |
|
|
1008 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
1009 | |
1379 | |
1010 | delete $self->{texture}; |
1380 | delete $self->{texture}; |
1011 | } |
1381 | } |
1012 | |
1382 | |
1013 | sub update { |
1383 | sub set_fontsize { |
1014 | my ($self) = @_; |
1384 | my ($self, $fontsize) = @_; |
1015 | |
1385 | |
|
|
1386 | $self->{fontsize} = $fontsize; |
1016 | delete $self->{texture}; |
1387 | delete $self->{texture}; |
|
|
1388 | |
1017 | $self->SUPER::update; |
1389 | $self->update; |
|
|
1390 | $self->check_size; |
1018 | } |
1391 | } |
1019 | |
1392 | |
1020 | sub _draw { |
1393 | sub _draw { |
1021 | my ($self) = @_; |
1394 | my ($self) = @_; |
1022 | |
1395 | |
1023 | my $tex = $self->{texture} ||= do { |
1396 | my $tex = $self->{texture} ||= do { |
|
|
1397 | $self->{layout}->set_foreground (@{$self->{fg}}); |
|
|
1398 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1024 | $self->{layout}->set_width ($self->{w}); |
1399 | $self->{layout}->set_width ($self->{w}); |
1025 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
1400 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
|
|
1401 | |
1026 | new_from_layout CFClient::Texture $self->{layout} |
1402 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
|
|
1403 | |
|
|
1404 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
|
|
1405 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1406 | : ($self->{w} - $tex->{w}) * 0.5); |
|
|
1407 | |
|
|
1408 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
|
|
1409 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1410 | : ($self->{h} - $tex->{h}) * 0.5); |
|
|
1411 | |
|
|
1412 | $tex |
1027 | }; |
1413 | }; |
1028 | |
1414 | |
1029 | glEnable GL_BLEND; |
|
|
1030 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1031 | glEnable GL_TEXTURE_2D; |
1415 | glEnable GL_TEXTURE_2D; |
1032 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1416 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1033 | |
1417 | |
|
|
1418 | if ($tex->{format} == GL_ALPHA) { |
1034 | glColor @{$self->{fg}}; |
1419 | glColor @{$self->{fg}}; |
1035 | |
1420 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
1036 | my $x = |
1421 | } else { |
1037 | $self->{align} < 0 ? $self->{padding} |
1422 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
1038 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1423 | } |
1039 | : ($self->{w} - $tex->{w}) * 0.5; |
|
|
1040 | |
|
|
1041 | $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); |
|
|
1042 | |
1424 | |
1043 | glDisable GL_TEXTURE_2D; |
1425 | glDisable GL_TEXTURE_2D; |
1044 | glDisable GL_BLEND; |
|
|
1045 | } |
1426 | } |
1046 | |
1427 | |
1047 | ############################################################################# |
1428 | ############################################################################# |
1048 | |
1429 | |
1049 | package CFClient::UI::Entry; |
1430 | package CFClient::UI::EntryBase; |
1050 | |
1431 | |
1051 | our @ISA = CFClient::UI::Label::; |
1432 | our @ISA = CFClient::UI::Label::; |
1052 | |
1433 | |
1053 | use SDL; |
1434 | use CFClient::OpenGL; |
1054 | use SDL::OpenGL; |
|
|
1055 | |
1435 | |
1056 | sub new { |
1436 | sub new { |
1057 | my $class = shift; |
1437 | my $class = shift; |
1058 | |
1438 | |
1059 | $class->SUPER::new ( |
1439 | $class->SUPER::new ( |
1060 | fg => [1, 1, 1], |
1440 | fg => [1, 1, 1], |
1061 | bg => [0, 0, 0, 0.2], |
1441 | bg => [0, 0, 0, 0.2], |
1062 | active_bg => [1, 1, 1, 0.5], |
1442 | active_bg => [1, 1, 1, 0.5], |
1063 | active_fg => [0, 0, 0], |
1443 | active_fg => [0, 0, 0], |
1064 | can_hover => 1, |
1444 | can_hover => 1, |
1065 | can_focus => 1, |
1445 | can_focus => 1, |
|
|
1446 | valign => 0, |
|
|
1447 | can_events => 1, |
1066 | @_ |
1448 | @_ |
1067 | ) |
1449 | ) |
1068 | } |
1450 | } |
1069 | |
1451 | |
1070 | sub _set_text { |
1452 | sub _set_text { |
1071 | my ($self, $text) = @_; |
1453 | my ($self, $text) = @_; |
1072 | |
1454 | |
1073 | my $old_text = $self->{text}; |
1455 | delete $self->{cur_h}; |
|
|
1456 | |
|
|
1457 | return if $self->{text} eq $text; |
|
|
1458 | |
|
|
1459 | delete $self->{texture}; |
1074 | |
1460 | |
1075 | $self->{last_activity} = $::NOW; |
1461 | $self->{last_activity} = $::NOW; |
1076 | |
|
|
1077 | $self->{text} = $text; |
1462 | $self->{text} = $text; |
1078 | $self->{layout}->set_width ($self->{w}); |
|
|
1079 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
|
|
1080 | |
1463 | |
1081 | $text =~ s/./*/g if $self->{hidden}; |
1464 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1465 | $self->{layout}->set_text ("$text "); |
1082 | |
1466 | |
1083 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1467 | $self->emit (changed => $self->{text}); |
1084 | |
|
|
1085 | $text = substr $text, 0, $self->{cursor}; |
|
|
1086 | utf8::encode $text; |
|
|
1087 | |
|
|
1088 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1089 | |
|
|
1090 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1091 | if $old_text ne $self->{text}; |
|
|
1092 | } |
|
|
1093 | |
|
|
1094 | sub size_request { |
|
|
1095 | my ($self) = @_; |
|
|
1096 | |
|
|
1097 | my ($w, $h) = $self->SUPER::size_request; |
|
|
1098 | |
|
|
1099 | ($w + 1, $h) # add 1 for cursor |
|
|
1100 | } |
|
|
1101 | |
|
|
1102 | sub size_allocate { |
|
|
1103 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1104 | |
|
|
1105 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1106 | |
|
|
1107 | $self->_set_text ($self->{text}); |
|
|
1108 | } |
1468 | } |
1109 | |
1469 | |
1110 | sub set_text { |
1470 | sub set_text { |
1111 | my ($self, $text) = @_; |
1471 | my ($self, $text) = @_; |
1112 | |
1472 | |
1113 | $self->{cursor} = length $text; |
1473 | $self->{cursor} = length $text; |
1114 | $self->_set_text ($text); |
1474 | $self->_set_text ($text); |
1115 | $self->update; |
1475 | $self->update; |
|
|
1476 | $self->check_size; |
|
|
1477 | } |
|
|
1478 | |
|
|
1479 | sub get_text { |
|
|
1480 | $_[0]{text} |
|
|
1481 | } |
|
|
1482 | |
|
|
1483 | sub size_request { |
|
|
1484 | my ($self) = @_; |
|
|
1485 | |
|
|
1486 | my ($w, $h) = $self->SUPER::size_request; |
|
|
1487 | |
|
|
1488 | ($w + 1, $h) # add 1 for cursor |
|
|
1489 | } |
|
|
1490 | |
|
|
1491 | sub size_allocate { |
|
|
1492 | my ($self, $w, $h) = @_; |
|
|
1493 | |
|
|
1494 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1116 | } |
1495 | } |
1117 | |
1496 | |
1118 | sub key_down { |
1497 | sub key_down { |
1119 | my ($self, $ev) = @_; |
1498 | my ($self, $ev) = @_; |
1120 | |
1499 | |
1121 | my $mod = $ev->key_mod; |
1500 | my $mod = $ev->{mod}; |
1122 | my $sym = $ev->key_sym; |
1501 | my $sym = $ev->{sym}; |
1123 | |
|
|
1124 | my $uni = $ev->key_unicode; |
1502 | my $uni = $ev->{unicode}; |
1125 | |
1503 | |
1126 | my $text = $self->get_text; |
1504 | my $text = $self->get_text; |
1127 | |
1505 | |
1128 | if ($sym == SDLK_BACKSPACE) { |
1506 | if ($uni == 8) { |
1129 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1507 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1130 | } elsif ($sym == SDLK_DELETE) { |
1508 | } elsif ($uni == 127) { |
1131 | substr $text, $self->{cursor}, 1, ""; |
1509 | substr $text, $self->{cursor}, 1, ""; |
1132 | } elsif ($sym == SDLK_LEFT) { |
1510 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1133 | --$self->{cursor} if $self->{cursor}; |
1511 | --$self->{cursor} if $self->{cursor}; |
1134 | } elsif ($sym == SDLK_RIGHT) { |
1512 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1135 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1513 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1136 | } elsif ($sym == SDLK_HOME) { |
1514 | } elsif ($sym == CFClient::SDLK_HOME) { |
1137 | $self->{cursor} = 0; |
1515 | $self->{cursor} = 0; |
1138 | } elsif ($sym == SDLK_END) { |
1516 | } elsif ($sym == CFClient::SDLK_END) { |
1139 | $self->{cursor} = length $text; |
1517 | $self->{cursor} = length $text; |
1140 | } elsif ($sym == SDLK_ESCAPE) { |
1518 | } elsif ($uni == 27) { |
1141 | $self->emit ('escape'); |
1519 | $self->emit ('escape'); |
1142 | } elsif ($uni) { |
1520 | } elsif ($uni) { |
1143 | substr $text, $self->{cursor}++, 0, chr $uni; |
1521 | substr $text, $self->{cursor}++, 0, chr $uni; |
1144 | } |
1522 | } |
1145 | |
1523 | |
1146 | $self->_set_text ($text); |
1524 | $self->_set_text ($text); |
1147 | $self->update; |
1525 | $self->update; |
|
|
1526 | $self->check_size; |
1148 | } |
1527 | } |
1149 | |
1528 | |
1150 | sub focus_in { |
1529 | sub focus_in { |
1151 | my ($self) = @_; |
1530 | my ($self) = @_; |
1152 | |
1531 | |
… | |
… | |
1200 | |
1579 | |
1201 | $self->SUPER::_draw; |
1580 | $self->SUPER::_draw; |
1202 | |
1581 | |
1203 | #TODO: force update every cursor change :( |
1582 | #TODO: force update every cursor change :( |
1204 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1583 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1584 | |
|
|
1585 | unless (exists $self->{cur_h}) { |
|
|
1586 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1587 | utf8::encode $text; |
|
|
1588 | |
|
|
1589 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1590 | } |
|
|
1591 | |
1205 | glColor @{$self->{fg}}; |
1592 | glColor @{$self->{fg}}; |
1206 | glBegin GL_LINES; |
1593 | glBegin GL_LINES; |
1207 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1208 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1594 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1595 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1209 | glEnd; |
1596 | glEnd; |
1210 | } |
1597 | } |
1211 | } |
1598 | } |
1212 | |
1599 | |
1213 | package CFClient::UI::LineEntry; |
1600 | package CFClient::UI::Entry; |
1214 | |
1601 | |
1215 | our @ISA = CFClient::UI::Entry::; |
1602 | our @ISA = CFClient::UI::EntryBase::; |
1216 | |
1603 | |
1217 | use SDL; |
1604 | use CFClient::OpenGL; |
1218 | use SDL::OpenGL; |
|
|
1219 | |
1605 | |
1220 | sub key_down { |
1606 | sub key_down { |
1221 | my ($self, $ev) = @_; |
1607 | my ($self, $ev) = @_; |
1222 | |
1608 | |
1223 | my $sym = $ev->key_sym; |
1609 | my $sym = $ev->{sym}; |
1224 | |
1610 | |
1225 | if ($sym == SDLK_RETURN) { |
1611 | if ($sym == 13) { |
|
|
1612 | unshift @{$self->{history}}, |
|
|
1613 | my $txt = $self->get_text; |
|
|
1614 | $self->{history_pointer} = -1; |
|
|
1615 | $self->{history_saveback} = ''; |
1226 | $self->emit (activate => $self->get_text); |
1616 | $self->emit (activate => $txt); |
1227 | $self->update; |
1617 | $self->update; |
|
|
1618 | |
|
|
1619 | } elsif ($sym == CFClient::SDLK_UP) { |
|
|
1620 | if ($self->{history_pointer} < 0) { |
|
|
1621 | $self->{history_saveback} = $self->get_text; |
|
|
1622 | } |
|
|
1623 | if (@{$self->{history} || []} > 0) { |
|
|
1624 | $self->{history_pointer}++; |
|
|
1625 | if ($self->{history_pointer} >= @{$self->{history} || []}) { |
|
|
1626 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
|
|
1627 | } |
|
|
1628 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
|
|
1629 | } |
|
|
1630 | |
|
|
1631 | } elsif ($sym == CFClient::SDLK_DOWN) { |
|
|
1632 | $self->{history_pointer}--; |
|
|
1633 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
|
|
1634 | |
|
|
1635 | if ($self->{history_pointer} >= 0) { |
|
|
1636 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
|
|
1637 | } else { |
|
|
1638 | $self->set_text ($self->{history_saveback}); |
|
|
1639 | } |
1228 | |
1640 | |
1229 | } else { |
1641 | } else { |
1230 | $self->SUPER::key_down ($ev); |
1642 | $self->SUPER::key_down ($ev); |
1231 | } |
1643 | } |
1232 | |
1644 | |
… | |
… | |
1236 | |
1648 | |
1237 | package CFClient::UI::Button; |
1649 | package CFClient::UI::Button; |
1238 | |
1650 | |
1239 | our @ISA = CFClient::UI::Label::; |
1651 | our @ISA = CFClient::UI::Label::; |
1240 | |
1652 | |
1241 | use SDL; |
1653 | use CFClient::OpenGL; |
1242 | use SDL::OpenGL; |
|
|
1243 | |
1654 | |
1244 | my @tex = |
1655 | my @tex = |
1245 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1656 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1246 | qw(b1_button_active.png); |
1657 | qw(b1_button_active.png); |
1247 | |
1658 | |
1248 | sub new { |
1659 | sub new { |
1249 | my $class = shift; |
1660 | my $class = shift; |
1250 | |
1661 | |
1251 | $class->SUPER::new ( |
1662 | $class->SUPER::new ( |
1252 | padding => 4, |
1663 | padding => 4, |
1253 | fg => [1, 1, 1], |
1664 | fg => [1, 1, 1], |
1254 | bg => [1, 1, 1, 0.2], |
1665 | bg => [1, 1, 1, 0.2], |
1255 | active_fg => [1, 1, 0], |
1666 | active_fg => [0, 0, 1], |
1256 | can_hover => 1, |
1667 | can_hover => 1, |
|
|
1668 | align => 0, |
|
|
1669 | valign => 0, |
|
|
1670 | can_events => 1, |
1257 | @_ |
1671 | @_ |
1258 | ) |
1672 | ) |
1259 | } |
1673 | } |
1260 | |
1674 | |
1261 | sub button_up { |
1675 | sub button_up { |
… | |
… | |
1269 | |
1683 | |
1270 | sub _draw { |
1684 | sub _draw { |
1271 | my ($self) = @_; |
1685 | my ($self) = @_; |
1272 | |
1686 | |
1273 | local $self->{fg} = $self->{fg}; |
1687 | local $self->{fg} = $self->{fg}; |
1274 | my $tex = $tex[0]; |
|
|
1275 | |
|
|
1276 | glEnable GL_BLEND; |
|
|
1277 | glEnable GL_TEXTURE_2D; |
|
|
1278 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1279 | |
1688 | |
1280 | if ($GRAB == $self) { |
1689 | if ($GRAB == $self) { |
1281 | $self->{fg} = $self->{active_fg}; |
1690 | $self->{fg} = $self->{active_fg}; |
1282 | } |
1691 | } |
1283 | |
1692 | |
1284 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1693 | glEnable GL_TEXTURE_2D; |
1285 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1694 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1695 | glColor 0, 0, 0, 1; |
1286 | |
1696 | |
1287 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1697 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
1288 | |
1698 | |
1289 | glDisable GL_TEXTURE_2D; |
1699 | glDisable GL_TEXTURE_2D; |
1290 | glDisable GL_BLEND; |
|
|
1291 | |
1700 | |
1292 | $self->SUPER::_draw; |
1701 | $self->SUPER::_draw; |
1293 | } |
1702 | } |
1294 | |
1703 | |
1295 | ############################################################################# |
1704 | ############################################################################# |
… | |
… | |
1297 | package CFClient::UI::CheckBox; |
1706 | package CFClient::UI::CheckBox; |
1298 | |
1707 | |
1299 | our @ISA = CFClient::UI::DrawBG::; |
1708 | our @ISA = CFClient::UI::DrawBG::; |
1300 | |
1709 | |
1301 | my @tex = |
1710 | my @tex = |
1302 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1711 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1303 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1712 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1304 | |
1713 | |
1305 | use SDL; |
1714 | use CFClient::OpenGL; |
1306 | use SDL::OpenGL; |
|
|
1307 | |
1715 | |
1308 | sub new { |
1716 | sub new { |
1309 | my $class = shift; |
1717 | my $class = shift; |
1310 | |
1718 | |
1311 | $class->SUPER::new ( |
1719 | $class->SUPER::new ( |
… | |
… | |
1322 | my ($self) = @_; |
1730 | my ($self) = @_; |
1323 | |
1731 | |
1324 | ($self->{padding} * 2 + 6) x 2 |
1732 | ($self->{padding} * 2 + 6) x 2 |
1325 | } |
1733 | } |
1326 | |
1734 | |
1327 | sub size_allocate { |
|
|
1328 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1329 | |
|
|
1330 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1331 | } |
|
|
1332 | |
|
|
1333 | sub button_down { |
1735 | sub button_down { |
1334 | my ($self, $ev, $x, $y) = @_; |
1736 | my ($self, $ev, $x, $y) = @_; |
1335 | |
1737 | |
1336 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1738 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1337 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1739 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1349 | |
1751 | |
1350 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1752 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1351 | |
1753 | |
1352 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1754 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1353 | |
1755 | |
|
|
1756 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1757 | |
|
|
1758 | glEnable GL_TEXTURE_2D; |
|
|
1759 | $tex->draw_quad_alpha (0, 0, $s, $s); |
|
|
1760 | glDisable GL_TEXTURE_2D; |
|
|
1761 | } |
|
|
1762 | |
|
|
1763 | ############################################################################# |
|
|
1764 | |
|
|
1765 | package CFClient::UI::Image; |
|
|
1766 | |
|
|
1767 | our @ISA = CFClient::UI::Base::; |
|
|
1768 | |
|
|
1769 | use CFClient::OpenGL; |
|
|
1770 | use Carp qw/confess/; |
|
|
1771 | |
|
|
1772 | our %loaded_images; |
|
|
1773 | |
|
|
1774 | sub new { |
|
|
1775 | my $class = shift; |
|
|
1776 | |
|
|
1777 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1778 | |
|
|
1779 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1780 | |
|
|
1781 | $loaded_images{$self->{image}} ||= |
|
|
1782 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1783 | |
|
|
1784 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1785 | |
|
|
1786 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1787 | |
|
|
1788 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1789 | |
|
|
1790 | $self |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | sub size_request { |
|
|
1794 | my ($self) = @_; |
|
|
1795 | |
|
|
1796 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1797 | } |
|
|
1798 | |
|
|
1799 | sub _draw { |
|
|
1800 | my ($self) = @_; |
|
|
1801 | |
|
|
1802 | my $tex = $self->{tex}; |
|
|
1803 | |
|
|
1804 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1805 | |
|
|
1806 | if ($self->{rot90}) { |
|
|
1807 | glRotate 90, 0, 0, 1; |
|
|
1808 | glTranslate 0, -$self->{w}, 0; |
|
|
1809 | |
|
|
1810 | ($w, $h) = ($h, $w); |
|
|
1811 | } |
|
|
1812 | |
|
|
1813 | glEnable GL_TEXTURE_2D; |
|
|
1814 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1815 | |
|
|
1816 | $tex->draw_quad_alpha (0, 0, $w, $h); |
|
|
1817 | |
|
|
1818 | glDisable GL_TEXTURE_2D; |
|
|
1819 | } |
|
|
1820 | |
|
|
1821 | ############################################################################# |
|
|
1822 | |
|
|
1823 | package CFClient::UI::VGauge; |
|
|
1824 | |
|
|
1825 | our @ISA = CFClient::UI::Base::; |
|
|
1826 | |
|
|
1827 | use List::Util qw(min max); |
|
|
1828 | |
|
|
1829 | use CFClient::OpenGL; |
|
|
1830 | |
|
|
1831 | my %tex = ( |
|
|
1832 | food => [ |
|
|
1833 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1834 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1835 | ], |
|
|
1836 | grace => [ |
|
|
1837 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1838 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
|
|
1839 | ], |
|
|
1840 | hp => [ |
|
|
1841 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1842 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1843 | ], |
|
|
1844 | mana => [ |
|
|
1845 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1846 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
|
|
1847 | ], |
|
|
1848 | ); |
|
|
1849 | |
|
|
1850 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1851 | sub new { |
|
|
1852 | my $class = shift; |
|
|
1853 | |
|
|
1854 | my $self = $class->SUPER::new ( |
|
|
1855 | type => 'food', |
|
|
1856 | @_ |
|
|
1857 | ); |
|
|
1858 | |
|
|
1859 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1860 | |
|
|
1861 | $self |
|
|
1862 | } |
|
|
1863 | |
|
|
1864 | sub size_request { |
|
|
1865 | my ($self) = @_; |
|
|
1866 | |
|
|
1867 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1868 | #@$tex{qw(w h)} |
|
|
1869 | (0, 0) |
|
|
1870 | } |
|
|
1871 | |
|
|
1872 | sub set_max { |
|
|
1873 | my ($self, $max) = @_; |
|
|
1874 | |
|
|
1875 | return if $self->{max_val} == $max; |
|
|
1876 | |
|
|
1877 | $self->{max_val} = $max; |
|
|
1878 | $self->update; |
|
|
1879 | } |
|
|
1880 | |
|
|
1881 | sub set_value { |
|
|
1882 | my ($self, $val, $max) = @_; |
|
|
1883 | |
|
|
1884 | $self->set_max ($max) |
|
|
1885 | if defined $max; |
|
|
1886 | |
|
|
1887 | return if $self->{val} == $val; |
|
|
1888 | |
|
|
1889 | $self->{val} = $val; |
|
|
1890 | $self->update; |
|
|
1891 | } |
|
|
1892 | |
|
|
1893 | sub _draw { |
|
|
1894 | my ($self) = @_; |
|
|
1895 | |
|
|
1896 | my $tex = $tex{$self->{type}}; |
|
|
1897 | my ($t1, $t2, $t3) = @$tex; |
|
|
1898 | |
|
|
1899 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1900 | |
|
|
1901 | if ($self->{vertical}) { |
|
|
1902 | glRotate 90, 0, 0, 1; |
|
|
1903 | glTranslate 0, -$self->{w}, 0; |
|
|
1904 | |
|
|
1905 | ($w, $h) = ($h, $w); |
|
|
1906 | } |
|
|
1907 | |
|
|
1908 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1909 | |
|
|
1910 | my $ycut1 = max 0, min 1, $ycut; |
|
|
1911 | my $ycut2 = max 0, min 1, $ycut - 1; |
|
|
1912 | |
|
|
1913 | my $h1 = $self->{h} * (1 - $ycut1); |
|
|
1914 | my $h2 = $self->{h} * (1 - $ycut2); |
|
|
1915 | |
1354 | glEnable GL_BLEND; |
1916 | glEnable GL_BLEND; |
|
|
1917 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1355 | glEnable GL_TEXTURE_2D; |
1918 | glEnable GL_TEXTURE_2D; |
1356 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1919 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1357 | |
1920 | |
1358 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1921 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1922 | glBegin GL_QUADS; |
|
|
1923 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1924 | glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1925 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1926 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1927 | glEnd; |
1359 | |
1928 | |
1360 | $tex->draw_quad (0, 0, $s, $s); |
1929 | my $ycut1 = List::Util::min 1, $ycut; |
|
|
1930 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1931 | glBegin GL_QUADS; |
|
|
1932 | glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1933 | glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1934 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1935 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1936 | glEnd; |
1361 | |
1937 | |
|
|
1938 | if ($t3) { |
|
|
1939 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
|
|
1940 | glBegin GL_QUADS; |
|
|
1941 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1942 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
|
|
1943 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
|
|
1944 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1945 | glEnd; |
|
|
1946 | } |
|
|
1947 | |
|
|
1948 | glDisable GL_BLEND; |
1362 | glDisable GL_TEXTURE_2D; |
1949 | glDisable GL_TEXTURE_2D; |
1363 | glDisable GL_BLEND; |
1950 | } |
|
|
1951 | |
|
|
1952 | ############################################################################# |
|
|
1953 | |
|
|
1954 | package CFClient::UI::Gauge; |
|
|
1955 | |
|
|
1956 | our @ISA = CFClient::UI::VBox::; |
|
|
1957 | |
|
|
1958 | sub new { |
|
|
1959 | my ($class, %arg) = @_; |
|
|
1960 | |
|
|
1961 | my $self = $class->SUPER::new ( |
|
|
1962 | tooltip => $arg{type}, |
|
|
1963 | can_hover => 1, |
|
|
1964 | can_events => 1, |
|
|
1965 | %arg, |
|
|
1966 | ); |
|
|
1967 | |
|
|
1968 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); |
|
|
1969 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
|
|
1970 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); |
|
|
1971 | |
|
|
1972 | $self |
|
|
1973 | } |
|
|
1974 | |
|
|
1975 | sub set_fontsize { |
|
|
1976 | my ($self, $fsize) = @_; |
|
|
1977 | |
|
|
1978 | $self->{value}->set_fontsize ($fsize); |
|
|
1979 | $self->{max} ->set_fontsize ($fsize); |
|
|
1980 | } |
|
|
1981 | |
|
|
1982 | sub set_max { |
|
|
1983 | my ($self, $max) = @_; |
|
|
1984 | |
|
|
1985 | $self->{gauge}->set_max ($max); |
|
|
1986 | $self->{max}->set_text ($max); |
|
|
1987 | } |
|
|
1988 | |
|
|
1989 | sub set_value { |
|
|
1990 | my ($self, $val, $max) = @_; |
|
|
1991 | |
|
|
1992 | $self->set_max ($max) |
|
|
1993 | if defined $max; |
|
|
1994 | |
|
|
1995 | $self->{gauge}->set_value ($val, $max); |
|
|
1996 | $self->{value}->set_text ($val); |
1364 | } |
1997 | } |
1365 | |
1998 | |
1366 | ############################################################################# |
1999 | ############################################################################# |
1367 | |
2000 | |
1368 | package CFClient::UI::Slider; |
2001 | package CFClient::UI::Slider; |
1369 | |
2002 | |
1370 | use strict; |
2003 | use strict; |
1371 | |
2004 | |
1372 | use SDL::OpenGL; |
2005 | use CFClient::OpenGL; |
1373 | |
2006 | |
1374 | our @ISA = CFClient::UI::DrawBG::; |
2007 | our @ISA = CFClient::UI::DrawBG::; |
1375 | |
2008 | |
1376 | my @tex = |
2009 | my @tex = |
1377 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
2010 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1378 | qw(s1_slider.png s1_slider_bg.png); |
2011 | qw(s1_slider.png s1_slider_bg.png); |
1379 | |
2012 | |
1380 | sub new { |
2013 | sub new { |
1381 | my $class = shift; |
2014 | my $class = shift; |
1382 | |
2015 | |
1383 | # range [value, low, high, page] |
2016 | # range [value, low, high, page, unit] |
1384 | |
2017 | |
1385 | # TODO: 0-width page |
2018 | # TODO: 0-width page |
1386 | # TODO: req_w/h are wrong with vertical |
2019 | # TODO: req_w/h are wrong with vertical |
1387 | # TODO: calculations are off |
2020 | # TODO: calculations are off |
1388 | my $self = $class->SUPER::new ( |
2021 | my $self = $class->SUPER::new ( |
1389 | fg => [1, 1, 1], |
2022 | fg => [1, 1, 1], |
1390 | active_fg => [0, 0, 0], |
2023 | active_fg => [0, 0, 0], |
1391 | range => [0, 0, 100, 10], |
2024 | range => [0, 0, 100, 10], |
1392 | req_w => 40, |
2025 | req_w => $::WIDTH / 80, |
1393 | req_h => 13, |
2026 | req_h => $::WIDTH / 80, |
1394 | vertical => 0, |
2027 | vertical => 0, |
1395 | can_hover => 1, |
2028 | can_hover => 1, |
1396 | inner_pad => 5, |
2029 | inner_pad => .05, |
1397 | @_ |
2030 | @_ |
1398 | ); |
2031 | ); |
1399 | |
2032 | |
|
|
2033 | $self->set_value ($self->{range}[0]); |
|
|
2034 | $self->update; |
|
|
2035 | |
1400 | $self |
2036 | $self |
|
|
2037 | } |
|
|
2038 | |
|
|
2039 | sub set_value { |
|
|
2040 | my ($self, $value) = @_; |
|
|
2041 | |
|
|
2042 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
|
|
2043 | |
|
|
2044 | $hi = $lo + 1 if $hi <= $lo; |
|
|
2045 | |
|
|
2046 | $value = $lo if $value < $lo; |
|
|
2047 | $value = $hi if $value > $hi; |
|
|
2048 | |
|
|
2049 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
|
|
2050 | if $unit; |
|
|
2051 | |
|
|
2052 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2053 | |
|
|
2054 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
|
|
2055 | |
|
|
2056 | if ($value != $old_value) { |
|
|
2057 | $self->emit (changed => $value); |
|
|
2058 | $self->update; |
|
|
2059 | } |
1401 | } |
2060 | } |
1402 | |
2061 | |
1403 | sub size_request { |
2062 | sub size_request { |
1404 | my ($self) = @_; |
2063 | my ($self) = @_; |
1405 | |
2064 | |
… | |
… | |
1418 | |
2077 | |
1419 | sub mouse_motion { |
2078 | sub mouse_motion { |
1420 | my ($self, $ev, $x, $y) = @_; |
2079 | my ($self, $ev, $x, $y) = @_; |
1421 | |
2080 | |
1422 | if ($GRAB == $self) { |
2081 | if ($GRAB == $self) { |
|
|
2082 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
2083 | |
|
|
2084 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2085 | |
|
|
2086 | $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; |
|
|
2087 | |
|
|
2088 | $self->set_value ($x * ($hi - $lo) + $lo); |
|
|
2089 | } |
|
|
2090 | } |
|
|
2091 | |
|
|
2092 | sub update { |
|
|
2093 | my ($self) = @_; |
|
|
2094 | |
|
|
2095 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
|
|
2096 | $self->set_value ($self->{range}[0]); |
|
|
2097 | |
1423 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2098 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1424 | |
2099 | |
1425 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2100 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
1426 | |
2101 | |
1427 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
2102 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
1428 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1429 | |
2103 | |
1430 | $x -= $inner_pad_px; # substract the padding |
2104 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
1431 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
2105 | $value = $self->{scale} * ($value - $lo); |
1432 | $x = $lo if $x < $lo; |
|
|
1433 | $x = $hi - $page if $x > $hi - $page; |
|
|
1434 | $self->{range}[0] = $x; |
|
|
1435 | |
2106 | |
1436 | $self->emit (changed => $x); |
2107 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
1437 | $self->update; |
2108 | |
|
|
2109 | $value = 0 if $value < 0; |
|
|
2110 | $page = 1 - $value if $value + $page > 1; |
|
|
2111 | |
|
|
2112 | $self->{knob_x} = $value; |
|
|
2113 | $self->{knob_w} = $page; |
1438 | } |
2114 | }); |
1439 | } |
|
|
1440 | |
2115 | |
1441 | # the inner_* stuff is for generating a padding for the slider handle, |
2116 | $self->SUPER::update; |
1442 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1443 | # correct propably, but it does the job for now |
|
|
1444 | sub _calc_inner_pad_px { |
|
|
1445 | my ($self, $w) = @_; |
|
|
1446 | ($w / 100) * $self->{inner_pad} # % to pixels |
|
|
1447 | } |
2117 | } |
1448 | |
2118 | |
1449 | sub _draw { |
2119 | sub _draw { |
1450 | my ($self) = @_; |
2120 | my ($self) = @_; |
1451 | |
2121 | |
1452 | $self->SUPER::_draw (); |
2122 | $self->SUPER::_draw (); |
1453 | |
2123 | |
1454 | my ($w, $h) = @$self{qw(w h)}; |
2124 | glScale $self->{w}, $self->{h}; |
1455 | |
2125 | |
1456 | if ($self->{vertical}) { |
2126 | if ($self->{vertical}) { |
1457 | # draw a vertical slider like a rotated horizontal slider |
2127 | # draw a vertical slider like a rotated horizontal slider |
1458 | |
2128 | |
1459 | glRotate 90, 0, 0, 1; |
2129 | glRotate 90, 0, 0, 1; |
1460 | glTranslate 0, -$self->{w}, 0; |
2130 | glTranslate 0, 1, 0; |
1461 | |
|
|
1462 | ($w, $h) = ($h, $w); |
|
|
1463 | } |
2131 | } |
1464 | |
2132 | |
1465 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2133 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1466 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2134 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1467 | |
2135 | |
1468 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1469 | |
|
|
1470 | $hi = $value + 1 if $lo == $hi; |
|
|
1471 | |
|
|
1472 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1473 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1474 | |
|
|
1475 | $page = int $page * $inner_w / ($hi - $lo); |
|
|
1476 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
|
|
1477 | |
|
|
1478 | $w -= $page; |
|
|
1479 | $page &= ~1; |
|
|
1480 | glTranslate $page * 0.5, 0, 0; |
|
|
1481 | $page ||= 2; |
|
|
1482 | |
|
|
1483 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
|
|
1484 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
|
|
1485 | |
|
|
1486 | glEnable GL_BLEND; |
|
|
1487 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1488 | glEnable GL_TEXTURE_2D; |
2136 | glEnable GL_TEXTURE_2D; |
1489 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2137 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1490 | |
2138 | |
1491 | # draw background |
2139 | # draw background |
1492 | $tex[1]->draw_quad (0, 0, $w, $h); |
2140 | $tex[1]->draw_quad_alpha (0, 0, 1, 1); |
1493 | |
2141 | |
1494 | # draw handle |
2142 | # draw handle |
1495 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
2143 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
1496 | |
2144 | |
1497 | glDisable GL_BLEND; |
|
|
1498 | glDisable GL_TEXTURE_2D; |
2145 | glDisable GL_TEXTURE_2D; |
1499 | } |
2146 | } |
1500 | |
2147 | |
1501 | ############################################################################# |
2148 | ############################################################################# |
1502 | |
2149 | |
1503 | package CFClient::UI::TextView; |
2150 | package CFClient::UI::TextView; |
1504 | |
2151 | |
1505 | our @ISA = CFClient::UI::HBox::; |
2152 | our @ISA = CFClient::UI::HBox::; |
1506 | |
2153 | |
1507 | use SDL::OpenGL; |
2154 | use CFClient::OpenGL; |
1508 | |
2155 | |
1509 | sub new { |
2156 | sub new { |
1510 | my $class = shift; |
2157 | my $class = shift; |
1511 | |
2158 | |
1512 | my $self = $class->SUPER::new ( |
2159 | my $self = $class->SUPER::new ( |
1513 | req_w => $::WIDTH / 6, |
2160 | fontsize => 1, |
1514 | req_h => $::HEIGHT / 6, |
2161 | can_events => 0, |
1515 | fontsize => $::FONTSIZE, |
2162 | #font => default_font |
1516 | @_, |
2163 | @_, |
1517 | |
2164 | |
1518 | layout => (new CFClient::Layout), |
2165 | layout => (new CFClient::Layout 1), |
1519 | par => [], |
2166 | par => [], |
1520 | height => 0, |
2167 | height => 0, |
1521 | children => [ |
2168 | children => [ |
1522 | (new CFClient::UI::Empty expand => 1), |
2169 | (new CFClient::UI::Empty expand => 1), |
1523 | (new CFClient::UI::Slider vertical => 1), |
2170 | (new CFClient::UI::Slider vertical => 1), |
1524 | ], |
2171 | ], |
1525 | ); |
2172 | ); |
1526 | |
2173 | |
1527 | $self->{children}[1]->connect (changed => sub { |
2174 | $self->{children}[1]->connect (changed => sub { $self->update }); |
1528 | $self->update; |
|
|
1529 | }); |
|
|
1530 | |
2175 | |
1531 | $self |
2176 | $self |
1532 | } |
2177 | } |
1533 | |
2178 | |
1534 | sub set_fontsize { |
2179 | sub set_fontsize { |
… | |
… | |
1541 | sub text_height { |
2186 | sub text_height { |
1542 | my ($self, $text) = @_; |
2187 | my ($self, $text) = @_; |
1543 | |
2188 | |
1544 | my $layout = $self->{layout}; |
2189 | my $layout = $self->{layout}; |
1545 | |
2190 | |
1546 | $layout->set_height ($self->{fontsize}); |
2191 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1547 | $layout->set_width ($self->{w}); |
2192 | $layout->set_width ($self->{children}[0]{w}); |
1548 | $layout->set_text ($text); |
2193 | $layout->set_markup ($text); |
1549 | |
2194 | |
1550 | ($layout->size)[1] |
2195 | ($layout->size)[1] |
1551 | } |
2196 | } |
1552 | |
2197 | |
1553 | sub reflow { |
2198 | sub reflow { |
… | |
… | |
1555 | |
2200 | |
1556 | $self->{need_reflow}++; |
2201 | $self->{need_reflow}++; |
1557 | $self->update; |
2202 | $self->update; |
1558 | } |
2203 | } |
1559 | |
2204 | |
1560 | sub size_request { |
|
|
1561 | my ($self) = @_; |
|
|
1562 | |
|
|
1563 | ($self->{req_w}, $self->{req_h}) |
|
|
1564 | } |
|
|
1565 | |
|
|
1566 | sub size_allocate { |
2205 | sub size_allocate { |
1567 | my ($self, $x, $y, $w, $h) = @_; |
2206 | my ($self, $w, $h) = @_; |
1568 | |
2207 | |
1569 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
2208 | $self->SUPER::size_allocate ($w, $h); |
1570 | |
2209 | |
|
|
2210 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1571 | $self->{layout}->set_height ($self->{fontsize}); |
2211 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1572 | $self->{layout}->set_width ($self->{w}); |
2212 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1573 | |
2213 | |
1574 | $self->reflow; |
2214 | $self->reflow; |
1575 | } |
2215 | } |
1576 | |
2216 | |
1577 | sub add_paragraph { |
2217 | sub add_paragraph { |
… | |
… | |
1596 | |
2236 | |
1597 | return unless $self->{h} > 0; |
2237 | return unless $self->{h} > 0; |
1598 | |
2238 | |
1599 | delete $self->{texture}; |
2239 | delete $self->{texture}; |
1600 | |
2240 | |
1601 | $ROOT->on_refresh ($self, sub { |
2241 | $ROOT->on_post_alloc ($self, sub { |
1602 | if (delete $self->{need_reflow}) { |
2242 | if (delete $self->{need_reflow}) { |
1603 | my $height = 0; |
2243 | my $height = 0; |
1604 | |
2244 | |
1605 | $height += $_->[0] = $self->text_height ($_->[2]) |
2245 | $height += $_->[0] = $self->text_height ($_->[2]) |
1606 | for @{$self->{par}}; |
2246 | for @{$self->{par}}; |
… | |
… | |
1610 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
2250 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
1611 | |
2251 | |
1612 | delete $self->{texture}; |
2252 | delete $self->{texture}; |
1613 | } |
2253 | } |
1614 | |
2254 | |
1615 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
2255 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
1616 | glClearColor 0, 0, 0, 1; |
2256 | glClearColor 0, 0, 0, 0; |
1617 | glClear GL_COLOR_BUFFER_BIT; |
2257 | glClear GL_COLOR_BUFFER_BIT; |
1618 | |
2258 | |
1619 | glEnable GL_BLEND; |
|
|
1620 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1621 | glEnable GL_TEXTURE_2D; |
2259 | glEnable GL_TEXTURE_2D; |
1622 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2260 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1623 | |
2261 | |
1624 | my $top = int $self->{children}[1]{range}[0]; |
2262 | my $top = int $self->{children}[1]{range}[0]; |
1625 | |
2263 | |
… | |
… | |
1628 | |
2266 | |
1629 | my $y = 0; |
2267 | my $y = 0; |
1630 | |
2268 | |
1631 | my $layout = $self->{layout}; |
2269 | my $layout = $self->{layout}; |
1632 | |
2270 | |
|
|
2271 | $layout->set_font ($self->{font}) if $self->{font}; |
|
|
2272 | |
1633 | for my $par (@{$self->{par}}) { |
2273 | for my $par (@{$self->{par}}) { |
1634 | my $h = $par->[0]; |
2274 | my $h = $par->[0]; |
1635 | |
2275 | |
1636 | if ($y0 < $y + $h && $y < $y1) { |
2276 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2277 | $layout->set_foreground (@{ $par->[1] }); |
1637 | $layout->set_text ($par->[2]); |
2278 | $layout->set_markup ($par->[2]); |
1638 | |
2279 | |
1639 | glColor @{ $par->[1] }; |
|
|
1640 | my ($W, $H) = $layout->size; |
2280 | my ($W, $H) = $layout->size; |
1641 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
2281 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
1642 | } |
2282 | } |
1643 | |
2283 | |
1644 | $y += $h; |
2284 | $y += $h; |
1645 | } |
2285 | } |
1646 | |
2286 | |
1647 | glDisable GL_TEXTURE_2D; |
2287 | glDisable GL_TEXTURE_2D; |
1648 | glDisable GL_BLEND; |
|
|
1649 | }; |
2288 | }; |
1650 | }); |
2289 | }); |
1651 | } |
2290 | } |
1652 | |
2291 | |
1653 | sub _draw { |
2292 | sub _draw { |
1654 | my ($self) = @_; |
2293 | my ($self) = @_; |
1655 | |
2294 | |
1656 | if ($self->{texture}) { |
|
|
1657 | glEnable GL_TEXTURE_2D; |
2295 | glEnable GL_TEXTURE_2D; |
1658 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2296 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1659 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
2297 | glColor 1, 1, 1, 1; |
|
|
2298 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
1660 | glDisable GL_TEXTURE_2D; |
2299 | glDisable GL_TEXTURE_2D; |
1661 | } |
|
|
1662 | |
2300 | |
1663 | $self->{children}[1]->draw; |
2301 | $self->{children}[1]->draw; |
1664 | |
2302 | |
1665 | } |
2303 | } |
1666 | |
2304 | |
1667 | ############################################################################# |
2305 | ############################################################################# |
1668 | |
2306 | |
1669 | package CFClient::UI::MapWidget; |
|
|
1670 | |
|
|
1671 | use strict; |
|
|
1672 | |
|
|
1673 | use List::Util qw(min max); |
|
|
1674 | |
|
|
1675 | use SDL; |
|
|
1676 | use SDL::OpenGL; |
|
|
1677 | |
|
|
1678 | our @ISA = CFClient::UI::Base::; |
|
|
1679 | |
|
|
1680 | sub new { |
|
|
1681 | my $class = shift; |
|
|
1682 | |
|
|
1683 | $class->SUPER::new ( |
|
|
1684 | z => -1, |
|
|
1685 | can_focus => 1, |
|
|
1686 | list => (glGenLists 1), |
|
|
1687 | @_ |
|
|
1688 | ) |
|
|
1689 | } |
|
|
1690 | |
|
|
1691 | sub key_down { |
|
|
1692 | print "MAPKEYDOWN\n"; |
|
|
1693 | } |
|
|
1694 | |
|
|
1695 | sub key_up { |
|
|
1696 | } |
|
|
1697 | |
|
|
1698 | sub button_down { |
|
|
1699 | my ($self, $ev, $x, $y) = @_; |
|
|
1700 | |
|
|
1701 | $self->focus_in; |
|
|
1702 | |
|
|
1703 | if ($ev->button == 2) { |
|
|
1704 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1705 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1706 | |
|
|
1707 | $self->{motion} = sub { |
|
|
1708 | my ($ev, $x, $y) = @_; |
|
|
1709 | |
|
|
1710 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1711 | |
|
|
1712 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1713 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1714 | |
|
|
1715 | $self->update; |
|
|
1716 | }; |
|
|
1717 | } |
|
|
1718 | } |
|
|
1719 | |
|
|
1720 | sub button_up { |
|
|
1721 | my ($self, $ev, $x, $y) = @_; |
|
|
1722 | |
|
|
1723 | delete $self->{motion}; |
|
|
1724 | } |
|
|
1725 | |
|
|
1726 | sub mouse_motion { |
|
|
1727 | my ($self, $ev, $x, $y) = @_; |
|
|
1728 | |
|
|
1729 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1730 | } |
|
|
1731 | |
|
|
1732 | sub size_request { |
|
|
1733 | ( |
|
|
1734 | 1 + 32 * int $::WIDTH / 32, |
|
|
1735 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1736 | ) |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | sub update { |
|
|
1740 | my ($self) = @_; |
|
|
1741 | |
|
|
1742 | $self->{need_update} = 1; |
|
|
1743 | $self->SUPER::update; |
|
|
1744 | } |
|
|
1745 | |
|
|
1746 | sub draw { |
|
|
1747 | my ($self) = @_; |
|
|
1748 | |
|
|
1749 | if (delete $self->{need_update}) { |
|
|
1750 | glNewList $self->{list}, GL_COMPILE; |
|
|
1751 | |
|
|
1752 | if ($::MAP) { |
|
|
1753 | my $sw = int $::WIDTH / 32; |
|
|
1754 | my $sh = int $::HEIGHT / 32; |
|
|
1755 | |
|
|
1756 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1757 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1758 | |
|
|
1759 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1760 | |
|
|
1761 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1762 | |
|
|
1763 | if ($::CFG->{fow_enable}) { |
|
|
1764 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1765 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1766 | glConvolutionFilter2D |
|
|
1767 | GL_CONVOLUTION_2D, |
|
|
1768 | GL_ALPHA, |
|
|
1769 | 3, 3, |
|
|
1770 | GL_ALPHA, GL_FLOAT, |
|
|
1771 | pack "f*", |
|
|
1772 | 0.1, 0.1, 0.1, |
|
|
1773 | 0.1, 0.2, 0.1, |
|
|
1774 | 0.1, 0.1, 0.1, |
|
|
1775 | ; |
|
|
1776 | glEnable GL_CONVOLUTION_2D; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | my $tex = new CFClient::Texture |
|
|
1780 | w => $w, |
|
|
1781 | h => $h, |
|
|
1782 | data => $data, |
|
|
1783 | internalformat => GL_ALPHA, |
|
|
1784 | format => GL_ALPHA; |
|
|
1785 | |
|
|
1786 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1787 | |
|
|
1788 | glEnable GL_BLEND; |
|
|
1789 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1790 | glEnable GL_TEXTURE_2D; |
|
|
1791 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1792 | |
|
|
1793 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1794 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1795 | |
|
|
1796 | glDisable GL_TEXTURE_2D; |
|
|
1797 | glDisable GL_BLEND; |
|
|
1798 | } |
|
|
1799 | |
|
|
1800 | # HACK BEGIN |
|
|
1801 | { |
|
|
1802 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1803 | my ($w, $h) = (250, 250); |
|
|
1804 | |
|
|
1805 | glEnable GL_BLEND; |
|
|
1806 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1807 | glEnable GL_TEXTURE_2D; |
|
|
1808 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1809 | |
|
|
1810 | CFClient::Texture->new ( |
|
|
1811 | w => $w, |
|
|
1812 | h => $h, |
|
|
1813 | data => $::MAP->mapmap ($w, $h), |
|
|
1814 | type => GL_UNSIGNED_INT_8_8_8_8_REV |
|
|
1815 | )->draw_quad (100, 100); |
|
|
1816 | |
|
|
1817 | glDisable GL_TEXTURE_2D; |
|
|
1818 | glDisable GL_BLEND; |
|
|
1819 | } |
|
|
1820 | # HACK END |
|
|
1821 | } |
|
|
1822 | |
|
|
1823 | glEndList; |
|
|
1824 | } |
|
|
1825 | |
|
|
1826 | glPushMatrix; |
|
|
1827 | glCallList $self->{list}; |
|
|
1828 | glPopMatrix; |
|
|
1829 | |
|
|
1830 | if ($FOCUS != $self) { |
|
|
1831 | glColor 64/255, 64/255, 64/255; |
|
|
1832 | glLogicOp GL_AND; |
|
|
1833 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1834 | glBegin GL_QUADS; |
|
|
1835 | glVertex 0, 0; |
|
|
1836 | glVertex 0, $::HEIGHT; |
|
|
1837 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1838 | glVertex $::WIDTH, 0; |
|
|
1839 | glEnd; |
|
|
1840 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1841 | } |
|
|
1842 | } |
|
|
1843 | |
|
|
1844 | my %DIR = ( |
|
|
1845 | SDLK_KP8, [1, "north"], |
|
|
1846 | SDLK_KP9, [2, "northeast"], |
|
|
1847 | SDLK_KP6, [3, "east"], |
|
|
1848 | SDLK_KP3, [4, "southeast"], |
|
|
1849 | SDLK_KP2, [5, "south"], |
|
|
1850 | SDLK_KP1, [6, "southwest"], |
|
|
1851 | SDLK_KP4, [7, "west"], |
|
|
1852 | SDLK_KP7, [8, "northwest"], |
|
|
1853 | |
|
|
1854 | SDLK_UP, [1, "north"], |
|
|
1855 | SDLK_RIGHT, [3, "east"], |
|
|
1856 | SDLK_DOWN, [5, "south"], |
|
|
1857 | SDLK_LEFT, [7, "west"], |
|
|
1858 | ); |
|
|
1859 | |
|
|
1860 | sub key_down { |
|
|
1861 | my ($self, $ev) = @_; |
|
|
1862 | |
|
|
1863 | my $mod = $ev->key_mod; |
|
|
1864 | my $sym = $ev->key_sym; |
|
|
1865 | |
|
|
1866 | if ($sym == SDLK_KP5) { |
|
|
1867 | $::CONN->user_send ("command stay fire"); |
|
|
1868 | } elsif ($sym == SDLK_a) { |
|
|
1869 | $::CONN->user_send ("command apply"); |
|
|
1870 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1871 | $self->emit ('activate_console'); |
|
|
1872 | } elsif ($sym == SDLK_SLASH) { |
|
|
1873 | $self->emit ('activate_console' => '/'); |
|
|
1874 | } elsif (exists $DIR{$sym}) { |
|
|
1875 | if ($mod & KMOD_SHIFT) { |
|
|
1876 | $self->{shft}++; |
|
|
1877 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1878 | } elsif ($mod & KMOD_CTRL) { |
|
|
1879 | $self->{ctrl}++; |
|
|
1880 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1881 | } else { |
|
|
1882 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1883 | } |
|
|
1884 | } |
|
|
1885 | } |
|
|
1886 | |
|
|
1887 | sub key_up { |
|
|
1888 | my ($self, $ev) = @_; |
|
|
1889 | |
|
|
1890 | my $mod = $ev->key_mod; |
|
|
1891 | my $sym = $ev->key_sym; |
|
|
1892 | |
|
|
1893 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1894 | $::CONN->user_send ("command fire_stop"); |
|
|
1895 | } |
|
|
1896 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1897 | $::CONN->user_send ("command run_stop"); |
|
|
1898 | } |
|
|
1899 | } |
|
|
1900 | |
|
|
1901 | ############################################################################# |
|
|
1902 | |
|
|
1903 | package CFClient::UI::Animator; |
2307 | package CFClient::UI::Animator; |
1904 | |
2308 | |
1905 | use SDL::OpenGL; |
2309 | use CFClient::OpenGL; |
1906 | |
2310 | |
1907 | our @ISA = CFClient::UI::Bin::; |
2311 | our @ISA = CFClient::UI::Bin::; |
1908 | |
2312 | |
1909 | sub moveto { |
2313 | sub moveto { |
1910 | my ($self, $x, $y) = @_; |
2314 | my ($self, $x, $y) = @_; |
… | |
… | |
1948 | |
2352 | |
1949 | sub new { |
2353 | sub new { |
1950 | my $class = shift; |
2354 | my $class = shift; |
1951 | |
2355 | |
1952 | my $self = $class->SUPER::new ( |
2356 | my $self = $class->SUPER::new ( |
1953 | state => 0, |
2357 | state => 0, |
1954 | connect_activate => \&toggle_flopper, |
2358 | connect_activate => \&toggle_flopper, |
1955 | @_ |
2359 | @_ |
1956 | ); |
2360 | ); |
1957 | |
2361 | |
1958 | if ($self->{state}) { |
2362 | if ($self->{state}) { |
… | |
… | |
1979 | $self->emit (changed => $self->{state}); |
2383 | $self->emit (changed => $self->{state}); |
1980 | } |
2384 | } |
1981 | |
2385 | |
1982 | ############################################################################# |
2386 | ############################################################################# |
1983 | |
2387 | |
|
|
2388 | package CFClient::UI::Tooltip; |
|
|
2389 | |
|
|
2390 | our @ISA = CFClient::UI::Bin::; |
|
|
2391 | |
|
|
2392 | use CFClient::OpenGL; |
|
|
2393 | |
|
|
2394 | sub new { |
|
|
2395 | my $class = shift; |
|
|
2396 | |
|
|
2397 | $class->SUPER::new ( |
|
|
2398 | @_, |
|
|
2399 | can_events => 0, |
|
|
2400 | ) |
|
|
2401 | } |
|
|
2402 | |
|
|
2403 | sub set_tooltip_from { |
|
|
2404 | my ($self, $widget) = @_; |
|
|
2405 | |
|
|
2406 | $self->add (new CFClient::UI::Label |
|
|
2407 | markup => $widget->{tooltip}, |
|
|
2408 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
2409 | fontsize => 0.8, |
|
|
2410 | fg => [0, 0, 0, 1], |
|
|
2411 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
|
|
2412 | ); |
|
|
2413 | } |
|
|
2414 | |
|
|
2415 | sub size_request { |
|
|
2416 | my ($self) = @_; |
|
|
2417 | |
|
|
2418 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2419 | |
|
|
2420 | ($w + 4, $h + 4) |
|
|
2421 | } |
|
|
2422 | |
|
|
2423 | sub size_allocate { |
|
|
2424 | my ($self, $w, $h) = @_; |
|
|
2425 | |
|
|
2426 | $self->SUPER::size_allocate ($w - 4, $h - 4); |
|
|
2427 | } |
|
|
2428 | |
|
|
2429 | sub _draw { |
|
|
2430 | my ($self) = @_; |
|
|
2431 | |
|
|
2432 | glTranslate 0.375, 0.375; |
|
|
2433 | |
|
|
2434 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2435 | |
|
|
2436 | glColor 1, 0.8, 0.4; |
|
|
2437 | glBegin GL_QUADS; |
|
|
2438 | glVertex 0 , 0; |
|
|
2439 | glVertex 0 , $h; |
|
|
2440 | glVertex $w, $h; |
|
|
2441 | glVertex $w, 0; |
|
|
2442 | glEnd; |
|
|
2443 | |
|
|
2444 | glColor 0, 0, 0; |
|
|
2445 | glBegin GL_LINE_LOOP; |
|
|
2446 | glVertex 0 , 0; |
|
|
2447 | glVertex 0 , $h; |
|
|
2448 | glVertex $w, $h; |
|
|
2449 | glVertex $w, 0; |
|
|
2450 | glEnd; |
|
|
2451 | |
|
|
2452 | glTranslate 2 - 0.375, 2 - 0.375; |
|
|
2453 | $self->SUPER::_draw; |
|
|
2454 | } |
|
|
2455 | |
|
|
2456 | ############################################################################# |
|
|
2457 | |
|
|
2458 | package CFClient::UI::Face; |
|
|
2459 | |
|
|
2460 | our @ISA = CFClient::UI::Base::; |
|
|
2461 | |
|
|
2462 | use CFClient::OpenGL; |
|
|
2463 | |
|
|
2464 | sub new { |
|
|
2465 | my $class = shift; |
|
|
2466 | |
|
|
2467 | $class->SUPER::new ( |
|
|
2468 | aspect => 1, |
|
|
2469 | @_, |
|
|
2470 | ) |
|
|
2471 | } |
|
|
2472 | |
|
|
2473 | sub size_request { |
|
|
2474 | (32, 8) |
|
|
2475 | } |
|
|
2476 | |
|
|
2477 | sub _draw { |
|
|
2478 | my ($self) = @_; |
|
|
2479 | |
|
|
2480 | return unless $::CONN;#d# manage and cache textures differently |
|
|
2481 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
|
|
2482 | |
|
|
2483 | # TODO animation |
|
|
2484 | if ($tex) { |
|
|
2485 | glEnable GL_TEXTURE_2D; |
|
|
2486 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2487 | glColor 1, 1, 1, 1; |
|
|
2488 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
|
|
2489 | glDisable GL_TEXTURE_2D; |
|
|
2490 | } |
|
|
2491 | } |
|
|
2492 | |
|
|
2493 | ############################################################################# |
|
|
2494 | |
|
|
2495 | package CFClient::UI::InventoryItem; |
|
|
2496 | |
|
|
2497 | our @ISA = CFClient::UI::HBox::; |
|
|
2498 | |
|
|
2499 | sub new { |
|
|
2500 | my $class = shift; |
|
|
2501 | |
|
|
2502 | my %args = @_; |
|
|
2503 | |
|
|
2504 | my $item = delete $args{item}; |
|
|
2505 | |
|
|
2506 | my $desc = $item->{nrof} < 2 |
|
|
2507 | ? $item->{name} |
|
|
2508 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2509 | |
|
|
2510 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2511 | and $desc .= " (open)"; |
|
|
2512 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2513 | and $desc .= " (applied)"; |
|
|
2514 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2515 | and $desc .= " (unpaid)"; |
|
|
2516 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2517 | and $desc .= " (magic)"; |
|
|
2518 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2519 | and $desc .= " (cursed)"; |
|
|
2520 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2521 | and $desc .= " (damned)"; |
|
|
2522 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2523 | and $desc .= " *"; |
|
|
2524 | |
|
|
2525 | my $self = $class->SUPER::new ( |
|
|
2526 | can_hover => 1, |
|
|
2527 | can_events => 1, |
|
|
2528 | tooltip => (CFClient::UI::Label->escape ($desc) |
|
|
2529 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2530 | connect_button_down => sub { |
|
|
2531 | my ($self, $ev, $x, $y) = @_; |
|
|
2532 | |
|
|
2533 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2534 | if ($ev->{button} == 1) { |
|
|
2535 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
|
|
2536 | } elsif ($ev->{button} == 2) { |
|
|
2537 | $::CONN->send ("apply $item->{tag}"); |
|
|
2538 | } elsif ($ev->{button} == 3) { |
|
|
2539 | my @menu_items = ( |
|
|
2540 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2541 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2542 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2543 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
|
|
2544 | [ |
|
|
2545 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
|
|
2546 | sub { $::CONN->send ("lock ". () ."$item->{tag}") }, |
|
|
2547 | ], |
|
|
2548 | ); |
|
|
2549 | |
|
|
2550 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
|
|
2551 | } |
|
|
2552 | |
|
|
2553 | 1 |
|
|
2554 | }, |
|
|
2555 | %args |
|
|
2556 | ); |
|
|
2557 | |
|
|
2558 | $self->add (new CFClient::UI::Face |
|
|
2559 | can_events => 0, |
|
|
2560 | face => $item->{face}, |
|
|
2561 | anim => $item->{anim}, |
|
|
2562 | animspeed => $item->{animspeed}, |
|
|
2563 | ); |
|
|
2564 | |
|
|
2565 | $self->add (new CFClient::UI::Label |
|
|
2566 | can_events => 0, |
|
|
2567 | text => $desc, |
|
|
2568 | ); |
|
|
2569 | |
|
|
2570 | $self |
|
|
2571 | } |
|
|
2572 | |
|
|
2573 | ############################################################################# |
|
|
2574 | |
|
|
2575 | package CFClient::UI::Inventory; |
|
|
2576 | |
|
|
2577 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2578 | |
|
|
2579 | sub new { |
|
|
2580 | my $class = shift; |
|
|
2581 | |
|
|
2582 | my $self = $class->SUPER::new ( |
|
|
2583 | scrolled => (new CFClient::UI::VBox), |
|
|
2584 | @_, |
|
|
2585 | ); |
|
|
2586 | |
|
|
2587 | $self |
|
|
2588 | } |
|
|
2589 | |
|
|
2590 | sub set_items { |
|
|
2591 | my ($self, $items) = @_; |
|
|
2592 | |
|
|
2593 | $self->{scrolled}->clear; |
|
|
2594 | return unless $items; |
|
|
2595 | |
|
|
2596 | my @items = sort { |
|
|
2597 | ($a->{type} <=> $b->{type}) |
|
|
2598 | or ($a->{name} cmp $b->{name}) |
|
|
2599 | } @$items; |
|
|
2600 | |
|
|
2601 | $self->{real_items} = \@items; |
|
|
2602 | |
|
|
2603 | for my $item (@items) { |
|
|
2604 | my $desc = $item->{nrof} < 2 |
|
|
2605 | ? $item->{name} |
|
|
2606 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2607 | |
|
|
2608 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
|
|
2609 | } |
|
|
2610 | |
|
|
2611 | $self->{scrolled}->add (@items); |
|
|
2612 | |
|
|
2613 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2614 | } |
|
|
2615 | |
|
|
2616 | sub size_request { |
|
|
2617 | my ($self) = @_; |
|
|
2618 | ($self->{req_w}, $self->{req_h}); |
|
|
2619 | } |
|
|
2620 | |
|
|
2621 | ############################################################################# |
|
|
2622 | |
|
|
2623 | package CFClient::UI::Menu; |
|
|
2624 | |
|
|
2625 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2626 | |
|
|
2627 | use CFClient::OpenGL; |
|
|
2628 | |
|
|
2629 | sub new { |
|
|
2630 | my $class = shift; |
|
|
2631 | |
|
|
2632 | my $self = $class->SUPER::new ( |
|
|
2633 | items => [], |
|
|
2634 | z => 100, |
|
|
2635 | @_, |
|
|
2636 | ); |
|
|
2637 | |
|
|
2638 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2639 | |
|
|
2640 | for my $item (@{ $self->{items} }) { |
|
|
2641 | my ($widget, $cb) = @$item; |
|
|
2642 | |
|
|
2643 | # handle various types of items, only text for now |
|
|
2644 | if (!ref $widget) { |
|
|
2645 | $widget = new CFClient::UI::Label |
|
|
2646 | can_hover => 1, |
|
|
2647 | can_events => 1, |
|
|
2648 | text => $widget; |
|
|
2649 | } |
|
|
2650 | |
|
|
2651 | $self->{item}{$widget} = $item; |
|
|
2652 | |
|
|
2653 | $self->{vbox}->add ($widget); |
|
|
2654 | } |
|
|
2655 | |
|
|
2656 | $self |
|
|
2657 | } |
|
|
2658 | |
|
|
2659 | # popup given the event (must be a mouse button down event currently) |
|
|
2660 | sub popup { |
|
|
2661 | my ($self, $ev) = @_; |
|
|
2662 | |
|
|
2663 | $self->emit ("popdown"); |
|
|
2664 | |
|
|
2665 | # maybe save $GRAB? must be careful about events... |
|
|
2666 | $GRAB = $self; |
|
|
2667 | $self->{button} = $ev->{button}; |
|
|
2668 | |
|
|
2669 | $self->show; |
|
|
2670 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2671 | } |
|
|
2672 | |
|
|
2673 | sub mouse_motion { |
|
|
2674 | my ($self, $ev, $x, $y) = @_; |
|
|
2675 | |
|
|
2676 | # TODO: should use vbox->find_widget or so |
|
|
2677 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2678 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2679 | } |
|
|
2680 | |
|
|
2681 | sub button_up { |
|
|
2682 | my ($self, $ev, $x, $y) = @_; |
|
|
2683 | |
|
|
2684 | if ($ev->{button} == $self->{button}) { |
|
|
2685 | undef $GRAB; |
|
|
2686 | $self->hide; |
|
|
2687 | |
|
|
2688 | $self->emit ("popdown"); |
|
|
2689 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2690 | } |
|
|
2691 | } |
|
|
2692 | |
|
|
2693 | ############################################################################# |
|
|
2694 | |
|
|
2695 | package CFClient::UI::Statusbox; |
|
|
2696 | |
|
|
2697 | our @ISA = CFClient::UI::VBox::; |
|
|
2698 | |
|
|
2699 | sub reorder { |
|
|
2700 | my ($self) = @_; |
|
|
2701 | my $NOW = time; |
|
|
2702 | |
|
|
2703 | while (my ($k, $v) = each %{ $self->{item} }) { |
|
|
2704 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
|
|
2705 | } |
|
|
2706 | |
|
|
2707 | my @widgets; |
|
|
2708 | |
|
|
2709 | my @items = sort { |
|
|
2710 | $a->{pri} <=> $b->{pri} |
|
|
2711 | or $b->{id} <=> $a->{id} |
|
|
2712 | } values %{ $self->{item} }; |
|
|
2713 | |
|
|
2714 | my $count = 10 + 1; |
|
|
2715 | for my $item (@items) { |
|
|
2716 | last unless --$count; |
|
|
2717 | |
|
|
2718 | push @widgets, $item->{label} ||= do { |
|
|
2719 | # TODO: doesn't handle markup well (read as: at all) |
|
|
2720 | my $short = $item->{count} > 1 |
|
|
2721 | ? "<b>$item->{count} ×</b> $item->{text}" |
|
|
2722 | : $item->{text}; |
|
|
2723 | |
|
|
2724 | for ($short) { |
|
|
2725 | s/^\s+//; |
|
|
2726 | s/\s+/ /g; |
|
|
2727 | my $len = int 40 / $item->{fontsize}; |
|
|
2728 | substr $_, $len, length, "…" if $len < length; |
|
|
2729 | } |
|
|
2730 | |
|
|
2731 | new CFClient::UI::Label |
|
|
2732 | markup => $short, |
|
|
2733 | tooltip => $item->{tooltip}, |
|
|
2734 | tooltip_font => $::FONT_PROP, |
|
|
2735 | tooltip_width => 0.67, |
|
|
2736 | fontsize => $item->{fontsize}, |
|
|
2737 | fg => $item->{fg}, |
|
|
2738 | can_events => 1, |
|
|
2739 | can_hover => 1 |
|
|
2740 | }; |
|
|
2741 | } |
|
|
2742 | |
|
|
2743 | $self->clear; |
|
|
2744 | $self->SUPER::add (reverse @widgets); |
|
|
2745 | } |
|
|
2746 | |
|
|
2747 | sub add { |
|
|
2748 | my ($self, $text, %arg) = @_; |
|
|
2749 | |
|
|
2750 | $text =~ s/^\s+//; |
|
|
2751 | $text =~ s/\s+$//; |
|
|
2752 | |
|
|
2753 | my $timeout = time + ((delete $arg{timeout}) || 60); |
|
|
2754 | |
|
|
2755 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
|
|
2756 | |
|
|
2757 | if (my $item = $self->{item}{$group}) { |
|
|
2758 | if ($item->{text} eq $text) { |
|
|
2759 | $item->{count}++; |
|
|
2760 | } else { |
|
|
2761 | $item->{count} = 1; |
|
|
2762 | $item->{text} = $item->{tooltip} = $text; |
|
|
2763 | } |
|
|
2764 | $item->{id} = ++$self->{id}; |
|
|
2765 | $item->{timeout} = $timeout; |
|
|
2766 | delete $item->{label}; |
|
|
2767 | } else { |
|
|
2768 | $self->{item}{$group} = { |
|
|
2769 | id => ++$self->{id}, |
|
|
2770 | text => $text, |
|
|
2771 | timeout => $timeout, |
|
|
2772 | tooltip => $text, |
|
|
2773 | fontsize => 0.8, |
|
|
2774 | fg => [0.8, 0.8, 0.8, 0.8], |
|
|
2775 | pri => 0, |
|
|
2776 | count => 1, |
|
|
2777 | %arg, |
|
|
2778 | }; |
|
|
2779 | } |
|
|
2780 | |
|
|
2781 | $self->reorder; |
|
|
2782 | } |
|
|
2783 | |
|
|
2784 | ############################################################################# |
|
|
2785 | |
1984 | package CFClient::UI::Root; |
2786 | package CFClient::UI::Root; |
1985 | |
2787 | |
1986 | our @ISA = CFClient::UI::Container::; |
2788 | our @ISA = CFClient::UI::Container::; |
1987 | |
2789 | |
1988 | use SDL::OpenGL; |
2790 | use CFClient::OpenGL; |
|
|
2791 | |
|
|
2792 | sub new { |
|
|
2793 | my $class = shift; |
|
|
2794 | |
|
|
2795 | $class->SUPER::new ( |
|
|
2796 | @_, |
|
|
2797 | ) |
|
|
2798 | } |
|
|
2799 | |
|
|
2800 | sub configure { |
|
|
2801 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2802 | |
|
|
2803 | $self->{w} = $w; |
|
|
2804 | $self->{h} = $h; |
|
|
2805 | } |
|
|
2806 | |
|
|
2807 | sub check_size { |
|
|
2808 | my ($self) = @_; |
|
|
2809 | |
|
|
2810 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2811 | if $self->{w}; |
|
|
2812 | } |
1989 | |
2813 | |
1990 | sub size_request { |
2814 | sub size_request { |
1991 | ($::WIDTH, $::HEIGHT) |
2815 | my ($self) = @_; |
|
|
2816 | |
|
|
2817 | ($self->{w}, $self->{h}) |
1992 | } |
2818 | } |
1993 | |
2819 | |
1994 | sub size_allocate { |
2820 | sub size_allocate { |
1995 | my ($self, $x, $y, $w, $h) = @_; |
2821 | my ($self, $w, $h) = @_; |
1996 | |
2822 | |
1997 | $self->_size_allocate ($x, $y, $w, $h); |
2823 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
|
|
2824 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
1998 | |
2825 | |
1999 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
2826 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
2000 | for @{$self->{children}}; |
2827 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
2001 | } |
|
|
2002 | |
2828 | |
2003 | sub _topleft { |
2829 | for my $child ($self->children) { |
|
|
2830 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2831 | |
|
|
2832 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
|
|
2833 | if exists $child->{req_x}; |
|
|
2834 | |
|
|
2835 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
|
|
2836 | if exists $child->{req_y}; |
|
|
2837 | |
|
|
2838 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
|
|
2839 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
|
|
2840 | |
|
|
2841 | $child->configure ($X, $Y, $W, $H); |
|
|
2842 | } |
|
|
2843 | } |
|
|
2844 | |
|
|
2845 | sub coord2local { |
2004 | my ($self, $x, $y) = @_; |
2846 | my ($self, $x, $y) = @_; |
2005 | |
2847 | |
2006 | ($x, $y) |
2848 | ($x, $y) |
2007 | } |
2849 | } |
2008 | |
2850 | |
|
|
2851 | sub coord2global { |
|
|
2852 | my ($self, $x, $y) = @_; |
|
|
2853 | |
|
|
2854 | ($x, $y) |
|
|
2855 | } |
|
|
2856 | |
2009 | sub update { |
2857 | sub update { |
2010 | my ($self) = @_; |
2858 | my ($self) = @_; |
2011 | |
2859 | |
2012 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2860 | $self->check_size; |
2013 | ::refresh (); |
2861 | $::WANT_REFRESH++; |
2014 | } |
2862 | } |
2015 | |
2863 | |
2016 | sub add { |
2864 | sub add { |
2017 | my ($self, $widget) = @_; |
2865 | my ($self, @children) = @_; |
2018 | |
2866 | |
|
|
2867 | for my $child (@children) { |
|
|
2868 | $child->{toplevel} = 1; |
|
|
2869 | |
|
|
2870 | # integerise window positions |
|
|
2871 | $child->{x} = int $child->{x}; |
|
|
2872 | $child->{y} = int $child->{y}; |
|
|
2873 | } |
|
|
2874 | |
2019 | $self->SUPER::add ($widget); |
2875 | $self->SUPER::add (@children); |
2020 | |
|
|
2021 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
2022 | } |
2876 | } |
2023 | |
2877 | |
2024 | sub on_refresh { |
2878 | sub on_refresh { |
2025 | my ($self, $id, $cb) = @_; |
2879 | my ($self, $id, $cb) = @_; |
2026 | |
2880 | |
2027 | $self->{refresh_hook}{$id} = $cb; |
2881 | $self->{refresh_hook}{$id} = $cb; |
2028 | } |
2882 | } |
2029 | |
2883 | |
|
|
2884 | sub on_post_alloc { |
|
|
2885 | my ($self, $id, $cb) = @_; |
|
|
2886 | |
|
|
2887 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2888 | } |
|
|
2889 | |
2030 | sub draw { |
2890 | sub draw { |
2031 | my ($self) = @_; |
2891 | my ($self) = @_; |
2032 | |
2892 | |
2033 | while (my $rcb = delete $self->{refresh_hook}) { |
2893 | while ($self->{refresh_hook}) { |
2034 | $_->() for values %$rcb; |
2894 | $_->() |
|
|
2895 | for values %{delete $self->{refresh_hook}}; |
|
|
2896 | } |
|
|
2897 | |
|
|
2898 | if ($self->{check_size}) { |
|
|
2899 | my @queue = ([], []); |
|
|
2900 | |
|
|
2901 | for (;;) { |
|
|
2902 | if ($self->{check_size}) { |
|
|
2903 | # heuristic: check containers last |
|
|
2904 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2905 | for values %{delete $self->{check_size}} |
|
|
2906 | } |
|
|
2907 | |
|
|
2908 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2909 | |
|
|
2910 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2911 | ? @$widget{qw(user_w user_h)} |
|
|
2912 | : $widget->size_request; |
|
|
2913 | |
|
|
2914 | if (delete $widget->{force_alloc} |
|
|
2915 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
|
|
2916 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
2917 | |
|
|
2918 | $widget->{req_w} = $w; |
|
|
2919 | $widget->{req_h} = $h; |
|
|
2920 | |
|
|
2921 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
|
|
2922 | |
|
|
2923 | $widget->{parent}->check_size |
|
|
2924 | if $widget->{parent}; |
|
|
2925 | } |
|
|
2926 | } |
|
|
2927 | } |
|
|
2928 | |
|
|
2929 | while ($self->{size_alloc}) { |
|
|
2930 | for (values %{delete $self->{size_alloc}}) { |
|
|
2931 | my ($widget, $w, $h) = @$_; |
|
|
2932 | |
|
|
2933 | $w = 0 if $w < 0; |
|
|
2934 | $h = 0 if $h < 0; |
|
|
2935 | |
|
|
2936 | $widget->{w} = $w; |
|
|
2937 | $widget->{h} = $h; |
|
|
2938 | $widget->size_allocate ($w, $h); |
|
|
2939 | $widget->emit (size_allocate => $w, $h); |
|
|
2940 | } |
|
|
2941 | } |
|
|
2942 | |
|
|
2943 | while ($self->{post_alloc_hook}) { |
|
|
2944 | $_->() |
|
|
2945 | for values %{delete $self->{post_alloc_hook}}; |
2035 | } |
2946 | } |
2036 | |
2947 | |
2037 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2948 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
2038 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2949 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2039 | glClear GL_COLOR_BUFFER_BIT; |
2950 | glClear GL_COLOR_BUFFER_BIT; |
… | |
… | |
2050 | ############################################################################# |
2961 | ############################################################################# |
2051 | |
2962 | |
2052 | package CFClient::UI; |
2963 | package CFClient::UI; |
2053 | |
2964 | |
2054 | $ROOT = new CFClient::UI::Root; |
2965 | $ROOT = new CFClient::UI::Root; |
|
|
2966 | $TOOLTIP = new CFClient::UI::Tooltip; |
2055 | |
2967 | |
2056 | 1 |
2968 | 1 |
2057 | |
2969 | |