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.24 by root, Sat Apr 8 20:25:26 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
11our @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 { $FOCUS->button_down ($_[0]) if $FOCUS } 15sub feed_sdl_button_down_event { }
17sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 16sub feed_sdl_button_up_event { }
18 17
19sub new { 18sub new {
20 my $class = shift; 19 my $class = shift;
21 20
22 bless { @_ }, $class 21 bless { @_ }, $class
23}
24
25sub activate {
26 push @ACTIVE_WIDGETS, $_[0];
27 Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28}
29
30sub deactivate {
31 @ACTIVE_WIDGETS =
32 sort { $a->{z} <=> $b->{z} }
33 grep { $_ && $_ != $_[0] }
34 @ACTIVE_WIDGETS;
35} 22}
36 23
37sub move { 24sub move {
38 my ($self, $x, $y, $z) = @_; 25 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 26 $self->{x} = $x;
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 {
92sub _draw { 86sub _draw {
93 my ($widget) = @_; 87 my ($widget) = @_;
94} 88}
95 89
96sub bbox { 90sub bbox {
97 my ($widget) = @_; 91 my ($self) = @_;
92 my ($w, $h) = $self->size_request;
93 (
94 $self->{x},
95 $self->{y},
96 $self->{x} = $w,
97 $self->{y} = $h
98 )
99}
100
101sub del_parent { $_[0]->{parent} = undef }
102
103sub set_parent {
104 my ($self, $par) = @_;
105
106 $self->{parent} = $par;
107 Scalar::Util::weaken $self->{parent};
108}
109
110sub get_parent {
111 $_[0]->{parent}
112}
113
114sub update {
115 my ($self) = @_;
116
117 $self->{parent}->update
118 if $self->{parent};
98} 119}
99 120
100sub DESTROY { 121sub DESTROY {
101 my ($self) = @_; 122 my ($self) = @_;
102 123
103 $self->deactivate; 124 #$self->deactivate;
104} 125}
105 126
106package Crossfire::Client::Widget::Container; 127package Crossfire::Client::Widget::Bin;
107 128
108our @ISA = Crossfire::Client::Widget::; 129our @ISA = Crossfire::Client::Widget::;
109 130
110use SDL::OpenGL; 131sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->{expand} = $_[2] }
111
112sub add { $_[0]->{child} = $_[1] }
113sub get { $_[0]->{child} } 132sub get { $_[0]->{child} }
114 133
134sub remove {
135 my ($self, $chld) = @_;
136 delete $self->{child}
137 if $self->{child} == $chld;
138}
139
140sub size_request {
115sub 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}
116 149
117sub _draw { die "Containers can't be drawn!" } 150sub _draw {
151 my ($self) = @_;
118 152
153 $self->{child}->draw;
154}
155
119package Crossfire::Client::Widget::Window; 156package Crossfire::Client::Widget::Toplevel;
120 157
121our @ISA = Crossfire::Client::Widget::Container::; 158our @ISA = Crossfire::Client::Widget::;
122 159
123use SDL::OpenGL; 160use SDL::OpenGL;
124 161
125sub add { 162sub add {
126 my ($self, $chld) = @_; 163 my ($self, $chld) = @_;
164
165 push @{$self->{childs}}, $chld;
166 @{$self->{childs}} =
167 sort { $a->{z} <=> $b->{z} }
168 @{$self->{childs}};
169
170 $chld->set_parent ($self);
171 $chld->size_allocate ($chld->size_request);
172}
173
174sub remove {
175 my ($self, $chld) = @_;
176 @{$self->{childs}} =
177 sort { $a->{z} <=> $b->{z} }
178 grep { $_ && $_ != $_[0] }
179 @{$self->{childs}}
180}
181
182sub update {
183 my ($self) = @_;
184 ::refresh ();
185}
186
187sub _draw {
188 my ($self) = @_;
189
190 $_->draw for @{$self->{childs}};
191}
192
193package Crossfire::Client::Widget::Window;
194
195our @ISA = Crossfire::Client::Widget::Bin::;
196
197use SDL::OpenGL;
198
199sub add {
200 my ($self, $chld) = @_;
201 warn "ADD $chld\n";
127 $self->SUPER::add ($chld); 202 $self->SUPER::add ($chld);
203 $chld->set_parent ($self);
204}
205
206sub remove {
207 my ($self) = @_;
208 # TODO FIXME: removing a child from a window will crash, see render_chld
209 # $self->update;
210}
211
212sub update {
213 my ($self) = @_;
128 $self->render_chld; 214 $self->render_chld;
129} 215}
130 216
131sub render_chld { 217sub render_chld {
132 my ($self) = @_; 218 my ($self) = @_;
133 my $chld = $self->get; 219 my $chld = $self->get;
134 my ($w, $h) = $self->size_request; 220 my ($w, $h) = $self->size_request;
135 221
222 require Carp;
223 Carp::cluck "RENDERCHI $w $h";
224 warn "RENDERCHI $w $h\n";
136 $self->{texture} = 225 $self->{texture} =
137 Crossfire::Client::Texture->new_from_opengl ( 226 Crossfire::Client::Texture->new_from_opengl (
138 $w, $h, sub { $chld->draw } 227 $w, $h, sub { $chld->draw }
139 ); 228 );
140 $self->{texture}->upload; 229 $self->{texture}->upload;
141} 230}
142 231
143sub size_request { 232sub size_request {
144 my ($self) = @_; 233 my ($self) = @_;
145 my $chld = $self->get 234 ($self->w, $self->h)
146 or return (0, 0);
147 $chld->size_request
148} 235}
149 236
150sub _draw { 237sub size_allocate {
151 my ($self) = @_; 238 my ($self, $w, $h) = @_;
152 239
153 my ($w, $h) = $self->size_request;#TODO# use widht/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);
251
154 my $tex = $self->{texture} 252 my $tex = $self->{texture}
155 or return; 253 or return;
156 254
157 glEnable GL_BLEND; 255 glEnable GL_BLEND;
158 glEnable GL_TEXTURE_2D; 256 glEnable GL_TEXTURE_2D;
159 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 257 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
160 glBindTexture GL_TEXTURE_2D, $tex->{name}; 258 glBindTexture GL_TEXTURE_2D, $tex->{name};
161
162 glColor 1, 1, 1;
163 259
164 glBegin GL_QUADS; 260 glBegin GL_QUADS;
165 glTexCoord 0, 0; glVertex 0, 0; 261 glTexCoord 0, 0; glVertex 0, 0;
166 glTexCoord 0, 1; glVertex 0, $h; 262 glTexCoord 0, 1; glVertex 0, $h;
167 glTexCoord 1, 1; glVertex $w, $h; 263 glTexCoord 1, 1; glVertex $w, $h;
172 glDisable GL_TEXTURE_2D; 268 glDisable GL_TEXTURE_2D;
173} 269}
174 270
175package Crossfire::Client::Widget::Frame; 271package Crossfire::Client::Widget::Frame;
176 272
177our @ISA = Crossfire::Client::Widget::Container::; 273our @ISA = Crossfire::Client::Widget::Bin::;
178 274
179use SDL::OpenGL; 275use SDL::OpenGL;
180 276
181sub size_request { 277sub size_request {
182 my ($self) = @_; 278 my ($self) = @_;
183 my $chld = $self->get 279 my $chld = $self->get
184 or return (0, 0); 280 or return (0, 0);
281
282 $chld->move (2, 2);
283
185 map { $_ + 4 } $chld->size_request; 284 map { $_ + 4 } $chld->size_request;
186} 285}
187 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);
295}
296
188sub _draw { 297sub _draw {
189 my ($self) = @_; 298 my ($self) = @_;
190 299
191 my $chld = $self->get; 300 my $chld = $self->get;
192 301
193 my ($w, $h) = $chld->size_request; 302 my ($w, $h) = $chld->size_request;
194 303
195 glColor 1, 0, 0;
196 glBegin GL_QUADS; 304 glBegin GL_QUADS;
305 glColor 0, 0, 0;
197 glTexCoord 0, 0; glVertex 0 , 0; 306 glTexCoord 0, 0; glVertex 0 , 0;
198 glTexCoord 0, 1; glVertex 0 , $h + 4; 307 glTexCoord 0, 1; glVertex 0 , $h + 4;
199 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 308 glTexCoord 1, 1; glVertex $w + 4 , $h + 4;
200 glTexCoord 1, 0; glVertex $w + 4 , 0; 309 glTexCoord 1, 0; glVertex $w + 4 , 0;
201 glEnd; 310 glEnd;
202 311
203 glPushMatrix;
204 glTranslate (2, 2, 0);
205 $chld->draw; 312 $chld->draw;
206 glPopMatrix; 313}
314
315package Crossfire::Client::Widget::FancyFrame;
316
317our @ISA = Crossfire::Client::Widget::Frame::;
318
319use SDL::OpenGL;
320
321sub new {
322 my ($self, $theme) = @_;
323 $self = $self->SUPER::new;
324
325 $self->{txts} = [
326 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
327 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/
328 ];
329 $self
330}
331
332sub size_request {
333 my ($self) = @_;
334 my ($w, $h) = $self->get->size_request;
335
336 $h += $self->{txts}->[1]->{height};
337 $h += $self->{txts}->[4]->{height};
338 $w += $self->{txts}->[2]->{width};
339 $w += $self->{txts}->[3]->{width};
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);
358 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
359}
360
361sub _draw {
362 my ($self) = @_;
363
364 my ($w, $h) = ($self->w, $self->h);
365 my ($cw, $ch) = ($self->get->w, $self->get->h);
366
367 glEnable GL_BLEND;
368 glEnable GL_TEXTURE_2D;
369 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
370 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
371
372 my $top = $self->{txts}->[1];
373 glBindTexture GL_TEXTURE_2D, $top->{name};
374
375 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , 0;
377 glTexCoord 0, 1; glVertex 0 , $top->{height};
378 glTexCoord 1, 1; glVertex $w , $top->{height};
379 glTexCoord 1, 0; glVertex $w , 0;
380 glEnd;
381
382 my $left = $self->{txts}->[3];
383 glBindTexture GL_TEXTURE_2D, $left->{name};
384
385 glBegin GL_QUADS;
386 glTexCoord 0, 0; glVertex 0 , $top->{height};
387 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
388 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
389 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
390 glEnd;
391
392 my $right = $self->{txts}->[2];
393 glBindTexture GL_TEXTURE_2D, $right->{name};
394
395 glBegin GL_QUADS;
396 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
397 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
398 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
399 glTexCoord 1, 0; glVertex $w , $top->{height};
400 glEnd;
401
402 my $bottom = $self->{txts}->[4];
403 glBindTexture GL_TEXTURE_2D, $bottom->{name};
404
405 glBegin GL_QUADS;
406 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
407 glTexCoord 0, 1; glVertex 0 , $h;
408 glTexCoord 1, 1; glVertex $w , $h;
409 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
410 glEnd;
411
412 my $bg = $self->{txts}->[0];
413 glBindTexture GL_TEXTURE_2D, $bg->{name};
414 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
415 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
416 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
417
418 my $rep_x = $cw / $bg->{width};
419 my $rep_y = $ch / $bg->{height};
420
421 glBegin GL_QUADS;
422 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
423 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
424 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
425 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
426 glEnd;
427
428 glDisable GL_BLEND;
429 glDisable GL_TEXTURE_2D;
430
431 $self->get->draw;
432
207} 433}
208 434
209package Crossfire::Client::Widget::Table; 435package Crossfire::Client::Widget::Table;
210 436
211our @ISA = Crossfire::Client::Widget::Container::; 437our @ISA = Crossfire::Client::Widget::Bin::;
212 438
213use SDL::OpenGL; 439use SDL::OpenGL;
214 440
215sub add { 441sub add {
216 my ($self, $x, $y, $chld) = @_; 442 my ($self, $x, $y, $chld) = @_;
443 my $old_chld = $self->{childs}[$y][$x];
444
217 $self->{childs}[$y][$x] = $chld; 445 $self->{childs}[$y][$x] = $chld;
446 $chld->set_parent ($self);
447 $self->update;
218} 448}
219 449
220sub max_row_height { 450sub max_row_height {
221 my ($self, $row) = @_; 451 my ($self, $row) = @_;
222 452
272 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 502 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) {
273 my $x = 0; 503 my $x = 0;
274 504
275 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 505 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) {
276 506
277 glPushMatrix;
278 glTranslate ($x, $y, 0);#TODO#there must be no translate here, instead the widget must be moved
279 my $c = $self->{childs}->[$yi]->[$xi]; 507 my $c = $self->{childs}->[$yi]->[$xi];
508 if ($c) {
509 $c->move ($x, $y, 0); #TODO: Move to size_request
280 $c->draw if $c; 510 $c->draw if $c;
281 glPopMatrix; 511 }
282 512
283 $x += $self->max_col_width ($xi); 513 $x += $self->max_col_width ($xi);
284 } 514 }
285 515
286 $y += $self->max_row_height ($yi); 516 $y += $self->max_row_height ($yi);
287 } 517 }
288} 518}
289 519
290package Crossfire::Client::Widget::VBox; 520package Crossfire::Client::Widget::VBox;
291 521
292our @ISA = Crossfire::Client::Widget::Container::; 522our @ISA = Crossfire::Client::Widget::Bin::;
293 523
294use SDL::OpenGL; 524use SDL::OpenGL;
295 525
296sub add { 526sub add {
297 my ($self, $chld) = @_; 527 my ($self, $chld, $expand) = @_;
298 push @{$self->{childs}}, $chld; 528 push @{$self->{childs}}, $chld;
529 $chld->{expand} = $expand;
530 $chld->set_parent ($self);
531 $self->update;
299} 532}
300 533
301sub size_request { 534sub size_request {
302 my ($self) = @_; 535 my ($self) = @_;
303 536
309 } 542 }
310 543
311 return ($ws, $hs); 544 return ($ws, $hs);
312} 545}
313 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
314sub _draw { 588sub _draw {
315 my ($self) = @_; 589 my ($self) = @_;
316 590
317 my ($x, $y); 591 my ($x, $y);
318 for (@{$self->{childs} || []}) { 592 for (@{$self->{childs} || []}) {
319 glPushMatrix;
320 glTranslate (0, $y, 0);# see above TODO
321 $_->draw; 593 $_->draw;
322 glPopMatrix;
323 my ($w, $h) = $_->size_request;
324 $y += $h; 594 $y += $_->h;
325 } 595 }
326} 596}
327 597
328package Crossfire::Client::Widget::Label; 598package Crossfire::Client::Widget::Label;
329 599
330our @ISA = Crossfire::Client::Widget::; 600our @ISA = Crossfire::Client::Widget::;
331 601
332use SDL::OpenGL; 602use SDL::OpenGL;
333 603
334sub new { 604sub new {
335 my ($class, $x, $y, $z, $ttf, $text) = @_; 605 my ($class, $x, $y, $z, $height, $text) = @_;
336 606
607 # TODO: color, and make height, xyz etc. optional
337 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, ttf => $ttf); 608 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
338 609
339 $self->set_text ($text); 610 $self->set_text ($text);
340 611
341 $self 612 $self
342} 613}
343 614
344sub set_text { 615sub set_text {
345 my ($self, $text) = @_; 616 my ($self, $text) = @_;
617
618 $self->{text} = $text;
346 $self->{texture} = new_from_ttf Crossfire::Client::Texture $self->{ttf}, $self->{text} = $text; 619 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height};
620
621 $self->update;
347} 622}
348 623
349sub get_text { 624sub get_text {
350 my ($self, $text) = @_; 625 my ($self, $text) = @_;
626
351 $self->{text} 627 $self->{text}
352} 628}
353 629
354sub size_request { 630sub size_request {
355 my ($self) = @_; 631 my ($self) = @_;
365 641
366 my $tex = $self->{texture}; 642 my $tex = $self->{texture};
367 643
368 glEnable GL_BLEND; 644 glEnable GL_BLEND;
369 glEnable GL_TEXTURE_2D; 645 glEnable GL_TEXTURE_2D;
646 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
370 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
371 glBindTexture GL_TEXTURE_2D, $tex->{name}; 648 glBindTexture GL_TEXTURE_2D, $tex->{name};
372 649
373 glColor 1, 1, 1; 650 glColor 1, 0, 0, 1; # TODO color
374 651
375 glBegin GL_QUADS; 652 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , 0; 653 glTexCoord 0, 0; glVertex 0 , 0;
377 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 654 glTexCoord 0, 1; glVertex 0 , $tex->{height};
378 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 655 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
381 658
382 glDisable GL_BLEND; 659 glDisable GL_BLEND;
383 glDisable GL_TEXTURE_2D; 660 glDisable GL_TEXTURE_2D;
384} 661}
385 662
386package Crossfire::Client::Widget::TextView; 663package Crossfire::Client::Widget::TextEntry;
664
665our @ISA = Crossfire::Client::Widget::Label::;
666
667use SDL;
668use SDL::OpenGL;
669
670sub key_down {
671 my ($self, $ev) = @_;
672
673 my $mod = $ev->key_mod;
674 my $sym = $ev->key_sym;
675
676 $ev->set_unicode (1);
677 my $uni = $ev->key_unicode;
678
679 my $text = $self->get_text;
680
681 if ($sym == SDLK_BACKSPACE) {
682 substr $text, -1, 1, '';
683
684 } elsif ($uni) {
685 $text .= chr $uni;
686 }
687 $self->set_text ($text);
688}
689
690package Crossfire::Client::Widget::MapWidget;
387 691
388use strict; 692use strict;
389 693
390our @ISA = qw/Crossfire::Client::Widget/; 694use List::Util qw(min max);
391 695
696use SDL;
392use SDL::OpenGL; 697use SDL::OpenGL;
393use SDL::OpenGL::Constants; 698use SDL::OpenGL::Constants;
394 699
395sub add_line {
396 my ($self, $line) = @_;
397 push @{$self->{lines}}, $line;
398}
399
400sub _draw {
401 my ($self) = @_;
402
403}
404
405package Crossfire::Client::Widget::MapWidget;
406
407use strict;
408
409our @ISA = qw/Crossfire::Client::Widget/; 700our @ISA = Crossfire::Client::Widget::;
410
411use SDL;
412use SDL::OpenGL;
413use SDL::OpenGL::Constants;
414 701
415sub key_down { 702sub key_down {
416 print "MAPKEYDOWN\n"; 703 print "MAPKEYDOWN\n";
417} 704}
418 705
419sub key_up { 706sub key_up {
420} 707}
421 708
709sub size_request {
710
711}
712
713sub size_allocate {
714}
715
422sub _draw { 716sub _draw {
423 my ($self) = @_; 717 my ($self) = @_;
718
719 my $mx = $::CONN->{mapx};
720 my $my = $::CONN->{mapy};
721
722 my $map = $::CONN->{map};
723
724 my ($xofs, $yofs);
725
726 my $sw = 1 + int $::WIDTH / 32;
727 my $sh = 1 + int $::HEIGHT / 32;
728
729 if ($::CONN->{mapw} > $sw) {
730 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
731 } else {
732 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
733 }
734
735 if ($::CONN->{maph} > $sh) {
736 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
737 } else {
738 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
739 }
424 740
425 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
426 glEnable GL_BLEND; 742 glEnable GL_BLEND;
427 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 743 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
428 744
429 my $map = $::CONN->{map}; 745 my $sw4 = ($sw + 3) & ~3;
746 my $lighting = "\x00" x ($sw4 * $sh);
430 747
431 for my $x (0 .. int $::WIDTH / 32) { 748 for my $x (0 .. $sw - 1) {
432 for my $y (0 .. int $::HEIGHT / 32) { 749 for my $y (0 .. $sh - 1) {
433 750
434 my $cell = $map->[$x + $::CONN->{mapx}] 751 my $cell = $map->[$x + $xofs][$y + $yofs]
435 [$y + $::CONN->{mapy}]
436 or next; 752 or next;
437 753
438 my $darkness = $cell->[0] * (1 / 255); 754 my $darkness = $cell->[0] * (1 / 255);
439 if ($darkness < 0) { 755 if ($darkness < 0) {
440 glColor 0.3, 0.3, 0.3; 756 $darkness = 0.15;
441 } else {
442 glColor $darkness, $darkness, $darkness;
443 } 757 }
758 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
444 759
445 for my $num (grep $_, @$cell[1,2,3]) { 760 for my $num (grep $_, @$cell[1,2,3]) {
446 my $tex = $::CONN->{face}[$num]{texture} || next; 761 my $tex = $::CONN->{face}[$num]{texture} || next;
447 762
448 glBindTexture GL_TEXTURE_2D, $tex->{name}; 763 glBindTexture GL_TEXTURE_2D, $tex->{name};
460 glTexCoord 1, 0; glVertex $px + $w, $py; 775 glTexCoord 1, 0; glVertex $px + $w, $py;
461 glEnd; 776 glEnd;
462 } 777 }
463 } 778 }
464 } 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;
465 809
466 glDisable GL_TEXTURE_2D; 810 glDisable GL_TEXTURE_2D;
467 glDisable GL_BLEND; 811 glDisable GL_BLEND;
468} 812}
469 813
516 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 860 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
517 $::CONN->send ("command run_stop"); 861 $::CONN->send ("command run_stop");
518 } 862 }
519} 863}
520 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
5211; 9051;
522 906

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines