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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines