ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.157 by root, Sun Apr 23 21:47:32 2006 UTC vs.
Revision 1.230 by root, Wed May 24 21:59:54 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
11 12
12our $ROOT; 13our $ROOT;
13our $TOOLTIP; 14our $TOOLTIP;
14our $BUTTON_STATE; 15our $BUTTON_STATE;
15 16
17our %WIDGET; # all widgets, weak-referenced
18
16sub check_tooltip { 19sub check_tooltip {
17 if (!$GRAB) { 20 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
20 23
21 if ($TOOLTIP->{owner} != $widget) { 24 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget; 25 $TOOLTIP->{owner} = $widget;
23 26
24 my $tip = $widget->{tooltip}; 27 my $tip = $widget->{tooltip};
25 28
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 29 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 30
28 $TOOLTIP->set_text ($widget->{tooltip}); 31 $TOOLTIP->set_tooltip_from ($widget);
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show; 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;
31 } 42 }
32 43
33 return; 44 return;
34 } 45 }
35 } 46 }
39 delete $TOOLTIP->{owner}; 50 delete $TOOLTIP->{owner};
40} 51}
41 52
42# class methods for events 53# class methods for events
43sub feed_sdl_key_down_event { 54sub feed_sdl_key_down_event {
44 $FOCUS->key_down ($_[0]) if $FOCUS; 55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
56 if $FOCUS;
45} 57}
46 58
47sub feed_sdl_key_up_event { 59sub feed_sdl_key_up_event {
48 $FOCUS->key_up ($_[0]) if $FOCUS; 60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
61 if $FOCUS;
49} 62}
50 63
51sub feed_sdl_button_down_event { 64sub feed_sdl_button_down_event {
52 my ($ev) = @_; 65 my ($ev) = @_;
53 my ($x, $y) = ($ev->{x}, $ev->{y}); 66 my ($x, $y) = ($ev->{x}, $ev->{y});
61 check_tooltip; 74 check_tooltip;
62 } 75 }
63 76
64 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 77 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
65 78
66 $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 }
67} 83}
68 84
69sub feed_sdl_button_up_event { 85sub feed_sdl_button_up_event {
70 my ($ev) = @_; 86 my ($ev) = @_;
71 my ($x, $y) = ($ev->{x}, $ev->{y}); 87 my ($x, $y) = ($ev->{x}, $ev->{y});
72 88
73 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 89 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
74 90
75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
76 92
77 $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 }
78 97
79 if (!$BUTTON_STATE) { 98 if (!$BUTTON_STATE) {
80 my $grab = $GRAB; undef $GRAB; 99 my $grab = $GRAB; undef $GRAB;
81 $grab->update if $grab; 100 $grab->update if $grab;
82 $GRAB->update if $GRAB; 101 $GRAB->update if $GRAB;
98 $HOVER->update if $HOVER && $HOVER->{can_hover}; 117 $HOVER->update if $HOVER && $HOVER->{can_hover};
99 118
100 check_tooltip; 119 check_tooltip;
101 } 120 }
102 121
103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 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 }
104} 126}
105 127
106# convert position array to integers 128# convert position array to integers
107sub harmonize { 129sub harmonize {
108 my ($vals) = @_; 130 my ($vals) = @_;
114 $rem += $_ - $i; 136 $rem += $_ - $i;
115 $_ = $i; 137 $_ = $i;
116 } 138 }
117} 139}
118 140
141sub full_refresh {
142 # make a copy, otherwise for complains about freed values.
143 my @widgets = values %WIDGET;
144
145 $_->update
146 for @widgets;
147}
148
149sub reconfigure_widgets {
150 # make a copy, otherwise C<for> complains about freed values.
151 my @widgets = values %WIDGET;
152
153 $_->reconfigure
154 for @widgets;
155}
156
157# call when resolution changes etc.
158sub rescale_widgets {
159 my ($sx, $sy) = @_;
160
161 for my $widget (values %WIDGET) {
162 if ($widget->{is_toplevel}) {
163 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
164 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
165 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
166 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
167 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
168 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
169 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
170 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
171 }
172 }
173
174 reconfigure_widgets;
175}
176
119############################################################################# 177#############################################################################
120 178
121package CFClient::UI::Base; 179package CFClient::UI::Base;
122 180
123use strict; 181use strict;
126 184
127sub new { 185sub new {
128 my $class = shift; 186 my $class = shift;
129 187
130 my $self = bless { 188 my $self = bless {
131 x => 0, 189 x => 0,
132 y => 0, 190 y => 0,
133 z => 0, 191 z => 0,
134 can_events => 1, 192 can_events => 1,
135 @_ 193 @_
136 }, $class; 194 }, $class;
137 195
138 for (keys %$self) { 196 for (keys %$self) {
139 if (/^connect_(.*)$/) { 197 if (/^connect_(.*)$/) {
140 $self->connect ($1 => delete $self->{$_}); 198 $self->connect ($1 => delete $self->{$_});
141 } 199 }
142 } 200 }
143 201
202 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
203
144 $self 204 $self
145} 205}
146 206
207sub destroy {
208 my ($self) = @_;
209
210 $self->hide;
211 %$self = ();
212}
213
147sub show { 214sub show {
148 my ($self) = @_; 215 my ($self) = @_;
149
150 return if $self->{parent}; 216 return if $self->{parent};
151 217
152 $CFClient::UI::ROOT->add ($self); 218 $CFClient::UI::ROOT->add ($self);
153} 219}
154 220
221sub show_centered {
222 my ($self) = @_;
223 return if $self->{parent};
224
225 $self->show;
226
227 $CFClient::UI::ROOT->on_post_alloc (
228 "centered $self" => sub {
229 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
230 },
231 );
232}
233
155sub hide { 234sub hide {
156 my ($self) = @_; 235 my ($self) = @_;
157 236
158 return unless $self->{parent}; 237 undef $GRAB if $GRAB == $self;
238 undef $HOVER if $HOVER == $self;
159 239
160 $self->{parent}->remove ($self); 240 $self->{parent}->remove ($self)
241 if $self->{parent};
161} 242}
162 243
163sub move { 244sub move {
164 my ($self, $x, $y, $z) = @_; 245 my ($self, $x, $y, $z) = @_;
165 246
168 $self->{z} = $z if defined $z; 249 $self->{z} = $z if defined $z;
169 250
170 $self->update; 251 $self->update;
171} 252}
172 253
173sub needs_redraw { 254sub set_size {
174 0 255 my ($self, $w, $h) = @_;
256
257 $self->{user_w} = $w;
258 $self->{user_h} = $h;
259
260 $self->check_size;
175} 261}
176 262
177sub size_request { 263sub size_request {
178 require Carp; 264 require Carp;
179 Carp::confess "size_request is abstract"; 265 Carp::confess "size_request is abstract";
199 $self->{y} = $y; 285 $self->{y} = $y;
200 $self->update; 286 $self->update;
201 } 287 }
202 288
203 if ($self->{w} != $w || $self->{h} != $h) { 289 if ($self->{w} != $w || $self->{h} != $h) {
204 $self->{w} = $w; 290 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
205 $self->{h} = $h;
206
207 $self->size_allocate ($w, $h);
208 $self->update;
209 } 291 }
210} 292}
211 293
212sub size_allocate { 294sub size_allocate {
213 # nothing to be done 295 # nothing to be done
214} 296}
215 297
298sub reconfigure {
299 my ($self) = @_;
300
301 $self->check_size (1);
302 $self->update;
303}
304
305sub children {
306}
307
216sub set_max_size { 308sub set_max_size {
217 my ($self, $w, $h) = @_; 309 my ($self, $w, $h) = @_;
218 310
219 delete $self->{max_w}; $self->{max_w} = $w if $w; 311 delete $self->{max_w}; $self->{max_w} = $w if $w;
220 delete $self->{max_h}; $self->{max_h} = $h if $h; 312 delete $self->{max_h}; $self->{max_h} = $h if $h;
221} 313}
222 314
223# return top left coordinates 315sub set_tooltip {
224sub _topleft {
225 my ($self, $x, $y) = @_; 316 my ($self, $tooltip) = @_;
226 317
227 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 318 $self->{tooltip} = $tooltip;
319
320 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
321 delete $CFClient::UI::TOOLTIP->{owner};
322 CFClient::UI::check_tooltip;
323 }
228} 324}
229 325
230# translate global coordinates to local coordinate system 326# translate global coordinates to local coordinate system
231sub coord2local { 327sub coord2local {
232 my ($self, $x, $y) = @_; 328 my ($self, $x, $y) = @_;
233 329
234 my ($X, $Y) = $self->_topleft; 330 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
235 ($x - $X, $y - $Y)
236} 331}
237 332
238# translate local coordinates to global coordinate system 333# translate local coordinates to global coordinate system
239sub coord2global { 334sub coord2global {
240 my ($self, $x, $y) = @_; 335 my ($self, $x, $y) = @_;
241 336
242 my ($X, $Y) = $self->_topleft; 337 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
243 ($x + $X, $y + $Y)
244} 338}
245 339
246sub focus_in { 340sub focus_in {
247 my ($self) = @_; 341 my ($self) = @_;
248 342
308 glVertex $x + $self->{w}, $y + $self->{h}; 402 glVertex $x + $self->{w}, $y + $self->{h};
309 glVertex $x , $y + $self->{h}; 403 glVertex $x , $y + $self->{h};
310 glEnd; 404 glEnd;
311 glDisable GL_BLEND; 405 glDisable GL_BLEND;
312 } 406 }
407
408 if ($ENV{PCLIENT_DEBUG}) {
409 glPushMatrix;
410 glColor 1, 1, 0, 1;
411 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
412 glBegin GL_LINE_LOOP;
413 glVertex 0 , 0;
414 glVertex $self->{w}, 0;
415 glVertex $self->{w}, $self->{h};
416 glVertex 0 , $self->{h};
417 glEnd;
418 glPopMatrix;
419 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
420 }
313} 421}
314 422
315sub _draw { 423sub _draw {
316 my ($self) = @_; 424 my ($self) = @_;
317 425
332 440
333sub set_parent { 441sub set_parent {
334 my ($self, $parent) = @_; 442 my ($self, $parent) = @_;
335 443
336 Scalar::Util::weaken ($self->{parent} = $parent); 444 Scalar::Util::weaken ($self->{parent} = $parent);
445
446 # TODO: req_w _does_change after ->reconfigure
447 $self->check_size
448 unless exists $self->{req_w};
449
450 $self->show;
337} 451}
338 452
339sub check_size { 453sub check_size {
340 my ($self) = @_; 454 my ($self, $forced) = @_;
341 455
342 return unless $self->{parent}; 456 $self->{force_alloc} = 1 if $forced;
343 457 $CFClient::UI::ROOT->{check_size}{$self} = $self;
344 my ($w, $h) = $self->size_request;
345
346 if ($w != $self->{req_w} || $h != $self->{req_h}) {
347 $self->{req_w} = $w;
348 $self->{req_h} = $h;
349
350 $self->{parent}->check_size;
351 }
352} 458}
353 459
354sub update { 460sub update {
355 my ($self) = @_; 461 my ($self) = @_;
356 462
365} 471}
366 472
367sub emit { 473sub emit {
368 my ($self, $signal, @args) = @_; 474 my ($self, $signal, @args) = @_;
369 475
370 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 476 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
371 $cb->($self, @args);
372 }
373} 477}
374 478
375sub DESTROY { 479sub DESTROY {
376 my ($self) = @_; 480 my ($self) = @_;
377 481
482 delete $WIDGET{$self+0};
378 #$self->deactivate; 483 #$self->deactivate;
379} 484}
380 485
381############################################################################# 486#############################################################################
382 487
391 my $class = shift; 496 my $class = shift;
392 497
393 # range [value, low, high, page] 498 # range [value, low, high, page]
394 499
395 $class->SUPER::new ( 500 $class->SUPER::new (
396 bg => [0, 0, 0, 0.2], 501 #bg => [0, 0, 0, 0.2],
397 active_bg => [1, 1, 1, 0.5], 502 #active_bg => [1, 1, 1, 0.5],
398 @_ 503 @_
399 ) 504 )
400} 505}
401 506
402sub _draw { 507sub _draw {
403 my ($self) = @_; 508 my ($self) = @_;
404 509
510 my $color = $FOCUS == $self && $self->{active_bg}
511 ? $self->{active_bg}
512 : $self->{bg};
513
514 if ($color && (@$color < 4 || $color->[3])) {
405 my ($w, $h) = @$self{qw(w h)}; 515 my ($w, $h) = @$self{qw(w h)};
406 516
407 glEnable GL_BLEND; 517 glEnable GL_BLEND;
408 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 518 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
409 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 519 glColor @$color;
410 520
411 glBegin GL_QUADS; 521 glBegin GL_QUADS;
412 glVertex 0 , 0; 522 glVertex 0 , 0;
413 glVertex 0 , $h; 523 glVertex 0 , $h;
414 glVertex $w, $h; 524 glVertex $w, $h;
415 glVertex $w, 0; 525 glVertex $w, 0;
416 glEnd; 526 glEnd;
417 527
418 glDisable GL_BLEND; 528 glDisable GL_BLEND;
529 }
419} 530}
420 531
421############################################################################# 532#############################################################################
422 533
423package CFClient::UI::Empty; 534package CFClient::UI::Empty;
444sub new { 555sub new {
445 my ($class, %arg) = @_; 556 my ($class, %arg) = @_;
446 557
447 my $children = delete $arg{children} || []; 558 my $children = delete $arg{children} || [];
448 559
449 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 560 my $self = $class->SUPER::new (
561 children => [],
562 can_events => 0,
563 %arg,
564 );
450 $self->add ($_) for @$children; 565 $self->add ($_) for @$children;
451 566
452 $self 567 $self
453} 568}
454 569
455sub add { 570sub add {
456 my ($self, $child) = @_; 571 my ($self, @widgets) = @_;
457 572
458 $child->set_parent ($self); 573 $_->set_parent ($self)
574 for @widgets;
459 575
460 use sort 'stable'; 576 use sort 'stable';
461 577
462 $self->{children} = [ 578 $self->{children} = [
463 sort { $a->{z} <=> $b->{z} } 579 sort { $a->{z} <=> $b->{z} }
464 @{$self->{children}}, $child 580 @{$self->{children}}, @widgets
465 ]; 581 ];
466 582
467 $child->check_size; 583 $self->check_size (1);
584 $self->update;
585}
586
587sub children {
588 @{ $_[0]{children} }
468} 589}
469 590
470sub remove { 591sub remove {
471 my ($self, $child) = @_; 592 my ($self, $child) = @_;
472 593
473 delete $child->{parent}; 594 delete $child->{parent};
595 $child->hide;
474 596
475 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 597 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
598
599 $self->check_size;
600 $self->update;
601}
602
603sub clear {
604 my ($self) = @_;
605
606 my $children = delete $self->{children};
607 $self->{children} = [];
608
609 for (@$children) {
610 delete $_->{parent};
611 $_->hide;
612 }
476 613
477 $self->check_size; 614 $self->check_size;
478 $self->update; 615 $self->update;
479} 616}
480 617
558} 695}
559 696
560sub update { 697sub update {
561 my ($self) = @_; 698 my ($self) = @_;
562 699
563 # we want to do this delayed... 700 $ROOT->on_post_alloc ($self => sub { $self->render_child });
564 $self->render_chld;
565 $self->SUPER::update; 701 $self->SUPER::update;
566}
567
568sub render_chld {
569 my ($self) = @_;
570
571 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
572 glClearColor 0, 0, 0, 1;
573 glClear GL_COLOR_BUFFER_BIT;
574 $self->child->draw;
575 };
576} 702}
577 703
578sub size_allocate { 704sub size_allocate {
579 my ($self, $w, $h) = @_; 705 my ($self, $w, $h) = @_;
580 706
581 $self->child->configure (0, 0, $w, $h); 707 $self->SUPER::size_allocate ($w, $h);
708 $self->update;
709}
582 710
711sub _render {
712 $_[0]{children}[0]->draw;
713}
714
715sub render_child {
716 my ($self) = @_;
717
718 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
719 glClearColor 0, 0, 0, 0;
720 glClear GL_COLOR_BUFFER_BIT;
721
583 $self->render_chld; 722 $self->_render;
723 };
584} 724}
585 725
586sub _draw { 726sub _draw {
587 my ($self) = @_; 727 my ($self) = @_;
588 728
589 my ($w, $h) = ($self->w, $self->h); 729 my ($w, $h) = ($self->w, $self->h);
590 730
591 my $tex = $self->{texture} 731 my $tex = $self->{texture}
592 or return; 732 or return;
593 733
594 glEnable GL_BLEND;
595 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
596 glEnable GL_TEXTURE_2D; 734 glEnable GL_TEXTURE_2D;
597 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
736 glColor 1, 1, 1, 1;
598 737
599 $tex->draw_quad (0, 0, $w, $h); 738 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
600 739
601 glDisable GL_BLEND;
602 glDisable GL_TEXTURE_2D; 740 glDisable GL_TEXTURE_2D;
603} 741}
604 742
605############################################################################# 743#############################################################################
606 744
607package CFClient::UI::ViewPort; 745package CFClient::UI::ViewPort;
608 746
609our @ISA = CFClient::UI::Window::; 747our @ISA = CFClient::UI::Window::;
610 748
611sub new { die }
612
613sub size_request { 749sub size_request {
614 my ($self) = @_; 750 my ($self) = @_;
615 751
616 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 752 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
617 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
618 753
619 @$self{qw(child_w child_h)} 754 @$self{qw(child_w child_h)}
620} 755}
621 756
622sub _draw { 757sub size_allocate {
623 my ($self) = @_; 758 my ($self, $w, $h) = @_;
624 759
625 $self->{children}[1]->draw; 760 my ($cw, $ch) = @$self{qw(child_w child_h)};
761# $w = $self->{w};
762 $self->child->configure (0, 0, $cw, $ch);
763 $self->update;
626} 764}
627 765
766sub set_offset {
767 my ($self, $x, $y) = @_;
768
769 $self->{view_x} = int $x;
770 $self->{view_y} = int $y;
771
772 $self->update;
773}
774
775# hmm, this does not work for topleft of $self... but we should not ask for that
776sub coord2local {
777 my ($self, $x, $y) = @_;
778
779 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
780}
781
782sub coord2global {
783 my ($self, $x, $y) = @_;
784
785 $x = List::Util::min $self->{w}, $x - $self->{view_x};
786 $y = List::Util::min $self->{h}, $y - $self->{view_y};
787
788 $self->SUPER::coord2global ($x, $y)
789}
790
791sub find_widget {
792 my ($self, $x, $y) = @_;
793
794 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
795 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
796 ) {
797 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
798 } else {
799 $self->CFClient::UI::Base::find_widget ($x, $y)
800 }
801}
802
803sub _render {
804 my ($self) = @_;
805
806 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
807
808 $self->SUPER::_render;
809}
628 810
629############################################################################# 811#############################################################################
630 812
631package CFClient::UI::Frame; 813package CFClient::UI::ScrolledWindow;
632 814
633our @ISA = CFClient::UI::Bin::; 815our @ISA = CFClient::UI::HBox::;
634
635use CFClient::OpenGL;
636 816
637sub new { 817sub new {
638 my $class = shift; 818 my $class = shift;
639 819
820 my $self;
821
822 my $slider = new CFClient::UI::Slider
823 vertical => 1,
824 range => [0, 0, 1, 0.01], # HACK fix
825 connect_changed => sub {
826 $self->{vp}->set_offset (0, $_[1]);
827 },
828 ;
829
640 my $self = $class->SUPER::new ( 830 $self = $class->SUPER::new (
641 bg => [1, 1, 1, 1], 831 vp => (new CFClient::UI::ViewPort expand => 1),
642 border_bg => [1, 1, 1, 1], 832 slider => $slider,
643 border => 0.8,
644 @_ 833 @_,
645 ); 834 );
646 835
836 $self->{vp}->add ($self->{scrolled});
837 $self->add ($self->{vp});
838 $self->add ($self->{slider});
839
647 $self 840 $self
648}
649
650sub set_size {
651 my ($self, $w, $h) = @_;
652 $self->{req_w} = $w;
653 $self->{req_h} = $h;
654 $self->check_size;
655}
656
657sub size_request {
658 my ($self) = @_;
659 ($self->{req_w}, $self->{req_h})
660} 841}
661 842
662sub size_allocate { 843sub size_allocate {
663 my ($self, $w, $h) = @_; 844 my ($self, $w, $h) = @_;
664 $self->{w} = $w; 845
665 $self->{h} = $h; 846 $self->SUPER::size_allocate ($w, $h);
666 $self->child->configure (0, 0, $w, $h); 847
848 my $child = $self->{vp}->child;
849 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
850}
851
852#TODO# update range on size_allocate depending on child
853# update viewport offset on scroll
854
855#############################################################################
856
857package CFClient::UI::Frame;
858
859our @ISA = CFClient::UI::Bin::;
860
861use CFClient::OpenGL;
862
863sub new {
864 my $class = shift;
865
866 $class->SUPER::new (
867 bg => undef,
868 @_,
869 )
667} 870}
668 871
669sub _draw { 872sub _draw {
670 my ($self) = @_; 873 my ($self) = @_;
671 874
672 my ($w, $h) = ($self->{w}, $self->{h}); 875 if ($self->{bg}) {
876 my ($w, $h) = @$self{qw(w h)};
673 877
674 glEnable GL_BLEND; 878 glEnable GL_BLEND;
675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 879 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
676 glEnable GL_TEXTURE_2D; 880 glColor @{ $self->{bg} };
677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
678 881
679# glBegin GL_QUADS; 882 glBegin GL_QUADS;
680# glColor 0, 0, 0, 0;
681# glVertex 0 , 0; 883 glVertex 0 , 0;
682# glVertex 0 , $h; 884 glVertex 0 , $h;
683# glVertex $w, $h; 885 glVertex $w, $h;
684# glVertex $w, 0; 886 glVertex $w, 0;
685# glEnd; 887 glEnd;
686 888
687
688 $self->child->draw;
689 glDisable GL_BLEND; 889 glDisable GL_BLEND;
690 glDisable GL_TEXTURE_2D; 890 }
891
892 $self->SUPER::_draw;
691} 893}
692 894
693############################################################################# 895#############################################################################
694 896
695package CFClient::UI::FancyFrame; 897package CFClient::UI::FancyFrame;
706 my $class = shift; 908 my $class = shift;
707 909
708 # TODO: user_x, user_y, overwrite moveto? 910 # TODO: user_x, user_y, overwrite moveto?
709 911
710 my $self = $class->SUPER::new ( 912 my $self = $class->SUPER::new (
711 bg => [1, 1, 1, 1], 913 bg => [1, 1, 1, 1],
712 border_bg => [1, 1, 1, 1], 914 border_bg => [1, 1, 1, 1],
713 border => 0.8, 915 border => 0.6,
916 is_toplevel => 1,
714 can_events => 1, 917 can_events => 1,
715 @_ 918 @_
716 ); 919 );
717 920
718 $self->{title} &&= new CFClient::UI::Label 921 $self->{title} &&= new CFClient::UI::Label
719 align => 0, 922 align => 0,
720 valign => 1, 923 valign => 1,
721 text => $self->{title}, 924 text => $self->{title},
722 fontsize => 1; 925 fontsize => $self->{border};
723 926
724 $self 927 $self
725} 928}
726 929
727sub border { 930sub border {
728 int $_[0]{border} * $::FONTSIZE 931 int $_[0]{border} * $::FONTSIZE
729} 932}
730 933
731sub size_request { 934sub size_request {
732 my ($self) = @_; 935 my ($self) = @_;
733
734 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
735 936
736 my ($w, $h) = $self->SUPER::size_request; 937 my ($w, $h) = $self->SUPER::size_request;
737 938
738 ( 939 (
739 $w + $self->border * 2, 940 $w + $self->border * 2,
745 my ($self, $w, $h) = @_; 946 my ($self, $w, $h) = @_;
746 947
747 $h -= List::Util::max 0, $self->border * 2; 948 $h -= List::Util::max 0, $self->border * 2;
748 $w -= List::Util::max 0, $self->border * 2; 949 $w -= List::Util::max 0, $self->border * 2;
749 950
750 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 951 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
751 if $self->{title}; 952 if $self->{title};
752 953
753 $self->child->configure ($self->border, $self->border, $w, $h); 954 $self->child->configure ($self->border, $self->border, $w, $h);
754} 955}
755 956
756sub button_down { 957sub button_down {
757 my ($self, $ev, $x, $y) = @_; 958 my ($self, $ev, $x, $y) = @_;
758 959
960 my ($w, $h) = @$self{qw(w h)};
759 my $border = $self->border; 961 my $border = $self->border;
760 962
761 if ($x < $self->{w} && $x >= $self->{w} - $border 963 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
762 && $y < $self->{h} && $y >= $self->{h} - $border) { 964 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
763 965
966 if ($lr & $td) {
967 my ($wx, $wy) = ($self->{x}, $self->{y});
764 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 968 my ($ox, $oy) = ($ev->{x}, $ev->{y});
765 my ($bw, $bh) = ($self->{w}, $self->{h}); 969 my ($bw, $bh) = ($self->{w}, $self->{h});
766 970
971 my $mx = $x < $border;
972 my $my = $y < $border;
973
767 $self->{motion} = sub { 974 $self->{motion} = sub {
768 my ($ev, $x, $y) = @_; 975 my ($ev, $x, $y) = @_;
769 976
770 ($x, $y) = ($ev->{x}, $ev->{y}); 977 my $dx = $ev->{x} - $ox;
978 my $dy = $ev->{y} - $oy;
771 979
772 $self->{user_w} = $bw + $x - $ox; 980 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
773 $self->{user_h} = $bh + $y - $oy; 981 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
982 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
774 $self->check_size; 983 $self->check_size;
775 }; 984 };
776 985
777 } elsif ($x >= 0 && $x < $self->{w} 986 } elsif ($lr ^ $td) {
778 && $y >= 0 && $y < $border) {
779
780 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 987 my ($ox, $oy) = ($ev->{x}, $ev->{y});
781 my ($bx, $by) = ($self->{x}, $self->{y}); 988 my ($bx, $by) = ($self->{x}, $self->{y});
782 989
783 $self->{motion} = sub { 990 $self->{motion} = sub {
784 my ($ev, $x, $y) = @_; 991 my ($ev, $x, $y) = @_;
807 my ($self) = @_; 1014 my ($self) = @_;
808 1015
809 my ($w, $h ) = ($self->{w}, $self->{h}); 1016 my ($w, $h ) = ($self->{w}, $self->{h});
810 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1017 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
811 1018
812 glEnable GL_BLEND;
813 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
814 glEnable GL_TEXTURE_2D; 1019 glEnable GL_TEXTURE_2D;
815 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1020 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
816 1021
817 my $border = $self->border; 1022 my $border = $self->border;
818 1023
819 glColor @{ $self->{border_bg} }; 1024 glColor @{ $self->{border_bg} };
820 $tex[1]->draw_quad (0, 0, $w, $border); 1025 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
821 $tex[3]->draw_quad (0, $border, $border, $ch); 1026 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
822 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 1027 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
823 $tex[4]->draw_quad (0, $h - $border, $w, $border); 1028 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
824 1029
1030 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
825 my $bg = $tex[0]; 1031 my $bg = $tex[0];
826 1032
827 # TODO: repeat texture not scale 1033 # TODO: repeat texture not scale
828 my $rep_x = $cw / $bg->{w}; 1034 my $rep_x = $cw / $bg->{w};
829 my $rep_y = $ch / $bg->{h}; 1035 my $rep_y = $ch / $bg->{h};
830 1036
831 glColor @{ $self->{bg} }; 1037 glColor @{ $self->{bg} };
832 1038
833 $bg->{s} = $rep_x; 1039 $bg->{s} = $rep_x;
834 $bg->{t} = $rep_y; 1040 $bg->{t} = $rep_y;
835 $bg->{wrap_mode} = 1; 1041 $bg->{wrap_mode} = 1;
836 $bg->draw_quad ($border, $border, $cw, $ch); 1042 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1043 }
837 1044
838 glDisable GL_TEXTURE_2D; 1045 glDisable GL_TEXTURE_2D;
839 glDisable GL_BLEND;
840 1046
841 $self->{title}->draw if $self->{title}; 1047 $self->{title}->draw if $self->{title};
1048
842 $self->child->draw; 1049 $self->child->draw;
843} 1050}
844 1051
845############################################################################# 1052#############################################################################
846 1053
868 $self->{children}[$y][$x] = $child; 1075 $self->{children}[$y][$x] = $child;
869 1076
870 $child->check_size; 1077 $child->check_size;
871} 1078}
872 1079
1080sub children {
1081 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1082}
1083
873# TODO: move to container class maybe? send childs a signal on removal? 1084# TODO: move to container class maybe? send childs a signal on removal?
874sub clear { 1085sub clear {
875 my ($self) = @_; 1086 my ($self) = @_;
876 1087
1088 my @children = $self->children;
877 delete $self->{children}; 1089 delete $self->{children};
1090
1091 for (@children) {
1092 delete $_->{parent};
1093 $_->hide;
1094 }
1095
878 $self->update; 1096 $self->update;
879} 1097}
880 1098
881sub get_wh { 1099sub get_wh {
882 my ($self) = @_; 1100 my ($self) = @_;
1059} 1277}
1060 1278
1061sub size_allocate { 1279sub size_allocate {
1062 my ($self, $w, $h) = @_; 1280 my ($self, $w, $h) = @_;
1063 1281
1282 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1283
1064 my $children = $self->{children}; 1284 my $children = $self->{children};
1065 1285
1066 my @h = map $_->{req_h}, @$children; 1286 my @h = map $_->{req_h}, @$children;
1067 1287
1068 my $req_h = List::Util::sum @h; 1288 my $req_h = List::Util::sum @h;
1097 1317
1098############################################################################# 1318#############################################################################
1099 1319
1100package CFClient::UI::Label; 1320package CFClient::UI::Label;
1101 1321
1102our @ISA = CFClient::UI::Base::; 1322our @ISA = CFClient::UI::DrawBG::;
1103 1323
1104use CFClient::OpenGL; 1324use CFClient::OpenGL;
1105 1325
1106sub new { 1326sub new {
1107 my ($class, %arg) = @_; 1327 my ($class, %arg) = @_;
1108 1328
1109 my $self = $class->SUPER::new ( 1329 my $self = $class->SUPER::new (
1110 fg => [1, 1, 1], 1330 fg => [1, 1, 1],
1331 #bg => none
1332 #active_bg => none
1111 #font => default_font 1333 #font => default_font
1334 #text => initial text
1335 #markup => initial narkup
1336 #max_w => maximum pixel width
1337 ellipsise => 3, # end
1338 layout => (new CFClient::Layout),
1112 fontsize => 1, 1339 fontsize => 1,
1113 text => "",
1114 align => -1, 1340 align => -1,
1115 valign => -1, 1341 valign => -1,
1116 padding => 2, 1342 padding => 2,
1117 layout => new CFClient::Layout,
1118 can_events => 0, 1343 can_events => 0,
1119 %arg 1344 %arg
1120 ); 1345 );
1121 1346
1122 if (exists $self->{template}) { 1347 if (exists $self->{template}) {
1123 my $layout = new CFClient::Layout; 1348 my $layout = new CFClient::Layout;
1124 $layout->set_text (delete $self->{template}); 1349 $layout->set_text (delete $self->{template});
1125 $self->{template} = $layout; 1350 $self->{template} = $layout;
1126 } 1351 }
1127 1352
1128 $self->set_text (delete $self->{text}) if exists $self->{text}; 1353 if (exists $self->{markup}) {
1129 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1354 $self->set_markup (delete $self->{markup});
1355 } else {
1356 $self->set_text (delete $self->{text});
1357 }
1130 1358
1131 $self 1359 $self
1132} 1360}
1133 1361
1134sub escape_text { 1362sub escape($) {
1135 local $_ = $_[1]; 1363 local $_ = $_[0];
1136 1364
1137 s/&/&amp;/g; 1365 s/&/&amp;/g;
1138 s/>/&gt;/g; 1366 s/>/&gt;/g;
1139 s/</&lt;/g; 1367 s/</&lt;/g;
1140 1368
1141 $_[1] 1369 $_
1370}
1371
1372sub update {
1373 my ($self) = @_;
1374
1375 delete $self->{texture};
1376 $self->SUPER::update;
1142} 1377}
1143 1378
1144sub set_text { 1379sub set_text {
1145 my ($self, $text) = @_; 1380 my ($self, $text) = @_;
1146 1381
1382 return if $self->{text} eq "T$text";
1383 $self->{text} = "T$text";
1384
1385 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1147 $self->{layout}->set_text ($text); 1386 $self->{layout}->set_text ($text);
1148 1387
1149 delete $self->{texture}; 1388 $self->update;
1150 $self->check_size; 1389 $self->check_size;
1151 $self->update;
1152} 1390}
1153 1391
1154sub set_markup { 1392sub set_markup {
1155 my ($self, $markup) = @_; 1393 my ($self, $markup) = @_;
1156 1394
1395 return if $self->{text} eq "M$markup";
1396 $self->{text} = "M$markup";
1397
1398 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1399
1400 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1157 $self->{layout}->set_markup ($markup); 1401 $self->{layout}->set_markup ($markup);
1158 1402
1159 delete $self->{texture}; 1403 $self->update;
1160 $self->check_size; 1404 $self->check_size;
1161 $self->update;
1162} 1405}
1163 1406
1164sub size_request { 1407sub size_request {
1165 my ($self) = @_; 1408 my ($self) = @_;
1166 1409
1167 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1410 $self->{layout}->set_font ($self->{font}) if $self->{font};
1168 $self->{layout}->set_width ($self->{max_w} || -1); 1411 $self->{layout}->set_width ($self->{max_w} || -1);
1412 $self->{layout}->set_ellipsise ($self->{ellipsise});
1413 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1169 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1414 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1170 1415
1171 my ($w, $h) = $self->{layout}->size; 1416 my ($w, $h) = $self->{layout}->size;
1172 1417
1173 if (exists $self->{template}) { 1418 if (exists $self->{template}) {
1195sub set_fontsize { 1440sub set_fontsize {
1196 my ($self, $fontsize) = @_; 1441 my ($self, $fontsize) = @_;
1197 1442
1198 $self->{fontsize} = $fontsize; 1443 $self->{fontsize} = $fontsize;
1199 delete $self->{texture}; 1444 delete $self->{texture};
1445
1446 $self->update;
1200 $self->check_size; 1447 $self->check_size;
1201 $self->update;
1202} 1448}
1203 1449
1204sub _draw { 1450sub _draw {
1205 my ($self) = @_; 1451 my ($self) = @_;
1206 1452
1453 $self->SUPER::_draw; # draw background, if applicable
1454
1207 my $tex = $self->{texture} ||= do { 1455 my $tex = $self->{texture} ||= do {
1456 $self->{layout}->set_foreground (@{$self->{fg}});
1208 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1457 $self->{layout}->set_font ($self->{font}) if $self->{font};
1209 $self->{layout}->set_width ($self->{w}); 1458 $self->{layout}->set_width ($self->{w});
1459 $self->{layout}->set_ellipsise ($self->{ellipsise});
1460 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1210 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1461 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1462
1211 new_from_layout CFClient::Texture $self->{layout} 1463 my $tex = new_from_layout CFClient::Texture $self->{layout};
1464
1465 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1466 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1467 : ($self->{w} - $tex->{w}) * 0.5);
1468
1469 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1470 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1471 : ($self->{h} - $tex->{h}) * 0.5);
1472
1473 $tex
1212 }; 1474 };
1213 1475
1214 glEnable GL_BLEND;
1215 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1216 glEnable GL_TEXTURE_2D; 1476 glEnable GL_TEXTURE_2D;
1217 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1477 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1218 1478
1479 if ($tex->{format} == GL_ALPHA) {
1219 glColor @{$self->{fg}}; 1480 glColor @{$self->{fg}};
1220
1221 $self->{ox} = int (
1222 $self->{align} < 0 ? $self->{padding}
1223 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1224 : ($self->{w} - $tex->{w}) * 0.5
1225 );
1226
1227 $self->{oy} = int (
1228 $self->{valign} < 0 ? $self->{padding}
1229 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1230 : ($self->{h} - $tex->{h}) * 0.5
1231 );
1232
1233 $tex->draw_quad ($self->{ox}, $self->{oy}); 1481 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1482 } else {
1483 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1484 }
1234 1485
1235 glDisable GL_TEXTURE_2D; 1486 glDisable GL_TEXTURE_2D;
1236 glDisable GL_BLEND;
1237} 1487}
1238 1488
1239############################################################################# 1489#############################################################################
1240 1490
1241package CFClient::UI::EntryBase; 1491package CFClient::UI::EntryBase;
1246 1496
1247sub new { 1497sub new {
1248 my $class = shift; 1498 my $class = shift;
1249 1499
1250 $class->SUPER::new ( 1500 $class->SUPER::new (
1251 fg => [1, 1, 1], 1501 fg => [1, 1, 1],
1252 bg => [0, 0, 0, 0.2], 1502 bg => [0, 0, 0, 0.2],
1253 active_bg => [1, 1, 1, 0.5], 1503 active_bg => [1, 1, 1, 0.5],
1254 active_fg => [0, 0, 0], 1504 active_fg => [0, 0, 0],
1255 can_hover => 1, 1505 can_hover => 1,
1256 can_focus => 1, 1506 can_focus => 1,
1257 valign => 0, 1507 valign => 0,
1258 can_events => 1, 1508 can_events => 1,
1509 #text => ...
1259 @_ 1510 @_
1260 ) 1511 )
1261} 1512}
1262 1513
1263sub _set_text { 1514sub _set_text {
1274 1525
1275 $text =~ s/./*/g if $self->{hidden}; 1526 $text =~ s/./*/g if $self->{hidden};
1276 $self->{layout}->set_text ("$text "); 1527 $self->{layout}->set_text ("$text ");
1277 1528
1278 $self->emit (changed => $self->{text}); 1529 $self->emit (changed => $self->{text});
1279}
1280
1281sub get_text {
1282 $_[0]{text}
1283}
1284
1285sub size_request {
1286 my ($self) = @_;
1287
1288 my ($w, $h) = $self->SUPER::size_request;
1289
1290 ($w + 1, $h) # add 1 for cursor
1291}
1292
1293sub size_allocate {
1294 my ($self, $w, $h) = @_;
1295
1296 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1297} 1530}
1298 1531
1299sub set_text { 1532sub set_text {
1300 my ($self, $text) = @_; 1533 my ($self, $text) = @_;
1301 1534
1302 $self->{cursor} = length $text; 1535 $self->{cursor} = length $text;
1303 $self->_set_text ($text); 1536 $self->_set_text ($text);
1304 $self->update; 1537 $self->update;
1538 $self->check_size;
1539}
1540
1541sub get_text {
1542 $_[0]{text}
1543}
1544
1545sub size_request {
1546 my ($self) = @_;
1547
1548 my ($w, $h) = $self->SUPER::size_request;
1549
1550 ($w + 1, $h) # add 1 for cursor
1551}
1552
1553sub size_allocate {
1554 my ($self, $w, $h) = @_;
1555
1556 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1305} 1557}
1306 1558
1307sub key_down { 1559sub key_down {
1308 my ($self, $ev) = @_; 1560 my ($self, $ev) = @_;
1309 1561
1311 my $sym = $ev->{sym}; 1563 my $sym = $ev->{sym};
1312 my $uni = $ev->{unicode}; 1564 my $uni = $ev->{unicode};
1313 1565
1314 my $text = $self->get_text; 1566 my $text = $self->get_text;
1315 1567
1316 if ($sym == 8) { 1568 if ($uni == 8) {
1317 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1569 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1318 } elsif ($sym == 127) { 1570 } elsif ($uni == 127) {
1319 substr $text, $self->{cursor}, 1, ""; 1571 substr $text, $self->{cursor}, 1, "";
1320 } elsif ($sym == CFClient::SDLK_LEFT) { 1572 } elsif ($sym == CFClient::SDLK_LEFT) {
1321 --$self->{cursor} if $self->{cursor}; 1573 --$self->{cursor} if $self->{cursor};
1322 } elsif ($sym == CFClient::SDLK_RIGHT) { 1574 } elsif ($sym == CFClient::SDLK_RIGHT) {
1323 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1575 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1324 } elsif ($sym == CFClient::SDLK_HOME) { 1576 } elsif ($sym == CFClient::SDLK_HOME) {
1325 $self->{cursor} = 0; 1577 $self->{cursor} = 0;
1326 } elsif ($sym == CFClient::SDLK_END) { 1578 } elsif ($sym == CFClient::SDLK_END) {
1327 $self->{cursor} = length $text; 1579 $self->{cursor} = length $text;
1328 } elsif ($sym == 27) { 1580 } elsif ($uni == 27) {
1329 $self->emit ('escape'); 1581 $self->emit ('escape');
1330 } elsif ($uni) { 1582 } elsif ($uni) {
1331 substr $text, $self->{cursor}++, 0, chr $uni; 1583 substr $text, $self->{cursor}++, 0, chr $uni;
1332 } 1584 }
1333 1585
1334 $self->_set_text ($text); 1586 $self->_set_text ($text);
1335 $self->update; 1587 $self->update;
1588 $self->check_size;
1336} 1589}
1337 1590
1338sub focus_in { 1591sub focus_in {
1339 my ($self) = @_; 1592 my ($self) = @_;
1340 1593
1416 my ($self, $ev) = @_; 1669 my ($self, $ev) = @_;
1417 1670
1418 my $sym = $ev->{sym}; 1671 my $sym = $ev->{sym};
1419 1672
1420 if ($sym == 13) { 1673 if ($sym == 13) {
1674 unshift @{$self->{history}},
1675 my $txt = $self->get_text;
1676 $self->{history_pointer} = -1;
1677 $self->{history_saveback} = '';
1421 $self->emit (activate => $self->get_text); 1678 $self->emit (activate => $txt);
1422 $self->update; 1679 $self->update;
1680
1681 } elsif ($sym == CFClient::SDLK_UP) {
1682 if ($self->{history_pointer} < 0) {
1683 $self->{history_saveback} = $self->get_text;
1684 }
1685 if (@{$self->{history} || []} > 0) {
1686 $self->{history_pointer}++;
1687 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1688 $self->{history_pointer} = @{$self->{history} || []} - 1;
1689 }
1690 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1691 }
1692
1693 } elsif ($sym == CFClient::SDLK_DOWN) {
1694 $self->{history_pointer}--;
1695 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1696
1697 if ($self->{history_pointer} >= 0) {
1698 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1699 } else {
1700 $self->set_text ($self->{history_saveback});
1701 }
1423 1702
1424 } else { 1703 } else {
1425 $self->SUPER::key_down ($ev); 1704 $self->SUPER::key_down ($ev);
1426 } 1705 }
1427 1706
1441 1720
1442sub new { 1721sub new {
1443 my $class = shift; 1722 my $class = shift;
1444 1723
1445 $class->SUPER::new ( 1724 $class->SUPER::new (
1446 padding => 4, 1725 padding => 4,
1447 fg => [1, 1, 1], 1726 fg => [1, 1, 1],
1448 bg => [1, 1, 1, 0.2],
1449 active_fg => [0, 0, 1], 1727 active_fg => [0, 0, 1],
1450 can_hover => 1, 1728 can_hover => 1,
1451 align => 0, 1729 align => 0,
1452 valign => 0, 1730 valign => 0,
1453 can_events => 1, 1731 can_events => 1,
1454 @_ 1732 @_
1455 ) 1733 )
1456} 1734}
1457 1735
1471 1749
1472 if ($GRAB == $self) { 1750 if ($GRAB == $self) {
1473 $self->{fg} = $self->{active_fg}; 1751 $self->{fg} = $self->{active_fg};
1474 } 1752 }
1475 1753
1476 glEnable GL_BLEND;
1477 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1478 glEnable GL_TEXTURE_2D; 1754 glEnable GL_TEXTURE_2D;
1479 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1755 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1480 glColor 0, 0, 0, 1; 1756 glColor 0, 0, 0, 1;
1481 1757
1482 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1758 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1483 1759
1484 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1485 glDisable GL_BLEND;
1486 1761
1487 $self->SUPER::_draw; 1762 $self->SUPER::_draw;
1488} 1763}
1489 1764
1490############################################################################# 1765#############################################################################
1504 1779
1505 $class->SUPER::new ( 1780 $class->SUPER::new (
1506 padding => 2, 1781 padding => 2,
1507 fg => [1, 1, 1], 1782 fg => [1, 1, 1],
1508 active_fg => [1, 1, 0], 1783 active_fg => [1, 1, 0],
1784 bg => [0, 0, 0, 0.2],
1785 active_bg => [1, 1, 1, 0.5],
1509 state => 0, 1786 state => 0,
1510 can_hover => 1, 1787 can_hover => 1,
1511 @_ 1788 @_
1512 ) 1789 )
1513} 1790}
1537 1814
1538 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1815 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1539 1816
1540 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1817 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1541 1818
1542 glEnable GL_BLEND; 1819 my $tex = $self->{state} ? $tex[1] : $tex[0];
1820
1543 glEnable GL_TEXTURE_2D; 1821 glEnable GL_TEXTURE_2D;
1544 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1545
1546 my $tex = $self->{state} ? $tex[1] : $tex[0];
1547
1548 $tex->draw_quad (0, 0, $s, $s); 1822 $tex->draw_quad_alpha (0, 0, $s, $s);
1549
1550 glDisable GL_TEXTURE_2D; 1823 glDisable GL_TEXTURE_2D;
1551 glDisable GL_BLEND;
1552} 1824}
1553 1825
1554############################################################################# 1826#############################################################################
1555 1827
1556package CFClient::UI::Image; 1828package CFClient::UI::Image;
1599 glTranslate 0, -$self->{w}, 0; 1871 glTranslate 0, -$self->{w}, 0;
1600 1872
1601 ($w, $h) = ($h, $w); 1873 ($w, $h) = ($h, $w);
1602 } 1874 }
1603 1875
1604 glEnable GL_BLEND;
1605 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1606 glEnable GL_TEXTURE_2D; 1876 glEnable GL_TEXTURE_2D;
1607 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1877 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1608 1878
1609 $tex->draw_quad (0, 0, $w, $h); 1879 $tex->draw_quad_alpha (0, 0, $w, $h);
1610 1880
1611 glDisable GL_BLEND;
1612 glDisable GL_TEXTURE_2D; 1881 glDisable GL_TEXTURE_2D;
1613} 1882}
1614 1883
1615############################################################################# 1884#############################################################################
1616 1885
1617package CFClient::UI::VGauge; 1886package CFClient::UI::VGauge;
1618 1887
1619our @ISA = CFClient::UI::Base::; 1888our @ISA = CFClient::UI::Base::;
1889
1890use List::Util qw(min max);
1620 1891
1621use CFClient::OpenGL; 1892use CFClient::OpenGL;
1622 1893
1623my %tex = ( 1894my %tex = (
1624 food => [ 1895 food => [
1625 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1896 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1626 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1897 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1627 ], 1898 ],
1628 grace => [ 1899 grace => [
1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1900 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1630 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1901 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1631 ], 1902 ],
1632 hp => [ 1903 hp => [
1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1904 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1634 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1905 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1635 ], 1906 ],
1636 mana => [ 1907 mana => [
1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1908 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1638 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1909 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1639 ], 1910 ],
1640); 1911);
1641 1912
1642# eg. VGauge->new (gauge => 'food'), default gauge: food 1913# eg. VGauge->new (gauge => 'food'), default gauge: food
1643sub new { 1914sub new {
1662} 1933}
1663 1934
1664sub set_max { 1935sub set_max {
1665 my ($self, $max) = @_; 1936 my ($self, $max) = @_;
1666 1937
1938 return if $self->{max_val} == $max;
1939
1667 $self->{max_val} = $max; 1940 $self->{max_val} = $max;
1941 $self->update;
1668} 1942}
1669 1943
1670sub set_value { 1944sub set_value {
1671 my ($self, $val, $max) = @_; 1945 my ($self, $val, $max) = @_;
1672 1946
1673 $self->set_max ($max) 1947 $self->set_max ($max)
1674 if defined $max; 1948 if defined $max;
1675 1949
1676 $max = $self->{max_val}; 1950 return if $self->{val} == $val;
1951
1677 $self->{val} = $val; 1952 $self->{val} = $val;
1678
1679 $self->update; 1953 $self->update;
1680} 1954}
1681 1955
1682sub _draw { 1956sub _draw {
1683 my ($self) = @_; 1957 my ($self) = @_;
1684 1958
1685 my $tex = $tex{$self->{type}}; 1959 my $tex = $tex{$self->{type}};
1960 my ($t1, $t2, $t3) = @$tex;
1686 1961
1687 my ($w, $h) = ($self->{w}, $self->{h}); 1962 my ($w, $h) = ($self->{w}, $self->{h});
1688 1963
1689 if ($self->{vertical}) { 1964 if ($self->{vertical}) {
1690 glRotate 90, 0, 0, 1; 1965 glRotate 90, 0, 0, 1;
1692 1967
1693 ($w, $h) = ($h, $w); 1968 ($w, $h) = ($h, $w);
1694 } 1969 }
1695 1970
1696 my $ycut = $self->{val} / ($self->{max_val} || 1); 1971 my $ycut = $self->{val} / ($self->{max_val} || 1);
1697 $ycut = 1 if $self->{val} > $self->{max_val};
1698 1972
1699 my $t1 = $tex->[0]; 1973 my $ycut1 = max 0, min 1, $ycut;
1700 my $t2 = $tex->[1]; 1974 my $ycut2 = max 0, min 1, $ycut - 1;
1975
1976 my $h1 = $self->{h} * (1 - $ycut1);
1977 my $h2 = $self->{h} * (1 - $ycut2);
1701 1978
1702 glEnable GL_BLEND; 1979 glEnable GL_BLEND;
1703 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1980 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1704 glEnable GL_TEXTURE_2D; 1981 glEnable GL_TEXTURE_2D;
1705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1982 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1706 1983
1707 my $h1 = $self->{h} - $ycut * $self->{h};
1708 my $h2 = $ycut * $self->{h};
1709
1710 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1984 glBindTexture GL_TEXTURE_2D, $t1->{name};
1711 glBegin GL_QUADS; 1985 glBegin GL_QUADS;
1712 glTexCoord 0 , 0; glVertex 0 , 0; 1986 glTexCoord 0 , 0; glVertex 0 , 0;
1713 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1987 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1714 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1988 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1715 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1989 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1716 glEnd; 1990 glEnd;
1717 1991
1992 my $ycut1 = List::Util::min 1, $ycut;
1718 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1993 glBindTexture GL_TEXTURE_2D, $t2->{name};
1719 glBegin GL_QUADS; 1994 glBegin GL_QUADS;
1720 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1995 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1721 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1996 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1722 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1997 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1723 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1998 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1724 glEnd; 1999 glEnd;
2000
2001 if ($t3) {
2002 glBindTexture GL_TEXTURE_2D, $t3->{name};
2003 glBegin GL_QUADS;
2004 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
2005 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
2006 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
2007 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
2008 glEnd;
2009 }
1725 2010
1726 glDisable GL_BLEND; 2011 glDisable GL_BLEND;
1727 glDisable GL_TEXTURE_2D; 2012 glDisable GL_TEXTURE_2D;
1728} 2013}
1729 2014
1735 2020
1736sub new { 2021sub new {
1737 my ($class, %arg) = @_; 2022 my ($class, %arg) = @_;
1738 2023
1739 my $self = $class->SUPER::new ( 2024 my $self = $class->SUPER::new (
1740 tooltip => $arg{type}, 2025 tooltip => $arg{type},
2026 can_hover => 1,
2027 can_events => 1,
1741 %arg, 2028 %arg,
1742 ); 2029 );
1743 2030
1744 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); 2031 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1745 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); 2032 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1746 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); 2033 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1747 2034
1748 $self 2035 $self
1749} 2036}
1750 2037
1751sub set_fontsize { 2038sub set_fontsize {
1753 2040
1754 $self->{value}->set_fontsize ($fsize); 2041 $self->{value}->set_fontsize ($fsize);
1755 $self->{max} ->set_fontsize ($fsize); 2042 $self->{max} ->set_fontsize ($fsize);
1756} 2043}
1757 2044
2045sub set_max {
2046 my ($self, $max) = @_;
2047
2048 $self->{gauge}->set_max ($max);
2049 $self->{max}->set_text ($max);
2050}
2051
1758sub set_value { 2052sub set_value {
1759 my ($self, $val, $max) = @_; 2053 my ($self, $val, $max) = @_;
1760 2054
1761 $self->set_max ($max) 2055 $self->set_max ($max)
1762 if defined $max; 2056 if defined $max;
1763 2057
1764 $self->{gauge}->set_value ($val, $max); 2058 $self->{gauge}->set_value ($val, $max);
1765 $self->{value}->set_text ($val); 2059 $self->{value}->set_text ($val);
1766}
1767
1768sub set_max {
1769 my ($self, $max) = @_;
1770
1771 $self->{gauge}->set_max ($max);
1772 $self->{max}->set_text ($max);
1773} 2060}
1774 2061
1775############################################################################# 2062#############################################################################
1776 2063
1777package CFClient::UI::Slider; 2064package CFClient::UI::Slider;
1787 qw(s1_slider.png s1_slider_bg.png); 2074 qw(s1_slider.png s1_slider_bg.png);
1788 2075
1789sub new { 2076sub new {
1790 my $class = shift; 2077 my $class = shift;
1791 2078
1792 # range [value, low, high, page] 2079 # range [value, low, high, page, unit]
1793 2080
1794 # TODO: 0-width page 2081 # TODO: 0-width page
1795 # TODO: req_w/h are wrong with vertical 2082 # TODO: req_w/h are wrong with vertical
1796 # TODO: calculations are off 2083 # TODO: calculations are off
1797 my $self = $class->SUPER::new ( 2084 my $self = $class->SUPER::new (
1798 fg => [1, 1, 1], 2085 fg => [1, 1, 1],
1799 active_fg => [0, 0, 0], 2086 active_fg => [0, 0, 0],
2087 bg => [0, 0, 0, 0.2],
2088 active_bg => [1, 1, 1, 0.5],
1800 range => [0, 0, 100, 10], 2089 range => [0, 0, 100, 10, 0],
1801 req_w => 20, 2090 req_w => $::WIDTH / 80,
1802 req_h => 20, 2091 req_h => $::WIDTH / 80,
1803 vertical => 0, 2092 vertical => 0,
1804 can_hover => 1, 2093 can_hover => 1,
1805 inner_pad => 5, 2094 inner_pad => 0.02,
1806 @_ 2095 @_
1807 ); 2096 );
1808 2097
2098 $self->set_value ($self->{range}[0]);
2099 $self->update;
2100
1809 $self 2101 $self
2102}
2103
2104sub set_range {
2105 my ($self, $range) = @_;
2106
2107 $self->{range} = $range;
2108
2109 $self->update;
2110}
2111
2112sub set_value {
2113 my ($self, $value) = @_;
2114
2115 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2116
2117 $hi = $lo + 1 if $hi <= $lo;
2118
2119 $page = $hi - $lo if $page > $hi - $lo;
2120
2121 $value = $lo if $value < $lo;
2122 $value = $hi - $page if $value > $hi - $page;
2123
2124 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2125 if $unit;
2126
2127 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2128
2129 if ($value != $old_value) {
2130 $self->emit (changed => $value);
2131 $self->update;
2132 }
1810} 2133}
1811 2134
1812sub size_request { 2135sub size_request {
1813 my ($self) = @_; 2136 my ($self) = @_;
1814 2137
1820 2143
1821sub button_down { 2144sub button_down {
1822 my ($self, $ev, $x, $y) = @_; 2145 my ($self, $ev, $x, $y) = @_;
1823 2146
1824 $self->SUPER::button_down ($ev, $x, $y); 2147 $self->SUPER::button_down ($ev, $x, $y);
2148
2149 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2150
1825 $self->mouse_motion ($ev, $x, $y); 2151 $self->mouse_motion ($ev, $x, $y);
1826} 2152}
1827 2153
1828sub mouse_motion { 2154sub mouse_motion {
1829 my ($self, $ev, $x, $y) = @_; 2155 my ($self, $ev, $x, $y) = @_;
1830 2156
1831 if ($GRAB == $self) { 2157 if ($GRAB == $self) {
2158 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2159
2160 my (undef, $lo, $hi, $page) = @{$self->{range}};
2161
2162 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2163
2164 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2165 }
2166}
2167
2168sub update {
2169 my ($self) = @_;
2170
2171 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2172 $self->set_value ($self->{range}[0]);
2173
1832 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2174 my ($value, $lo, $hi, $page) = @{$self->{range}};
2175 my $range = ($hi - $page - $lo) || 1e-100;
1833 2176
1834 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2177 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
1835 2178
1836 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2179 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
1837 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 2180 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
1838 2181
1839 $x -= $inner_pad_px; # substract the padding 2182 $value = ($value - $lo) / $range;
1840 $x = $x * ($hi - $lo) / $inner_w + $lo; 2183 $value = $value * $self->{scale} + $self->{offset};
1841 $x = $lo if $x < $lo;
1842 $x = $hi - $page if $x > $hi - $page;
1843 $self->{range}[0] = $x;
1844 2184
1845 $self->emit (changed => $x); 2185 $self->{knob_x} = $value - $knob_w * 0.5;
1846 $self->update; 2186 $self->{knob_w} = $knob_w;
1847 } 2187 });
1848}
1849 2188
1850# the inner_* stuff is for generating a padding for the slider handle, 2189 $self->SUPER::update;
1851# so that the handle doesn't leave the texture. This calculation isn't 100%
1852# correct propably, but it does the job for now
1853sub _calc_inner_pad_px {
1854 my ($self, $w) = @_;
1855 ($w / 100) * $self->{inner_pad} # % to pixels
1856} 2190}
1857 2191
1858sub _draw { 2192sub _draw {
1859 my ($self) = @_; 2193 my ($self) = @_;
1860 2194
1861 $self->SUPER::_draw (); 2195 $self->SUPER::_draw ();
1862 2196
1863 my ($w, $h) = @$self{qw(w h)}; 2197 glScale $self->{w}, $self->{h};
1864 2198
1865 if ($self->{vertical}) { 2199 if ($self->{vertical}) {
1866 # draw a vertical slider like a rotated horizontal slider 2200 # draw a vertical slider like a rotated horizontal slider
1867 2201
2202 glTranslate 1, 0, 0;
1868 glRotate 90, 0, 0, 1; 2203 glRotate 90, 0, 0, 1;
1869 glTranslate 0, -$self->{w}, 0;
1870
1871 ($w, $h) = ($h, $w);
1872 } 2204 }
1873 2205
1874 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2206 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1875 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2207 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1876 2208
1877 my ($value, $lo, $hi, $page) = @{$self->{range}};
1878
1879 $hi = $value + 1 if $lo == $hi;
1880
1881 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1882 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1883
1884 $page = int $page * $inner_w / ($hi - $lo);
1885 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1886
1887 $w -= $page;
1888 $page &= ~1;
1889 glTranslate $page * 0.5, 0, 0;
1890 $page ||= 2;
1891
1892 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1893 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1894
1895 glEnable GL_BLEND;
1896 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1897 glEnable GL_TEXTURE_2D; 2209 glEnable GL_TEXTURE_2D;
1898 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2210 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1899 2211
1900 # draw background 2212 # draw background
1901 $tex[1]->draw_quad (0, 0, $w, $h); 2213 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
1902 2214
1903 # draw handle 2215 # draw handle
1904 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2216 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
1905 2217
1906 glDisable GL_BLEND;
1907 glDisable GL_TEXTURE_2D; 2218 glDisable GL_TEXTURE_2D;
1908} 2219}
2220
2221#############################################################################
2222
2223package CFClient::UI::ValSlider;
2224
2225our @ISA = CFClient::UI::HBox::;
2226
2227sub new {
2228 my ($class, %arg) = @_;
2229
2230 my $range = delete $arg{range};
2231
2232 my $self = $class->SUPER::new (
2233 slider => (new CFClient::UI::Slider expand => 1, range => $range),
2234 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}),
2235 to_value => sub { shift },
2236 from_value => sub { shift },
2237 %arg,
2238 );
2239
2240 $self->{slider}->connect (changed => sub {
2241 my ($self, $value) = @_;
2242 $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2243 $self->{parent}->emit (changed => $value);
2244 });
2245
2246# $self->{entry}->connect (changed => sub {
2247# my ($self, $value) = @_;
2248# $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2249# $self->{parent}->emit (changed => $value);
2250# });
2251
2252 $self->add ($self->{slider}, $self->{entry});
2253
2254 $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2255
2256 $self
2257}
2258
2259sub set_range { shift->{slider}->set_range (@_) }
2260sub set_value { shift->{slider}->set_value (@_) }
1909 2261
1910############################################################################# 2262#############################################################################
1911 2263
1912package CFClient::UI::TextView; 2264package CFClient::UI::TextView;
1913 2265
1917 2269
1918sub new { 2270sub new {
1919 my $class = shift; 2271 my $class = shift;
1920 2272
1921 my $self = $class->SUPER::new ( 2273 my $self = $class->SUPER::new (
1922 fontsize => 1, 2274 fontsize => 1,
2275 can_events => 0,
1923 #font => default_font 2276 #font => default_font
1924 @_, 2277 @_,
1925 2278
1926 layout => (new CFClient::Layout), 2279 layout => (new CFClient::Layout 1),
1927 par => [], 2280 par => [],
1928 height => 0, 2281 height => 0,
1929 children => [ 2282 children => [
1930 (new CFClient::UI::Empty expand => 1), 2283 (new CFClient::UI::Empty expand => 1),
1931 (new CFClient::UI::Slider vertical => 1), 2284 (new CFClient::UI::Slider vertical => 1),
1932 ], 2285 ],
1933 ); 2286 );
1934 2287
1935 $self->{children}[1]->connect (changed => sub { 2288 $self->{children}[1]->connect (changed => sub { $self->update });
1936 $self->update;
1937 });
1938 2289
1939 $self 2290 $self
1940} 2291}
1941 2292
1942sub set_fontsize { 2293sub set_fontsize {
1943 my ($self, $fontsize) = @_; 2294 my ($self, $fontsize) = @_;
1944 2295
1945 $self->{fontsize} = $fontsize; 2296 $self->{fontsize} = $fontsize;
1946 $self->reflow; 2297 $self->reflow;
1947}
1948
1949sub text_height {
1950 my ($self, $text) = @_;
1951
1952 my $layout = $self->{layout};
1953
1954 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1955 $layout->set_width ($self->{w});
1956 $layout->set_text ($text);
1957
1958 ($layout->size)[1]
1959}
1960
1961sub reflow {
1962 my ($self) = @_;
1963
1964 $self->{need_reflow}++;
1965 $self->update;
1966} 2298}
1967 2299
1968sub size_allocate { 2300sub size_allocate {
1969 my ($self, $w, $h) = @_; 2301 my ($self, $w, $h) = @_;
1970 2302
1975 $self->{layout}->set_width ($self->{children}[0]{w}); 2307 $self->{layout}->set_width ($self->{children}[0]{w});
1976 2308
1977 $self->reflow; 2309 $self->reflow;
1978} 2310}
1979 2311
2312sub text_size {
2313 my ($self, $text, $indent) = @_;
2314
2315 my $layout = $self->{layout};
2316
2317 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2318 $layout->set_width ($self->{children}[0]{w} - $indent);
2319 $layout->set_markup ($text);
2320
2321 $layout->size
2322}
2323
2324sub reflow {
2325 my ($self) = @_;
2326
2327 $self->{need_reflow}++;
2328 $self->update;
2329}
2330
2331sub set_offset {
2332 my ($self, $offset) = @_;
2333
2334 # todo: base offset on lines or so, not on pixels
2335 $self->{children}[1]->set_value ($offset);
2336}
2337
2338sub clear {
2339 my ($self) = @_;
2340
2341 $self->{par} = [];
2342 $self->{height} = 0;
2343 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2344}
2345
1980sub add_paragraph { 2346sub add_paragraph {
1981 my ($self, $color, $text) = @_; 2347 my ($self, $color, $text, $indent) = @_;
1982 2348
1983 #TODO: intelligently "reformat" paragraph 2349 for my $line (split /\n/, $text) {
1984 2350 my ($w, $h) = $self->text_size ($line);
1985 my $height = $self->text_height ($text);
1986
1987 $self->{height} += $height; 2351 $self->{height} += $h;
2352 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2353 }
1988 2354
1989 push @{$self->{par}}, [$height, $color, $text];
1990
1991 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2355 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
1992 $self->{children}[1]->update;
1993} 2356}
1994 2357
1995sub update { 2358sub update {
1996 my ($self) = @_; 2359 my ($self) = @_;
1997 2360
1999 2362
2000 return unless $self->{h} > 0; 2363 return unless $self->{h} > 0;
2001 2364
2002 delete $self->{texture}; 2365 delete $self->{texture};
2003 2366
2004 $ROOT->on_refresh ($self, sub { 2367 $ROOT->on_post_alloc ($self, sub {
2368 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2369
2005 if (delete $self->{need_reflow}) { 2370 if (delete $self->{need_reflow}) {
2006 my $height = 0; 2371 my $height = 0;
2007 2372
2008 $height += $_->[0] = $self->text_height ($_->[2]) 2373 my $layout = $self->{layout};
2374
2375 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2376
2009 for @{$self->{par}}; 2377 for (@{$self->{par}}) {
2378 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2379 $layout->set_width ($W - $_->[3]);
2380 $layout->set_markup ($_->[4]);
2381 my ($w, $h) = $layout->size;
2382 $_->[0] = $w + $_->[3];
2383 $_->[1] = $h;
2384 }
2385
2386 $height += $_->[1];
2387 }
2010 2388
2011 $self->{height} = $height; 2389 $self->{height} = $height;
2012 2390
2013 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2391 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2014 2392
2015 delete $self->{texture}; 2393 delete $self->{texture};
2016 } 2394 }
2017 2395
2018 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2396 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2019 glClearColor 0, 0, 0, 1; 2397 glClearColor 0.5, 0.5, 0.5, 0;
2020 glClear GL_COLOR_BUFFER_BIT; 2398 glClear GL_COLOR_BUFFER_BIT;
2021 2399
2400 my $top = int $self->{children}[1]{range}[0];
2401
2402 my $y0 = $top;
2403 my $y1 = $top + $H;
2404
2405 my $y = 0;
2406
2407 my $layout = $self->{layout};
2408
2409 $layout->set_font ($self->{font}) if $self->{font};
2410
2022 glEnable GL_BLEND; 2411 glEnable GL_BLEND;
2412 #TODO# not correct in windows where rgba is forced off
2023 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2413 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2024 glEnable GL_TEXTURE_2D;
2025 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2026
2027 my $top = int $self->{children}[1]{range}[0];
2028
2029 my $y0 = $top;
2030 my $y1 = $top + $self->{h};
2031
2032 my $y = 0;
2033
2034 my $layout = $self->{layout};
2035
2036 $layout->set_font ($self->{font}) if $self->{font};
2037 2414
2038 for my $par (@{$self->{par}}) { 2415 for my $par (@{$self->{par}}) {
2039 my $h = $par->[0]; 2416 my $h = $par->[1];
2040 2417
2041 if ($y0 < $y + $h && $y < $y1) { 2418 if ($y0 < $y + $h && $y < $y1) {
2419 $layout->set_foreground (@{ $par->[2] });
2420 $layout->set_width ($W - $par->[3]);
2042 $layout->set_text ($par->[2]); 2421 $layout->set_markup ($par->[4]);
2043 2422
2044 glColor @{ $par->[1] }; 2423 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2045 my ($W, $H) = $layout->size; 2424
2046 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2425 glRasterPos $par->[3], $y - $y0;
2426 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2047 } 2427 }
2048 2428
2049 $y += $h; 2429 $y += $h;
2050 } 2430 }
2051 2431
2052 glDisable GL_TEXTURE_2D;
2053 glDisable GL_BLEND; 2432 glDisable GL_BLEND;
2054 }; 2433 };
2055 }); 2434 });
2056} 2435}
2057 2436
2058sub _draw { 2437sub _draw {
2059 my ($self) = @_; 2438 my ($self) = @_;
2060 2439
2061 if ($self->{texture}) {
2062 glEnable GL_TEXTURE_2D; 2440 glEnable GL_TEXTURE_2D;
2063 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2441 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2442 glColor 1, 1, 1, 1;
2064 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2443 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2065 glDisable GL_TEXTURE_2D; 2444 glDisable GL_TEXTURE_2D;
2066 }
2067 2445
2068 $self->{children}[1]->draw; 2446 $self->{children}[1]->draw;
2069 2447
2070} 2448}
2071 2449
2119 2497
2120sub new { 2498sub new {
2121 my $class = shift; 2499 my $class = shift;
2122 2500
2123 my $self = $class->SUPER::new ( 2501 my $self = $class->SUPER::new (
2124 state => 0, 2502 state => 0,
2125 connect_activate => \&toggle_flopper, 2503 connect_activate => \&toggle_flopper,
2126 can_events => 1,
2127 @_ 2504 @_
2128 ); 2505 );
2129 2506
2130 if ($self->{state}) { 2507 if ($self->{state}) {
2131 $self->{state} = 0; 2508 $self->{state} = 0;
2166 @_, 2543 @_,
2167 can_events => 0, 2544 can_events => 0,
2168 ) 2545 )
2169} 2546}
2170 2547
2171sub set_text { 2548sub set_tooltip_from {
2172 my ($self, $text) = @_; 2549 my ($self, $widget) = @_;
2173 2550
2174 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2551 $self->add (new CFClient::UI::Label
2175 $self->{label}->set_text ($text); 2552 markup => $widget->{tooltip},
2176 $self->add ($self->{label}); 2553 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2554 fontsize => 0.8,
2555 fg => [0, 0, 0, 1],
2556 ellipsise => 0,
2557 font => ($widget->{tooltip_font} || $::FONT_PROP),
2558 );
2177} 2559}
2178 2560
2179sub size_request { 2561sub size_request {
2180 my ($self) = @_; 2562 my ($self) = @_;
2181 2563
2182 $self->child->set_max_size ($::WIDTH * 0.2);
2183
2184 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2564 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2185 2565
2186 ($w + 4, $h + 4) 2566 ($w + 4, $h + 4)
2187} 2567}
2188 2568
2569sub size_allocate {
2570 my ($self, $w, $h) = @_;
2571
2572 $self->SUPER::size_allocate ($w - 4, $h - 4);
2573}
2574
2189sub _draw { 2575sub _draw {
2190 my ($self) = @_; 2576 my ($self) = @_;
2191 2577
2192 glPushMatrix;
2193 glTranslate 0.375, 0.375; 2578 glTranslate 0.375, 0.375;
2194 2579
2195 my ($w, $h) = @$self{qw(w h)}; 2580 my ($w, $h) = @$self{qw(w h)};
2196 2581
2197 glColor 1, 0.8, 0.4; 2582 glColor 1, 0.8, 0.4;
2208 glVertex 0 , $h; 2593 glVertex 0 , $h;
2209 glVertex $w, $h; 2594 glVertex $w, $h;
2210 glVertex $w, 0; 2595 glVertex $w, 0;
2211 glEnd; 2596 glEnd;
2212 2597
2213 glPopMatrix; 2598 glTranslate 2 - 0.375, 2 - 0.375;
2214
2215 glTranslate 2, 2;
2216 $self->SUPER::_draw; 2599 $self->SUPER::_draw;
2217} 2600}
2218 2601
2219############################################################################# 2602#############################################################################
2220 2603
2604package CFClient::UI::Face;
2605
2606our @ISA = CFClient::UI::Base::;
2607
2608use CFClient::OpenGL;
2609
2610sub new {
2611 my $class = shift;
2612
2613 my $self = $class->SUPER::new (
2614 aspect => 1,
2615 @_,
2616 );
2617
2618 if ($self->{anim} && $self->{animspeed}) {
2619 Scalar::Util::weaken (my $widget = $self);
2620
2621 $self->{timer} = Event->timer (
2622 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2623 hard => 1,
2624 interval => $self->{animspeed},
2625 cb => sub {
2626 ++$widget->{frame};
2627 $widget->update;
2628 },
2629 );
2630 }
2631
2632 $self
2633}
2634
2635sub size_request {
2636 (32, 8)
2637}
2638
2639sub update {
2640 my ($self) = @_;
2641
2642 return unless $self->{visible};
2643
2644 $self->SUPER::update;
2645}
2646
2647sub _draw {
2648 my ($self) = @_;
2649
2650 return unless $::CONN;
2651
2652 my $face;
2653
2654 if ($self->{frame}) {
2655 my $anim = $::CONN->{anim}[$self->{anim}];
2656
2657 $face = $anim->[ $self->{frame} % @$anim ]
2658 if $anim && @$anim;
2659 }
2660
2661 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2662
2663 if ($tex) {
2664 glEnable GL_TEXTURE_2D;
2665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2666 glColor 1, 1, 1, 1;
2667 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2668 glDisable GL_TEXTURE_2D;
2669 }
2670}
2671
2672sub DESTROY {
2673 my ($self) = @_;
2674
2675 $self->{timer}->cancel
2676 if $self->{timer};
2677
2678 $self->SUPER::DESTROY;
2679}
2680
2681#############################################################################
2682
2683package CFClient::UI::InventoryItem;
2684
2685our @ISA = CFClient::UI::HBox::;
2686
2687sub _item_to_desc {
2688 my ($item) = @_;
2689
2690 my $desc =
2691 $item->{nrof} < 2
2692 ? $item->{name}
2693 : "$item->{nrof} × $item->{name_pl}";
2694
2695 $item->{flags} & Crossfire::Protocol::F_OPEN
2696 and $desc .= " (open)";
2697 $item->{flags} & Crossfire::Protocol::F_APPLIED
2698 and $desc .= " (applied)";
2699 $item->{flags} & Crossfire::Protocol::F_UNPAID
2700 and $desc .= " (unpaid)";
2701 $item->{flags} & Crossfire::Protocol::F_MAGIC
2702 and $desc .= " (magic)";
2703 $item->{flags} & Crossfire::Protocol::F_CURSED
2704 and $desc .= " (cursed)";
2705 $item->{flags} & Crossfire::Protocol::F_DAMNED
2706 and $desc .= " (damned)";
2707 $item->{flags} & Crossfire::Protocol::F_LOCKED
2708 and $desc .= " *";
2709
2710 $desc
2711}
2712
2713sub new {
2714 my $class = shift;
2715
2716 my %args = @_;
2717
2718 my $item = delete $args{item};
2719
2720 my $desc = _item_to_desc ($item);
2721
2722 my $self = $class->SUPER::new (
2723 can_hover => 1,
2724 can_events => 1,
2725 tooltip => ((CFClient::UI::Label::escape $desc)
2726 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2727 connect_button_down => sub {
2728 my ($self, $ev, $x, $y) = @_;
2729
2730 # todo: maybe put examine on 1? but should just be a tooltip :(
2731 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2732 my $targ = $::CONN->{player}{tag};
2733
2734 if ($item->{container} == $::CONN->{player}{tag}) {
2735 $targ = $main::OPENCONT;
2736 }
2737
2738 $::CONN->send ("move $targ $item->{tag} 0");
2739 } elsif ($ev->{button} == 1) {
2740 $::CONN->send ("examine $item->{tag}");
2741 } elsif ($ev->{button} == 2) {
2742 $::CONN->send ("apply $item->{tag}");
2743 } elsif ($ev->{button} == 3) {
2744 my @menu_items = (
2745 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2746 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2747 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2748 (
2749 $item->{flags} & Crossfire::Protocol::F_LOCKED
2750 ? (
2751 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2752 )
2753 : (
2754 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2755 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2756 )
2757 ),
2758 );
2759
2760 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2761 }
2762
2763 1
2764 },
2765 %args
2766 );
2767
2768
2769 $self->add (new CFClient::UI::Face
2770 can_events => 0,
2771 face => $item->{face},
2772 anim => $item->{anim},
2773 animspeed => $item->{animspeed},
2774 );
2775
2776 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2777
2778 $self->{item} = $item;
2779
2780 $self->update_item;
2781
2782 $self
2783}
2784
2785sub update_item {
2786 my ($self) = @_;
2787
2788 my $desc = _item_to_desc ($self->{item});
2789
2790 $self->{name_lbl}->set_text ($desc);
2791}
2792
2793#############################################################################
2794
2795package CFClient::UI::Inventory;
2796
2797our @ISA = CFClient::UI::ScrolledWindow::;
2798
2799sub new {
2800 my $class = shift;
2801
2802 my $self = $class->SUPER::new (
2803 scrolled => (new CFClient::UI::Table),
2804 @_,
2805 );
2806
2807 $self
2808}
2809
2810sub set_items {
2811 my ($self, $items) = @_;
2812
2813 $self->{scrolled}->clear;
2814 return unless $items;
2815
2816 my @items = sort {
2817 ($a->{type} <=> $b->{type})
2818 or ($a->{name} cmp $b->{name})
2819 } @$items;
2820
2821 $self->{real_items} = \@items;
2822
2823 for my $item (@items) {
2824 $item->{item} = $item;
2825 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2826 $item->update_item ();
2827 }
2828
2829 my $i = 0;
2830 for (@items) {
2831 $self->{scrolled}->add (0, $i, $_);
2832 my $nrof = $_->{item}->{nrof} || 1;
2833 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2834 }
2835
2836# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2837}
2838
2839sub size_request {
2840 my ($self) = @_;
2841 ($self->{req_w}, $self->{req_h});
2842}
2843
2844#############################################################################
2845
2846package CFClient::UI::Menu;
2847
2848our @ISA = CFClient::UI::FancyFrame::;
2849
2850use CFClient::OpenGL;
2851
2852sub new {
2853 my $class = shift;
2854
2855 my $self = $class->SUPER::new (
2856 items => [],
2857 z => 100,
2858 @_,
2859 );
2860
2861 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2862
2863 for my $item (@{ $self->{items} }) {
2864 my ($widget, $cb) = @$item;
2865
2866 # handle various types of items, only text for now
2867 if (!ref $widget) {
2868 $widget = new CFClient::UI::Label
2869 can_hover => 1,
2870 can_events => 1,
2871 text => $widget;
2872 }
2873
2874 $self->{item}{$widget} = $item;
2875
2876 $self->{vbox}->add ($widget);
2877 }
2878
2879 $self
2880}
2881
2882# popup given the event (must be a mouse button down event currently)
2883sub popup {
2884 my ($self, $ev) = @_;
2885
2886 $self->emit ("popdown");
2887
2888 # maybe save $GRAB? must be careful about events...
2889 $GRAB = $self;
2890 $self->{button} = $ev->{button};
2891
2892 $self->show;
2893 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2894}
2895
2896sub mouse_motion {
2897 my ($self, $ev, $x, $y) = @_;
2898
2899 # TODO: should use vbox->find_widget or so
2900 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2901 $self->{hover} = $self->{item}{$HOVER};
2902}
2903
2904sub button_up {
2905 my ($self, $ev, $x, $y) = @_;
2906
2907 if ($ev->{button} == $self->{button}) {
2908 undef $GRAB;
2909 $self->hide;
2910
2911 $self->emit ("popdown");
2912 $self->{hover}[1]->() if $self->{hover};
2913 }
2914}
2915
2916#############################################################################
2917
2918package CFClient::UI::Statusbox;
2919
2920our @ISA = CFClient::UI::VBox::;
2921
2922sub new {
2923 my $class = shift;
2924
2925 $class->SUPER::new (
2926 fontsize => 0.8,
2927 @_,
2928 )
2929}
2930
2931sub reorder {
2932 my ($self) = @_;
2933 my $NOW = time;
2934
2935 while (my ($k, $v) = each %{ $self->{item} }) {
2936 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2937 }
2938
2939 my @widgets;
2940
2941 my @items = sort {
2942 $a->{pri} <=> $b->{pri}
2943 or $b->{id} <=> $a->{id}
2944 } values %{ $self->{item} };
2945
2946 my $count = 10 + 1;
2947 for my $item (@items) {
2948 last unless --$count;
2949
2950 push @widgets, $item->{label} ||= do {
2951 # TODO: doesn't handle markup well (read as: at all)
2952 my $short = $item->{count} > 1
2953 ? "<b>$item->{count} ×</b> $item->{text}"
2954 : $item->{text};
2955
2956 for ($short) {
2957 s/^\s+//;
2958 s/\s+/ /g;
2959 }
2960
2961 new CFClient::UI::Label
2962 markup => $short,
2963 tooltip => $item->{tooltip},
2964 tooltip_font => $::FONT_PROP,
2965 tooltip_width => 0.67,
2966 fontsize => $item->{fontsize} || $self->{fontsize},
2967 max_w => $::WIDTH * 0.44,
2968 fg => $item->{fg},
2969 can_events => 1,
2970 can_hover => 1
2971 };
2972 }
2973
2974 $self->clear;
2975 $self->SUPER::add (reverse @widgets);
2976}
2977
2978sub add {
2979 my ($self, $text, %arg) = @_;
2980
2981 $text =~ s/^\s+//;
2982 $text =~ s/\s+$//;
2983
2984 my $timeout = time + ((delete $arg{timeout}) || 60);
2985
2986 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2987
2988 if (my $item = $self->{item}{$group}) {
2989 if ($item->{text} eq $text) {
2990 $item->{count}++;
2991 } else {
2992 $item->{count} = 1;
2993 $item->{text} = $item->{tooltip} = $text;
2994 }
2995 $item->{id} = ++$self->{id};
2996 $item->{timeout} = $timeout;
2997 delete $item->{label};
2998 } else {
2999 $self->{item}{$group} = {
3000 id => ++$self->{id},
3001 text => $text,
3002 timeout => $timeout,
3003 tooltip => $text,
3004 fg => [0.8, 0.8, 0.8, 0.8],
3005 pri => 0,
3006 count => 1,
3007 %arg,
3008 };
3009 }
3010
3011 $self->reorder;
3012}
3013
3014sub reconfigure {
3015 my ($self) = @_;
3016
3017 delete $_->{label}
3018 for values %{ $self->{item} || {} };
3019
3020 $self->reorder;
3021 $self->SUPER::reconfigure;
3022}
3023
3024#############################################################################
3025
2221package CFClient::UI::Root; 3026package CFClient::UI::Root;
2222 3027
2223our @ISA = CFClient::UI::Container::; 3028our @ISA = CFClient::UI::Container::;
2224 3029
2225use CFClient::OpenGL; 3030use CFClient::OpenGL;
2226 3031
2227sub check_size { 3032sub new {
2228 my ($self) = @_; 3033 my $class = shift;
2229 3034
2230 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 3035 $class->SUPER::new (
2231} 3036 @_,
2232 3037 )
2233sub size_request {
2234 ($::WIDTH, $::HEIGHT)
2235} 3038}
2236 3039
2237sub configure { 3040sub configure {
2238 my ($self, $x, $y, $w, $h) = @_; 3041 my ($self, $x, $y, $w, $h) = @_;
2239 3042
2240 $self->SUPER::configure ($x, $y, $w, $h); 3043 $self->{w} = $w;
3044 $self->{h} = $h;
3045}
2241 3046
3047sub check_size {
3048 my ($self) = @_;
3049
3050 $self->size_allocate ($self->{w}, $self->{h})
3051 if $self->{w};
3052}
3053
3054sub size_request {
3055 my ($self) = @_;
3056
3057 ($self->{w}, $self->{h})
3058}
3059
3060sub size_allocate {
3061 my ($self, $w, $h) = @_;
3062
2242 for my $child (@{$self->{children}}) { 3063 for my $child ($self->children) {
2243 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3064 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2244 3065
3066 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3067 if exists $child->{req_x};
3068
3069 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3070 if exists $child->{req_y};
3071
2245 $X = List::Util::max 0, List::Util::min $w - $W, $X; 3072 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2246 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 3073 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3074
2247 $child->configure ($X, $Y, $W,$H); 3075 $child->configure ($X, $Y, $W, $H);
2248 } 3076 }
2249} 3077}
2250 3078
2251sub _topleft { 3079sub coord2local {
2252 my ($self, $x, $y) = @_; 3080 my ($self, $x, $y) = @_;
2253 3081
2254 ($x, $y) 3082 ($x, $y)
2255} 3083}
2256 3084
3085sub coord2global {
3086 my ($self, $x, $y) = @_;
3087
3088 ($x, $y)
3089}
3090
2257sub update { 3091sub update {
2258 my ($self) = @_; 3092 my ($self) = @_;
2259 3093
2260 $self->check_size; 3094 $self->check_size;
2261 ::refresh (); 3095 $::WANT_REFRESH++;
2262} 3096}
2263 3097
2264sub add { 3098sub add {
2265 my ($self, $child) = @_; 3099 my ($self, @children) = @_;
2266 3100
3101 for (my @widgets = @children; my $w = pop @widgets; ) {
3102 push @widgets, $w->children;
3103 $w->{root} = $self;
3104 $w->{visible} = 1;
3105 }
3106
3107 for my $child (@children) {
3108 $child->{is_toplevel} = 1;
3109
2267 # integerize window positions 3110 # integerise window positions
2268 $child->{x} = int $child->{x}; 3111 $child->{x} = int $child->{x};
2269 $child->{y} = int $child->{y}; 3112 $child->{y} = int $child->{y};
3113 }
2270 3114
2271 $self->SUPER::add ($child); 3115 $self->SUPER::add (@children);
3116}
3117
3118sub remove {
3119 my ($self, @children) = @_;
3120
3121 $self->SUPER::remove (@children);
3122
3123 while (@children) {
3124 my $w = pop @children;
3125 push @children, $w->children;
3126 delete $w->{visible};
3127 }
2272} 3128}
2273 3129
2274sub on_refresh { 3130sub on_refresh {
2275 my ($self, $id, $cb) = @_; 3131 my ($self, $id, $cb) = @_;
2276 3132
2277 $self->{refresh_hook}{$id} = $cb; 3133 $self->{refresh_hook}{$id} = $cb;
2278} 3134}
2279 3135
3136sub on_post_alloc {
3137 my ($self, $id, $cb) = @_;
3138
3139 $self->{post_alloc_hook}{$id} = $cb;
3140}
3141
2280sub draw { 3142sub draw {
2281 my ($self) = @_; 3143 my ($self) = @_;
2282 3144
2283 while (my $rcb = delete $self->{refresh_hook}) { 3145 while ($self->{refresh_hook}) {
2284 $_->() for values %$rcb; 3146 $_->()
3147 for values %{delete $self->{refresh_hook}};
3148 }
3149
3150 if ($self->{check_size}) {
3151 my @queue = ([], []);
3152
3153 for (;;) {
3154 if ($self->{check_size}) {
3155 # heuristic: check containers last
3156 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
3157 for values %{delete $self->{check_size}}
3158 }
3159
3160 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
3161
3162 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3163 ? @$widget{qw(user_w user_h)}
3164 : $widget->size_request;
3165
3166 if (delete $widget->{force_alloc}
3167 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3168 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3169
3170 $widget->{req_w} = $w;
3171 $widget->{req_h} = $h;
3172
3173 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3174
3175 $widget->{parent}->check_size
3176 if $widget->{parent};
3177 }
3178 }
3179 }
3180
3181 while ($self->{size_alloc}) {
3182 for (values %{delete $self->{size_alloc}}) {
3183 my ($widget, $w, $h) = @$_;
3184
3185 $w = 0 if $w < 0;
3186 $h = 0 if $h < 0;
3187
3188 $widget->{w} = $w;
3189 $widget->{h} = $h;
3190 $widget->size_allocate ($w, $h);
3191 $widget->emit (size_allocate => $w, $h);
3192 }
3193 }
3194
3195 while ($self->{post_alloc_hook}) {
3196 $_->()
3197 for values %{delete $self->{post_alloc_hook}};
2285 } 3198 }
2286 3199
2287 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3200 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2288 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3201 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2289 glClear GL_COLOR_BUFFER_BIT; 3202 glClear GL_COLOR_BUFFER_BIT;
2290 3203
2291 glMatrixMode GL_PROJECTION; 3204 glMatrixMode GL_PROJECTION;
2292 glLoadIdentity; 3205 glLoadIdentity;
2293 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3206 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2294 glMatrixMode GL_MODELVIEW; 3207 glMatrixMode GL_MODELVIEW;
2295 glLoadIdentity; 3208 glLoadIdentity;
2296 3209
2297 $self->_draw; 3210 $self->_draw;
2298} 3211}
2300############################################################################# 3213#############################################################################
2301 3214
2302package CFClient::UI; 3215package CFClient::UI;
2303 3216
2304$ROOT = new CFClient::UI::Root; 3217$ROOT = new CFClient::UI::Root;
2305$TOOLTIP = new CFClient::UI::Tooltip; 3218$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2306 3219
23071 32201
2308 3221

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines