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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines