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.34 by elmex, Sun Apr 9 02:44:51 2006 UTC vs.
Revision 1.37 by root, Sun Apr 9 21:05:50 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
11#our @ACTIVE_WIDGETS;
12 11
13# class methods for events 12# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 14sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
16sub feed_sdl_button_down_event { } 15sub feed_sdl_button_down_event { }
20 my $class = shift; 19 my $class = shift;
21 20
22 bless { @_ }, $class 21 bless { @_ }, $class
23} 22}
24 23
25#sub activate {
26# push @ACTIVE_WIDGETS, $_[0];
27# Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28#}
29
30#sub deactivate {
31# @ACTIVE_WIDGETS =
32# sort { $a->{z} <=> $b->{z} }
33# grep { $_ && $_ != $_[0] }
34# @ACTIVE_WIDGETS;
35#}
36
37sub move { 24sub move {
38 my ($self, $x, $y, $z) = @_; 25 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 26 $self->{x} = $x;
40 $self->{y} = $y; 27 $self->{y} = $y;
41 $self->{z} = $z if defined $z; 28 $self->{z} = $z if defined $z;
44sub needs_redraw { 31sub needs_redraw {
45 0 32 0
46} 33}
47 34
48sub size_request { 35sub size_request {
36 require Carp;
49 die "size_request is abtract"; 37 Carp::confess "size_request is abtract";
38}
39
40sub size_allocate {
41 my ($self, $w, $h) = @_;
42 $self->w ($w);
43 $self->h ($h);
50} 44}
51 45
52sub focus_in { 46sub focus_in {
53 my ($widget) = @_; 47 my ($widget) = @_;
54 $FOCUS = $widget; 48 $FOCUS = $widget;
72 66
73sub button_up { 67sub button_up {
74 my ($widget, $sdlev) = @_; 68 my ($widget, $sdlev) = @_;
75} 69}
76 70
77sub w { $_[0]->{w} } 71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} }
78sub h { $_[0]->{h} } 72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} }
79sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} }
80sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} }
81sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} }
82 76
83sub draw { 77sub draw {
128 my ($self) = @_; 122 my ($self) = @_;
129 123
130 #$self->deactivate; 124 #$self->deactivate;
131} 125}
132 126
133package Crossfire::Client::Widget::Container; 127package Crossfire::Client::Widget::Bin;
134 128
135our @ISA = Crossfire::Client::Widget::; 129our @ISA = Crossfire::Client::Widget::;
136 130
137use SDL::OpenGL;
138
139sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update } 131sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->{expand} = $_[2] }
140sub get { $_[0]->{child} } 132sub get { $_[0]->{child} }
133
141sub remove { 134sub remove {
142 my ($self, $chld) = @_; 135 my ($self, $chld) = @_;
143 delete $self->{child} 136 delete $self->{child}
144 if $self->{child} == $chld; 137 if $self->{child} == $chld;
145} 138}
146 139
140sub size_request {
147sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 141 $_[0]->{child}->size_request if $_[0]->{child}
142}
143sub size_allocate {
144 my ($self, $w, $h) = @_;
145 $self->SUPER::size_allocate ($w, $h);
146 $self->{child}->size_allocate ($w, $h)
147 if $self->{child}
148}
148 149
149sub _draw { die "Containers can't be drawn!" } 150sub _draw {
151 my ($self) = @_;
152
153 $self->{child}->draw;
154}
150 155
151package Crossfire::Client::Widget::Toplevel; 156package Crossfire::Client::Widget::Toplevel;
152 157
153our @ISA = Crossfire::Client::Widget::; 158our @ISA = Crossfire::Client::Widget::;
154 159
161 @{$self->{childs}} = 166 @{$self->{childs}} =
162 sort { $a->{z} <=> $b->{z} } 167 sort { $a->{z} <=> $b->{z} }
163 @{$self->{childs}}; 168 @{$self->{childs}};
164 169
165 $chld->set_parent ($self); 170 $chld->set_parent ($self);
171 $chld->size_allocate ($chld->size_request);
166} 172}
167 173
168sub remove { 174sub remove {
169 my ($self, $chld) = @_; 175 my ($self, $chld) = @_;
170 @{$self->{childs}} = 176 @{$self->{childs}} =
184 $_->draw for @{$self->{childs}}; 190 $_->draw for @{$self->{childs}};
185} 191}
186 192
187package Crossfire::Client::Widget::Window; 193package Crossfire::Client::Widget::Window;
188 194
189our @ISA = Crossfire::Client::Widget::Container::; 195our @ISA = Crossfire::Client::Widget::Bin::;
190 196
191use SDL::OpenGL; 197use SDL::OpenGL;
192 198
193sub add { 199sub add {
194 my ($self, $chld) = @_; 200 my ($self, $chld) = @_;
201 warn "ADD $chld\n";
195 $self->SUPER::add ($chld); 202 $self->SUPER::add ($chld);
196 $chld->set_parent ($self); 203 $chld->set_parent ($self);
197 $self->update; #TODO: Move this to the size_request event propably?
198} 204}
199 205
200sub remove { 206sub remove {
201 my ($self) = @_; 207 my ($self) = @_;
202 # TODO FIXME: removing a child from a window will crash, see render_chld 208 # TODO FIXME: removing a child from a window will crash, see render_chld
203 $self->update; 209 # $self->update;
204} 210}
205 211
206sub update { 212sub update {
207 my ($self) = @_; 213 my ($self) = @_;
208 $self->render_chld; 214 $self->render_chld;
211sub render_chld { 217sub render_chld {
212 my ($self) = @_; 218 my ($self) = @_;
213 my $chld = $self->get; 219 my $chld = $self->get;
214 my ($w, $h) = $self->size_request; 220 my ($w, $h) = $self->size_request;
215 221
222 require Carp;
223 Carp::cluck "RENDERCHI $w $h";
224 warn "RENDERCHI $w $h\n";
216 $self->{texture} = 225 $self->{texture} =
217 Crossfire::Client::Texture->new_from_opengl ( 226 Crossfire::Client::Texture->new_from_opengl (
218 $w, $h, sub { $chld->draw } 227 $w, $h, sub { $chld->draw }
219 ); 228 );
220 $self->{texture}->upload; 229 $self->{texture}->upload;
221} 230}
222 231
223sub size_request { 232sub size_request {
224 my ($self) = @_; 233 my ($self) = @_;
225 my $chld = $self->get 234 ($self->w, $self->h)
226 or return (0, 0);
227 $chld->size_request
228} 235}
229 236
230sub _draw { 237sub size_allocate {
231 my ($self) = @_; 238 my ($self, $w, $h) = @_;
232 239
233 my ($w, $h) = $self->size_request;#TODO# use width/height of texture 240 $self->w ($w);
241 $self->h ($h);
242 $self->get->size_allocate ($w, $h);
243
244 $self->update; #TODO: Move this to the size_request event propably?
245}
246
247sub _draw {
248 my ($self) = @_;
249
250 my ($w, $h) = ($self->w, $self->h);
234 251
235 my $tex = $self->{texture} 252 my $tex = $self->{texture}
236 or return; 253 or return;
237 254
238 glEnable GL_BLEND; 255 glEnable GL_BLEND;
239 glEnable GL_TEXTURE_2D; 256 glEnable GL_TEXTURE_2D;
240 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
241 glBindTexture GL_TEXTURE_2D, $tex->{name}; 258 glBindTexture GL_TEXTURE_2D, $tex->{name};
242
243 glColor 1, 0, 1;
244 259
245 glBegin GL_QUADS; 260 glBegin GL_QUADS;
246 glTexCoord 0, 0; glVertex 0, 0; 261 glTexCoord 0, 0; glVertex 0, 0;
247 glTexCoord 0, 1; glVertex 0, $h; 262 glTexCoord 0, 1; glVertex 0, $h;
248 glTexCoord 1, 1; glVertex $w, $h; 263 glTexCoord 1, 1; glVertex $w, $h;
253 glDisable GL_TEXTURE_2D; 268 glDisable GL_TEXTURE_2D;
254} 269}
255 270
256package Crossfire::Client::Widget::Frame; 271package Crossfire::Client::Widget::Frame;
257 272
258our @ISA = Crossfire::Client::Widget::Container::; 273our @ISA = Crossfire::Client::Widget::Bin::;
259 274
260use SDL::OpenGL; 275use SDL::OpenGL;
261 276
262sub size_request { 277sub size_request {
263 my ($self) = @_; 278 my ($self) = @_;
265 or return (0, 0); 280 or return (0, 0);
266 281
267 $chld->move (2, 2); 282 $chld->move (2, 2);
268 283
269 map { $_ + 4 } $chld->size_request; 284 map { $_ + 4 } $chld->size_request;
285}
286
287sub size_allocate {
288 my ($self, $w, $h) = @_;
289
290 $self->w ($w);
291 $self->h ($h);
292
293 $self->get->size_allocate ($w - 4, $h - 4);
294 $self->get->move (2, 2);
270} 295}
271 296
272sub _draw { 297sub _draw {
273 my ($self) = @_; 298 my ($self) = @_;
274 299
300 $self->{txts} = [ 325 $self->{txts} = [
301 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 326 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
302 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 327 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/
303 ]; 328 ];
304 $self 329 $self
305
306} 330}
307 331
308sub size_request { 332sub size_request {
309 my ($self) = @_; 333 my ($self) = @_;
310 my ($w, $h) = $self->get->size_request; 334 my ($w, $h) = $self->get->size_request;
312 $h += $self->{txts}->[1]->{height}; 336 $h += $self->{txts}->[1]->{height};
313 $h += $self->{txts}->[4]->{height}; 337 $h += $self->{txts}->[4]->{height};
314 $w += $self->{txts}->[2]->{width}; 338 $w += $self->{txts}->[2]->{width};
315 $w += $self->{txts}->[3]->{width}; 339 $w += $self->{txts}->[3]->{width};
316 340
341 ($w, $h)
342}
343
344sub size_allocate {
345 my ($self, $w, $h) = @_;
346
347 $self->w ($w);
348 $self->h ($h);
349 $h -= $self->{txts}->[1]->{height};
350 $h -= $self->{txts}->[4]->{height};
351 $w -= $self->{txts}->[2]->{width};
352 $w -= $self->{txts}->[3]->{width};
353
354 $h = $h < 0 ? 0 : $h;
355 $w = $w < 0 ? 0 : $w;
356 warn "CHILD:$w $h\n";
357 $self->get->size_allocate ($w, $h);
317 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 358 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
318
319 ($w, $h)
320} 359}
321 360
322sub _draw { 361sub _draw {
323 my ($self) = @_; 362 my ($self) = @_;
324 363
325 my ($w, $h) = $self->size_request; 364 my ($w, $h) = ($self->w, $self->h);
326 my ($cw, $ch) = $self->get->size_request; 365 my ($cw, $ch) = ($self->get->w, $self->get->h);
327 366
328 glEnable GL_BLEND; 367 glEnable GL_BLEND;
329 glEnable GL_TEXTURE_2D; 368 glEnable GL_TEXTURE_2D;
330 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 369 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
331 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 370 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
332
333 371
334 my $top = $self->{txts}->[1]; 372 my $top = $self->{txts}->[1];
335 glBindTexture GL_TEXTURE_2D, $top->{name}; 373 glBindTexture GL_TEXTURE_2D, $top->{name};
336 374
337 glColor 1, 1, 1, 0.8;
338
339 glBegin GL_QUADS; 375 glBegin GL_QUADS;
340 glTexCoord 0, 0; glVertex 0 , 0; 376 glTexCoord 0, 0; glVertex 0 , 0;
341 glTexCoord 0, 1; glVertex 0 , $top->{height}; 377 glTexCoord 0, 1; glVertex 0 , $top->{height};
342 glTexCoord 1, 1; glVertex $w , $top->{height}; 378 glTexCoord 1, 1; glVertex $w , $top->{height};
343 glTexCoord 1, 0; glVertex $w , 0; 379 glTexCoord 1, 0; glVertex $w , 0;
344 glEnd; 380 glEnd;
345 381
346 my $left = $self->{txts}->[3]; 382 my $left = $self->{txts}->[3];
347 glBindTexture GL_TEXTURE_2D, $left->{name}; 383 glBindTexture GL_TEXTURE_2D, $left->{name};
348 384
349 glColor 1, 1, 1, 0.8;
350
351 glBegin GL_QUADS; 385 glBegin GL_QUADS;
352 glTexCoord 0, 0; glVertex 0 , $top->{height}; 386 glTexCoord 0, 0; glVertex 0 , $top->{height};
353 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 387 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
354 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 388 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
355 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 389 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
356 glEnd; 390 glEnd;
357 391
358 my $right = $self->{txts}->[2]; 392 my $right = $self->{txts}->[2];
359 glBindTexture GL_TEXTURE_2D, $right->{name}; 393 glBindTexture GL_TEXTURE_2D, $right->{name};
360 394
361 glColor 1, 1, 1, 0.8;
362
363 glBegin GL_QUADS; 395 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 396 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
365 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 397 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
366 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 398 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
367 glTexCoord 1, 0; glVertex $w , $top->{height}; 399 glTexCoord 1, 0; glVertex $w , $top->{height};
368 glEnd; 400 glEnd;
369 401
370 my $bottom = $self->{txts}->[4]; 402 my $bottom = $self->{txts}->[4];
371 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 403 glBindTexture GL_TEXTURE_2D, $bottom->{name};
372 404
373 glColor 1, 1, 1, 0.8;
374
375 glBegin GL_QUADS; 405 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 406 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
377 glTexCoord 0, 1; glVertex 0 , $h; 407 glTexCoord 0, 1; glVertex 0 , $h;
378 glTexCoord 1, 1; glVertex $w , $h; 408 glTexCoord 1, 1; glVertex $w , $h;
379 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 409 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
380 glEnd; 410 glEnd;
381 411
382 my $bg = $self->{txts}->[0]; 412 my $bg = $self->{txts}->[0];
383 glBindTexture GL_TEXTURE_2D, $bg->{name}; 413 glBindTexture GL_TEXTURE_2D, $bg->{name};
414 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
384 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 415 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
385 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 416 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
386 417
387 glColor 1, 1, 1, 0.8;
388
389 my $rep_x = $cw / $bg->{width}; 418 my $rep_x = $cw / $bg->{width};
390 my $rep_y = $ch / $bg->{height}; 419 my $rep_y = $ch / $bg->{height};
391 420
392 glBegin GL_QUADS; 421 glBegin GL_QUADS;
393 glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; 422 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
394 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; 423 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
395 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; 424 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
396 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; 425 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
397 glEnd; 426 glEnd;
398
399 $self->get->draw;
400 427
401 glDisable GL_BLEND; 428 glDisable GL_BLEND;
402 glDisable GL_TEXTURE_2D; 429 glDisable GL_TEXTURE_2D;
430
431 $self->get->draw;
432
403} 433}
404 434
405package Crossfire::Client::Widget::Table; 435package Crossfire::Client::Widget::Table;
406 436
407our @ISA = Crossfire::Client::Widget::Container::; 437our @ISA = Crossfire::Client::Widget::Bin::;
408 438
409use SDL::OpenGL; 439use SDL::OpenGL;
410 440
411sub add { 441sub add {
412 my ($self, $x, $y, $chld) = @_; 442 my ($self, $x, $y, $chld) = @_;
487 } 517 }
488} 518}
489 519
490package Crossfire::Client::Widget::VBox; 520package Crossfire::Client::Widget::VBox;
491 521
492our @ISA = Crossfire::Client::Widget::Container::; 522our @ISA = Crossfire::Client::Widget::Bin::;
493 523
494use SDL::OpenGL; 524use SDL::OpenGL;
495 525
496sub add { 526sub add {
497 my ($self, $chld) = @_; 527 my ($self, $chld, $expand) = @_;
498 push @{$self->{childs}}, $chld; 528 push @{$self->{childs}}, $chld;
529 $chld->{expand} = $expand;
499 $chld->set_parent ($self); 530 $chld->set_parent ($self);
500 $self->update; 531 $self->update;
501} 532}
502 533
503sub size_request { 534sub size_request {
511 } 542 }
512 543
513 return ($ws, $hs); 544 return ($ws, $hs);
514} 545}
515 546
547sub size_allocate {
548 my ($self, $w, $h) = @_;
549
550 $self->w ($w);
551 $self->h ($h);
552
553 my $exp;
554 my @oth;
555 # find expand widget
556 for (@{$self->{childs}}) {
557 if ($_->{expand}) {
558 $exp = $_;
559 last;
560 }
561 push @oth, $_;
562 }
563
564 my ($ow, $oh);
565
566 # get sizes of other widgets
567 for (@oth) {
568 my ($w, $h) = $_->size_request;
569 $oh += $h;
570 if ($ow < $w) { $ow = $w }
571 }
572
573 my $y = 0;
574 for (@{$self->{childs}}) {
575 $_->move (0, $y);
576
577 if ($_ == $exp) {
578 $_->size_allocate ($w, $h - $oh);
579 $y += $h - $oh;
580 } else {
581 my ($cw, $h) = $_->size_request;
582 $_->size_allocate ($w, $h);
583 $y += $h;
584 }
585 }
586}
587
516sub _draw { 588sub _draw {
517 my ($self) = @_; 589 my ($self) = @_;
518 590
519 my ($x, $y); 591 my ($x, $y);
520 for (@{$self->{childs} || []}) { 592 for (@{$self->{childs} || []}) {
521 $_->move (0, $y, 0); #TODO: move to size_request
522 $_->draw; 593 $_->draw;
523 my ($w, $h) = $_->size_request;
524 $y += $h; 594 $y += $_->h;
525 } 595 }
526} 596}
527 597
528package Crossfire::Client::Widget::Label; 598package Crossfire::Client::Widget::Label;
529 599
575 glEnable GL_TEXTURE_2D; 645 glEnable GL_TEXTURE_2D;
576 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 646 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
577 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
578 glBindTexture GL_TEXTURE_2D, $tex->{name}; 648 glBindTexture GL_TEXTURE_2D, $tex->{name};
579 649
580 glColor 1, 1, 1, 0.6; # TODO color 650 glColor 1, 0, 0, 1; # TODO color
581 651
582 glBegin GL_QUADS; 652 glBegin GL_QUADS;
583 glTexCoord 0, 0; glVertex 0 , 0; 653 glTexCoord 0, 0; glVertex 0 , 0;
584 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 654 glTexCoord 0, 1; glVertex 0 , $tex->{height};
585 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 655 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
615 $text .= chr $uni; 685 $text .= chr $uni;
616 } 686 }
617 $self->set_text ($text); 687 $self->set_text ($text);
618} 688}
619 689
620
621# XXX: TextView isn't neccessary with pango multiline text rendering
622package Crossfire::Client::Widget::TextView;
623
624use strict;
625
626our @ISA = qw/Crossfire::Client::Widget/;
627
628use SDL::OpenGL;
629use SDL::OpenGL::Constants;
630
631sub new {
632 my ($class, $text, $h) = @_;
633 my $self = $class->SUPER::new ();
634
635 $self->{txt_height} = $h;
636 @{$self->{lines}} = split /\r?\n/, $text;
637
638 for (split /\r?\n/, $text) {
639 $self->add_line ($_);
640 }
641 $self
642}
643
644#sub render_lines {
645# my ($self) = @_;
646#
647# $self->{txt_lines} = [];
648#
649# for (@{$self->{lines}}) {
650# push @{$self->{txt_lines}},
651# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
652# }
653#}
654
655sub add_line {
656 my ($self, $line) = @_;
657 push @{$self->{lines}}, $line;
658
659 push @{$self->{txt_lines}},
660 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
661}
662
663sub size_request {
664 my ($self) = @_;
665
666 my $w = 0;
667 my $h = 0;
668
669 for (@{$self->{txt_lines}}) {
670 if ($w < $_->{width}) { $w = $_->{width} }
671 $h += $_->{height};
672 }
673
674 return ($w, $h);
675}
676
677sub draw_line {
678 my ($self, $tex, $y) = @_;
679
680 glBindTexture GL_TEXTURE_2D, $tex->{name};
681
682 glColor 1, 0, 1;
683
684 glBegin GL_QUADS;
685 glTexCoord 0, 0; glVertex 0 , $y;
686 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height};
687 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height};
688 glTexCoord 1, 0; glVertex $tex->{width}, $y;
689 glEnd;
690}
691
692sub _draw {
693 my ($self) = @_;
694
695 glEnable GL_BLEND;
696 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
698
699 my $l = 0;
700 for (@{$self->{txt_lines}}) {
701 $self->draw_line ($_, $l);
702 $l += $_->{height};
703 }
704
705 glDisable GL_BLEND;
706 glDisable GL_TEXTURE_2D;
707}
708
709package Crossfire::Client::Widget::MapWidget; 690package Crossfire::Client::Widget::MapWidget;
710 691
711use strict; 692use strict;
712 693
713use List::Util qw(min max); 694use List::Util qw(min max);
721sub key_down { 702sub key_down {
722 print "MAPKEYDOWN\n"; 703 print "MAPKEYDOWN\n";
723} 704}
724 705
725sub key_up { 706sub key_up {
707}
708
709sub size_request {
710
711}
712
713sub size_allocate {
726} 714}
727 715
728sub _draw { 716sub _draw {
729 my ($self) = @_; 717 my ($self) = @_;
730 718
750 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 738 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
751 } 739 }
752 740
753 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
754 glEnable GL_BLEND; 742 glEnable GL_BLEND;
755 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 743 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
744
745 my $sw4 = ($sw + 3) & ~3;
746 my $lighting = "\x00" x ($sw4 * $sh);
756 747
757 for my $x (0 .. $sw - 1) { 748 for my $x (0 .. $sw - 1) {
758 for my $y (0 .. $sh - 1) { 749 for my $y (0 .. $sh - 1) {
759 750
760 my $cell = $map->[$x + $xofs][$y + $yofs] 751 my $cell = $map->[$x + $xofs][$y + $yofs]
761 or next; 752 or next;
762 753
763 my $darkness = $cell->[0] * (1 / 255); 754 my $darkness = $cell->[0] * (1 / 255);
764 if ($darkness < 0) { 755 if ($darkness < 0) {
765 glColor 0.3, 0.3, 0.3; 756 $darkness = 0.15;
766 } else {
767 glColor $darkness, $darkness, $darkness;
768 } 757 }
758 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
769 759
770 for my $num (grep $_, @$cell[1,2,3]) { 760 for my $num (grep $_, @$cell[1,2,3]) {
771 my $tex = $::CONN->{face}[$num]{texture} || next; 761 my $tex = $::CONN->{face}[$num]{texture} || next;
772 762
773 glBindTexture GL_TEXTURE_2D, $tex->{name}; 763 glBindTexture GL_TEXTURE_2D, $tex->{name};
785 glTexCoord 1, 0; glVertex $px + $w, $py; 775 glTexCoord 1, 0; glVertex $px + $w, $py;
786 glEnd; 776 glEnd;
787 } 777 }
788 } 778 }
789 } 779 }
780
781# if (1) { # higher quality darkness
782# $lighting =~ s/(.)/$1$1$1/gs;
783# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
784#
785# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
786#
787# $lighting = $pb->get_pixels;
788# $lighting =~ s/(.)../$1/gs;
789# }
790
791 $lighting = new Crossfire::Client::Texture
792 width => $sw4,
793 height => $sh,
794 data => $lighting,
795 internalformat => GL_ALPHA4,
796 format => GL_ALPHA;
797
798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
799 glColor 0, 0, 0, 0.75;
800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
801 glBindTexture GL_TEXTURE_2D, $lighting->{name};
802 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
803 glBegin GL_QUADS;
804 glTexCoord 0, 0; glVertex 0 , 0;
805 glTexCoord 0, 1; glVertex 0 , $sh * 32;
806 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
807 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
808 glEnd;
790 809
791 glDisable GL_TEXTURE_2D; 810 glDisable GL_TEXTURE_2D;
792 glDisable GL_BLEND; 811 glDisable GL_BLEND;
793} 812}
794 813
841 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 860 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
842 $::CONN->send ("command run_stop"); 861 $::CONN->send ("command run_stop");
843 } 862 }
844} 863}
845 864
865package Crossfire::Client::Widget::Animator;
866
867use SDL::OpenGL;
868
869our @ISA = Crossfire::Client::Widget::Bin::;
870
871sub moveto {
872 my ($self, $x, $y) = @_;
873
874 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
875 $self->{speed} = 0.2;
876 $self->{time} = 1;
877
878 ::animation_start $self;
879}
880
881sub animate {
882 my ($self, $interval) = @_;
883
884 $self->{time} -= $interval * $self->{speed};
885 if ($self->{time} <= 0) {
886 $self->{time} = 0;
887 ::animation_stop $self;
888 }
889
890 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
891
892 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
893 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
894}
895
896sub _draw {
897 my ($self) = @_;
898
899 glPushMatrix;
900 glRotate $self->{time} * 10000, 0, 1, 0;
901 $self->{child}->draw;
902 glPopMatrix;
903}
904
8461; 9051;
847 906

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines