ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.190
Committed: Mon May 8 22:17:38 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.189: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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