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.328 by root, Sat Jul 22 13:43:05 2006 UTC vs.
Revision 1.444 by root, Sat Dec 8 18:38:13 2007 UTC

1package CFClient::UI; 1package CFPlus::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util ();
7use List::Util (); 6use List::Util ();
8use Event;
9 7
10use CFClient; 8use CFPlus;
9use CFPlus::Pod;
11use CFClient::Texture; 10use CFPlus::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
27 $TOOLTIP->hide; 29 $TOOLTIP->hide;
28 30
29 $TOOLTIP->{owner} = $widget; 31 $TOOLTIP->{owner} = $widget;
32 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
30 33
31 return if $ENV{CFPLUS_DEBUG} & 8; 34 return if $ENV{CFPLUS_DEBUG} & 8;
32 35
33 my $tip = $widget->{tooltip};
34
35 $tip = $tip->($widget) if CODE:: eq ref $tip;
36
37 $TOOLTIP->set_tooltip_from ($widget); 36 $TOOLTIP->set_tooltip_from ($widget);
38 $TOOLTIP->show; 37 $TOOLTIP->show;
39 } 38 }
40 39
41 return; 40 return;
42 } 41 }
43 } 42 }
44 } 43 }
45 44
46 $TOOLTIP->hide; 45 $TOOLTIP->hide;
46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
47 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
48}); 48};
49 49
50sub get_layout { 50sub get_layout {
51 my $layout; 51 my $layout;
52 52
53 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
79sub feed_sdl_key_up_event { 79sub feed_sdl_key_up_event {
80 $FOCUS->emit (key_up => $_[0]) 80 $FOCUS->emit (key_up => $_[0])
81 if $FOCUS; 81 if $FOCUS;
82} 82}
83 83
84sub check_hover {
85 my ($widget) = @_;
86
87 if ($widget != $HOVER) {
88 my $hover = $HOVER; $HOVER = $widget;
89
90 $hover->update if $hover && $hover->{can_hover};
91 $HOVER->update if $HOVER && $HOVER->{can_hover};
92
93 $TOOLTIP_WATCHER->again;
94 }
95}
96
84sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
85 my ($ev) = @_; 98 my ($ev) = @_;
86 my ($x, $y) = ($ev->{x}, $ev->{y}); 99 my ($x, $y) = ($ev->{x}, $ev->{y});
87 100
88 warn "button down $ev->{button}\n";#d# 101 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
89 102
90 if (!$BUTTON_STATE) { 103 unless ($GRAB) {
91 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
92 105
93 $GRAB = $widget; 106 $GRAB = $widget;
94 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
95 108
96 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
97 } 110 }
98 111
99 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 112 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel
115 my $delta = $ev->{button} * 2 - 9;
116 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT;
100 117
101 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) 118 $ev->{dx} = $shift ? $delta : 0;
102 if $GRAB; 119 $ev->{dy} = $shift ? 0 : $delta;
120
121 $GRAB->emit (mouse_wheel => $ev);
122 } else {
123 $GRAB->emit (button_down => $ev)
124 }
125 }
103} 126}
104 127
105sub feed_sdl_button_up_event { 128sub feed_sdl_button_up_event {
106 my ($ev) = @_; 129 my ($ev) = @_;
107 my ($x, $y) = ($ev->{x}, $ev->{y});
108 130
109 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 131 my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y});
110 132
111 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 133 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
112 134
113 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) 135 $GRAB->emit (button_up => $ev)
114 if $GRAB; 136 if $GRAB && $ev->{button} != 4 && $ev->{button} != 5;
115 137
116 if (!$BUTTON_STATE) { 138 unless ($BUTTON_STATE) {
117 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
118 $grab->update if $grab; 140 $grab->update if $grab;
119 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
120 142
143 check_hover $widget;
121 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->trigger;
122 } 145 }
123} 146}
124 147
125sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
126 my ($ev) = @_; 149 my ($ev) = @_;
127 my ($x, $y) = ($ev->{x}, $ev->{y}); 150 my ($x, $y) = ($ev->{x}, $ev->{y});
128 151
129 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 152 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
130 153
131 if ($widget != $HOVER) { 154 check_hover $widget;
132 my $hover = $HOVER; $HOVER = $widget;
133 155
134 $hover->update if $hover && $hover->{can_hover}; 156 $HOVER->emit (mouse_motion => $ev)
135 $HOVER->update if $HOVER && $HOVER->{can_hover};
136
137 $TOOLTIP_WATCHER->start;
138 }
139
140 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
141 if $HOVER; 157 if $HOVER;
142} 158}
143 159
144# convert position array to integers 160# convert position array to integers
145sub harmonize { 161sub harmonize {
195 reconfigure_widgets; 211 reconfigure_widgets;
196} 212}
197 213
198############################################################################# 214#############################################################################
199 215
216package CFPlus::UI::Event;
217
218sub xy {
219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
220}
221
222#############################################################################
223
200package CFClient::UI::Base; 224package CFPlus::UI::Base;
201 225
202use strict; 226use strict;
203 227
204use CFClient::OpenGL; 228use CFPlus::OpenGL;
205 229
206sub new { 230sub new {
207 my $class = shift; 231 my $class = shift;
208 232
209 my $self = bless { 233 my $self = bless {
214 h => undef, 238 h => undef,
215 can_events => 1, 239 can_events => 1,
216 @_ 240 @_
217 }, $class; 241 }, $class;
218 242
219 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 243 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
220 244
221 for (keys %$self) { 245 for (keys %$self) {
222 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
223 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
224 } 248 }
225 } 249 }
226 250
227 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) {
228 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x};
229 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{y}; 253 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y};
230 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{w}; 254 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w};
231 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{h}; 255 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h};
232 256
233 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
234 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
235 259
236 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
241 265
242sub destroy { 266sub destroy {
243 my ($self) = @_; 267 my ($self) = @_;
244 268
245 $self->hide; 269 $self->hide;
270 $self->emit ("destroy");
246 %$self = (); 271 %$self = ();
247} 272}
248 273
274sub TO_JSON {
275 { "\fw" => $_[0]{s_id} }
276}
277
249sub show { 278sub show {
250 my ($self) = @_; 279 my ($self) = @_;
251 280
252 return if $self->{parent}; 281 return if $self->{parent};
253 282
254 $CFClient::UI::ROOT->add ($self); 283 $CFPlus::UI::ROOT->add ($self);
255} 284}
256 285
257sub set_visible { 286sub set_visible {
258 my ($self) = @_; 287 my ($self) = @_;
259 288
280 delete $self->{root}; 309 delete $self->{root};
281 310
282 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
283 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
284 313
285 $CFClient::UI::TOOLTIP_WATCHER->cb->() 314 $CFPlus::UI::TOOLTIP_WATCHER->trigger
286 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
287 316
288 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
289 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
290} 319}
292sub set_visibility { 321sub set_visibility {
293 my ($self, $visible) = @_; 322 my ($self, $visible) = @_;
294 323
295 return if $self->{visible} == $visible; 324 return if $self->{visible} == $visible;
296 325
297 $visible ? $self->hide 326 $visible ? $self->show
298 : $self->show; 327 : $self->hide;
299} 328}
300 329
301sub toggle_visibility { 330sub toggle_visibility {
302 my ($self) = @_; 331 my ($self) = @_;
303 332
332 $self->{force_h} = $h; 361 $self->{force_h} = $h;
333 362
334 $self->realloc; 363 $self->realloc;
335} 364}
336 365
366# traverse the widget chain up to find the maximum "physical" size constraints
367sub get_max_wh {
368 my ($self) = @_;
369
370 return $self->{parent}->get_max_wh
371 if $self->{parent};
372
373 ($::WIDTH, $::HEIGHT)
374}
375
337sub size_request { 376sub size_request {
338 require Carp; 377 require Carp;
339 Carp::confess "size_request is abstract"; 378 Carp::confess "size_request is abstract";
340} 379}
341 380
347 my ($self, $x, $y, $w, $h) = @_; 386 my ($self, $x, $y, $w, $h) = @_;
348 387
349 if ($self->{aspect}) { 388 if ($self->{aspect}) {
350 my ($ow, $oh) = ($w, $h); 389 my ($ow, $oh) = ($w, $h);
351 390
352 $w = List::Util::min $w, int $h * $self->{aspect}; 391 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect};
353 $h = List::Util::min $h, int $w / $self->{aspect}; 392 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect};
354 393
355 # use alignment to adjust x, y 394 # use alignment to adjust x, y
356 395
357 $x += int 0.5 * ($ow - $w); 396 $x += int 0.5 * ($ow - $w);
358 $y += int 0.5 * ($oh - $h); 397 $y += int 0.5 * ($oh - $h);
399 438
400 return if $self->{tooltip} eq $tooltip; 439 return if $self->{tooltip} eq $tooltip;
401 440
402 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
403 442
404 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
405 delete $CFClient::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
406 $CFClient::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->trigger;
407 } 446 }
408} 447}
409 448
410# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
411sub coord2local { 450sub coord2local {
412 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
413 452
453 Carp::confess unless $self->{parent};#d#
454
414 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
415} 456}
416 457
417# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
418sub coord2global { 459sub coord2global {
419 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
461
462 Carp::confess unless $self->{parent};#d#
420 463
421 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
422} 465}
423 466
424sub invoke_focus_in { 467sub invoke_focus_in {
458 501
459sub invoke_mouse_motion { 0 } 502sub invoke_mouse_motion { 0 }
460sub invoke_button_up { 0 } 503sub invoke_button_up { 0 }
461sub invoke_key_down { 0 } 504sub invoke_key_down { 0 }
462sub invoke_key_up { 0 } 505sub invoke_key_up { 0 }
506sub invoke_mouse_wheel { 0 }
463 507
464sub invoke_button_down { 508sub invoke_button_down {
465 my ($self, $ev, $x, $y) = @_; 509 my ($self, $ev, $x, $y) = @_;
466 510
467 $self->grab_focus; 511 $self->grab_focus;
468 512
469 warn "button down $ev->{button} $x $y\n";#d#
470
471 0 513 0
472} 514}
473 515
474sub connect { 516sub connect {
475 my ($self, $signal, $cb) = @_; 517 my ($self, $signal, $cb) = @_;
476 518
477 push @{ $self->{signal_cb}{$signal} }, $cb; 519 push @{ $self->{signal_cb}{$signal} }, $cb;
520
521 defined wantarray and CFPlus::guard {
522 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
523 @{ $self->{signal_cb}{$signal} };
524 }
478} 525}
526
527sub disconnect_all {
528 my ($self, $signal) = @_;
529
530 delete $self->{signal_cb}{$signal};
531}
532
533my %has_coords = (
534 button_down => 1,
535 button_up => 1,
536 mouse_motion => 1,
537 mouse_wheel => 1,
538);
479 539
480sub emit { 540sub emit {
481 my ($self, $signal, @args) = @_; 541 my ($self, $signal, @args) = @_;
482 542
483 #d##TODO# stop propagating at first true, do not use sum 543 # I do not really like this solution, but I do not like duplication
484 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before 544 # and needlessly verbose code, either.
545 my @append
546 = $has_coords{$signal}
547 ? $args[0]->xy ($self)
548 : ();
549
550 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
551
552 for my $cb (
553 @{$self->{signal_cb}{$signal} || []}, # before
485 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure 554 ($self->can ("invoke_$signal") || sub { 1 }), # closure
555 ) {
556 return $cb->($self, @args, @append) || next;
557 }
558
559 # parent
486 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 560 $self->{parent} && $self->{parent}->emit ($signal, @args)
487} 561}
488 562
489sub find_widget { 563#sub find_widget {
490 my ($self, $x, $y) = @_; 564# in .xs
491
492 return () unless $self->{can_events};
493
494 return $self
495 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
496 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
497
498 ()
499}
500 565
501sub set_parent { 566sub set_parent {
502 my ($self, $parent) = @_; 567 my ($self, $parent) = @_;
503 568
504 Scalar::Util::weaken ($self->{parent} = $parent); 569 CFPlus::weaken ($self->{parent} = $parent);
505 $self->set_visible if $parent->{visible}; 570 $self->set_visible if $parent->{visible};
506} 571}
507 572
508sub realloc { 573sub realloc {
509 my ($self) = @_; 574 my ($self) = @_;
535 600
536# using global variables seems a bit hacky, but passing through all drawing 601# using global variables seems a bit hacky, but passing through all drawing
537# functions seems pointless. 602# functions seems pointless.
538our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 603our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
539 604
540sub draw { 605#sub draw {
541 my ($self) = @_; 606#CFPlus.xs
542
543 return unless $self->{h} && $self->{w};
544
545 # update screen rectangle
546 local $draw_x = $draw_x + $self->{x};
547 local $draw_y = $draw_y + $self->{y};
548
549 # skip widgets that are entirely outside the drawing area
550 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
551 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
552
553 glPushMatrix;
554 glTranslate $self->{x}, $self->{y}, 0;
555
556 if ($self == $HOVER && $self->{can_hover}) {
557 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
558 glEnable GL_BLEND;
559 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
560 glBegin GL_QUADS;
561 glVertex 0 , 0;
562 glVertex $self->{w}, 0;
563 glVertex $self->{w}, $self->{h};
564 glVertex 0 , $self->{h};
565 glEnd;
566 glDisable GL_BLEND;
567 }
568
569 if ($ENV{CFPLUS_DEBUG} & 1) {
570 glPushMatrix;
571 glColor 1, 1, 0, 1;
572 glTranslate 0.375, 0.375;
573 glBegin GL_LINE_LOOP;
574 glVertex 0 , 0;
575 glVertex $self->{w} - 1, 0;
576 glVertex $self->{w} - 1, $self->{h} - 1;
577 glVertex 0 , $self->{h} - 1;
578 glEnd;
579 glPopMatrix;
580 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
581 }
582
583 $self->_draw;
584 glPopMatrix;
585}
586 607
587sub _draw { 608sub _draw {
588 my ($self) = @_; 609 my ($self) = @_;
589 610
590 warn "no draw defined for $self\n"; 611 warn "no draw defined for $self\n";
591} 612}
592 613
593sub DESTROY { 614sub DESTROY {
594 my ($self) = @_; 615 my ($self) = @_;
595 616
596 return if CFClient::in_destruct; 617 return if CFPlus::in_destruct;
597 618
598 delete $WIDGET{$self+0}; 619 local $@;
599
600 eval { $self->destroy }; 620 eval { $self->destroy };
601 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 621 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
622
623 delete $WIDGET{$self+0};
602} 624}
603 625
604############################################################################# 626#############################################################################
605 627
606package CFClient::UI::DrawBG; 628package CFPlus::UI::DrawBG;
607 629
608our @ISA = CFClient::UI::Base::; 630our @ISA = CFPlus::UI::Base::;
609 631
610use strict; 632use strict;
611use CFClient::OpenGL; 633use CFPlus::OpenGL;
612 634
613sub new { 635sub new {
614 my $class = shift; 636 my $class = shift;
615
616 # range [value, low, high, page]
617 637
618 $class->SUPER::new ( 638 $class->SUPER::new (
619 #bg => [0, 0, 0, 0.2], 639 #bg => [0, 0, 0, 0.2],
620 #active_bg => [1, 1, 1, 0.5], 640 #active_bg => [1, 1, 1, 0.5],
621 @_ 641 @_
633 my ($w, $h) = @$self{qw(w h)}; 653 my ($w, $h) = @$self{qw(w h)};
634 654
635 glEnable GL_BLEND; 655 glEnable GL_BLEND;
636 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 656 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
637 glColor_premultiply @$color; 657 glColor_premultiply @$color;
638
639 glBegin GL_QUADS;
640 glVertex 0 , 0;
641 glVertex 0 , $h;
642 glVertex $w, $h; 658 glRect 0, 0, $w, $h;
643 glVertex $w, 0;
644 glEnd;
645
646 glDisable GL_BLEND; 659 glDisable GL_BLEND;
647 } 660 }
648} 661}
649 662
650############################################################################# 663#############################################################################
651 664
652package CFClient::UI::Empty; 665package CFPlus::UI::Empty;
653 666
654our @ISA = CFClient::UI::Base::; 667our @ISA = CFPlus::UI::Base::;
655 668
656sub new { 669sub new {
657 my ($class, %arg) = @_; 670 my ($class, %arg) = @_;
658 $class->SUPER::new (can_events => 0, %arg); 671 $class->SUPER::new (can_events => 0, %arg);
659} 672}
666 679
667sub draw { } 680sub draw { }
668 681
669############################################################################# 682#############################################################################
670 683
671package CFClient::UI::Container; 684package CFPlus::UI::Container;
672 685
673our @ISA = CFClient::UI::Base::; 686our @ISA = CFPlus::UI::Base::;
674 687
675sub new { 688sub new {
676 my ($class, %arg) = @_; 689 my ($class, %arg) = @_;
677 690
678 my $children = delete $arg{children}; 691 my $children = delete $arg{children};
682 can_events => 0, 695 can_events => 0,
683 %arg, 696 %arg,
684 ); 697 );
685 698
686 $self->add (@$children) 699 $self->add (@$children)
687 if $children; 700 if $children && @$children;
688 701
689 $self 702 $self
703}
704
705sub realloc {
706 my ($self) = @_;
707
708 $self->{force_realloc} = 1;
709 $self->{force_size_alloc} = 1;
710 $self->SUPER::realloc;
690} 711}
691 712
692sub add { 713sub add {
693 my ($self, @widgets) = @_; 714 my ($self, @widgets) = @_;
694 715
695 $_->set_parent ($self) 716 $_->set_parent ($self)
696 for @widgets; 717 for @widgets;
697 718
719 # TODO: only do this in widgets that need it, e.g. root, fixed
698 use sort 'stable'; 720 use sort 'stable';
699 721
700 $self->{children} = [ 722 $self->{children} = [
701 sort { $a->{z} <=> $b->{z} } 723 sort { $a->{z} <=> $b->{z} }
702 @{$self->{children}}, @widgets 724 @{$self->{children}}, @widgets
703 ]; 725 ];
704 726
705 $self->realloc; 727 $self->realloc;
728
729 $self->emit (c_add => \@widgets);
730
731 map $_+0, @widgets
706} 732}
707 733
708sub children { 734sub children {
709 @{ $_[0]{children} } 735 @{ $_[0]{children} }
710} 736}
711 737
712sub remove { 738sub remove {
713 my ($self, $child) = @_; 739 my ($self, @widgets) = @_;
714 740
741 $self->emit (c_remove => \@widgets);
742
743 for my $child (@widgets) {
715 delete $child->{parent}; 744 delete $child->{parent};
716 $child->hide; 745 $child->hide;
717
718 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 746 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
747 }
719 748
720 $self->realloc; 749 $self->realloc;
721} 750}
722 751
723sub clear { 752sub clear {
724 my ($self) = @_; 753 my ($self) = @_;
725 754
726 my $children = delete $self->{children}; 755 my $children = $self->{children};
727 $self->{children} = []; 756 $self->{children} = [];
728 757
729 for (@$children) { 758 for (@$children) {
730 delete $_->{parent}; 759 delete $_->{parent};
731 $_->hide; 760 $_->hide;
751} 780}
752 781
753sub _draw { 782sub _draw {
754 my ($self) = @_; 783 my ($self) = @_;
755 784
756 $_->draw for @{$self->{children}}; 785 $_->draw for $self->visible_children;
757} 786}
758 787
759############################################################################# 788#############################################################################
760 789
761package CFClient::UI::Bin; 790package CFPlus::UI::Bin;
762 791
763our @ISA = CFClient::UI::Container::; 792our @ISA = CFPlus::UI::Container::;
764 793
765sub new { 794sub new {
766 my ($class, %arg) = @_; 795 my ($class, %arg) = @_;
767 796
768 my $child = (delete $arg{child}) || new CFClient::UI::Empty::; 797 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::;
769 798
770 $class->SUPER::new (children => [$child], %arg) 799 $class->SUPER::new (children => [$child], %arg)
771} 800}
772 801
773sub add { 802sub add {
774 my ($self, $child) = @_; 803 my ($self, $child) = @_;
775 804
776 $self->SUPER::remove ($_) for @{ $self->{children} }; 805 $self->clear;
777 $self->SUPER::add ($child); 806 $self->SUPER::add ($child);
778} 807}
779 808
780sub remove { 809sub remove {
781 my ($self, $widget) = @_; 810 my ($self, $widget) = @_;
782 811
783 $self->SUPER::remove ($widget); 812 $self->SUPER::remove ($widget);
784 813
785 $self->{children} = [new CFClient::UI::Empty] 814 $self->{children} = [new CFPlus::UI::Empty]
786 unless @{$self->{children}}; 815 unless @{$self->{children}};
787} 816}
788 817
789sub child { $_[0]->{children}[0] } 818sub child { $_[0]->{children}[0] }
790 819
799 828
800 1 829 1
801} 830}
802 831
803############################################################################# 832#############################################################################
804
805# back-buffered drawing area 833# back-buffered drawing area
806 834
807package CFClient::UI::Window; 835package CFPlus::UI::Window;
808 836
809our @ISA = CFClient::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
810 838
811use CFClient::OpenGL; 839use CFPlus::OpenGL;
812 840
813sub new { 841sub new {
814 my ($class, %arg) = @_; 842 my ($class, %arg) = @_;
815 843
816 my $self = $class->SUPER::new (%arg); 844 my $self = $class->SUPER::new (%arg);
838} 866}
839 867
840sub render_child { 868sub render_child {
841 my ($self) = @_; 869 my ($self) = @_;
842 870
843 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 871 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub {
844 glClearColor 0, 0, 0, 0; 872 glClearColor 0, 0, 0, 0;
845 glClear GL_COLOR_BUFFER_BIT; 873 glClear GL_COLOR_BUFFER_BIT;
846 874
847 { 875 {
848 package CFClient::UI::Base; 876 package CFPlus::UI::Base;
849 877
850 ($draw_x, $draw_y, $draw_w, $draw_h) = 878 local ($draw_x, $draw_y, $draw_w, $draw_h) =
851 (0, 0, $self->{w}, $self->{h}); 879 (0, 0, $self->{w}, $self->{h});
880
881 $self->_render;
852 } 882 }
853
854 $self->_render;
855 }; 883 };
856} 884}
857 885
858sub _draw { 886sub _draw {
859 my ($self) = @_; 887 my ($self) = @_;
860
861 my ($w, $h) = @$self{qw(w h)};
862 888
863 my $tex = $self->{texture} 889 my $tex = $self->{texture}
864 or return; 890 or return;
865 891
866 glEnable GL_TEXTURE_2D; 892 glEnable GL_TEXTURE_2D;
867 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 893 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
868 glColor 0, 0, 0, 1; 894 glColor 0, 0, 0, 1;
869 895
870 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 896 $tex->draw_quad_alpha_premultiplied (0, 0);
871 897
872 glDisable GL_TEXTURE_2D; 898 glDisable GL_TEXTURE_2D;
873} 899}
874 900
875############################################################################# 901#############################################################################
876 902
877package CFClient::UI::ViewPort; 903package CFPlus::UI::ViewPort;
878 904
905use List::Util qw(min max);
906
879our @ISA = CFClient::UI::Window::; 907our @ISA = CFPlus::UI::Window::;
880 908
881sub new { 909sub new {
882 my $class = shift; 910 my $class = shift;
883 911
884 $class->SUPER::new ( 912 $class->SUPER::new (
891sub size_request { 919sub size_request {
892 my ($self) = @_; 920 my ($self) = @_;
893 921
894 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 922 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
895 923
896 $w = 10 if $self->{scroll_x}; 924 $w = 1 if $self->{scroll_x};
897 $h = 10 if $self->{scroll_y}; 925 $h = 1 if $self->{scroll_y};
898 926
899 ($w, $h) 927 ($w, $h)
900} 928}
901 929
902sub invoke_size_allocate { 930sub invoke_size_allocate {
914} 942}
915 943
916sub set_offset { 944sub set_offset {
917 my ($self, $x, $y) = @_; 945 my ($self, $x, $y) = @_;
918 946
947 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
948 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
949
950 if ($x != $self->{view_x} or $y != $self->{view_y}) {
919 $self->{view_x} = int $x; 951 $self->{view_x} = $x;
920 $self->{view_y} = int $y; 952 $self->{view_y} = $y;
921 953
954 $self->emit (changed => $x, $y);
922 $self->update; 955 $self->update;
956 }
957}
958
959sub set_center {
960 my ($self, $x, $y) = @_;
961
962 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
963}
964
965sub make_visible {
966 my ($self, $x, $y, $border) = @_;
967
968 if ( $x < $self->{view_x} + $self->{w} * $border
969 || $x > $self->{view_x} + $self->{w} * (1 - $border)
970 || $y < $self->{view_y} + $self->{h} * $border
971 || $y > $self->{view_y} + $self->{h} * (1 - $border)
972 ) {
973 $self->set_center ($x, $y);
974 }
923} 975}
924 976
925# hmm, this does not work for topleft of $self... but we should not ask for that 977# hmm, this does not work for topleft of $self... but we should not ask for that
926sub coord2local { 978sub coord2local {
927 my ($self, $x, $y) = @_; 979 my ($self, $x, $y) = @_;
942 my ($self, $x, $y) = @_; 994 my ($self, $x, $y) = @_;
943 995
944 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 996 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
945 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 997 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
946 ) { 998 ) {
947 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 999 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
948 } else { 1000 } else {
949 $self->CFClient::UI::Base::find_widget ($x, $y) 1001 $self->CFPlus::UI::Base::find_widget ($x, $y)
950 } 1002 }
951} 1003}
952 1004
953sub _render { 1005sub _render {
954 my ($self) = @_; 1006 my ($self) = @_;
955 1007
956 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x}; 1008 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x};
957 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y}; 1009 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y};
958 1010
959 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1011 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
960 1012
961 $self->SUPER::_render; 1013 $self->SUPER::_render;
962} 1014}
963 1015
964############################################################################# 1016#############################################################################
965 1017
966package CFClient::UI::ScrolledWindow; 1018package CFPlus::UI::ScrolledWindow;
967 1019
968our @ISA = CFClient::UI::HBox::; 1020our @ISA = CFPlus::UI::Table::;
969 1021
970sub new { 1022sub new {
971 my ($class, %arg) = @_; 1023 my ($class, %arg) = @_;
972 1024
973 my $child = delete $arg{child}; 1025 my $child = delete $arg{child};
974 1026
975 my $self; 1027 my $self;
976 1028
977 my $slider = new CFClient::UI::Slider 1029 my $hslider = new CFPlus::UI::Slider
1030 c_col => 0,
1031 c_row => 1,
1032 vertical => 0,
1033 range => [0, 0, 1, 0.01], # HACK fix
1034 on_changed => sub {
1035 $self->{hpos} = $_[1];
1036 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1037 },
1038 ;
1039
1040 my $vslider = new CFPlus::UI::Slider
1041 c_col => 1,
1042 c_row => 0,
978 vertical => 1, 1043 vertical => 1,
979 range => [0, 0, 1, 0.01], # HACK fix 1044 range => [0, 0, 1, 0.01], # HACK fix
980 on_changed => sub { 1045 on_changed => sub {
981 $self->{vp}->set_offset (0, $_[1]); 1046 $self->{vpos} = $_[1];
1047 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
982 }, 1048 },
983 ; 1049 ;
984 1050
985 $self = $class->SUPER::new ( 1051 $self = $class->SUPER::new (
986 vp => (new CFClient::UI::ViewPort expand => 1), 1052 scroll_x => 0,
1053 scroll_y => 1,
1054 can_events => 1,
987 slider => $slider, 1055 hslider => $hslider,
1056 vslider => $vslider,
1057 col_expand => [1, 0],
1058 row_expand => [1, 0],
988 %arg, 1059 %arg,
989 ); 1060 );
990 1061
1062 $self->{vp} = new CFPlus::UI::ViewPort
1063 c_col => 0,
1064 c_row => 0,
1065 expand => 1,
1066 scroll_x => $self->{scroll_x},
1067 scroll_y => $self->{scroll_y},
1068 on_changed => sub {
1069 my ($vp, $x, $y) = @_;
1070
1071 $vp->{parent}{hslider}->set_value ($x);
1072 $vp->{parent}{vslider}->set_value ($y);
1073
1074 0
1075 },
1076 on_size_allocate => sub {
1077 my ($vp, $w, $h) = @_;
1078 $vp->{parent}->update_slider;
1079 0
1080 },
1081 ;
1082
991 $self->SUPER::add ($self->{vp}, $self->{slider}); 1083 $self->SUPER::add ($self->{vp});
1084
992 $self->add ($child) if $child; 1085 $self->add ($child) if $child;
993 1086
994 $self 1087 $self
995} 1088}
996
997#TODO# update range on size_allocate depending on child
998 1089
999sub add { 1090sub add {
1000 my ($self, $widget) = @_; 1091 my ($self, $widget) = @_;
1001 1092
1002 $self->{vp}->add ($self->{child} = $widget); 1093 $self->{vp}->add ($self->{child} = $widget);
1003} 1094}
1004 1095
1096sub set_offset { shift->{vp}->set_offset (@_) }
1097sub set_center { shift->{vp}->set_center (@_) }
1098sub make_visible { shift->{vp}->make_visible (@_) }
1099
1100sub update_slider {
1101 my ($self) = @_;
1102
1103 my $child = ($self->{vp} or return)->child;
1104
1105 if ($self->{scroll_x}) {
1106 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1107 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1108
1109 my $visible = $w1 > $w2;
1110 if ($visible != $self->{hslider_visible}) {
1111 $self->{hslider_visible} = $visible;
1112 $visible ? $self->SUPER::add ($self->{hslider})
1113 : $self->SUPER::remove ($self->{hslider});
1114 }
1115 }
1116
1117 if ($self->{scroll_y}) {
1118 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1119 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1120
1121 my $visible = $h1 > $h2;
1122 if ($visible != $self->{vslider_visible}) {
1123 $self->{vslider_visible} = $visible;
1124 $visible ? $self->SUPER::add ($self->{vslider})
1125 : $self->SUPER::remove ($self->{vslider});
1126 }
1127 }
1128}
1129
1130sub start_dragging {
1131 my ($self, $ev) = @_;
1132
1133 $self->grab_focus;
1134
1135 my $ox = $self->{vp}{view_x};
1136 my $oy = $self->{vp}{view_y};
1137
1138 $self->{motion} = sub {
1139 my ($ev, $x, $y) = @_;
1140
1141 $ox -= $ev->{xrel};
1142 $oy -= $ev->{yrel};
1143
1144 $self->{vp}->set_offset ($ox, $oy);
1145 };
1146}
1147
1148sub invoke_mouse_wheel {
1149 my ($self, $ev) = @_;
1150
1151 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1152 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1153
1154 1
1155}
1156
1005sub invoke_button_down { 1157sub invoke_button_down {
1006 my ($self, $ev) = @_; 1158 my ($self, $ev, $x, $y) = @_;
1007 1159
1008 warn "button down $ev->{button}\n";#d# 1160 if ($ev->{button} == 2) {
1161 $self->start_dragging ($ev);
1162 return 1;
1009 1163 }
1164
1010 0 1165 0
1011} 1166}
1012 1167
1013sub update_slider { 1168sub invoke_button_up {
1014 my ($self) = @_; 1169 my ($self, $ev, $x, $y) = @_;
1015 1170
1016 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1171 if (delete $self->{motion}) {
1017} 1172 return 1;
1173 }
1018 1174
1019sub update { 1175 0
1020 my ($self) = @_; 1176}
1021 1177
1022 $self->SUPER::update; 1178sub invoke_mouse_motion {
1179 my ($self, $ev, $x, $y) = @_;
1023 1180
1024 $self->update_slider; 1181 if ($self->{motion}) {
1182 $self->{motion}->($ev, $x, $y);
1183 return 1;
1184 }
1185
1186 0
1025} 1187}
1026 1188
1027sub invoke_size_allocate { 1189sub invoke_size_allocate {
1028 my ($self, $w, $h) = @_; 1190 my ($self, $w, $h) = @_;
1029 1191
1030 $self->update_slider; 1192 $self->update_slider;
1031
1032 $self->SUPER::invoke_size_allocate ($w, $h) 1193 $self->SUPER::invoke_size_allocate ($w, $h)
1033} 1194}
1034 1195
1035############################################################################# 1196#############################################################################
1036 1197
1037package CFClient::UI::Frame; 1198package CFPlus::UI::Frame;
1038 1199
1039our @ISA = CFClient::UI::Bin::; 1200our @ISA = CFPlus::UI::Bin::;
1040 1201
1041use CFClient::OpenGL; 1202use CFPlus::OpenGL;
1042 1203
1043sub new { 1204sub new {
1044 my $class = shift; 1205 my $class = shift;
1045 1206
1046 $class->SUPER::new ( 1207 $class->SUPER::new (
1056 my ($w, $h) = @$self{qw(w h)}; 1217 my ($w, $h) = @$self{qw(w h)};
1057 1218
1058 glEnable GL_BLEND; 1219 glEnable GL_BLEND;
1059 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 1220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1060 glColor_premultiply @{ $self->{bg} }; 1221 glColor_premultiply @{ $self->{bg} };
1061
1062 glBegin GL_QUADS;
1063 glVertex 0 , 0;
1064 glVertex 0 , $h;
1065 glVertex $w, $h; 1222 glRect 0, 0, $w, $h;
1066 glVertex $w, 0;
1067 glEnd;
1068
1069 glDisable GL_BLEND; 1223 glDisable GL_BLEND;
1070 } 1224 }
1071 1225
1072 $self->SUPER::_draw; 1226 $self->SUPER::_draw;
1073} 1227}
1074 1228
1075############################################################################# 1229#############################################################################
1076 1230
1077package CFClient::UI::FancyFrame; 1231package CFPlus::UI::FancyFrame;
1078 1232
1079our @ISA = CFClient::UI::Bin::; 1233our @ISA = CFPlus::UI::Bin::;
1080 1234
1081use CFClient::OpenGL; 1235use CFPlus::OpenGL;
1236
1237sub new {
1238 my ($class, %arg) = @_;
1239
1240 if ((exists $arg{label}) && !ref $arg{label}) {
1241 $arg{label} = new CFPlus::UI::Label
1242 align => 1,
1243 valign => 0,
1244 text => $arg{label},
1245 fontsize => ($arg{border} || 0.8) * 0.75;
1246 }
1247
1248 my $self = $class->SUPER::new (
1249 # label => "",
1250 fg => [0.6, 0.3, 0.1],
1251 border => 0.8,
1252 style => 'single',
1253 %arg,
1254 );
1255
1256 $self
1257}
1258
1259sub add {
1260 my ($self, @widgets) = @_;
1261
1262 $self->SUPER::add (@widgets);
1263 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1264}
1265
1266sub border {
1267 int $_[0]{border} * $::FONTSIZE
1268}
1269
1270sub size_request {
1271 my ($self) = @_;
1272
1273 ($self->{label_w}, undef) = $self->{label}->size_request
1274 if $self->{label};
1275
1276 my ($w, $h) = $self->SUPER::size_request;
1277
1278 (
1279 $w + $self->border * 2,
1280 $h + $self->border * 2,
1281 )
1282}
1283
1284sub invoke_size_allocate {
1285 my ($self, $w, $h) = @_;
1286
1287 my $border = $self->border;
1288
1289 $w -= List::Util::max 0, $border * 2;
1290 $h -= List::Util::max 0, $border * 2;
1291
1292 if (my $label = $self->{label}) {
1293 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1294 $label->{h} = List::Util::min $h, $border;
1295 $label->invoke_size_allocate ($label->{w}, $label->{h});
1296 }
1297
1298 $self->child->configure ($border, $border, $w, $h);
1299
1300 1
1301}
1302
1303sub _draw {
1304 my ($self) = @_;
1305
1306 my $child = $self->{children}[0];
1307
1308 my $border = $self->border;
1309 my ($w, $h) = ($self->{w}, $self->{h});
1310
1311 $child->draw;
1312
1313 glColor @{$self->{fg}};
1314 glBegin GL_LINE_STRIP;
1315 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1316 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1317 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1318 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1319 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1320 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1321 glEnd;
1322
1323 if ($self->{label}) {
1324 glTranslate $border * 2, 0;
1325 $self->{label}->_draw;
1326 }
1327}
1328
1329#############################################################################
1330
1331package CFPlus::UI::Toplevel;
1332
1333our @ISA = CFPlus::UI::Bin::;
1334
1335use CFPlus::OpenGL;
1082 1336
1083my $bg = 1337my $bg =
1084 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", 1338 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png",
1085 mipmap => 1, wrap => 1; 1339 mipmap => 1, wrap => 1;
1086 1340
1087my @border = 1341my @border =
1088 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1342 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1089 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1343 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1344
1345my @icon =
1346 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1347 qw(x1_move.png x1_resize.png);
1090 1348
1091sub new { 1349sub new {
1092 my ($class, %arg) = @_; 1350 my ($class, %arg) = @_;
1093 1351
1094 my $self = $class->SUPER::new ( 1352 my $self = $class->SUPER::new (
1099 min_w => 64, 1357 min_w => 64,
1100 min_h => 32, 1358 min_h => 32,
1101 %arg, 1359 %arg,
1102 ); 1360 );
1103 1361
1104 $self->{title_widget} = new CFClient::UI::Label 1362 $self->{title_widget} = new CFPlus::UI::Label
1105 align => 0, 1363 align => 0,
1106 valign => 1, 1364 valign => 1,
1107 text => $self->{title}, 1365 text => $self->{title},
1108 fontsize => $self->{border}, 1366 fontsize => $self->{border},
1109 if exists $self->{title}; 1367 if exists $self->{title};
1110 1368
1111 if ($self->{has_close_button}) { 1369 if ($self->{has_close_button}) {
1112 $self->{close_button} = 1370 $self->{close_button} =
1113 new CFClient::UI::ImageButton 1371 new CFPlus::UI::ImageButton
1114 path => 'x1_close.png', 1372 path => 'x1_close.png',
1115 on_activate => sub { $self->emit ("delete") }; 1373 on_activate => sub { $self->emit ("delete") };
1116 1374
1117 $self->CFClient::UI::Container::add ($self->{close_button}); 1375 $self->CFPlus::UI::Container::add ($self->{close_button});
1118 } 1376 }
1119 1377
1120 $self 1378 $self
1121} 1379}
1122 1380
1123sub add { 1381sub add {
1124 my ($self, @widgets) = @_; 1382 my ($self, @widgets) = @_;
1125 1383
1126 $self->SUPER::add (@widgets); 1384 $self->SUPER::add (@widgets);
1127 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1385 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button};
1128 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1386 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1129} 1387}
1130 1388
1131sub border { 1389sub border {
1132 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1391}
1392
1393sub get_max_wh {
1394 my ($self) = @_;
1395
1396 return ($self->{w}, $self->{h})
1397 if $self->{visible} && $self->{w};
1398
1399 $self->SUPER::get_max_wh
1133} 1400}
1134 1401
1135sub size_request { 1402sub size_request {
1136 my ($self) = @_; 1403 my ($self) = @_;
1137 1404
1241 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1508 $self->{motion}->($ev, $x, $y) if $self->{motion};
1242 1509
1243 ! ! $self->{motion} 1510 ! ! $self->{motion}
1244} 1511}
1245 1512
1513sub invoke_visibility_change {
1514 my ($self, $visible) = @_;
1515
1516 delete $self->{motion} unless $visible;
1517
1518 0
1519}
1520
1246sub _draw { 1521sub _draw {
1247 my ($self) = @_; 1522 my ($self) = @_;
1248 1523
1249 my $child = $self->{children}[0]; 1524 my $child = $self->{children}[0];
1250 1525
1255 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1530 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1256 1531
1257 my $border = $self->border; 1532 my $border = $self->border;
1258 1533
1259 glColor @{ $self->{border_bg} }; 1534 glColor @{ $self->{border_bg} };
1260 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1535 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1261 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1536 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1262 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1537 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1263 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1538 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1539
1540 # move
1541 my $w2 = ($w - $border) * .5;
1542 my $h2 = ($h - $border) * .5;
1543 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1545 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1546
1547 # resize
1548 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1549 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1550 unless $self->{has_close_button};
1551 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1264 1553
1265 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1554 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1266 glColor @{ $self->{bg} }; 1555 glColor @{ $self->{bg} };
1267 1556
1268 # TODO: repeat texture not scale 1557 # TODO: repeat texture not scale
1287 if $self->{close_button}; 1576 if $self->{close_button};
1288} 1577}
1289 1578
1290############################################################################# 1579#############################################################################
1291 1580
1292package CFClient::UI::Table; 1581package CFPlus::UI::Table;
1293 1582
1294our @ISA = CFClient::UI::Base::; 1583our @ISA = CFPlus::UI::Container::;
1295 1584
1296use List::Util qw(max sum); 1585use List::Util qw(max sum);
1297 1586
1298use CFClient::OpenGL; 1587use CFPlus::OpenGL;
1299 1588
1300sub new { 1589sub new {
1301 my $class = shift; 1590 my $class = shift;
1302 1591
1303 $class->SUPER::new ( 1592 $class->SUPER::new (
1304 col_expand => [], 1593 col_expand => [],
1594 row_expand => [],
1305 @_, 1595 @_,
1306 ) 1596 )
1307} 1597}
1308 1598
1309sub children {
1310 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1311}
1312
1313sub add { 1599sub add {
1314 my ($self, $x, $y, $child) = @_; 1600 my ($self, @widgets) = @_;
1315 1601
1316 $child->set_parent ($self); 1602 for my $child (@widgets) {
1317 $self->{children}[$y][$x] = $child; 1603 $child->{c_rowspan} ||= 1;
1604 $child->{c_colspan} ||= 1;
1605 }
1318 1606
1319 $self->realloc; 1607 $self->SUPER::add (@widgets);
1320} 1608}
1321 1609
1322sub remove { 1610sub add_at {
1611 my $self = shift;
1612
1613 my @widgets;
1614
1615 while (@_) {
1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1617
1618 $child->{c_row} = $row;
1619 $child->{c_col} = $col;
1620
1621 push @widgets, $child;
1622 }
1623
1624 $self->add (@widgets);
1625}
1626
1627sub get_wh {
1323 my ($self, $child) = @_; 1628 my ($self) = @_;
1324 1629
1325 # TODO: not yet implemented 1630 my (@w, @h);
1326}
1327
1328# TODO: move to container class maybe? send children a signal on removal?
1329sub clear {
1330 my ($self) = @_;
1331 1631
1332 my @children = $self->children; 1632 my @children = $self->children;
1333 delete $self->{children}; 1633
1634 # first pass, columns
1635 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1637
1638 my $sw = sum @w[$c .. $c + $cs - 1];
1639
1640 if ($w > $sw) {
1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1642 }
1334 1643 }
1335 for (@children) {
1336 delete $_->{parent};
1337 $_->hide;
1338 }
1339 1644
1340 $self->realloc; 1645 # second pass, rows
1341} 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1342
1343sub get_wh {
1344 my ($self) = @_;
1345
1346 my (@w, @h);
1347
1348 for my $y (0 .. $#{$self->{children}}) {
1349 my $row = $self->{children}[$y]
1350 or next;
1351
1352 for my $x (0 .. $#$row) {
1353 my $widget = $row->[$x]
1354 or next;
1355 my ($w, $h) = @$widget{qw(req_w req_h)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1356 1648
1357 $w[$x] = max $w[$x], $w; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1358 $h[$y] = max $h[$y], $h; 1650
1651 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1359 } 1653 }
1360 } 1654 }
1361 1655
1362 (\@w, \@h) 1656 (\@w, \@h)
1363} 1657}
1379 my ($ws, $hs) = $self->get_wh; 1673 my ($ws, $hs) = $self->get_wh;
1380 1674
1381 my $req_w = (sum @$ws) || 1; 1675 my $req_w = (sum @$ws) || 1;
1382 my $req_h = (sum @$hs) || 1; 1676 my $req_h = (sum @$hs) || 1;
1383 1677
1384 # TODO: nicer code && do row_expand 1678 # now linearly scale the rows/columns to the allocated size
1385 my @col_expand = @{$self->{col_expand}}; 1679 my @col_expand = @{$self->{col_expand}};
1386 @col_expand = (1) x @$ws unless @col_expand; 1680 @col_expand = (1) x @$ws unless @col_expand;
1387 my $col_expand = (sum @col_expand) || 1; 1681 my $col_expand = (sum @col_expand) || 1;
1388 1682
1389 # linearly scale sizes
1390 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1391 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1392 1684
1393 CFClient::UI::harmonize $ws; 1685 CFPlus::UI::harmonize $ws;
1686
1687 my @row_expand = @{$self->{row_expand}};
1688 @row_expand = (1) x @$ws unless @row_expand;
1689 my $row_expand = (sum @row_expand) || 1;
1690
1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1692
1394 CFClient::UI::harmonize $hs; 1693 CFPlus::UI::harmonize $hs;
1395 1694
1396 my $y; 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1397 1697
1398 for my $r (0 .. $#{$self->{children}}) { 1698 for my $widget ($self->children) {
1399 my $row = $self->{children}[$r] 1699 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1400 or next;
1401 1700
1402 my $x = 0; 1701 $widget->configure (
1403 my $row_h = $hs->[$r]; 1702 $x[$c], $y[$r],
1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1404 1704 );
1405 for my $c (0 .. $#$row) { 1705 }
1406 my $col_w = $ws->[$c];
1407 1706
1408 if (my $widget = $row->[$c]) { 1707 1
1409 $widget->configure ($x, $y, $col_w, $row_h); 1708}
1410 }
1411 1709
1412 $x += $col_w; 1710#############################################################################
1711
1712package CFPlus::UI::Fixed;
1713
1714use List::Util qw(min max);
1715
1716our @ISA = CFPlus::UI::Container::;
1717
1718sub _scale($$$) {
1719 my ($rel, $val, $max) = @_;
1720
1721 $rel ? $val * $max : $val
1722}
1723
1724sub size_request {
1725 my ($self) = @_;
1726
1727 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1728
1729 # determine overall size by querying abs widgets
1730 for my $child ($self->visible_children) {
1731 unless ($child->{c_rel}) {
1732 my $x = $child->{c_x};
1733 my $y = $child->{c_y};
1734
1735 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1736 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1413 } 1737 }
1738 }
1414 1739
1415 $y += $row_h; 1740 my $W = $x2 - $x1;
1741 my $H = $y2 - $y1;
1742
1743 # now layout remaining widgets
1744 for my $child ($self->visible_children) {
1745 if ($child->{c_rel}) {
1746 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1747 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1748
1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1751 }
1752 }
1753
1754 my $W = $x2 - $x1;
1755 my $H = $y2 - $y1;
1756
1757 ($W, $H)
1758}
1759
1760sub invoke_size_allocate {
1761 my ($self, $W, $H) = @_;
1762
1763 for my $child ($self->visible_children) {
1764 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1765 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1766
1767 $x += $child->{c_halign} * $child->{req_w};
1768 $y += $child->{c_valign} * $child->{req_h};
1769
1770 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1416 } 1771 }
1417 1772
1418 1 1773 1
1419} 1774}
1420 1775
1421sub find_widget {
1422 my ($self, $x, $y) = @_;
1423
1424 $x -= $self->{x};
1425 $y -= $self->{y};
1426
1427 my $res;
1428
1429 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1430 $res = $_->find_widget ($x, $y)
1431 and return $res;
1432 }
1433
1434 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1435}
1436
1437sub _draw {
1438 my ($self) = @_;
1439
1440 for (grep $_, @{$self->{children}}) {
1441 $_->draw for grep $_, @$_;
1442 }
1443}
1444
1445############################################################################# 1776#############################################################################
1446 1777
1447package CFClient::UI::Box; 1778package CFPlus::UI::Box;
1448 1779
1449our @ISA = CFClient::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1450 1781
1451sub size_request { 1782sub size_request {
1452 my ($self) = @_; 1783 my ($self) = @_;
1784
1785 my @children = $self->visible_children;
1453 1786
1454 $self->{vertical} 1787 $self->{vertical}
1455 ? ( 1788 ? (
1456 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1457 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1458 ) 1791 )
1459 : ( 1792 : (
1460 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1461 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1462 ) 1795 )
1463} 1796}
1464 1797
1465sub invoke_size_allocate { 1798sub invoke_size_allocate {
1466 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
1487 $req[$_] += $space * $children[$_]{expand} 1820 $req[$_] += $space * $children[$_]{expand}
1488 for 0 .. $#children; 1821 for 0 .. $#children;
1489 } 1822 }
1490 } 1823 }
1491 1824
1492 CFClient::UI::harmonize \@req; 1825 CFPlus::UI::harmonize \@req;
1493 1826
1494 my $pos = 0; 1827 my $pos = 0;
1495 for (0 .. $#children) { 1828 for (0 .. $#children) {
1496 my $alloc = $req[$_]; 1829 my $alloc = $req[$_];
1497 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1830 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1502 1 1835 1
1503} 1836}
1504 1837
1505############################################################################# 1838#############################################################################
1506 1839
1507package CFClient::UI::HBox; 1840package CFPlus::UI::HBox;
1508 1841
1509our @ISA = CFClient::UI::Box::; 1842our @ISA = CFPlus::UI::Box::;
1510 1843
1511sub new { 1844sub new {
1512 my $class = shift; 1845 my $class = shift;
1513 1846
1514 $class->SUPER::new ( 1847 $class->SUPER::new (
1517 ) 1850 )
1518} 1851}
1519 1852
1520############################################################################# 1853#############################################################################
1521 1854
1522package CFClient::UI::VBox; 1855package CFPlus::UI::VBox;
1523 1856
1524our @ISA = CFClient::UI::Box::; 1857our @ISA = CFPlus::UI::Box::;
1525 1858
1526sub new { 1859sub new {
1527 my $class = shift; 1860 my $class = shift;
1528 1861
1529 $class->SUPER::new ( 1862 $class->SUPER::new (
1532 ) 1865 )
1533} 1866}
1534 1867
1535############################################################################# 1868#############################################################################
1536 1869
1537package CFClient::UI::Label; 1870package CFPlus::UI::Label;
1538 1871
1539our @ISA = CFClient::UI::DrawBG::; 1872our @ISA = CFPlus::UI::DrawBG::;
1540 1873
1541use CFClient::OpenGL; 1874use CFPlus::OpenGL;
1542 1875
1543sub new { 1876sub new {
1544 my ($class, %arg) = @_; 1877 my ($class, %arg) = @_;
1545 1878
1546 my $self = $class->SUPER::new ( 1879 my $self = $class->SUPER::new (
1549 #active_bg => none 1882 #active_bg => none
1550 #font => default_font 1883 #font => default_font
1551 #text => initial text 1884 #text => initial text
1552 #markup => initial narkup 1885 #markup => initial narkup
1553 #max_w => maximum pixel width 1886 #max_w => maximum pixel width
1887 #style => 0, # render flags
1554 ellipsise => 3, # end 1888 ellipsise => 3, # end
1555 layout => (new CFClient::Layout), 1889 layout => (new CFPlus::Layout),
1556 fontsize => 1, 1890 fontsize => 1,
1557 align => -1, 1891 align => -1,
1558 valign => -1, 1892 valign => -1,
1559 padding_x => 2, 1893 padding_x => 2,
1560 padding_y => 2, 1894 padding_y => 2,
1561 can_events => 0, 1895 can_events => 0,
1562 %arg 1896 %arg
1563 ); 1897 );
1564 1898
1565 if (exists $self->{template}) { 1899 if (exists $self->{template}) {
1566 my $layout = new CFClient::Layout; 1900 my $layout = new CFPlus::Layout;
1567 $layout->set_text (delete $self->{template}); 1901 $layout->set_text (delete $self->{template});
1568 $self->{template} = $layout; 1902 $self->{template} = $layout;
1569 } 1903 }
1570 1904
1571 if (exists $self->{markup}) { 1905 if (exists $self->{markup}) {
1575 } 1909 }
1576 1910
1577 $self 1911 $self
1578} 1912}
1579 1913
1580sub escape($) {
1581 local $_ = $_[0];
1582
1583 s/&/&amp;/g;
1584 s/>/&gt;/g;
1585 s/</&lt;/g;
1586
1587 $_
1588}
1589
1590sub update { 1914sub update {
1591 my ($self) = @_; 1915 my ($self) = @_;
1592 1916
1593 delete $self->{texture}; 1917 delete $self->{texture};
1594 $self->SUPER::update; 1918 $self->SUPER::update;
1597sub realloc { 1921sub realloc {
1598 my ($self) = @_; 1922 my ($self) = @_;
1599 1923
1600 delete $self->{ox}; 1924 delete $self->{ox};
1601 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1926}
1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1602} 1932}
1603 1933
1604sub set_text { 1934sub set_text {
1605 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1606 1936
1631 1961
1632sub size_request { 1962sub size_request {
1633 my ($self) = @_; 1963 my ($self) = @_;
1634 1964
1635 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1636 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1637 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1638 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1639 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1640 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1641 1973
1642 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
1714 : ($self->{w} - $size->[0]) * 0.5); 2046 : ($self->{w} - $size->[0]) * 0.5);
1715 2047
1716 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2048 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1717 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2049 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1718 : ($self->{h} - $size->[1]) * 0.5); 2050 : ($self->{h} - $size->[1]) * 0.5);
2051
2052 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1719 }; 2053 };
1720 2054
1721 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2055# unless ($self->{list}) {
1722 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2056# $self->{list} = CFPlus::OpenGL::glGenList;
1723 2057# CFPlus::OpenGL::glNewList $self->{list};
1724 $self->{layout}->render ($self->{ox}, $self->{oy}); 2058# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2059# CFPlus::OpenGL::glEndList;
2060# }
2061#
2062# CFPlus::OpenGL::glCallList $self->{list};
2063
2064 $self->{layout}->draw;
1725} 2065}
2066
2067#sub destroy {
2068# my ($self) = @_;
2069#
2070# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2071#
2072# $self->SUPER::destroy;
2073#}
1726 2074
1727############################################################################# 2075#############################################################################
1728 2076
1729package CFClient::UI::EntryBase; 2077package CFPlus::UI::EntryBase;
1730 2078
1731our @ISA = CFClient::UI::Label::; 2079our @ISA = CFPlus::UI::Label::;
1732 2080
1733use CFClient::OpenGL; 2081use CFPlus::OpenGL;
1734 2082
1735sub new { 2083sub new {
1736 my $class = shift; 2084 my $class = shift;
1737 2085
1738 $class->SUPER::new ( 2086 $class->SUPER::new (
1739 fg => [1, 1, 1], 2087 fg => [1, 1, 1],
1740 bg => [0, 0, 0, 0.2], 2088 bg => [0, 0, 0, 0.2],
2089 outline => [0.6, 0.3, 0.1],
1741 active_bg => [1, 1, 1, 0.5], 2090 active_bg => [0, 0, 1, .2],
1742 active_fg => [0, 0, 0], 2091 active_fg => [1, 1, 1],
2092 active_outline => [1, 1, 0],
1743 can_hover => 1, 2093 can_hover => 1,
1744 can_focus => 1, 2094 can_focus => 1,
1745 valign => 0, 2095 valign => 0,
1746 can_events => 1, 2096 can_events => 1,
2097 ellipsise => 0,
1747 #text => ... 2098 #text => ...
1748 #hidden => "*", 2099 #hidden => "*",
1749 @_ 2100 @_
1750 ) 2101 )
1751} 2102}
1796 my $sym = $ev->{sym}; 2147 my $sym = $ev->{sym};
1797 my $uni = $ev->{unicode}; 2148 my $uni = $ev->{unicode};
1798 2149
1799 my $text = $self->get_text; 2150 my $text = $self->get_text;
1800 2151
2152 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2153
1801 if ($uni == 8) { 2154 if ($uni == 8) {
1802 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2155 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1803 } elsif ($uni == 127) { 2156 } elsif ($uni == 127) {
1804 substr $text, $self->{cursor}, 1, ""; 2157 substr $text, $self->{cursor}, 1, "";
1805 } elsif ($sym == CFClient::SDLK_LEFT) { 2158 } elsif ($sym == CFPlus::SDLK_LEFT) {
1806 --$self->{cursor} if $self->{cursor}; 2159 --$self->{cursor} if $self->{cursor};
1807 } elsif ($sym == CFClient::SDLK_RIGHT) { 2160 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1808 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2161 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1809 } elsif ($sym == CFClient::SDLK_HOME) { 2162 } elsif ($sym == CFPlus::SDLK_HOME) {
2163 # what a hack
2164 $self->{cursor} =
2165 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2166 ? length $1
2167 : 0;
2168 } elsif ($sym == CFPlus::SDLK_END) {
2169 # uh, again
2170 $self->{cursor} =
2171 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2172 ? $self->{cursor} + length $1
2173 : length $self->{text};
2174 } elsif ($uni == 21) { # ctrl-u
2175 $text = "";
1810 $self->{cursor} = 0; 2176 $self->{cursor} = 0;
1811 } elsif ($sym == CFClient::SDLK_END) {
1812 $self->{cursor} = length $text;
1813 } elsif ($uni == 27) { 2177 } elsif ($uni == 27) {
1814 $self->emit ('escape'); 2178 $self->emit ('escape');
1815 } elsif ($uni) { 2179 } elsif ($uni == 0x0d) {
2180 substr $text, $self->{cursor}++, 0, "\012";
2181 } elsif ($uni >= 0x20) {
1816 substr $text, $self->{cursor}++, 0, chr $uni; 2182 substr $text, $self->{cursor}++, 0, chr $uni;
1817 } else { 2183 } else {
1818 return 0; 2184 return 0;
1819 } 2185 }
1820 2186
1821 $self->_set_text ($text); 2187 $self->_set_text ($text);
1822 2188
1823 $self->realloc; 2189 $self->realloc;
2190 $self->update;
1824 2191
1825 1 2192 1
1826} 2193}
1827 2194
1828sub invoke_focus_in { 2195sub invoke_focus_in {
1870 glColor_premultiply @{$self->{bg}}; 2237 glColor_premultiply @{$self->{bg}};
1871 } 2238 }
1872 2239
1873 glEnable GL_BLEND; 2240 glEnable GL_BLEND;
1874 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2241 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1875 glBegin GL_QUADS;
1876 glVertex 0 , 0;
1877 glVertex 0 , $self->{h};
1878 glVertex $self->{w}, $self->{h}; 2242 glRect 0, 0, $self->{w}, $self->{h};
1879 glVertex $self->{w}, 0;
1880 glEnd;
1881 glDisable GL_BLEND; 2243 glDisable GL_BLEND;
1882 2244
1883 $self->SUPER::_draw; 2245 $self->SUPER::_draw;
1884 2246
1885 #TODO: force update every cursor change :( 2247 #TODO: force update every cursor change :(
1887 2249
1888 unless (exists $self->{cur_h}) { 2250 unless (exists $self->{cur_h}) {
1889 my $text = substr $self->{text}, 0, $self->{cursor}; 2251 my $text = substr $self->{text}, 0, $self->{cursor};
1890 utf8::encode $text; 2252 utf8::encode $text;
1891 2253
1892 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2254 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1893 } 2255 }
1894 2256
1895 glColor @{$self->{fg}}; 2257 glColor_premultiply @{$self->{active_fg}};
1896 glBegin GL_LINES; 2258 glBegin GL_LINES;
1897 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2259 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
1898 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2260 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1899 glEnd; 2261 glEnd;
1900 }
1901}
1902 2262
2263 glLineWidth 3;
2264 glColor @{$self->{active_outline}};
2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2266 glLineWidth 1;
2267
2268 } else {
2269 glColor @{$self->{outline}};
2270 glBegin GL_LINE_STRIP;
2271 glVertex .5, $self->{h} * .5;
2272 glVertex .5, $self->{h} - 2.5;
2273 glVertex $self->{w} - .5, $self->{h} - 2.5;
2274 glVertex $self->{w} - .5, $self->{h} * .5;
2275 glEnd;
2276 }
2277}
2278
2279#############################################################################
2280
1903package CFClient::UI::Entry; 2281package CFPlus::UI::Entry;
1904 2282
1905our @ISA = CFClient::UI::EntryBase::; 2283our @ISA = CFPlus::UI::EntryBase::;
1906 2284
1907use CFClient::OpenGL; 2285use CFPlus::OpenGL;
1908 2286
1909sub invoke_key_down { 2287sub invoke_key_down {
1910 my ($self, $ev) = @_; 2288 my ($self, $ev) = @_;
1911 2289
1912 my $sym = $ev->{sym}; 2290 my $sym = $ev->{sym};
1913 2291
1914 if ($sym == 13) { 2292 if ($ev->{uni} == 0x0d || $sym == 13) {
1915 unshift @{$self->{history}}, 2293 unshift @{$self->{history}},
1916 my $txt = $self->get_text; 2294 my $txt = $self->get_text;
1917 2295
1918 $self->{history_pointer} = -1; 2296 $self->{history_pointer} = -1;
1919 $self->{history_saveback} = ''; 2297 $self->{history_saveback} = '';
1920 $self->emit (activate => $txt); 2298 $self->emit (activate => $txt);
1921 $self->update; 2299 $self->update;
1922 2300
1923 } elsif ($sym == CFClient::SDLK_UP) { 2301 } elsif ($sym == CFPlus::SDLK_UP) {
1924 if ($self->{history_pointer} < 0) { 2302 if ($self->{history_pointer} < 0) {
1925 $self->{history_saveback} = $self->get_text; 2303 $self->{history_saveback} = $self->get_text;
1926 } 2304 }
1927 if (@{$self->{history} || []} > 0) { 2305 if (@{$self->{history} || []} > 0) {
1928 $self->{history_pointer}++; 2306 $self->{history_pointer}++;
1930 $self->{history_pointer} = @{$self->{history} || []} - 1; 2308 $self->{history_pointer} = @{$self->{history} || []} - 1;
1931 } 2309 }
1932 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2310 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1933 } 2311 }
1934 2312
1935 } elsif ($sym == CFClient::SDLK_DOWN) { 2313 } elsif ($sym == CFPlus::SDLK_DOWN) {
1936 $self->{history_pointer}--; 2314 $self->{history_pointer}--;
1937 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2315 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1938 2316
1939 if ($self->{history_pointer} >= 0) { 2317 if ($self->{history_pointer} >= 0) {
1940 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2318 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1949 1 2327 1
1950} 2328}
1951 2329
1952############################################################################# 2330#############################################################################
1953 2331
2332package CFPlus::UI::TextEdit;
2333
2334our @ISA = CFPlus::UI::EntryBase::;
2335
2336use CFPlus::OpenGL;
2337
2338sub move_cursor_ver {
2339 my ($self, $dy) = @_;
2340
2341 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2342
2343 $y += $dy;
2344
2345 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2346 $self->{cursor} = $index;
2347 delete $self->{cur_h};
2348 $self->update;
2349 return;
2350 }
2351}
2352
2353sub invoke_key_down {
2354 my ($self, $ev) = @_;
2355
2356 my $sym = $ev->{sym};
2357
2358 if ($sym == CFPlus::SDLK_UP) {
2359 $self->move_cursor_ver (-1);
2360 } elsif ($sym == CFPlus::SDLK_DOWN) {
2361 $self->move_cursor_ver (+1);
2362 } else {
2363 return $self->SUPER::invoke_key_down ($ev)
2364 }
2365
2366 1
2367}
2368
2369#############################################################################
2370
1954package CFClient::UI::Button; 2371package CFPlus::UI::ButtonBin;
1955 2372
1956our @ISA = CFClient::UI::Label::; 2373our @ISA = CFPlus::UI::Bin::;
1957 2374
1958use CFClient::OpenGL; 2375use CFPlus::OpenGL;
1959 2376
1960my @tex = 2377my @tex =
1961 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2378 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1962 qw(b1_button_active.png); 2379 qw(b1_button_inactive.png b1_button_active.png);
1963 2380
1964sub new { 2381sub new {
1965 my $class = shift; 2382 my $class = shift;
1966 2383
1967 $class->SUPER::new ( 2384 $class->SUPER::new (
1968 padding_x => 4,
1969 padding_y => 4,
1970 fg => [1, 1, 1],
1971 active_fg => [0, 0, 1],
1972 can_hover => 1, 2385 can_hover => 1,
1973 align => 0, 2386 align => 0,
1974 valign => 0, 2387 valign => 0,
1975 can_events => 1, 2388 can_events => 1,
1976 @_ 2389 @_
1988} 2401}
1989 2402
1990sub _draw { 2403sub _draw {
1991 my ($self) = @_; 2404 my ($self) = @_;
1992 2405
1993 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
1994
1995 glEnable GL_TEXTURE_2D; 2406 glEnable GL_TEXTURE_2D;
1996 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2407 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1997 glColor 0, 0, 0, 1; 2408 glColor 0, 0, 0, 1;
1998 2409
2410 my $tex = $tex[$GRAB == $self];
1999 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2411 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2000 2412
2001 glDisable GL_TEXTURE_2D; 2413 glDisable GL_TEXTURE_2D;
2002 2414
2003 $self->SUPER::_draw; 2415 $self->SUPER::_draw;
2004} 2416}
2005 2417
2006############################################################################# 2418#############################################################################
2007 2419
2420package CFPlus::UI::Button;
2421
2422our @ISA = CFPlus::UI::Label::;
2423
2424use CFPlus::OpenGL;
2425
2426my @tex =
2427 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2428 qw(b1_button_inactive.png b1_button_active.png);
2429
2430sub new {
2431 my $class = shift;
2432
2433 $class->SUPER::new (
2434 padding_x => 4,
2435 padding_y => 4,
2436 fg => [1.0, 1.0, 1.0],
2437 active_fg => [0.8, 0.8, 0.8],
2438 can_hover => 1,
2439 align => 0,
2440 valign => 0,
2441 can_events => 1,
2442 @_
2443 )
2444}
2445
2446sub invoke_button_up {
2447 my ($self, $ev, $x, $y) = @_;
2448
2449 $self->emit ("activate")
2450 if $x >= 0 && $x < $self->{w}
2451 && $y >= 0 && $y < $self->{h};
2452
2453 1
2454}
2455
2456sub _draw {
2457 my ($self) = @_;
2458
2459 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2460
2461 glEnable GL_TEXTURE_2D;
2462 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2463 glColor 0, 0, 0, 1;
2464
2465 my $tex = $tex[$GRAB == $self];
2466 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2467
2468 glDisable GL_TEXTURE_2D;
2469
2470 $self->SUPER::_draw;
2471}
2472
2473#############################################################################
2474
2475package CFPlus::UI::CheckBox;
2476
2477our @ISA = CFPlus::UI::DrawBG::;
2478
2479my @tex =
2480 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2481 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2482
2483use CFPlus::OpenGL;
2484
2485sub new {
2486 my $class = shift;
2487
2488 $class->SUPER::new (
2489 padding_x => 2,
2490 padding_y => 2,
2491 fg => [1, 1, 1],
2492 active_fg => [1, 1, 0],
2493 bg => [0, 0, 0, 0.2],
2494 active_bg => [1, 1, 1, 0.5],
2495 state => 0,
2496 can_hover => 1,
2497 @_
2498 )
2499}
2500
2501sub size_request {
2502 my ($self) = @_;
2503
2504 (6) x 2
2505}
2506
2507sub toggle {
2508 my ($self) = @_;
2509
2510 $self->{state} = !$self->{state};
2511 $self->emit (changed => $self->{state});
2512 $self->update;
2513}
2514
2515sub invoke_button_down {
2516 my ($self, $ev, $x, $y) = @_;
2517
2518 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2519 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2520 $self->toggle;
2521 } else {
2522 return 0
2523 }
2524
2525 1
2526}
2527
2528sub _draw {
2529 my ($self) = @_;
2530
2531 $self->SUPER::_draw;
2532
2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2534
2535 my ($w, $h) = @$self{qw(w h)};
2536
2537 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2538
2539 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2540
2541 my $tex = $self->{state} ? $tex[1] : $tex[0];
2542
2543 glEnable GL_TEXTURE_2D;
2544 $tex->draw_quad_alpha (0, 0, $s, $s);
2545 glDisable GL_TEXTURE_2D;
2546}
2547
2548#############################################################################
2549
2550package CFPlus::UI::Image;
2551
2552our @ISA = CFPlus::UI::Base::;
2553
2554use CFPlus::OpenGL;
2555
2556our %texture_cache;
2557
2558sub new {
2559 my $class = shift;
2560
2561 my $self = $class->SUPER::new (
2562 can_events => 0,
2563 scale => 1,
2564 @_,
2565 );
2566
2567 $self->{path} || $self->{tex}
2568 or Carp::croak "'path' or 'tex' attributes required";
2569
2570 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2571 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2572
2573 CFPlus::weaken $texture_cache{$self->{path}};
2574
2575 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2576
2577 $self
2578}
2579
2580sub STORABLE_freeze {
2581 my ($self, $cloning) = @_;
2582
2583 $self->{path}
2584 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n";
2585
2586 $self->{path}
2587}
2588
2589sub STORABLE_attach {
2590 my ($self, $cloning, $path) = @_;
2591
2592 $self->new (path => $path)
2593}
2594
2595sub size_request {
2596 my ($self) = @_;
2597
2598 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2599}
2600
2601sub _draw {
2602 my ($self) = @_;
2603
2604 my $tex = $self->{tex};
2605
2606 my ($w, $h) = ($self->{w}, $self->{h});
2607
2608 if ($self->{rot90}) {
2609 glRotate 90, 0, 0, 1;
2610 glTranslate 0, -$self->{w}, 0;
2611
2612 ($w, $h) = ($h, $w);
2613 }
2614
2615 glEnable GL_TEXTURE_2D;
2616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2617
2618 $tex->draw_quad_alpha (0, 0, $w, $h);
2619
2620 glDisable GL_TEXTURE_2D;
2621}
2622
2623#############################################################################
2624
2008package CFClient::UI::ImageButton; 2625package CFPlus::UI::ImageButton;
2009 2626
2010our @ISA = CFClient::UI::Image::; 2627our @ISA = CFPlus::UI::Image::;
2011 2628
2012use CFClient::OpenGL; 2629use CFPlus::OpenGL;
2013 2630
2014my %textures; 2631my %textures;
2015 2632
2016sub new { 2633sub new {
2017 my $class = shift; 2634 my $class = shift;
2027 can_events => 1, 2644 can_events => 1,
2028 @_ 2645 @_
2029 ); 2646 );
2030} 2647}
2031 2648
2649sub invoke_button_down {
2650 my ($self, $ev, $x, $y) = @_;
2651
2652 1
2653}
2654
2032sub invoke_button_up { 2655sub invoke_button_up {
2033 my ($self, $ev, $x, $y) = @_; 2656 my ($self, $ev, $x, $y) = @_;
2034 2657
2035 $self->emit ("activate") 2658 $self->emit ("activate")
2036 if $x >= 0 && $x < $self->{w} 2659 if $x >= 0 && $x < $self->{w}
2039 1 2662 1
2040} 2663}
2041 2664
2042############################################################################# 2665#############################################################################
2043 2666
2044package CFClient::UI::CheckBox;
2045
2046our @ISA = CFClient::UI::DrawBG::;
2047
2048my @tex =
2049 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
2050 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2051
2052use CFClient::OpenGL;
2053
2054sub new {
2055 my $class = shift;
2056
2057 $class->SUPER::new (
2058 padding_x => 2,
2059 padding_y => 2,
2060 fg => [1, 1, 1],
2061 active_fg => [1, 1, 0],
2062 bg => [0, 0, 0, 0.2],
2063 active_bg => [1, 1, 1, 0.5],
2064 state => 0,
2065 can_hover => 1,
2066 @_
2067 )
2068}
2069
2070sub size_request {
2071 my ($self) = @_;
2072
2073 (6) x 2
2074}
2075
2076sub toggle {
2077 my ($self) = @_;
2078
2079 $self->{state} = !$self->{state};
2080 $self->emit (changed => $self->{state});
2081 $self->update;
2082}
2083
2084sub invoke_button_down {
2085 my ($self, $ev, $x, $y) = @_;
2086
2087 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2088 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2089 $self->toggle;
2090 } else {
2091 return 0
2092 }
2093
2094 1
2095}
2096
2097sub _draw {
2098 my ($self) = @_;
2099
2100 $self->SUPER::_draw;
2101
2102 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2103
2104 my ($w, $h) = @$self{qw(w h)};
2105
2106 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2107
2108 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2109
2110 my $tex = $self->{state} ? $tex[1] : $tex[0];
2111
2112 glEnable GL_TEXTURE_2D;
2113 $tex->draw_quad_alpha (0, 0, $s, $s);
2114 glDisable GL_TEXTURE_2D;
2115}
2116
2117#############################################################################
2118
2119package CFClient::UI::Image; 2667package CFPlus::UI::VGauge;
2120 2668
2121our @ISA = CFClient::UI::Base::; 2669our @ISA = CFPlus::UI::Base::;
2122
2123use CFClient::OpenGL;
2124
2125our %texture_cache;
2126
2127sub new {
2128 my $class = shift;
2129
2130 my $self = $class->SUPER::new (
2131 can_events => 0,
2132 @_,
2133 );
2134
2135 $self->{path} || $self->{tex}
2136 or Carp::croak "'path' or 'tex' attributes required";
2137
2138 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2139 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2140
2141 Scalar::Util::weaken $texture_cache{$self->{path}};
2142
2143 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2144
2145 $self
2146}
2147
2148sub size_request {
2149 my ($self) = @_;
2150
2151 ($self->{tex}{w}, $self->{tex}{h})
2152}
2153
2154sub _draw {
2155 my ($self) = @_;
2156
2157 my $tex = $self->{tex};
2158
2159 my ($w, $h) = ($self->{w}, $self->{h});
2160
2161 if ($self->{rot90}) {
2162 glRotate 90, 0, 0, 1;
2163 glTranslate 0, -$self->{w}, 0;
2164
2165 ($w, $h) = ($h, $w);
2166 }
2167
2168 glEnable GL_TEXTURE_2D;
2169 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2170
2171 $tex->draw_quad_alpha (0, 0, $w, $h);
2172
2173 glDisable GL_TEXTURE_2D;
2174}
2175
2176#############################################################################
2177
2178package CFClient::UI::VGauge;
2179
2180our @ISA = CFClient::UI::Base::;
2181 2670
2182use List::Util qw(min max); 2671use List::Util qw(min max);
2183 2672
2184use CFClient::OpenGL; 2673use CFPlus::OpenGL;
2185 2674
2186my %tex = ( 2675my %tex = (
2187 food => [ 2676 food => [
2188 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2677 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2189 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2678 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2190 ], 2679 ],
2191 grace => [ 2680 grace => [
2192 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2681 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2193 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2682 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2194 ], 2683 ],
2195 hp => [ 2684 hp => [
2196 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2685 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2197 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2686 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2198 ], 2687 ],
2199 mana => [ 2688 mana => [
2200 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2689 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2201 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2690 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2202 ], 2691 ],
2203); 2692);
2204 2693
2205# eg. VGauge->new (gauge => 'food'), default gauge: food 2694# eg. VGauge->new (gauge => 'food'), default gauge: food
2308 glDisable GL_TEXTURE_2D; 2797 glDisable GL_TEXTURE_2D;
2309} 2798}
2310 2799
2311############################################################################# 2800#############################################################################
2312 2801
2802package CFPlus::UI::Progress;
2803
2804our @ISA = CFPlus::UI::Label::;
2805
2806use CFPlus::OpenGL;
2807
2808sub new {
2809 my ($class, %arg) = @_;
2810
2811 my $self = $class->SUPER::new (
2812 fg => [1, 1, 1],
2813 bg => [0, 0, 1, 0.2],
2814 bar => [0.7, 0.5, 0.1, 0.8],
2815 outline => [0.4, 0.3, 0],
2816 fontsize => 0.9,
2817 valign => 0,
2818 align => 0,
2819 can_events => 1,
2820 ellipsise => 1,
2821 label => "%d%%",
2822 %arg,
2823 );
2824
2825 $self->set_value ($arg{value} || -1);
2826
2827 $self
2828}
2829
2830sub set_label {
2831 my ($self, $label) = @_;
2832
2833 return if $self->{label} eq $label;
2834 $self->{label} = $label;
2835
2836 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value});
2837}
2838
2839sub set_value {
2840 my ($self, $value) = @_;
2841
2842 if ($self->{value} ne $value) {
2843 $self->{value} = $value;
2844
2845 if ($value < 0) {
2846 $self->set_text ("-");
2847 } else {
2848 $self->set_text (sprintf $self->{label}, $value * 100);
2849 }
2850
2851 $self->update;
2852 }
2853}
2854
2855sub _draw {
2856 my ($self) = @_;
2857
2858 glEnable GL_BLEND;
2859 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2860
2861 if ($self->{value} >= 0) {
2862 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2863
2864 glColor_premultiply @{$self->{bar}};
2865 glRect 2, 2, $s, $self->{h} - 2;
2866 glColor_premultiply @{$self->{bg}};
2867 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2868 }
2869
2870 glColor_premultiply @{$self->{outline}};
2871 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2872
2873 glDisable GL_BLEND;
2874
2875 {
2876 local $self->{bg}; # do not draw background
2877 $self->SUPER::_draw;
2878 }
2879}
2880
2881#############################################################################
2882
2883package CFPlus::UI::ExperienceProgress;
2884
2885our @ISA = CFPlus::UI::Progress::;
2886
2887sub new {
2888 my ($class, %arg) = @_;
2889
2890 my $self = $class->SUPER::new (
2891 tooltip => sub {
2892 my ($self) = @_;
2893
2894 sprintf "level %d\n%s points\n%s next level\n%s to go",
2895 $self->{lvl},
2896 ::formsep ($self->{exp}),
2897 ::formsep ($self->{nxt}),
2898 ::formsep ($self->{nxt} - $self->{exp}),
2899 },
2900 %arg
2901 );
2902
2903 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2904 if $::CONN;
2905
2906 $self
2907}
2908
2909sub DESTROY {
2910 my ($self) = @_;
2911
2912 delete $::CONN->{on_exp_update}{$self+0}
2913 if $::CONN;
2914
2915 $self->SUPER::DESTROY;
2916}
2917
2918sub set_value {
2919 my ($self, $lvl, $exp) = @_;
2920
2921 $self->{lvl} = $lvl;
2922 $self->{exp} = $exp;
2923
2924 my $v = -1;
2925
2926 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2927 my $l0 = $table->[$lvl - 1];
2928 my $l1 = $table->[$lvl];
2929
2930 $self->{nxt} = $l1;
2931
2932 $v = ($exp - $l0) / ($l1 - $l0);
2933 }
2934
2935 $self->SUPER::set_value ($v);
2936}
2937
2938#############################################################################
2939
2313package CFClient::UI::Gauge; 2940package CFPlus::UI::Gauge;
2314 2941
2315our @ISA = CFClient::UI::VBox::; 2942our @ISA = CFPlus::UI::VBox::;
2316 2943
2317sub new { 2944sub new {
2318 my ($class, %arg) = @_; 2945 my ($class, %arg) = @_;
2319 2946
2320 my $self = $class->SUPER::new ( 2947 my $self = $class->SUPER::new (
2322 can_hover => 1, 2949 can_hover => 1,
2323 can_events => 1, 2950 can_events => 1,
2324 %arg, 2951 %arg,
2325 ); 2952 );
2326 2953
2327 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 2954 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999");
2328 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2955 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2329 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); 2956 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999");
2330 2957
2331 $self 2958 $self
2332} 2959}
2333 2960
2334sub set_fontsize { 2961sub set_fontsize {
2355 $self->{value}->set_text ($val); 2982 $self->{value}->set_text ($val);
2356} 2983}
2357 2984
2358############################################################################# 2985#############################################################################
2359 2986
2360package CFClient::UI::Slider; 2987package CFPlus::UI::Slider;
2361 2988
2362use strict; 2989use strict;
2363 2990
2364use CFClient::OpenGL; 2991use CFPlus::OpenGL;
2365 2992
2366our @ISA = CFClient::UI::DrawBG::; 2993our @ISA = CFPlus::UI::DrawBG::;
2367 2994
2368my @tex = 2995my @tex =
2369 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 2996 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ }
2370 qw(s1_slider.png s1_slider_bg.png); 2997 qw(s1_slider.png s1_slider_bg.png);
2371 2998
2372sub new { 2999sub new {
2373 my $class = shift; 3000 my $class = shift;
2374 3001
2442 3069
2443 $self->SUPER::invoke_button_down ($ev, $x, $y); 3070 $self->SUPER::invoke_button_down ($ev, $x, $y);
2444 3071
2445 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3072 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2446 3073
2447 $self->invoke_mouse_motion ($ev, $x, $y) 3074 $self->invoke_mouse_motion ($ev, $x, $y);
3075
3076 1
2448} 3077}
2449 3078
2450sub invoke_mouse_motion { 3079sub invoke_mouse_motion {
2451 my ($self, $ev, $x, $y) = @_; 3080 my ($self, $ev, $x, $y) = @_;
2452 3081
2459 3088
2460 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3089 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2461 } else { 3090 } else {
2462 return 0; 3091 return 0;
2463 } 3092 }
3093
3094 1
3095}
3096
3097sub invoke_mouse_wheel {
3098 my ($self, $ev) = @_;
3099
3100 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3101
3102 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
3103
3104 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2464 3105
2465 1 3106 1
2466} 3107}
2467 3108
2468sub update { 3109sub update {
2519 glDisable GL_TEXTURE_2D; 3160 glDisable GL_TEXTURE_2D;
2520} 3161}
2521 3162
2522############################################################################# 3163#############################################################################
2523 3164
2524package CFClient::UI::ValSlider; 3165package CFPlus::UI::ValSlider;
2525 3166
2526our @ISA = CFClient::UI::HBox::; 3167our @ISA = CFPlus::UI::HBox::;
2527 3168
2528sub new { 3169sub new {
2529 my ($class, %arg) = @_; 3170 my ($class, %arg) = @_;
2530 3171
2531 my $range = delete $arg{range}; 3172 my $range = delete $arg{range};
2532 3173
2533 my $self = $class->SUPER::new ( 3174 my $self = $class->SUPER::new (
2534 slider => (new CFClient::UI::Slider expand => 1, range => $range), 3175 slider => (new CFPlus::UI::Slider expand => 1, range => $range),
2535 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), 3176 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}),
2536 to_value => sub { shift }, 3177 to_value => sub { shift },
2537 from_value => sub { shift }, 3178 from_value => sub { shift },
2538 %arg, 3179 %arg,
2539 ); 3180 );
2540 3181
2560sub set_range { shift->{slider}->set_range (@_) } 3201sub set_range { shift->{slider}->set_range (@_) }
2561sub set_value { shift->{slider}->set_value (@_) } 3202sub set_value { shift->{slider}->set_value (@_) }
2562 3203
2563############################################################################# 3204#############################################################################
2564 3205
2565package CFClient::UI::TextScroller; 3206package CFPlus::UI::TextScroller;
2566 3207
2567our @ISA = CFClient::UI::HBox::; 3208our @ISA = CFPlus::UI::HBox::;
2568 3209
2569use CFClient::OpenGL; 3210use CFPlus::OpenGL;
2570 3211
2571sub new { 3212sub new {
2572 my $class = shift; 3213 my $class = shift;
2573 3214
2574 my $self = $class->SUPER::new ( 3215 my $self = $class->SUPER::new (
2575 fontsize => 1, 3216 fontsize => 1,
2576 can_events => 0, 3217 can_events => 1,
2577 indent => 0, 3218 indent => 0,
2578 #font => default_font 3219 #font => default_font
2579 @_, 3220 @_,
2580 3221
2581 layout => (new CFClient::Layout), 3222 layout => (new CFPlus::Layout),
2582 par => [], 3223 par => [],
3224 max_par => 0,
2583 height => 0, 3225 height => 0,
2584 children => [ 3226 children => [
2585 (new CFClient::UI::Empty expand => 1), 3227 (new CFPlus::UI::Empty expand => 1),
2586 (new CFClient::UI::Slider vertical => 1), 3228 (new CFPlus::UI::Slider vertical => 1),
2587 ], 3229 ],
2588 ); 3230 );
2589 3231
2590 $self->{children}[1]->connect (changed => sub { $self->update }); 3232 $self->{children}[1]->connect (changed => sub { $self->update });
2591 3233
2600} 3242}
2601 3243
2602sub size_request { 3244sub size_request {
2603 my ($self) = @_; 3245 my ($self) = @_;
2604 3246
2605 my ($empty, $slider) = @{ $self->{children} }; 3247 my ($empty, $slider) = $self->visible_children;
2606 3248
2607 local $self->{children} = [$empty, $slider]; 3249 local $self->{children} = [$empty, $slider];
2608 $self->SUPER::size_request 3250 $self->SUPER::size_request
2609} 3251}
2610 3252
2621 3263
2622 $self->reflow; 3264 $self->reflow;
2623 3265
2624 local $self->{children} = [$empty, $slider]; 3266 local $self->{children} = [$empty, $slider];
2625 $self->SUPER::invoke_size_allocate ($w, $h) 3267 $self->SUPER::invoke_size_allocate ($w, $h)
3268}
3269
3270sub invoke_mouse_wheel {
3271 my ($self, $ev) = @_;
3272
3273 return 0 unless $ev->{dy}; # only vertical movements
3274
3275 $self->{children}[1]->emit (mouse_wheel => $ev);
3276
3277 1
2626} 3278}
2627 3279
2628sub get_layout { 3280sub get_layout {
2629 my ($self, $para) = @_; 3281 my ($self, $para) = @_;
2630 3282
2658 3310
2659 # todo: base offset on lines or so, not on pixels 3311 # todo: base offset on lines or so, not on pixels
2660 $self->{children}[1]->set_value ($offset); 3312 $self->{children}[1]->set_value ($offset);
2661} 3313}
2662 3314
3315sub current_paragraph {
3316 my ($self) = @_;
3317
3318 $self->{top_paragraph} - 1
3319}
3320
3321sub scroll_to {
3322 my ($self, $para) = @_;
3323
3324 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
3325
3326 $self->{scroll_to} = $para;
3327 $self->update;
3328}
3329
2663sub clear { 3330sub clear {
2664 my ($self) = @_; 3331 my ($self) = @_;
2665 3332
2666 my (undef, undef, @other) = @{ $self->{children} }; 3333 my (undef, undef, @other) = @{ $self->{children} };
2667 $self->remove ($_) for @other; 3334 $self->remove ($_) for @other;
2670 $self->{height} = 0; 3337 $self->{height} = 0;
2671 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 3338 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2672} 3339}
2673 3340
2674sub add_paragraph { 3341sub add_paragraph {
2675 my ($self, $color, $para, $indent) = @_; 3342 my $self = shift;
2676 3343
2677 my ($text, @w) = ref $para ? @$para : $para; 3344 for my $para (@_) {
2678
2679 $para = { 3345 $para = {
3346 fg => [1, 1, 1, 1],
3347 indent => 0,
3348 markup => "",
3349 widget => [],
3350 ref $para ? %$para : (markup => $para),
2680 w => 1e10, 3351 w => 1e10,
2681 wrapped => 1, 3352 wrapped => 1,
2682 fg => $color,
2683 indent => $indent,
2684 markup => $text,
2685 widget => \@w,
2686 }; 3353 };
2687 3354
2688 $self->add (@w) if @w; 3355 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2689 push @{$self->{par}}, $para; 3356 push @{$self->{par}}, $para;
3357 }
3358
3359 if (my $max = $self->{max_par}) {
3360 shift @{$self->{par}} while @{$self->{par}} > $max;
3361 }
2690 3362
2691 $self->{need_reflow}++; 3363 $self->{need_reflow}++;
2692 $self->update; 3364 $self->update;
2693} 3365}
2694 3366
2695sub scroll_to_bottom { 3367sub scroll_to_bottom {
2696 my ($self) = @_; 3368 my ($self) = @_;
2697 3369
2698 $self->{scroll_to_bottom} = 1; 3370 $self->{scroll_to} = $#{$self->{par}};
2699 $self->update; 3371 $self->update;
2700} 3372}
2701 3373
3374sub force_uptodate {
3375 my ($self) = @_;
3376
3377 if (delete $self->{need_reflow}) {
3378 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3379
3380 my $height = 0;
3381
3382 for my $para (@{$self->{par}}) {
3383 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3384 my $layout = $self->get_layout ($para);
3385 my ($w, $h) = $layout->size;
3386
3387 $para->{w} = $w + $para->{indent};
3388 $para->{h} = $h;
3389 $para->{wrapped} = $layout->has_wrapped;
3390 }
3391
3392 $para->{y} = $height;
3393 $height += $para->{h};
3394 }
3395
3396 $self->{height} = $height;
3397 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3398
3399 delete $self->{texture};
3400 }
3401
3402 if (my $paridx = delete $self->{scroll_to}) {
3403 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3404 }
3405}
3406
2702sub update { 3407sub update {
2703 my ($self) = @_; 3408 my ($self) = @_;
2704 3409
2705 $self->SUPER::update; 3410 $self->SUPER::update;
2706 3411
2707 return unless $self->{h} > 0; 3412 return unless $self->{h} > 0;
2708 3413
2709 delete $self->{texture}; 3414 delete $self->{texture};
2710 3415
2711 $ROOT->on_post_alloc ($self => sub { 3416 $ROOT->on_post_alloc ($self => sub {
3417 $self->force_uptodate;
3418
2712 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3419 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2713 3420
2714 if (delete $self->{need_reflow}) {
2715 my $height = 0;
2716
2717 for my $para (@{$self->{par}}) {
2718 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2719 my $layout = $self->get_layout ($para);
2720 my ($w, $h) = $layout->size;
2721
2722 $para->{w} = $w + $para->{indent};
2723 $para->{h} = $h;
2724 $para->{wrapped} = $layout->has_wrapped;
2725 }
2726
2727 $height += $para->{h};
2728 }
2729
2730 $self->{height} = $height;
2731
2732 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2733
2734 delete $self->{texture};
2735 }
2736
2737 if (delete $self->{scroll_to_bottom}) {
2738 $self->{children}[1]->set_value (1e10);
2739 }
2740
2741 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 3421 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2742 glClearColor 0, 0, 0, 0; 3422 glClearColor 0, 0, 0, 0;
2743 glClear GL_COLOR_BUFFER_BIT; 3423 glClear GL_COLOR_BUFFER_BIT;
2744 3424
3425 package CFPlus::UI::Base;
3426 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3427 (0, 0, $self->{w}, $self->{h});
3428
3429 my $top = int $self->{children}[1]{range}[0];
3430
3431 my $paridx = 0;
3432 my $top_paragraph;
2745 my $top = int $self->{children}[1]{range}[0]; 3433 my $top = int $self->{children}[1]{range}[0];
2746 3434
2747 my $y0 = $top; 3435 my $y0 = $top;
2748 my $y1 = $top + $H; 3436 my $y1 = $top + $H;
2749 3437
2750 my $y = 0;
2751
2752 for my $para (@{$self->{par}}) { 3438 for my $para (@{$self->{par}}) {
2753 my $h = $para->{h}; 3439 my $h = $para->{h};
3440 my $y = $para->{y};
2754 3441
2755 if ($y0 < $y + $h && $y < $y1) { 3442 if ($y0 < $y + $h && $y < $y1) {
2756
2757 my $layout = $self->get_layout ($para); 3443 my $layout = $self->get_layout ($para);
2758 3444
2759 $layout->render ($para->{indent}, $y - $y0); 3445 $layout->render ($para->{indent}, $y - $y0);
3446 $layout->draw;
2760 3447
2761 if (my @w = @{ $para->{widget} }) { 3448 if (my @w = @{ $para->{widget} }) {
2762 my @s = $layout->get_shapes; 3449 my @s = $layout->get_shapes;
2763 3450
2764 for (@w) { 3451 for (@w) {
2770 $_->draw; 3457 $_->draw;
2771 } 3458 }
2772 } 3459 }
2773 } 3460 }
2774 3461
2775 $y += $h; 3462 $paridx++;
3463 $top_paragraph ||= $paridx if $y >= $top;
2776 } 3464 }
3465
3466 $self->{top_paragraph} = $top_paragraph;
2777 }; 3467 };
2778 }); 3468 });
2779} 3469}
2780 3470
2781sub reconfigure { 3471sub reconfigure {
2799 $self->{children}[1]->draw; 3489 $self->{children}[1]->draw;
2800} 3490}
2801 3491
2802############################################################################# 3492#############################################################################
2803 3493
2804package CFClient::UI::Animator; 3494package CFPlus::UI::Animator;
2805 3495
2806use CFClient::OpenGL; 3496use CFPlus::OpenGL;
2807 3497
2808our @ISA = CFClient::UI::Bin::; 3498our @ISA = CFPlus::UI::Bin::;
2809 3499
2810sub moveto { 3500sub moveto {
2811 my ($self, $x, $y) = @_; 3501 my ($self, $x, $y) = @_;
2812 3502
2813 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3503 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2841 glPopMatrix; 3531 glPopMatrix;
2842} 3532}
2843 3533
2844############################################################################# 3534#############################################################################
2845 3535
2846package CFClient::UI::Flopper; 3536package CFPlus::UI::Flopper;
2847 3537
2848our @ISA = CFClient::UI::Button::; 3538our @ISA = CFPlus::UI::Button::;
2849 3539
2850sub new { 3540sub new {
2851 my $class = shift; 3541 my $class = shift;
2852 3542
2853 my $self = $class->SUPER::new ( 3543 my $self = $class->SUPER::new (
2865 $self->{other}->toggle_visibility; 3555 $self->{other}->toggle_visibility;
2866} 3556}
2867 3557
2868############################################################################# 3558#############################################################################
2869 3559
2870package CFClient::UI::Tooltip; 3560package CFPlus::UI::Tooltip;
2871 3561
2872our @ISA = CFClient::UI::Bin::; 3562our @ISA = CFPlus::UI::Bin::;
2873 3563
2874use CFClient::OpenGL; 3564use CFPlus::OpenGL;
2875 3565
2876sub new { 3566sub new {
2877 my $class = shift; 3567 my $class = shift;
2878 3568
2879 $class->SUPER::new ( 3569 $class->SUPER::new (
2883} 3573}
2884 3574
2885sub set_tooltip_from { 3575sub set_tooltip_from {
2886 my ($self, $widget) = @_; 3576 my ($self, $widget) = @_;
2887 3577
2888 my $tooltip = $widget->{tooltip}; 3578 my $tip = $widget->{tooltip};
3579 $tip = $tip->($widget) if "CODE" eq ref $tip;
3580
3581 $tip = CFPlus::Pod::section_label tooltip => $1
3582 if $tip =~ /^#(.*)$/;
2889 3583
2890 if ($ENV{CFPLUS_DEBUG} & 2) { 3584 if ($ENV{CFPLUS_DEBUG} & 2) {
2891 $tooltip .= "\n\n" . (ref $widget) . "\n" 3585 $tip .= "\n\n" . (ref $widget) . "\n"
2892 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3586 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2893 . "req $widget->{req_w} $widget->{req_h}\n" 3587 . "req $widget->{req_w} $widget->{req_h}\n"
2894 . "visible $widget->{visible}"; 3588 . "visible $widget->{visible}";
2895 } 3589 }
2896 3590
2897 $tooltip =~ s/^\n+//; 3591 $tip =~ s/^\n+//;
2898 $tooltip =~ s/\n+$//; 3592 $tip =~ s/\n+$//;
2899 3593
2900 $self->add (new CFClient::UI::Label 3594 $self->add (new CFPlus::UI::Label
2901 markup => $tooltip, 3595 markup => $tip,
2902 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3596 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2903 fontsize => 0.8, 3597 fontsize => 0.8,
2904 fg => [0, 0, 0, 1], 3598 style => 1, # FLAG_INVERSE
2905 ellipsise => 0, 3599 ellipsise => 0,
2906 font => ($widget->{tooltip_font} || $::FONT_PROP), 3600 font => ($widget->{tooltip_font} || $::FONT_PROP),
2907 ); 3601 );
2908} 3602}
2909 3603
2928 3622
2929 $self->{root}->on_post_alloc ("move_$self" => sub { 3623 $self->{root}->on_post_alloc ("move_$self" => sub {
2930 my $widget = $self->{owner} 3624 my $widget = $self->{owner}
2931 or return; 3625 or return;
2932 3626
3627 if ($widget->{visible}) {
2933 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3628 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2934 3629
2935 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3630 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2936 if $x + $self->{w} > $self->{root}{w}; 3631 if $x + $self->{w} > $self->{root}{w};
2937 3632
2938 $self->move_abs ($x, $y); 3633 $self->move_abs ($x, $y);
3634 } else {
3635 $self->hide;
3636 }
2939 }); 3637 });
2940} 3638}
2941 3639
2942sub _draw { 3640sub _draw {
2943 my ($self) = @_; 3641 my ($self) = @_;
2944 3642
2945 glTranslate 0.375, 0.375;
2946
2947 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
2948 3644
2949 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
2950 glBegin GL_QUADS; 3646 glRect 0, 0, $w, $h;
2951 glVertex 0 , 0;
2952 glVertex 0 , $h;
2953 glVertex $w, $h;
2954 glVertex $w, 0;
2955 glEnd;
2956 3647
2957 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
2958 glBegin GL_LINE_LOOP; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
2959 glVertex 0 , 0;
2960 glVertex 0 , $h;
2961 glVertex $w, $h;
2962 glVertex $w, 0;
2963 glEnd;
2964 3650
2965 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
2966 3652
2967 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
2968} 3654}
2969 3655
2970############################################################################# 3656#############################################################################
2971 3657
2972package CFClient::UI::Face; 3658package CFPlus::UI::Face;
2973 3659
2974our @ISA = CFClient::UI::Base::; 3660our @ISA = CFPlus::UI::DrawBG::;
2975 3661
2976use CFClient::OpenGL; 3662use CFPlus::OpenGL;
2977 3663
2978sub new { 3664sub new {
2979 my $class = shift; 3665 my $class = shift;
2980 3666
2981 my $self = $class->SUPER::new ( 3667 my $self = $class->SUPER::new (
3668 size_w => 32,
3669 size_h => 8,
2982 aspect => 1, 3670 aspect => 1,
2983 can_events => 0, 3671 can_events => 0,
2984 @_, 3672 @_,
2985 ); 3673 );
2986 3674
2987 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
2988 Scalar::Util::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
2989 3677
2990 $self->{timer} = Event->timer ( 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
2991 at => $self->{animspeed} * int $::NOW / $self->{animspeed}, 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
2992 hard => 1, 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
2993 interval => $self->{animspeed}, 3681 return unless $::CONN;
2994 cb => sub { 3682
3683 my $w = $widget
3684 or return;
3685
2995 ++$widget->{frame}; 3686 ++$w->{frame};
3687 $w->update_face;
3688
3689 # somehow, $widget can go away
2996 $widget->update; 3690 $w->update;
2997 }, 3691 $w->update_timer;
2998 ); 3692 };
3693
3694 $self->update_face;
3695 $self->update_timer;
2999 } 3696 }
3000 3697
3001 $self 3698 $self
3002} 3699}
3003 3700
3701sub update_timer {
3702 my ($self) = @_;
3703
3704 return unless $self->{timer};
3705
3706 if ($self->{visible}) {
3707 $self->{timer}->set (
3708 $self->{anim_start}
3709 + $self->{animspeed}
3710 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3711 );
3712 $self->{timer}->start;
3713 } else {
3714 $self->{timer}->stop;
3715 }
3716}
3717
3718sub update_face {
3719 my ($self) = @_;
3720
3721 if ($::CONN) {
3722 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3723 if ($anim && @$anim) {
3724 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3725 delete $self->{face_change_cb};
3726
3727 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) {
3728 unless ($tex->{name} || $tex->{loading}) {
3729 $tex->upload (sub { $self->reconfigure });
3730 }
3731 }
3732 }
3733 }
3734 }
3735}
3736
3004sub size_request { 3737sub size_request {
3005 (32, 8) 3738 my ($self) = @_;
3739
3740 if ($::CONN) {
3741 if (my $faceid = $::CONN->{face}[$self->{face}]{id}) {
3742 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3743 if ($tex->{name}) {
3744 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3745 } elsif (!$tex->{loading}) {
3746 $tex->upload (sub { $self->reconfigure });
3747 }
3748 }
3749
3750 $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure });
3751 }
3752 }
3753
3754 ($self->{size_w} || 8, $self->{size_h} || 8)
3006} 3755}
3007 3756
3008sub update { 3757sub update {
3009 my ($self) = @_; 3758 my ($self) = @_;
3010 3759
3011 return unless $self->{visible}; 3760 return unless $self->{visible};
3012 3761
3013 $self->SUPER::update; 3762 $self->SUPER::update;
3014} 3763}
3015 3764
3765sub invoke_visibility_change {
3766 my ($self) = @_;
3767
3768 $self->update_timer;
3769
3770 0
3771}
3772
3016sub _draw { 3773sub _draw {
3017 my ($self) = @_; 3774 my ($self) = @_;
3018 3775
3019 return unless $::CONN; 3776 $self->SUPER::_draw;
3020 3777
3021 my $face; 3778 if (my $tex = $self->{tex}) {
3022
3023 if ($self->{frame}) {
3024 my $anim = $::CONN->{anim}[$self->{anim}];
3025
3026 $face = $anim->[ $self->{frame} % @$anim ]
3027 if $anim && @$anim;
3028 }
3029
3030 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3031
3032 if ($tex) {
3033 glEnable GL_TEXTURE_2D; 3779 glEnable GL_TEXTURE_2D;
3034 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3780 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3035 glColor 0, 0, 0, 1; 3781 glColor 0, 0, 0, 1;
3036 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3782 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3037 glDisable GL_TEXTURE_2D; 3783 glDisable GL_TEXTURE_2D;
3039} 3785}
3040 3786
3041sub destroy { 3787sub destroy {
3042 my ($self) = @_; 3788 my ($self) = @_;
3043 3789
3044 $self->{timer}->cancel 3790 (delete $self->{timer})->cancel
3045 if $self->{timer}; 3791 if $self->{timer};
3046 3792
3047 $self->SUPER::destroy; 3793 $self->SUPER::destroy;
3048} 3794}
3049 3795
3050############################################################################# 3796#############################################################################
3051 3797
3052package CFClient::UI::Buttonbar; 3798package CFPlus::UI::Buttonbar;
3053 3799
3054our @ISA = CFClient::UI::HBox::; 3800our @ISA = CFPlus::UI::HBox::;
3055 3801
3056# TODO: should actualyl wrap buttons and other goodies. 3802# TODO: should actually wrap buttons and other goodies.
3057 3803
3058############################################################################# 3804#############################################################################
3059 3805
3060package CFClient::UI::Menu; 3806package CFPlus::UI::Menu;
3061 3807
3062our @ISA = CFClient::UI::FancyFrame::; 3808our @ISA = CFPlus::UI::Toplevel::;
3063 3809
3064use CFClient::OpenGL; 3810use CFPlus::OpenGL;
3065 3811
3066sub new { 3812sub new {
3067 my $class = shift; 3813 my $class = shift;
3068 3814
3069 my $self = $class->SUPER::new ( 3815 my $self = $class->SUPER::new (
3070 items => [], 3816 items => [],
3071 z => 100, 3817 z => 100,
3072 @_, 3818 @_,
3073 ); 3819 );
3074 3820
3075 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3821 $self->add ($self->{vbox} = new CFPlus::UI::VBox);
3076 3822
3077 for my $item (@{ $self->{items} }) { 3823 for my $item (@{ $self->{items} }) {
3078 my ($widget, $cb, $tooltip) = @$item; 3824 my ($widget, $cb, $tooltip) = @$item;
3079 3825
3080 # handle various types of items, only text for now 3826 # handle various types of items, only text for now
3081 if (!ref $widget) { 3827 if (!ref $widget) {
3082 if ($widget =~ /\t/) { 3828 if ($widget =~ /\t/) {
3083 my ($left, $right) = split /\t/, $widget, 2; 3829 my ($left, $right) = split /\t/, $widget, 2;
3084 3830
3085 $widget = new CFClient::UI::HBox 3831 $widget = new CFPlus::UI::HBox
3086 can_hover => 1, 3832 can_hover => 1,
3087 can_events => 1, 3833 can_events => 1,
3088 tooltip => $tooltip, 3834 tooltip => $tooltip,
3089 children => [ 3835 children => [
3090 (new CFClient::UI::Label markup => $left, expand => 1), 3836 (new CFPlus::UI::Label markup => $left, expand => 1),
3091 (new CFClient::UI::Label markup => $right, align => +1), 3837 (new CFPlus::UI::Label markup => $right, align => +1),
3092 ], 3838 ],
3093 ; 3839 ;
3094 3840
3095 } else { 3841 } else {
3096 $widget = new CFClient::UI::Label 3842 $widget = new CFPlus::UI::Label
3097 can_hover => 1, 3843 can_hover => 1,
3098 can_events => 1, 3844 can_events => 1,
3099 markup => $widget, 3845 markup => $widget,
3100 tooltip => $tooltip; 3846 tooltip => $tooltip;
3101 } 3847 }
3149 1 3895 1
3150} 3896}
3151 3897
3152############################################################################# 3898#############################################################################
3153 3899
3154package CFClient::UI::Multiplexer; 3900package CFPlus::UI::Multiplexer;
3155 3901
3156our @ISA = CFClient::UI::Container::; 3902our @ISA = CFPlus::UI::Container::;
3157 3903
3158sub new { 3904sub new {
3159 my $class = shift; 3905 my $class = shift;
3160 3906
3161 my $self = $class->SUPER::new ( 3907 my $self = $class->SUPER::new (
3222 $self->{current}->draw; 3968 $self->{current}->draw;
3223} 3969}
3224 3970
3225############################################################################# 3971#############################################################################
3226 3972
3227package CFClient::UI::Notebook; 3973package CFPlus::UI::Notebook;
3228 3974
3975use CFPlus::OpenGL;
3976
3229our @ISA = CFClient::UI::VBox::; 3977our @ISA = CFPlus::UI::VBox::;
3230 3978
3231sub new { 3979sub new {
3232 my $class = shift; 3980 my $class = shift;
3233 3981
3234 my $self = $class->SUPER::new ( 3982 my $self = $class->SUPER::new (
3235 buttonbar => (new CFClient::UI::Buttonbar), 3983 buttonbar => (new CFPlus::UI::Buttonbar),
3236 multiplexer => (new CFClient::UI::Multiplexer expand => 1), 3984 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3985 active_outline => [.7, .7, 0.2],
3237 # filter => # will be put between multiplexer and $self 3986 # filter => # will be put between multiplexer and $self
3238 @_, 3987 @_,
3239 ); 3988 );
3240 3989
3241 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3990 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3242 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3991 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3243 3992
3993 {
3994 Scalar::Util::weaken (my $wself = $self);
3995
3996 $self->{multiplexer}->connect (c_add => sub {
3997 my ($mplex, $widgets) = @_;
3998
3999 for my $child (@$widgets) {
4000 Scalar::Util::weaken $child;
4001 $child->{c_tab_} ||= do {
4002 my $tab =
4003 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
4004 ? $child->{c_tab}
4005 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4006
4007 $tab->connect (activate => sub {
4008 $wself->set_current_page ($child);
4009 });
4010
4011 $tab
4012 };
4013
4014 $self->{buttonbar}->add ($child->{c_tab_});
4015 }
4016 });
4017
4018 $self->{multiplexer}->connect (c_remove => sub {
4019 my ($mplex, $widgets) = @_;
4020
4021 for my $child (@$widgets) {
4022 $wself->{buttonbar}->remove ($child->{c_tab_});
4023 }
4024 });
4025 }
4026
3244 $self 4027 $self
3245} 4028}
3246 4029
3247sub add { 4030sub add {
4031 my ($self, @widgets) = @_;
4032
4033 $self->{multiplexer}->add (@widgets)
4034}
4035
4036sub remove {
4037 my ($self, @widgets) = @_;
4038
4039 $self->{multiplexer}->remove (@widgets)
4040}
4041
4042sub pages {
4043 my ($self) = @_;
4044 $self->{multiplexer}->children
4045}
4046
4047sub add_tab {
3248 my ($self, $title, $widget, $tooltip) = @_; 4048 my ($self, $title, $widget, $tooltip) = @_;
3249 4049
3250 Scalar::Util::weaken $self; 4050 $title = [$title, $tooltip] unless ref $title;
4051 $widget->{c_tab} = $title;
3251 4052
3252 $self->{buttonbar}->add (new CFClient::UI::Button
3253 markup => $title,
3254 tooltip => $tooltip,
3255 on_activate => sub { $self->set_current_page ($widget) },
3256 );
3257
3258 $self->{multiplexer}->add ($widget); 4053 $self->add ($widget);
3259} 4054}
3260 4055
3261sub get_current_page { 4056sub get_current_page {
3262 my ($self) = @_; 4057 my ($self) = @_;
3263 4058
3269 4064
3270 $self->{multiplexer}->set_current_page ($page); 4065 $self->{multiplexer}->set_current_page ($page);
3271 $self->emit (page_changed => $self->{multiplexer}{current}); 4066 $self->emit (page_changed => $self->{multiplexer}{current});
3272} 4067}
3273 4068
4069sub _draw {
4070 my ($self) = @_;
4071
4072 $self->SUPER::_draw ();
4073
4074 if (my $cur = $self->{multiplexer}{current}) {
4075 if ($cur = $cur->{c_tab_}) {
4076 glTranslate $self->{buttonbar}{x} + $cur->{x},
4077 $self->{buttonbar}{y} + $cur->{y};
4078 glLineWidth 3;
4079 #glEnable GL_BLEND;
4080 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4081 glColor @{$self->{active_outline}};
4082 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4083 glLineWidth 1;
4084 #glDisable GL_BLEND;
4085 }
4086 }
4087}
4088
3274############################################################################# 4089#############################################################################
3275 4090
3276package CFClient::UI::Combobox; 4091package CFPlus::UI::Selector;
3277 4092
3278use utf8; 4093use utf8;
3279 4094
3280our @ISA = CFClient::UI::Button::; 4095our @ISA = CFPlus::UI::Button::;
3281 4096
3282sub new { 4097sub new {
3283 my $class = shift; 4098 my $class = shift;
3284 4099
3285 my $self = $class->SUPER::new ( 4100 my $self = $class->SUPER::new (
3302 my ($value, $title, $tooltip) = @$_; 4117 my ($value, $title, $tooltip) = @$_;
3303 4118
3304 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4119 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3305 } 4120 }
3306 4121
3307 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 4122 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
3308} 4123}
3309 4124
3310sub _set_value { 4125sub _set_value {
3311 my ($self, $value) = @_; 4126 my ($self, $value) = @_;
3312 4127
3313 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4128 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4129 $item ||= $self->{options}[0]
3314 or return; 4130 or return;
3315 4131
3316 $self->{value} = $item->[0]; 4132 $self->{value} = $item->[0];
3317 $self->set_markup ("$item->[1] ⇓"); 4133 $self->set_markup ("$item->[1] ⇓");
3318 $self->set_tooltip ($item->[2]); 4134# $self->set_tooltip ($item->[2]);
3319} 4135}
3320 4136
3321sub set_value { 4137sub set_value {
3322 my ($self, $value) = @_; 4138 my ($self, $value) = @_;
3323 4139
3325 4141
3326 $self->_set_value ($value); 4142 $self->_set_value ($value);
3327 $self->emit (changed => $value); 4143 $self->emit (changed => $value);
3328} 4144}
3329 4145
4146sub set_options {
4147 my ($self, $options) = @_;
4148
4149 $self->{options} = $options;
4150 $self->_set_value ($self->{value});
4151}
4152
3330############################################################################# 4153#############################################################################
3331 4154
3332package CFClient::UI::Statusbox; 4155package CFPlus::UI::Statusbox;
3333 4156
3334our @ISA = CFClient::UI::VBox::; 4157our @ISA = CFPlus::UI::VBox::;
3335 4158
3336sub new { 4159sub new {
3337 my $class = shift; 4160 my $class = shift;
3338 4161
3339 my $self = $class->SUPER::new ( 4162 my $self = $class->SUPER::new (
3340 fontsize => 0.8, 4163 fontsize => 0.8,
3341 @_, 4164 @_,
3342 ); 4165 );
3343 4166
3344 Scalar::Util::weaken (my $this = $self); 4167 CFPlus::weaken (my $this = $self);
3345 4168
3346 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4169 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
3347 4170
3348 $self 4171 $self
3349} 4172}
3350 4173
3351sub reorder { 4174sub reorder {
3352 my ($self) = @_; 4175 my ($self) = @_;
3353 my $NOW = Time::HiRes::time; 4176 my $NOW = Time::HiRes::time;
3354 4177
3355 # freeze display when hovering over any label 4178 # freeze display when hovering over any label
3356 return if $CFClient::UI::TOOLTIP->{owner} 4179 return if $CFPlus::UI::TOOLTIP->{owner}
3357 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label}, 4180 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label},
3358 values %{ $self->{item} }; 4181 values %{ $self->{item} };
3359 4182
3360 while (my ($k, $v) = each %{ $self->{item} }) { 4183 while (my ($k, $v) = each %{ $self->{item} }) {
3361 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4184 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3362 } 4185 }
4186
4187 $self->{timer}->set (1, 1);
3363 4188
3364 my @widgets; 4189 my @widgets;
3365 4190
3366 my @items = sort { 4191 my @items = sort {
3367 $a->{pri} <=> $b->{pri} 4192 $a->{pri} <=> $b->{pri}
3368 or $b->{id} <=> $a->{id} 4193 or $b->{id} <=> $a->{id}
3369 } values %{ $self->{item} }; 4194 } values %{ $self->{item} };
3370
3371 $self->{timer}->interval (1);
3372 4195
3373 my $count = 10 + 1; 4196 my $count = 10 + 1;
3374 for my $item (@items) { 4197 for my $item (@items) {
3375 last unless --$count; 4198 last unless --$count;
3376 4199
3383 for ($short) { 4206 for ($short) {
3384 s/^\s+//; 4207 s/^\s+//;
3385 s/\s+/ /g; 4208 s/\s+/ /g;
3386 } 4209 }
3387 4210
3388 new CFClient::UI::Label 4211 new CFPlus::UI::Label
3389 markup => $short, 4212 markup => $short,
3390 tooltip => $item->{tooltip}, 4213 tooltip => $item->{tooltip},
3391 tooltip_font => $::FONT_PROP, 4214 tooltip_font => $::FONT_PROP,
3392 tooltip_width => 0.67, 4215 tooltip_width => 0.67,
3393 fontsize => $item->{fontsize} || $self->{fontsize}, 4216 fontsize => $item->{fontsize} || $self->{fontsize},
3400 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4223 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3401 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4224 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3402 $label->update; 4225 $label->update;
3403 $label->set_max_size (undef, $label->{req_h} * $diff) 4226 $label->set_max_size (undef, $label->{req_h} * $diff)
3404 if $diff < 1; 4227 if $diff < 1;
3405 $self->{timer}->interval (1/30); 4228 $self->{timer}->set (1/30, 1/30);
3406 } else { 4229 } else {
3407 $label->{fg}[3] = $item->{fg}[3] || 1; 4230 $label->{fg}[3] = $item->{fg}[3] || 1;
3408 } 4231 }
3409 4232
3410 push @widgets, $label; 4233 push @widgets, $label;
3472 $self->SUPER::destroy; 4295 $self->SUPER::destroy;
3473} 4296}
3474 4297
3475############################################################################# 4298#############################################################################
3476 4299
3477package CFClient::UI::Inventory;
3478
3479our @ISA = CFClient::UI::Table::;
3480
3481sub new {
3482 my $class = shift;
3483
3484 my $self = $class->SUPER::new (
3485 col_expand => [0, 1, 0],
3486 @_,
3487 );
3488
3489 $self
3490}
3491
3492sub set_items {
3493 my ($self, $items) = @_;
3494
3495 $self->clear;
3496 return unless $items;
3497
3498 my @items = sort {
3499 ($a->{type} <=> $b->{type})
3500 or ($a->{name} cmp $b->{name})
3501 } values %$items;
3502
3503 $self->{real_items} = \@items;
3504
3505 my $row = 0;
3506 for my $item (@items) {
3507 CFClient::Item::update_widgets $item;
3508
3509 $self->add (0, $row, $item->{face_widget});
3510 $self->add (1, $row, $item->{desc_widget});
3511 $self->add (2, $row, $item->{weight_widget});
3512
3513 $row++;
3514 }
3515}
3516
3517#############################################################################
3518
3519package CFClient::UI::SpellList;
3520
3521our @ISA = CFClient::UI::Table::;
3522
3523sub new {
3524 my $class = shift;
3525
3526 my $self = $class->SUPER::new (
3527 binding => [],
3528 commands => [],
3529 @_,
3530 )
3531}
3532
3533my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3534
3535my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3536 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3537my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3538 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3539my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3540 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3541my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3542 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3543my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3544 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3545
3546sub rebuild_spell_list {
3547 my ($self) = @_;
3548
3549 $CFClient::UI::ROOT->on_refresh ($self => sub {
3550 $self->clear;
3551
3552 return unless $::CONN;
3553
3554 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3555 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3556 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3557 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3558 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3559
3560 my $row = 0;
3561
3562 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3563 my $spell = $self->{spell}{$_};
3564
3565 $row++;
3566
3567 my $spell_cb = sub {
3568 my ($widget, $ev) = @_;
3569
3570 if ($ev->{button} == 1) {
3571 $::CONN->user_send ("cast $spell->{name}");
3572 } elsif ($ev->{button} == 2) {
3573 $::CONN->user_send ("invoke $spell->{name}");
3574 } elsif ($ev->{button} == 3) {
3575 (new CFClient::UI::Menu
3576 items => [
3577 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3578 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3579 ],
3580 )->popup ($ev);
3581 } else {
3582 return 0;
3583 }
3584
3585 1
3586 };
3587
3588 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3589
3590 #TODO: add path info to tooltip
3591 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3592
3593 $self->add (0, $row, new CFClient::UI::Face
3594 face => $spell->{face},
3595 can_hover => 1,
3596 can_events => 1,
3597 tooltip => $tooltip,
3598 on_button_down => $spell_cb,
3599 );
3600
3601 $self->add (1, $row, new CFClient::UI::Label
3602 expand => 1,
3603 text => $spell->{name},
3604 can_hover => 1,
3605 can_events => 1,
3606 tooltip => $tooltip,
3607 on_button_down => $spell_cb,
3608 );
3609
3610 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3611 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3612 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3613 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3614 }
3615 });
3616}
3617
3618sub add_spell {
3619 my ($self, $spell) = @_;
3620
3621 $self->{spell}->{$spell->{name}} = $spell;
3622 $self->rebuild_spell_list;
3623}
3624
3625sub remove_spell {
3626 my ($self, $spell) = @_;
3627
3628 delete $self->{spell}->{$spell->{name}};
3629 $self->rebuild_spell_list;
3630}
3631
3632sub clear_spells {
3633 my ($self) = @_;
3634
3635 $self->{spell} = {};
3636 $self->rebuild_spell_list;
3637}
3638
3639#############################################################################
3640
3641package CFClient::UI::Root; 4300package CFPlus::UI::Root;
3642 4301
3643our @ISA = CFClient::UI::Container::; 4302our @ISA = CFPlus::UI::Container::;
3644 4303
3645use List::Util qw(min max); 4304use List::Util qw(min max);
3646 4305
3647use CFClient::OpenGL; 4306use CFPlus::OpenGL;
3648 4307
3649sub new { 4308sub new {
3650 my $class = shift; 4309 my $class = shift;
3651 4310
3652 my $self = $class->SUPER::new ( 4311 my $self = $class->SUPER::new (
3653 visible => 1, 4312 visible => 1,
3654 @_, 4313 @_,
3655 ); 4314 );
3656 4315
3657 Scalar::Util::weaken ($self->{root} = $self); 4316 CFPlus::weaken ($self->{root} = $self);
3658 4317
3659 $self 4318 $self
3660} 4319}
3661 4320
3662sub size_request { 4321sub size_request {
3710} 4369}
3711 4370
3712sub update { 4371sub update {
3713 my ($self) = @_; 4372 my ($self) = @_;
3714 4373
3715 $::WANT_REFRESH++; 4374 $::WANT_REFRESH->start;
3716} 4375}
3717 4376
3718sub add { 4377sub add {
3719 my ($self, @children) = @_; 4378 my ($self, @children) = @_;
3720 4379
3757 while ($self->{refresh_hook}) { 4416 while ($self->{refresh_hook}) {
3758 $_->() 4417 $_->()
3759 for values %{delete $self->{refresh_hook}}; 4418 for values %{delete $self->{refresh_hook}};
3760 } 4419 }
3761 4420
3762 if ($self->{realloc}) { 4421 while ($self->{realloc}) {
3763 my %queue; 4422 my %queue;
3764 my @queue; 4423 my @queue;
3765 my $widget; 4424 my $widget;
3766 4425
3767 outer: 4426 outer:
3814 } 4473 }
3815 } 4474 }
3816 4475
3817 delete $self->{realloc}{$widget+0}; 4476 delete $self->{realloc}{$widget+0};
3818 } 4477 }
3819 }
3820 4478
3821 while (my $size_alloc = delete $self->{size_alloc}) { 4479 while (my $size_alloc = delete $self->{size_alloc}) {
3822 my @queue = sort { $b->{visible} <=> $a->{visible} } 4480 my @queue = sort { $a->{visible} <=> $b->{visible} }
3823 values %$size_alloc; 4481 values %$size_alloc;
3824 4482
3825 while () { 4483 while () {
3826 my $widget = pop @queue || last; 4484 my $widget = pop @queue || last;
3827 4485
3828 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4486 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3829 4487
3830 $w = 0 if $w < 0;
3831 $h = 0 if $h < 0;
3832
3833 $w = max $widget->{min_w}, $w; 4488 $w = max $widget->{min_w}, $w;
3834 $h = max $widget->{min_h}, $h; 4489 $h = max $widget->{min_h}, $h;
3835 4490
4491# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4492# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4493
3836 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4494 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3837 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4495 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3838 4496
3839 $w = int $w + 0.5; 4497 $w = int $w + 0.5;
3840 $h = int $h + 0.5; 4498 $h = int $h + 0.5;
3841 4499
3842 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4500 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3843 $widget->{old_w} = $widget->{w}; 4501 $widget->{old_w} = $widget->{w};
3844 $widget->{old_h} = $widget->{h}; 4502 $widget->{old_h} = $widget->{h};
3845 4503
3846 $widget->{w} = $w; 4504 $widget->{w} = $w;
3847 $widget->{h} = $h; 4505 $widget->{h} = $h;
3848 4506
3849 $widget->emit (size_allocate => $w, $h); 4507 $widget->emit (size_allocate => $w, $h);
4508 }
3850 } 4509 }
3851 } 4510 }
3852 } 4511 }
3853 4512
3854 while ($self->{post_alloc_hook}) { 4513 while ($self->{post_alloc_hook}) {
3855 $_->() 4514 $_->()
3856 for values %{delete $self->{post_alloc_hook}}; 4515 for values %{delete $self->{post_alloc_hook}};
3857 } 4516 }
3858
3859 4517
3860 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4518 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3861 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4519 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3862 glClear GL_COLOR_BUFFER_BIT; 4520 glClear GL_COLOR_BUFFER_BIT;
3863 4521
3866 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4524 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3867 glMatrixMode GL_MODELVIEW; 4525 glMatrixMode GL_MODELVIEW;
3868 glLoadIdentity; 4526 glLoadIdentity;
3869 4527
3870 { 4528 {
3871 package CFClient::UI::Base; 4529 package CFPlus::UI::Base;
3872 4530
3873 ($draw_x, $draw_y, $draw_w, $draw_h) = 4531 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3874 (0, 0, $self->{w}, $self->{h}); 4532 (0, 0, $self->{w}, $self->{h});
3875 }
3876 4533
3877 $self->_draw; 4534 $self->_draw;
4535 }
3878} 4536}
3879 4537
3880############################################################################# 4538#############################################################################
3881 4539
3882package CFClient::UI; 4540package CFPlus::UI;
3883 4541
3884$ROOT = new CFClient::UI::Root; 4542$ROOT = new CFPlus::UI::Root;
3885$TOOLTIP = new CFClient::UI::Tooltip z => 900; 4543$TOOLTIP = new CFPlus::UI::Tooltip z => 900;
3886 4544
38871 45451
3888 4546

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines