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.38 by root, Sun Apr 9 21:34:49 2006 UTC vs.
Revision 1.53 by root, Mon Apr 10 11:56:28 2006 UTC

5use Scalar::Util; 5use Scalar::Util;
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 10use Crossfire::Client;
11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
11 16
12# class methods for events 17# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
14sub 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
15sub 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) if $GRAB;
40}
41
16sub 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 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev) if $HOVER;
71}
17 72
18sub new { 73sub new {
19 my $class = shift; 74 my $class = shift;
20 75
21 bless { @_ }, $class 76 bless { @_ }, $class
37 Carp::confess "size_request is abtract"; 92 Carp::confess "size_request is abtract";
38} 93}
39 94
40sub size_allocate { 95sub size_allocate {
41 my ($self, $w, $h) = @_; 96 my ($self, $w, $h) = @_;
97
42 $self->w ($w); 98 $self->{w} = $w;
43 $self->h ($h); 99 $self->{h} = $h;
44} 100}
45 101
46sub focus_in { 102sub focus_in {
47 my ($widget) = @_; 103 my ($self) = @_;
48 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
49} 108}
50 109
51sub focus_out { 110sub focus_out {
52 my ($widget) = @_; 111 my ($self) = @_;
53}
54 112
55sub key_down { 113 return unless $FOCUS == $self;
56 my ($widget, $sdlev) = @_;
57}
58 114
59sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
60 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
61} 117}
62 118
119sub mouse_motion { }
120sub button_up { }
63sub button_down { 121sub button_down { }
64 my ($widget, $sdlev) = @_; 122sub key_down { }
65} 123sub key_up { }
66 124
67sub button_up {
68 my ($widget, $sdlev) = @_;
69}
70
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 130
77sub draw { 131sub draw {
78 my ($self) = @_; 132 my ($self) = @_;
79 133
80 glPushMatrix; 134 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
83 glPopMatrix; 148 glPopMatrix;
84} 149}
85 150
86sub _draw { 151sub _draw {
87 my ($self) = @_; 152 my ($self) = @_;
134 my ($self) = @_; 199 my ($self) = @_;
135 200
136 #$self->deactivate; 201 #$self->deactivate;
137} 202}
138 203
204#############################################################################
205
139package Crossfire::Client::Widget::Container; 206package Crossfire::Client::Widget::Container;
140 207
141our @ISA = Crossfire::Client::Widget::; 208our @ISA = Crossfire::Client::Widget::;
142 209
143sub new { 210sub new {
157 224
158 @{$self->{children}} = 225 @{$self->{children}} =
159 sort { $a->{z} <=> $b->{z} } 226 sort { $a->{z} <=> $b->{z} }
160 @{$self->{children}}, $chld; 227 @{$self->{children}}, $chld;
161 228
162 $self->size_allocate ($self->{w}, $self->{h}); 229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
163} 231}
164 232
165sub remove { 233sub remove {
166 my ($self, $widget) = @_; 234 my ($self, $widget) = @_;
167 235
171} 239}
172 240
173sub find_widget { 241sub find_widget {
174 my ($self, $x, $y) = @_; 242 my ($self, $x, $y) = @_;
175 243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
176 my $res; 247 my $res;
177 248
178 for (@{ $self->{children} }) { 249 for (reverse @{ $self->{children} }) {
179 $res = $_->find_widget ($x, $y) 250 $res = $_->find_widget ($x, $y)
180 and return $res; 251 and return $res;
181 } 252 }
182 253
183 () 254 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
184}
185
186sub size_request {
187 my ($self) = @_;
188
189 my ($hs, $ws) = (0, 0);
190 for (@{$self->{children} || []}) {
191 my ($w, $h) = $_->size_request;
192 $hs += $h;
193 if ($ws < $w) { $ws = $w }
194 }
195
196 return ($ws, $hs);
197} 255}
198 256
199sub _draw { 257sub _draw {
200 my ($self) = @_; 258 my ($self) = @_;
201 259
202 $_->draw for @{$self->{children}}; 260 $_->draw for @{$self->{children}};
203} 261}
204 262
263#############################################################################
264
205package Crossfire::Client::Widget::Bin; 265package Crossfire::Client::Widget::Bin;
206 266
207our @ISA = Crossfire::Client::Widget::Container::; 267our @ISA = Crossfire::Client::Widget::Container::;
208 268
209sub get { $_[0]->{children}[0] } 269sub child { $_[0]->{children}[0] }
210 270
211sub size_request { 271sub size_request {
212 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
213} 273}
214 274
215sub size_allocate { 275sub size_allocate {
216 my ($self, $w, $h) = @_; 276 my ($self, $w, $h) = @_;
277
217 $self->SUPER::size_allocate ($w, $h); 278 $self->SUPER::size_allocate ($w, $h);
218 $self->{children}[0]->size_allocate ($w, $h) 279 $self->{children}[0]->size_allocate ($w, $h)
219 if $self->{children}[0] 280 if $self->{children}[0]
220} 281}
221 282
283#############################################################################
284
222package Crossfire::Client::Widget::Toplevel; 285package Crossfire::Client::Widget::Window;
223 286
224our @ISA = Crossfire::Client::Widget::Container::; 287our @ISA = Crossfire::Client::Widget::Bin::;
288
289use SDL::OpenGL;
290
291sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_;
293
294 my $self = $class->SUPER::new;
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297}
225 298
226sub update { 299sub update {
227 my ($self) = @_; 300 my ($self) = @_;
228 301
229 ::refresh ();
230}
231
232package Crossfire::Client::Widget::Window;
233
234our @ISA = Crossfire::Client::Widget::Bin::;
235
236use SDL::OpenGL;
237
238sub add {
239 my ($self, $chld) = @_;
240 warn "ADD $chld\n";
241 $self->SUPER::add ($chld);
242 $chld->set_parent ($self);
243}
244
245sub remove {
246 my ($self) = @_;
247 # TODO FIXME: removing a child from a window will crash, see render_chld
248 # $self->update;
249}
250
251sub update {
252 my ($self) = @_;
253 $self->render_chld; 302 $self->render_chld;
303 $self->SUPER::update;
254} 304}
255 305
256sub render_chld { 306sub render_chld {
257 my ($self) = @_; 307 my ($self) = @_;
258 my $chld = $self->get;
259 my ($w, $h) = $self->size_request;
260 308
261 require Carp;
262 Carp::cluck "RENDERCHI $w $h";
263 warn "RENDERCHI $w $h\n";
264 $self->{texture} = 309 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
266 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 312 );
268 $self->{texture}->upload;
269}
270
271sub size_request {
272 my ($self) = @_;
273 ($self->w, $self->h)
274} 313}
275 314
276sub size_allocate { 315sub size_allocate {
277 my ($self, $w, $h) = @_; 316 my ($self, $w, $h) = @_;
278 317
279 $self->w ($w); 318 $self->{w} = $w;
280 $self->h ($h); 319 $self->{h} = $h;
320
281 $self->get->size_allocate ($w, $h); 321 $self->child->size_allocate ($w, $h);
282 322
283 $self->update; #TODO: Move this to the size_request event propably? 323 $self->render_chld;
284} 324}
285 325
286sub _draw { 326sub _draw {
287 my ($self) = @_; 327 my ($self) = @_;
288 328
305 345
306 glDisable GL_BLEND; 346 glDisable GL_BLEND;
307 glDisable GL_TEXTURE_2D; 347 glDisable GL_TEXTURE_2D;
308} 348}
309 349
350#############################################################################
351
310package Crossfire::Client::Widget::Frame; 352package Crossfire::Client::Widget::Frame;
311 353
312our @ISA = Crossfire::Client::Widget::Bin::; 354our @ISA = Crossfire::Client::Widget::Bin::;
313 355
314use SDL::OpenGL; 356use SDL::OpenGL;
315 357
316sub size_request { 358sub size_request {
317 my ($self) = @_; 359 my ($self) = @_;
318 my $chld = $self->get 360 my $chld = $self->child
319 or return (0, 0); 361 or return (0, 0);
320 362
321 $chld->move (2, 2); 363 $chld->move (2, 2);
322 364
323 map { $_ + 4 } $chld->size_request; 365 map { $_ + 4 } $chld->size_request;
324} 366}
325 367
326sub size_allocate { 368sub size_allocate {
327 my ($self, $w, $h) = @_; 369 my ($self, $w, $h) = @_;
328 370
329 $self->w ($w); 371 $self->{w} = $w;
330 $self->h ($h); 372 $self->{h} = $h;
331 373
332 $self->get->size_allocate ($w - 4, $h - 4); 374 $self->child->size_allocate ($w - 4, $h - 4);
333 $self->get->move (2, 2); 375 $self->child->move (2, 2);
334} 376}
335 377
336sub _draw { 378sub _draw {
337 my ($self) = @_; 379 my ($self) = @_;
338 380
339 my $chld = $self->get; 381 my $chld = $self->child;
340 382
341 my ($w, $h) = $chld->size_request; 383 my ($w, $h) = $chld->size_request;
342 384
343 glBegin GL_QUADS; 385 glBegin GL_QUADS;
344 glColor 0, 0, 0; 386 glColor 0, 0, 0;
349 glEnd; 391 glEnd;
350 392
351 $chld->draw; 393 $chld->draw;
352} 394}
353 395
396#############################################################################
397
354package Crossfire::Client::Widget::FancyFrame; 398package Crossfire::Client::Widget::FancyFrame;
355 399
356our @ISA = Crossfire::Client::Widget::Frame::; 400our @ISA = Crossfire::Client::Widget::Bin::;
357 401
358use SDL::OpenGL; 402use SDL::OpenGL;
359 403
360sub new { 404my @tex =
361 my ($self, $theme) = @_;
362 $self = $self->SUPER::new;
363
364 $self->{txts} = [
365 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 405 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
366 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 406 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
367 ];
368 $self
369}
370 407
371sub size_request { 408sub size_request {
372 my ($self) = @_; 409 my ($self) = @_;
410
373 my ($w, $h) = $self->get->size_request; 411 my ($w, $h) = $self->SUPER::size_request;
374 412
375 $h += $self->{txts}->[1]->{height}; 413 $h += $tex[1]->{height};
376 $h += $self->{txts}->[4]->{height}; 414 $h += $tex[4]->{height};
377 $w += $self->{txts}->[2]->{width}; 415 $w += $tex[2]->{width};
378 $w += $self->{txts}->[3]->{width}; 416 $w += $tex[3]->{width};
379 417
380 ($w, $h) 418 ($w, $h)
381} 419}
382 420
383sub size_allocate { 421sub size_allocate {
384 my ($self, $w, $h) = @_; 422 my ($self, $w, $h) = @_;
385 423
386 $self->w ($w); 424 $self->SUPER::size_allocate ($w, $h);
387 $self->h ($h); 425
388 $h -= $self->{txts}->[1]->{height}; 426 $h -= $tex[1]->{height};
389 $h -= $self->{txts}->[4]->{height}; 427 $h -= $tex[4]->{height};
390 $w -= $self->{txts}->[2]->{width}; 428 $w -= $tex[2]->{width};
391 $w -= $self->{txts}->[3]->{width}; 429 $w -= $tex[3]->{width};
392 430
393 $h = $h < 0 ? 0 : $h; 431 $h = $h < 0 ? 0 : $h;
394 $w = $w < 0 ? 0 : $w; 432 $w = $w < 0 ? 0 : $w;
395 warn "CHILD:$w $h\n"; 433
396 $self->get->size_allocate ($w, $h); 434 $self->child->size_allocate ($w, $h);
397 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 435 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
398} 436}
399 437
400sub _draw { 438sub _draw {
401 my ($self) = @_; 439 my ($self) = @_;
402 440
403 my ($w, $h) = ($self->w, $self->h); 441 my ($w, $h) = ($self->{w}, $self->{h});
404 my ($cw, $ch) = ($self->get->w, $self->get->h); 442 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
405 443
406 glEnable GL_BLEND; 444 glEnable GL_BLEND;
407 glEnable GL_TEXTURE_2D; 445 glEnable GL_TEXTURE_2D;
408 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 446 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
410 448
411 my $top = $self->{txts}->[1]; 449 my $top = $tex[1];
412 glBindTexture GL_TEXTURE_2D, $top->{name}; 450 glBindTexture GL_TEXTURE_2D, $top->{name};
413 451
414 glBegin GL_QUADS; 452 glBegin GL_QUADS;
415 glTexCoord 0, 0; glVertex 0 , 0; 453 glTexCoord 0, 0; glVertex 0 , 0;
416 glTexCoord 0, 1; glVertex 0 , $top->{height}; 454 glTexCoord 0, 1; glVertex 0 , $top->{height};
417 glTexCoord 1, 1; glVertex $w , $top->{height}; 455 glTexCoord 1, 1; glVertex $w , $top->{height};
418 glTexCoord 1, 0; glVertex $w , 0; 456 glTexCoord 1, 0; glVertex $w , 0;
419 glEnd; 457 glEnd;
420 458
421 my $left = $self->{txts}->[3]; 459 my $left = $tex[3];
422 glBindTexture GL_TEXTURE_2D, $left->{name}; 460 glBindTexture GL_TEXTURE_2D, $left->{name};
423 461
424 glBegin GL_QUADS; 462 glBegin GL_QUADS;
425 glTexCoord 0, 0; glVertex 0 , $top->{height}; 463 glTexCoord 0, 0; glVertex 0 , $top->{height};
426 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 464 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
427 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 465 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
428 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 466 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
429 glEnd; 467 glEnd;
430 468
431 my $right = $self->{txts}->[2]; 469 my $right = $tex[2];
432 glBindTexture GL_TEXTURE_2D, $right->{name}; 470 glBindTexture GL_TEXTURE_2D, $right->{name};
433 471
434 glBegin GL_QUADS; 472 glBegin GL_QUADS;
435 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 473 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
436 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 474 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
437 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 475 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
438 glTexCoord 1, 0; glVertex $w , $top->{height}; 476 glTexCoord 1, 0; glVertex $w , $top->{height};
439 glEnd; 477 glEnd;
440 478
441 my $bottom = $self->{txts}->[4]; 479 my $bottom = $tex[4];
442 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 480 glBindTexture GL_TEXTURE_2D, $bottom->{name};
443 481
444 glBegin GL_QUADS; 482 glBegin GL_QUADS;
445 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 483 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
446 glTexCoord 0, 1; glVertex 0 , $h; 484 glTexCoord 0, 1; glVertex 0 , $h;
447 glTexCoord 1, 1; glVertex $w , $h; 485 glTexCoord 1, 1; glVertex $w , $h;
448 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 486 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
449 glEnd; 487 glEnd;
450 488
451 my $bg = $self->{txts}->[0]; 489 my $bg = $tex[0];
452 glBindTexture GL_TEXTURE_2D, $bg->{name}; 490 glBindTexture GL_TEXTURE_2D, $bg->{name};
453 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 491 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
454 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 492 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
455 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 493 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
456 494
465 glEnd; 503 glEnd;
466 504
467 glDisable GL_BLEND; 505 glDisable GL_BLEND;
468 glDisable GL_TEXTURE_2D; 506 glDisable GL_TEXTURE_2D;
469 507
470 $self->get->draw; 508 $self->child->draw;
471 509
472} 510}
511
512#############################################################################
473 513
474package Crossfire::Client::Widget::Table; 514package Crossfire::Client::Widget::Table;
475 515
476our @ISA = Crossfire::Client::Widget::Bin::; 516our @ISA = Crossfire::Client::Widget::Bin::;
477 517
554 594
555 $y += $self->max_row_height ($yi); 595 $y += $self->max_row_height ($yi);
556 } 596 }
557} 597}
558 598
599#############################################################################
600
559package Crossfire::Client::Widget::VBox; 601package Crossfire::Client::Widget::VBox;
560 602
561our @ISA = Crossfire::Client::Widget::Container::; 603our @ISA = Crossfire::Client::Widget::Container::;
562 604
563use SDL::OpenGL; 605use SDL::OpenGL;
606
607sub size_request {
608 my ($self) = @_;
609
610 my @alloc = map [$_->size_request], @{$self->{children}};
611
612 (
613 (List::Util::max map $_->[0], @alloc),
614 (List::Util::sum map $_->[1], @alloc),
615 )
616}
564 617
565sub size_allocate { 618sub size_allocate {
566 my ($self, $w, $h) = @_; 619 my ($self, $w, $h) = @_;
567 620
568 $self->w ($w); 621 $self->w ($w);
601 $y += $h; 654 $y += $h;
602 } 655 }
603 } 656 }
604} 657}
605 658
606sub _draw { 659#############################################################################
607 my ($self) = @_;
608
609 my ($x, $y);
610 for (@{$self->{children} || []}) {
611 $_->draw;
612 $y += $_->h;
613 }
614}
615 660
616package Crossfire::Client::Widget::Label; 661package Crossfire::Client::Widget::Label;
617 662
618our @ISA = Crossfire::Client::Widget::; 663our @ISA = Crossfire::Client::Widget::;
619 664
620use SDL::OpenGL; 665use SDL::OpenGL;
621 666
622sub new { 667sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
624 669
670 $height ||= $::FONTSIZE;
671
625 # TODO: color, and make height, xyz etc. optional 672 # TODO: color, and make height, xyz etc. optional
626 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
627 674
628 $self->set_text ($text); 675 $self->set_text ($text);
629 676
646} 693}
647 694
648sub size_request { 695sub size_request {
649 my ($self) = @_; 696 my ($self) = @_;
650 697
651 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
652 $self->{texture}{width}, 700 $self->{texture}{width},
653 $self->{texture}{height}, 701 $self->{texture}{height},
702 )
703 } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705
706 ($w, $h)
654 ) 707 }
708
655} 709}
656 710
657sub _draw { 711sub _draw {
658 my ($self) = @_; 712 my ($self) = @_;
659 713
676 730
677 glDisable GL_BLEND; 731 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
679} 733}
680 734
735#############################################################################
736
681package Crossfire::Client::Widget::TextEntry; 737package Crossfire::Client::Widget::Entry;
682 738
683our @ISA = Crossfire::Client::Widget::Label::; 739our @ISA = Crossfire::Client::Widget::Label::;
684 740
685use SDL; 741use SDL;
686use SDL::OpenGL; 742use SDL::OpenGL;
689 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
690 746
691 my $mod = $ev->key_mod; 747 my $mod = $ev->key_mod;
692 my $sym = $ev->key_sym; 748 my $sym = $ev->key_sym;
693 749
694 $ev->set_unicode (1);
695 my $uni = $ev->key_unicode; 750 my $uni = $ev->key_unicode;
696 751
697 my $text = $self->get_text; 752 my $text = $self->get_text;
698 753
699 if ($sym == SDLK_BACKSPACE) { 754 if ($sym == SDLK_BACKSPACE) {
700 substr $text, -1, 1, ''; 755 substr $text, -1, 1, '';
701
702 } elsif ($uni) { 756 } elsif ($uni) {
703 $text .= chr $uni; 757 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
704 } 759 }
760
705 $self->set_text ($text); 761 $self->set_text ($text);
706} 762}
763
764sub button_down {
765 my ($self, $ev) = @_;
766
767 $self->focus_in;
768}
769
770sub _draw {
771 my ($self) = @_;
772
773 if ($FOCUS == $self) {
774 glColor 1, 1, 1;
775 } else {
776 glColor 0.7, 0.7, 0.7;
777 }
778
779 glBegin GL_QUADS;
780 glVertex 0 , 0;
781 glVertex 0 , $self->{h} - 1;
782 glVertex $self->{w} - 1, $self->{h} - 1;
783 glVertex $self->{w} - 1, 0;
784 glEnd;
785
786 $self->SUPER::_draw;
787}
788
789#############################################################################
707 790
708package Crossfire::Client::Widget::MapWidget; 791package Crossfire::Client::Widget::MapWidget;
709 792
710use strict; 793use strict;
711 794
723 806
724sub key_up { 807sub key_up {
725} 808}
726 809
727sub size_request { 810sub size_request {
728 811 (
729} 812 1 + int $::WIDTH / 32,
730 813 1 + int $::HEIGHT / 32,
731sub size_allocate { 814 )
732} 815}
733 816
734sub _draw { 817sub _draw {
735 my ($self) = @_; 818 my ($self) = @_;
736 819
769 my $cell = $map->[$x + $xofs][$y + $yofs] 852 my $cell = $map->[$x + $xofs][$y + $yofs]
770 or next; 853 or next;
771 854
772 my $darkness = $cell->[0] * (1 / 255); 855 my $darkness = $cell->[0] * (1 / 255);
773 if ($darkness < 0) { 856 if ($darkness < 0) {
774 $darkness = 0.15; 857 $darkness = $cell->[1] ? 0.2 : 0;
775 } 858 }
776 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; 859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
777 860
778 for my $num (grep $_, @$cell[1,2,3]) { 861 for my $num (grep $_, @$cell[1,2,3]) {
779 my $tex = $::CONN->{face}[$num]{texture} || next; 862 my $tex = $::CONN->{face}[$num]{texture} || next;
808 891
809 $lighting = new Crossfire::Client::Texture 892 $lighting = new Crossfire::Client::Texture
810 width => $sw4, 893 width => $sw4,
811 height => $sh, 894 height => $sh,
812 data => $lighting, 895 data => $lighting,
813 internalformat => GL_ALPHA4, 896 internalformat => GL_ALPHA,
814 format => GL_ALPHA; 897 format => GL_ALPHA;
815 898
816 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
817 glColor 0, 0, 0, 0.75; 900 glColor 0.5, 0.5, 0.5, 1;
818 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
819 glBindTexture GL_TEXTURE_2D, $lighting->{name}; 902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
820 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
821 glBegin GL_QUADS; 904 glBegin GL_QUADS;
822 glTexCoord 0, 0; glVertex 0 , 0; 905 glTexCoord 0, 0; glVertex 0 , 0;
878 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 961 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
879 $::CONN->send ("command run_stop"); 962 $::CONN->send ("command run_stop");
880 } 963 }
881} 964}
882 965
966#############################################################################
967
883package Crossfire::Client::Widget::Animator; 968package Crossfire::Client::Widget::Animator;
884 969
885use SDL::OpenGL; 970use SDL::OpenGL;
886 971
887our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = Crossfire::Client::Widget::Bin::;
888 973
889sub moveto { 974sub moveto {
890 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
891 976
892 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
893 $self->{speed} = 0.2; 978 $self->{speed} = 2;
894 $self->{time} = 1; 979 $self->{time} = 1;
895 980
896 ::animation_start $self; 981 ::animation_start $self;
897} 982}
898 983
913 998
914sub _draw { 999sub _draw {
915 my ($self) = @_; 1000 my ($self) = @_;
916 1001
917 glPushMatrix; 1002 glPushMatrix;
918 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
919 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
920 glPopMatrix; 1005 glPopMatrix;
921} 1006}
922 1007
9231; 1008#############################################################################
924 1009
1010package Crossfire::Client::Widget::Toplevel;
1011
1012our @ISA = Crossfire::Client::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 update {
1028 my ($self) = @_;
1029
1030 $self->size_allocate ($self->size_request);
1031 ::refresh ();
1032}
1033
1034sub add {
1035 my ($self, $widget) = @_;
1036
1037 $self->SUPER::add ($widget);
1038
1039 $widget->size_allocate ($widget->size_request);
1040}
1041
1042sub draw {
1043 my ($self) = @_;
1044
1045 $self->_draw;
1046}
1047
1048#############################################################################
1049
1050package Crossfire::Client::Widget;
1051
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1053
10541
1055

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines