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.18 by root, Sat Apr 8 13:36:26 2006 UTC vs.
Revision 1.32 by elmex, Sun Apr 9 01:21:11 2006 UTC

6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10our $FOCUS; # the widget with current focus 10our $FOCUS; # the widget with current focus
11our @ACTIVE_WIDGETS; 11#our @ACTIVE_WIDGETS;
12 12
13# class methods for events 13# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
16sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 16sub feed_sdl_button_down_event { }
17sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 17sub feed_sdl_button_up_event { }
18 18
19sub new { 19sub new {
20 my $class = shift; 20 my $class = shift;
21 21
22 bless { @_ }, $class 22 bless { @_ }, $class
23} 23}
24 24
25sub activate { 25#sub activate {
26 push @ACTIVE_WIDGETS, $_[0]; 26# push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; 27# Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28} 28#}
29 29
30sub deactivate { 30#sub deactivate {
31 @ACTIVE_WIDGETS = 31# @ACTIVE_WIDGETS =
32 sort { $a->{z} <=> $b->{z} } 32# sort { $a->{z} <=> $b->{z} }
33 grep { $_ && $_ != $_[0] } 33# grep { $_ && $_ != $_[0] }
34 @ACTIVE_WIDGETS; 34# @ACTIVE_WIDGETS;
35} 35#}
36 36
37sub move { 37sub move {
38 my ($self, $x, $y, $z) = @_; 38 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 39 $self->{x} = $x;
40 $self->{y} = $y; 40 $self->{y} = $y;
41 $self->{z} = $z if defined $z; 41 $self->{z} = $z if defined $z;
42} 42}
43 43
44sub needs_redraw {
45 0
46}
47
44sub size_request { 48sub size_request {
45 die "size_request is abtract"; 49 die "size_request is abtract";
46} 50}
47 51
48sub focus_in { 52sub focus_in {
88sub _draw { 92sub _draw {
89 my ($widget) = @_; 93 my ($widget) = @_;
90} 94}
91 95
92sub bbox { 96sub bbox {
93 my ($widget) = @_; 97 my ($self) = @_;
98 my ($w, $h) = $self->size_request;
99 (
100 $self->{x},
101 $self->{y},
102 $self->{x} = $w,
103 $self->{y} = $h
104 )
105}
106
107sub del_parent { $_[0]->{parent} = undef }
108
109sub set_parent {
110 my ($self, $par) = @_;
111
112 $self->{parent} = $par;
113 Scalar::Util::weaken $self->{parent};
114}
115
116sub get_parent {
117 $_[0]->{parent}
118}
119
120sub update {
121 my ($self) = @_;
122
123 $self->{parent}->update
124 if $self->{parent};
94} 125}
95 126
96sub DESTROY { 127sub DESTROY {
97 my ($self) = @_; 128 my ($self) = @_;
98 129
99 $self->deactivate; 130 #$self->deactivate;
100} 131}
101 132
102package Crossfire::Client::Widget::Container; 133package Crossfire::Client::Widget::Container;
103 134
104our @ISA = Crossfire::Client::Widget::; 135our @ISA = Crossfire::Client::Widget::;
105 136
106use SDL::OpenGL; 137use SDL::OpenGL;
107 138
108sub add { $_[0]->{child} = $_[1] } 139sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update }
109sub get { $_[0]->{child} } 140sub get { $_[0]->{child} }
141sub remove {
142 my ($self, $chld) = @_;
143 delete $self->{child}
144 if $self->{child} == $chld;
145}
110 146
111sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 147sub size_request { $_[0]->{child}->size_request if $_[0]->{child} }
112 148
113sub _draw { die "Containers can't be drawn!" } 149sub _draw { die "Containers can't be drawn!" }
114 150
115package Crossfire::Client::Widget::Frame; 151package Crossfire::Client::Widget::Toplevel;
152
153our @ISA = Crossfire::Client::Widget::;
154
155use SDL::OpenGL;
156
157sub add {
158 my ($self, $chld) = @_;
159
160 push @{$self->{childs}}, $chld;
161 @{$self->{childs}} =
162 sort { $a->{z} <=> $b->{z} }
163 @{$self->{childs}};
164
165 $chld->set_parent ($self);
166}
167
168sub remove {
169 my ($self, $chld) = @_;
170 @{$self->{childs}} =
171 sort { $a->{z} <=> $b->{z} }
172 grep { $_ && $_ != $_[0] }
173 @{$self->{childs}}
174}
175
176sub update {
177 my ($self) = @_;
178 ::refresh ();
179}
180
181sub _draw {
182 my ($self) = @_;
183
184 $_->draw for @{$self->{childs}};
185}
186
187package Crossfire::Client::Widget::Window;
116 188
117our @ISA = Crossfire::Client::Widget::Container::; 189our @ISA = Crossfire::Client::Widget::Container::;
118 190
119use SDL::OpenGL; 191use SDL::OpenGL;
192
193sub add {
194 my ($self, $chld) = @_;
195 $self->SUPER::add ($chld);
196 $chld->set_parent ($self);
197 $self->update; #TODO: Move this to the size_request event propably?
198}
199
200sub remove {
201 my ($self) = @_;
202 # TODO FIXME: removing a child from a window will crash, see render_chld
203 $self->update;
204}
205
206sub update {
207 my ($self) = @_;
208 $self->render_chld;
209}
210
211sub render_chld {
212 my ($self) = @_;
213 my $chld = $self->get;
214 my ($w, $h) = $self->size_request;
215
216 $self->{texture} =
217 Crossfire::Client::Texture->new_from_opengl (
218 $w, $h, sub { $chld->draw }
219 );
220 $self->{texture}->upload;
221}
120 222
121sub size_request { 223sub size_request {
122 my ($self) = @_; 224 my ($self) = @_;
123 my $chld = $self->get 225 my $chld = $self->get
124 or return (0, 0); 226 or return (0, 0);
227 $chld->size_request
228}
229
230sub _draw {
231 my ($self) = @_;
232
233 my ($w, $h) = $self->size_request;#TODO# use width/height of texture
234
235 my $tex = $self->{texture}
236 or return;
237
238 glEnable GL_BLEND;
239 glEnable GL_TEXTURE_2D;
240 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
241 glBindTexture GL_TEXTURE_2D, $tex->{name};
242
243 glColor 1, 0, 1;
244
245 glBegin GL_QUADS;
246 glTexCoord 0, 0; glVertex 0, 0;
247 glTexCoord 0, 1; glVertex 0, $h;
248 glTexCoord 1, 1; glVertex $w, $h;
249 glTexCoord 1, 0; glVertex $w, 0;
250 glEnd;
251
252 glDisable GL_BLEND;
253 glDisable GL_TEXTURE_2D;
254}
255
256package Crossfire::Client::Widget::Frame;
257
258our @ISA = Crossfire::Client::Widget::Container::;
259
260use SDL::OpenGL;
261
262sub size_request {
263 my ($self) = @_;
264 my $chld = $self->get
265 or return (0, 0);
266
267 $chld->move (2, 2);
268
125 map { $_ + 4 } $chld->size_request; 269 map { $_ + 4 } $chld->size_request;
126} 270}
127 271
128sub _draw { 272sub _draw {
129 my ($self) = @_; 273 my ($self) = @_;
130 274
131 my $chld = $self->get; 275 my $chld = $self->get;
132 276
133 my ($w, $h) = $chld->size_request; 277 my ($w, $h) = $chld->size_request;
134 278
135 glColor 1, 0, 0;
136 glBegin GL_QUADS; 279 glBegin GL_QUADS;
280 glColor 0, 0, 0;
137 glTexCoord 0, 0; glVertex 0 , 0; 281 glTexCoord 0, 0; glVertex 0 , 0;
138 glTexCoord 0, 1; glVertex 0 , $h + 4; 282 glTexCoord 0, 1; glVertex 0 , $h + 4;
139 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 283 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
140 glTexCoord 1, 0; glVertex $w + 4 , 0; 284 glTexCoord 1, 0; glVertex $w + 4 , 0;
141 glEnd; 285 glEnd;
142 286
143 glPushMatrix;
144 glTranslate (2, 2, 0);
145 $chld->_draw; 287 $chld->draw;
146 glPopMatrix;
147} 288}
289
290package Crossfire::Client::Widget::FancyFrame;
291
292our @ISA = Crossfire::Client::Widget::Frame::;
293
294use SDL::OpenGL;
295
296#TODO: implement themed frame
148 297
149package Crossfire::Client::Widget::Table; 298package Crossfire::Client::Widget::Table;
150 299
151our @ISA = Crossfire::Client::Widget::Container::; 300our @ISA = Crossfire::Client::Widget::Container::;
152 301
153use SDL::OpenGL; 302use SDL::OpenGL;
154 303
155sub add { 304sub add {
156 my ($self, $x, $y, $chld) = @_; 305 my ($self, $x, $y, $chld) = @_;
306 my $old_chld = $self->{childs}[$y][$x];
307
157 $self->{childs}[$y][$x] = $chld; 308 $self->{childs}[$y][$x] = $chld;
309 $chld->set_parent ($self);
310 $self->update;
158} 311}
159 312
160sub max_row_height { 313sub max_row_height {
161 my ($self, $row) = @_; 314 my ($self, $row) = @_;
162 315
212 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 365 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
213 my $x = 0; 366 my $x = 0;
214 367
215 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 368 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
216 369
217 glPushMatrix;
218 glTranslate ($x, $y, 0);
219 my $c = $self->{childs}->[$yi]->[$xi]; 370 my $c = $self->{childs}->[$yi]->[$xi];
371 if ($c) {
372 $c->move ($x, $y, 0); #TODO: Move to size_request
220 $c->_draw if $c; 373 $c->draw if $c;
221 glPopMatrix; 374 }
222 375
223 $x += $self->max_col_width ($xi); 376 $x += $self->max_col_width ($xi);
224 } 377 }
225 378
226 $y += $self->max_row_height ($yi); 379 $y += $self->max_row_height ($yi);
234use SDL::OpenGL; 387use SDL::OpenGL;
235 388
236sub add { 389sub add {
237 my ($self, $chld) = @_; 390 my ($self, $chld) = @_;
238 push @{$self->{childs}}, $chld; 391 push @{$self->{childs}}, $chld;
392 $chld->set_parent ($self);
393 $self->update;
239} 394}
240 395
241sub size_request { 396sub size_request {
242 my ($self) = @_; 397 my ($self) = @_;
243 398
254sub _draw { 409sub _draw {
255 my ($self) = @_; 410 my ($self) = @_;
256 411
257 my ($x, $y); 412 my ($x, $y);
258 for (@{$self->{childs} || []}) { 413 for (@{$self->{childs} || []}) {
259 glPushMatrix; 414 $_->move (0, $y, 0); #TODO: move to size_request
260 glTranslate (0, $y, 0);
261 $_->_draw; 415 $_->draw;
262 glPopMatrix;
263 my ($w, $h) = $_->size_request; 416 my ($w, $h) = $_->size_request;
264 $y += $h; 417 $y += $h;
265 } 418 }
266} 419}
267 420
270our @ISA = Crossfire::Client::Widget::; 423our @ISA = Crossfire::Client::Widget::;
271 424
272use SDL::OpenGL; 425use SDL::OpenGL;
273 426
274sub new { 427sub new {
275 my ($class, $x, $y, $z, $ttf, $text) = @_; 428 my ($class, $x, $y, $z, $height, $text) = @_;
276 429
277 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 430 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
278 431
279 $self->set_text ($text); 432 $self->set_text ($text);
280 433
281 $self 434 $self
282} 435}
283 436
284sub set_text { 437sub set_text {
285 my ($self, $text) = @_; 438 my ($self, $text) = @_;
439
440 $self->{text} = $text;
286 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 441 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
442
443 $self->update;
287} 444}
288 445
289sub get_text { 446sub get_text {
290 my ($self, $text) = @_; 447 my ($self, $text) = @_;
448
291 $self->{text} 449 $self->{text}
292} 450}
293 451
294sub size_request { 452sub size_request {
295 my ($self) = @_; 453 my ($self) = @_;
305 463
306 my $tex = $self->{texture}; 464 my $tex = $self->{texture};
307 465
308 glEnable GL_BLEND; 466 glEnable GL_BLEND;
309 glEnable GL_TEXTURE_2D; 467 glEnable GL_TEXTURE_2D;
468 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
310 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 469 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
311 glBindTexture GL_TEXTURE_2D, $tex->{name}; 470 glBindTexture GL_TEXTURE_2D, $tex->{name};
312 471
313 glColor 1, 1, 1; 472 glColor 1, 1, 1, 0.8;
314 473
315 glBegin GL_QUADS; 474 glBegin GL_QUADS;
316 glTexCoord 0, 0; glVertex 0 , 0; 475 glTexCoord 0, 0; glVertex 0 , 0;
317 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 476 glTexCoord 0, 1; glVertex 0 , $tex->{height};
318 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 477 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
321 480
322 glDisable GL_BLEND; 481 glDisable GL_BLEND;
323 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
324} 483}
325 484
485package Crossfire::Client::Widget::TextEntry;
486
487our @ISA = Crossfire::Client::Widget::Label::;
488
489use SDL;
490use SDL::OpenGL;
491
492sub key_down {
493 my ($self, $ev) = @_;
494
495 my $mod = $ev->key_mod;
496 my $sym = $ev->key_sym;
497
498 $ev->set_unicode (1);
499 my $uni = $ev->key_unicode;
500
501 my $text = $self->get_text;
502
503 if ($sym == SDLK_BACKSPACE) {
504 substr $text, -1, 1, '';
505
506 } elsif ($uni) {
507 $text .= chr $uni;
508 }
509 $self->set_text ($text);
510}
511
512
513# XXX: TextView isn't neccessary with pango multiline text rendering
326package Crossfire::Client::Widget::TextView; 514package Crossfire::Client::Widget::TextView;
327 515
328use strict; 516use strict;
329 517
330our @ISA = qw/Crossfire::Client::Widget/; 518our @ISA = qw/Crossfire::Client::Widget/;
331 519
332use SDL::OpenGL; 520use SDL::OpenGL;
333use SDL::OpenGL::Constants; 521use SDL::OpenGL::Constants;
522
523sub new {
524 my ($class, $text, $h) = @_;
525 my $self = $class->SUPER::new ();
526
527 $self->{txt_height} = $h;
528 @{$self->{lines}} = split /\r?\n/, $text;
529
530 for (split /\r?\n/, $text) {
531 $self->add_line ($_);
532 }
533 $self
534}
535
536#sub render_lines {
537# my ($self) = @_;
538#
539# $self->{txt_lines} = [];
540#
541# for (@{$self->{lines}}) {
542# push @{$self->{txt_lines}},
543# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
544# }
545#}
334 546
335sub add_line { 547sub add_line {
336 my ($self, $line) = @_; 548 my ($self, $line) = @_;
337 push @{$self->{lines}}, $line; 549 push @{$self->{lines}}, $line;
338}
339 550
551 push @{$self->{txt_lines}},
552 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
553}
554
555sub size_request {
556 my ($self) = @_;
557
558 my $w = 0;
559 my $h = 0;
560
561 for (@{$self->{txt_lines}}) {
562 if ($w < $_->{width}) { $w = $_->{width} }
563 $h += $_->{height};
564 }
565
566 return ($w, $h);
567}
568
340sub _draw { 569sub draw_line {
341 my ($self) = @_; 570 my ($self, $tex, $y) = @_;
342 571
572 glBindTexture GL_TEXTURE_2D, $tex->{name};
573
574 glColor 1, 0, 1;
575
576 glBegin GL_QUADS;
577 glTexCoord 0, 0; glVertex 0 , $y;
578 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height};
579 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height};
580 glTexCoord 1, 0; glVertex $tex->{width}, $y;
581 glEnd;
582}
583
584sub _draw {
585 my ($self) = @_;
586
587 glEnable GL_BLEND;
588 glEnable GL_TEXTURE_2D;
589 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
590
591 my $l = 0;
592 for (@{$self->{txt_lines}}) {
593 $self->draw_line ($_, $l);
594 $l += $_->{height};
595 }
596
597 glDisable GL_BLEND;
598 glDisable GL_TEXTURE_2D;
343} 599}
344 600
345package Crossfire::Client::Widget::MapWidget; 601package Crossfire::Client::Widget::MapWidget;
346 602
347use strict; 603use strict;
348 604
349our @ISA = qw/Crossfire::Client::Widget/; 605use List::Util qw(min max);
350 606
351use SDL; 607use SDL;
352use SDL::OpenGL; 608use SDL::OpenGL;
353use SDL::OpenGL::Constants; 609use SDL::OpenGL::Constants;
610
611our @ISA = Crossfire::Client::Widget::;
354 612
355sub key_down { 613sub key_down {
356 print "MAPKEYDOWN\n"; 614 print "MAPKEYDOWN\n";
357} 615}
358 616
359sub key_up { 617sub key_up {
360} 618}
361 619
362sub _draw { 620sub _draw {
621 my ($self) = @_;
622
623 my $mx = $::CONN->{mapx};
624 my $my = $::CONN->{mapy};
625
626 my $map = $::CONN->{map};
627
628 my ($xofs, $yofs);
629
630 my $sw = 1 + int $::WIDTH / 32;
631 my $sh = 1 + int $::HEIGHT / 32;
632
633 if ($::CONN->{mapw} > $sw) {
634 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
635 } else {
636 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
637 }
638
639 if ($::CONN->{maph} > $sh) {
640 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
641 } else {
642 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
643 }
644
363 glEnable GL_TEXTURE_2D; 645 glEnable GL_TEXTURE_2D;
364 glEnable GL_BLEND; 646 glEnable GL_BLEND;
365 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
366 648
367 my $map = $::CONN->{map}; 649 for my $x (0 .. $sw - 1) {
368
369 for my $x (0 .. $::CONN->{mapw} - 1) {
370 for my $y (0 .. $::CONN->{maph} - 1) { 650 for my $y (0 .. $sh - 1) {
371 651
372 my $cell = $map->[$x][$y] 652 my $cell = $map->[$x + $xofs][$y + $yofs]
373 or next; 653 or next;
374 654
375 my $darkness = $cell->[3] * (1 / 255); 655 my $darkness = $cell->[0] * (1 / 255);
656 if ($darkness < 0) {
657 glColor 0.3, 0.3, 0.3;
658 } else {
376 glColor $darkness, $darkness, $darkness; 659 glColor $darkness, $darkness, $darkness;
660 }
377 661
378 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 662 for my $num (grep $_, @$cell[1,2,3]) {
379 my $tex = $::CONN->{face}[$num]{texture} || next; 663 my $tex = $::CONN->{face}[$num]{texture} || next;
380 664
381 glBindTexture GL_TEXTURE_2D, $tex->{name}; 665 glBindTexture GL_TEXTURE_2D, $tex->{name};
382 666
667 my $w = $tex->{width};
668 my $h = $tex->{height};
669
670 my $px = ($x + 1) * 32 - $w;
671 my $py = ($y + 1) * 32 - $h;
672
383 glBegin GL_QUADS; 673 glBegin GL_QUADS;
384 glTexCoord 0, 0; glVertex $x * 32 , $y * 32; 674 glTexCoord 0, 0; glVertex $px , $py;
385 glTexCoord 0, 1; glVertex $x * 32 , $y * 32 + 32; 675 glTexCoord 0, 1; glVertex $px , $py + $h;
386 glTexCoord 1, 1; glVertex $x * 32 + 32, $y * 32 + 32; 676 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
387 glTexCoord 1, 0; glVertex $x * 32 + 32, $y * 32; 677 glTexCoord 1, 0; glVertex $px + $w, $py;
388 glEnd; 678 glEnd;
389 } 679 }
390 } 680 }
391 } 681 }
392 682

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines