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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines