ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.43 by root, Sun Apr 9 22:12:11 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6use List::Util ();
6 7
7use SDL::OpenGL; 8use CFClient;
8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $FOCUS; # the widget with current focus 12our $ROOT;
13our $TOOLTIP;
14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (length $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_markup ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
13 41
14# class methods for events 42# class methods for events
15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 43sub feed_sdl_key_down_event {
16sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 44 $FOCUS->key_down ($_[0]) if $FOCUS;
45}
46
47sub feed_sdl_key_up_event {
48 $FOCUS->key_up ($_[0]) if $FOCUS;
49}
50
17sub feed_sdl_button_down_event { } 51sub feed_sdl_button_down_event {
52 my ($ev) = @_;
53 my ($x, $y) = ($ev->{x}, $ev->{y});
54
55 if (!$BUTTON_STATE) {
56 my $widget = $ROOT->find_widget ($x, $y);
57
58 $GRAB = $widget;
59 $GRAB->update if $GRAB;
60
61 check_tooltip;
62 }
63
64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
65
66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
67}
68
18sub feed_sdl_button_up_event { } 69sub feed_sdl_button_up_event {
70 my ($ev) = @_;
71 my ($x, $y) = ($ev->{x}, $ev->{y});
72
73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
74
75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
76
77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
78
79 if (!$BUTTON_STATE) {
80 my $grab = $GRAB; undef $GRAB;
81 $grab->update if $grab;
82 $GRAB->update if $GRAB;
83
84 check_tooltip;
85 }
86}
87
88sub feed_sdl_motion_event {
89 my ($ev) = @_;
90 my ($x, $y) = ($ev->{x}, $ev->{y});
91
92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
93
94 if ($widget != $HOVER) {
95 my $hover = $HOVER; $HOVER = $widget;
96
97 $hover->update if $hover && $hover->{can_hover};
98 $HOVER->update if $HOVER && $HOVER->{can_hover};
99
100 check_tooltip;
101 }
102
103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
104}
105
106# convert position array to integers
107sub harmonize {
108 my ($vals) = @_;
109
110 my $rem = 0;
111
112 for (@$vals) {
113 my $i = int $_ + $rem;
114 $rem += $_ - $i;
115 $_ = $i;
116 }
117}
118
119#############################################################################
120
121package CFClient::UI::Base;
122
123use strict;
124
125use CFClient::OpenGL;
19 126
20sub new { 127sub new {
21 my $class = shift; 128 my $class = shift;
22 129
23 bless { @_ }, $class 130 my $self = bless {
131 x => 0,
132 y => 0,
133 z => 0,
134 can_events => 1,
135 @_
136 }, $class;
137
138 for (keys %$self) {
139 if (/^connect_(.*)$/) {
140 $self->connect ($1 => delete $self->{$_});
141 }
142 }
143
144 $self
145}
146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
154sub show {
155 my ($self) = @_;
156
157 return if $self->{parent};
158
159 $CFClient::UI::ROOT->add ($self);
160}
161
162sub hide {
163 my ($self) = @_;
164
165 return unless $self->{parent};
166
167 $self->{parent}->remove ($self);
24} 168}
25 169
26sub move { 170sub move {
27 my ($self, $x, $y, $z) = @_; 171 my ($self, $x, $y, $z) = @_;
172
28 $self->{x} = $x; 173 $self->{x} = int $x;
29 $self->{y} = $y; 174 $self->{y} = int $y;
30 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
31}
32 176
33sub needs_redraw { 177 $self->update;
34 0 178}
179
180sub set_size {
181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
35} 187}
36 188
37sub size_request { 189sub size_request {
38 require Carp; 190 require Carp;
39 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
192}
193
194sub configure {
195 my ($self, $x, $y, $w, $h) = @_;
196
197 if ($self->{aspect}) {
198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
200
201 # use alignment to adjust x, y
202
203 $x += int +($w - $w2) * 0.5;
204 $y += int +($h - $h2) * 0.5;
205
206 ($w, $h) = ($w2, $h2);
207 }
208
209 if ($self->{x} != $x || $self->{y} != $y) {
210 $self->{x} = $x;
211 $self->{y} = $y;
212 $self->update;
213 }
214
215 if ($self->{w} != $w || $self->{h} != $h) {
216 $self->{w} = $w;
217 $self->{h} = $h;
218
219 $self->size_allocate ($w, $h);
220 $self->update;
221 }
40} 222}
41 223
42sub size_allocate { 224sub size_allocate {
225 # nothing to be done
226}
227
228sub set_max_size {
43 my ($self, $w, $h) = @_; 229 my ($self, $w, $h) = @_;
44 230
45 $self->{w} = $w; 231 delete $self->{max_w}; $self->{max_w} = $w if $w;
46 $self->{h} = $h; 232 delete $self->{max_h}; $self->{max_h} = $h if $h;
233}
234
235# return top left coordinates
236sub _topleft {
237 my ($self, $x, $y) = @_;
238
239 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
240}
241
242# translate global coordinates to local coordinate system
243sub coord2local {
244 my ($self, $x, $y) = @_;
245
246 my ($X, $Y) = $self->_topleft;
247 ($x - $X, $y - $Y)
248}
249
250# translate local coordinates to global coordinate system
251sub coord2global {
252 my ($self, $x, $y) = @_;
253
254 my ($X, $Y) = $self->_topleft;
255 ($x + $X, $y + $Y)
47} 256}
48 257
49sub focus_in { 258sub focus_in {
50 my ($widget) = @_; 259 my ($self) = @_;
51 $FOCUS = $widget; 260
261 return if $FOCUS == $self;
262 return unless $self->{can_focus};
263
264 my $focus = $FOCUS; $FOCUS = $self;
265
266 $self->emit (focus_in => $focus);
267
268 $focus->update if $focus;
269 $FOCUS->update;
52} 270}
53 271
54sub focus_out { 272sub focus_out {
55 my ($widget) = @_; 273 my ($self) = @_;
56}
57 274
275 return unless $FOCUS == $self;
276
277 my $focus = $FOCUS; undef $FOCUS;
278
279 $self->emit (focus_out => $focus);
280
281 $focus->update if $focus; #?
282}
283
284sub mouse_motion { }
285sub button_up { }
58sub key_down { 286sub key_down { }
59 my ($widget, $sdlev) = @_;
60}
61
62sub key_up { 287sub key_up { }
63 my ($widget, $sdlev) = @_;
64}
65 288
66sub button_down { 289sub button_down {
67 my ($widget, $sdlev) = @_; 290 my ($self, $ev, $x, $y) = @_;
68}
69 291
70sub button_up { 292 $self->focus_in;
71 my ($widget, $sdlev) = @_;
72} 293}
73 294
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 295sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 296sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 297sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 298sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 299sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 300
80sub draw { 301sub draw {
81 my ($self) = @_; 302 my ($self) = @_;
303
304 return unless $self->{h} && $self->{w};
82 305
83 glPushMatrix; 306 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 307 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 308 $self->_draw;
86 glPopMatrix; 309 glPopMatrix;
310
311 if ($self == $HOVER && $self->{can_hover}) {
312 my ($x, $y) = @$self{qw(x y)};
313
314 glColor 1, 0.8, 0.5, 0.2;
315 glEnable GL_BLEND;
316 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
317 glBegin GL_QUADS;
318 glVertex $x , $y;
319 glVertex $x + $self->{w}, $y;
320 glVertex $x + $self->{w}, $y + $self->{h};
321 glVertex $x , $y + $self->{h};
322 glEnd;
323 glDisable GL_BLEND;
324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
87} 339}
88 340
89sub _draw { 341sub _draw {
90 my ($self) = @_; 342 my ($self) = @_;
91 343
92 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
93}
94
95sub bbox {
96 my ($self) = @_;
97 my ($w, $h) = $self->size_request;
98 (
99 $self->{x},
100 $self->{y},
101 $self->{x} = $w,
102 $self->{y} = $h
103 )
104} 345}
105 346
106sub find_widget { 347sub find_widget {
107 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
108 351
109 return $self 352 return $self
110 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
111 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
112 355
113 () 356 ()
114} 357}
115 358
116sub del_parent { $_[0]->{parent} = undef }
117
118sub set_parent { 359sub set_parent {
119 my ($self, $par) = @_; 360 my ($self, $parent) = @_;
120 361
121 $self->{parent} = $par;
122 Scalar::Util::weaken $self->{parent}; 362 Scalar::Util::weaken ($self->{parent} = $parent);
123} 363}
124 364
125sub get_parent { 365sub check_size {
366 my ($self) = @_;
367
126 $_[0]->{parent} 368 $self->{parent}
369 or return 1;
370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
376 $self->{req_w} = $w;
377 $self->{req_h} = $h;
378
379 $self->{parent}->check_size
380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
388 }
127} 389}
128 390
129sub update { 391sub update {
130 my ($self) = @_; 392 my ($self) = @_;
131 393
132 $self->{parent}->update 394 $self->{parent}->update
133 if $self->{parent}; 395 if $self->{parent};
134} 396}
135 397
398sub connect {
399 my ($self, $signal, $cb) = @_;
400
401 push @{ $self->{signal_cb}{$signal} }, $cb;
402}
403
404sub emit {
405 my ($self, $signal, @args) = @_;
406
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
408 $cb->($self, @args);
409 }
410}
411
136sub DESTROY { 412sub DESTROY {
137 my ($self) = @_; 413 my ($self) = @_;
138 414
139 #$self->deactivate; 415 #$self->deactivate;
140} 416}
141 417
142############################################################################# 418#############################################################################
143 419
144package Crossfire::Client::Widget::Container; 420package CFClient::UI::DrawBG;
145 421
146our @ISA = Crossfire::Client::Widget::; 422our @ISA = CFClient::UI::Base::;
423
424use strict;
425use CFClient::OpenGL;
147 426
148sub new { 427sub new {
428 my $class = shift;
429
430 # range [value, low, high, page]
431
432 $class->SUPER::new (
433 bg => [0, 0, 0, 0.2],
434 active_bg => [1, 1, 1, 0.5],
435 @_
436 )
437}
438
439sub _draw {
440 my ($self) = @_;
441
442 my ($w, $h) = @$self{qw(w h)};
443
444 glEnable GL_BLEND;
445 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
446 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
447
448 glBegin GL_QUADS;
449 glVertex 0 , 0;
450 glVertex 0 , $h;
451 glVertex $w, $h;
452 glVertex $w, 0;
453 glEnd;
454
455 glDisable GL_BLEND;
456}
457
458#############################################################################
459
460package CFClient::UI::Empty;
461
462our @ISA = CFClient::UI::Base::;
463
464sub new {
149 my ($class, @widgets) = @_; 465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
150 468
469sub size_request {
470 (0, 0)
471}
472
473sub draw { }
474
475#############################################################################
476
477package CFClient::UI::Container;
478
479our @ISA = CFClient::UI::Base::;
480
481sub new {
482 my ($class, %arg) = @_;
483
484 my $children = delete $arg{children} || [];
485
151 my $self = $class->SUPER::new (children => []); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
152 $self->add ($_) for @widgets; 487 $self->add ($_) for @$children;
153 488
154 $self 489 $self
155} 490}
156 491
157sub add { 492sub add {
158 my ($self, $chld, $expand) = @_; 493 my ($self, $child) = @_;
159 494
160 $chld->{expand} = $expand;
161 $chld->set_parent ($self); 495 $child->set_parent ($self);
162 496
497 use sort 'stable';
498
163 @{$self->{children}} = 499 $self->{children} = [
164 sort { $a->{z} <=> $b->{z} } 500 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 501 @{$self->{children}}, $child
502 ];
166 503
167 $self->size_allocate ($self->{w}, $self->{h}) 504 $child->check_size;
168 if $self->{w}; #TODO: check for "realised state" 505}
506
507sub remove {
508 my ($self, $child) = @_;
509
510 delete $child->{parent};
511
512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
513
514 $self->check_size;
515 $self->update;
516}
517
518sub clear {
519 my ($self) = @_;
520
521 delete $_->{parent}
522 for @{ delete $self->{children} };
523
524 $self->{children} = [];
525}
526
527sub find_widget {
528 my ($self, $x, $y) = @_;
529
530 $x -= $self->{x};
531 $y -= $self->{y};
532
533 my $res;
534
535 for (reverse @{ $self->{children} }) {
536 $res = $_->find_widget ($x, $y)
537 and return $res;
538 }
539
540 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
541}
542
543sub _draw {
544 my ($self) = @_;
545
546 $_->draw for @{$self->{children}};
547}
548
549#############################################################################
550
551package CFClient::UI::Bin;
552
553our @ISA = CFClient::UI::Container::;
554
555sub new {
556 my ($class, %arg) = @_;
557
558 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
559
560 $class->SUPER::new (children => [$child], %arg)
561}
562
563sub add {
564 my ($self, $child) = @_;
565
566 $self->{children} = [];
567
568 $self->SUPER::add ($child);
169} 569}
170 570
171sub remove { 571sub remove {
172 my ($self, $widget) = @_; 572 my ($self, $widget) = @_;
173 573
174 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 574 $self->SUPER::remove ($widget);
175 575
176 $self->size_allocate ($self->{w}, $self->{h}); 576 $self->{children} = [new CFClient::UI::Empty]
577 unless @{$self->{children}};
578}
579
580sub child { $_[0]->{children}[0] }
581
582sub size_request {
583 $_[0]{children}[0]->size_request
584}
585
586sub size_allocate {
587 my ($self, $w, $h) = @_;
588
589 $self->{children}[0]->configure (0, 0, $w, $h);
590}
591
592#############################################################################
593
594package CFClient::UI::Window;
595
596our @ISA = CFClient::UI::Bin::;
597
598use CFClient::OpenGL;
599
600sub new {
601 my ($class, %arg) = @_;
602
603 my $self = $class->SUPER::new (%arg);
604}
605
606sub update {
607 my ($self) = @_;
608
609 # we want to do this delayed...
610 $self->render_chld;
611 $self->SUPER::update;
612}
613
614sub render_chld {
615 my ($self) = @_;
616
617 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
618 glClearColor 0, 0, 0, 1;
619 glClear GL_COLOR_BUFFER_BIT;
620 $self->child->draw;
621 };
622}
623
624sub size_allocate {
625 my ($self, $w, $h) = @_;
626
627 $self->child->configure (0, 0, $w, $h);
628
629 $self->render_chld;
630}
631
632sub _draw {
633 my ($self) = @_;
634
635 my ($w, $h) = ($self->w, $self->h);
636
637 my $tex = $self->{texture}
638 or return;
639
640 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
644
645 $tex->draw_quad (0, 0, $w, $h);
646
647 glDisable GL_BLEND;
648 glDisable GL_TEXTURE_2D;
649}
650
651#############################################################################
652
653package CFClient::UI::ViewPort;
654
655our @ISA = CFClient::UI::Window::;
656
657sub new { die }
658
659sub size_request {
660 my ($self) = @_;
661
662 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
664
665 @$self{qw(child_w child_h)}
666}
667
668sub _draw {
669 my ($self) = @_;
670
671 $self->{children}[1]->draw;
672}
673
674
675#############################################################################
676
677package CFClient::UI::Frame;
678
679our @ISA = CFClient::UI::Bin::;
680
681use CFClient::OpenGL;
682
683sub new {
684 my $class = shift;
685
686 my $self = $class->SUPER::new (
687 bg => [1, 1, 1, 1],
688 border_bg => [1, 1, 1, 1],
689 border => 0.8,
690 @_
691 );
692
693 $self
694}
695
696sub _draw {
697 my ($self) = @_;
698
699 my ($w, $h) = ($self->{w}, $self->{h});
700
701 glEnable GL_BLEND;
702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
703 glEnable GL_TEXTURE_2D;
704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
705
706# glBegin GL_QUADS;
707# glColor 0, 0, 0, 0;
708# glVertex 0 , 0;
709# glVertex 0 , $h;
710# glVertex $w, $h;
711# glVertex $w, 0;
712# glEnd;
713
714
715 $self->child->draw;
716 glDisable GL_BLEND;
717 glDisable GL_TEXTURE_2D;
718}
719
720#############################################################################
721
722package CFClient::UI::FancyFrame;
723
724our @ISA = CFClient::UI::Bin::;
725
726use CFClient::OpenGL;
727
728my @tex =
729 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
730 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
731
732sub new {
733 my $class = shift;
734
735 # TODO: user_x, user_y, overwrite moveto?
736
737 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1],
740 border => 0.8,
741 can_events => 1,
742 @_
743 );
744
745 $self->{title} &&= new CFClient::UI::Label
746 align => 0,
747 valign => 1,
748 text => $self->{title},
749 fontsize => 1;
750
751 $self
752}
753
754sub border {
755 int $_[0]{border} * $::FONTSIZE
756}
757
758sub size_request {
759 my ($self) = @_;
760
761 my ($w, $h) = $self->SUPER::size_request;
762
763 (
764 $w + $self->border * 2,
765 $h + $self->border * 2,
766 )
767}
768
769sub size_allocate {
770 my ($self, $w, $h) = @_;
771
772 $h -= List::Util::max 0, $self->border * 2;
773 $w -= List::Util::max 0, $self->border * 2;
774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
778 $self->child->configure ($self->border, $self->border, $w, $h);
779}
780
781sub button_down {
782 my ($self, $ev, $x, $y) = @_;
783
784 my $border = $self->border;
785
786 if ($x < $self->{w} && $x >= $self->{w} - $border
787 && $y < $self->{h} && $y >= $self->{h} - $border) {
788
789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
790 my ($bw, $bh) = ($self->{w}, $self->{h});
791
792 $self->{motion} = sub {
793 my ($ev, $x, $y) = @_;
794
795 ($x, $y) = ($ev->{x}, $ev->{y});
796
797 $self->{user_w} = $bw + $x - $ox;
798 $self->{user_h} = $bh + $y - $oy;
799 $self->check_size;
800 };
801
802 } elsif ($x >= 0 && $x < $self->{w}
803 && $y >= 0 && $y < $border) {
804
805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
806 my ($bx, $by) = ($self->{x}, $self->{y});
807
808 $self->{motion} = sub {
809 my ($ev, $x, $y) = @_;
810
811 ($x, $y) = ($ev->{x}, $ev->{y});
812
813 $self->move ($bx + $x - $ox, $by + $y - $oy);
814 $self->update;
815 };
816 }
817}
818
819sub button_up {
820 my ($self, $ev, $x, $y) = @_;
821
822 delete $self->{motion};
823}
824
825sub mouse_motion {
826 my ($self, $ev, $x, $y) = @_;
827
828 $self->{motion}->($ev, $x, $y) if $self->{motion};
829}
830
831sub _draw {
832 my ($self) = @_;
833
834 my ($w, $h ) = ($self->{w}, $self->{h});
835 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
836
837 glEnable GL_BLEND;
838 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
839 glEnable GL_TEXTURE_2D;
840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
841
842 my $border = $self->border;
843
844 glColor @{ $self->{border_bg} };
845 $tex[1]->draw_quad (0, 0, $w, $border);
846 $tex[3]->draw_quad (0, $border, $border, $ch);
847 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
848 $tex[4]->draw_quad (0, $h - $border, $w, $border);
849
850 my $bg = $tex[0];
851
852 # TODO: repeat texture not scale
853 my $rep_x = $cw / $bg->{w};
854 my $rep_y = $ch / $bg->{h};
855
856 glColor @{ $self->{bg} };
857
858 $bg->{s} = $rep_x;
859 $bg->{t} = $rep_y;
860 $bg->{wrap_mode} = 1;
861 $bg->draw_quad ($border, $border, $cw, $ch);
862
863 glDisable GL_TEXTURE_2D;
864 glDisable GL_BLEND;
865
866 $self->{title}->draw if $self->{title};
867 $self->child->draw;
868}
869
870#############################################################################
871
872package CFClient::UI::Table;
873
874our @ISA = CFClient::UI::Base::;
875
876use List::Util qw(max sum);
877
878use CFClient::OpenGL;
879
880sub new {
881 my $class = shift;
882
883 $class->SUPER::new (
884 col_expand => [],
885 @_
886 )
887}
888
889sub add {
890 my ($self, $x, $y, $child) = @_;
891
892 $child->set_parent ($self);
893 $self->{children}[$y][$x] = $child;
894
895 $child->check_size;
896}
897
898# TODO: move to container class maybe? send childs a signal on removal?
899sub clear {
900 my ($self) = @_;
901
902 delete $self->{children};
903 $self->update;
904}
905
906sub get_wh {
907 my ($self) = @_;
908
909 my (@w, @h);
910
911 for my $y (0 .. $#{$self->{children}}) {
912 my $row = $self->{children}[$y]
913 or next;
914
915 for my $x (0 .. $#$row) {
916 my $widget = $row->[$x]
917 or next;
918 my ($w, $h) = @$widget{qw(req_w req_h)};
919
920 $w[$x] = max $w[$x], $w;
921 $h[$y] = max $h[$y], $h;
922 }
923 }
924
925 (\@w, \@h)
926}
927
928sub size_request {
929 my ($self) = @_;
930
931 my ($ws, $hs) = $self->get_wh;
932
933 (
934 (sum @$ws),
935 (sum @$hs),
936 )
937}
938
939sub size_allocate {
940 my ($self, $w, $h) = @_;
941
942 my ($ws, $hs) = $self->get_wh;
943
944 my $req_w = sum @$ws;
945 my $req_h = sum @$hs;
946
947 # TODO: nicer code && do row_expand
948 my @col_expand = @{$self->{col_expand}};
949 @col_expand = (1) x @$ws unless @col_expand;
950 my $col_expand = (sum @col_expand) || 1;
951
952 # linearly scale sizes
953 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
954 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
955
956 CFClient::UI::harmonize $ws;
957 CFClient::UI::harmonize $hs;
958
959 my $y;
960
961 for my $r (0 .. $#{$self->{children}}) {
962 my $row = $self->{children}[$r]
963 or next;
964
965 my $x = 0;
966 my $row_h = $hs->[$r];
967
968 for my $c (0 .. $#$row) {
969 my $col_w = $ws->[$c];
970
971 if (my $widget = $row->[$c]) {
972 $widget->configure ($x, $y, $col_w, $row_h);
973 }
974
975 $x += $col_w;
976 }
977
978 $y += $row_h;
979 }
980
177} 981}
178 982
179sub find_widget { 983sub find_widget {
180 my ($self, $x, $y) = @_; 984 my ($self, $x, $y) = @_;
181 985
986 $x -= $self->{x};
987 $y -= $self->{y};
988
182 my $res; 989 my $res;
183 990
184 for (@{ $self->{children} }) { 991 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
185 $res = $_->find_widget ($x, $y) 992 $res = $_->find_widget ($x, $y)
186 and return $res; 993 and return $res;
187 } 994 }
188 995
189 () 996 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
190} 997}
191 998
192sub _draw { 999sub _draw {
193 my ($self) = @_; 1000 my ($self) = @_;
194 1001
195 $_->draw for @{$self->{children}}; 1002 for (grep $_, @{$self->{children}}) {
1003 $_->draw for grep $_, @$_;
1004 }
196} 1005}
197 1006
198############################################################################# 1007#############################################################################
199 1008
200package Crossfire::Client::Widget::Bin; 1009package CFClient::UI::HBox;
201 1010
1011# TODO: wrap into common Box base class
1012
202our @ISA = Crossfire::Client::Widget::Container::; 1013our @ISA = CFClient::UI::Container::;
203
204sub child { $_[0]->{children}[0] }
205 1014
206sub size_request { 1015sub size_request {
207 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 1016 my ($self) = @_;
1017
1018 my @alloc = map [$_->size_request], @{$self->{children}};
1019
1020 (
1021 (List::Util::sum map $_->[0], @alloc),
1022 (List::Util::max map $_->[1], @alloc),
1023 )
208} 1024}
209 1025
210sub size_allocate { 1026sub size_allocate {
211 my ($self, $w, $h) = @_; 1027 my ($self, $w, $h) = @_;
212 1028
213 $self->SUPER::size_allocate ($w, $h); 1029 ($h, $w) = ($w, $h);
214 $self->{children}[0]->size_allocate ($w, $h)
215 if $self->{children}[0]
216}
217 1030
218#############################################################################
219
220package Crossfire::Client::Widget::Toplevel;
221
222our @ISA = Crossfire::Client::Widget::Container::;
223
224sub update {
225 my ($self) = @_;
226
227 ::refresh ();
228}
229
230sub add {
231 my ($self, $widget) = @_;
232
233 $self->SUPER::add ($widget);
234
235 $widget->size_allocate ($widget->size_request);
236}
237
238#############################################################################
239
240package Crossfire::Client::Widget::Window;
241
242our @ISA = Crossfire::Client::Widget::Bin::;
243
244use SDL::OpenGL;
245
246sub new {
247 my ($class, $x, $y, $z, $w, $h) = @_;
248
249 my $self = $class->SUPER::new;
250
251 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
252}
253
254sub update {
255 my ($self) = @_;
256
257 $self->render_chld;
258 $self->SUPER::update;
259}
260
261sub render_chld {
262 my ($self) = @_;
263
264 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl (
266 $self->{w}, $self->{h}, sub { $self->child->draw }
267 );
268}
269
270sub size_allocate {
271 my ($self, $w, $h) = @_;
272
273 $self->{w} = $w;
274 $self->{h} = $h;
275
276 $self->child->size_allocate ($w, $h);
277
278 $self->render_chld;
279}
280
281sub _draw {
282 my ($self) = @_;
283
284 my ($w, $h) = ($self->w, $self->h);
285
286 my $tex = $self->{texture}
287 or return;
288
289 glEnable GL_BLEND;
290 glEnable GL_TEXTURE_2D;
291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
292 glBindTexture GL_TEXTURE_2D, $tex->{name};
293
294 glBegin GL_QUADS;
295 glTexCoord 0, 0; glVertex 0, 0;
296 glTexCoord 0, 1; glVertex 0, $h;
297 glTexCoord 1, 1; glVertex $w, $h;
298 glTexCoord 1, 0; glVertex $w, 0;
299 glEnd;
300
301 glDisable GL_BLEND;
302 glDisable GL_TEXTURE_2D;
303}
304
305#############################################################################
306
307package Crossfire::Client::Widget::Frame;
308
309our @ISA = Crossfire::Client::Widget::Bin::;
310
311use SDL::OpenGL;
312
313sub size_request {
314 my ($self) = @_;
315 my $chld = $self->child
316 or return (0, 0);
317
318 $chld->move (2, 2);
319
320 map { $_ + 4 } $chld->size_request;
321}
322
323sub size_allocate {
324 my ($self, $w, $h) = @_;
325
326 $self->{w} = $w;
327 $self->{h} = $h;
328
329 $self->child->size_allocate ($w - 4, $h - 4);
330 $self->child->move (2, 2);
331}
332
333sub _draw {
334 my ($self) = @_;
335
336 my $chld = $self->child; 1031 my $children = $self->{children};
337 1032
338 my ($w, $h) = $chld->size_request; 1033 my @h = map $_->{req_w}, @$children;
339 1034
340 glBegin GL_QUADS; 1035 my $req_h = List::Util::sum @h;
341 glColor 0, 0, 0;
342 glTexCoord 0, 0; glVertex 0 , 0;
343 glTexCoord 0, 1; glVertex 0 , $h + 4;
344 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
345 glTexCoord 1, 0; glVertex $w + 4 , 0;
346 glEnd;
347 1036
348 $chld->draw; 1037 if ($req_h > $h) {
349} 1038 # ah well, not enough space
1039 $_ *= $h / $req_h for @h;
1040 } else {
1041 my $exp = List::Util::sum map $_->{expand}, @$children;
1042 $exp ||= 1;
1043
1044 for (0 .. $#$children) {
1045 my $child = $children->[$_];
350 1046
351############################################################################# 1047 my $alloc_h = $h[$_];
352 1048 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
353package Crossfire::Client::Widget::FancyFrame; 1049 $h[$_] = $alloc_h;
354
355our @ISA = Crossfire::Client::Widget::Bin::;
356
357use SDL::OpenGL;
358
359my @tex =
360 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
361 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
362
363sub size_request {
364 my ($self) = @_;
365
366 my ($w, $h) = $self->SUPER::size_request;
367
368 $h += $tex[1]->{height};
369 $h += $tex[4]->{height};
370 $w += $tex[2]->{width};
371 $w += $tex[3]->{width};
372
373 ($w, $h)
374}
375
376sub size_allocate {
377 my ($self, $w, $h) = @_;
378
379 $self->SUPER::size_allocate ($w, $h);
380
381 $h -= $tex[1]->{height};
382 $h -= $tex[4]->{height};
383 $w -= $tex[2]->{width};
384 $w -= $tex[3]->{width};
385
386 $h = $h < 0 ? 0 : $h;
387 $w = $w < 0 ? 0 : $w;
388
389 $self->child->size_allocate ($w, $h);
390 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
391}
392
393sub _draw {
394 my ($self) = @_;
395
396 my ($w, $h) = ($self->{w}, $self->{h});
397 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
398
399 glEnable GL_BLEND;
400 glEnable GL_TEXTURE_2D;
401 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
402 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
403
404 my $top = $tex[1];
405 glBindTexture GL_TEXTURE_2D, $top->{name};
406
407 glBegin GL_QUADS;
408 glTexCoord 0, 0; glVertex 0 , 0;
409 glTexCoord 0, 1; glVertex 0 , $top->{height};
410 glTexCoord 1, 1; glVertex $w , $top->{height};
411 glTexCoord 1, 0; glVertex $w , 0;
412 glEnd;
413
414 my $left = $tex[3];
415 glBindTexture GL_TEXTURE_2D, $left->{name};
416
417 glBegin GL_QUADS;
418 glTexCoord 0, 0; glVertex 0 , $top->{height};
419 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
420 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
421 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
422 glEnd;
423
424 my $right = $tex[2];
425 glBindTexture GL_TEXTURE_2D, $right->{name};
426
427 glBegin GL_QUADS;
428 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
429 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
430 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
431 glTexCoord 1, 0; glVertex $w , $top->{height};
432 glEnd;
433
434 my $bottom = $tex[4];
435 glBindTexture GL_TEXTURE_2D, $bottom->{name};
436
437 glBegin GL_QUADS;
438 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
439 glTexCoord 0, 1; glVertex 0 , $h;
440 glTexCoord 1, 1; glVertex $w , $h;
441 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
442 glEnd;
443
444 my $bg = $tex[0];
445 glBindTexture GL_TEXTURE_2D, $bg->{name};
446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
447 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
448 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
449
450 my $rep_x = $cw / $bg->{width};
451 my $rep_y = $ch / $bg->{height};
452
453 glBegin GL_QUADS;
454 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
455 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
456 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
457 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
458 glEnd;
459
460 glDisable GL_BLEND;
461 glDisable GL_TEXTURE_2D;
462
463 $self->child->draw;
464
465}
466
467#############################################################################
468
469package Crossfire::Client::Widget::Table;
470
471our @ISA = Crossfire::Client::Widget::Bin::;
472
473use SDL::OpenGL;
474
475sub add {
476 my ($self, $x, $y, $chld) = @_;
477 my $old_chld = $self->{children}[$y][$x];
478
479 $self->{children}[$y][$x] = $chld;
480 $chld->set_parent ($self);
481 $self->update;
482}
483
484sub max_row_height {
485 my ($self, $row) = @_;
486
487 my $hs = 0;
488 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
489 my $c = $self->{children}->[$row]->[$xi];
490 if ($c) {
491 my ($w, $h) = $c->size_request;
492 if ($hs < $h) { $hs = $h }
493 } 1050 }
494 } 1051 }
495 return $hs;
496}
497 1052
498sub max_col_width { 1053 CFClient::UI::harmonize \@h;
499 my ($self, $col) = @_;
500
501 my $ws = 0;
502 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
503 my $c = ($self->{children}->[$yi] || [])->[$col];
504 if ($c) {
505 my ($w, $h) = $c->size_request;
506 if ($ws < $w) { $ws = $w }
507 }
508 }
509 return $ws;
510}
511
512sub size_request {
513 my ($self) = @_;
514
515 my ($hs, $ws) = (0, 0);
516
517 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
518 $hs += $self->max_row_height ($yi);
519 }
520
521 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
522 my $wm = 0;
523 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
524 $wm += $self->max_col_width ($xi)
525 }
526 if ($ws < $wm) { $ws = $wm }
527 }
528
529 return ($ws, $hs);
530}
531
532sub _draw {
533 my ($self) = @_;
534 1054
535 my $y = 0; 1055 my $y = 0;
536 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 1056 for (0 .. $#$children) {
1057 my $child = $children->[$_];
537 my $x = 0; 1058 my $h = $h[$_];
1059 $child->configure ($y, 0, $h, $w);
538 1060
539 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 1061 $y += $h;
1062 }
540 1063
541 my $c = $self->{children}->[$yi]->[$xi];
542 if ($c) {
543 $c->move ($x, $y, 0); #TODO: Move to size_request
544 $c->draw if $c;
545 }
546
547 $x += $self->max_col_width ($xi);
548 }
549
550 $y += $self->max_row_height ($yi);
551 } 1064 1
552} 1065}
553 1066
554############################################################################# 1067#############################################################################
555 1068
556package Crossfire::Client::Widget::VBox; 1069package CFClient::UI::VBox;
557 1070
1071# TODO: wrap into common Box base class
1072
558our @ISA = Crossfire::Client::Widget::Container::; 1073our @ISA = CFClient::UI::Container::;
559
560use SDL::OpenGL;
561 1074
562sub size_request { 1075sub size_request {
563 my ($self) = @_; 1076 my ($self) = @_;
564 1077
565 my @alloc = map [$_->size_request], @{$self->{children}}; 1078 my @alloc = map [$_->size_request], @{$self->{children}};
571} 1084}
572 1085
573sub size_allocate { 1086sub size_allocate {
574 my ($self, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
575 1088
576 $self->w ($w); 1089 my $children = $self->{children};
577 $self->h ($h);
578 1090
579 my $exp; 1091 my @h = map $_->{req_h}, @$children;
580 my @oth; 1092
581 # find expand widget 1093 my $req_h = List::Util::sum @h;
582 for (@{$self->{children}}) { 1094
583 if ($_->{expand}) { 1095 if ($req_h > $h) {
1096 # ah well, not enough space
1097 $_ *= $h / $req_h for @h;
1098 } else {
1099 my $exp = List::Util::sum map $_->{expand}, @$children;
584 $exp = $_; 1100 $exp ||= 1;
585 last; 1101
1102 for (0 .. $#$children) {
1103 my $child = $children->[$_];
1104
1105 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
586 } 1106 }
587 push @oth, $_;
588 } 1107 }
589 1108
590 my ($ow, $oh); 1109 CFClient::UI::harmonize \@h;
591
592 # get sizes of other widgets
593 for (@oth) {
594 my ($w, $h) = $_->size_request;
595 $oh += $h;
596 if ($ow < $w) { $ow = $w }
597 }
598 1110
599 my $y = 0; 1111 my $y = 0;
600 for (@{$self->{children}}) { 1112 for (0 .. $#$children) {
601 $_->move (0, $y); 1113 my $child = $children->[$_];
1114 my $h = $h[$_];
1115 $child->configure (0, $y, $w, $h);
602 1116
603 if ($_ == $exp) {
604 $_->size_allocate ($w, $h - $oh);
605 $y += $h - $oh;
606 } else {
607 my ($cw, $h) = $_->size_request;
608 $_->size_allocate ($w, $h);
609 $y += $h; 1117 $y += $h;
610 } 1118 }
1119
611 } 1120 1
612} 1121}
613 1122
614############################################################################# 1123#############################################################################
615 1124
616package Crossfire::Client::Widget::Label; 1125package CFClient::UI::Label;
617 1126
618our @ISA = Crossfire::Client::Widget::; 1127our @ISA = CFClient::UI::Base::;
619 1128
620use SDL::OpenGL; 1129use CFClient::OpenGL;
621 1130
622sub new { 1131sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 1132 my ($class, %arg) = @_;
624 1133
625 # TODO: color, and make height, xyz etc. optional 1134 my $self = $class->SUPER::new (
626 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 1135 fg => [1, 1, 1],
1136 #font => default_font
1137 fontsize => 1,
1138 text => "",
1139 align => -1,
1140 valign => -1,
1141 padding => 2,
1142 layout => new CFClient::Layout,
1143 can_events => 0,
1144 %arg
1145 );
627 1146
628 $self->set_text ($text); 1147 if (exists $self->{template}) {
1148 my $layout = new CFClient::Layout;
1149 $layout->set_text (delete $self->{template});
1150 $self->{template} = $layout;
1151 }
1152
1153 $self->set_text (delete $self->{text}) if exists $self->{text};
1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
629 1155
630 $self 1156 $self
1157}
1158
1159sub escape {
1160 local $_ = $_[1];
1161
1162 s/&/&amp;/g;
1163 s/>/&gt;/g;
1164 s/</&lt;/g;
1165
1166 $_[1]
631} 1167}
632 1168
633sub set_text { 1169sub set_text {
634 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
635 1171
1172 $self->{layout}->set_text ($text);
1173
1174 delete $self->{texture};
1175 $self->check_size;
1176 $self->update;
1177}
1178
1179sub set_markup {
1180 my ($self, $markup) = @_;
1181
1182 $self->{layout}->set_markup ($markup);
1183
1184 delete $self->{texture};
1185 $self->check_size;
1186 $self->update;
1187}
1188
1189sub size_request {
1190 my ($self) = @_;
1191
1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1195
1196 my ($w, $h) = $self->{layout}->size;
1197
1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1201
1202 my ($w2, $h2) = $self->{template}->size;
1203
1204 $w = List::Util::max $w, $w2;
1205 $h = List::Util::max $h, $h2;
1206 }
1207
1208 (
1209 $w + $self->{padding} * 2,
1210 $h + $self->{padding} * 2,
1211 )
1212}
1213
1214sub size_allocate {
1215 my ($self, $w, $h) = @_;
1216
1217 delete $self->{texture};
1218}
1219
1220sub set_fontsize {
1221 my ($self, $fontsize) = @_;
1222
1223 $self->{fontsize} = $fontsize;
1224 delete $self->{texture};
1225 $self->check_size;
1226 $self->update;
1227}
1228
1229sub _draw {
1230 my ($self) = @_;
1231
1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1234 $self->{layout}->set_width ($self->{w});
1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1236 new_from_layout CFClient::Texture $self->{layout}
1237 };
1238
1239 glEnable GL_BLEND;
1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1241 glEnable GL_TEXTURE_2D;
1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1243
1244 glColor @{$self->{fg}};
1245
1246 $self->{ox} = int (
1247 $self->{align} < 0 ? $self->{padding}
1248 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1249 : ($self->{w} - $tex->{w}) * 0.5
1250 );
1251
1252 $self->{oy} = int (
1253 $self->{valign} < 0 ? $self->{padding}
1254 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1255 : ($self->{h} - $tex->{h}) * 0.5
1256 );
1257
1258 $tex->draw_quad ($self->{ox}, $self->{oy});
1259
1260 glDisable GL_TEXTURE_2D;
1261 glDisable GL_BLEND;
1262}
1263
1264#############################################################################
1265
1266package CFClient::UI::EntryBase;
1267
1268our @ISA = CFClient::UI::Label::;
1269
1270use CFClient::OpenGL;
1271
1272sub new {
1273 my $class = shift;
1274
1275 $class->SUPER::new (
1276 fg => [1, 1, 1],
1277 bg => [0, 0, 0, 0.2],
1278 active_bg => [1, 1, 1, 0.5],
1279 active_fg => [0, 0, 0],
1280 can_hover => 1,
1281 can_focus => 1,
1282 valign => 0,
1283 can_events => 1,
1284 @_
1285 )
1286}
1287
1288sub _set_text {
1289 my ($self, $text) = @_;
1290
1291 delete $self->{cur_h};
1292
1293 return if $self->{text} eq $text;
1294
1295 delete $self->{texture};
1296
1297 $self->{last_activity} = $::NOW;
636 $self->{text} = $text; 1298 $self->{text} = $text;
637 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
638 1299
1300 $text =~ s/./*/g if $self->{hidden};
1301 $self->{layout}->set_text ("$text ");
1302
1303 $self->emit (changed => $self->{text});
1304}
1305
1306sub get_text {
1307 $_[0]{text}
1308}
1309
1310sub size_request {
1311 my ($self) = @_;
1312
1313 my ($w, $h) = $self->SUPER::size_request;
1314
1315 ($w + 1, $h) # add 1 for cursor
1316}
1317
1318sub size_allocate {
1319 my ($self, $w, $h) = @_;
1320
1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1322}
1323
1324sub set_text {
1325 my ($self, $text) = @_;
1326
1327 $self->{cursor} = length $text;
1328 $self->_set_text ($text);
639 $self->update; 1329 $self->update;
640} 1330}
641 1331
642sub get_text { 1332sub key_down {
643 my ($self, $text) = @_; 1333 my ($self, $ev) = @_;
644 1334
645 $self->{text} 1335 my $mod = $ev->{mod};
1336 my $sym = $ev->{sym};
1337 my $uni = $ev->{unicode};
1338
1339 my $text = $self->get_text;
1340
1341 if ($sym == 8) {
1342 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1343 } elsif ($sym == 127) {
1344 substr $text, $self->{cursor}, 1, "";
1345 } elsif ($sym == CFClient::SDLK_LEFT) {
1346 --$self->{cursor} if $self->{cursor};
1347 } elsif ($sym == CFClient::SDLK_RIGHT) {
1348 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1349 } elsif ($sym == CFClient::SDLK_HOME) {
1350 $self->{cursor} = 0;
1351 } elsif ($sym == CFClient::SDLK_END) {
1352 $self->{cursor} = length $text;
1353 } elsif ($sym == 27) {
1354 $self->emit ('escape');
1355 } elsif ($uni) {
1356 substr $text, $self->{cursor}++, 0, chr $uni;
1357 }
1358
1359 $self->_set_text ($text);
1360 $self->update;
1361}
1362
1363sub focus_in {
1364 my ($self) = @_;
1365
1366 $self->{last_activity} = $::NOW;
1367
1368 $self->SUPER::focus_in;
1369}
1370
1371sub button_down {
1372 my ($self, $ev, $x, $y) = @_;
1373
1374 $self->SUPER::button_down ($ev, $x, $y);
1375
1376 my $idx = $self->{layout}->xy_to_index ($x, $y);
1377
1378 # byte-index to char-index
1379 my $text = $self->{text};
1380 utf8::encode $text;
1381 $self->{cursor} = length substr $text, 0, $idx;
1382
1383 $self->_set_text ($self->{text});
1384 $self->update;
1385}
1386
1387sub mouse_motion {
1388 my ($self, $ev, $x, $y) = @_;
1389# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1390}
1391
1392sub _draw {
1393 my ($self) = @_;
1394
1395 local $self->{fg} = $self->{fg};
1396
1397 if ($FOCUS == $self) {
1398 glColor @{$self->{active_bg}};
1399 $self->{fg} = $self->{active_fg};
1400 } else {
1401 glColor @{$self->{bg}};
1402 }
1403
1404 glEnable GL_BLEND;
1405 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1406 glBegin GL_QUADS;
1407 glVertex 0 , 0;
1408 glVertex 0 , $self->{h};
1409 glVertex $self->{w}, $self->{h};
1410 glVertex $self->{w}, 0;
1411 glEnd;
1412 glDisable GL_BLEND;
1413
1414 $self->SUPER::_draw;
1415
1416 #TODO: force update every cursor change :(
1417 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1418
1419 unless (exists $self->{cur_h}) {
1420 my $text = substr $self->{text}, 0, $self->{cursor};
1421 utf8::encode $text;
1422
1423 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1424 }
1425
1426 glColor @{$self->{fg}};
1427 glBegin GL_LINES;
1428 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1429 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1430 glEnd;
1431 }
1432}
1433
1434package CFClient::UI::Entry;
1435
1436our @ISA = CFClient::UI::EntryBase::;
1437
1438use CFClient::OpenGL;
1439
1440sub key_down {
1441 my ($self, $ev) = @_;
1442
1443 my $sym = $ev->{sym};
1444
1445 if ($sym == 13) {
1446 $self->emit (activate => $self->get_text);
1447 $self->update;
1448
1449 } else {
1450 $self->SUPER::key_down ($ev);
1451 }
1452
1453}
1454
1455#############################################################################
1456
1457package CFClient::UI::Button;
1458
1459our @ISA = CFClient::UI::Label::;
1460
1461use CFClient::OpenGL;
1462
1463my @tex =
1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1465 qw(b1_button_active.png);
1466
1467sub new {
1468 my $class = shift;
1469
1470 $class->SUPER::new (
1471 padding => 4,
1472 fg => [1, 1, 1],
1473 bg => [1, 1, 1, 0.2],
1474 active_fg => [0, 0, 1],
1475 can_hover => 1,
1476 align => 0,
1477 valign => 0,
1478 can_events => 1,
1479 @_
1480 )
1481}
1482
1483sub button_up {
1484 my ($self, $ev, $x, $y) = @_;
1485
1486 if ($x >= 0 && $x < $self->{w}
1487 && $y >= 0 && $y < $self->{h}) {
1488 $self->emit ("activate");
1489 }
1490}
1491
1492sub _draw {
1493 my ($self) = @_;
1494
1495 local $self->{fg} = $self->{fg};
1496
1497 if ($GRAB == $self) {
1498 $self->{fg} = $self->{active_fg};
1499 }
1500
1501 glEnable GL_BLEND;
1502 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1503 glEnable GL_TEXTURE_2D;
1504 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1505 glColor 0, 0, 0, 1;
1506
1507 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1508
1509 glDisable GL_TEXTURE_2D;
1510 glDisable GL_BLEND;
1511
1512 $self->SUPER::_draw;
1513}
1514
1515#############################################################################
1516
1517package CFClient::UI::CheckBox;
1518
1519our @ISA = CFClient::UI::DrawBG::;
1520
1521my @tex =
1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1524
1525use CFClient::OpenGL;
1526
1527sub new {
1528 my $class = shift;
1529
1530 $class->SUPER::new (
1531 padding => 2,
1532 fg => [1, 1, 1],
1533 active_fg => [1, 1, 0],
1534 state => 0,
1535 can_hover => 1,
1536 @_
1537 )
646} 1538}
647 1539
648sub size_request { 1540sub size_request {
649 my ($self) = @_; 1541 my ($self) = @_;
650 1542
1543 ($self->{padding} * 2 + 6) x 2
1544}
1545
1546sub button_down {
1547 my ($self, $ev, $x, $y) = @_;
1548
1549 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1550 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1551 $self->{state} = !$self->{state};
1552 $self->emit (changed => $self->{state});
651 ( 1553 }
652 $self->{texture}{width},
653 $self->{texture}{height},
654 )
655} 1554}
656 1555
657sub _draw { 1556sub _draw {
658 my ($self) = @_; 1557 my ($self) = @_;
659 1558
660 my $tex = $self->{texture}; 1559 $self->SUPER::_draw;
1560
1561 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1562
1563 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1564
1565 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
661 1566
662 glEnable GL_BLEND; 1567 glEnable GL_BLEND;
663 glEnable GL_TEXTURE_2D; 1568 glEnable GL_TEXTURE_2D;
664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1569 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1570
1571 my $tex = $self->{state} ? $tex[1] : $tex[0];
1572
1573 $tex->draw_quad (0, 0, $s, $s);
1574
1575 glDisable GL_TEXTURE_2D;
1576 glDisable GL_BLEND;
1577}
1578
1579#############################################################################
1580
1581package CFClient::UI::Image;
1582
1583our @ISA = CFClient::UI::Base::;
1584
1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1587
1588our %loaded_images;
1589
1590sub new {
1591 my $class = shift;
1592
1593 my $self = $class->SUPER::new (can_events => 0, @_);
1594
1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1596
1597 $loaded_images{$self->{image}} ||=
1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1599
1600 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1601
1602 Scalar::Util::weaken $loaded_images{$self->{image}};
1603
1604 $self->{aspect} = $tex->{w} / $tex->{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 ($self->{tex}->{w}, $self->{tex}->{h})
1613}
1614
1615sub _draw {
1616 my ($self) = @_;
1617
1618 my $tex = $self->{tex};
1619
1620 my ($w, $h) = ($self->{w}, $self->{h});
1621
1622 if ($self->{rot90}) {
1623 glRotate 90, 0, 0, 1;
1624 glTranslate 0, -$self->{w}, 0;
1625
1626 ($w, $h) = ($h, $w);
1627 }
1628
1629 glEnable GL_BLEND;
1630 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1631 glEnable GL_TEXTURE_2D;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1632 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
666 glBindTexture GL_TEXTURE_2D, $tex->{name};
667 1633
668 glColor 1, 0, 0, 1; # TODO color 1634 $tex->draw_quad (0, 0, $w, $h);
669
670 glBegin GL_QUADS;
671 glTexCoord 0, 0; glVertex 0 , 0;
672 glTexCoord 0, 1; glVertex 0 , $tex->{height};
673 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
674 glTexCoord 1, 0; glVertex $tex->{width}, 0;
675 glEnd;
676 1635
677 glDisable GL_BLEND; 1636 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 1637 glDisable GL_TEXTURE_2D;
679} 1638}
680 1639
681############################################################################# 1640#############################################################################
682 1641
683package Crossfire::Client::Widget::TextEntry; 1642package CFClient::UI::VGauge;
684 1643
685our @ISA = Crossfire::Client::Widget::Label::; 1644our @ISA = CFClient::UI::Base::;
686 1645
687use SDL; 1646use List::Util qw(min max);
688use SDL::OpenGL;
689 1647
690sub key_down { 1648use CFClient::OpenGL;
1649
1650my %tex = (
1651 food => [
1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1654 ],
1655 grace => [
1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1658 ],
1659 hp => [
1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1662 ],
1663 mana => [
1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1666 ],
1667);
1668
1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1670sub new {
1671 my $class = shift;
1672
1673 my $self = $class->SUPER::new (
1674 type => 'food',
1675 @_
1676 );
1677
1678 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1679
1680 $self
1681}
1682
1683sub size_request {
1684 my ($self) = @_;
1685
1686 #my $tex = $tex{$self->{type}}[0];
1687 #@$tex{qw(w h)}
1688 (0, 0)
1689}
1690
1691sub set_max {
1692 my ($self, $max) = @_;
1693
1694 $self->{max_val} = $max;
1695}
1696
1697sub set_value {
1698 my ($self, $val, $max) = @_;
1699
1700 $self->set_max ($max)
1701 if defined $max;
1702
1703 $max = $self->{max_val};
1704 $self->{val} = $val;
1705
1706 $self->update;
1707}
1708
1709sub _draw {
1710 my ($self) = @_;
1711
1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1714
1715 my ($w, $h) = ($self->{w}, $self->{h});
1716
1717 if ($self->{vertical}) {
1718 glRotate 90, 0, 0, 1;
1719 glTranslate 0, -$self->{w}, 0;
1720
1721 ($w, $h) = ($h, $w);
1722 }
1723
1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1725
1726 my $ycut1 = max 0, min 1, $ycut;
1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1729 my $h1 = $self->{h} * (1 - $ycut1);
1730 my $h2 = $self->{h} * (1 - $ycut2);
1731
1732 glEnable GL_BLEND;
1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1734 glEnable GL_TEXTURE_2D;
1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1736
1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1738 glBegin GL_QUADS;
1739 glTexCoord 0 , 0; glVertex 0 , 0;
1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1743 glEnd;
1744
1745 my $ycut1 = List::Util::min 1, $ycut;
1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1747 glBegin GL_QUADS;
1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1763
1764 glDisable GL_BLEND;
1765 glDisable GL_TEXTURE_2D;
1766}
1767
1768#############################################################################
1769
1770package CFClient::UI::Gauge;
1771
1772our @ISA = CFClient::UI::VBox::;
1773
1774sub new {
1775 my ($class, %arg) = @_;
1776
1777 my $self = $class->SUPER::new (
1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1782 );
1783
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1787
1788 $self
1789}
1790
1791sub set_fontsize {
691 my ($self, $ev) = @_; 1792 my ($self, $fsize) = @_;
692 1793
693 my $mod = $ev->key_mod; 1794 $self->{value}->set_fontsize ($fsize);
694 my $sym = $ev->key_sym; 1795 $self->{max} ->set_fontsize ($fsize);
1796}
695 1797
696 $ev->set_unicode (1); 1798sub set_value {
697 my $uni = $ev->key_unicode; 1799 my ($self, $val, $max) = @_;
698 1800
699 my $text = $self->get_text; 1801 $self->set_max ($max)
1802 if defined $max;
700 1803
701 if ($sym == SDLK_BACKSPACE) { 1804 $self->{gauge}->set_value ($val, $max);
702 substr $text, -1, 1, ''; 1805 $self->{value}->set_text ($val);
1806}
703 1807
704 } elsif ($uni) { 1808sub set_max {
705 $text .= chr $uni; 1809 my ($self, $max) = @_;
706 } 1810
1811 $self->{gauge}->set_max ($max);
707 $self->set_text ($text); 1812 $self->{max}->set_text ($max);
708} 1813}
709 1814
710############################################################################# 1815#############################################################################
711 1816
712package Crossfire::Client::Widget::MapWidget; 1817package CFClient::UI::Slider;
713 1818
714use strict; 1819use strict;
715 1820
716use List::Util qw(min max); 1821use CFClient::OpenGL;
717 1822
718use SDL; 1823our @ISA = CFClient::UI::DrawBG::;
719use SDL::OpenGL;
720use SDL::OpenGL::Constants;
721 1824
722our @ISA = Crossfire::Client::Widget::; 1825my @tex =
1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1827 qw(s1_slider.png s1_slider_bg.png);
723 1828
724sub key_down { 1829sub new {
725 print "MAPKEYDOWN\n"; 1830 my $class = shift;
726}
727 1831
728sub key_up { 1832 # range [value, low, high, page]
1833
1834 # TODO: 0-width page
1835 # TODO: req_w/h are wrong with vertical
1836 # TODO: calculations are off
1837 my $self = $class->SUPER::new (
1838 fg => [1, 1, 1],
1839 active_fg => [0, 0, 0],
1840 range => [0, 0, 100, 10],
1841 req_w => 20,
1842 req_h => 20,
1843 vertical => 0,
1844 can_hover => 1,
1845 inner_pad => 5,
1846 @_
1847 );
1848
1849 $self
729} 1850}
730 1851
731sub size_request { 1852sub size_request {
1853 my ($self) = @_;
732 1854
1855 my $w = $self->{req_w};
1856 my $h = $self->{req_h};
1857
1858 $self->{vertical} ? ($h, $w) : ($w, $h)
1859}
1860
1861sub button_down {
1862 my ($self, $ev, $x, $y) = @_;
1863
1864 $self->SUPER::button_down ($ev, $x, $y);
1865 $self->mouse_motion ($ev, $x, $y);
1866}
1867
1868sub mouse_motion {
1869 my ($self, $ev, $x, $y) = @_;
1870
1871 if ($GRAB == $self) {
1872 my ($value, $lo, $hi, $page) = @{$self->{range}};
1873
1874 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1875
1876 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1877 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1878
1879 $x -= $inner_pad_px; # substract the padding
1880 $x = $x * ($hi - $lo) / $inner_w + $lo;
1881 $x = $lo if $x < $lo;
1882 $x = $hi - $page if $x > $hi - $page;
1883 $self->{range}[0] = $x;
1884
1885 $self->emit (changed => $x);
1886 $self->update;
1887 }
1888}
1889
1890# the inner_* stuff is for generating a padding for the slider handle,
1891# so that the handle doesn't leave the texture. This calculation isn't 100%
1892# correct propably, but it does the job for now
1893sub _calc_inner_pad_px {
1894 my ($self, $w) = @_;
1895 ($w / 100) * $self->{inner_pad} # % to pixels
1896}
1897
1898sub _draw {
1899 my ($self) = @_;
1900
1901 $self->SUPER::_draw ();
1902
1903 my ($w, $h) = @$self{qw(w h)};
1904
1905 if ($self->{vertical}) {
1906 # draw a vertical slider like a rotated horizontal slider
1907
1908 glRotate 90, 0, 0, 1;
1909 glTranslate 0, -$self->{w}, 0;
1910
1911 ($w, $h) = ($h, $w);
1912 }
1913
1914 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1915 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1916
1917 my ($value, $lo, $hi, $page) = @{$self->{range}};
1918
1919 $hi = $value + 1 if $lo == $hi;
1920
1921 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1922 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1923
1924 $page = int $page * $inner_w / ($hi - $lo);
1925 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1926
1927 $w -= $page;
1928 $page &= ~1;
1929 glTranslate $page * 0.5, 0, 0;
1930 $page ||= 2;
1931
1932 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1933 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1934
1935 glEnable GL_BLEND;
1936 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1937 glEnable GL_TEXTURE_2D;
1938 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1939
1940 # draw background
1941 $tex[1]->draw_quad (0, 0, $w, $h);
1942
1943 # draw handle
1944 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1945
1946 glDisable GL_BLEND;
1947 glDisable GL_TEXTURE_2D;
1948}
1949
1950#############################################################################
1951
1952package CFClient::UI::TextView;
1953
1954our @ISA = CFClient::UI::HBox::;
1955
1956use CFClient::OpenGL;
1957
1958sub new {
1959 my $class = shift;
1960
1961 my $self = $class->SUPER::new (
1962 fontsize => 1,
1963 #font => default_font
1964 @_,
1965
1966 layout => (new CFClient::Layout),
1967 par => [],
1968 height => 0,
1969 children => [
1970 (new CFClient::UI::Empty expand => 1),
1971 (new CFClient::UI::Slider vertical => 1),
1972 ],
1973 );
1974
1975 $self->{children}[1]->connect (changed => sub {
1976 $self->update;
1977 });
1978
1979 $self
1980}
1981
1982sub set_fontsize {
1983 my ($self, $fontsize) = @_;
1984
1985 $self->{fontsize} = $fontsize;
1986 $self->reflow;
1987}
1988
1989sub text_height {
1990 my ($self, $text) = @_;
1991
1992 my $layout = $self->{layout};
1993
1994 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1995 $layout->set_width ($self->{w});
1996 $layout->set_text ($text);
1997
1998 ($layout->size)[1]
1999}
2000
2001sub reflow {
2002 my ($self) = @_;
2003
2004 $self->{need_reflow}++;
2005 $self->update;
733} 2006}
734 2007
735sub size_allocate { 2008sub size_allocate {
2009 my ($self, $w, $h) = @_;
2010
2011 $self->SUPER::size_allocate ($w, $h);
2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2015 $self->{layout}->set_width ($self->{children}[0]{w});
2016
2017 $self->reflow;
2018}
2019
2020sub add_paragraph {
2021 my ($self, $color, $text) = @_;
2022
2023 #TODO: intelligently "reformat" paragraph
2024
2025 my $height = $self->text_height ($text);
2026
2027 $self->{height} += $height;
2028
2029 push @{$self->{par}}, [$height, $color, $text];
2030
2031 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2032 $self->{children}[1]->update;
2033}
2034
2035sub update {
2036 my ($self) = @_;
2037
2038 $self->SUPER::update;
2039
2040 return unless $self->{h} > 0;
2041
2042 delete $self->{texture};
2043
2044 $ROOT->on_refresh ($self, sub {
2045 if (delete $self->{need_reflow}) {
2046 my $height = 0;
2047
2048 $height += $_->[0] = $self->text_height ($_->[2])
2049 for @{$self->{par}};
2050
2051 $self->{height} = $height;
2052
2053 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2054
2055 delete $self->{texture};
2056 }
2057
2058 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
2059 glClearColor 0, 0, 0, 1;
2060 glClear GL_COLOR_BUFFER_BIT;
2061
2062 glEnable GL_BLEND;
2063 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2064 glEnable GL_TEXTURE_2D;
2065 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2066
2067 my $top = int $self->{children}[1]{range}[0];
2068
2069 my $y0 = $top;
2070 my $y1 = $top + $self->{h};
2071
2072 my $y = 0;
2073
2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
2077
2078 for my $par (@{$self->{par}}) {
2079 my $h = $par->[0];
2080
2081 if ($y0 < $y + $h && $y < $y1) {
2082 $layout->set_text ($par->[2]);
2083
2084 glColor @{ $par->[1] };
2085 my ($W, $H) = $layout->size;
2086 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
2087 }
2088
2089 $y += $h;
2090 }
2091
2092 glDisable GL_TEXTURE_2D;
2093 glDisable GL_BLEND;
2094 };
2095 });
736} 2096}
737 2097
738sub _draw { 2098sub _draw {
739 my ($self) = @_; 2099 my ($self) = @_;
740 2100
741 my $mx = $::CONN->{mapx}; 2101 if ($self->{texture}) {
742 my $my = $::CONN->{mapy};
743
744 my $map = $::CONN->{map};
745
746 my ($xofs, $yofs);
747
748 my $sw = 1 + int $::WIDTH / 32;
749 my $sh = 1 + int $::HEIGHT / 32;
750
751 if ($::CONN->{mapw} > $sw) {
752 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
753 } else {
754 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
755 }
756
757 if ($::CONN->{maph} > $sh) {
758 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
759 } else {
760 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
761 }
762
763 glEnable GL_TEXTURE_2D; 2102 glEnable GL_TEXTURE_2D;
764 glEnable GL_BLEND;
765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2103 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
766 2104 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
767 my $sw4 = ($sw + 3) & ~3;
768 my $lighting = "\x00" x ($sw4 * $sh);
769
770 for my $x (0 .. $sw - 1) {
771 for my $y (0 .. $sh - 1) {
772
773 my $cell = $map->[$x + $xofs][$y + $yofs]
774 or next;
775
776 my $darkness = $cell->[0] * (1 / 255);
777 if ($darkness < 0) {
778 $darkness = 0.15;
779 }
780 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
781
782 for my $num (grep $_, @$cell[1,2,3]) {
783 my $tex = $::CONN->{face}[$num]{texture} || next;
784
785 glBindTexture GL_TEXTURE_2D, $tex->{name};
786
787 my $w = $tex->{width};
788 my $h = $tex->{height};
789
790 my $px = ($x + 1) * 32 - $w;
791 my $py = ($y + 1) * 32 - $h;
792
793 glBegin GL_QUADS;
794 glTexCoord 0, 0; glVertex $px , $py;
795 glTexCoord 0, 1; glVertex $px , $py + $h;
796 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
797 glTexCoord 1, 0; glVertex $px + $w, $py;
798 glEnd;
799 }
800 }
801 }
802
803# if (1) { # higher quality darkness
804# $lighting =~ s/(.)/$1$1$1/gs;
805# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
806#
807# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
808#
809# $lighting = $pb->get_pixels;
810# $lighting =~ s/(.)../$1/gs;
811# }
812
813 $lighting = new Crossfire::Client::Texture
814 width => $sw4,
815 height => $sh,
816 data => $lighting,
817 internalformat => GL_ALPHA4,
818 format => GL_ALPHA;
819
820 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
821 glColor 0, 0, 0, 0.75;
822 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
823 glBindTexture GL_TEXTURE_2D, $lighting->{name};
824 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
825 glBegin GL_QUADS;
826 glTexCoord 0, 0; glVertex 0 , 0;
827 glTexCoord 0, 1; glVertex 0 , $sh * 32;
828 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
829 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
830 glEnd;
831
832 glDisable GL_TEXTURE_2D; 2105 glDisable GL_TEXTURE_2D;
833 glDisable GL_BLEND;
834}
835
836my %DIR = (
837 SDLK_KP8, [1, "north"],
838 SDLK_KP9, [2, "northeast"],
839 SDLK_KP6, [3, "east"],
840 SDLK_KP3, [4, "southeast"],
841 SDLK_KP2, [5, "south"],
842 SDLK_KP1, [6, "southwest"],
843 SDLK_KP4, [7, "west"],
844 SDLK_KP7, [8, "northwest"],
845
846 SDLK_UP, [1, "north"],
847 SDLK_RIGHT, [3, "east"],
848 SDLK_DOWN, [5, "south"],
849 SDLK_LEFT, [7, "west"],
850);
851
852sub key_down {
853 my ($self, $ev) = @_;
854
855 my $mod = $ev->key_mod;
856 my $sym = $ev->key_sym;
857
858 if ($sym == SDLK_KP5) {
859 $::CONN->send ("command stay fire");
860 } elsif (exists $DIR{$sym}) {
861 if ($mod & KMOD_SHIFT) {
862 $self->{shft}++;
863 $::CONN->send ("command fire $DIR{$sym}[0]");
864 } elsif ($mod & KMOD_CTRL) {
865 $self->{ctrl}++;
866 $::CONN->send ("command run $DIR{$sym}[0]");
867 } else {
868 $::CONN->send ("command $DIR{$sym}[1]");
869 }
870 } 2106 }
871}
872 2107
873sub key_up { 2108 $self->{children}[1]->draw;
874 my ($self, $ev) = @_;
875 2109
876 my $mod = $ev->key_mod;
877 my $sym = $ev->key_sym;
878
879 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
880 $::CONN->send ("command fire_stop");
881 }
882 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
883 $::CONN->send ("command run_stop");
884 }
885} 2110}
886 2111
887############################################################################# 2112#############################################################################
888 2113
889package Crossfire::Client::Widget::Animator; 2114package CFClient::UI::Animator;
890 2115
891use SDL::OpenGL; 2116use CFClient::OpenGL;
892 2117
893our @ISA = Crossfire::Client::Widget::Bin::; 2118our @ISA = CFClient::UI::Bin::;
894 2119
895sub moveto { 2120sub moveto {
896 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
897 2122
898 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 2123 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
899 $self->{speed} = 0.2; 2124 $self->{speed} = 0.001;
900 $self->{time} = 1; 2125 $self->{time} = 1;
901 2126
902 ::animation_start $self; 2127 ::animation_start $self;
903} 2128}
904 2129
919 2144
920sub _draw { 2145sub _draw {
921 my ($self) = @_; 2146 my ($self) = @_;
922 2147
923 glPushMatrix; 2148 glPushMatrix;
924 glRotate $self->{time} * 10000, 0, 1, 0; 2149 glRotate $self->{time} * 1000, 0, 1, 0;
925 $self->{children}[0]->draw; 2150 $self->{children}[0]->draw;
926 glPopMatrix; 2151 glPopMatrix;
927} 2152}
928 2153
9291; 2154#############################################################################
930 2155
2156package CFClient::UI::Flopper;
2157
2158our @ISA = CFClient::UI::Button::;
2159
2160sub new {
2161 my $class = shift;
2162
2163 my $self = $class->SUPER::new (
2164 state => 0,
2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
2167 @_
2168 );
2169
2170 if ($self->{state}) {
2171 $self->{state} = 0;
2172 $self->toggle_flopper;
2173 }
2174
2175 $self
2176}
2177
2178sub toggle_flopper {
2179 my ($self) = @_;
2180
2181 # TODO: use animation
2182 if ($self->{state} = !$self->{state}) {
2183 $CFClient::UI::ROOT->add ($self->{other});
2184 $self->{other}->move ($self->coord2global (0, $self->{h}));
2185 $self->emit ("open");
2186 } else {
2187 $CFClient::UI::ROOT->remove ($self->{other});
2188 $self->emit ("close");
2189 }
2190
2191 $self->emit (changed => $self->{state});
2192}
2193
2194#############################################################################
2195
2196package CFClient::UI::Tooltip;
2197
2198our @ISA = CFClient::UI::Bin::;
2199
2200use CFClient::OpenGL;
2201
2202sub new {
2203 my $class = shift;
2204
2205 $class->SUPER::new (
2206 @_,
2207 can_events => 0,
2208 )
2209}
2210
2211sub set_markup {
2212 my ($self, $text) = @_;
2213
2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2215 $self->{label}->set_markup ($text);
2216 $self->add ($self->{label});
2217}
2218
2219sub size_request {
2220 my ($self) = @_;
2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2225
2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2233}
2234
2235sub _draw {
2236 my ($self) = @_;
2237
2238 glPushMatrix;
2239 glTranslate 0.375, 0.375;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 glColor 1, 0.8, 0.4;
2244 glBegin GL_QUADS;
2245 glVertex 0 , 0;
2246 glVertex 0 , $h;
2247 glVertex $w, $h;
2248 glVertex $w, 0;
2249 glEnd;
2250
2251 glColor 0, 0, 0;
2252 glBegin GL_LINE_LOOP;
2253 glVertex 0 , 0;
2254 glVertex 0 , $h;
2255 glVertex $w, $h;
2256 glVertex $w, 0;
2257 glEnd;
2258
2259 glPopMatrix;
2260
2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
2264
2265#############################################################################
2266
2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
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 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
2305package CFClient::UI::Root;
2306
2307our @ISA = CFClient::UI::Container::;
2308
2309use CFClient::OpenGL;
2310
2311sub check_size {
2312 my ($self) = @_;
2313
2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2315}
2316
2317sub size_request {
2318 ($::WIDTH, $::HEIGHT)
2319}
2320
2321sub configure {
2322 my ($self, $x, $y, $w, $h) = @_;
2323
2324 $self->SUPER::configure ($x, $y, $w, $h);
2325
2326 for my $child (@{$self->{children}}) {
2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2328
2329 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H);
2332 }
2333}
2334
2335sub _topleft {
2336 my ($self, $x, $y) = @_;
2337
2338 ($x, $y)
2339}
2340
2341sub update {
2342 my ($self) = @_;
2343
2344 $self->check_size;
2345 ::refresh ();
2346}
2347
2348sub add {
2349 my ($self, $child) = @_;
2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
2354
2355 $self->SUPER::add ($child);
2356}
2357
2358sub on_refresh {
2359 my ($self, $id, $cb) = @_;
2360
2361 $self->{refresh_hook}{$id} = $cb;
2362}
2363
2364sub draw {
2365 my ($self) = @_;
2366
2367 while (my $rcb = delete $self->{refresh_hook}) {
2368 $_->() for values %$rcb;
2369 }
2370
2371 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2372 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2373 glClear GL_COLOR_BUFFER_BIT;
2374
2375 glMatrixMode GL_PROJECTION;
2376 glLoadIdentity;
2377 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2378 glMatrixMode GL_MODELVIEW;
2379 glLoadIdentity;
2380
2381 $self->_draw;
2382}
2383
2384#############################################################################
2385
2386package CFClient::UI;
2387
2388$ROOT = new CFClient::UI::Root;
2389$TOOLTIP = new CFClient::UI::Tooltip;
2390
23911
2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines