ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.213
Committed: Wed May 17 15:18:57 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.212: +30 -13 lines
Log Message:
better text layout, minor fixes

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