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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines