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.41 by root, Sun Apr 9 21:50:21 2006 UTC vs.
Revision 1.65 by root, Tue Apr 11 14:04:27 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
144package Crossfire::Client::Widget::Container; 220package CFClient::Widget::Container;
145 221
146our @ISA = Crossfire::Client::Widget::; 222our @ISA = CFClient::Widget::;
147 223
148sub new { 224sub new {
149 my ($class, @widgets) = @_; 225 my ($class, %arg) = @_;
150 226
227 my $children = delete $arg{children} || [];
228
151 my $self = $class->SUPER::new (children => []); 229 my $self = $class->SUPER::new (children => [], %arg);
152 $self->add ($_) for @widgets; 230 $self->add ($_) for @$children;
153 231
154 $self 232 $self
155} 233}
156 234
157sub add { 235sub add {
162 240
163 @{$self->{children}} = 241 @{$self->{children}} =
164 sort { $a->{z} <=> $b->{z} } 242 sort { $a->{z} <=> $b->{z} }
165 @{$self->{children}}, $chld; 243 @{$self->{children}}, $chld;
166 244
167 $self->size_allocate ($self->{w}, $self->{h}); 245 $self->size_allocate ($self->{w}, $self->{h})
246 if $self->{w}; #TODO: check for "realised state"
168} 247}
169 248
170sub remove { 249sub remove {
171 my ($self, $widget) = @_; 250 my ($self, $widget) = @_;
172 251
176} 255}
177 256
178sub find_widget { 257sub find_widget {
179 my ($self, $x, $y) = @_; 258 my ($self, $x, $y) = @_;
180 259
260 $x -= $self->{x};
261 $y -= $self->{y};
262
181 my $res; 263 my $res;
182 264
183 for (@{ $self->{children} }) { 265 for (reverse @{ $self->{children} }) {
184 $res = $_->find_widget ($x, $y) 266 $res = $_->find_widget ($x, $y)
185 and return $res; 267 and return $res;
186 } 268 }
187 269
188 () 270 $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} 271}
203 272
204sub _draw { 273sub _draw {
205 my ($self) = @_; 274 my ($self) = @_;
206 275
207 $_->draw for @{$self->{children}}; 276 $_->draw for @{$self->{children}};
208} 277}
209 278
210############################################################################# 279#############################################################################
211 280
212package Crossfire::Client::Widget::Bin; 281package CFClient::Widget::Bin;
213 282
214our @ISA = Crossfire::Client::Widget::Container::; 283our @ISA = CFClient::Widget::Container::;
215 284
216sub child { $_[0]->{children}[0] } 285sub child { $_[0]->{children}[0] }
217 286
218sub size_request { 287sub size_request {
219 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 288 $_[0]{children}[0]->size_request if $_[0]{children}[0];
220} 289}
221 290
222sub size_allocate { 291sub size_allocate {
223 my ($self, $w, $h) = @_; 292 my ($self, $w, $h) = @_;
293
224 $self->SUPER::size_allocate ($w, $h); 294 $self->SUPER::size_allocate ($w, $h);
225 $self->{children}[0]->size_allocate ($w, $h) 295 $self->{children}[0]->size_allocate ($w, $h)
226 if $self->{children}[0] 296 if $self->{children}[0]
227} 297}
228 298
229############################################################################# 299#############################################################################
230 300
231package Crossfire::Client::Widget::Toplevel; 301package CFClient::Widget::Window;
232 302
233our @ISA = Crossfire::Client::Widget::Container::; 303our @ISA = CFClient::Widget::Bin::;
304
305use SDL::OpenGL;
306
307sub new {
308 my ($class, %arg) = @_;
309
310 my $self = $class->SUPER::new (%arg);
311}
234 312
235sub update { 313sub update {
236 my ($self) = @_; 314 my ($self) = @_;
237 315
238 ::refresh (); 316 # we want to do this delayed...
239}
240
241#############################################################################
242
243package Crossfire::Client::Widget::Window;
244
245our @ISA = Crossfire::Client::Widget::Bin::;
246
247use SDL::OpenGL;
248
249sub add {
250 my ($self, $chld) = @_;
251 warn "ADD $chld\n";
252 $self->SUPER::add ($chld);
253 $chld->set_parent ($self);
254}
255
256sub remove {
257 my ($self) = @_;
258 # TODO FIXME: removing a child from a window will crash, see render_chld
259 # $self->update;
260}
261
262sub update {
263 my ($self) = @_;
264 $self->render_chld; 317 $self->render_chld;
318 $self->SUPER::update;
265} 319}
266 320
267sub render_chld { 321sub render_chld {
268 my ($self) = @_; 322 my ($self) = @_;
269 my $chld = $self->get;
270 my ($w, $h) = $self->size_request;
271 323
272 require Carp;
273 Carp::cluck "RENDERCHI $w $h";
274 warn "RENDERCHI $w $h\n";
275 $self->{texture} = 324 $self->{texture} =
276 Crossfire::Client::Texture->new_from_opengl ( 325 CFClient::Texture->new_from_opengl (
277 $w, $h, sub { $chld->draw } 326 $self->{w}, $self->{h}, sub { $self->child->draw }
278 ); 327 );
279 $self->{texture}->upload;
280}
281
282sub size_request {
283 my ($self) = @_;
284 ($self->w, $self->h)
285} 328}
286 329
287sub size_allocate { 330sub size_allocate {
288 my ($self, $w, $h) = @_; 331 my ($self, $w, $h) = @_;
289 332
290 $self->w ($w); 333 $self->{w} = $w;
291 $self->h ($h); 334 $self->{h} = $h;
335
292 $self->get->size_allocate ($w, $h); 336 $self->child->size_allocate ($w, $h);
293 337
294 $self->update; #TODO: Move this to the size_request event propably? 338 $self->render_chld;
295} 339}
296 340
297sub _draw { 341sub _draw {
298 my ($self) = @_; 342 my ($self) = @_;
299 343
303 or return; 347 or return;
304 348
305 glEnable GL_BLEND; 349 glEnable GL_BLEND;
306 glEnable GL_TEXTURE_2D; 350 glEnable GL_TEXTURE_2D;
307 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 351 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
308 glBindTexture GL_TEXTURE_2D, $tex->{name};
309 352
310 glBegin GL_QUADS; 353 $tex->draw_quad (0, 0, $w, $h);
311 glTexCoord 0, 0; glVertex 0, 0;
312 glTexCoord 0, 1; glVertex 0, $h;
313 glTexCoord 1, 1; glVertex $w, $h;
314 glTexCoord 1, 0; glVertex $w, 0;
315 glEnd;
316 354
317 glDisable GL_BLEND; 355 glDisable GL_BLEND;
318 glDisable GL_TEXTURE_2D; 356 glDisable GL_TEXTURE_2D;
319} 357}
320 358
321############################################################################# 359#############################################################################
322 360
323package Crossfire::Client::Widget::Frame; 361package CFClient::Widget::Frame;
324 362
325our @ISA = Crossfire::Client::Widget::Bin::; 363our @ISA = CFClient::Widget::Bin::;
326 364
327use SDL::OpenGL; 365use SDL::OpenGL;
328 366
329sub size_request { 367sub size_request {
330 my ($self) = @_; 368 my ($self) = @_;
336 map { $_ + 4 } $chld->size_request; 374 map { $_ + 4 } $chld->size_request;
337} 375}
338 376
339sub size_allocate { 377sub size_allocate {
340 my ($self, $w, $h) = @_; 378 my ($self, $w, $h) = @_;
341 379
342 $self->SUPER::size_allocate ($w, $h); 380 $self->{w} = $w;
381 $self->{h} = $h;
343 382
344 $self->child->size_allocate ($w - 4, $h - 4); 383 $self->child->size_allocate ($w - 4, $h - 4);
345 $self->child->move (2, 2); 384 $self->child->move (2, 2);
346} 385}
347 386
352 391
353 my ($w, $h) = $chld->size_request; 392 my ($w, $h) = $chld->size_request;
354 393
355 glBegin GL_QUADS; 394 glBegin GL_QUADS;
356 glColor 0, 0, 0; 395 glColor 0, 0, 0;
357 glTexCoord 0, 0; glVertex 0 , 0; 396 glVertex 0 , 0;
358 glTexCoord 0, 1; glVertex 0 , $h + 4; 397 glVertex 0 , $h + 4;
359 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 398 glVertex $w + 4 , $h + 4;
360 glTexCoord 1, 0; glVertex $w + 4 , 0; 399 glVertex $w + 4 , 0;
361 glEnd; 400 glEnd;
362 401
363 $chld->draw; 402 $chld->draw;
364} 403}
365 404
366############################################################################# 405#############################################################################
367 406
368package Crossfire::Client::Widget::FancyFrame; 407package CFClient::Widget::FancyFrame;
369 408
370our @ISA = Crossfire::Client::Widget::Bin::; 409our @ISA = CFClient::Widget::Bin::;
371 410
372use SDL::OpenGL; 411use SDL::OpenGL;
373 412
374my @tex = 413my @tex =
375 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 414 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
376 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 415 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
377 416
378sub size_request { 417sub size_request {
379 my ($self) = @_; 418 my ($self) = @_;
380 419
398 $w -= $tex[2]->{width}; 437 $w -= $tex[2]->{width};
399 $w -= $tex[3]->{width}; 438 $w -= $tex[3]->{width};
400 439
401 $h = $h < 0 ? 0 : $h; 440 $h = $h < 0 ? 0 : $h;
402 $w = $w < 0 ? 0 : $w; 441 $w = $w < 0 ? 0 : $w;
403 warn "CHILD:$w $h\n"; 442
404 $self->child->size_allocate ($w, $h); 443 $self->child->size_allocate ($w, $h);
405 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 444 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
406} 445}
407 446
408sub _draw { 447sub _draw {
409 my ($self) = @_; 448 my ($self) = @_;
410 449
411 my ($w, $h) = ($self->w, $self->h); 450 my ($w, $h) = ($self->{w}, $self->{h});
412 my ($cw, $ch) = ($self->child->w, $self->child->h); 451 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
413 452
414 glEnable GL_BLEND; 453 glEnable GL_BLEND;
415 glEnable GL_TEXTURE_2D; 454 glEnable GL_TEXTURE_2D;
416 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 455 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
418 457
419 my $top = $tex[1]; 458 my $top = $tex[1];
420 glBindTexture GL_TEXTURE_2D, $top->{name}; 459 $top->draw_quad (0, 0, $w, $top->{height});
421
422 glBegin GL_QUADS;
423 glTexCoord 0, 0; glVertex 0 , 0;
424 glTexCoord 0, 1; glVertex 0 , $top->{height};
425 glTexCoord 1, 1; glVertex $w , $top->{height};
426 glTexCoord 1, 0; glVertex $w , 0;
427 glEnd;
428 460
429 my $left = $tex[3]; 461 my $left = $tex[3];
430 glBindTexture GL_TEXTURE_2D, $left->{name}; 462 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
431
432 glBegin GL_QUADS;
433 glTexCoord 0, 0; glVertex 0 , $top->{height};
434 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
435 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
436 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
437 glEnd;
438 463
439 my $right = $tex[2]; 464 my $right = $tex[2];
440 glBindTexture GL_TEXTURE_2D, $right->{name}; 465 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
441
442 glBegin GL_QUADS;
443 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
444 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
445 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
446 glTexCoord 1, 0; glVertex $w , $top->{height};
447 glEnd;
448 466
449 my $bottom = $tex[4]; 467 my $bottom = $tex[4];
450 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 468 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
451
452 glBegin GL_QUADS;
453 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
454 glTexCoord 0, 1; glVertex 0 , $h;
455 glTexCoord 1, 1; glVertex $w , $h;
456 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
457 glEnd;
458 469
459 my $bg = $tex[0]; 470 my $bg = $tex[0];
460 glBindTexture GL_TEXTURE_2D, $bg->{name}; 471 glBindTexture GL_TEXTURE_2D, $bg->{name};
461 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 472 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
462 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 473 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
463 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 474 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
464 475
465 my $rep_x = $cw / $bg->{width}; 476 my $rep_x = $cw / $bg->{width};
466 my $rep_y = $ch / $bg->{height}; 477 my $rep_y = $ch / $bg->{height};
467 478
468 glBegin GL_QUADS; 479 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
469 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
470 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
471 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
472 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
473 glEnd;
474 480
475 glDisable GL_BLEND; 481 glDisable GL_BLEND;
476 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
477 483
478 $self->child->draw; 484 $self->child->draw;
479 485
480} 486}
481 487
482############################################################################# 488#############################################################################
483 489
484package Crossfire::Client::Widget::Table; 490package CFClient::Widget::Table;
485 491
486our @ISA = Crossfire::Client::Widget::Bin::; 492our @ISA = CFClient::Widget::Bin::;
487 493
488use SDL::OpenGL; 494use SDL::OpenGL;
489 495
490sub add { 496sub add {
491 my ($self, $x, $y, $chld) = @_; 497 my ($self, $x, $y, $chld) = @_;
566 } 572 }
567} 573}
568 574
569############################################################################# 575#############################################################################
570 576
571package Crossfire::Client::Widget::VBox; 577package CFClient::Widget::VBox;
572 578
573our @ISA = Crossfire::Client::Widget::Container::; 579our @ISA = CFClient::Widget::Container::;
574 580
575use SDL::OpenGL; 581use SDL::OpenGL;
582
583sub size_request {
584 my ($self) = @_;
585
586 my @alloc = map [$_->size_request], @{$self->{children}};
587
588 (
589 (List::Util::max map $_->[0], @alloc),
590 (List::Util::sum map $_->[1], @alloc),
591 )
592}
576 593
577sub size_allocate { 594sub size_allocate {
578 my ($self, $w, $h) = @_; 595 my ($self, $w, $h) = @_;
579 596
580 $self->w ($w); 597 $self->w ($w);
613 $y += $h; 630 $y += $h;
614 } 631 }
615 } 632 }
616} 633}
617 634
618sub _draw {
619 my ($self) = @_;
620
621 my ($x, $y);
622 for (@{$self->{children} || []}) {
623 $_->draw;
624 $y += $_->h;
625 }
626}
627
628############################################################################# 635#############################################################################
629 636
630package Crossfire::Client::Widget::Label; 637package CFClient::Widget::Label;
631 638
632our @ISA = Crossfire::Client::Widget::; 639our @ISA = CFClient::Widget::;
633 640
634use SDL::OpenGL; 641use SDL::OpenGL;
635 642
636sub new { 643sub new {
637 my ($class, $x, $y, $z, $height, $text) = @_; 644 my ($class, %arg) = @_;
638 645
639 # TODO: color, and make height, xyz etc. optional 646 # TODO: color, and make height, xyz etc. optional
640 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 647 my $self = $class->SUPER::new (
648 color => [1, 1, 1],
649 height => $::FONTSIZE,
650 text => "",
651 layout => new CFClient::Layout,
652 %arg
653 );
641 654
642 $self->set_text ($text); 655 $self->set_text ($self->{text});
643 656
644 $self 657 $self
645} 658}
646 659
647sub set_text { 660sub set_text {
648 my ($self, $text) = @_; 661 my ($self, $text) = @_;
649 662
650 $self->{text} = $text; 663 $self->{text} = $text;
651 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 664 $self->{layout}->set_markup ($text);
652 665
653 $self->update; 666 delete $self->{texture};
654} 667}
655 668
656sub get_text { 669sub get_text {
657 my ($self, $text) = @_; 670 my ($self, $text) = @_;
658 671
660} 673}
661 674
662sub size_request { 675sub size_request {
663 my ($self) = @_; 676 my ($self) = @_;
664 677
665 ( 678 $self->{layout}->set_width;
679 $self->{layout}->set_height ($self->{height});
680 $self->{layout}->size
681# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
682# (
666 $self->{texture}{width}, 683# $self->{texture}{width},
667 $self->{texture}{height}, 684# $self->{texture}{height},
668 ) 685# )
686# } else {
687# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
688#
689# ($w, $h)
690# }
691}
692
693sub size_allocate {
694 my ($self, $w, $h) = @_;
695
696 $self->SUPER::size_allocate ($w, $h);
697 delete $self->{texture};
669} 698}
670 699
671sub _draw { 700sub _draw {
672 my ($self) = @_; 701 my ($self) = @_;
673 702
674 my $tex = $self->{texture}; 703 my $tex = $self->{texture} ||= do {
704 $self->{layout}->set_width ($self->{w});
705 new_from_layout CFClient::Texture $self->{layout};
706 };
675 707
676 glEnable GL_BLEND; 708 glEnable GL_BLEND;
677 glEnable GL_TEXTURE_2D; 709 glEnable GL_TEXTURE_2D;
678 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
679 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 711 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
680 glBindTexture GL_TEXTURE_2D, $tex->{name};
681 712
682 glColor 1, 0, 0, 1; # TODO color 713 glColor @{$self->{color}};
683 714
684 glBegin GL_QUADS; 715 $tex->draw_quad (0, 0);
685 glTexCoord 0, 0; glVertex 0 , 0;
686 glTexCoord 0, 1; glVertex 0 , $tex->{height};
687 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
688 glTexCoord 1, 0; glVertex $tex->{width}, 0;
689 glEnd;
690 716
691 glDisable GL_BLEND; 717 glDisable GL_BLEND;
692 glDisable GL_TEXTURE_2D; 718 glDisable GL_TEXTURE_2D;
693} 719}
694 720
695############################################################################# 721#############################################################################
696 722
697package Crossfire::Client::Widget::TextEntry; 723package CFClient::Widget::Entry;
698 724
699our @ISA = Crossfire::Client::Widget::Label::; 725our @ISA = CFClient::Widget::Label::;
700 726
701use SDL; 727use SDL;
702use SDL::OpenGL; 728use SDL::OpenGL;
703 729
704sub key_down { 730sub key_down {
705 my ($self, $ev) = @_; 731 my ($self, $ev) = @_;
706 732
707 my $mod = $ev->key_mod; 733 my $mod = $ev->key_mod;
708 my $sym = $ev->key_sym; 734 my $sym = $ev->key_sym;
709 735
710 $ev->set_unicode (1);
711 my $uni = $ev->key_unicode; 736 my $uni = $ev->key_unicode;
712 737
713 my $text = $self->get_text; 738 my $text = $self->get_text;
714 739
715 if ($sym == SDLK_BACKSPACE) { 740 if ($sym == SDLK_BACKSPACE) {
716 substr $text, -1, 1, ''; 741 substr $text, -1, 1, '';
717
718 } elsif ($uni) { 742 } elsif ($uni) {
719 $text .= chr $uni; 743 $text .= chr $uni;
720 } 744 }
745
721 $self->set_text ($text); 746 $self->set_text ($text);
722} 747}
723 748
724############################################################################# 749sub button_down {
750 my ($self, $ev) = @_;
725 751
752 $self->focus_in;
753}
754
755sub mouse_motion {
756 my ($self, $ev, $x, $y) = @_;
757 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
758}
759
760sub _draw {
761 my ($self) = @_;
762
763 if ($FOCUS == $self) {
764 glColor 1, 1, 1;
765 } else {
766 glColor 0.7, 0.7, 0.7;
767 }
768
769 glBegin GL_QUADS;
770 glVertex 0 , 0;
771 glVertex 0 , $self->{h} - 1;
772 glVertex $self->{w} - 1, $self->{h} - 1;
773 glVertex $self->{w} - 1, 0;
774 glEnd;
775
776 $self->SUPER::_draw;
777}
778
779#############################################################################
780
726package Crossfire::Client::Widget::MapWidget; 781package CFClient::Widget::MapWidget;
727 782
728use strict; 783use strict;
729 784
730use List::Util qw(min max); 785use List::Util qw(min max);
731 786
732use SDL; 787use SDL;
733use SDL::OpenGL; 788use SDL::OpenGL;
734use SDL::OpenGL::Constants; 789use SDL::OpenGL::Constants;
735 790
736our @ISA = Crossfire::Client::Widget::; 791our @ISA = CFClient::Widget::;
792
793sub new {
794 my $class = shift;
795
796 $class->SUPER::new (
797 z => -1,
798 list => (glGenLists 1),
799 @_
800 )
801}
737 802
738sub key_down { 803sub key_down {
739 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
740} 805}
741 806
742sub key_up { 807sub key_up {
743} 808}
744 809
745sub size_request { 810sub size_request {
746 811 (
812 1 + int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32,
814 )
747} 815}
748 816
749sub size_allocate { 817sub update {
818 my ($self) = @_;
819
820 $self->{need_update} = 1;
750} 821}
751 822
752sub _draw { 823sub _draw {
753 my ($self) = @_; 824 my ($self) = @_;
754 825
826 if (delete $self->{need_update}) {
827 glNewList $self->{list}, GL_COMPILE;
828
755 my $mx = $::CONN->{mapx}; 829 my $mx = $::CONN->{mapx};
756 my $my = $::CONN->{mapy}; 830 my $my = $::CONN->{mapy};
757 831
758 my $map = $::CONN->{map}; 832 my $map = $::CONN->{map};
759 833
760 my ($xofs, $yofs); 834 my ($xofs, $yofs);
761 835
762 my $sw = 1 + int $::WIDTH / 32; 836 my $sw = 1 + int $::WIDTH / 32;
763 my $sh = 1 + int $::HEIGHT / 32; 837 my $sh = 1 + int $::HEIGHT / 32;
764 838
765 if ($::CONN->{mapw} > $sw) { 839 if ($::CONN->{mapw} > $sw) {
766 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 840 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
767 } else { 841 } else {
768 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 842 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
769 } 843 }
770 844
771 if ($::CONN->{maph} > $sh) { 845 if ($::CONN->{maph} > $sh) {
772 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 846 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
773 } else { 847 } else {
774 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 848 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
775 } 849 }
776 850
777 glEnable GL_TEXTURE_2D; 851 glEnable GL_TEXTURE_2D;
778 glEnable GL_BLEND; 852 glEnable GL_BLEND;
779 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
780 854
781 my $sw4 = ($sw + 3) & ~3; 855 my $sw4 = ($sw + 3) & ~3;
782 my $lighting = "\x00" x ($sw4 * $sh); 856 my $darkness = "\x00" x ($sw4 * $sh);
783 857
784 for my $x (0 .. $sw - 1) { 858 for my $x (0 .. $sw - 1) {
859 my $row = $map->[$x + $xofs];
785 for my $y (0 .. $sh - 1) { 860 for my $y (0 .. $sh - 1) {
786 861
787 my $cell = $map->[$x + $xofs][$y + $yofs] 862 my $cell = $row->[$y + $yofs]
788 or next; 863 or next;
789 864
790 my $darkness = $cell->[0] * (1 / 255); 865 my $dark = $cell->[0];
791 if ($darkness < 0) { 866 if ($dark < 0) {
792 $darkness = 0.15; 867 substr $darkness, $y * $sw4 + $x, 1, chr 224;
868 } else {
869 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
793 } 870 }
794 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
795 871
796 for my $num (grep $_, @$cell[1,2,3]) { 872 for my $num (grep $_, @$cell[1,2,3]) {
797 my $tex = $::CONN->{face}[$num]{texture} || next; 873 my $tex = $::CONN->{face}[$num]{texture} || next;
798 874
799 glBindTexture GL_TEXTURE_2D, $tex->{name};
800
801 my $w = $tex->{width}; 875 my $w = $tex->{width};
802 my $h = $tex->{height}; 876 my $h = $tex->{height};
803 877
804 my $px = ($x + 1) * 32 - $w; 878 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
805 my $py = ($y + 1) * 32 - $h;
806
807 glBegin GL_QUADS;
808 glTexCoord 0, 0; glVertex $px , $py;
809 glTexCoord 0, 1; glVertex $px , $py + $h;
810 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
811 glTexCoord 1, 0; glVertex $px + $w, $py;
812 glEnd; 879 }
813 } 880 }
814 } 881 }
815 }
816 882
817# if (1) { # higher quality darkness 883# if (1) { # higher quality darkness
818# $lighting =~ s/(.)/$1$1$1/gs; 884# $lighting =~ s/(.)/$1$1$1/gs;
819# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 885# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
820# 886#
822# 888#
823# $lighting = $pb->get_pixels; 889# $lighting = $pb->get_pixels;
824# $lighting =~ s/(.)../$1/gs; 890# $lighting =~ s/(.)../$1/gs;
825# } 891# }
826 892
827 $lighting = new Crossfire::Client::Texture
828 width => $sw4,
829 height => $sh,
830 data => $lighting,
831 internalformat => GL_ALPHA4,
832 format => GL_ALPHA;
833
834 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 893 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
835 glColor 0, 0, 0, 0.75;
836 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 894 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
837 glBindTexture GL_TEXTURE_2D, $lighting->{name};
838 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
839 glBegin GL_QUADS;
840 glTexCoord 0, 0; glVertex 0 , 0;
841 glTexCoord 0, 1; glVertex 0 , $sh * 32;
842 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
843 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
844 glEnd;
845 895
896 $darkness = new CFClient::Texture
897 width => $sw4,
898 height => $sh,
899 data => $darkness,
900 internalformat => GL_ALPHA,
901 format => GL_ALPHA;
902
903 glColor 0.45, 0.45, 0.45, 1;
904 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
905
846 glDisable GL_TEXTURE_2D; 906 glDisable GL_TEXTURE_2D;
847 glDisable GL_BLEND; 907 glDisable GL_BLEND;
908
909 glEndList;
910 }
911
912 glCallList $self->{list};
848} 913}
849 914
850my %DIR = ( 915my %DIR = (
851 SDLK_KP8, [1, "north"], 916 SDLK_KP8, [1, "north"],
852 SDLK_KP9, [2, "northeast"], 917 SDLK_KP9, [2, "northeast"],
898 } 963 }
899} 964}
900 965
901############################################################################# 966#############################################################################
902 967
903package Crossfire::Client::Widget::Animator; 968package CFClient::Widget::Animator;
904 969
905use SDL::OpenGL; 970use SDL::OpenGL;
906 971
907our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = CFClient::Widget::Bin::;
908 973
909sub moveto { 974sub moveto {
910 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
911 976
912 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
913 $self->{speed} = 0.2; 978 $self->{speed} = 0.001;
914 $self->{time} = 1; 979 $self->{time} = 1;
915 980
916 ::animation_start $self; 981 ::animation_start $self;
917} 982}
918 983
933 998
934sub _draw { 999sub _draw {
935 my ($self) = @_; 1000 my ($self) = @_;
936 1001
937 glPushMatrix; 1002 glPushMatrix;
938 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
939 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
940 glPopMatrix; 1005 glPopMatrix;
941} 1006}
942 1007
9431; 1008#############################################################################
944 1009
1010package CFClient::Widget::Toplevel;
1011
1012our @ISA = CFClient::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub size_allocate {
1019 my ($self, $w, $h) = @_;
1020
1021 $self->SUPER::size_allocate ($w, $h);
1022
1023 $_->size_allocate ($_->size_request)
1024 for @{$self->{children}};
1025}
1026
1027sub translate {
1028 my ($self, $x, $y) = @_;
1029
1030 ($x, $y)
1031}
1032
1033sub update {
1034 my ($self) = @_;
1035
1036 $self->size_allocate ($self->size_request);
1037 ::refresh ();
1038}
1039
1040sub add {
1041 my ($self, $widget) = @_;
1042
1043 $self->SUPER::add ($widget);
1044
1045 $widget->size_allocate ($widget->size_request);
1046}
1047
1048sub draw {
1049 my ($self) = @_;
1050
1051 $self->_draw;
1052}
1053
1054#############################################################################
1055
1056package CFClient::Widget;
1057
1058$TOPLEVEL = new CFClient::Widget::Toplevel;
1059
10601
1061

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines