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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines