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.155 by root, Sun Apr 23 02:22:54 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines