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.29 by root, Sun Apr 9 00:09:50 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;
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);
128 $self->render_chld; #TODO: Move this to the size_request event propably? 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;
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;
158 glEnable GL_BLEND; 238 glEnable GL_BLEND;
159 glEnable GL_TEXTURE_2D; 239 glEnable GL_TEXTURE_2D;
160 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 240 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
161 glBindTexture GL_TEXTURE_2D, $tex->{name}; 241 glBindTexture GL_TEXTURE_2D, $tex->{name};
162 242
163 glColor 1, 1, 1; 243 glColor 1, 0, 1;
164 244
165 glBegin GL_QUADS; 245 glBegin GL_QUADS;
166 glTexCoord 0, 0; glVertex 0, 0; 246 glTexCoord 0, 0; glVertex 0, 0;
167 glTexCoord 0, 1; glVertex 0, $h; 247 glTexCoord 0, 1; glVertex 0, $h;
168 glTexCoord 1, 1; glVertex $w, $h; 248 glTexCoord 1, 1; glVertex $w, $h;
181 261
182sub size_request { 262sub size_request {
183 my ($self) = @_; 263 my ($self) = @_;
184 my $chld = $self->get 264 my $chld = $self->get
185 or return (0, 0); 265 or return (0, 0);
266
267 $chld->move (2, 2);
268
186 map { $_ + 4 } $chld->size_request; 269 map { $_ + 4 } $chld->size_request;
187} 270}
188 271
189sub _draw { 272sub _draw {
190 my ($self) = @_; 273 my ($self) = @_;
191 274
192 my $chld = $self->get; 275 my $chld = $self->get;
193 276
194 $chld->move (2, 2, 0); #TODO:move to size_request
195
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 $chld->draw; 287 $chld->draw;
207} 288}
208 289
290package Crossfire::Client::Widget::FancyFrame;
291
292our @ISA = Crossfire::Client::Widget::Frame::;
293
294use SDL::OpenGL;
295
296#TODO: implement themed frame
297
209package Crossfire::Client::Widget::Table; 298package Crossfire::Client::Widget::Table;
210 299
211our @ISA = Crossfire::Client::Widget::Container::; 300our @ISA = Crossfire::Client::Widget::Container::;
212 301
213use SDL::OpenGL; 302use SDL::OpenGL;
214 303
215sub add { 304sub add {
216 my ($self, $x, $y, $chld) = @_; 305 my ($self, $x, $y, $chld) = @_;
306 my $old_chld = $self->{childs}[$y][$x];
307
217 $self->{childs}[$y][$x] = $chld; 308 $self->{childs}[$y][$x] = $chld;
309 $chld->set_parent ($self);
310 $self->update;
218} 311}
219 312
220sub max_row_height { 313sub max_row_height {
221 my ($self, $row) = @_; 314 my ($self, $row) = @_;
222 315
294use SDL::OpenGL; 387use SDL::OpenGL;
295 388
296sub add { 389sub add {
297 my ($self, $chld) = @_; 390 my ($self, $chld) = @_;
298 push @{$self->{childs}}, $chld; 391 push @{$self->{childs}}, $chld;
392 $chld->set_parent ($self);
393 $self->update;
299} 394}
300 395
301sub size_request { 396sub size_request {
302 my ($self) = @_; 397 my ($self) = @_;
303 398
341 436
342sub set_text { 437sub set_text {
343 my ($self, $text) = @_; 438 my ($self, $text) = @_;
344 439
345 $self->{text} = $text; 440 $self->{text} = $text;
346
347 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 441 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
442
443 $self->update;
348} 444}
349 445
350sub get_text { 446sub get_text {
351 my ($self, $text) = @_; 447 my ($self, $text) = @_;
352 448
367 463
368 my $tex = $self->{texture}; 464 my $tex = $self->{texture};
369 465
370 glEnable GL_BLEND; 466 glEnable GL_BLEND;
371 glEnable GL_TEXTURE_2D; 467 glEnable GL_TEXTURE_2D;
468 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
372 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 469 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
373 glBindTexture GL_TEXTURE_2D, $tex->{name}; 470 glBindTexture GL_TEXTURE_2D, $tex->{name};
374 471
375 glColor 1, 1, 1, 0.8; 472 glColor 1, 1, 1, 0.8;
376 473
383 480
384 glDisable GL_BLEND; 481 glDisable GL_BLEND;
385 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
386} 483}
387 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
388package Crossfire::Client::Widget::TextView; 514package Crossfire::Client::Widget::TextView;
389 515
390use strict; 516use strict;
391 517
392our @ISA = qw/Crossfire::Client::Widget/; 518our @ISA = qw/Crossfire::Client::Widget/;
393 519
394use SDL::OpenGL; 520use SDL::OpenGL;
395use 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#}
396 546
397sub add_line { 547sub add_line {
398 my ($self, $line) = @_; 548 my ($self, $line) = @_;
399 push @{$self->{lines}}, $line; 549 push @{$self->{lines}}, $line;
400}
401 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
402sub _draw { 569sub draw_line {
403 my ($self) = @_; 570 my ($self, $tex, $y) = @_;
404 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;
405} 599}
406 600
407package Crossfire::Client::Widget::MapWidget; 601package Crossfire::Client::Widget::MapWidget;
408 602
409use strict; 603use strict;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines