ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.231
Committed: Thu May 25 00:26:19 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.230: +53 -37 lines
Log Message:
improve focus model, improve completer behaviour

File Contents

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