ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.220
Committed: Mon May 22 03:28:55 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.219: +40 -28 lines
Log Message:
use glDrawPixels to draw Textview, implement indenting and render individual lines. force full refresh after expose. add lots of tooltips

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