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.42 by root, Sun Apr 9 22:00:58 2006 UTC vs.
Revision 1.67 by root, Tue Apr 11 14:36:59 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use CFClient;
11 11
12our $FOCUS; # the widget with current focus 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
13 16
14# class methods for events 17# class methods for events
15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
16sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
17sub feed_sdl_button_down_event { } 26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
40}
41
18sub feed_sdl_button_up_event { } 42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51
52 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab;
55 $GRAB->update if $GRAB;
56 }
57}
58
59sub feed_sdl_motion_event {
60 my ($ev) = @_;
61 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
62
63 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
64
65 if ($widget != $HOVER) {
66 my $hover = $HOVER; $HOVER = $widget;
67
68 $hover->update if $hover;
69 $HOVER->update if $HOVER;
70 }
71
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73}
19 74
20sub new { 75sub new {
21 my $class = shift; 76 my $class = shift;
22 77
23 bless { @_ }, $class 78 bless {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
24} 84}
25 85
26sub move { 86sub move {
27 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
28 $self->{x} = $x; 88 $self->{x} = $x;
44 104
45 $self->{w} = $w; 105 $self->{w} = $w;
46 $self->{h} = $h; 106 $self->{h} = $h;
47} 107}
48 108
109# translate global koordinates to local coordinate system
110sub translate {
111 my ($self, $x, $y) = @_;
112
113 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
114}
115
49sub focus_in { 116sub focus_in {
50 my ($widget) = @_; 117 my ($self) = @_;
51 $FOCUS = $widget; 118
119 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus;
121 $FOCUS->update;
52} 122}
53 123
54sub focus_out { 124sub focus_out {
55 my ($widget) = @_; 125 my ($self) = @_;
56}
57 126
58sub key_down { 127 return unless $FOCUS == $self;
59 my ($widget, $sdlev) = @_;
60}
61 128
62sub key_up { 129 my $focus = $FOCUS; undef $FOCUS;
63 my ($widget, $sdlev) = @_; 130 $focus->update if $focus; #?
64} 131}
65 132
133sub mouse_motion { }
134sub button_up { }
66sub button_down { 135sub button_down { }
67 my ($widget, $sdlev) = @_; 136sub key_down { }
68} 137sub key_up { }
69 138
70sub button_up {
71 my ($widget, $sdlev) = @_;
72}
73
74sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
75sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
76sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
77sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
78sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
79 144
80sub draw { 145sub draw {
81 my ($self) = @_; 146 my ($self) = @_;
82 147
83 glPushMatrix; 148 glPushMatrix;
84 glTranslate $self->{x}, $self->{y}, 0; 149 glTranslate $self->{x}, $self->{y}, 0;
85 $self->_draw; 150 $self->_draw;
151 if ($self == $HOVER) {
152 glColor 1, 1, 1, 0.4;
153 glEnable GL_BLEND;
154 glBegin GL_QUADS;
155 glVertex 0 , 0;
156 glVertex $self->{w}, 0;
157 glVertex $self->{w}, $self->{h};
158 glVertex 0 , $self->{h};
159 glEnd;
160 glDisable GL_BLEND;
161 }
86 glPopMatrix; 162 glPopMatrix;
87} 163}
88 164
89sub _draw { 165sub _draw {
90 my ($self) = @_; 166 my ($self) = @_;
139 #$self->deactivate; 215 #$self->deactivate;
140} 216}
141 217
142############################################################################# 218#############################################################################
143 219
220package CFClient::Widget::Empty;
221
222our @ISA = CFClient::Widget::;
223
224sub size_request {
225 (0, 0)
226}
227
228sub draw { }
229
230#############################################################################
231
144package Crossfire::Client::Widget::Container; 232package CFClient::Widget::Container;
145 233
146our @ISA = Crossfire::Client::Widget::; 234our @ISA = CFClient::Widget::;
147 235
148sub new { 236sub new {
149 my ($class, @widgets) = @_; 237 my ($class, %arg) = @_;
150 238
239 my $children = delete $arg{children} || [];
240
151 my $self = $class->SUPER::new (children => []); 241 my $self = $class->SUPER::new (children => [], %arg);
152 $self->add ($_) for @widgets; 242 $self->add ($_) for @$children;
153 243
154 $self 244 $self
155} 245}
156 246
157sub add { 247sub add {
158 my ($self, $chld, $expand) = @_; 248 my ($self, $chld, $expand) = @_;
159 249
160 $chld->{expand} = $expand; 250 $chld->{expand} = $expand;
161 $chld->set_parent ($self); 251 $chld->set_parent ($self);
162 252
163 @{$self->{children}} = 253 $self->{children} = [
164 sort { $a->{z} <=> $b->{z} } 254 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 255 @{$self->{children}}, $chld
256 ];
166 257
167 $self->size_allocate ($self->{w}, $self->{h}); 258 $self->size_allocate ($self->{w}, $self->{h})
259 if $self->{w}; #TODO: check for "realised state"
168} 260}
169 261
170sub remove { 262sub remove {
171 my ($self, $widget) = @_; 263 my ($self, $widget) = @_;
172 264
176} 268}
177 269
178sub find_widget { 270sub find_widget {
179 my ($self, $x, $y) = @_; 271 my ($self, $x, $y) = @_;
180 272
273 $x -= $self->{x};
274 $y -= $self->{y};
275
181 my $res; 276 my $res;
182 277
183 for (@{ $self->{children} }) { 278 for (reverse @{ $self->{children} }) {
184 $res = $_->find_widget ($x, $y) 279 $res = $_->find_widget ($x, $y)
185 and return $res; 280 and return $res;
186 } 281 }
187 282
188 () 283 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
189}
190
191sub size_request {
192 my ($self) = @_;
193
194 my ($hs, $ws) = (0, 0);
195 for (@{$self->{children} || []}) {
196 my ($w, $h) = $_->size_request;
197 $hs += $h;
198 if ($ws < $w) { $ws = $w }
199 }
200
201 return ($ws, $hs);
202} 284}
203 285
204sub _draw { 286sub _draw {
205 my ($self) = @_; 287 my ($self) = @_;
206 288
207 $_->draw for @{$self->{children}}; 289 $_->draw for @{$self->{children}};
208} 290}
209 291
210############################################################################# 292#############################################################################
211 293
212package Crossfire::Client::Widget::Bin; 294package CFClient::Widget::Bin;
213 295
214our @ISA = Crossfire::Client::Widget::Container::; 296our @ISA = CFClient::Widget::Container::;
297
298sub new {
299 my ($class, %arg) = @_;
300
301 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::;
302
303 $class->SUPER::new (children => [$child], %arg)
304}
305
306sub add {
307 my ($self, $widget) = @_;
308
309 $self->{children} = [];
310
311 $self->SUPER::add ($widget);
312}
313
314sub remove {
315 my ($self, $widget) = @_;
316
317 $self->SUPER::remove ($widget);
318
319 $self->{children} = [new CFClient::Widget::Empty]
320 unless @{$self->{children}};
321}
215 322
216sub child { $_[0]->{children}[0] } 323sub child { $_[0]->{children}[0] }
217 324
218sub size_request { 325sub size_request {
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 326 $_[0]{children}[0]->size_request if $_[0]{children}[0];
227 if $self->{children}[0] 334 if $self->{children}[0]
228} 335}
229 336
230############################################################################# 337#############################################################################
231 338
232package Crossfire::Client::Widget::Toplevel; 339package CFClient::Widget::Window;
233 340
234our @ISA = Crossfire::Client::Widget::Container::; 341our @ISA = CFClient::Widget::Bin::;
342
343use SDL::OpenGL;
344
345sub new {
346 my ($class, %arg) = @_;
347
348 my $self = $class->SUPER::new (%arg);
349}
235 350
236sub update { 351sub update {
237 my ($self) = @_; 352 my ($self) = @_;
238 353
239 ::refresh (); 354 # we want to do this delayed...
240}
241
242sub add {
243 my ($self, $widget) = @_;
244
245 $self->SUPER::add ($widget);
246
247 $widget->size_allocate ($widget->size_request);
248}
249
250#############################################################################
251
252package Crossfire::Client::Widget::Window;
253
254our @ISA = Crossfire::Client::Widget::Bin::;
255
256use SDL::OpenGL;
257
258sub new {
259 my ($class, $x, $y, $z, $w, $h) = @_;
260
261 my $self = $class->SUPER::new;
262
263 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
264}
265
266sub update {
267 my ($self) = @_;
268
269 $self->render_chld; 355 $self->render_chld;
270 $self->SUPER::update; 356 $self->SUPER::update;
271} 357}
272 358
273sub render_chld { 359sub render_chld {
274 my ($self) = @_; 360 my ($self) = @_;
275 361
276 $self->{texture} = 362 $self->{texture} =
277 Crossfire::Client::Texture->new_from_opengl ( 363 CFClient::Texture->new_from_opengl (
278 $self->{w}, $self->{h}, sub { $self->child->draw } 364 $self->{w}, $self->{h}, sub { $self->child->draw }
279 ); 365 );
280} 366}
281 367
282sub size_allocate { 368sub size_allocate {
299 or return; 385 or return;
300 386
301 glEnable GL_BLEND; 387 glEnable GL_BLEND;
302 glEnable GL_TEXTURE_2D; 388 glEnable GL_TEXTURE_2D;
303 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 389 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
304 glBindTexture GL_TEXTURE_2D, $tex->{name};
305 390
306 glBegin GL_QUADS; 391 $tex->draw_quad (0, 0, $w, $h);
307 glTexCoord 0, 0; glVertex 0, 0;
308 glTexCoord 0, 1; glVertex 0, $h;
309 glTexCoord 1, 1; glVertex $w, $h;
310 glTexCoord 1, 0; glVertex $w, 0;
311 glEnd;
312 392
313 glDisable GL_BLEND; 393 glDisable GL_BLEND;
314 glDisable GL_TEXTURE_2D; 394 glDisable GL_TEXTURE_2D;
315} 395}
316 396
317############################################################################# 397#############################################################################
318 398
319package Crossfire::Client::Widget::Frame; 399package CFClient::Widget::Frame;
320 400
321our @ISA = Crossfire::Client::Widget::Bin::; 401our @ISA = CFClient::Widget::Bin::;
322 402
323use SDL::OpenGL; 403use SDL::OpenGL;
324 404
325sub size_request { 405sub size_request {
326 my ($self) = @_; 406 my ($self) = @_;
349 429
350 my ($w, $h) = $chld->size_request; 430 my ($w, $h) = $chld->size_request;
351 431
352 glBegin GL_QUADS; 432 glBegin GL_QUADS;
353 glColor 0, 0, 0; 433 glColor 0, 0, 0;
354 glTexCoord 0, 0; glVertex 0 , 0; 434 glVertex 0 , 0;
355 glTexCoord 0, 1; glVertex 0 , $h + 4; 435 glVertex 0 , $h + 4;
356 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 436 glVertex $w + 4 , $h + 4;
357 glTexCoord 1, 0; glVertex $w + 4 , 0; 437 glVertex $w + 4 , 0;
358 glEnd; 438 glEnd;
359 439
360 $chld->draw; 440 $chld->draw;
361} 441}
362 442
363############################################################################# 443#############################################################################
364 444
365package Crossfire::Client::Widget::FancyFrame; 445package CFClient::Widget::FancyFrame;
366 446
367our @ISA = Crossfire::Client::Widget::Bin::; 447our @ISA = CFClient::Widget::Bin::;
368 448
369use SDL::OpenGL; 449use SDL::OpenGL;
370 450
371my @tex = 451my @tex =
372 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 452 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
373 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 453 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
374 454
375sub size_request { 455sub size_request {
376 my ($self) = @_; 456 my ($self) = @_;
377 457
395 $w -= $tex[2]->{width}; 475 $w -= $tex[2]->{width};
396 $w -= $tex[3]->{width}; 476 $w -= $tex[3]->{width};
397 477
398 $h = $h < 0 ? 0 : $h; 478 $h = $h < 0 ? 0 : $h;
399 $w = $w < 0 ? 0 : $w; 479 $w = $w < 0 ? 0 : $w;
400 warn "CHILD:$w $h\n"; 480
481 my $child = $self->child;
482
401 $self->child->size_allocate ($w, $h); 483 $child->size_allocate ($w, $h);
402 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 484 $child->move ($tex[3]->{width}, $tex[1]->{height});
403} 485}
404 486
405sub _draw { 487sub _draw {
406 my ($self) = @_; 488 my ($self) = @_;
407 489
408 my ($w, $h) = ($self->w, $self->h); 490 my ($w, $h) = ($self->{w}, $self->{h});
409 my ($cw, $ch) = ($self->child->w, $self->child->h); 491 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
410 492
411 glEnable GL_BLEND; 493 glEnable GL_BLEND;
412 glEnable GL_TEXTURE_2D; 494 glEnable GL_TEXTURE_2D;
413 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 495 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
414 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
415 497
416 my $top = $tex[1]; 498 my $top = $tex[1];
417 glBindTexture GL_TEXTURE_2D, $top->{name}; 499 $top->draw_quad (0, 0, $w, $top->{height});
418
419 glBegin GL_QUADS;
420 glTexCoord 0, 0; glVertex 0 , 0;
421 glTexCoord 0, 1; glVertex 0 , $top->{height};
422 glTexCoord 1, 1; glVertex $w , $top->{height};
423 glTexCoord 1, 0; glVertex $w , 0;
424 glEnd;
425 500
426 my $left = $tex[3]; 501 my $left = $tex[3];
427 glBindTexture GL_TEXTURE_2D, $left->{name}; 502 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
428
429 glBegin GL_QUADS;
430 glTexCoord 0, 0; glVertex 0 , $top->{height};
431 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
432 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
433 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
434 glEnd;
435 503
436 my $right = $tex[2]; 504 my $right = $tex[2];
437 glBindTexture GL_TEXTURE_2D, $right->{name}; 505 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
438
439 glBegin GL_QUADS;
440 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
441 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
442 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
443 glTexCoord 1, 0; glVertex $w , $top->{height};
444 glEnd;
445 506
446 my $bottom = $tex[4]; 507 my $bottom = $tex[4];
447 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 508 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
448
449 glBegin GL_QUADS;
450 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
451 glTexCoord 0, 1; glVertex 0 , $h;
452 glTexCoord 1, 1; glVertex $w , $h;
453 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
454 glEnd;
455 509
456 my $bg = $tex[0]; 510 my $bg = $tex[0];
457 glBindTexture GL_TEXTURE_2D, $bg->{name}; 511 glBindTexture GL_TEXTURE_2D, $bg->{name};
458 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 512 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
459 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 513 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
460 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 514 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
461 515
462 my $rep_x = $cw / $bg->{width}; 516 my $rep_x = $cw / $bg->{width};
463 my $rep_y = $ch / $bg->{height}; 517 my $rep_y = $ch / $bg->{height};
464 518
465 glBegin GL_QUADS; 519 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
466 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
467 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
468 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
469 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
470 glEnd;
471 520
472 glDisable GL_BLEND; 521 glDisable GL_BLEND;
473 glDisable GL_TEXTURE_2D; 522 glDisable GL_TEXTURE_2D;
474 523
475 $self->child->draw; 524 $self->child->draw;
476 525
477} 526}
478 527
479############################################################################# 528#############################################################################
480 529
481package Crossfire::Client::Widget::Table; 530package CFClient::Widget::Table;
482 531
483our @ISA = Crossfire::Client::Widget::Bin::; 532our @ISA = CFClient::Widget::Bin::;
484 533
485use SDL::OpenGL; 534use SDL::OpenGL;
486 535
487sub add { 536sub add {
488 my ($self, $x, $y, $chld) = @_; 537 my ($self, $x, $y, $chld) = @_;
563 } 612 }
564} 613}
565 614
566############################################################################# 615#############################################################################
567 616
568package Crossfire::Client::Widget::VBox; 617package CFClient::Widget::VBox;
569 618
570our @ISA = Crossfire::Client::Widget::Container::; 619our @ISA = CFClient::Widget::Container::;
571 620
572use SDL::OpenGL; 621use SDL::OpenGL;
622
623sub size_request {
624 my ($self) = @_;
625
626 my @alloc = map [$_->size_request], @{$self->{children}};
627
628 (
629 (List::Util::max map $_->[0], @alloc),
630 (List::Util::sum map $_->[1], @alloc),
631 )
632}
573 633
574sub size_allocate { 634sub size_allocate {
575 my ($self, $w, $h) = @_; 635 my ($self, $w, $h) = @_;
576 636
577 $self->w ($w); 637 $self->w ($w);
610 $y += $h; 670 $y += $h;
611 } 671 }
612 } 672 }
613} 673}
614 674
615sub _draw {
616 my ($self) = @_;
617
618 my ($x, $y);
619 for (@{$self->{children} || []}) {
620 $_->draw;
621 $y += $_->h;
622 }
623}
624
625############################################################################# 675#############################################################################
626 676
627package Crossfire::Client::Widget::Label; 677package CFClient::Widget::Label;
628 678
629our @ISA = Crossfire::Client::Widget::; 679our @ISA = CFClient::Widget::;
630 680
631use SDL::OpenGL; 681use SDL::OpenGL;
632 682
633sub new { 683sub new {
634 my ($class, $x, $y, $z, $height, $text) = @_; 684 my ($class, %arg) = @_;
635 685
636 # TODO: color, and make height, xyz etc. optional 686 # TODO: color, and make height, xyz etc. optional
637 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 687 my $self = $class->SUPER::new (
688 color => [1, 1, 1],
689 height => $::FONTSIZE,
690 text => "",
691 layout => new CFClient::Layout,
692 %arg
693 );
638 694
639 $self->set_text ($text); 695 $self->set_text ($self->{text});
640 696
641 $self 697 $self
642} 698}
643 699
644sub set_text { 700sub set_text {
645 my ($self, $text) = @_; 701 my ($self, $text) = @_;
646 702
647 $self->{text} = $text; 703 $self->{text} = $text;
648 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 704 $self->{layout}->set_markup ($text);
649 705
650 $self->update; 706 delete $self->{texture};
651} 707}
652 708
653sub get_text { 709sub get_text {
654 my ($self, $text) = @_; 710 my ($self, $text) = @_;
655 711
657} 713}
658 714
659sub size_request { 715sub size_request {
660 my ($self) = @_; 716 my ($self) = @_;
661 717
662 ( 718 $self->{layout}->set_width;
719 $self->{layout}->set_height ($self->{height});
720 $self->{layout}->size
721# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
722# (
663 $self->{texture}{width}, 723# $self->{texture}{width},
664 $self->{texture}{height}, 724# $self->{texture}{height},
665 ) 725# )
726# } else {
727# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
728#
729# ($w, $h)
730# }
731}
732
733sub size_allocate {
734 my ($self, $w, $h) = @_;
735
736 $self->SUPER::size_allocate ($w, $h);
737 delete $self->{texture};
666} 738}
667 739
668sub _draw { 740sub _draw {
669 my ($self) = @_; 741 my ($self) = @_;
670 742
671 my $tex = $self->{texture}; 743 my $tex = $self->{texture} ||= do {
744 $self->{layout}->set_width ($self->{w});
745 new_from_layout CFClient::Texture $self->{layout};
746 };
672 747
673 glEnable GL_BLEND; 748 glEnable GL_BLEND;
674 glEnable GL_TEXTURE_2D; 749 glEnable GL_TEXTURE_2D;
675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 750 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 751 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
677 glBindTexture GL_TEXTURE_2D, $tex->{name};
678 752
679 glColor 1, 0, 0, 1; # TODO color 753 glColor @{$self->{color}};
680 754
681 glBegin GL_QUADS; 755 $tex->draw_quad (0, 0);
682 glTexCoord 0, 0; glVertex 0 , 0;
683 glTexCoord 0, 1; glVertex 0 , $tex->{height};
684 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
685 glTexCoord 1, 0; glVertex $tex->{width}, 0;
686 glEnd;
687 756
688 glDisable GL_BLEND; 757 glDisable GL_BLEND;
689 glDisable GL_TEXTURE_2D; 758 glDisable GL_TEXTURE_2D;
690} 759}
691 760
692############################################################################# 761#############################################################################
693 762
694package Crossfire::Client::Widget::TextEntry; 763package CFClient::Widget::Entry;
695 764
696our @ISA = Crossfire::Client::Widget::Label::; 765our @ISA = CFClient::Widget::Label::;
697 766
698use SDL; 767use SDL;
699use SDL::OpenGL; 768use SDL::OpenGL;
700 769
701sub key_down { 770sub key_down {
702 my ($self, $ev) = @_; 771 my ($self, $ev) = @_;
703 772
704 my $mod = $ev->key_mod; 773 my $mod = $ev->key_mod;
705 my $sym = $ev->key_sym; 774 my $sym = $ev->key_sym;
706 775
707 $ev->set_unicode (1);
708 my $uni = $ev->key_unicode; 776 my $uni = $ev->key_unicode;
709 777
710 my $text = $self->get_text; 778 my $text = $self->get_text;
711 779
712 if ($sym == SDLK_BACKSPACE) { 780 if ($sym == SDLK_BACKSPACE) {
713 substr $text, -1, 1, ''; 781 substr $text, -1, 1, '';
714
715 } elsif ($uni) { 782 } elsif ($uni) {
716 $text .= chr $uni; 783 $text .= chr $uni;
717 } 784 }
785
718 $self->set_text ($text); 786 $self->set_text ($text);
719} 787}
720 788
721############################################################################# 789sub button_down {
790 my ($self, $ev) = @_;
722 791
792 $self->focus_in;
793}
794
795sub mouse_motion {
796 my ($self, $ev, $x, $y) = @_;
797 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
798}
799
800sub _draw {
801 my ($self) = @_;
802
803 if ($FOCUS == $self) {
804 glColor 1, 1, 1;
805 } else {
806 glColor 0.7, 0.7, 0.7;
807 }
808
809 glBegin GL_QUADS;
810 glVertex 0 , 0;
811 glVertex 0 , $self->{h} - 1;
812 glVertex $self->{w} - 1, $self->{h} - 1;
813 glVertex $self->{w} - 1, 0;
814 glEnd;
815
816 $self->SUPER::_draw;
817}
818
819#############################################################################
820
723package Crossfire::Client::Widget::MapWidget; 821package CFClient::Widget::MapWidget;
724 822
725use strict; 823use strict;
726 824
727use List::Util qw(min max); 825use List::Util qw(min max);
728 826
729use SDL; 827use SDL;
730use SDL::OpenGL; 828use SDL::OpenGL;
731use SDL::OpenGL::Constants; 829use SDL::OpenGL::Constants;
732 830
733our @ISA = Crossfire::Client::Widget::; 831our @ISA = CFClient::Widget::;
832
833sub new {
834 my $class = shift;
835
836 $class->SUPER::new (
837 z => -1,
838 list => (glGenLists 1),
839 @_
840 )
841}
734 842
735sub key_down { 843sub key_down {
736 print "MAPKEYDOWN\n"; 844 print "MAPKEYDOWN\n";
737} 845}
738 846
739sub key_up { 847sub key_up {
740} 848}
741 849
742sub size_request { 850sub size_request {
743 851 (
852 1 + int $::WIDTH / 32,
853 1 + int $::HEIGHT / 32,
854 )
744} 855}
745 856
746sub size_allocate { 857sub update {
858 my ($self) = @_;
859
860 $self->{need_update} = 1;
747} 861}
748 862
749sub _draw { 863sub _draw {
750 my ($self) = @_; 864 my ($self) = @_;
751 865
866 if (delete $self->{need_update}) {
867 glNewList $self->{list}, GL_COMPILE;
868
752 my $mx = $::CONN->{mapx}; 869 my $mx = $::CONN->{mapx};
753 my $my = $::CONN->{mapy}; 870 my $my = $::CONN->{mapy};
754 871
755 my $map = $::CONN->{map}; 872 my $map = $::CONN->{map};
756 873
757 my ($xofs, $yofs); 874 my ($xofs, $yofs);
758 875
759 my $sw = 1 + int $::WIDTH / 32; 876 my $sw = 1 + int $::WIDTH / 32;
760 my $sh = 1 + int $::HEIGHT / 32; 877 my $sh = 1 + int $::HEIGHT / 32;
761 878
762 if ($::CONN->{mapw} > $sw) { 879 if ($::CONN->{mapw} > $sw) {
763 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 880 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
764 } else { 881 } else {
765 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 882 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
766 } 883 }
767 884
768 if ($::CONN->{maph} > $sh) { 885 if ($::CONN->{maph} > $sh) {
769 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 886 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
770 } else { 887 } else {
771 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 888 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
772 } 889 }
773 890
774 glEnable GL_TEXTURE_2D; 891 glEnable GL_TEXTURE_2D;
775 glEnable GL_BLEND; 892 glEnable GL_BLEND;
776 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 893 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
777 894
778 my $sw4 = ($sw + 3) & ~3; 895 my $sw4 = ($sw + 3) & ~3;
779 my $lighting = "\x00" x ($sw4 * $sh); 896 my $darkness = "\x00" x ($sw4 * $sh);
780 897
781 for my $x (0 .. $sw - 1) { 898 for my $x (0 .. $sw - 1) {
899 my $row = $map->[$x + $xofs];
782 for my $y (0 .. $sh - 1) { 900 for my $y (0 .. $sh - 1) {
783 901
784 my $cell = $map->[$x + $xofs][$y + $yofs] 902 my $cell = $row->[$y + $yofs]
785 or next; 903 or next;
786 904
787 my $darkness = $cell->[0] * (1 / 255); 905 my $dark = $cell->[0];
788 if ($darkness < 0) { 906 if ($dark < 0) {
789 $darkness = 0.15; 907 substr $darkness, $y * $sw4 + $x, 1, chr 224;
908 } else {
909 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
790 } 910 }
791 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
792 911
793 for my $num (grep $_, @$cell[1,2,3]) { 912 for my $num (grep $_, @$cell[1,2,3]) {
794 my $tex = $::CONN->{face}[$num]{texture} || next; 913 my $tex = $::CONN->{face}[$num]{texture} || next;
795 914
796 glBindTexture GL_TEXTURE_2D, $tex->{name};
797
798 my $w = $tex->{width}; 915 my $w = $tex->{width};
799 my $h = $tex->{height}; 916 my $h = $tex->{height};
800 917
801 my $px = ($x + 1) * 32 - $w; 918 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
802 my $py = ($y + 1) * 32 - $h;
803
804 glBegin GL_QUADS;
805 glTexCoord 0, 0; glVertex $px , $py;
806 glTexCoord 0, 1; glVertex $px , $py + $h;
807 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
808 glTexCoord 1, 0; glVertex $px + $w, $py;
809 glEnd; 919 }
810 } 920 }
811 } 921 }
812 }
813 922
814# if (1) { # higher quality darkness 923# if (1) { # higher quality darkness
815# $lighting =~ s/(.)/$1$1$1/gs; 924# $lighting =~ s/(.)/$1$1$1/gs;
816# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 925# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
817# 926#
819# 928#
820# $lighting = $pb->get_pixels; 929# $lighting = $pb->get_pixels;
821# $lighting =~ s/(.)../$1/gs; 930# $lighting =~ s/(.)../$1/gs;
822# } 931# }
823 932
824 $lighting = new Crossfire::Client::Texture
825 width => $sw4,
826 height => $sh,
827 data => $lighting,
828 internalformat => GL_ALPHA4,
829 format => GL_ALPHA;
830
831 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 933 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
832 glColor 0, 0, 0, 0.75;
833 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 934 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
834 glBindTexture GL_TEXTURE_2D, $lighting->{name};
835 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
836 glBegin GL_QUADS;
837 glTexCoord 0, 0; glVertex 0 , 0;
838 glTexCoord 0, 1; glVertex 0 , $sh * 32;
839 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
840 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
841 glEnd;
842 935
936 $darkness = new CFClient::Texture
937 width => $sw4,
938 height => $sh,
939 data => $darkness,
940 internalformat => GL_ALPHA,
941 format => GL_ALPHA;
942
943 glColor 0.45, 0.45, 0.45, 1;
944 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
945
843 glDisable GL_TEXTURE_2D; 946 glDisable GL_TEXTURE_2D;
844 glDisable GL_BLEND; 947 glDisable GL_BLEND;
948
949 glEndList;
950 }
951
952 glCallList $self->{list};
845} 953}
846 954
847my %DIR = ( 955my %DIR = (
848 SDLK_KP8, [1, "north"], 956 SDLK_KP8, [1, "north"],
849 SDLK_KP9, [2, "northeast"], 957 SDLK_KP9, [2, "northeast"],
895 } 1003 }
896} 1004}
897 1005
898############################################################################# 1006#############################################################################
899 1007
900package Crossfire::Client::Widget::Animator; 1008package CFClient::Widget::Animator;
901 1009
902use SDL::OpenGL; 1010use SDL::OpenGL;
903 1011
904our @ISA = Crossfire::Client::Widget::Bin::; 1012our @ISA = CFClient::Widget::Bin::;
905 1013
906sub moveto { 1014sub moveto {
907 my ($self, $x, $y) = @_; 1015 my ($self, $x, $y) = @_;
908 1016
909 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1017 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
910 $self->{speed} = 0.2; 1018 $self->{speed} = 0.001;
911 $self->{time} = 1; 1019 $self->{time} = 1;
912 1020
913 ::animation_start $self; 1021 ::animation_start $self;
914} 1022}
915 1023
930 1038
931sub _draw { 1039sub _draw {
932 my ($self) = @_; 1040 my ($self) = @_;
933 1041
934 glPushMatrix; 1042 glPushMatrix;
935 glRotate $self->{time} * 10000, 0, 1, 0; 1043 glRotate $self->{time} * 1000, 0, 1, 0;
936 $self->{children}[0]->draw; 1044 $self->{children}[0]->draw;
937 glPopMatrix; 1045 glPopMatrix;
938} 1046}
939 1047
9401; 1048#############################################################################
941 1049
1050package CFClient::Widget::Toplevel;
1051
1052our @ISA = CFClient::Widget::Container::;
1053
1054sub size_request {
1055 ($::WIDTH, $::HEIGHT)
1056}
1057
1058sub size_allocate {
1059 my ($self, $w, $h) = @_;
1060
1061 $self->SUPER::size_allocate ($w, $h);
1062
1063 $_->size_allocate ($_->size_request)
1064 for @{$self->{children}};
1065}
1066
1067sub translate {
1068 my ($self, $x, $y) = @_;
1069
1070 ($x, $y)
1071}
1072
1073sub update {
1074 my ($self) = @_;
1075
1076 $self->size_allocate ($self->size_request);
1077 ::refresh ();
1078}
1079
1080sub add {
1081 my ($self, $widget) = @_;
1082
1083 $self->SUPER::add ($widget);
1084
1085 $widget->size_allocate ($widget->size_request);
1086}
1087
1088sub draw {
1089 my ($self) = @_;
1090
1091 $self->_draw;
1092}
1093
1094#############################################################################
1095
1096package CFClient::Widget;
1097
1098$TOPLEVEL = new CFClient::Widget::Toplevel;
1099
11001
1101

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines