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.212 by elmex, Wed May 17 14:55:14 2006 UTC vs.
Revision 1.254 by root, Mon May 29 01:53:23 2006 UTC

5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8 8
9use CFClient; 9use CFClient;
10use CFClient::Texture;
10 11
11our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
12 13
14our $LAYOUT;
13our $ROOT; 15our $ROOT;
14our $TOOLTIP; 16our $TOOLTIP;
15our $BUTTON_STATE; 17our $BUTTON_STATE;
16 18
17our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20
21sub get_layout {
22 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = {
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 };
29 }
30
31 return $LAYOUT;
32}
33
34sub set_layout {
35 my ($layout) = @_;
36 $LAYOUT = $layout;
37}
18 38
19sub check_tooltip { 39sub check_tooltip {
20 if (!$GRAB) { 40 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
23 43
24 if ($TOOLTIP->{owner} != $widget) { 44 if ($TOOLTIP->{owner} != $widget) {
45 $TOOLTIP->hide;
46
25 $TOOLTIP->{owner} = $widget; 47 $TOOLTIP->{owner} = $widget;
26 48
27 my $tip = $widget->{tooltip}; 49 my $tip = $widget->{tooltip};
28 50
29 $tip = $tip->($widget) if CODE:: eq ref $tip; 51 $tip = $tip->($widget) if CODE:: eq ref $tip;
30 52
31 $TOOLTIP->set_tooltip_from ($widget); 53 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show; 54 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH;
38
39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
42 } 55 }
43 56
44 return; 57 return;
45 } 58 }
46 } 59 }
50 delete $TOOLTIP->{owner}; 63 delete $TOOLTIP->{owner};
51} 64}
52 65
53# class methods for events 66# class methods for events
54sub feed_sdl_key_down_event { 67sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 68 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 69 if $FOCUS;
57} 70}
58 71
59sub feed_sdl_key_up_event { 72sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 73 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 74 if $FOCUS;
62} 75}
63 76
64sub feed_sdl_button_down_event { 77sub feed_sdl_button_down_event {
65 my ($ev) = @_; 78 my ($ev) = @_;
74 check_tooltip; 87 check_tooltip;
75 } 88 }
76 89
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 90 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 91
79 if ($GRAB) { 92 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 93 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 94}
84 95
85sub feed_sdl_button_up_event { 96sub feed_sdl_button_up_event {
86 my ($ev) = @_; 97 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 98 my ($x, $y) = ($ev->{x}, $ev->{y});
88 99
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 100 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 101
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 102 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 103
93 if ($GRAB) { 104 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 105 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 106
98 if (!$BUTTON_STATE) { 107 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 108 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 109 $grab->update if $grab;
101 $GRAB->update if $GRAB; 110 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 126 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 127
119 check_tooltip; 128 check_tooltip;
120 } 129 }
121 130
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 131 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 132 if $HOVER;
126} 133}
127 134
128# convert position array to integers 135# convert position array to integers
129sub harmonize { 136sub harmonize {
130 my ($vals) = @_; 137 my ($vals) = @_;
136 $rem += $_ - $i; 143 $rem += $_ - $i;
137 $_ = $i; 144 $_ = $i;
138 } 145 }
139} 146}
140 147
148sub full_refresh {
149 # make a copy, otherwise for complains about freed values.
150 my @widgets = values %WIDGET;
151
152 $_->update
153 for @widgets;
154}
155
156sub reconfigure_widgets {
157 # make a copy, otherwise C<for> complains about freed values.
158 my @widgets = values %WIDGET;
159
160 $_->reconfigure
161 for @widgets;
162}
163
141# call when resolution changes etc. 164# call when resolution changes etc.
142sub rescale_widgets { 165sub rescale_widgets {
143 my ($sx, $sy) = @_; 166 my ($sx, $sy) = @_;
144 167
145 for my $widget (values %WIDGET) { 168 for my $widget (values %WIDGET) {
146 if ($widget->{toplevel}) { 169 if ($widget->{is_toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 170 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
148 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 171 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
149 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 172 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
150 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
151 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 173 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
152 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 174 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
153 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 175 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
154 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
155 } 176 }
156
157 $widget->reconfigure;
158 } 177 }
178
179 reconfigure_widgets;
159} 180}
160 181
161############################################################################# 182#############################################################################
162 183
163package CFClient::UI::Base; 184package CFClient::UI::Base;
176 can_events => 1, 197 can_events => 1,
177 @_ 198 @_
178 }, $class; 199 }, $class;
179 200
180 for (keys %$self) { 201 for (keys %$self) {
181 if (/^connect_(.*)$/) { 202 if (/^on_(.*)$/) {
182 $self->connect ($1 => delete $self->{$_}); 203 $self->connect ($1 => delete $self->{$_});
183 } 204 }
184 } 205 }
185 206
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 207 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187 208
209 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
210 $self->{req_x} = $layout->{x} * $::WIDTH;
211 $self->{req_y} = $layout->{y} * $::HEIGHT;
212 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
213 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
214 }
215
188 $self 216 $self
189} 217}
190 218
191sub destroy { 219sub destroy {
192 my ($self) = @_; 220 my ($self) = @_;
201 return if $self->{parent}; 229 return if $self->{parent};
202 230
203 $CFClient::UI::ROOT->add ($self); 231 $CFClient::UI::ROOT->add ($self);
204} 232}
205 233
206sub hide { 234sub center {
207 my ($self) = @_; 235 my ($self) = @_;
236
237 $CFClient::UI::ROOT->on_post_alloc (
238 "center_$self" => sub {
239 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
240 },
241 );
242
243 $self->update;
244}
245
246sub set_visible {
247 my ($self) = @_;
248
249 return if $self->{visible};
250
251 $self->{root} = $self->{parent}{root};
252 $self->{visible} = $self->{parent}{visible} + 1;
253
254 $self->emit (visibility_change => 1);
255
256 $self->realloc if !exists $self->{req_w};
257
258 $_->set_visible for $self->children;
259}
260
261sub set_invisible {
262 my ($self) = @_;
263
264 return unless $self->{visible};
265
266 $_->set_invisible for $self->children;
267
268 delete $self->{root};
269 delete $self->{visible};
208 270
209 undef $GRAB if $GRAB == $self; 271 undef $GRAB if $GRAB == $self;
210 undef $HOVER if $HOVER == $self; 272 undef $HOVER if $HOVER == $self;
273
274 CFClient::UI::check_tooltip
275 if $TOOLTIP->{owner} == $self;
276
277 $self->focus_out;
278
279 $self->emit (visibility_change => 0);
280}
281
282sub set_visibility {
283 my ($self, $visible) = @_;
284
285 return if $self->{visible} == $visible;
286
287 $visible ? $self->hide
288 : $self->show;
289}
290
291sub toggle_visibility {
292 my ($self) = @_;
293
294 $self->{visible}
295 ? $self->hide
296 : $self->show;
297}
298
299sub hide {
300 my ($self) = @_;
301
302 $self->set_invisible;
211 303
212 $self->{parent}->remove ($self) 304 $self->{parent}->remove ($self)
213 if $self->{parent}; 305 if $self->{parent};
214} 306}
215 307
224} 316}
225 317
226sub set_size { 318sub set_size {
227 my ($self, $w, $h) = @_; 319 my ($self, $w, $h) = @_;
228 320
229 $self->{user_w} = $w; 321 $self->{def_w} = $w;
230 $self->{user_h} = $h; 322 $self->{def_h} = $h;
231 323
232 $self->check_size; 324 $self->realloc;
233} 325}
234 326
235sub size_request { 327sub size_request {
236 require Carp; 328 require Carp;
237 Carp::confess "size_request is abstract"; 329 Carp::confess "size_request is abstract";
257 $self->{y} = $y; 349 $self->{y} = $y;
258 $self->update; 350 $self->update;
259 } 351 }
260 352
261 if ($self->{w} != $w || $self->{h} != $h) { 353 if ($self->{w} != $w || $self->{h} != $h) {
354 return unless $self->{visible};
355
262 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 356 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
263 } 357 }
264} 358}
265 359
266sub size_allocate { 360sub size_allocate {
267 # nothing to be done 361 # nothing to be done
268} 362}
269 363
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
277sub children { 364sub children {
278} 365}
279 366
280sub set_max_size { 367sub set_max_size {
281 my ($self, $w, $h) = @_; 368 my ($self, $w, $h) = @_;
284 delete $self->{max_h}; $self->{max_h} = $h if $h; 371 delete $self->{max_h}; $self->{max_h} = $h if $h;
285} 372}
286 373
287sub set_tooltip { 374sub set_tooltip {
288 my ($self, $tooltip) = @_; 375 my ($self, $tooltip) = @_;
376
377 $tooltip =~ s/^\s+//;
378 $tooltip =~ s/\s+$//;
379
380 return if $self->{tooltip} eq $tooltip;
289 381
290 $self->{tooltip} = $tooltip; 382 $self->{tooltip} = $tooltip;
291 383
292 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 384 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
293 delete $CFClient::UI::TOOLTIP->{owner}; 385 delete $CFClient::UI::TOOLTIP->{owner};
315 return if $FOCUS == $self; 407 return if $FOCUS == $self;
316 return unless $self->{can_focus}; 408 return unless $self->{can_focus};
317 409
318 my $focus = $FOCUS; $FOCUS = $self; 410 my $focus = $FOCUS; $FOCUS = $self;
319 411
320 $self->emit (focus_in => $focus); 412 $self->_emit (focus_in => $focus);
321 413
322 $focus->update if $focus; 414 $focus->update if $focus;
323 $FOCUS->update; 415 $FOCUS->update;
324} 416}
325 417
328 420
329 return unless $FOCUS == $self; 421 return unless $FOCUS == $self;
330 422
331 my $focus = $FOCUS; undef $FOCUS; 423 my $focus = $FOCUS; undef $FOCUS;
332 424
333 $self->emit (focus_out => $focus); 425 $self->_emit (focus_out => $focus);
334 426
335 $focus->update if $focus; #? 427 $focus->update if $focus; #?
428
429 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
430 unless $FOCUS;
336} 431}
337 432
338sub mouse_motion { } 433sub mouse_motion { }
339sub button_up { } 434sub button_up { }
340sub key_down { } 435sub key_down { }
341sub key_up { } 436sub key_up { }
342 437
343sub button_down { 438sub button_down {
344 my ($self, $ev, $x, $y) = @_; 439 my ($self, $ev, $x, $y) = @_;
345 440
346 $self->focus_in; 441 $self->focus_in;
349sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 444sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
350sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 445sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
351sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 446sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
352sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 447sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
353sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 448sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
449
450sub find_widget {
451 my ($self, $x, $y) = @_;
452
453 return () unless $self->{can_events};
454
455 return $self
456 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
457 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
458
459 ()
460}
461
462sub set_parent {
463 my ($self, $parent) = @_;
464
465 Scalar::Util::weaken ($self->{parent} = $parent);
466
467 $self->set_visible if $parent->{visible};
468}
469
470sub connect {
471 my ($self, $signal, $cb) = @_;
472
473 push @{ $self->{signal_cb}{$signal} }, $cb;
474}
475
476sub _emit {
477 my ($self, $signal, @args) = @_;
478
479 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
480}
481
482sub emit {
483 my ($self, $signal, @args) = @_;
484
485 $self->_emit ($signal, @args)
486 || $self->$signal (@args);
487}
488
489sub visibility_change {
490 #my ($self, $visible) = @_;
491}
492
493sub realloc {
494 my ($self) = @_;
495
496 if ($self->{visible}) {
497 return if $self->{root}{realloc}{$self};
498
499 $self->{root}{realloc}{$self} = $self;
500 $self->{root}->update;
501 } else {
502 delete $self->{req_w};
503 }
504}
505
506sub update {
507 my ($self) = @_;
508
509 $self->{parent}->update
510 if $self->{parent};
511}
354 512
355sub draw { 513sub draw {
356 my ($self) = @_; 514 my ($self) = @_;
357 515
358 return unless $self->{h} && $self->{w}; 516 return unless $self->{h} && $self->{w};
380 if ($ENV{PCLIENT_DEBUG}) { 538 if ($ENV{PCLIENT_DEBUG}) {
381 glPushMatrix; 539 glPushMatrix;
382 glColor 1, 1, 0, 1; 540 glColor 1, 1, 0, 1;
383 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 541 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
384 glBegin GL_LINE_LOOP; 542 glBegin GL_LINE_LOOP;
385 glVertex 0 , 0; 543 glVertex 0 , 0;
386 glVertex $self->{w}, 0; 544 glVertex $self->{w} - 1, 0;
387 glVertex $self->{w}, $self->{h}; 545 glVertex $self->{w} - 1, $self->{h} - 1;
388 glVertex 0 , $self->{h}; 546 glVertex 0 , $self->{h} - 1;
389 glEnd; 547 glEnd;
390 glPopMatrix; 548 glPopMatrix;
391 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 549 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
392 } 550 }
393} 551}
394 552
395sub _draw { 553sub _draw {
396 my ($self) = @_; 554 my ($self) = @_;
397 555
398 warn "no draw defined for $self\n"; 556 warn "no draw defined for $self\n";
399}
400
401sub find_widget {
402 my ($self, $x, $y) = @_;
403
404 return () unless $self->{can_events};
405
406 return $self
407 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
408 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
409
410 ()
411}
412
413sub set_parent {
414 my ($self, $parent) = @_;
415
416 Scalar::Util::weaken ($self->{parent} = $parent);
417
418 # TODO: req_w _does_change after ->reconfigure
419 $self->check_size
420 unless exists $self->{req_w};
421}
422
423sub check_size {
424 my ($self, $forced) = @_;
425
426 $self->{force_alloc} = 1 if $forced;
427 $CFClient::UI::ROOT->{check_size}{$self} = $self;
428}
429
430sub update {
431 my ($self) = @_;
432
433 $self->{parent}->update
434 if $self->{parent};
435}
436
437sub connect {
438 my ($self, $signal, $cb) = @_;
439
440 push @{ $self->{signal_cb}{$signal} }, $cb;
441}
442
443sub emit {
444 my ($self, $signal, @args) = @_;
445
446 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
447} 557}
448 558
449sub DESTROY { 559sub DESTROY {
450 my ($self) = @_; 560 my ($self) = @_;
451 561
548 $self->{children} = [ 658 $self->{children} = [
549 sort { $a->{z} <=> $b->{z} } 659 sort { $a->{z} <=> $b->{z} }
550 @{$self->{children}}, @widgets 660 @{$self->{children}}, @widgets
551 ]; 661 ];
552 662
553 $self->check_size (1); 663 $self->realloc;
554 $self->update;
555} 664}
556 665
557sub children { 666sub children {
558 @{ $_[0]{children} } 667 @{ $_[0]{children} }
559} 668}
564 delete $child->{parent}; 673 delete $child->{parent};
565 $child->hide; 674 $child->hide;
566 675
567 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 676 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
568 677
569 $self->check_size; 678 $self->realloc;
570 $self->update;
571} 679}
572 680
573sub clear { 681sub clear {
574 my ($self) = @_; 682 my ($self) = @_;
575 683
579 for (@$children) { 687 for (@$children) {
580 delete $_->{parent}; 688 delete $_->{parent};
581 $_->hide; 689 $_->hide;
582 } 690 }
583 691
584 $self->check_size; 692 $self->realloc;
585 $self->update;
586} 693}
587 694
588sub find_widget { 695sub find_widget {
589 my ($self, $x, $y) = @_; 696 my ($self, $x, $y) = @_;
590 697
643sub size_request { 750sub size_request {
644 $_[0]{children}[0]->size_request 751 $_[0]{children}[0]->size_request
645} 752}
646 753
647sub size_allocate { 754sub size_allocate {
648 my ($self, $w, $h) = @_; 755 my ($self, $w, $h, $changed) = @_;
649 756
650 $self->{children}[0]->configure (0, 0, $w, $h); 757 $self->{children}[0]->configure (0, 0, $w, $h);
651} 758}
652 759
653############################################################################# 760#############################################################################
670 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 777 $ROOT->on_post_alloc ($self => sub { $self->render_child });
671 $self->SUPER::update; 778 $self->SUPER::update;
672} 779}
673 780
674sub size_allocate { 781sub size_allocate {
675 my ($self, $w, $h) = @_; 782 my ($self, $w, $h, $changed) = @_;
676 783
677 $self->SUPER::size_allocate ($w, $h); 784 $self->SUPER::size_allocate ($w, $h, $changed);
678 $self->update; 785 $self->update
786 if $changed;
679} 787}
680 788
681sub _render { 789sub _render {
682 $_[0]{children}[0]->draw; 790 $_[0]{children}[0]->draw;
683} 791}
688 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 796 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
689 glClearColor 0, 0, 0, 0; 797 glClearColor 0, 0, 0, 0;
690 glClear GL_COLOR_BUFFER_BIT; 798 glClear GL_COLOR_BUFFER_BIT;
691 799
692 $self->_render; 800 $self->_render;
693# glColorMask 1, 1, 1, 0;
694# glEnable GL_BLEND;
695# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
696# glRasterPos 0, 0;
697# glCopyPixels 0, 0, $self->{w}, $self->{h};
698# glDisable GL_BLEND;
699# glColorMask 1, 1, 1, 1;
700 }; 801 };
701} 802}
702 803
703sub _draw { 804sub _draw {
704 my ($self) = @_; 805 my ($self) = @_;
708 my $tex = $self->{texture} 809 my $tex = $self->{texture}
709 or return; 810 or return;
710 811
711 glEnable GL_TEXTURE_2D; 812 glEnable GL_TEXTURE_2D;
712 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
713 glColor 0, 0, 0, 1; 814 glColor 1, 1, 1, 1;
714 815
715 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 816 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
716 817
717 glDisable GL_TEXTURE_2D; 818 glDisable GL_TEXTURE_2D;
718} 819}
721 822
722package CFClient::UI::ViewPort; 823package CFClient::UI::ViewPort;
723 824
724our @ISA = CFClient::UI::Window::; 825our @ISA = CFClient::UI::Window::;
725 826
827sub new {
828 my $class = shift;
829
830 $class->SUPER::new (
831 scroll_x => 0,
832 scroll_y => 1,
833 @_,
834 )
835}
836
726sub size_request { 837sub size_request {
727 my ($self) = @_; 838 my ($self) = @_;
728 839
729 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 840 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
730 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
731 841
732 @$self{qw(child_w child_h)} 842 $w = 10 if $self->{scroll_x};
843 $h = 10 if $self->{scroll_y};
844
845 ($w, $h)
733} 846}
734 847
735sub size_allocate { 848sub size_allocate {
736 my ($self, $w, $h) = @_; 849 my ($self, $w, $h, $changed) = @_;
737 850
851 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
852 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
853
854 $self->child->configure (0, 0, $w, $h);
738 $self->update; 855 $self->update;
739} 856}
740 857
741sub set_offset { 858sub set_offset {
742 my ($self, $x, $y) = @_; 859 my ($self, $x, $y) = @_;
793 my $class = shift; 910 my $class = shift;
794 911
795 my $self; 912 my $self;
796 913
797 my $slider = new CFClient::UI::Slider 914 my $slider = new CFClient::UI::Slider
798 vertical => 1, 915 vertical => 1,
799 range => [0, 0, 1, 0.01], # HACK fix 916 range => [0, 0, 1, 0.01], # HACK fix
800 connect_changed => sub { 917 on_changed => sub {
801 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 918 $self->{vp}->set_offset (0, $_[1]);
802 }, 919 },
803 ; 920 ;
804 921
805 $self = $class->SUPER::new ( 922 $self = $class->SUPER::new (
806 vp => (new CFClient::UI::ViewPort), 923 vp => (new CFClient::UI::ViewPort expand => 1),
807 slider => $slider, 924 slider => $slider,
808 @_, 925 @_,
809 ); 926 );
810 927
811 $self->{vp}->add ($self->{scrolled}); 928 $self->{vp}->add ($self->{scrolled});
812 $self->add ($self->{vp}); 929 $self->add ($self->{vp});
813 $self->add ($self->{slider}); 930 $self->add ($self->{slider});
814 931
815 $self 932 $self
933}
934
935sub update {
936 my ($self) = @_;
937
938 $self->SUPER::update;
939
940 # todo: overwrite size_allocate of child
941 my $child = $self->{vp}->child;
942 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
943}
944
945sub size_allocate {
946 my ($self, $w, $h, $changed) = @_;
947
948 $self->SUPER::size_allocate ($w, $h, $changed);
949
950 my $child = $self->{vp}->child;
951 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
816} 952}
817 953
818#TODO# update range on size_allocate depending on child 954#TODO# update range on size_allocate depending on child
819# update viewport offset on scroll 955# update viewport offset on scroll
820 956
871 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1007 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
872 1008
873sub new { 1009sub new {
874 my $class = shift; 1010 my $class = shift;
875 1011
876 # TODO: user_x, user_y, overwrite moveto?
877
878 my $self = $class->SUPER::new ( 1012 my $self = $class->SUPER::new (
879 bg => [1, 1, 1, 1], 1013 bg => [1, 1, 1, 1],
880 border_bg => [1, 1, 1, 1], 1014 border_bg => [1, 1, 1, 1],
881 border => 0.6, 1015 border => 0.6,
882 toplevel => 1, 1016 is_toplevel => 1,
883 can_events => 1, 1017 can_events => 1,
884 @_ 1018 @_
885 ); 1019 );
886 1020
887 $self->{title} &&= new CFClient::UI::Label 1021 $self->{title} &&= new CFClient::UI::Label
888 align => 0, 1022 align => 0,
907 $h + $self->border * 2, 1041 $h + $self->border * 2,
908 ) 1042 )
909} 1043}
910 1044
911sub size_allocate { 1045sub size_allocate {
912 my ($self, $w, $h) = @_; 1046 my ($self, $w, $h, $changed) = @_;
1047
1048 return unless $changed;
913 1049
914 $h -= List::Util::max 0, $self->border * 2; 1050 $h -= List::Util::max 0, $self->border * 2;
915 $w -= List::Util::max 0, $self->border * 2; 1051 $w -= List::Util::max 0, $self->border * 2;
916 1052
917 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1053 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
941 my ($ev, $x, $y) = @_; 1077 my ($ev, $x, $y) = @_;
942 1078
943 my $dx = $ev->{x} - $ox; 1079 my $dx = $ev->{x} - $ox;
944 my $dy = $ev->{y} - $oy; 1080 my $dy = $ev->{y} - $oy;
945 1081
1082 $self->{user_x} = $wx + $dx * $mx;
1083 $self->{user_y} = $wy + $dy * $my;
946 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1084 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
947 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1085 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
948 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1086 $self->move ($self->{user_x}, $self->{user_y});
949 $self->check_size; 1087 $self->realloc;
950 }; 1088 };
951 1089
952 } elsif ($lr ^ $td) { 1090 } elsif ($lr ^ $td) {
953 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1091 my ($ox, $oy) = ($ev->{x}, $ev->{y});
954 my ($bx, $by) = ($self->{x}, $self->{y}); 1092 my ($bx, $by) = ($self->{x}, $self->{y});
956 $self->{motion} = sub { 1094 $self->{motion} = sub {
957 my ($ev, $x, $y) = @_; 1095 my ($ev, $x, $y) = @_;
958 1096
959 ($x, $y) = ($ev->{x}, $ev->{y}); 1097 ($x, $y) = ($ev->{x}, $ev->{y});
960 1098
961 $self->move ($bx + $x - $ox, $by + $y - $oy); 1099 $self->{user_x} = $bx + $x - $ox;
1100 $self->{user_y} = $by + $y - $oy;
1101
1102 $self->move ($self->{user_x}, $self->{user_y});
962 $self->update; 1103 $self->realloc;
963 }; 1104 };
964 } 1105 }
965} 1106}
966 1107
967sub button_up { 1108sub button_up {
1028sub new { 1169sub new {
1029 my $class = shift; 1170 my $class = shift;
1030 1171
1031 $class->SUPER::new ( 1172 $class->SUPER::new (
1032 col_expand => [], 1173 col_expand => [],
1033 @_ 1174 @_,
1034 ) 1175 )
1176}
1177
1178sub children {
1179 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1035} 1180}
1036 1181
1037sub add { 1182sub add {
1038 my ($self, $x, $y, $child) = @_; 1183 my ($self, $x, $y, $child) = @_;
1039 1184
1040 $child->set_parent ($self); 1185 $child->set_parent ($self);
1041 $self->{children}[$y][$x] = $child; 1186 $self->{children}[$y][$x] = $child;
1042 1187
1043 $child->check_size; 1188 $self->realloc;
1044} 1189}
1045 1190
1046sub children {
1047 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1048}
1049
1050# TODO: move to container class maybe? send childs a signal on removal? 1191# TODO: move to container class maybe? send children a signal on removal?
1051sub clear { 1192sub clear {
1052 my ($self) = @_; 1193 my ($self) = @_;
1053 1194
1054 my @children = $self->children; 1195 my @children = $self->children;
1055 delete $self->{children}; 1196 delete $self->{children};
1057 for (@children) { 1198 for (@children) {
1058 delete $_->{parent}; 1199 delete $_->{parent};
1059 $_->hide; 1200 $_->hide;
1060 } 1201 }
1061 1202
1062 $self->update; 1203 $self->realloc;
1063} 1204}
1064 1205
1065sub get_wh { 1206sub get_wh {
1066 my ($self) = @_; 1207 my ($self) = @_;
1067 1208
1094 (sum @$hs), 1235 (sum @$hs),
1095 ) 1236 )
1096} 1237}
1097 1238
1098sub size_allocate { 1239sub size_allocate {
1099 my ($self, $w, $h) = @_; 1240 my ($self, $w, $h, $changed) = @_;
1100 1241
1101 my ($ws, $hs) = $self->get_wh; 1242 my ($ws, $hs) = $self->get_wh;
1102 1243
1103 my $req_w = sum @$ws; 1244 my $req_w = (sum @$ws) || 1;
1104 my $req_h = sum @$hs; 1245 my $req_h = (sum @$hs) || 1;
1105 1246
1106 # TODO: nicer code && do row_expand 1247 # TODO: nicer code && do row_expand
1107 my @col_expand = @{$self->{col_expand}}; 1248 my @col_expand = @{$self->{col_expand}};
1108 @col_expand = (1) x @$ws unless @col_expand; 1249 @col_expand = (1) x @$ws unless @col_expand;
1109 my $col_expand = (sum @col_expand) || 1; 1250 my $col_expand = (sum @col_expand) || 1;
1163 } 1304 }
1164} 1305}
1165 1306
1166############################################################################# 1307#############################################################################
1167 1308
1309package CFClient::UI::Box;
1310
1311our @ISA = CFClient::UI::Container::;
1312
1313sub size_request {
1314 my ($self) = @_;
1315
1316 $self->{vertical}
1317 ? (
1318 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1319 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1320 )
1321 : (
1322 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1323 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1324 )
1325}
1326
1327sub size_allocate {
1328 my ($self, $w, $h, $changed) = @_;
1329
1330 my $space = $self->{vertical} ? $h : $w;
1331 my $children = $self->{children};
1332
1333 my @req;
1334
1335 if ($self->{homogeneous}) {
1336 @req = ($space / (@$children || 1)) x @$children;
1337 } else {
1338 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1339 my $req = List::Util::sum @req;
1340
1341 if ($req > $space) {
1342 # ah well, not enough space
1343 $_ *= $space / $req for @req;
1344 } else {
1345 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1346
1347 $space = ($space - $req) / $expand; # remaining space to give away
1348
1349 $req[$_] += $space * $children->[$_]{expand}
1350 for 0 .. $#$children;
1351 }
1352 }
1353
1354 CFClient::UI::harmonize \@req;
1355
1356 my $pos = 0;
1357 for (0 .. $#$children) {
1358 my $alloc = $req[$_];
1359 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1360
1361 $pos += $alloc;
1362 }
1363
1364 1
1365}
1366
1367#############################################################################
1368
1168package CFClient::UI::HBox; 1369package CFClient::UI::HBox;
1169 1370
1170# TODO: wrap into common Box base class
1171
1172our @ISA = CFClient::UI::Container::; 1371our @ISA = CFClient::UI::Box::;
1173 1372
1174sub size_request { 1373sub new {
1175 my ($self) = @_; 1374 my $class = shift;
1176 1375
1177 my @alloc = map [$_->size_request], @{$self->{children}}; 1376 $class->SUPER::new (
1178 1377 vertical => 0,
1179 ( 1378 @_,
1180 (List::Util::sum map $_->[0], @alloc),
1181 (List::Util::max map $_->[1], @alloc),
1182 ) 1379 )
1183} 1380}
1184 1381
1185sub size_allocate {
1186 my ($self, $w, $h) = @_;
1187
1188 ($h, $w) = ($w, $h);
1189
1190 my $children = $self->{children};
1191
1192 my @h = map $_->{req_w}, @$children;
1193
1194 my $req_h = List::Util::sum @h;
1195
1196 if ($req_h > $h) {
1197 # ah well, not enough space
1198 $_ *= $h / $req_h for @h;
1199 } else {
1200 my $exp = List::Util::sum map $_->{expand}, @$children;
1201 $exp ||= 1;
1202
1203 for (0 .. $#$children) {
1204 my $child = $children->[$_];
1205
1206 my $alloc_h = $h[$_];
1207 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1208 $h[$_] = $alloc_h;
1209 }
1210 }
1211
1212 CFClient::UI::harmonize \@h;
1213
1214 my $y = 0;
1215 for (0 .. $#$children) {
1216 my $child = $children->[$_];
1217 my $h = $h[$_];
1218 $child->configure ($y, 0, $h, $w);
1219
1220 $y += $h;
1221 }
1222
1223 1
1224}
1225
1226############################################################################# 1382#############################################################################
1227 1383
1228package CFClient::UI::VBox; 1384package CFClient::UI::VBox;
1229 1385
1230# TODO: wrap into common Box base class
1231
1232our @ISA = CFClient::UI::Container::; 1386our @ISA = CFClient::UI::Box::;
1233 1387
1234sub size_request { 1388sub new {
1235 my ($self) = @_; 1389 my $class = shift;
1236 1390
1237 my @alloc = map [$_->size_request], @{$self->{children}}; 1391 $class->SUPER::new (
1238 1392 vertical => 1,
1239 ( 1393 @_,
1240 (List::Util::max map $_->[0], @alloc),
1241 (List::Util::sum map $_->[1], @alloc),
1242 ) 1394 )
1243}
1244
1245sub size_allocate {
1246 my ($self, $w, $h) = @_;
1247
1248 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1249
1250 my $children = $self->{children};
1251
1252 my @h = map $_->{req_h}, @$children;
1253
1254 my $req_h = List::Util::sum @h;
1255
1256 if ($req_h > $h) {
1257 # ah well, not enough space
1258 $_ *= $h / $req_h for @h;
1259 } else {
1260 my $exp = List::Util::sum map $_->{expand}, @$children;
1261 $exp ||= 1;
1262
1263 for (0 .. $#$children) {
1264 my $child = $children->[$_];
1265
1266 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1267 }
1268 }
1269
1270 CFClient::UI::harmonize \@h;
1271
1272 my $y = 0;
1273 for (0 .. $#$children) {
1274 my $child = $children->[$_];
1275 my $h = $h[$_];
1276 $child->configure (0, $y, $w, $h);
1277
1278 $y += $h;
1279 }
1280
1281 1
1282} 1395}
1283 1396
1284############################################################################# 1397#############################################################################
1285 1398
1286package CFClient::UI::Label; 1399package CFClient::UI::Label;
1297 #bg => none 1410 #bg => none
1298 #active_bg => none 1411 #active_bg => none
1299 #font => default_font 1412 #font => default_font
1300 #text => initial text 1413 #text => initial text
1301 #markup => initial narkup 1414 #markup => initial narkup
1415 #max_w => maximum pixel width
1416 ellipsise => 3, # end
1302 layout => (new CFClient::Layout), 1417 layout => (new CFClient::Layout),
1303 fontsize => 1, 1418 fontsize => 1,
1304 align => -1, 1419 align => -1,
1305 valign => -1, 1420 valign => -1,
1306 padding => 2, 1421 padding => 2,
1347 $self->{text} = "T$text"; 1462 $self->{text} = "T$text";
1348 1463
1349 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1464 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1350 $self->{layout}->set_text ($text); 1465 $self->{layout}->set_text ($text);
1351 1466
1467 $self->realloc;
1352 $self->update; 1468 $self->update;
1353 $self->check_size;
1354} 1469}
1355 1470
1356sub set_markup { 1471sub set_markup {
1357 my ($self, $markup) = @_; 1472 my ($self, $markup) = @_;
1358 1473
1362 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1477 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1363 1478
1364 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1479 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1365 $self->{layout}->set_markup ($markup); 1480 $self->{layout}->set_markup ($markup);
1366 1481
1482 $self->realloc;
1367 $self->update; 1483 $self->update;
1368 $self->check_size;
1369} 1484}
1370 1485
1371sub size_request { 1486sub size_request {
1372 my ($self) = @_; 1487 my ($self) = @_;
1373 1488
1374 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1489 $self->{layout}->set_font ($self->{font}) if $self->{font};
1375 $self->{layout}->set_width ($self->{max_w} || -1); 1490 $self->{layout}->set_width ($self->{max_w} || -1);
1491 $self->{layout}->set_ellipsise ($self->{ellipsise});
1492 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1376 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1493 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1377 1494
1378 my ($w, $h) = $self->{layout}->size; 1495 my ($w, $h) = $self->{layout}->size;
1379 1496
1380 if (exists $self->{template}) { 1497 if (exists $self->{template}) {
1392 $h + $self->{padding} * 2, 1509 $h + $self->{padding} * 2,
1393 ) 1510 )
1394} 1511}
1395 1512
1396sub size_allocate { 1513sub size_allocate {
1397 my ($self, $w, $h) = @_; 1514 my ($self, $w, $h, $changed) = @_;
1398 1515
1399 delete $self->{texture}; 1516 delete $self->{texture}
1517 if $changed;
1400} 1518}
1401 1519
1402sub set_fontsize { 1520sub set_fontsize {
1403 my ($self, $fontsize) = @_; 1521 my ($self, $fontsize) = @_;
1404 1522
1405 $self->{fontsize} = $fontsize; 1523 $self->{fontsize} = $fontsize;
1406 delete $self->{texture}; 1524 delete $self->{texture};
1407 1525
1408 $self->update; 1526 $self->realloc;
1409 $self->check_size;
1410} 1527}
1411 1528
1412sub _draw { 1529sub _draw {
1413 my ($self) = @_; 1530 my ($self) = @_;
1414 1531
1416 1533
1417 my $tex = $self->{texture} ||= do { 1534 my $tex = $self->{texture} ||= do {
1418 $self->{layout}->set_foreground (@{$self->{fg}}); 1535 $self->{layout}->set_foreground (@{$self->{fg}});
1419 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1536 $self->{layout}->set_font ($self->{font}) if $self->{font};
1420 $self->{layout}->set_width ($self->{w}); 1537 $self->{layout}->set_width ($self->{w});
1538 $self->{layout}->set_ellipsise ($self->{ellipsise});
1539 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1421 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1540 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1422 1541
1423 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1542 my $tex = new_from_layout CFClient::Texture $self->{layout};
1424 1543
1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding} 1544 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1545 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1464 active_fg => [0, 0, 0], 1583 active_fg => [0, 0, 0],
1465 can_hover => 1, 1584 can_hover => 1,
1466 can_focus => 1, 1585 can_focus => 1,
1467 valign => 0, 1586 valign => 0,
1468 can_events => 1, 1587 can_events => 1,
1588 #text => ...
1469 @_ 1589 @_
1470 ) 1590 )
1471} 1591}
1472 1592
1473sub _set_text { 1593sub _set_text {
1483 $self->{text} = $text; 1603 $self->{text} = $text;
1484 1604
1485 $text =~ s/./*/g if $self->{hidden}; 1605 $text =~ s/./*/g if $self->{hidden};
1486 $self->{layout}->set_text ("$text "); 1606 $self->{layout}->set_text ("$text ");
1487 1607
1488 $self->emit (changed => $self->{text}); 1608 $self->_emit (changed => $self->{text});
1489} 1609}
1490 1610
1491sub set_text { 1611sub set_text {
1492 my ($self, $text) = @_; 1612 my ($self, $text) = @_;
1493 1613
1494 $self->{cursor} = length $text; 1614 $self->{cursor} = length $text;
1495 $self->_set_text ($text); 1615 $self->_set_text ($text);
1496 $self->update; 1616
1497 $self->check_size; 1617 $self->realloc;
1498} 1618}
1499 1619
1500sub get_text { 1620sub get_text {
1501 $_[0]{text} 1621 $_[0]{text}
1502} 1622}
1505 my ($self) = @_; 1625 my ($self) = @_;
1506 1626
1507 my ($w, $h) = $self->SUPER::size_request; 1627 my ($w, $h) = $self->SUPER::size_request;
1508 1628
1509 ($w + 1, $h) # add 1 for cursor 1629 ($w + 1, $h) # add 1 for cursor
1510}
1511
1512sub size_allocate {
1513 my ($self, $w, $h) = @_;
1514
1515 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1516} 1630}
1517 1631
1518sub key_down { 1632sub key_down {
1519 my ($self, $ev) = @_; 1633 my ($self, $ev) = @_;
1520 1634
1535 } elsif ($sym == CFClient::SDLK_HOME) { 1649 } elsif ($sym == CFClient::SDLK_HOME) {
1536 $self->{cursor} = 0; 1650 $self->{cursor} = 0;
1537 } elsif ($sym == CFClient::SDLK_END) { 1651 } elsif ($sym == CFClient::SDLK_END) {
1538 $self->{cursor} = length $text; 1652 $self->{cursor} = length $text;
1539 } elsif ($uni == 27) { 1653 } elsif ($uni == 27) {
1540 $self->emit ('escape'); 1654 $self->_emit ('escape');
1541 } elsif ($uni) { 1655 } elsif ($uni) {
1542 substr $text, $self->{cursor}++, 0, chr $uni; 1656 substr $text, $self->{cursor}++, 0, chr $uni;
1543 } 1657 }
1544 1658
1545 $self->_set_text ($text); 1659 $self->_set_text ($text);
1546 $self->update; 1660
1547 $self->check_size; 1661 $self->realloc;
1548} 1662}
1549 1663
1550sub focus_in { 1664sub focus_in {
1551 my ($self) = @_; 1665 my ($self) = @_;
1552 1666
1632 if ($sym == 13) { 1746 if ($sym == 13) {
1633 unshift @{$self->{history}}, 1747 unshift @{$self->{history}},
1634 my $txt = $self->get_text; 1748 my $txt = $self->get_text;
1635 $self->{history_pointer} = -1; 1749 $self->{history_pointer} = -1;
1636 $self->{history_saveback} = ''; 1750 $self->{history_saveback} = '';
1637 $self->emit (activate => $txt); 1751 $self->_emit (activate => $txt);
1638 $self->update; 1752 $self->update;
1639 1753
1640 } elsif ($sym == CFClient::SDLK_UP) { 1754 } elsif ($sym == CFClient::SDLK_UP) {
1641 if ($self->{history_pointer} < 0) { 1755 if ($self->{history_pointer} < 0) {
1642 $self->{history_saveback} = $self->get_text; 1756 $self->{history_saveback} = $self->get_text;
1690 can_events => 1, 1804 can_events => 1,
1691 @_ 1805 @_
1692 ) 1806 )
1693} 1807}
1694 1808
1809sub activate { }
1810
1695sub button_up { 1811sub button_up {
1696 my ($self, $ev, $x, $y) = @_; 1812 my ($self, $ev, $x, $y) = @_;
1697 1813
1814 $self->emit ("activate")
1698 if ($x >= 0 && $x < $self->{w} 1815 if $x >= 0 && $x < $self->{w}
1699 && $y >= 0 && $y < $self->{h}) { 1816 && $y >= 0 && $y < $self->{h};
1700 $self->emit ("activate");
1701 }
1702} 1817}
1703 1818
1704sub _draw { 1819sub _draw {
1705 my ($self) = @_; 1820 my ($self) = @_;
1706 1821
1758 my ($self, $ev, $x, $y) = @_; 1873 my ($self, $ev, $x, $y) = @_;
1759 1874
1760 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1875 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1761 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1876 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1762 $self->{state} = !$self->{state}; 1877 $self->{state} = !$self->{state};
1763 $self->emit (changed => $self->{state}); 1878 $self->_emit (changed => $self->{state});
1764 } 1879 }
1765} 1880}
1766 1881
1767sub _draw { 1882sub _draw {
1768 my ($self) = @_; 1883 my ($self) = @_;
2043 my $self = $class->SUPER::new ( 2158 my $self = $class->SUPER::new (
2044 fg => [1, 1, 1], 2159 fg => [1, 1, 1],
2045 active_fg => [0, 0, 0], 2160 active_fg => [0, 0, 0],
2046 bg => [0, 0, 0, 0.2], 2161 bg => [0, 0, 0, 0.2],
2047 active_bg => [1, 1, 1, 0.5], 2162 active_bg => [1, 1, 1, 0.5],
2048 range => [0, 0, 100, 10], 2163 range => [0, 0, 100, 10, 0],
2049 req_w => $::WIDTH / 80, 2164 req_w => $::WIDTH / 80,
2050 req_h => $::WIDTH / 80, 2165 req_h => $::WIDTH / 80,
2051 vertical => 0, 2166 vertical => 0,
2052 can_hover => 1, 2167 can_hover => 1,
2053 inner_pad => .05, 2168 inner_pad => 0.02,
2054 @_ 2169 @_
2055 ); 2170 );
2056 2171
2057 $self->set_value ($self->{range}[0]); 2172 $self->set_value ($self->{range}[0]);
2058 $self->update; 2173 $self->update;
2059 2174
2060 $self 2175 $self
2061} 2176}
2062 2177
2178sub changed { }
2179
2180sub set_range {
2181 my ($self, $range) = @_;
2182
2183 ($range, $self->{range}) = ($self->{range}, $range);
2184
2185 $self->update
2186 if "@$range" ne "@{$self->{range}}";
2187}
2188
2063sub set_value { 2189sub set_value {
2064 my ($self, $value) = @_; 2190 my ($self, $value) = @_;
2065 2191
2066 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 2192 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2067 2193
2068 $hi = $lo + 1 if $hi <= $lo; 2194 $hi = $lo + 1 if $hi <= $lo;
2069 2195
2196 $page = $hi - $lo if $page > $hi - $lo;
2197
2070 $value = $lo if $value < $lo; 2198 $value = $lo if $value < $lo;
2071 $value = $hi if $value > $hi; 2199 $value = $hi - $page if $value > $hi - $page;
2072 2200
2073 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 2201 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2074 if $unit; 2202 if $unit;
2075 2203
2076 $page = $hi - $lo if $page > $hi - $lo;
2077
2078 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2204 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2079 2205
2080 if ($value != $old_value) { 2206 if ($value != $old_value) {
2081 $self->emit (changed => $value); 2207 $self->_emit (changed => $value);
2082 $self->update; 2208 $self->update;
2083 } 2209 }
2084} 2210}
2085 2211
2086sub size_request { 2212sub size_request {
2094 2220
2095sub button_down { 2221sub button_down {
2096 my ($self, $ev, $x, $y) = @_; 2222 my ($self, $ev, $x, $y) = @_;
2097 2223
2098 $self->SUPER::button_down ($ev, $x, $y); 2224 $self->SUPER::button_down ($ev, $x, $y);
2225
2226 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2227
2099 $self->mouse_motion ($ev, $x, $y); 2228 $self->mouse_motion ($ev, $x, $y);
2100} 2229}
2101 2230
2102sub mouse_motion { 2231sub mouse_motion {
2103 my ($self, $ev, $x, $y) = @_; 2232 my ($self, $ev, $x, $y) = @_;
2105 if ($GRAB == $self) { 2234 if ($GRAB == $self) {
2106 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2235 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2107 2236
2108 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2237 my (undef, $lo, $hi, $page) = @{$self->{range}};
2109 2238
2110 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; 2239 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2111 2240
2112 $self->set_value ($x * ($hi - $lo) + $lo); 2241 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2113 } 2242 }
2114} 2243}
2115 2244
2116sub update { 2245sub update {
2117 my ($self) = @_; 2246 my ($self) = @_;
2118 2247
2119 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2248 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2120 $self->set_value ($self->{range}[0]); 2249 $self->set_value ($self->{range}[0]);
2121 2250
2122 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2251 my ($value, $lo, $hi, $page) = @{$self->{range}};
2252 my $range = ($hi - $page - $lo) || 1e-100;
2123 2253
2124 my $inner_w = 1 - 2 * $self->{inner_pad}; 2254 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2125 2255
2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1; 2256 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2257 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2127 2258
2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1); 2259 $value = ($value - $lo) / $range;
2129 $value = $self->{scale} * ($value - $lo); 2260 $value = $value * $self->{scale} + $self->{offset};
2130 2261
2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
2132
2133 $value = 0 if $value < 0;
2134 $page = 1 - $value if $value + $page > 1;
2135
2136 $self->{knob_x} = $value; 2262 $self->{knob_x} = $value - $knob_w * 0.5;
2137 $self->{knob_w} = $page; 2263 $self->{knob_w} = $knob_w;
2138 }); 2264 });
2139 2265
2140 $self->SUPER::update; 2266 $self->SUPER::update;
2141} 2267}
2142 2268
2148 glScale $self->{w}, $self->{h}; 2274 glScale $self->{w}, $self->{h};
2149 2275
2150 if ($self->{vertical}) { 2276 if ($self->{vertical}) {
2151 # draw a vertical slider like a rotated horizontal slider 2277 # draw a vertical slider like a rotated horizontal slider
2152 2278
2279 glTranslate 1, 0, 0;
2153 glRotate 90, 0, 0, 1; 2280 glRotate 90, 0, 0, 1;
2154 glTranslate 0, 1, 0;
2155 } 2281 }
2156 2282
2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2283 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2284 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2159 2285
2166 # draw handle 2292 # draw handle
2167 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); 2293 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2168 2294
2169 glDisable GL_TEXTURE_2D; 2295 glDisable GL_TEXTURE_2D;
2170} 2296}
2297
2298#############################################################################
2299
2300package CFClient::UI::ValSlider;
2301
2302our @ISA = CFClient::UI::HBox::;
2303
2304sub new {
2305 my ($class, %arg) = @_;
2306
2307 my $range = delete $arg{range};
2308
2309 my $self = $class->SUPER::new (
2310 slider => (new CFClient::UI::Slider expand => 1, range => $range),
2311 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}),
2312 to_value => sub { shift },
2313 from_value => sub { shift },
2314 %arg,
2315 );
2316
2317 $self->{slider}->connect (changed => sub {
2318 my ($self, $value) = @_;
2319 $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2320 $self->{parent}->emit (changed => $value);
2321 });
2322
2323# $self->{entry}->connect (changed => sub {
2324# my ($self, $value) = @_;
2325# $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2326# $self->{parent}->emit (changed => $value);
2327# });
2328
2329 $self->add ($self->{slider}, $self->{entry});
2330
2331 $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2332
2333 $self
2334}
2335
2336sub set_range { shift->{slider}->set_range (@_) }
2337sub set_value { shift->{slider}->set_value (@_) }
2171 2338
2172############################################################################# 2339#############################################################################
2173 2340
2174package CFClient::UI::TextView; 2341package CFClient::UI::TextView;
2175 2342
2205 2372
2206 $self->{fontsize} = $fontsize; 2373 $self->{fontsize} = $fontsize;
2207 $self->reflow; 2374 $self->reflow;
2208} 2375}
2209 2376
2210sub text_height {
2211 my ($self, $text) = @_;
2212
2213 my $layout = $self->{layout};
2214
2215 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2216 $layout->set_width ($self->{children}[0]{w});
2217 $layout->set_markup ($text);
2218
2219 ($layout->size)[1]
2220}
2221
2222sub reflow {
2223 my ($self) = @_;
2224
2225 $self->{need_reflow}++;
2226 $self->update;
2227}
2228
2229sub size_allocate { 2377sub size_allocate {
2230 my ($self, $w, $h) = @_; 2378 my ($self, $w, $h, $changed) = @_;
2231 2379
2232 $self->SUPER::size_allocate ($w, $h); 2380 $self->SUPER::size_allocate ($w, $h, $changed);
2381
2382 return unless $changed;
2233 2383
2234 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2384 $self->{layout}->set_font ($self->{font}) if $self->{font};
2235 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2385 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2236 $self->{layout}->set_width ($self->{children}[0]{w}); 2386 $self->{layout}->set_width ($self->{children}[0]{w});
2237 2387
2238 $self->reflow; 2388 $self->reflow;
2239} 2389}
2240 2390
2391sub text_size {
2392 my ($self, $text, $indent) = @_;
2393
2394 my $layout = $self->{layout};
2395
2396 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2397 $layout->set_width ($self->{children}[0]{w} - $indent);
2398 $layout->set_markup ($text);
2399
2400 $layout->size
2401}
2402
2403sub reflow {
2404 my ($self) = @_;
2405
2406 $self->{need_reflow}++;
2407 $self->update;
2408}
2409
2410sub set_offset {
2411 my ($self, $offset) = @_;
2412
2413 # todo: base offset on lines or so, not on pixels
2414 $self->{children}[1]->set_value ($offset);
2415}
2416
2417sub clear {
2418 my ($self) = @_;
2419
2420 $self->{par} = [];
2421 $self->{height} = 0;
2422 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2423}
2424
2241sub add_paragraph { 2425sub add_paragraph {
2242 my ($self, $color, $text) = @_; 2426 my ($self, $color, $text, $indent) = @_;
2243 2427
2244 #TODO: intelligently "reformat" paragraph 2428 for my $line (split /\n/, $text) {
2245 2429 my ($w, $h) = $self->text_size ($line);
2246 my $height = $self->text_height ($text);
2247
2248 $self->{height} += $height; 2430 $self->{height} += $h;
2431 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2432 }
2249 2433
2250 push @{$self->{par}}, [$height, $color, $text];
2251
2252 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2434 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2253 $self->{children}[1]->update;
2254} 2435}
2255 2436
2256sub update { 2437sub update {
2257 my ($self) = @_; 2438 my ($self) = @_;
2258 2439
2261 return unless $self->{h} > 0; 2442 return unless $self->{h} > 0;
2262 2443
2263 delete $self->{texture}; 2444 delete $self->{texture};
2264 2445
2265 $ROOT->on_post_alloc ($self, sub { 2446 $ROOT->on_post_alloc ($self, sub {
2447 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2448
2266 if (delete $self->{need_reflow}) { 2449 if (delete $self->{need_reflow}) {
2267 my $height = 0; 2450 my $height = 0;
2268 2451
2269 $height += $_->[0] = $self->text_height ($_->[2]) 2452 my $layout = $self->{layout};
2453
2454 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2455
2270 for @{$self->{par}}; 2456 for (@{$self->{par}}) {
2457 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2458 $layout->set_width ($W - $_->[3]);
2459 $layout->set_markup ($_->[4]);
2460 my ($w, $h) = $layout->size;
2461 $_->[0] = $w + $_->[3];
2462 $_->[1] = $h;
2463 }
2464
2465 $height += $_->[1];
2466 }
2271 2467
2272 $self->{height} = $height; 2468 $self->{height} = $height;
2273 2469
2274 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2470 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2275 2471
2276 delete $self->{texture}; 2472 delete $self->{texture};
2277 } 2473 }
2278 2474
2279 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2475 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2280 glClearColor 0, 0, 0, 0; 2476 glClearColor 0.5, 0.5, 0.5, 0;
2281 glClear GL_COLOR_BUFFER_BIT; 2477 glClear GL_COLOR_BUFFER_BIT;
2282 2478
2283 glEnable GL_TEXTURE_2D;
2284 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2285
2286 my $top = int $self->{children}[1]{range}[0]; 2479 my $top = int $self->{children}[1]{range}[0];
2287 2480
2288 my $y0 = $top; 2481 my $y0 = $top;
2289 my $y1 = $top + $self->{h}; 2482 my $y1 = $top + $H;
2290 2483
2291 my $y = 0; 2484 my $y = 0;
2292 2485
2293 my $layout = $self->{layout}; 2486 my $layout = $self->{layout};
2294 2487
2295 $layout->set_font ($self->{font}) if $self->{font}; 2488 $layout->set_font ($self->{font}) if $self->{font};
2296 2489
2490 glEnable GL_BLEND;
2491 #TODO# not correct in windows where rgba is forced off
2492 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2493
2297 for my $par (@{$self->{par}}) { 2494 for my $par (@{$self->{par}}) {
2298 my $h = $par->[0]; 2495 my $h = $par->[1];
2299 2496
2300 if ($y0 < $y + $h && $y < $y1) { 2497 if ($y0 < $y + $h && $y < $y1) {
2301 $layout->set_foreground (@{ $par->[1] }); 2498 $layout->set_foreground (@{ $par->[2] });
2499 $layout->set_width ($W - $par->[3]);
2302 $layout->set_markup ($par->[2]); 2500 $layout->set_markup ($par->[4]);
2303 2501
2304 my ($W, $H) = $layout->size; 2502 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2305 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2503
2504 glRasterPos $par->[3], $y - $y0;
2505 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2306 } 2506 }
2307 2507
2308 $y += $h; 2508 $y += $h;
2309 } 2509 }
2310 2510
2311 glDisable GL_TEXTURE_2D; 2511 glDisable GL_BLEND;
2312 }; 2512 };
2313 }); 2513 });
2314} 2514}
2315 2515
2316sub _draw { 2516sub _draw {
2317 my ($self) = @_; 2517 my ($self) = @_;
2318 2518
2319 glEnable GL_TEXTURE_2D; 2519 glEnable GL_TEXTURE_2D;
2320 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2520 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2321 glColor 1, 1, 1, 1; 2521 glColor 1, 1, 1, 1;
2322 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2522 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2323 glDisable GL_TEXTURE_2D; 2523 glDisable GL_TEXTURE_2D;
2324 2524
2325 $self->{children}[1]->draw; 2525 $self->{children}[1]->draw;
2326 2526
2327} 2527}
2376 2576
2377sub new { 2577sub new {
2378 my $class = shift; 2578 my $class = shift;
2379 2579
2380 my $self = $class->SUPER::new ( 2580 my $self = $class->SUPER::new (
2381 state => 0, 2581 state => 0,
2382 connect_activate => \&toggle_flopper, 2582 on_activate => \&toggle_flopper,
2383 @_ 2583 @_
2384 ); 2584 );
2385 2585
2386 if ($self->{state}) {
2387 $self->{state} = 0;
2388 $self->toggle_flopper;
2389 }
2390
2391 $self 2586 $self
2392} 2587}
2393 2588
2394sub toggle_flopper { 2589sub toggle_flopper {
2395 my ($self) = @_; 2590 my ($self) = @_;
2396 2591
2397 # TODO: use animation 2592 $self->{other}->toggle_visibility;
2398 if ($self->{state} = !$self->{state}) {
2399 $CFClient::UI::ROOT->add ($self->{other});
2400 $self->{other}->move ($self->coord2global (0, $self->{h}));
2401 $self->emit ("open");
2402 } else {
2403 $CFClient::UI::ROOT->remove ($self->{other});
2404 $self->emit ("close");
2405 }
2406
2407 $self->emit (changed => $self->{state});
2408} 2593}
2409 2594
2410############################################################################# 2595#############################################################################
2411 2596
2412package CFClient::UI::Tooltip; 2597package CFClient::UI::Tooltip;
2426 2611
2427sub set_tooltip_from { 2612sub set_tooltip_from {
2428 my ($self, $widget) = @_; 2613 my ($self, $widget) = @_;
2429 2614
2430 $self->add (new CFClient::UI::Label 2615 $self->add (new CFClient::UI::Label
2431 markup => $widget->{tooltip}, 2616 markup => $widget->{tooltip},
2432 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2617 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2433 fontsize => 0.8, 2618 fontsize => 0.8,
2434 fg => [0, 0, 0, 1], 2619 fg => [0, 0, 0, 1],
2620 ellipsise => 0,
2435 font => ($widget->{tooltip_font} || $::FONT_PROP), 2621 font => ($widget->{tooltip_font} || $::FONT_PROP),
2436 ); 2622 );
2437} 2623}
2438 2624
2439sub size_request { 2625sub size_request {
2440 my ($self) = @_; 2626 my ($self) = @_;
2443 2629
2444 ($w + 4, $h + 4) 2630 ($w + 4, $h + 4)
2445} 2631}
2446 2632
2447sub size_allocate { 2633sub size_allocate {
2448 my ($self, $w, $h) = @_; 2634 my ($self, $w, $h, $changed) = @_;
2449 2635
2636 return unless $changed;
2637
2450 $self->SUPER::size_allocate ($w - 4, $h - 4); 2638 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2639}
2640
2641sub visibility_change {
2642 my ($self, $visible) = @_;
2643
2644 return unless $visible;
2645
2646 $self->{root}->on_post_alloc ("move_$self" => sub {
2647 my $widget = $self->{owner}
2648 or return;
2649
2650 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2651
2652 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2653 if $x + $self->{w} > $::WIDTH;
2654
2655 $self->move ($x, $y);
2656 });
2451} 2657}
2452 2658
2453sub _draw { 2659sub _draw {
2454 my ($self) = @_; 2660 my ($self) = @_;
2455 2661
2472 glVertex $w, $h; 2678 glVertex $w, $h;
2473 glVertex $w, 0; 2679 glVertex $w, 0;
2474 glEnd; 2680 glEnd;
2475 2681
2476 glTranslate 2 - 0.375, 2 - 0.375; 2682 glTranslate 2 - 0.375, 2 - 0.375;
2683
2477 $self->SUPER::_draw; 2684 $self->SUPER::_draw;
2478} 2685}
2479 2686
2480############################################################################# 2687#############################################################################
2481 2688
2486use CFClient::OpenGL; 2693use CFClient::OpenGL;
2487 2694
2488sub new { 2695sub new {
2489 my $class = shift; 2696 my $class = shift;
2490 2697
2491 $class->SUPER::new ( 2698 my $self = $class->SUPER::new (
2492 aspect => 1, 2699 aspect => 1,
2700 can_events => 0,
2493 @_, 2701 @_,
2494 ) 2702 );
2703
2704 if ($self->{anim} && $self->{animspeed}) {
2705 Scalar::Util::weaken (my $widget = $self);
2706
2707 $self->{timer} = Event->timer (
2708 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2709 hard => 1,
2710 interval => $self->{animspeed},
2711 cb => sub {
2712 ++$widget->{frame};
2713 $widget->update;
2714 },
2715 );
2716 }
2717
2718 $self
2495} 2719}
2496 2720
2497sub size_request { 2721sub size_request {
2498 (32, 8) 2722 (32, 8)
2499} 2723}
2500 2724
2725sub update {
2726 my ($self) = @_;
2727
2728 return unless $self->{visible};
2729
2730 $self->SUPER::update;
2731}
2732
2501sub _draw { 2733sub _draw {
2502 my ($self) = @_; 2734 my ($self) = @_;
2503 2735
2504 return unless $::CONN;#d# manage and cache textures differently 2736 return unless $::CONN;
2737
2738 my $face;
2739
2740 if ($self->{frame}) {
2741 my $anim = $::CONN->{anim}[$self->{anim}];
2742
2743 $face = $anim->[ $self->{frame} % @$anim ]
2744 if $anim && @$anim;
2745 }
2746
2505 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2747 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2506 2748
2507 # TODO animation
2508 if ($tex) { 2749 if ($tex) {
2509 glEnable GL_TEXTURE_2D; 2750 glEnable GL_TEXTURE_2D;
2510 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2751 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2511 glColor 1, 1, 1, 1; 2752 glColor 1, 1, 1, 1;
2512 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2753 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2513 glDisable GL_TEXTURE_2D; 2754 glDisable GL_TEXTURE_2D;
2514 } 2755 }
2515} 2756}
2516 2757
2517############################################################################# 2758sub DESTROY {
2759 my ($self) = @_;
2518 2760
2761 $self->{timer}->cancel
2762 if $self->{timer};
2763
2764 $self->SUPER::DESTROY;
2765}
2766
2767#############################################################################
2768
2519package CFClient::UI::InventoryItem; 2769package CFClient::UI::Inventory;
2520 2770
2521our @ISA = CFClient::UI::HBox::; 2771our @ISA = CFClient::UI::ScrolledWindow::;
2522
2523sub _item_to_desc {
2524 my ($item) = @_;
2525
2526 my $desc =
2527 $item->{nrof} < 2
2528 ? $item->{name}
2529 : "$item->{nrof} $item->{name_pl}";
2530
2531 $item->{flags} & Crossfire::Protocol::F_OPEN
2532 and $desc .= " (open)";
2533 $item->{flags} & Crossfire::Protocol::F_APPLIED
2534 and $desc .= " (applied)";
2535 $item->{flags} & Crossfire::Protocol::F_UNPAID
2536 and $desc .= " (unpaid)";
2537 $item->{flags} & Crossfire::Protocol::F_MAGIC
2538 and $desc .= " (magic)";
2539 $item->{flags} & Crossfire::Protocol::F_CURSED
2540 and $desc .= " (cursed)";
2541 $item->{flags} & Crossfire::Protocol::F_DAMNED
2542 and $desc .= " (damned)";
2543 $item->{flags} & Crossfire::Protocol::F_LOCKED
2544 and $desc .= " *";
2545
2546 $desc
2547}
2548 2772
2549sub new { 2773sub new {
2550 my $class = shift; 2774 my $class = shift;
2551 2775
2552 my %args = @_;
2553
2554 my $item = delete $args{item};
2555
2556 my $desc = _item_to_desc ($item);
2557
2558 my $self = $class->SUPER::new ( 2776 my $self = $class->SUPER::new (
2559 can_hover => 1, 2777 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2560 can_events => 1,
2561 tooltip => ((CFClient::UI::Label::escape $desc)
2562 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2563 connect_button_down => sub {
2564 my ($self, $ev, $x, $y) = @_;
2565
2566 # todo: maybe put examine on 1? but should just be a tooltip :(
2567 if ($ev->{button} == 1) {
2568 my $targ = $::CONN->{player}{tag};
2569
2570 if ($item->{container} == $::CONN->{player}{tag}) {
2571 $targ = $main::OPENCONT;
2572 }
2573
2574 $::CONN->send ("move $targ $item->{tag} 0");
2575 } elsif ($ev->{button} == 2) {
2576 $::CONN->send ("apply $item->{tag}");
2577 } elsif ($ev->{button} == 3) {
2578 my @menu_items = (
2579 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2580 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2581 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2582 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2583 [
2584 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2585 sub { $::CONN->send ("lock $item->{tag}") },
2586 ],
2587 );
2588
2589 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2590 }
2591
2592 1
2593 },
2594 %args
2595 );
2596
2597
2598 $self->add (new CFClient::UI::Face
2599 can_events => 0,
2600 face => $item->{face},
2601 anim => $item->{anim},
2602 animspeed => $item->{animspeed},
2603 );
2604
2605 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2606
2607 $self->{item} = $item;
2608
2609 $self->update_item;
2610
2611 $self
2612}
2613
2614sub update_item {
2615 my ($self) = @_;
2616
2617 my $desc = _item_to_desc ($self->{item});
2618
2619 $self->{name_lbl}->set_text ($desc);
2620}
2621
2622#############################################################################
2623
2624package CFClient::UI::Inventory;
2625
2626our @ISA = CFClient::UI::ScrolledWindow::;
2627
2628sub new {
2629 my $class = shift;
2630
2631 my $self = $class->SUPER::new (
2632 scrolled => (new CFClient::UI::VBox),
2633 @_, 2778 @_,
2634 ); 2779 );
2635 2780
2636 $self 2781 $self
2637} 2782}
2647 or ($a->{name} cmp $b->{name}) 2792 or ($a->{name} cmp $b->{name})
2648 } @$items; 2793 } @$items;
2649 2794
2650 $self->{real_items} = \@items; 2795 $self->{real_items} = \@items;
2651 2796
2797 my $row = 0;
2652 for my $item (@items) { 2798 for my $item (@items) {
2653 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; 2799 CFClient::Item::update_widgets $item;
2654 $item->update_item ();
2655 }
2656 2800
2657 $self->{scrolled}->add (@items); 2801 $self->{scrolled}->add (0, $row, $item->{face_widget});
2802 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2803 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2658 2804
2659# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2805 $row++;
2660} 2806 }
2661
2662sub size_request {
2663 my ($self) = @_;
2664 ($self->{req_w}, $self->{req_h});
2665} 2807}
2666 2808
2667############################################################################# 2809#############################################################################
2668 2810
2669package CFClient::UI::Menu; 2811package CFClient::UI::Menu;
2704 2846
2705# popup given the event (must be a mouse button down event currently) 2847# popup given the event (must be a mouse button down event currently)
2706sub popup { 2848sub popup {
2707 my ($self, $ev) = @_; 2849 my ($self, $ev) = @_;
2708 2850
2709 $self->emit ("popdown"); 2851 $self->_emit ("popdown");
2710 2852
2711 # maybe save $GRAB? must be careful about events... 2853 # maybe save $GRAB? must be careful about events...
2712 $GRAB = $self; 2854 $GRAB = $self;
2713 $self->{button} = $ev->{button}; 2855 $self->{button} = $ev->{button};
2714 2856
2729 2871
2730 if ($ev->{button} == $self->{button}) { 2872 if ($ev->{button} == $self->{button}) {
2731 undef $GRAB; 2873 undef $GRAB;
2732 $self->hide; 2874 $self->hide;
2733 2875
2734 $self->emit ("popdown"); 2876 $self->_emit ("popdown");
2735 $self->{hover}[1]->() if $self->{hover}; 2877 $self->{hover}[1]->() if $self->{hover};
2736 } 2878 }
2737} 2879}
2738 2880
2739############################################################################# 2881#############################################################################
2774 # TODO: doesn't handle markup well (read as: at all) 2916 # TODO: doesn't handle markup well (read as: at all)
2775 my $short = $item->{count} > 1 2917 my $short = $item->{count} > 1
2776 ? "<b>$item->{count} ×</b> $item->{text}" 2918 ? "<b>$item->{count} ×</b> $item->{text}"
2777 : $item->{text}; 2919 : $item->{text};
2778 2920
2779 my $fontsize = $item->{fontsize} || $self->{fontsize};
2780
2781 for ($short) { 2921 for ($short) {
2782 s/^\s+//; 2922 s/^\s+//;
2783 s/\s+/ /g; 2923 s/\s+/ /g;
2784 my $len = int 40 / $fontsize;
2785 substr $_, $len, length, "…" if $len < length;
2786 } 2924 }
2787 2925
2788 new CFClient::UI::Label 2926 new CFClient::UI::Label
2789 markup => $short, 2927 markup => $short,
2790 tooltip => $item->{tooltip}, 2928 tooltip => $item->{tooltip},
2791 tooltip_font => $::FONT_PROP, 2929 tooltip_font => $::FONT_PROP,
2792 tooltip_width => 0.67, 2930 tooltip_width => 0.67,
2793 fontsize => $fontsize, 2931 fontsize => $item->{fontsize} || $self->{fontsize},
2932 max_w => $::WIDTH * 0.44,
2794 fg => $item->{fg}, 2933 fg => $item->{fg},
2795 can_events => 1, 2934 can_events => 1,
2796 can_hover => 1 2935 can_hover => 1
2797 }; 2936 };
2798 } 2937 }
2804sub add { 2943sub add {
2805 my ($self, $text, %arg) = @_; 2944 my ($self, $text, %arg) = @_;
2806 2945
2807 $text =~ s/^\s+//; 2946 $text =~ s/^\s+//;
2808 $text =~ s/\s+$//; 2947 $text =~ s/\s+$//;
2948
2949 return unless $text;
2809 2950
2810 my $timeout = time + ((delete $arg{timeout}) || 60); 2951 my $timeout = time + ((delete $arg{timeout}) || 60);
2811 2952
2812 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2953 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2813 2954
2835 } 2976 }
2836 2977
2837 $self->reorder; 2978 $self->reorder;
2838} 2979}
2839 2980
2981sub reconfigure {
2982 my ($self) = @_;
2983
2984 delete $_->{label}
2985 for values %{ $self->{item} || {} };
2986
2987 $self->reorder;
2988 $self->SUPER::reconfigure;
2989}
2990
2840############################################################################# 2991#############################################################################
2841 2992
2842package CFClient::UI::Root; 2993package CFClient::UI::Root;
2843 2994
2844our @ISA = CFClient::UI::Container::; 2995our @ISA = CFClient::UI::Container::;
2846use CFClient::OpenGL; 2997use CFClient::OpenGL;
2847 2998
2848sub new { 2999sub new {
2849 my $class = shift; 3000 my $class = shift;
2850 3001
2851 $class->SUPER::new ( 3002 my $self = $class->SUPER::new (
3003 visible => 1,
2852 @_, 3004 @_,
2853 ) 3005 );
3006
3007 Scalar::Util::weaken ($self->{root} = $self);
3008
3009 $self
2854} 3010}
2855 3011
2856sub configure { 3012sub configure {
2857 my ($self, $x, $y, $w, $h) = @_; 3013 my ($self, $x, $y, $w, $h) = @_;
2858 3014
2859 $self->{w} = $w; 3015 $self->{w} = $w;
2860 $self->{h} = $h; 3016 $self->{h} = $h;
2861} 3017}
2862 3018
2863sub check_size { 3019sub reconfigure {
2864 my ($self) = @_; 3020 my ($self) = @_;
2865 3021
3022 $self->SUPER::reconfigure;
3023
2866 $self->size_allocate ($self->{w}, $self->{h}) 3024 $self->size_allocate ($self->{w}, $self->{h}, 1)
2867 if $self->{w}; 3025 if $self->{w};
2868} 3026}
2869 3027
2870sub size_request { 3028sub size_request {
2871 my ($self) = @_; 3029 my ($self) = @_;
2872 3030
2873 ($self->{w}, $self->{h}) 3031 ($self->{w}, $self->{h})
2874} 3032}
2875 3033
2876sub size_allocate { 3034sub size_allocate {
2877 my ($self, $w, $h) = @_; 3035 my ($self, $w, $h, $changed) = @_;
2878
2879 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2880 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2881
2882 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2883 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2884 3036
2885 for my $child ($self->children) { 3037 for my $child ($self->children) {
2886 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3038 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2887 3039
2888 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3040 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2889 if exists $child->{req_x}; 3041 if exists $child->{req_x};
2890 3042
2891 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3043 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2892 if exists $child->{req_y}; 3044 if exists $child->{req_y};
2893 3045
3046 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3047
2894 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2895 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2896 3050
2897 $child->configure ($X, $Y, $W, $H); 3051 $child->configure ($X, $Y, $W, $H);
2898 } 3052 }
2911} 3065}
2912 3066
2913sub update { 3067sub update {
2914 my ($self) = @_; 3068 my ($self) = @_;
2915 3069
2916 $self->check_size;
2917 $::WANT_REFRESH++; 3070 $::WANT_REFRESH++;
2918} 3071}
2919 3072
2920sub add { 3073sub add {
2921 my ($self, @children) = @_; 3074 my ($self, @children) = @_;
2922 3075
2923 for my $child (@children) { 3076 for my $child (@children) {
2924 $child->{toplevel} = 1; 3077 $child->{is_toplevel} = 1;
2925 3078
2926 # integerise window positions 3079 # integerise window positions
2927 $child->{x} = int $child->{x}; 3080 $child->{x} = int $child->{x};
2928 $child->{y} = int $child->{y}; 3081 $child->{y} = int $child->{y};
2929 } 3082 }
2930 3083
2931 $self->SUPER::add (@children); 3084 $self->SUPER::add (@children);
2932} 3085}
2933 3086
3087sub remove {
3088 my ($self, @children) = @_;
3089
3090 $self->SUPER::remove (@children);
3091
3092 while (@children) {
3093 my $w = pop @children;
3094 push @children, $w->children;
3095 $w->set_invisible;
3096 }
3097}
3098
2934sub on_refresh { 3099sub on_refresh {
2935 my ($self, $id, $cb) = @_; 3100 my ($self, $id, $cb) = @_;
2936 3101
2937 $self->{refresh_hook}{$id} = $cb; 3102 $self->{refresh_hook}{$id} = $cb;
2938} 3103}
2949 while ($self->{refresh_hook}) { 3114 while ($self->{refresh_hook}) {
2950 $_->() 3115 $_->()
2951 for values %{delete $self->{refresh_hook}}; 3116 for values %{delete $self->{refresh_hook}};
2952 } 3117 }
2953 3118
2954 if ($self->{check_size}) { 3119 if ($self->{realloc}) {
2955 my @queue = ([], []); 3120 my @queue;
2956 3121
2957 for (;;) { 3122 while () {
2958 if ($self->{check_size}) { 3123 if ($self->{realloc}) {
2959 # heuristic: check containers last 3124 #TODO use array-of-depth approach
2960 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3125
2961 for values %{delete $self->{check_size}} 3126 @queue = sort { $a->{visible} <=> $b->{visible} }
3127 @queue, values %{delete $self->{realloc}};
2962 } 3128 }
2963 3129
2964 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3130 my $widget = pop @queue || last;
2965 3131
3132 $widget->{visible} or last; # do not resize invisible widgets
3133
2966 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3134 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
2967 ? @$widget{qw(user_w user_h)} 3135 ? @$widget{qw(def_w def_h)}
2968 : $widget->size_request; 3136 : $widget->size_request;
2969 3137
2970 if (delete $widget->{force_alloc}
2971 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2972 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3138 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2973 3139
2974 $widget->{req_w} = $w; 3140 $widget->{req_w} = $w;
2975 $widget->{req_h} = $h; 3141 $widget->{req_h} = $h;
2976 3142
2977 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3143 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2978 3144
2979 $widget->{parent}->check_size 3145 push @queue, $widget->{parent}
2980 if $widget->{parent}; 3146 if $widget->{parent};
2981 }
2982 } 3147 }
2983 } 3148 }
2984 3149
2985 while ($self->{size_alloc}) { 3150 while (my $size_alloc = delete $self->{size_alloc}) {
2986 for (values %{delete $self->{size_alloc}}) { 3151 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
2987 my ($widget, $w, $h) = @$_; 3152 values %$size_alloc;
3153
3154 while () {
3155 my ($widget, $w, $h) = @{ pop @queue or last };
2988 3156
2989 $w = 0 if $w < 0; 3157 $w = 0 if $w < 0;
2990 $h = 0 if $h < 0; 3158 $h = 0 if $h < 0;
2991 3159
3160 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3161
2992 $widget->{w} = $w; 3162 $widget->{w} = $w;
2993 $widget->{h} = $h; 3163 $widget->{h} = $h;
2994 $widget->size_allocate ($w, $h); 3164
2995 $widget->emit (size_allocate => $w, $h); 3165 $widget->emit (size_allocate => $w, $h, $changed);
2996 } 3166 }
2997 } 3167 }
2998 3168
2999 while ($self->{post_alloc_hook}) { 3169 while ($self->{post_alloc_hook}) {
3000 $_->() 3170 $_->()
3005 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3175 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3006 glClear GL_COLOR_BUFFER_BIT; 3176 glClear GL_COLOR_BUFFER_BIT;
3007 3177
3008 glMatrixMode GL_PROJECTION; 3178 glMatrixMode GL_PROJECTION;
3009 glLoadIdentity; 3179 glLoadIdentity;
3010 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3180 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3011 glMatrixMode GL_MODELVIEW; 3181 glMatrixMode GL_MODELVIEW;
3012 glLoadIdentity; 3182 glLoadIdentity;
3013 3183
3014 $self->_draw; 3184 $self->_draw;
3015} 3185}
3017############################################################################# 3187#############################################################################
3018 3188
3019package CFClient::UI; 3189package CFClient::UI;
3020 3190
3021$ROOT = new CFClient::UI::Root; 3191$ROOT = new CFClient::UI::Root;
3022$TOOLTIP = new CFClient::UI::Tooltip; 3192$TOOLTIP = new CFClient::UI::Tooltip z => 900;
3023 3193
30241 31941
3025 3195

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines