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.172 by root, Tue Apr 25 09:52:04 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines