1 | package CFClient::UI; |
1 | package CFClient::UI; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util (); |
|
|
6 | use List::Util (); |
6 | |
7 | |
7 | use CFClient; |
8 | use CFClient; |
8 | |
9 | |
9 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | |
11 | |
… | |
… | |
42 | |
43 | |
43 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
44 | |
45 | |
45 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | |
47 | |
47 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
48 | $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
48 | |
49 | |
49 | if (!$BUTTON_STATE) { |
50 | if (!$BUTTON_STATE) { |
50 | my $grab = $GRAB; undef $GRAB; |
51 | my $grab = $GRAB; undef $GRAB; |
51 | $grab->update if $grab; |
52 | $grab->update if $grab; |
52 | $GRAB->update if $GRAB; |
53 | $GRAB->update if $GRAB; |
… | |
… | |
60 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
61 | |
62 | |
62 | if ($widget != $HOVER) { |
63 | if ($widget != $HOVER) { |
63 | my $hover = $HOVER; $HOVER = $widget; |
64 | my $hover = $HOVER; $HOVER = $widget; |
64 | |
65 | |
65 | $hover->update if $hover; |
66 | $hover->update if $hover && $hover->{can_hover}; |
66 | $HOVER->update if $HOVER; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
67 | } |
68 | } |
68 | |
69 | |
69 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | } |
71 | } |
71 | |
72 | |
… | |
… | |
78 | use SDL::OpenGL; |
79 | use SDL::OpenGL; |
79 | |
80 | |
80 | sub new { |
81 | sub new { |
81 | my $class = shift; |
82 | my $class = shift; |
82 | |
83 | |
83 | bless { |
84 | my $self = bless { |
84 | x => 0, |
85 | x => 0, |
85 | y => 0, |
86 | y => 0, |
86 | z => 0, |
87 | z => 0, |
|
|
88 | w => -1, |
|
|
89 | h => -1, |
87 | @_ |
90 | @_ |
88 | }, $class |
91 | }, $class; |
|
|
92 | |
|
|
93 | for (keys %$self) { |
|
|
94 | if (/^connect_(.*)$/) { |
|
|
95 | $self->connect ($1 => delete $self->{$_}); |
|
|
96 | } |
|
|
97 | } |
|
|
98 | |
|
|
99 | $self |
89 | } |
100 | } |
90 | |
101 | |
91 | sub move { |
102 | sub move { |
92 | my ($self, $x, $y, $z) = @_; |
103 | my ($self, $x, $y, $z) = @_; |
93 | $self->{x} = $x; |
104 | $self->{x} = $x; |
… | |
… | |
102 | sub size_request { |
113 | sub size_request { |
103 | require Carp; |
114 | require Carp; |
104 | Carp::confess "size_request is abtract"; |
115 | Carp::confess "size_request is abtract"; |
105 | } |
116 | } |
106 | |
117 | |
107 | sub size_allocate { |
118 | sub _size_allocate { |
108 | my ($self, $w, $h) = @_; |
119 | my ($self, $x, $y, $w, $h) = @_; |
|
|
120 | |
|
|
121 | $self->{x} = $x; |
|
|
122 | $self->{y} = $y; |
|
|
123 | |
|
|
124 | return unless $self->{w} != $w || $self->{h} != $h; |
109 | |
125 | |
110 | $self->{w} = $w; |
126 | $self->{w} = $w; |
111 | $self->{h} = $h; |
127 | $self->{h} = $h; |
112 | } |
|
|
113 | |
128 | |
|
|
129 | 1 |
|
|
130 | } |
|
|
131 | |
|
|
132 | sub size_allocate { |
|
|
133 | my ($self, $x, $y, $w, $h) = @_; |
|
|
134 | |
|
|
135 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
136 | } |
|
|
137 | |
114 | # translate global koordinates to local coordinate system |
138 | # translate global coordinates to local coordinate system |
115 | sub translate { |
139 | sub translate { |
116 | my ($self, $x, $y) = @_; |
140 | my ($self, $x, $y) = @_; |
117 | |
141 | |
118 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
142 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
119 | } |
143 | } |
120 | |
144 | |
121 | sub focus_in { |
145 | sub focus_in { |
122 | my ($self) = @_; |
146 | my ($self) = @_; |
123 | |
147 | |
124 | return if $FOCUS == $self; |
148 | return if $FOCUS == $self; |
|
|
149 | return unless $self->{can_focus}; |
125 | |
150 | |
126 | my $focus = $FOCUS; $FOCUS = $self; |
151 | my $focus = $FOCUS; $FOCUS = $self; |
127 | $focus->update if $focus; |
152 | $focus->update if $focus; |
128 | $FOCUS->update; |
153 | $FOCUS->update; |
129 | } |
154 | } |
… | |
… | |
162 | glPushMatrix; |
187 | glPushMatrix; |
163 | glTranslate $self->{x}, $self->{y}, 0; |
188 | glTranslate $self->{x}, $self->{y}, 0; |
164 | $self->_draw; |
189 | $self->_draw; |
165 | glPopMatrix; |
190 | glPopMatrix; |
166 | |
191 | |
167 | if ($self == $HOVER) { |
192 | if ($self == $HOVER && $self->{can_hover}) { |
168 | my ($x, $y) = @$self{qw(x y)}; |
193 | my ($x, $y) = @$self{qw(x y)}; |
169 | |
194 | |
170 | glColor 1, 1, 1, 0.1; |
195 | glColor 0, 0, 1, 0.2; |
171 | glEnable GL_BLEND; |
196 | glEnable GL_BLEND; |
|
|
197 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
172 | glBegin GL_QUADS; |
198 | glBegin GL_QUADS; |
173 | glVertex $x , $y; |
199 | glVertex $x , $y; |
174 | glVertex $x + $self->{w}, $y; |
200 | glVertex $x + $self->{w}, $y; |
175 | glVertex $x + $self->{w}, $y + $self->{h}; |
201 | glVertex $x + $self->{w}, $y + $self->{h}; |
176 | glVertex $x , $y + $self->{h}; |
202 | glVertex $x , $y + $self->{h}; |
… | |
… | |
183 | my ($self) = @_; |
209 | my ($self) = @_; |
184 | |
210 | |
185 | warn "no draw defined for $self\n"; |
211 | warn "no draw defined for $self\n"; |
186 | } |
212 | } |
187 | |
213 | |
188 | sub bbox { |
|
|
189 | my ($self) = @_; |
|
|
190 | my ($w, $h) = $self->size_request; |
|
|
191 | ( |
|
|
192 | $self->{x}, |
|
|
193 | $self->{y}, |
|
|
194 | $self->{x} = $w, |
|
|
195 | $self->{y} = $h |
|
|
196 | ) |
|
|
197 | } |
|
|
198 | |
|
|
199 | sub find_widget { |
214 | sub find_widget { |
200 | my ($self, $x, $y) = @_; |
215 | my ($self, $x, $y) = @_; |
201 | |
216 | |
202 | return $self |
217 | return $self |
203 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
218 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
204 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
219 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
205 | |
220 | |
206 | () |
221 | () |
207 | } |
222 | } |
208 | |
223 | |
209 | sub del_parent { $_[0]->{parent} = undef } |
|
|
210 | |
|
|
211 | sub set_parent { |
224 | sub set_parent { |
212 | my ($self, $par) = @_; |
225 | my ($self, $par) = @_; |
213 | |
226 | |
214 | $self->{parent} = $par; |
227 | $self->{parent} = $par; |
215 | Scalar::Util::weaken $self->{parent}; |
228 | Scalar::Util::weaken $self->{parent}; |
… | |
… | |
258 | my $class = shift; |
271 | my $class = shift; |
259 | |
272 | |
260 | # range [value, low, high, page] |
273 | # range [value, low, high, page] |
261 | |
274 | |
262 | $class->SUPER::new ( |
275 | $class->SUPER::new ( |
263 | bg => [0, 0, 0, 0.4], |
276 | bg => [0, 0, 0, 0.2], |
264 | active_bg => [1, 1, 1], |
277 | active_bg => [1, 1, 1, 0.5], |
265 | @_ |
278 | @_ |
266 | ) |
279 | ) |
267 | } |
280 | } |
268 | |
281 | |
269 | sub _draw { |
282 | sub _draw { |
270 | my ($self) = @_; |
283 | my ($self) = @_; |
271 | |
284 | |
272 | my ($w, $h) = @$self{qw(w h)}; |
285 | my ($w, $h) = @$self{qw(w h)}; |
273 | |
286 | |
|
|
287 | glEnable GL_BLEND; |
|
|
288 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
274 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
289 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
290 | |
275 | glBegin GL_QUADS; |
291 | glBegin GL_QUADS; |
276 | glVertex 0 , 0; |
292 | glVertex 0 , 0; |
277 | glVertex 0 , $h; |
293 | glVertex 0 , $h; |
278 | glVertex $w, $h; |
294 | glVertex $w, $h; |
279 | glVertex $w, 0; |
295 | glVertex $w, 0; |
280 | glEnd; |
296 | glEnd; |
|
|
297 | |
|
|
298 | glDisable GL_BLEND; |
281 | } |
299 | } |
282 | |
300 | |
283 | ############################################################################# |
301 | ############################################################################# |
284 | |
302 | |
285 | package CFClient::UI::Empty; |
303 | package CFClient::UI::Empty; |
… | |
… | |
308 | |
326 | |
309 | $self |
327 | $self |
310 | } |
328 | } |
311 | |
329 | |
312 | sub add { |
330 | sub add { |
313 | my ($self, $chld, $expand) = @_; |
331 | my ($self, $chld) = @_; |
314 | |
332 | |
315 | $chld->{expand} = $expand; |
|
|
316 | $chld->set_parent ($self); |
333 | $chld->set_parent ($self); |
317 | |
334 | |
318 | $self->{children} = [ |
335 | $self->{children} = [ |
319 | sort { $a->{z} <=> $b->{z} } |
336 | sort { $a->{z} <=> $b->{z} } |
320 | @{$self->{children}}, $chld |
337 | @{$self->{children}}, $chld |
321 | ]; |
338 | ]; |
322 | |
339 | |
323 | $self->size_allocate ($self->{w}, $self->{h}) |
340 | $self->{w} = $self->{h} = -1; |
324 | if $self->{w}; #TODO: check for "realised state" |
341 | $self->update; |
325 | } |
342 | } |
326 | |
343 | |
327 | sub remove { |
344 | sub remove { |
328 | my ($self, $widget) = @_; |
345 | my ($self, $widget) = @_; |
329 | |
346 | |
330 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
347 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
331 | |
348 | |
332 | $self->size_allocate ($self->{w}, $self->{h}); |
349 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
333 | } |
350 | } |
334 | |
351 | |
335 | sub find_widget { |
352 | sub find_widget { |
336 | my ($self, $x, $y) = @_; |
353 | my ($self, $x, $y) = @_; |
337 | |
354 | |
… | |
… | |
390 | sub size_request { |
407 | sub size_request { |
391 | $_[0]{children}[0]->size_request |
408 | $_[0]{children}[0]->size_request |
392 | } |
409 | } |
393 | |
410 | |
394 | sub size_allocate { |
411 | sub size_allocate { |
395 | my ($self, $w, $h) = @_; |
412 | my ($self, $x, $y, $w, $h) = @_; |
396 | |
413 | |
397 | return unless $self->{w} != $w || $self->{h} != $h; |
414 | $self->_size_allocate ($x, $y, $w, $h) or return; |
398 | |
415 | |
399 | $self->SUPER::size_allocate ($w, $h); |
|
|
400 | $self->{children}[0]->size_allocate ($w, $h); |
416 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
401 | } |
417 | } |
402 | |
418 | |
403 | ############################################################################# |
419 | ############################################################################# |
404 | |
420 | |
405 | package CFClient::UI::Window; |
421 | package CFClient::UI::Window; |
… | |
… | |
423 | } |
439 | } |
424 | |
440 | |
425 | sub render_chld { |
441 | sub render_chld { |
426 | my ($self) = @_; |
442 | my ($self) = @_; |
427 | |
443 | |
428 | $self->{texture} = |
444 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
429 | CFClient::Texture->new_from_opengl ( |
445 | glClearColor 0, 0, 0, 1; |
430 | $self->{w}, $self->{h}, sub { $self->child->draw } |
446 | glClear GL_COLOR_BUFFER_BIT; |
431 | ); |
447 | $self->child->draw; |
|
|
448 | }; |
432 | } |
449 | } |
433 | |
450 | |
434 | sub size_allocate { |
451 | sub size_allocate { |
435 | my ($self, $w, $h) = @_; |
452 | my ($self, $x, $y, $w, $h) = @_; |
436 | |
453 | |
437 | return unless $self->{w} != $w || $self->{h} != $h; |
454 | $self->_size_allocate ($x, $y, $w, $h) or return; |
438 | |
455 | |
439 | $self->{w} = $w; |
|
|
440 | $self->{h} = $h; |
|
|
441 | |
|
|
442 | $self->child->size_allocate ($w, $h); |
456 | $self->child->size_allocate (0, 0, $w, $h); |
443 | |
457 | |
444 | $self->render_chld; |
458 | $self->render_chld; |
445 | } |
459 | } |
446 | |
460 | |
447 | sub _draw { |
461 | sub _draw { |
… | |
… | |
451 | |
465 | |
452 | my $tex = $self->{texture} |
466 | my $tex = $self->{texture} |
453 | or return; |
467 | or return; |
454 | |
468 | |
455 | glEnable GL_BLEND; |
469 | glEnable GL_BLEND; |
|
|
470 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
456 | glEnable GL_TEXTURE_2D; |
471 | glEnable GL_TEXTURE_2D; |
457 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
472 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
458 | |
473 | |
459 | $tex->draw_quad (0, 0, $w, $h); |
474 | $tex->draw_quad (0, 0, $w, $h); |
460 | |
475 | |
… | |
… | |
479 | |
494 | |
480 | map { $_ + 4 } $chld->size_request; |
495 | map { $_ + 4 } $chld->size_request; |
481 | } |
496 | } |
482 | |
497 | |
483 | sub size_allocate { |
498 | sub size_allocate { |
484 | my ($self, $w, $h) = @_; |
499 | my ($self, $x, $y, $w, $h) = @_; |
485 | |
500 | |
486 | return unless $self->{w} != $w || $self->{h} != $h; |
501 | $self->_size_allocate ($x, $y, $w, $h) or return; |
487 | |
502 | |
488 | $self->{w} = $w; |
|
|
489 | $self->{h} = $h; |
|
|
490 | |
|
|
491 | $self->child->size_allocate ($w - 4, $h - 4); |
503 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
492 | $self->child->move (2, 2); |
|
|
493 | } |
504 | } |
494 | |
505 | |
495 | sub _draw { |
506 | sub _draw { |
496 | my ($self) = @_; |
507 | my ($self) = @_; |
497 | |
508 | |
… | |
… | |
520 | |
531 | |
521 | my @tex = |
532 | my @tex = |
522 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
533 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
523 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
534 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
524 | |
535 | |
|
|
536 | sub new { |
|
|
537 | my $class = shift; |
|
|
538 | |
|
|
539 | # TODO: user_x, user_y, overwrite moveto? |
|
|
540 | |
|
|
541 | $class->SUPER::new ( |
|
|
542 | bg => [1, 1, 1, 1], |
|
|
543 | border_bg => [1, 1, 1, 1], |
|
|
544 | border => $::FONTSIZE * 0.8, |
|
|
545 | @_ |
|
|
546 | ) |
|
|
547 | } |
|
|
548 | |
525 | sub size_request { |
549 | sub size_request { |
526 | my ($self) = @_; |
550 | my ($self) = @_; |
527 | |
551 | |
|
|
552 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
553 | |
528 | my ($w, $h) = $self->SUPER::size_request; |
554 | my ($w, $h) = $self->SUPER::size_request; |
529 | |
555 | |
530 | $h += $tex[1]->{h}; |
556 | ( |
531 | $h += $tex[4]->{h}; |
557 | $w + $self->{border} * 2, |
532 | $w += $tex[2]->{w}; |
558 | $h + $self->{border} * 2, |
533 | $w += $tex[3]->{w}; |
559 | ) |
534 | |
|
|
535 | ($w, $h) |
|
|
536 | } |
560 | } |
537 | |
561 | |
538 | sub size_allocate { |
562 | sub size_allocate { |
539 | my ($self, $w, $h) = @_; |
563 | my ($self, $x, $y, $w, $h) = @_; |
540 | |
564 | |
541 | return unless $self->{w} != $w || $self->{h} != $h; |
565 | $self->_size_allocate ($x, $y, $w, $h) or return; |
542 | |
566 | |
543 | $self->SUPER::size_allocate ($w, $h); |
567 | $h -= List::Util::max 0, $self->{border} * 2; |
|
|
568 | $w -= List::Util::max 0, $self->{border} * 2; |
544 | |
569 | |
545 | $h -= $tex[1]->{h}; |
570 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
546 | $h -= $tex[4]->{h}; |
571 | } |
547 | $w -= $tex[2]->{w}; |
|
|
548 | $w -= $tex[3]->{w}; |
|
|
549 | |
572 | |
550 | $h = $h < 0 ? 0 : $h; |
573 | sub button_down { |
551 | $w = $w < 0 ? 0 : $w; |
574 | my ($self, $ev, $x, $y) = @_; |
552 | |
575 | |
553 | my $child = $self->child; |
576 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
|
|
577 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
554 | |
578 | |
555 | $child->size_allocate ($w, $h); |
579 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
556 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
580 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
|
|
581 | |
|
|
582 | $self->{motion} = sub { |
|
|
583 | my ($ev, $x, $y) = @_; |
|
|
584 | |
|
|
585 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
586 | |
|
|
587 | $self->{user_w} = $bw + $x - $ox; |
|
|
588 | $self->{user_h} = $bh + $y - $oy; |
|
|
589 | $self->update; |
|
|
590 | }; |
|
|
591 | |
|
|
592 | } elsif ($x >= 0 && $x < $self->{w} |
|
|
593 | && $y >= 0 && $y < $self->{border}) { |
|
|
594 | |
|
|
595 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
596 | my ($bx, $by) = ($self->{x}, $self->{y}); |
|
|
597 | |
|
|
598 | $self->{motion} = sub { |
|
|
599 | my ($ev, $x, $y) = @_; |
|
|
600 | |
|
|
601 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
602 | |
|
|
603 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
|
|
604 | $self->update; |
|
|
605 | }; |
|
|
606 | } |
|
|
607 | } |
|
|
608 | |
|
|
609 | sub button_up { |
|
|
610 | my ($self, $ev, $x, $y) = @_; |
|
|
611 | |
|
|
612 | delete $self->{motion}; |
|
|
613 | } |
|
|
614 | |
|
|
615 | sub mouse_motion { |
|
|
616 | my ($self, $ev, $x, $y) = @_; |
|
|
617 | |
|
|
618 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
557 | } |
619 | } |
558 | |
620 | |
559 | sub _draw { |
621 | sub _draw { |
560 | my ($self) = @_; |
622 | my ($self) = @_; |
561 | |
623 | |
562 | my ($w, $h) = ($self->{w}, $self->{h}); |
624 | my ($w, $h ) = ($self->{w}, $self->{h}); |
563 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
625 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
564 | |
626 | |
565 | glEnable GL_BLEND; |
627 | glEnable GL_BLEND; |
|
|
628 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
566 | glEnable GL_TEXTURE_2D; |
629 | glEnable GL_TEXTURE_2D; |
567 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
568 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
630 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
569 | |
631 | |
570 | my $top = $tex[1]; |
632 | glColor @{ $self->{border_bg} }; |
571 | $top->draw_quad (0, 0, $w, $top->{h}); |
633 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
572 | |
634 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
573 | my $left = $tex[3]; |
635 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
574 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
636 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
575 | |
|
|
576 | my $right = $tex[2]; |
|
|
577 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
|
|
578 | |
|
|
579 | my $bottom = $tex[4]; |
|
|
580 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
|
|
581 | |
637 | |
582 | my $bg = $tex[0]; |
638 | my $bg = $tex[0]; |
583 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
|
|
584 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
585 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
586 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
587 | |
639 | |
|
|
640 | # TODO: repeat texture not scale |
588 | my $rep_x = $cw / $bg->{w}; |
641 | my $rep_x = $cw / $bg->{w}; |
589 | my $rep_y = $ch / $bg->{h}; |
642 | my $rep_y = $ch / $bg->{h}; |
590 | |
643 | |
|
|
644 | glColor @{ $self->{bg} }; |
|
|
645 | |
|
|
646 | $bg->{s} = $rep_x; |
|
|
647 | $bg->{t} = $rep_y; |
|
|
648 | $bg->{wrap_mode} = 1; |
591 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
649 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
592 | |
650 | |
|
|
651 | glDisable GL_TEXTURE_2D; |
593 | glDisable GL_BLEND; |
652 | glDisable GL_BLEND; |
594 | glDisable GL_TEXTURE_2D; |
|
|
595 | |
653 | |
596 | $self->child->draw; |
654 | $self->child->draw; |
597 | |
|
|
598 | } |
655 | } |
599 | |
656 | |
600 | ############################################################################# |
657 | ############################################################################# |
601 | |
658 | |
602 | package CFClient::UI::Table; |
659 | package CFClient::UI::Table; |
603 | |
660 | |
604 | our @ISA = CFClient::UI::Base::; |
661 | our @ISA = CFClient::UI::Base::; |
605 | |
662 | |
|
|
663 | use List::Util qw(max sum); |
|
|
664 | |
606 | use SDL::OpenGL; |
665 | use SDL::OpenGL; |
|
|
666 | |
|
|
667 | sub new { |
|
|
668 | my $class = shift; |
|
|
669 | |
|
|
670 | $class->SUPER::new ( |
|
|
671 | col_expand => [], |
|
|
672 | @_ |
|
|
673 | ) |
|
|
674 | } |
607 | |
675 | |
608 | sub add { |
676 | sub add { |
609 | my ($self, $x, $y, $chld) = @_; |
677 | my ($self, $x, $y, $chld) = @_; |
610 | my $old_chld = $self->{children}[$y][$x]; |
|
|
611 | |
678 | |
612 | $self->{children}[$y][$x] = $chld; |
679 | $self->{children}[$y][$x] = $chld; |
613 | $chld->set_parent ($self); |
680 | $chld->set_parent ($self); |
|
|
681 | |
|
|
682 | $self->{w} = $self->{h} = -1; |
614 | $self->update; |
683 | $self->update; |
615 | } |
684 | } |
616 | |
685 | |
617 | sub max_row_height { |
686 | sub get_wh { |
618 | my ($self, $row) = @_; |
687 | my ($self) = @_; |
619 | |
688 | |
620 | my $hs = 0; |
689 | my (@w, @h); |
621 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
690 | |
|
|
691 | for my $y (0 .. $#{$self->{children}}) { |
622 | my $c = $self->{children}->[$row]->[$xi]; |
692 | my $row = $self->{children}[$y] |
623 | if ($c) { |
693 | or next; |
|
|
694 | |
|
|
695 | for my $x (0 .. $#$row) { |
|
|
696 | my $widget = $row->[$x] |
|
|
697 | or next; |
624 | my ($w, $h) = $c->size_request; |
698 | my ($w, $h) = $widget->size_request; |
625 | if ($hs < $h) { $hs = $h } |
699 | |
|
|
700 | $w[$x] = max $w[$x], $w; |
|
|
701 | $h[$y] = max $h[$y], $h; |
626 | } |
702 | } |
627 | } |
703 | } |
628 | return $hs; |
|
|
629 | } |
|
|
630 | |
704 | |
631 | sub max_col_width { |
705 | (\@w, \@h) |
|
|
706 | } |
|
|
707 | |
|
|
708 | sub size_request { |
632 | my ($self, $col) = @_; |
709 | my ($self) = @_; |
633 | |
710 | |
|
|
711 | my ($ws, $hs) = $self->get_wh; |
|
|
712 | |
|
|
713 | ( |
|
|
714 | (sum @$ws), |
|
|
715 | (sum @$hs), |
|
|
716 | ) |
|
|
717 | } |
|
|
718 | |
|
|
719 | sub size_allocate { |
|
|
720 | my ($self, $x, $y, $w, $h) = @_; |
|
|
721 | |
|
|
722 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
723 | |
|
|
724 | my ($ws, $hs) = $self->get_wh; |
|
|
725 | |
|
|
726 | my $req_w = sum @$ws; |
|
|
727 | my $req_h = sum @$hs; |
|
|
728 | |
|
|
729 | # TODO: nicer code && do row_expand |
|
|
730 | my @col_expand = @{$self->{col_expand}}; |
|
|
731 | @col_expand = (1) x @$ws unless @col_expand; |
|
|
732 | my $col_expand = (sum @col_expand) || 1; |
|
|
733 | |
|
|
734 | # linearly scale sizes |
|
|
735 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
|
|
736 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
737 | |
|
|
738 | $_ = int $_ for @$ws; #TODO: avoid rounding problems |
|
|
739 | $_ = int $_ for @$hs; #TODO: avoid rounding problems |
|
|
740 | |
|
|
741 | my $y; |
|
|
742 | |
|
|
743 | for my $r (0 .. $#{$self->{children}}) { |
|
|
744 | my $row = $self->{children}[$r] |
|
|
745 | or next; |
|
|
746 | |
634 | my $ws = 0; |
747 | my $x = 0; |
635 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
748 | my $row_h = $hs->[$r]; |
636 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
749 | |
637 | if ($c) { |
750 | for my $c (0 .. $#$row) { |
638 | my ($w, $h) = $c->size_request; |
751 | my $col_w = $ws->[$c]; |
639 | if ($ws < $w) { $ws = $w } |
752 | |
|
|
753 | if (my $widget = $row->[$c]) { |
|
|
754 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
755 | } |
|
|
756 | |
|
|
757 | $x += $col_w; |
640 | } |
758 | } |
|
|
759 | |
|
|
760 | $y += $row_h; |
641 | } |
761 | } |
642 | return $ws; |
762 | |
643 | } |
763 | } |
|
|
764 | |
|
|
765 | sub find_widget { |
|
|
766 | my ($self, $x, $y) = @_; |
|
|
767 | |
|
|
768 | $x -= $self->{x}; |
|
|
769 | $y -= $self->{y}; |
|
|
770 | |
|
|
771 | my $res; |
|
|
772 | |
|
|
773 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
774 | $res = $_->find_widget ($x, $y) |
|
|
775 | and return $res; |
|
|
776 | } |
|
|
777 | |
|
|
778 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
779 | } |
|
|
780 | |
|
|
781 | sub _draw { |
|
|
782 | my ($self) = @_; |
|
|
783 | |
|
|
784 | for (grep $_, @{$self->{children}}) { |
|
|
785 | $_->draw for grep $_, @$_; |
|
|
786 | } |
|
|
787 | } |
|
|
788 | |
|
|
789 | ############################################################################# |
|
|
790 | |
|
|
791 | package CFClient::UI::HBox; |
|
|
792 | |
|
|
793 | # TODO: wrap into common Box base class |
|
|
794 | |
|
|
795 | our @ISA = CFClient::UI::Container::; |
644 | |
796 | |
645 | sub size_request { |
797 | sub size_request { |
646 | my ($self) = @_; |
798 | my ($self) = @_; |
647 | |
799 | |
|
|
800 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
801 | |
|
|
802 | ( |
|
|
803 | (List::Util::sum map $_->[0], @alloc), |
|
|
804 | (List::Util::max map $_->[1], @alloc), |
|
|
805 | ) |
|
|
806 | } |
|
|
807 | |
|
|
808 | sub size_allocate { |
|
|
809 | my ($self, $x, $y, $w, $h) = @_; |
|
|
810 | |
|
|
811 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
812 | |
648 | my ($hs, $ws) = (0, 0); |
813 | ($h, $w) = ($w, $h); |
649 | |
814 | |
650 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
815 | my $children = $self->{children}; |
651 | $hs += $self->max_row_height ($yi); |
|
|
652 | } |
|
|
653 | |
816 | |
654 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
817 | my @h = map +($_->size_request)[0], @$children; |
655 | my $wm = 0; |
818 | |
656 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
819 | my $req_h = List::Util::sum @h; |
657 | $wm += $self->max_col_width ($xi) |
820 | |
|
|
821 | if ($req_h > $h) { |
|
|
822 | # ah well, not enough space |
|
|
823 | $_ *= $h / $req_h for @h; |
|
|
824 | } else { |
|
|
825 | my $exp = List::Util::sum map $_->{expand}, @$children; |
|
|
826 | $exp ||= 1; |
|
|
827 | |
|
|
828 | for (0 .. $#$children) { |
|
|
829 | my $child = $children->[$_]; |
|
|
830 | |
|
|
831 | my $alloc_h = $h[$_]; |
|
|
832 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
|
|
833 | $h[$_] = $alloc_h; |
658 | } |
834 | } |
659 | if ($ws < $wm) { $ws = $wm } |
|
|
660 | } |
835 | } |
661 | |
|
|
662 | return ($ws, $hs); |
|
|
663 | } |
|
|
664 | |
|
|
665 | sub _draw { |
|
|
666 | my ($self) = @_; |
|
|
667 | |
836 | |
668 | my $y = 0; |
837 | my $y = 0; |
669 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
838 | for (0 .. $#$children) { |
670 | my $x = 0; |
839 | my $child = $children->[$_]; |
|
|
840 | my $h = int $h[$_]; |
|
|
841 | $child->size_allocate ($y, 0, $h, $w); |
671 | |
842 | |
672 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
843 | $y += $h; |
673 | |
|
|
674 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
675 | if ($c) { |
|
|
676 | $c->move ($x, $y, 0); #TODO: Move to size_request |
|
|
677 | $c->draw if $c; |
|
|
678 | } |
|
|
679 | |
|
|
680 | $x += $self->max_col_width ($xi); |
|
|
681 | } |
|
|
682 | |
|
|
683 | $y += $self->max_row_height ($yi); |
|
|
684 | } |
844 | } |
685 | } |
845 | } |
686 | |
846 | |
687 | ############################################################################# |
847 | ############################################################################# |
688 | |
848 | |
689 | package CFClient::UI::VBox; |
849 | package CFClient::UI::VBox; |
690 | |
850 | |
|
|
851 | # TODO: wrap into common Box base class |
|
|
852 | |
691 | our @ISA = CFClient::UI::Container::; |
853 | our @ISA = CFClient::UI::Container::; |
692 | |
|
|
693 | use SDL::OpenGL; |
|
|
694 | |
854 | |
695 | sub size_request { |
855 | sub size_request { |
696 | my ($self) = @_; |
856 | my ($self) = @_; |
697 | |
857 | |
698 | my @alloc = map [$_->size_request], @{$self->{children}}; |
858 | my @alloc = map [$_->size_request], @{$self->{children}}; |
… | |
… | |
702 | (List::Util::sum map $_->[1], @alloc), |
862 | (List::Util::sum map $_->[1], @alloc), |
703 | ) |
863 | ) |
704 | } |
864 | } |
705 | |
865 | |
706 | sub size_allocate { |
866 | sub size_allocate { |
707 | my ($self, $w, $h) = @_; |
867 | my ($self, $x, $y, $w, $h) = @_; |
708 | |
868 | |
709 | return unless $self->{w} != $w || $self->{h} != $h; |
869 | $self->_size_allocate ($x, $y, $w, $h); |
710 | |
|
|
711 | $self->{w} = $w; |
|
|
712 | $self->{h} = $h; |
|
|
713 | |
|
|
714 | return unless $self->{h}; |
|
|
715 | |
870 | |
716 | my $children = $self->{children}; |
871 | my $children = $self->{children}; |
717 | |
872 | |
718 | my @h = map +($_->size_request)[1], @$children; |
873 | my @h = map +($_->size_request)[1], @$children; |
719 | |
874 | |
720 | my $req_h = List::Util::sum @h; |
875 | my $req_h = List::Util::sum @h; |
721 | |
876 | |
722 | if ($req_h > $h) { |
877 | if ($req_h > $h) { |
723 | # ah well, not enough space |
878 | # ah well, not enough space |
724 | $_ = $h[$_] * $h / $req_h for @h; |
879 | $_ *= $h / $req_h for @h; |
725 | } else { |
880 | } else { |
726 | my @exp = grep $_->{expand}, @$children; |
881 | my $exp = List::Util::sum map $_->{expand}, @$children; |
727 | @exp = @$children unless @exp; |
882 | $exp ||= 1; |
728 | |
883 | |
729 | my %exp = map +($_ => 1), @exp; |
|
|
730 | |
|
|
731 | for (0 .. $#$children) { |
884 | for (0 .. $#$children) { |
732 | my $child = $children->[$_]; |
885 | my $child = $children->[$_]; |
733 | |
886 | |
734 | my $alloc_h = $h[$_]; |
887 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
735 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
736 | $h[$_] = $alloc_h; |
|
|
737 | } |
888 | } |
738 | } |
889 | } |
739 | |
890 | |
740 | my $y = 0; |
891 | my $y = 0; |
741 | for (0 .. $#$children) { |
892 | for (0 .. $#$children) { |
742 | my $child = $children->[$_]; |
893 | my $child = $children->[$_]; |
743 | my $h = $h[$_]; |
894 | my $h = int $h[$_]; |
744 | $child->move (0, $y); |
|
|
745 | $child->size_allocate ($w, $h); |
895 | $child->size_allocate (0, $y, $w, $h); |
746 | |
896 | |
747 | $y += $h; |
897 | $y += $h; |
748 | } |
898 | } |
749 | } |
899 | } |
750 | |
900 | |
… | |
… | |
758 | |
908 | |
759 | sub new { |
909 | sub new { |
760 | my ($class, %arg) = @_; |
910 | my ($class, %arg) = @_; |
761 | |
911 | |
762 | my $self = $class->SUPER::new ( |
912 | my $self = $class->SUPER::new ( |
763 | fg => [1, 1, 1], |
913 | fg => [1, 1, 1], |
764 | height => $::FONTSIZE, |
914 | fontsize => $::FONTSIZE, |
765 | text => "", |
915 | text => "", |
766 | align => -1, |
916 | align => -1, |
|
|
917 | padding => 2, |
767 | layout => new CFClient::Layout, |
918 | layout => new CFClient::Layout, |
768 | %arg |
919 | %arg |
769 | ); |
920 | ); |
770 | |
921 | |
771 | $self->set_text ($self->{text}); |
922 | $self->set_text ($self->{text}); |
772 | |
923 | |
… | |
… | |
788 | |
939 | |
789 | $self->{text} = $text; |
940 | $self->{text} = $text; |
790 | $self->{layout}->set_markup ($text); |
941 | $self->{layout}->set_markup ($text); |
791 | |
942 | |
792 | delete $self->{texture}; |
943 | delete $self->{texture}; |
|
|
944 | # $self->{w} = $self->{h} = -1; |
793 | $self->update; |
945 | $self->update; |
794 | } |
946 | } |
795 | |
947 | |
796 | sub get_text { |
948 | sub get_text { |
797 | my ($self, $text) = @_; |
949 | my ($self, $text) = @_; |
… | |
… | |
801 | |
953 | |
802 | sub size_request { |
954 | sub size_request { |
803 | my ($self) = @_; |
955 | my ($self) = @_; |
804 | |
956 | |
805 | $self->{layout}->set_width; |
957 | $self->{layout}->set_width; |
806 | $self->{layout}->set_height ($self->{height}); |
958 | $self->{layout}->set_height ($self->{fontsize}); |
807 | $self->{layout}->size |
959 | my ($w, $h) = $self->{layout}->size; |
808 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
960 | |
809 | # ( |
961 | ( |
810 | # $self->{texture}{w}, |
962 | $w + $self->{padding} * 2, |
811 | # $self->{texture}{h}, |
963 | $h + $self->{padding} * 2, |
812 | # ) |
964 | ) |
813 | # } else { |
|
|
814 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
|
|
815 | # |
|
|
816 | # ($w, $h) |
|
|
817 | # } |
|
|
818 | } |
965 | } |
819 | |
966 | |
820 | sub size_allocate { |
967 | sub size_allocate { |
821 | my ($self, $w, $h) = @_; |
968 | my ($self, $x, $y, $w, $h) = @_; |
822 | |
969 | |
823 | return unless $self->{w} != $w || $self->{h} != $h; |
970 | $self->_size_allocate ($x, $y, $w, $h) or return; |
824 | |
971 | |
825 | $self->SUPER::size_allocate ($w, $h); |
|
|
826 | delete $self->{texture}; |
972 | delete $self->{texture}; |
827 | } |
973 | } |
828 | |
974 | |
829 | sub update { |
975 | sub update { |
830 | my ($self) = @_; |
976 | my ($self) = @_; |
… | |
… | |
836 | sub _draw { |
982 | sub _draw { |
837 | my ($self) = @_; |
983 | my ($self) = @_; |
838 | |
984 | |
839 | my $tex = $self->{texture} ||= do { |
985 | my $tex = $self->{texture} ||= do { |
840 | $self->{layout}->set_width ($self->{w}); |
986 | $self->{layout}->set_width ($self->{w}); |
|
|
987 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
841 | new_from_layout CFClient::Texture $self->{layout}; |
988 | new_from_layout CFClient::Texture $self->{layout} |
842 | }; |
989 | }; |
843 | |
990 | |
844 | glEnable GL_BLEND; |
991 | glEnable GL_BLEND; |
|
|
992 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
845 | glEnable GL_TEXTURE_2D; |
993 | glEnable GL_TEXTURE_2D; |
846 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
847 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
994 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
848 | |
995 | |
849 | glColor @{$self->{fg}}; |
996 | glColor @{$self->{fg}}; |
850 | |
997 | |
851 | my $x = |
998 | my $x = |
852 | $self->{align} < 0 ? 0 |
999 | $self->{align} < 0 ? $self->{padding} |
853 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
1000 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
854 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
1001 | : ($self->{w} - $tex->{w}) * 0.5; |
855 | |
1002 | |
856 | $tex->draw_quad ($x, 0); |
1003 | $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); |
857 | |
1004 | |
|
|
1005 | glDisable GL_TEXTURE_2D; |
858 | glDisable GL_BLEND; |
1006 | glDisable GL_BLEND; |
859 | glDisable GL_TEXTURE_2D; |
|
|
860 | } |
1007 | } |
861 | |
1008 | |
862 | ############################################################################# |
1009 | ############################################################################# |
863 | |
1010 | |
864 | package CFClient::UI::Entry; |
1011 | package CFClient::UI::Entry; |
… | |
… | |
871 | sub new { |
1018 | sub new { |
872 | my $class = shift; |
1019 | my $class = shift; |
873 | |
1020 | |
874 | $class->SUPER::new ( |
1021 | $class->SUPER::new ( |
875 | fg => [1, 1, 1], |
1022 | fg => [1, 1, 1], |
876 | bg => [0, 0, 0, 0.4], |
1023 | bg => [0, 0, 0, 0.2], |
877 | active_bg => [1, 1, 1], |
1024 | active_bg => [1, 1, 1, 0.5], |
878 | active_fg => [0, 0, 0], |
1025 | active_fg => [0, 0, 0], |
|
|
1026 | can_hover => 1, |
|
|
1027 | can_focus => 1, |
879 | @_ |
1028 | @_ |
880 | ) |
1029 | ) |
881 | } |
1030 | } |
882 | |
1031 | |
883 | sub _set_text { |
1032 | sub _set_text { |
884 | my ($self, $text) = @_; |
1033 | my ($self, $text) = @_; |
885 | |
1034 | |
|
|
1035 | my $old_text = $self->{text}; |
|
|
1036 | |
886 | $self->{last_activity} = $::NOW; |
1037 | $self->{last_activity} = $::NOW; |
887 | |
1038 | |
888 | $self->{text} = $text; |
1039 | $self->{text} = $text; |
889 | $self->{layout}->set_width ($self->{w}); |
1040 | $self->{layout}->set_width ($self->{w}); |
|
|
1041 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
890 | |
1042 | |
891 | $text =~ s/./*/g if $self->{hidden}; |
1043 | $text =~ s/./*/g if $self->{hidden}; |
892 | |
1044 | |
893 | |
|
|
894 | $self->{layout}->set_markup ($self->escape_text ($text)); |
1045 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
895 | |
1046 | |
896 | $text = substr $text, 0, $self->{cursor}; |
1047 | $text = substr $text, 0, $self->{cursor}; |
897 | utf8::encode $text; |
1048 | utf8::encode $text; |
898 | |
1049 | |
899 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
1050 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1051 | |
|
|
1052 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1053 | if $old_text ne $self->{text}; |
900 | } |
1054 | } |
901 | |
1055 | |
902 | sub size_request { |
1056 | sub size_request { |
903 | my ($self) = @_; |
1057 | my ($self) = @_; |
904 | |
1058 | |
… | |
… | |
906 | |
1060 | |
907 | ($w + 1, $h) # add 1 for cursor |
1061 | ($w + 1, $h) # add 1 for cursor |
908 | } |
1062 | } |
909 | |
1063 | |
910 | sub size_allocate { |
1064 | sub size_allocate { |
911 | my ($self, $w, $h) = @_; |
1065 | my ($self, $x, $y, $w, $h) = @_; |
912 | |
1066 | |
913 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
914 | |
|
|
915 | $self->SUPER::size_allocate ($w, $h); |
1067 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
916 | |
1068 | |
917 | $self->_set_text ($self->{text}); |
1069 | $self->_set_text ($self->{text}); |
918 | } |
1070 | } |
919 | |
1071 | |
920 | sub set_text { |
1072 | sub set_text { |
… | |
… | |
941 | substr $text, $self->{cursor}, 1, ""; |
1093 | substr $text, $self->{cursor}, 1, ""; |
942 | } elsif ($sym == SDLK_LEFT) { |
1094 | } elsif ($sym == SDLK_LEFT) { |
943 | --$self->{cursor} if $self->{cursor}; |
1095 | --$self->{cursor} if $self->{cursor}; |
944 | } elsif ($sym == SDLK_RIGHT) { |
1096 | } elsif ($sym == SDLK_RIGHT) { |
945 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1097 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
|
|
1098 | } elsif ($sym == SDLK_HOME) { |
|
|
1099 | $self->{cursor} = 0; |
|
|
1100 | } elsif ($sym == SDLK_END) { |
|
|
1101 | $self->{cursor} = length $text; |
|
|
1102 | } elsif ($sym == SDLK_ESCAPE) { |
|
|
1103 | $self->emit ('escape'); |
946 | } elsif ($uni) { |
1104 | } elsif ($uni) { |
947 | substr $text, $self->{cursor}++, 0, chr $uni; |
1105 | substr $text, $self->{cursor}++, 0, chr $uni; |
948 | } |
1106 | } |
949 | |
1107 | |
950 | $self->_set_text ($text); |
1108 | $self->_set_text ($text); |
… | |
… | |
965 | $self->SUPER::button_down ($ev, $x, $y); |
1123 | $self->SUPER::button_down ($ev, $x, $y); |
966 | |
1124 | |
967 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1125 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
968 | |
1126 | |
969 | # byte-index to char-index |
1127 | # byte-index to char-index |
970 | my $text = $self->{layout}; |
1128 | my $text = $self->{text}; |
971 | utf8::encode $text; |
1129 | utf8::encode $text; |
972 | $self->{cursor} = length substr $text, 0, $idx; |
1130 | $self->{cursor} = length substr $text, 0, $idx; |
973 | |
1131 | |
974 | $self->_set_text ($self->{text}); |
1132 | $self->_set_text ($self->{text}); |
975 | $self->update; |
1133 | $self->update; |
… | |
… | |
990 | $self->{fg} = $self->{active_fg}; |
1148 | $self->{fg} = $self->{active_fg}; |
991 | } else { |
1149 | } else { |
992 | glColor @{$self->{bg}}; |
1150 | glColor @{$self->{bg}}; |
993 | } |
1151 | } |
994 | |
1152 | |
|
|
1153 | glEnable GL_BLEND; |
|
|
1154 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
995 | glBegin GL_QUADS; |
1155 | glBegin GL_QUADS; |
996 | glVertex 0 , 0; |
1156 | glVertex 0 , 0; |
997 | glVertex 0 , $self->{h}; |
1157 | glVertex 0 , $self->{h}; |
998 | glVertex $self->{w}, $self->{h}; |
1158 | glVertex $self->{w}, $self->{h}; |
999 | glVertex $self->{w}, 0; |
1159 | glVertex $self->{w}, 0; |
1000 | glEnd; |
1160 | glEnd; |
|
|
1161 | glDisable GL_BLEND; |
1001 | |
1162 | |
1002 | $self->SUPER::_draw; |
1163 | $self->SUPER::_draw; |
1003 | |
1164 | |
1004 | #TODO: force update every cursor change :( |
1165 | #TODO: force update every cursor change :( |
1005 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1166 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
… | |
… | |
1009 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1170 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1010 | glEnd; |
1171 | glEnd; |
1011 | } |
1172 | } |
1012 | } |
1173 | } |
1013 | |
1174 | |
1014 | ############################################################################# |
|
|
1015 | |
|
|
1016 | package CFClient::UI::Slider; |
1175 | package CFClient::UI::LineEntry; |
1017 | |
1176 | |
1018 | use strict; |
1177 | our @ISA = CFClient::UI::Entry::; |
1019 | |
1178 | |
|
|
1179 | use SDL; |
1020 | use SDL::OpenGL; |
1180 | use SDL::OpenGL; |
1021 | use SDL::OpenGL::Constants; |
|
|
1022 | |
1181 | |
1023 | our @ISA = CFClient::UI::DrawBG::; |
1182 | sub key_down { |
1024 | |
|
|
1025 | sub size_request { |
|
|
1026 | my ($self) = @_; |
1183 | my ($self, $ev) = @_; |
1027 | |
1184 | |
1028 | my $w = 50; |
1185 | my $sym = $ev->key_sym; |
1029 | my $h = 10; |
|
|
1030 | |
1186 | |
1031 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1187 | if ($sym == SDLK_RETURN) { |
|
|
1188 | $self->emit (activate => $self->get_text); |
|
|
1189 | $self->update; |
|
|
1190 | |
|
|
1191 | } else { |
|
|
1192 | $self->SUPER::key_down ($ev); |
|
|
1193 | } |
|
|
1194 | |
1032 | } |
1195 | } |
|
|
1196 | |
|
|
1197 | ############################################################################# |
|
|
1198 | |
|
|
1199 | package CFClient::UI::Button; |
|
|
1200 | |
|
|
1201 | our @ISA = CFClient::UI::Label::; |
|
|
1202 | |
|
|
1203 | use SDL; |
|
|
1204 | use SDL::OpenGL; |
|
|
1205 | |
|
|
1206 | my @tex = |
|
|
1207 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1208 | qw(b1_button_active.png); |
1033 | |
1209 | |
1034 | sub new { |
1210 | sub new { |
1035 | my $class = shift; |
1211 | my $class = shift; |
1036 | |
1212 | |
|
|
1213 | $class->SUPER::new ( |
|
|
1214 | padding => 4, |
|
|
1215 | fg => [1, 1, 1], |
|
|
1216 | bg => [1, 1, 1, 0.2], |
|
|
1217 | active_fg => [1, 1, 0], |
|
|
1218 | can_hover => 1, |
|
|
1219 | @_ |
|
|
1220 | ) |
|
|
1221 | } |
|
|
1222 | |
|
|
1223 | sub button_up { |
|
|
1224 | my ($self, $ev, $x, $y) = @_; |
|
|
1225 | |
|
|
1226 | if ($x >= 0 && $x < $self->{w} |
|
|
1227 | && $y >= 0 && $y < $self->{h}) { |
|
|
1228 | $self->emit ("activate"); |
|
|
1229 | } |
|
|
1230 | } |
|
|
1231 | |
|
|
1232 | sub _draw { |
|
|
1233 | my ($self) = @_; |
|
|
1234 | |
|
|
1235 | local $self->{fg} = $self->{fg}; |
|
|
1236 | my $tex = $tex[0]; |
|
|
1237 | |
|
|
1238 | glEnable GL_BLEND; |
|
|
1239 | glEnable GL_TEXTURE_2D; |
|
|
1240 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1241 | |
|
|
1242 | if ($GRAB == $self) { |
|
|
1243 | $self->{fg} = $self->{active_fg}; |
|
|
1244 | } |
|
|
1245 | |
|
|
1246 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
1247 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1248 | |
|
|
1249 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
1250 | |
|
|
1251 | glDisable GL_TEXTURE_2D; |
|
|
1252 | glDisable GL_BLEND; |
|
|
1253 | |
|
|
1254 | $self->SUPER::_draw; |
|
|
1255 | } |
|
|
1256 | |
|
|
1257 | ############################################################################# |
|
|
1258 | |
|
|
1259 | package CFClient::UI::CheckBox; |
|
|
1260 | |
|
|
1261 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1262 | |
|
|
1263 | my @tex = |
|
|
1264 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1265 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
1266 | |
|
|
1267 | use SDL; |
|
|
1268 | use SDL::OpenGL; |
|
|
1269 | |
|
|
1270 | sub new { |
|
|
1271 | my $class = shift; |
|
|
1272 | |
|
|
1273 | $class->SUPER::new ( |
|
|
1274 | padding => 2, |
|
|
1275 | fg => [1, 1, 1], |
|
|
1276 | active_fg => [1, 1, 0], |
|
|
1277 | state => 0, |
|
|
1278 | can_hover => 1, |
|
|
1279 | @_ |
|
|
1280 | ) |
|
|
1281 | } |
|
|
1282 | |
|
|
1283 | sub size_request { |
|
|
1284 | my ($self) = @_; |
|
|
1285 | |
|
|
1286 | ($self->{padding} * 2 + 6) x 2 |
|
|
1287 | } |
|
|
1288 | |
|
|
1289 | sub size_allocate { |
|
|
1290 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1291 | |
|
|
1292 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1293 | } |
|
|
1294 | |
|
|
1295 | sub button_down { |
|
|
1296 | my ($self, $ev, $x, $y) = @_; |
|
|
1297 | |
|
|
1298 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
|
|
1299 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
|
|
1300 | $self->{state} = !$self->{state}; |
|
|
1301 | $self->emit (changed => $self->{state}); |
|
|
1302 | } |
|
|
1303 | } |
|
|
1304 | |
|
|
1305 | sub _draw { |
|
|
1306 | my ($self) = @_; |
|
|
1307 | |
|
|
1308 | $self->SUPER::_draw; |
|
|
1309 | |
|
|
1310 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
|
|
1311 | |
|
|
1312 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
|
|
1313 | |
|
|
1314 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
|
|
1315 | |
|
|
1316 | glEnable GL_BLEND; |
|
|
1317 | glEnable GL_TEXTURE_2D; |
|
|
1318 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1319 | |
|
|
1320 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1321 | |
|
|
1322 | $tex->draw_quad (0, 0, $s, $s); |
|
|
1323 | |
|
|
1324 | glDisable GL_TEXTURE_2D; |
|
|
1325 | glDisable GL_BLEND; |
|
|
1326 | } |
|
|
1327 | |
|
|
1328 | ############################################################################# |
|
|
1329 | |
|
|
1330 | package CFClient::UI::Slider; |
|
|
1331 | |
|
|
1332 | use strict; |
|
|
1333 | |
|
|
1334 | use SDL::OpenGL; |
|
|
1335 | |
|
|
1336 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1337 | |
|
|
1338 | my @tex = |
|
|
1339 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1340 | qw(s1_slider.png s1_slider_bg.png); |
|
|
1341 | |
|
|
1342 | sub new { |
|
|
1343 | my $class = shift; |
|
|
1344 | |
1037 | # range [value, low, high, page] |
1345 | # range [value, low, high, page] |
1038 | |
1346 | |
|
|
1347 | # TODO: 0-width page |
|
|
1348 | # TODO: req_w/h are wrong with vertical |
|
|
1349 | # TODO: calculations are off |
1039 | $class->SUPER::new ( |
1350 | my $self = $class->SUPER::new ( |
1040 | fg => [1, 1, 1], |
1351 | fg => [1, 1, 1], |
1041 | active_fg => [0, 0, 0], |
1352 | active_fg => [0, 0, 0], |
1042 | range => [0, 0, 100, 10], |
1353 | range => [0, 0, 100, 10], |
|
|
1354 | req_w => 40, |
|
|
1355 | req_h => 13, |
1043 | vertical => 1, |
1356 | vertical => 0, |
|
|
1357 | can_hover => 1, |
|
|
1358 | inner_pad => 5, |
1044 | @_ |
1359 | @_ |
1045 | ) |
1360 | ); |
|
|
1361 | |
|
|
1362 | $self |
|
|
1363 | } |
|
|
1364 | |
|
|
1365 | sub size_request { |
|
|
1366 | my ($self) = @_; |
|
|
1367 | |
|
|
1368 | my $w = $self->{req_w}; |
|
|
1369 | my $h = $self->{req_h}; |
|
|
1370 | |
|
|
1371 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1046 | } |
1372 | } |
1047 | |
1373 | |
1048 | sub button_down { |
1374 | sub button_down { |
1049 | my ($self, $ev, $x, $y) = @_; |
1375 | my ($self, $ev, $x, $y) = @_; |
1050 | |
1376 | |
… | |
… | |
1058 | if ($GRAB == $self) { |
1384 | if ($GRAB == $self) { |
1059 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1385 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1060 | |
1386 | |
1061 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1387 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1062 | |
1388 | |
|
|
1389 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1390 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1391 | |
|
|
1392 | $x -= $inner_pad_px; # substract the padding |
1063 | $x = $x * ($hi - $lo) / $w + $lo; |
1393 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
1064 | $x = $lo if $x < $lo; |
1394 | $x = $lo if $x < $lo; |
1065 | $x = $hi - $page if $x > $hi - $page; |
1395 | $x = $hi - $page if $x > $hi - $page; |
1066 | $self->{range}[0] = $x; |
1396 | $self->{range}[0] = $x; |
1067 | |
1397 | |
1068 | $self->emit (changed => $x); |
1398 | $self->emit (changed => $x); |
1069 | $self->update; |
1399 | $self->update; |
1070 | } |
1400 | } |
|
|
1401 | } |
|
|
1402 | |
|
|
1403 | # the inner_* stuff is for generating a padding for the slider handle, |
|
|
1404 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1405 | # correct propably, but it does the job for now |
|
|
1406 | sub _calc_inner_pad_px { |
|
|
1407 | my ($self, $w) = @_; |
|
|
1408 | ($w / 100) * $self->{inner_pad} # % to pixels |
1071 | } |
1409 | } |
1072 | |
1410 | |
1073 | sub _draw { |
1411 | sub _draw { |
1074 | my ($self) = @_; |
1412 | my ($self) = @_; |
1075 | |
1413 | |
… | |
… | |
1089 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1427 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1090 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1428 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1091 | |
1429 | |
1092 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1430 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1093 | |
1431 | |
|
|
1432 | $hi = $value + 1 if $lo == $hi; |
|
|
1433 | |
|
|
1434 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1435 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1436 | |
1094 | $page = int $page * $w / ($hi - $lo); |
1437 | $page = int $page * $inner_w / ($hi - $lo); |
1095 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1438 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1096 | |
1439 | |
1097 | $w -= $page; |
1440 | $w -= $page; |
1098 | $page &= ~1; |
1441 | $page &= ~1; |
1099 | glTranslate $page * 0.5, 0, 0; |
1442 | glTranslate $page * 0.5, 0, 0; |
|
|
1443 | $page ||= 2; |
1100 | |
1444 | |
1101 | glColor @$fg; |
|
|
1102 | glBegin GL_LINES; |
|
|
1103 | glVertex 0, 0; glVertex 0, $h; |
|
|
1104 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1105 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1106 | glEnd; |
|
|
1107 | |
|
|
1108 | my $knob_a = $value - $page * 0.5; |
1445 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
1109 | my $knob_b = $value + $page * 0.5; |
1446 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
1110 | |
1447 | |
1111 | glBegin GL_QUADS; |
1448 | glEnable GL_BLEND; |
1112 | glColor @$fg; |
1449 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1113 | glVertex $knob_a, 0; |
1450 | glEnable GL_TEXTURE_2D; |
1114 | glVertex $knob_a, $h; |
1451 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1115 | glVertex $knob_b, $h; |
|
|
1116 | glVertex $knob_b, 0; |
|
|
1117 | |
1452 | |
1118 | if ($knob_a < $knob_b - 2) { |
1453 | # draw background |
1119 | glColor @$bg; |
1454 | $tex[1]->draw_quad (0, 0, $w, $h); |
1120 | glVertex $knob_a + 1, 1; |
1455 | |
1121 | glVertex $knob_a + 1, $h - 1; |
1456 | # draw handle |
1122 | glVertex $knob_b - 1, $h - 1; |
1457 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
1123 | glVertex $knob_b - 1, 1; |
1458 | |
|
|
1459 | glDisable GL_BLEND; |
|
|
1460 | glDisable GL_TEXTURE_2D; |
|
|
1461 | } |
|
|
1462 | |
|
|
1463 | ############################################################################# |
|
|
1464 | |
|
|
1465 | package CFClient::UI::TextView; |
|
|
1466 | |
|
|
1467 | our @ISA = CFClient::UI::HBox::; |
|
|
1468 | |
|
|
1469 | use SDL::OpenGL; |
|
|
1470 | |
|
|
1471 | sub new { |
|
|
1472 | my $class = shift; |
|
|
1473 | |
|
|
1474 | my $self = $class->SUPER::new ( |
|
|
1475 | req_w => $::WIDTH / 6, |
|
|
1476 | req_h => $::HEIGHT / 6, |
|
|
1477 | fontsize => $::FONTSIZE, |
|
|
1478 | @_, |
|
|
1479 | |
|
|
1480 | layout => (new CFClient::Layout), |
|
|
1481 | par => [], |
|
|
1482 | height => 0, |
|
|
1483 | children => [ |
|
|
1484 | (new CFClient::UI::Empty expand => 1), |
|
|
1485 | (new CFClient::UI::Slider vertical => 1), |
|
|
1486 | ], |
|
|
1487 | ); |
|
|
1488 | |
|
|
1489 | $self->{children}[1]->connect (changed => sub { |
|
|
1490 | $self->update; |
|
|
1491 | }); |
|
|
1492 | |
|
|
1493 | $self |
|
|
1494 | } |
|
|
1495 | |
|
|
1496 | sub set_fontsize { |
|
|
1497 | my ($self, $fontsize) = @_; |
|
|
1498 | |
|
|
1499 | $self->{fontsize} = $fontsize; |
|
|
1500 | $self->reflow; |
|
|
1501 | } |
|
|
1502 | |
|
|
1503 | sub text_height { |
|
|
1504 | my ($self, $text) = @_; |
|
|
1505 | |
|
|
1506 | my $layout = $self->{layout}; |
|
|
1507 | |
|
|
1508 | $layout->set_height ($self->{fontsize}); |
|
|
1509 | $layout->set_width ($self->{w}); |
|
|
1510 | $layout->set_text ($text); |
1124 | } |
1511 | |
1125 | glEnd; |
1512 | ($layout->size)[1] |
|
|
1513 | } |
|
|
1514 | |
|
|
1515 | sub reflow { |
|
|
1516 | my ($self) = @_; |
|
|
1517 | |
|
|
1518 | $self->{need_reflow}++; |
|
|
1519 | $self->update; |
|
|
1520 | } |
|
|
1521 | |
|
|
1522 | sub size_request { |
|
|
1523 | my ($self) = @_; |
|
|
1524 | |
|
|
1525 | ($self->{req_w}, $self->{req_h}) |
|
|
1526 | } |
|
|
1527 | |
|
|
1528 | sub size_allocate { |
|
|
1529 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1530 | |
|
|
1531 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1532 | |
|
|
1533 | $self->{layout}->set_height ($self->{fontsize}); |
|
|
1534 | $self->{layout}->set_width ($self->{w}); |
|
|
1535 | |
|
|
1536 | $self->reflow; |
|
|
1537 | } |
|
|
1538 | |
|
|
1539 | sub add_paragraph { |
|
|
1540 | my ($self, $color, $text) = @_; |
|
|
1541 | |
|
|
1542 | #TODO: intelligently "reformat" paragraph |
|
|
1543 | |
|
|
1544 | my $height = $self->text_height ($text); |
|
|
1545 | |
|
|
1546 | $self->{height} += $height; |
|
|
1547 | |
|
|
1548 | push @{$self->{par}}, [$height, $color, $text]; |
|
|
1549 | |
|
|
1550 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
|
|
1551 | $self->{children}[1]->update; |
|
|
1552 | } |
|
|
1553 | |
|
|
1554 | sub update { |
|
|
1555 | my ($self) = @_; |
|
|
1556 | |
|
|
1557 | $self->SUPER::update; |
|
|
1558 | |
|
|
1559 | return unless $self->{h} > 0; |
|
|
1560 | |
|
|
1561 | delete $self->{texture}; |
|
|
1562 | |
|
|
1563 | $TOPLEVEL->on_refresh ($self, sub { |
|
|
1564 | if (delete $self->{need_reflow}) { |
|
|
1565 | my $height = 0; |
|
|
1566 | |
|
|
1567 | $height += $_->[0] = $self->text_height ($_->[2]) |
|
|
1568 | for @{$self->{par}}; |
|
|
1569 | |
|
|
1570 | $self->{height} = $height; |
|
|
1571 | |
|
|
1572 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
1573 | |
|
|
1574 | delete $self->{texture}; |
|
|
1575 | } |
|
|
1576 | |
|
|
1577 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
1578 | glClearColor 0, 0, 0, 1; |
|
|
1579 | glClear GL_COLOR_BUFFER_BIT; |
|
|
1580 | |
|
|
1581 | glEnable GL_BLEND; |
|
|
1582 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1583 | glEnable GL_TEXTURE_2D; |
|
|
1584 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1585 | |
|
|
1586 | my $top = int $self->{children}[1]{range}[0]; |
|
|
1587 | |
|
|
1588 | my $y0 = $top; |
|
|
1589 | my $y1 = $top + $self->{h}; |
|
|
1590 | |
|
|
1591 | my $y = 0; |
|
|
1592 | |
|
|
1593 | my $layout = $self->{layout}; |
|
|
1594 | |
|
|
1595 | for my $par (@{$self->{par}}) { |
|
|
1596 | my $h = $par->[0]; |
|
|
1597 | |
|
|
1598 | if ($y0 < $y + $h && $y < $y1) { |
|
|
1599 | $layout->set_text ($par->[2]); |
|
|
1600 | |
|
|
1601 | glColor @{ $par->[1] }; |
|
|
1602 | my ($W, $H) = $layout->size; |
|
|
1603 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
1604 | } |
|
|
1605 | |
|
|
1606 | $y += $h; |
|
|
1607 | } |
|
|
1608 | |
|
|
1609 | glDisable GL_TEXTURE_2D; |
|
|
1610 | glDisable GL_BLEND; |
|
|
1611 | }; |
|
|
1612 | }); |
|
|
1613 | } |
|
|
1614 | |
|
|
1615 | sub _draw { |
|
|
1616 | my ($self) = @_; |
|
|
1617 | |
|
|
1618 | if ($self->{texture}) { |
|
|
1619 | glEnable GL_TEXTURE_2D; |
|
|
1620 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1621 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
1622 | glDisable GL_TEXTURE_2D; |
|
|
1623 | } |
|
|
1624 | |
|
|
1625 | $self->{children}[1]->draw; |
|
|
1626 | |
1126 | } |
1627 | } |
1127 | |
1628 | |
1128 | ############################################################################# |
1629 | ############################################################################# |
1129 | |
1630 | |
1130 | package CFClient::UI::MapWidget; |
1631 | package CFClient::UI::MapWidget; |
… | |
… | |
1133 | |
1634 | |
1134 | use List::Util qw(min max); |
1635 | use List::Util qw(min max); |
1135 | |
1636 | |
1136 | use SDL; |
1637 | use SDL; |
1137 | use SDL::OpenGL; |
1638 | use SDL::OpenGL; |
1138 | use SDL::OpenGL::Constants; |
|
|
1139 | |
1639 | |
1140 | our @ISA = CFClient::UI::Base::; |
1640 | our @ISA = CFClient::UI::Base::; |
1141 | |
1641 | |
1142 | sub new { |
1642 | sub new { |
1143 | my $class = shift; |
1643 | my $class = shift; |
1144 | |
1644 | |
1145 | $class->SUPER::new ( |
1645 | $class->SUPER::new ( |
1146 | z => -1, |
1646 | z => -1, |
|
|
1647 | can_focus => 1, |
1147 | list => (glGenLists 1), |
1648 | list => (glGenLists 1), |
1148 | @_ |
1649 | @_ |
1149 | ) |
1650 | ) |
1150 | } |
1651 | } |
1151 | |
1652 | |
1152 | sub key_down { |
1653 | sub key_down { |
… | |
… | |
1156 | sub key_up { |
1657 | sub key_up { |
1157 | } |
1658 | } |
1158 | |
1659 | |
1159 | sub size_request { |
1660 | sub size_request { |
1160 | ( |
1661 | ( |
1161 | 1 + int $::WIDTH / 32, |
1662 | 1 + 32 * int $::WIDTH / 32, |
1162 | 1 + int $::HEIGHT / 32, |
1663 | 1 + 32 * int $::HEIGHT / 32, |
1163 | ) |
1664 | ) |
1164 | } |
1665 | } |
1165 | |
1666 | |
1166 | sub update { |
1667 | sub update { |
1167 | my ($self) = @_; |
1668 | my ($self) = @_; |
1168 | |
1669 | |
1169 | $self->{need_update} = 1; |
1670 | $self->{need_update} = 1; |
|
|
1671 | $self->SUPER::update; |
1170 | } |
1672 | } |
1171 | |
1673 | |
1172 | sub _draw { |
1674 | sub draw { |
1173 | my ($self) = @_; |
1675 | my ($self) = @_; |
1174 | |
1676 | |
|
|
1677 | $self->{need_update}++;#d# |
1175 | if (delete $self->{need_update}) { |
1678 | if (delete $self->{need_update}) { |
1176 | glNewList $self->{list}, GL_COMPILE; |
1679 | glNewList $self->{list}, GL_COMPILE; |
1177 | |
1680 | |
1178 | my $mx = $::CONN->{mapx}; |
|
|
1179 | my $my = $::CONN->{mapy}; |
|
|
1180 | |
|
|
1181 | my $map = $::CONN->{map}; |
|
|
1182 | |
|
|
1183 | my ($xofs, $yofs); |
|
|
1184 | |
|
|
1185 | my $sw = 1 + int $::WIDTH / 32; |
1681 | my $sw = int $::WIDTH / 32; |
1186 | my $sh = 1 + int $::HEIGHT / 32; |
1682 | my $sh = int $::HEIGHT / 32; |
1187 | |
1683 | |
1188 | if ($::CONN->{mapw} > $sw) { |
1684 | if ($::MAP) { |
1189 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
1685 | my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); |
1190 | } else { |
|
|
1191 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
|
|
1192 | } |
|
|
1193 | |
1686 | |
1194 | if ($::CONN->{maph} > $sh) { |
1687 | if ($::CFG->{fow_enable}) { |
1195 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
1688 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
1196 | } else { |
1689 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
1197 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1690 | glConvolutionFilter2D |
1198 | } |
1691 | GL_CONVOLUTION_2D, |
1199 | |
1692 | GL_ALPHA, |
1200 | glEnable GL_TEXTURE_2D; |
1693 | 3, 3, |
1201 | glEnable GL_BLEND; |
1694 | GL_ALPHA, GL_FLOAT, |
1202 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1695 | pack "f*", |
1203 | |
1696 | 0.1, 0.1, 0.1, |
1204 | my $sw4 = ($sw + 3) & ~3; |
1697 | 0.1, 0.2, 0.1, |
1205 | my $darkness = "\x00" x ($sw4 * $sh); |
1698 | 0.1, 0.1, 0.1, |
1206 | |
|
|
1207 | for my $x (0 .. $sw - 1) { |
|
|
1208 | my $row = $map->[$x + $xofs]; |
|
|
1209 | for my $y (0 .. $sh - 1) { |
|
|
1210 | |
|
|
1211 | my $cell = $row->[$y + $yofs] |
|
|
1212 | or next; |
1699 | ; |
1213 | |
1700 | glEnable GL_CONVOLUTION_2D; |
1214 | my $dark = $cell->[0]; |
|
|
1215 | if ($dark < 0) { |
|
|
1216 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
1217 | } else { |
|
|
1218 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
|
|
1219 | } |
1701 | } |
1220 | |
1702 | |
1221 | for my $num (grep $_, @$cell[1,2,3]) { |
1703 | my $tex = new CFClient::Texture |
1222 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1704 | w => $w, |
1223 | |
1705 | h => $h, |
1224 | my ($w, $h) = @$tex{qw(w h)}; |
1706 | data => $data, |
|
|
1707 | internalformat => GL_ALPHA, |
|
|
1708 | format => GL_ALPHA; |
1225 | |
1709 | |
1226 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1710 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
1227 | } |
1711 | |
|
|
1712 | glEnable GL_BLEND; |
|
|
1713 | glEnable GL_TEXTURE_2D; |
|
|
1714 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1715 | |
|
|
1716 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1717 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1718 | |
|
|
1719 | glDisable GL_TEXTURE_2D; |
|
|
1720 | glDisable GL_BLEND; |
1228 | } |
1721 | } |
1229 | } |
1722 | } |
1230 | |
1723 | |
1231 | # if (1) { # higher quality darkness |
|
|
1232 | # $lighting =~ s/(.)/$1$1$1/gs; |
|
|
1233 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
|
|
1234 | # |
|
|
1235 | # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
|
|
1236 | # |
|
|
1237 | # $lighting = $pb->get_pixels; |
|
|
1238 | # $lighting =~ s/(.)../$1/gs; |
|
|
1239 | # } |
|
|
1240 | |
|
|
1241 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1242 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1243 | |
|
|
1244 | $darkness = new CFClient::Texture |
|
|
1245 | width => $sw4, |
|
|
1246 | height => $sh, |
|
|
1247 | data => $darkness, |
|
|
1248 | internalformat => GL_ALPHA, |
|
|
1249 | format => GL_ALPHA; |
|
|
1250 | |
|
|
1251 | glColor 0.45, 0.45, 0.45, 1; |
|
|
1252 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
|
|
1253 | |
|
|
1254 | glDisable GL_TEXTURE_2D; |
|
|
1255 | glDisable GL_BLEND; |
|
|
1256 | |
|
|
1257 | glEndList; |
1724 | glEndList; |
1258 | } |
1725 | } |
1259 | |
1726 | |
1260 | glCallList $self->{list}; |
1727 | glCallList $self->{list}; |
|
|
1728 | |
|
|
1729 | if ($FOCUS != $self) { |
|
|
1730 | glColor 64/255, 64/255, 64/255; |
|
|
1731 | glLogicOp GL_AND; |
|
|
1732 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1733 | glBegin GL_QUADS; |
|
|
1734 | glVertex 0, 0; |
|
|
1735 | glVertex 0, $::HEIGHT; |
|
|
1736 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1737 | glVertex $::WIDTH, 0; |
|
|
1738 | glEnd; |
|
|
1739 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1740 | } |
1261 | } |
1741 | } |
1262 | |
1742 | |
1263 | my %DIR = ( |
1743 | my %DIR = ( |
1264 | SDLK_KP8, [1, "north"], |
1744 | SDLK_KP8, [1, "north"], |
1265 | SDLK_KP9, [2, "northeast"], |
1745 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
1281 | |
1761 | |
1282 | my $mod = $ev->key_mod; |
1762 | my $mod = $ev->key_mod; |
1283 | my $sym = $ev->key_sym; |
1763 | my $sym = $ev->key_sym; |
1284 | |
1764 | |
1285 | if ($sym == SDLK_KP5) { |
1765 | if ($sym == SDLK_KP5) { |
1286 | $::CONN->send ("command stay fire"); |
1766 | $::CONN->user_send ("command stay fire"); |
|
|
1767 | } elsif ($sym == SDLK_a) { |
|
|
1768 | $::CONN->user_send ("command apply"); |
|
|
1769 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1770 | $self->emit ('activate_console'); |
|
|
1771 | } elsif ($sym == SDLK_SLASH) { |
|
|
1772 | $self->emit ('activate_console' => '/'); |
1287 | } elsif (exists $DIR{$sym}) { |
1773 | } elsif (exists $DIR{$sym}) { |
1288 | if ($mod & KMOD_SHIFT) { |
1774 | if ($mod & KMOD_SHIFT) { |
1289 | $self->{shft}++; |
1775 | $self->{shft}++; |
1290 | $::CONN->send ("command fire $DIR{$sym}[0]"); |
1776 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
1291 | } elsif ($mod & KMOD_CTRL) { |
1777 | } elsif ($mod & KMOD_CTRL) { |
1292 | $self->{ctrl}++; |
1778 | $self->{ctrl}++; |
1293 | $::CONN->send ("command run $DIR{$sym}[0]"); |
1779 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
1294 | } else { |
1780 | } else { |
1295 | $::CONN->send ("command $DIR{$sym}[1]"); |
1781 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
1296 | } |
1782 | } |
1297 | } |
1783 | } |
1298 | } |
1784 | } |
1299 | |
1785 | |
1300 | sub key_up { |
1786 | sub key_up { |
… | |
… | |
1302 | |
1788 | |
1303 | my $mod = $ev->key_mod; |
1789 | my $mod = $ev->key_mod; |
1304 | my $sym = $ev->key_sym; |
1790 | my $sym = $ev->key_sym; |
1305 | |
1791 | |
1306 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
1792 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
1307 | $::CONN->send ("command fire_stop"); |
1793 | $::CONN->user_send ("command fire_stop"); |
1308 | } |
1794 | } |
1309 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
1795 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
1310 | $::CONN->send ("command run_stop"); |
1796 | $::CONN->user_send ("command run_stop"); |
1311 | } |
1797 | } |
1312 | } |
1798 | } |
1313 | |
1799 | |
1314 | ############################################################################# |
1800 | ############################################################################# |
1315 | |
1801 | |
… | |
… | |
1353 | glPopMatrix; |
1839 | glPopMatrix; |
1354 | } |
1840 | } |
1355 | |
1841 | |
1356 | ############################################################################# |
1842 | ############################################################################# |
1357 | |
1843 | |
|
|
1844 | package CFClient::UI::Flopper; |
|
|
1845 | |
|
|
1846 | our @ISA = CFClient::UI::Button::; |
|
|
1847 | |
|
|
1848 | sub new { |
|
|
1849 | my $class = shift; |
|
|
1850 | |
|
|
1851 | my $self = $class->SUPER::new ( |
|
|
1852 | state => 0, |
|
|
1853 | connect_activate => \&toggle_flopper, |
|
|
1854 | @_ |
|
|
1855 | ); |
|
|
1856 | |
|
|
1857 | if ($self->{state}) { |
|
|
1858 | $self->{state} = 0; |
|
|
1859 | $self->toggle_flopper; |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | $self |
|
|
1863 | } |
|
|
1864 | |
|
|
1865 | sub toggle_flopper { |
|
|
1866 | my ($self) = @_; |
|
|
1867 | |
|
|
1868 | # TODO: use animation |
|
|
1869 | if ($self->{state} = !$self->{state}) { |
|
|
1870 | $CFClient::UI::TOPLEVEL->add ($self->{other}); |
|
|
1871 | $self->{other}->move ( |
|
|
1872 | ($::WIDTH - $self->{other}{w}) * 0.5, |
|
|
1873 | ($::HEIGHT - $self->{other}{h}) * 0.5, |
|
|
1874 | ); |
|
|
1875 | } else { |
|
|
1876 | $CFClient::UI::TOPLEVEL->remove ($self->{other}); |
|
|
1877 | } |
|
|
1878 | } |
|
|
1879 | |
|
|
1880 | ############################################################################# |
|
|
1881 | |
1358 | package CFClient::UI::Toplevel; |
1882 | package CFClient::UI::Toplevel; |
1359 | |
1883 | |
1360 | our @ISA = CFClient::UI::Container::; |
1884 | our @ISA = CFClient::UI::Container::; |
|
|
1885 | |
|
|
1886 | use SDL::OpenGL; |
1361 | |
1887 | |
1362 | sub size_request { |
1888 | sub size_request { |
1363 | ($::WIDTH, $::HEIGHT) |
1889 | ($::WIDTH, $::HEIGHT) |
1364 | } |
1890 | } |
1365 | |
1891 | |
1366 | sub size_allocate { |
1892 | sub size_allocate { |
1367 | my ($self, $w, $h) = @_; |
1893 | my ($self, $x, $y, $w, $h) = @_; |
1368 | |
1894 | |
1369 | $self->SUPER::size_allocate ($w, $h); |
1895 | $self->_size_allocate ($x, $y, $w, $h); |
1370 | |
1896 | |
1371 | $_->size_allocate ($_->size_request) |
1897 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1372 | for @{$self->{children}}; |
1898 | for @{$self->{children}}; |
1373 | } |
1899 | } |
1374 | |
1900 | |
1375 | sub translate { |
1901 | sub translate { |
1376 | my ($self, $x, $y) = @_; |
1902 | my ($self, $x, $y) = @_; |
… | |
… | |
1379 | } |
1905 | } |
1380 | |
1906 | |
1381 | sub update { |
1907 | sub update { |
1382 | my ($self) = @_; |
1908 | my ($self) = @_; |
1383 | |
1909 | |
1384 | $self->size_allocate ($self->size_request); |
1910 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1385 | ::refresh (); |
1911 | ::refresh (); |
1386 | } |
1912 | } |
1387 | |
1913 | |
1388 | sub add { |
1914 | sub add { |
1389 | my ($self, $widget) = @_; |
1915 | my ($self, $widget) = @_; |
1390 | |
1916 | |
1391 | $self->SUPER::add ($widget); |
1917 | $self->SUPER::add ($widget); |
1392 | |
1918 | |
1393 | $widget->size_allocate ($widget->size_request); |
1919 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
1920 | } |
|
|
1921 | |
|
|
1922 | sub on_refresh { |
|
|
1923 | my ($self, $id, $cb) = @_; |
|
|
1924 | |
|
|
1925 | $self->{refresh_hook}{$id} = $cb; |
1394 | } |
1926 | } |
1395 | |
1927 | |
1396 | sub draw { |
1928 | sub draw { |
1397 | my ($self) = @_; |
1929 | my ($self) = @_; |
1398 | |
1930 | |
|
|
1931 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
1932 | $_->() for values %$rcb; |
|
|
1933 | } |
|
|
1934 | |
|
|
1935 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
1936 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1937 | glClear GL_COLOR_BUFFER_BIT; |
|
|
1938 | |
|
|
1939 | glMatrixMode GL_PROJECTION; |
|
|
1940 | glLoadIdentity; |
|
|
1941 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
1942 | glMatrixMode GL_MODELVIEW; |
|
|
1943 | glLoadIdentity; |
|
|
1944 | |
1399 | $self->_draw; |
1945 | $self->_draw; |
1400 | } |
1946 | } |
1401 | |
1947 | |
1402 | ############################################################################# |
1948 | ############################################################################# |
1403 | |
1949 | |