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.72 by root, Tue Apr 11 18:00:45 2006 UTC vs.
Revision 1.112 by root, Sat Apr 15 12:29:46 2006 UTC

1package CFClient::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6 6use List::Util ();
7use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9 7
10use CFClient; 8use CFClient;
11 9
12our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
13 11
45 43
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47 45
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49 47
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51 49
52 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab; 52 $grab->update if $grab;
55 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
63 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
64 62
65 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
66 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
67 65
68 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
69 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
70 } 68 }
71 69
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73} 71}
72
73# convert position array to integers
74sub harmonize {
75 my ($vals) = @_;
76
77 my $rem = 0;
78
79 for ($vals) {
80 my $i = int $_ + $rem;
81 $rem += $_ - $i;
82 $_ = $i;
83 }
84}
85
86#############################################################################
87
88package CFClient::UI::Base;
89
90use strict;
91
92use SDL::OpenGL;
74 93
75sub new { 94sub new {
76 my $class = shift; 95 my $class = shift;
77 96
78 bless { 97 my $self = bless {
79 x => 0, 98 x => 0,
80 y => 0, 99 y => 0,
81 z => 0, 100 z => 0,
101 w => -1,
102 h => -1,
82 @_ 103 @_
83 }, $class 104 }, $class;
105
106 for (keys %$self) {
107 if (/^connect_(.*)$/) {
108 $self->connect ($1 => delete $self->{$_});
109 }
110 }
111
112 $self
84} 113}
85 114
86sub move { 115sub move {
87 my ($self, $x, $y, $z) = @_; 116 my ($self, $x, $y, $z) = @_;
88 $self->{x} = $x; 117 $self->{x} = $x;
97sub size_request { 126sub size_request {
98 require Carp; 127 require Carp;
99 Carp::confess "size_request is abtract"; 128 Carp::confess "size_request is abtract";
100} 129}
101 130
102sub size_allocate { 131sub _size_allocate {
103 my ($self, $w, $h) = @_; 132 my ($self, $x, $y, $w, $h) = @_;
133
134 $self->{x} = $x;
135 $self->{y} = $y;
136
137 return unless $self->{w} != $w || $self->{h} != $h;
104 138
105 $self->{w} = $w; 139 $self->{w} = $w;
106 $self->{h} = $h; 140 $self->{h} = $h;
107}
108 141
142 1
143}
144
145sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_;
147
148 $self->_size_allocate ($x, $y, $w, $h);
149}
150
109# translate global koordinates to local coordinate system 151# translate global coordinates to local coordinate system
110sub translate { 152sub translate {
111 my ($self, $x, $y) = @_; 153 my ($self, $x, $y) = @_;
112 154
113 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
114} 156}
115 157
116sub focus_in { 158sub focus_in {
117 my ($self) = @_; 159 my ($self) = @_;
118 160
119 return if $FOCUS == $self; 161 return if $FOCUS == $self;
162 return unless $self->{can_focus};
120 163
121 my $focus = $FOCUS; $FOCUS = $self; 164 my $focus = $FOCUS; $FOCUS = $self;
122 $focus->update if $focus; 165 $focus->update if $focus;
123 $FOCUS->update; 166 $FOCUS->update;
124} 167}
157 glPushMatrix; 200 glPushMatrix;
158 glTranslate $self->{x}, $self->{y}, 0; 201 glTranslate $self->{x}, $self->{y}, 0;
159 $self->_draw; 202 $self->_draw;
160 glPopMatrix; 203 glPopMatrix;
161 204
162 if ($self == $HOVER) { 205 if ($self == $HOVER && $self->{can_hover}) {
163 my ($x, $y) = @$self->{qw(x y)}; 206 my ($x, $y) = @$self{qw(x y)};
164 207
165 glColor 1, 1, 1, 0.1; 208 glColor 0, 0, 1, 0.2;
166 glEnable GL_BLEND; 209 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
167 glBegin GL_QUADS; 211 glBegin GL_QUADS;
168 glVertex $x , $y; 212 glVertex $x , $y;
169 glVertex $x + $self->{w}, $y; 213 glVertex $x + $self->{w}, $y;
170 glVertex $x + $self->{w}, $y + $self->{h}; 214 glVertex $x + $self->{w}, $y + $self->{h};
171 glVertex $x , $y + $self->{h}; 215 glVertex $x , $y + $self->{h};
178 my ($self) = @_; 222 my ($self) = @_;
179 223
180 warn "no draw defined for $self\n"; 224 warn "no draw defined for $self\n";
181} 225}
182 226
183sub bbox {
184 my ($self) = @_;
185 my ($w, $h) = $self->size_request;
186 (
187 $self->{x},
188 $self->{y},
189 $self->{x} = $w,
190 $self->{y} = $h
191 )
192}
193
194sub find_widget { 227sub find_widget {
195 my ($self, $x, $y) = @_; 228 my ($self, $x, $y) = @_;
196 229
197 return $self 230 return $self
198 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 231 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
199 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 232 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
200 233
201 () 234 ()
202} 235}
203 236
204sub del_parent { $_[0]->{parent} = undef }
205
206sub set_parent { 237sub set_parent {
207 my ($self, $par) = @_; 238 my ($self, $par) = @_;
208 239
209 $self->{parent} = $par; 240 $self->{parent} = $par;
210 Scalar::Util::weaken $self->{parent}; 241 Scalar::Util::weaken $self->{parent};
240 #$self->deactivate; 271 #$self->deactivate;
241} 272}
242 273
243############################################################################# 274#############################################################################
244 275
245package CFClient::Widget::DrawBG; 276package CFClient::UI::DrawBG;
246 277
247our @ISA = CFClient::Widget::; 278our @ISA = CFClient::UI::Base::;
248 279
249use strict; 280use strict;
250use SDL::OpenGL; 281use SDL::OpenGL;
251 282
252sub new { 283sub new {
253 my $class = shift; 284 my $class = shift;
254 285
255 # range [value, low, high, page] 286 # range [value, low, high, page]
256 287
257 $class->SUPER::new ( 288 $class->SUPER::new (
258 bg => [0, 0, 0, 0.4], 289 bg => [0, 0, 0, 0.2],
259 active_bg => [1, 1, 1], 290 active_bg => [1, 1, 1, 0.5],
260 @_ 291 @_
261 ) 292 )
262} 293}
263 294
264sub _draw { 295sub _draw {
265 my ($self) = @_; 296 my ($self) = @_;
266 297
267 my ($w, $h) = @$self{qw(w h)}; 298 my ($w, $h) = @$self{qw(w h)};
268 299
300 glEnable GL_BLEND;
301 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
269 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 302 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
303
270 glBegin GL_QUADS; 304 glBegin GL_QUADS;
271 glVertex 0 , 0; 305 glVertex 0 , 0;
272 glVertex 0 , $h; 306 glVertex 0 , $h;
273 glVertex $w, $h; 307 glVertex $w, $h;
274 glVertex $w, 0; 308 glVertex $w, 0;
275 glEnd; 309 glEnd;
276}
277 310
278############################################################################# 311 glDisable GL_BLEND;
312}
279 313
314#############################################################################
315
280package CFClient::Widget::Empty; 316package CFClient::UI::Empty;
281 317
282our @ISA = CFClient::Widget::; 318our @ISA = CFClient::UI::Base::;
283 319
284sub size_request { 320sub size_request {
285 (0, 0) 321 (0, 0)
286} 322}
287 323
288sub draw { } 324sub draw { }
289 325
290############################################################################# 326#############################################################################
291 327
292package CFClient::Widget::Container; 328package CFClient::UI::Container;
293 329
294our @ISA = CFClient::Widget::; 330our @ISA = CFClient::UI::Base::;
295 331
296sub new { 332sub new {
297 my ($class, %arg) = @_; 333 my ($class, %arg) = @_;
298 334
299 my $children = delete $arg{children} || []; 335 my $children = delete $arg{children} || [];
303 339
304 $self 340 $self
305} 341}
306 342
307sub add { 343sub add {
308 my ($self, $chld, $expand) = @_; 344 my ($self, $chld) = @_;
309 345
310 $chld->{expand} = $expand;
311 $chld->set_parent ($self); 346 $chld->set_parent ($self);
312 347
313 $self->{children} = [ 348 $self->{children} = [
314 sort { $a->{z} <=> $b->{z} } 349 sort { $a->{z} <=> $b->{z} }
315 @{$self->{children}}, $chld 350 @{$self->{children}}, $chld
316 ]; 351 ];
317 352
318 $self->size_allocate ($self->{w}, $self->{h}) 353 $self->{w} = $self->{h} = -1;
319 if $self->{w}; #TODO: check for "realised state" 354 $self->update;
320} 355}
321 356
322sub remove { 357sub remove {
323 my ($self, $widget) = @_; 358 my ($self, $widget) = @_;
324 359
325 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
326 361
327 $self->size_allocate ($self->{w}, $self->{h}); 362 $self->size_allocate (0, 0, $self->{w}, $self->{h});
328} 363}
329 364
330sub find_widget { 365sub find_widget {
331 my ($self, $x, $y) = @_; 366 my ($self, $x, $y) = @_;
332 367
349 $_->draw for @{$self->{children}}; 384 $_->draw for @{$self->{children}};
350} 385}
351 386
352############################################################################# 387#############################################################################
353 388
354package CFClient::Widget::Bin; 389package CFClient::UI::Bin;
355 390
356our @ISA = CFClient::Widget::Container::; 391our @ISA = CFClient::UI::Container::;
357 392
358sub new { 393sub new {
359 my ($class, %arg) = @_; 394 my ($class, %arg) = @_;
360 395
361 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; 396 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
362 397
363 $class->SUPER::new (children => [$child], %arg) 398 $class->SUPER::new (children => [$child], %arg)
364} 399}
365 400
366sub add { 401sub add {
374sub remove { 409sub remove {
375 my ($self, $widget) = @_; 410 my ($self, $widget) = @_;
376 411
377 $self->SUPER::remove ($widget); 412 $self->SUPER::remove ($widget);
378 413
379 $self->{children} = [new CFClient::Widget::Empty] 414 $self->{children} = [new CFClient::UI::Empty]
380 unless @{$self->{children}}; 415 unless @{$self->{children}};
381} 416}
382 417
383sub child { $_[0]->{children}[0] } 418sub child { $_[0]->{children}[0] }
384 419
385sub size_request { 420sub size_request {
386 $_[0]{children}[0]->size_request 421 $_[0]{children}[0]->size_request
387} 422}
388 423
389sub size_allocate { 424sub size_allocate {
390 my ($self, $w, $h) = @_; 425 my ($self, $x, $y, $w, $h) = @_;
391 426
392 return unless $self->{w} != $w || $self->{h} != $h; 427 $self->_size_allocate ($x, $y, $w, $h) or return;
393 428
394 $self->SUPER::size_allocate ($w, $h);
395 $self->{children}[0]->size_allocate ($w, $h); 429 $self->{children}[0]->size_allocate (0, 0, $w, $h);
396} 430}
397 431
398############################################################################# 432#############################################################################
399 433
400package CFClient::Widget::Window; 434package CFClient::UI::Window;
401 435
402our @ISA = CFClient::Widget::Bin::; 436our @ISA = CFClient::UI::Bin::;
403 437
404use SDL::OpenGL; 438use SDL::OpenGL;
405 439
406sub new { 440sub new {
407 my ($class, %arg) = @_; 441 my ($class, %arg) = @_;
418} 452}
419 453
420sub render_chld { 454sub render_chld {
421 my ($self) = @_; 455 my ($self) = @_;
422 456
423 $self->{texture} = 457 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
424 CFClient::Texture->new_from_opengl ( 458 glClearColor 0, 0, 0, 1;
425 $self->{w}, $self->{h}, sub { $self->child->draw } 459 glClear GL_COLOR_BUFFER_BIT;
426 ); 460 $self->child->draw;
461 };
427} 462}
428 463
429sub size_allocate { 464sub size_allocate {
430 my ($self, $w, $h) = @_; 465 my ($self, $x, $y, $w, $h) = @_;
431 466
432 return unless $self->{w} != $w || $self->{h} != $h; 467 $self->_size_allocate ($x, $y, $w, $h) or return;
433 468
434 $self->{w} = $w;
435 $self->{h} = $h;
436
437 $self->child->size_allocate ($w, $h); 469 $self->child->size_allocate (0, 0, $w, $h);
438 470
439 $self->render_chld; 471 $self->render_chld;
440} 472}
441 473
442sub _draw { 474sub _draw {
446 478
447 my $tex = $self->{texture} 479 my $tex = $self->{texture}
448 or return; 480 or return;
449 481
450 glEnable GL_BLEND; 482 glEnable GL_BLEND;
483 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
451 glEnable GL_TEXTURE_2D; 484 glEnable GL_TEXTURE_2D;
452 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 485 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
453 486
454 $tex->draw_quad (0, 0, $w, $h); 487 $tex->draw_quad (0, 0, $w, $h);
455 488
457 glDisable GL_TEXTURE_2D; 490 glDisable GL_TEXTURE_2D;
458} 491}
459 492
460############################################################################# 493#############################################################################
461 494
462package CFClient::Widget::Frame; 495package CFClient::UI::Frame;
463 496
464our @ISA = CFClient::Widget::Bin::; 497our @ISA = CFClient::UI::Bin::;
465 498
466use SDL::OpenGL; 499use SDL::OpenGL;
467 500
468sub size_request { 501sub size_request {
469 my ($self) = @_; 502 my ($self) = @_;
474 507
475 map { $_ + 4 } $chld->size_request; 508 map { $_ + 4 } $chld->size_request;
476} 509}
477 510
478sub size_allocate { 511sub size_allocate {
479 my ($self, $w, $h) = @_; 512 my ($self, $x, $y, $w, $h) = @_;
480 513
481 return unless $self->{w} != $w || $self->{h} != $h; 514 $self->_size_allocate ($x, $y, $w, $h) or return;
482 515
483 $self->{w} = $w;
484 $self->{h} = $h;
485
486 $self->child->size_allocate ($w - 4, $h - 4); 516 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
487 $self->child->move (2, 2);
488} 517}
489 518
490sub _draw { 519sub _draw {
491 my ($self) = @_; 520 my ($self) = @_;
492 521
505 $chld->draw; 534 $chld->draw;
506} 535}
507 536
508############################################################################# 537#############################################################################
509 538
510package CFClient::Widget::FancyFrame; 539package CFClient::UI::FancyFrame;
511 540
512our @ISA = CFClient::Widget::Bin::; 541our @ISA = CFClient::UI::Bin::;
513 542
514use SDL::OpenGL; 543use SDL::OpenGL;
515 544
516my @tex = 545my @tex =
517 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
518 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 547 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
519 548
549sub new {
550 my $class = shift;
551
552 # TODO: user_x, user_y, overwrite moveto?
553
554 $class->SUPER::new (
555 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8,
558 @_
559 )
560}
561
520sub size_request { 562sub size_request {
521 my ($self) = @_; 563 my ($self) = @_;
522 564
565 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
566
523 my ($w, $h) = $self->SUPER::size_request; 567 my ($w, $h) = $self->SUPER::size_request;
524 568
525 $h += $tex[1]->{h}; 569 (
526 $h += $tex[4]->{h}; 570 $w + $self->{border} * 2,
527 $w += $tex[2]->{w}; 571 $h + $self->{border} * 2,
528 $w += $tex[3]->{w}; 572 )
529
530 ($w, $h)
531} 573}
532 574
533sub size_allocate { 575sub size_allocate {
534 my ($self, $w, $h) = @_; 576 my ($self, $x, $y, $w, $h) = @_;
535 577
536 return unless $self->{w} != $w || $self->{h} != $h; 578 $self->_size_allocate ($x, $y, $w, $h) or return;
537 579
538 $self->SUPER::size_allocate ($w, $h); 580 $h -= List::Util::max 0, $self->{border} * 2;
581 $w -= List::Util::max 0, $self->{border} * 2;
539 582
540 $h -= $tex[1]->{h}; 583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h);
541 $h -= $tex[4]->{h}; 584}
542 $w -= $tex[2]->{w};
543 $w -= $tex[3]->{w};
544 585
545 $h = $h < 0 ? 0 : $h; 586sub button_down {
546 $w = $w < 0 ? 0 : $w; 587 my ($self, $ev, $x, $y) = @_;
547 588
548 my $child = $self->child; 589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border}
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) {
549 591
550 $child->size_allocate ($w, $h); 592 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
551 $child->move ($tex[3]->{w}, $tex[1]->{h}); 593 my ($bw, $bh) = ($self->{w}, $self->{h});
594
595 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_;
597
598 ($x, $y) = ($ev->motion_x, $ev->motion_y);
599
600 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy;
602 $self->update;
603 };
604
605 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) {
607
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
609 my ($bx, $by) = ($self->{x}, $self->{y});
610
611 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_;
613
614 ($x, $y) = ($ev->motion_x, $ev->motion_y);
615
616 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update;
618 };
619 }
620}
621
622sub button_up {
623 my ($self, $ev, $x, $y) = @_;
624
625 delete $self->{motion};
626}
627
628sub mouse_motion {
629 my ($self, $ev, $x, $y) = @_;
630
631 $self->{motion}->($ev, $x, $y) if $self->{motion};
552} 632}
553 633
554sub _draw { 634sub _draw {
555 my ($self) = @_; 635 my ($self) = @_;
556 636
557 my ($w, $h) = ($self->{w}, $self->{h}); 637 my ($w, $h ) = ($self->{w}, $self->{h});
558 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 638 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
559 639
560 glEnable GL_BLEND; 640 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
561 glEnable GL_TEXTURE_2D; 642 glEnable GL_TEXTURE_2D;
562 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
564 644
565 my $top = $tex[1]; 645 glColor @{ $self->{border_bg} };
566 $top->draw_quad (0, 0, $w, $top->{h}); 646 $tex[1]->draw_quad (0, 0, $w, $self->{border});
567 647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch);
568 my $left = $tex[3]; 648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch);
569 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border});
570
571 my $right = $tex[2];
572 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
573
574 my $bottom = $tex[4];
575 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
576 650
577 my $bg = $tex[0]; 651 my $bg = $tex[0];
578 glBindTexture GL_TEXTURE_2D, $bg->{name};
579 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
580 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
581 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
582 652
653 # TODO: repeat texture not scale
583 my $rep_x = $cw / $bg->{w}; 654 my $rep_x = $cw / $bg->{w};
584 my $rep_y = $ch / $bg->{h}; 655 my $rep_y = $ch / $bg->{h};
585 656
657 glColor @{ $self->{bg} };
658
659 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1;
586 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch);
587 663
664 glDisable GL_TEXTURE_2D;
588 glDisable GL_BLEND; 665 glDisable GL_BLEND;
589 glDisable GL_TEXTURE_2D;
590 666
591 $self->child->draw; 667 $self->child->draw;
592
593} 668}
594 669
595############################################################################# 670#############################################################################
596 671
597package CFClient::Widget::Table; 672package CFClient::UI::Table;
598 673
599our @ISA = CFClient::Widget::; 674our @ISA = CFClient::UI::Base::;
675
676use List::Util qw(max sum);
600 677
601use SDL::OpenGL; 678use SDL::OpenGL;
679
680sub new {
681 my $class = shift;
682
683 $class->SUPER::new (
684 col_expand => [],
685 @_
686 )
687}
602 688
603sub add { 689sub add {
604 my ($self, $x, $y, $chld) = @_; 690 my ($self, $x, $y, $chld) = @_;
605 my $old_chld = $self->{children}[$y][$x];
606 691
607 $self->{children}[$y][$x] = $chld; 692 $self->{children}[$y][$x] = $chld;
608 $chld->set_parent ($self); 693 $chld->set_parent ($self);
694
695 $self->{w} = $self->{h} = -1;
609 $self->update; 696 $self->update;
610} 697}
611 698
612sub max_row_height { 699sub get_wh {
613 my ($self, $row) = @_; 700 my ($self) = @_;
614 701
615 my $hs = 0; 702 my (@w, @h);
616 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 703
704 for my $y (0 .. $#{$self->{children}}) {
617 my $c = $self->{children}->[$row]->[$xi]; 705 my $row = $self->{children}[$y]
618 if ($c) { 706 or next;
707
708 for my $x (0 .. $#$row) {
709 my $widget = $row->[$x]
710 or next;
619 my ($w, $h) = $c->size_request; 711 my ($w, $h) = $widget->size_request;
620 if ($hs < $h) { $hs = $h } 712
713 $w[$x] = max $w[$x], $w;
714 $h[$y] = max $h[$y], $h;
621 } 715 }
622 } 716 }
623 return $hs;
624}
625 717
626sub max_col_width { 718 (\@w, \@h)
719}
720
721sub size_request {
627 my ($self, $col) = @_; 722 my ($self) = @_;
628 723
724 my ($ws, $hs) = $self->get_wh;
725
726 (
727 (sum @$ws),
728 (sum @$hs),
729 )
730}
731
732sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736
737 my ($ws, $hs) = $self->get_wh;
738
739 my $req_w = sum @$ws;
740 my $req_h = sum @$hs;
741
742 # TODO: nicer code && do row_expand
743 my @col_expand = @{$self->{col_expand}};
744 @col_expand = (1) x @$ws unless @col_expand;
745 my $col_expand = (sum @col_expand) || 1;
746
747 # linearly scale sizes
748 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
749 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
750
751 CFClient::UI::harmonize $ws;
752 CFClient::UI::harmonize $hs;
753
754 my $y;
755
756 for my $r (0 .. $#{$self->{children}}) {
757 my $row = $self->{children}[$r]
758 or next;
759
629 my $ws = 0; 760 my $x = 0;
630 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 761 my $row_h = $hs->[$r];
631 my $c = ($self->{children}->[$yi] || [])->[$col]; 762
632 if ($c) { 763 for my $c (0 .. $#$row) {
633 my ($w, $h) = $c->size_request; 764 my $col_w = $ws->[$c];
634 if ($ws < $w) { $ws = $w } 765
766 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h);
768 }
769
770 $x += $col_w;
635 } 771 }
772
773 $y += $row_h;
636 } 774 }
637 return $ws; 775
638} 776}
777
778sub find_widget {
779 my ($self, $x, $y) = @_;
780
781 $x -= $self->{x};
782 $y -= $self->{y};
783
784 my $res;
785
786 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
787 $res = $_->find_widget ($x, $y)
788 and return $res;
789 }
790
791 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
792}
793
794sub _draw {
795 my ($self) = @_;
796
797 for (grep $_, @{$self->{children}}) {
798 $_->draw for grep $_, @$_;
799 }
800}
801
802#############################################################################
803
804package CFClient::UI::HBox;
805
806# TODO: wrap into common Box base class
807
808our @ISA = CFClient::UI::Container::;
639 809
640sub size_request { 810sub size_request {
641 my ($self) = @_; 811 my ($self) = @_;
642 812
813 my @alloc = map [$_->size_request], @{$self->{children}};
814
815 (
816 (List::Util::sum map $_->[0], @alloc),
817 (List::Util::max map $_->[1], @alloc),
818 )
819}
820
821sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825
643 my ($hs, $ws) = (0, 0); 826 ($h, $w) = ($w, $h);
644 827
645 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 828 my $children = $self->{children};
646 $hs += $self->max_row_height ($yi);
647 }
648 829
649 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 830 my @h = map +($_->size_request)[0], @$children;
650 my $wm = 0; 831
651 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 832 my $req_h = List::Util::sum @h;
652 $wm += $self->max_col_width ($xi) 833
834 if ($req_h > $h) {
835 # ah well, not enough space
836 $_ *= $h / $req_h for @h;
837 } else {
838 my $exp = List::Util::sum map $_->{expand}, @$children;
839 $exp ||= 1;
840
841 for (0 .. $#$children) {
842 my $child = $children->[$_];
843
844 my $alloc_h = $h[$_];
845 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
846 $h[$_] = $alloc_h;
653 } 847 }
654 if ($ws < $wm) { $ws = $wm }
655 } 848 }
656 849
657 return ($ws, $hs); 850 CFClient::UI::harmonize \@h;
658}
659
660sub _draw {
661 my ($self) = @_;
662 851
663 my $y = 0; 852 my $y = 0;
664 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 853 for (0 .. $#$children) {
854 my $child = $children->[$_];
665 my $x = 0; 855 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w);
666 857
667 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 858 $y += $h;
668
669 my $c = $self->{children}->[$yi]->[$xi];
670 if ($c) {
671 $c->move ($x, $y, 0); #TODO: Move to size_request
672 $c->draw if $c;
673 }
674
675 $x += $self->max_col_width ($xi);
676 }
677
678 $y += $self->max_row_height ($yi);
679 } 859 }
680} 860}
681 861
682############################################################################# 862#############################################################################
683 863
684package CFClient::Widget::VBox; 864package CFClient::UI::VBox;
685 865
866# TODO: wrap into common Box base class
867
686our @ISA = CFClient::Widget::Container::; 868our @ISA = CFClient::UI::Container::;
687
688use SDL::OpenGL;
689 869
690sub size_request { 870sub size_request {
691 my ($self) = @_; 871 my ($self) = @_;
692 872
693 my @alloc = map [$_->size_request], @{$self->{children}}; 873 my @alloc = map [$_->size_request], @{$self->{children}};
697 (List::Util::sum map $_->[1], @alloc), 877 (List::Util::sum map $_->[1], @alloc),
698 ) 878 )
699} 879}
700 880
701sub size_allocate { 881sub size_allocate {
702 my ($self, $w, $h) = @_; 882 my ($self, $x, $y, $w, $h) = @_;
703 883
704 return unless $self->{w} != $w || $self->{h} != $h; 884 $self->_size_allocate ($x, $y, $w, $h);
705
706 $self->{w} = $w;
707 $self->{h} = $h;
708
709 return unless $self->{h};
710 885
711 my $children = $self->{children}; 886 my $children = $self->{children};
712 887
713 my @h = map +($_->size_request)[1], @$children; 888 my @h = map +($_->size_request)[1], @$children;
714 889
715 my $req_h = List::Util::sum @h; 890 my $req_h = List::Util::sum @h;
716 891
717 if ($req_h > $h) { 892 if ($req_h > $h) {
718 # ah well, not enough space 893 # ah well, not enough space
719 $_ = $h[$_] * $h / $req_h for @h; 894 $_ *= $h / $req_h for @h;
720 } else { 895 } else {
721 my @exp = grep $_->{expand}, @$children; 896 my $exp = List::Util::sum map $_->{expand}, @$children;
722 @exp = @$children unless @exp; 897 $exp ||= 1;
723 898
724 my %exp = map +($_ => 1), @exp;
725
726 for (0 .. $#$children) { 899 for (0 .. $#$children) {
727 my $child = $children->[$_]; 900 my $child = $children->[$_];
728 901
729 my $alloc_h = $h[$_]; 902 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
730 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
731 $h[$_] = $alloc_h;
732 } 903 }
733 } 904 }
905
906 CFClient::UI::harmonize \@h;
734 907
735 my $y = 0; 908 my $y = 0;
736 for (0 .. $#$children) { 909 for (0 .. $#$children) {
737 my $child = $children->[$_]; 910 my $child = $children->[$_];
738 my $h = $h[$_]; 911 my $h = $h[$_];
739 $child->move (0, $y);
740 $child->size_allocate ($w, $h); 912 $child->size_allocate (0, $y, $w, $h);
741 913
742 $y += $h; 914 $y += $h;
743 } 915 }
744} 916}
745 917
746############################################################################# 918#############################################################################
747 919
748package CFClient::Widget::Label; 920package CFClient::UI::Label;
749 921
750our @ISA = CFClient::Widget::; 922our @ISA = CFClient::UI::Base::;
751 923
752use SDL::OpenGL; 924use SDL::OpenGL;
753 925
754sub new { 926sub new {
755 my ($class, %arg) = @_; 927 my ($class, %arg) = @_;
756 928
757 my $self = $class->SUPER::new ( 929 my $self = $class->SUPER::new (
758 fg => [1, 1, 1], 930 fg => [1, 1, 1],
759 height => $::FONTSIZE, 931 fontsize => $::FONTSIZE,
760 text => "", 932 text => "",
761 align => -1, 933 align => -1,
934 padding => 2,
762 layout => new CFClient::Layout, 935 layout => new CFClient::Layout,
763 %arg 936 %arg
764 ); 937 );
765 938
766 $self->set_text ($self->{text}); 939 $self->set_text ($self->{text});
767 940
783 956
784 $self->{text} = $text; 957 $self->{text} = $text;
785 $self->{layout}->set_markup ($text); 958 $self->{layout}->set_markup ($text);
786 959
787 delete $self->{texture}; 960 delete $self->{texture};
961# $self->{w} = $self->{h} = -1;
788 $self->update; 962 $self->update;
789} 963}
790 964
791sub get_text { 965sub get_text {
792 my ($self, $text) = @_; 966 my ($self, $text) = @_;
796 970
797sub size_request { 971sub size_request {
798 my ($self) = @_; 972 my ($self) = @_;
799 973
800 $self->{layout}->set_width; 974 $self->{layout}->set_width;
801 $self->{layout}->set_height ($self->{height}); 975 $self->{layout}->set_height ($self->{fontsize});
802 $self->{layout}->size 976 my ($w, $h) = $self->{layout}->size;
803# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack 977
804# ( 978 (
805# $self->{texture}{w}, 979 $w + $self->{padding} * 2,
806# $self->{texture}{h}, 980 $h + $self->{padding} * 2,
807# ) 981 )
808# } else {
809# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
810#
811# ($w, $h)
812# }
813} 982}
814 983
815sub size_allocate { 984sub size_allocate {
816 my ($self, $w, $h) = @_; 985 my ($self, $x, $y, $w, $h) = @_;
817 986
818 return unless $self->{w} != $w || $self->{h} != $h; 987 $self->_size_allocate ($x, $y, $w, $h) or return;
819 988
820 $self->SUPER::size_allocate ($w, $h);
821 delete $self->{texture}; 989 delete $self->{texture};
822} 990}
823 991
824sub update { 992sub update {
825 my ($self) = @_; 993 my ($self) = @_;
831sub _draw { 999sub _draw {
832 my ($self) = @_; 1000 my ($self) = @_;
833 1001
834 my $tex = $self->{texture} ||= do { 1002 my $tex = $self->{texture} ||= do {
835 $self->{layout}->set_width ($self->{w}); 1003 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
836 new_from_layout CFClient::Texture $self->{layout}; 1005 new_from_layout CFClient::Texture $self->{layout}
837 }; 1006 };
838 1007
839 glEnable GL_BLEND; 1008 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
840 glEnable GL_TEXTURE_2D; 1010 glEnable GL_TEXTURE_2D;
841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
843 1012
844 glColor @{$self->{fg}}; 1013 glColor @{$self->{fg}};
845 1014
846 my $x = 1015 my $x =
847 $self->{align} < 0 ? 0 1016 $self->{align} < 0 ? $self->{padding}
848 : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} 1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
849 : ($self->{w} + $self->{texture}{w}) * 0.5; 1018 : ($self->{w} - $tex->{w}) * 0.5;
850 1019
851 $tex->draw_quad ($x, 0); 1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5);
852 1021
1022 glDisable GL_TEXTURE_2D;
853 glDisable GL_BLEND; 1023 glDisable GL_BLEND;
854 glDisable GL_TEXTURE_2D;
855} 1024}
856 1025
857############################################################################# 1026#############################################################################
858 1027
859package CFClient::Widget::Entry; 1028package CFClient::UI::Entry;
860 1029
861our @ISA = CFClient::Widget::Label::; 1030our @ISA = CFClient::UI::Label::;
862 1031
863use SDL; 1032use SDL;
864use SDL::OpenGL; 1033use SDL::OpenGL;
865 1034
866sub new { 1035sub new {
867 my $class = shift; 1036 my $class = shift;
868 1037
869 $class->SUPER::new ( 1038 $class->SUPER::new (
870 fg => [1, 1, 1], 1039 fg => [1, 1, 1],
871 bg => [0, 0, 0, 0.4], 1040 bg => [0, 0, 0, 0.2],
872 active_bg => [1, 1, 1], 1041 active_bg => [1, 1, 1, 0.5],
873 active_fg => [0, 0, 0], 1042 active_fg => [0, 0, 0],
1043 can_hover => 1,
1044 can_focus => 1,
874 @_ 1045 @_
875 ) 1046 )
876} 1047}
877 1048
878sub _set_text { 1049sub _set_text {
879 my ($self, $text) = @_; 1050 my ($self, $text) = @_;
880 1051
1052 my $old_text = $self->{text};
1053
881 $self->{last_activity} = $::NOW; 1054 $self->{last_activity} = $::NOW;
882 1055
883 $self->{text} = $text; 1056 $self->{text} = $text;
884 $self->{layout}->set_width ($self->{w}); 1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
885 1059
886 $text =~ s/./*/g if $self->{hidden}; 1060 $text =~ s/./*/g if $self->{hidden};
887 1061
888
889 $self->{layout}->set_markup ($self->escape_text ($text)); 1062 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
890 1063
891 $text = substr $text, 0, $self->{cursor}; 1064 $text = substr $text, 0, $self->{cursor};
892 utf8::encode $text; 1065 utf8::encode $text;
893 1066
894 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1067 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1068
1069 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1070 if $old_text ne $self->{text};
895} 1071}
896 1072
897sub size_request { 1073sub size_request {
898 my ($self) = @_; 1074 my ($self) = @_;
899 1075
901 1077
902 ($w + 1, $h) # add 1 for cursor 1078 ($w + 1, $h) # add 1 for cursor
903} 1079}
904 1080
905sub size_allocate { 1081sub size_allocate {
906 my ($self, $w, $h) = @_; 1082 my ($self, $x, $y, $w, $h) = @_;
907 1083
908 return unless $self->{w} != $w || $self->{h} != $h;
909
910 $self->SUPER::size_allocate ($w, $h); 1084 $self->SUPER::size_allocate ($x, $y, $w, $h);
911 1085
912 $self->_set_text ($self->{text}); 1086 $self->_set_text ($self->{text});
913} 1087}
914 1088
915sub set_text { 1089sub set_text {
936 substr $text, $self->{cursor}, 1, ""; 1110 substr $text, $self->{cursor}, 1, "";
937 } elsif ($sym == SDLK_LEFT) { 1111 } elsif ($sym == SDLK_LEFT) {
938 --$self->{cursor} if $self->{cursor}; 1112 --$self->{cursor} if $self->{cursor};
939 } elsif ($sym == SDLK_RIGHT) { 1113 } elsif ($sym == SDLK_RIGHT) {
940 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1114 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) {
1116 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) {
1118 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) {
1120 $self->emit ('escape');
941 } elsif ($uni) { 1121 } elsif ($uni) {
942 substr $text, $self->{cursor}++, 0, chr $uni; 1122 substr $text, $self->{cursor}++, 0, chr $uni;
943 } 1123 }
944 1124
945 $self->_set_text ($text); 1125 $self->_set_text ($text);
960 $self->SUPER::button_down ($ev, $x, $y); 1140 $self->SUPER::button_down ($ev, $x, $y);
961 1141
962 my $idx = $self->{layout}->xy_to_index ($x, $y); 1142 my $idx = $self->{layout}->xy_to_index ($x, $y);
963 1143
964 # byte-index to char-index 1144 # byte-index to char-index
965 my $text = $self->{layout}; 1145 my $text = $self->{text};
966 utf8::encode $text; 1146 utf8::encode $text;
967 $self->{cursor} = length substr $text, 0, $idx; 1147 $self->{cursor} = length substr $text, 0, $idx;
968 1148
969 $self->_set_text ($self->{text}); 1149 $self->_set_text ($self->{text});
970 $self->update; 1150 $self->update;
985 $self->{fg} = $self->{active_fg}; 1165 $self->{fg} = $self->{active_fg};
986 } else { 1166 } else {
987 glColor @{$self->{bg}}; 1167 glColor @{$self->{bg}};
988 } 1168 }
989 1169
1170 glEnable GL_BLEND;
1171 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
990 glBegin GL_QUADS; 1172 glBegin GL_QUADS;
991 glVertex 0 , 0; 1173 glVertex 0 , 0;
992 glVertex 0 , $self->{h}; 1174 glVertex 0 , $self->{h};
993 glVertex $self->{w}, $self->{h}; 1175 glVertex $self->{w}, $self->{h};
994 glVertex $self->{w}, 0; 1176 glVertex $self->{w}, 0;
995 glEnd; 1177 glEnd;
1178 glDisable GL_BLEND;
996 1179
997 $self->SUPER::_draw; 1180 $self->SUPER::_draw;
998 1181
999 #TODO: force update every cursor change :( 1182 #TODO: force update every cursor change :(
1000 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1004 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1005 glEnd; 1188 glEnd;
1006 } 1189 }
1007} 1190}
1008 1191
1009############################################################################# 1192package CFClient::UI::LineEntry;
1010 1193
1011package CFClient::Widget::Slider; 1194our @ISA = CFClient::UI::Entry::;
1012 1195
1013use strict; 1196use SDL;
1014
1015use SDL::OpenGL; 1197use SDL::OpenGL;
1016use SDL::OpenGL::Constants;
1017 1198
1018our @ISA = CFClient::Widget::DrawBG::; 1199sub key_down {
1019
1020sub size_request {
1021 my ($self) = @_; 1200 my ($self, $ev) = @_;
1022 1201
1023 my $w = 50; 1202 my $sym = $ev->key_sym;
1024 my $h = 10;
1025 1203
1026 $self->{vertical} ? ($h, $w) : ($w, $h) 1204 if ($sym == SDLK_RETURN) {
1205 $self->emit (activate => $self->get_text);
1206 $self->update;
1207
1208 } else {
1209 $self->SUPER::key_down ($ev);
1210 }
1211
1027} 1212}
1213
1214#############################################################################
1215
1216package CFClient::UI::Button;
1217
1218our @ISA = CFClient::UI::Label::;
1219
1220use SDL;
1221use SDL::OpenGL;
1222
1223my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1225 qw(b1_button_active.png);
1028 1226
1029sub new { 1227sub new {
1030 my $class = shift; 1228 my $class = shift;
1031 1229
1230 $class->SUPER::new (
1231 padding => 4,
1232 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0],
1235 can_hover => 1,
1236 @_
1237 )
1238}
1239
1240sub button_up {
1241 my ($self, $ev, $x, $y) = @_;
1242
1243 if ($x >= 0 && $x < $self->{w}
1244 && $y >= 0 && $y < $self->{h}) {
1245 $self->emit ("activate");
1246 }
1247}
1248
1249sub _draw {
1250 my ($self) = @_;
1251
1252 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258
1259 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg};
1261 }
1262
1263 glBindTexture GL_TEXTURE_2D, $tex->{name};
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1265
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
1267
1268 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND;
1270
1271 $self->SUPER::_draw;
1272}
1273
1274#############################################################################
1275
1276package CFClient::UI::CheckBox;
1277
1278our @ISA = CFClient::UI::DrawBG::;
1279
1280my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283
1284use SDL;
1285use SDL::OpenGL;
1286
1287sub new {
1288 my $class = shift;
1289
1290 $class->SUPER::new (
1291 padding => 2,
1292 fg => [1, 1, 1],
1293 active_fg => [1, 1, 0],
1294 state => 0,
1295 can_hover => 1,
1296 @_
1297 )
1298}
1299
1300sub size_request {
1301 my ($self) = @_;
1302
1303 ($self->{padding} * 2 + 6) x 2
1304}
1305
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down {
1313 my ($self, $ev, $x, $y) = @_;
1314
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1317 $self->{state} = !$self->{state};
1318 $self->emit (changed => $self->{state});
1319 }
1320}
1321
1322sub _draw {
1323 my ($self) = @_;
1324
1325 $self->SUPER::_draw;
1326
1327 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1328
1329 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1330
1331 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1332
1333 glEnable GL_BLEND;
1334 glEnable GL_TEXTURE_2D;
1335 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1336
1337 my $tex = $self->{state} ? $tex[1] : $tex[0];
1338
1339 $tex->draw_quad (0, 0, $s, $s);
1340
1341 glDisable GL_TEXTURE_2D;
1342 glDisable GL_BLEND;
1343}
1344
1345#############################################################################
1346
1347package CFClient::UI::Slider;
1348
1349use strict;
1350
1351use SDL::OpenGL;
1352
1353our @ISA = CFClient::UI::DrawBG::;
1354
1355my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1357 qw(s1_slider.png s1_slider_bg.png);
1358
1359sub new {
1360 my $class = shift;
1361
1032 # range [value, low, high, page] 1362 # range [value, low, high, page]
1033 1363
1364 # TODO: 0-width page
1365 # TODO: req_w/h are wrong with vertical
1366 # TODO: calculations are off
1034 $class->SUPER::new ( 1367 my $self = $class->SUPER::new (
1035 fg => [1, 1, 1], 1368 fg => [1, 1, 1],
1036 active_fg => [0, 0, 0], 1369 active_fg => [0, 0, 0],
1037 range => [0, 0, 100, 10], 1370 range => [0, 0, 100, 10],
1371 req_w => 40,
1372 req_h => 13,
1038 vertical => 1, 1373 vertical => 0,
1374 can_hover => 1,
1375 inner_pad => 5,
1039 @_ 1376 @_
1040 ) 1377 );
1378
1379 $self
1380}
1381
1382sub size_request {
1383 my ($self) = @_;
1384
1385 my $w = $self->{req_w};
1386 my $h = $self->{req_h};
1387
1388 $self->{vertical} ? ($h, $w) : ($w, $h)
1041} 1389}
1042 1390
1043sub button_down { 1391sub button_down {
1044 my ($self, $ev, $x, $y) = @_; 1392 my ($self, $ev, $x, $y) = @_;
1045 1393
1053 if ($GRAB == $self) { 1401 if ($GRAB == $self) {
1054 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1402 my ($value, $lo, $hi, $page) = @{$self->{range}};
1055 1403
1056 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1404 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1057 1405
1406 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1407 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1408
1409 $x -= $inner_pad_px; # substract the padding
1058 $x = $x * ($hi - $lo) / $w + $lo; 1410 $x = $x * ($hi - $lo) / $inner_w + $lo;
1059 $x = $lo if $x < $lo; 1411 $x = $lo if $x < $lo;
1060 $x = $hi - $page if $x > $hi - $page; 1412 $x = $hi - $page if $x > $hi - $page;
1061 $self->{range}[0] = $x; 1413 $self->{range}[0] = $x;
1062 1414
1063 $self->emit (changed => $x); 1415 $self->emit (changed => $x);
1064 $self->update; 1416 $self->update;
1065 } 1417 }
1418}
1419
1420# the inner_* stuff is for generating a padding for the slider handle,
1421# so that the handle doesn't leave the texture. This calculation isn't 100%
1422# correct propably, but it does the job for now
1423sub _calc_inner_pad_px {
1424 my ($self, $w) = @_;
1425 ($w / 100) * $self->{inner_pad} # % to pixels
1066} 1426}
1067 1427
1068sub _draw { 1428sub _draw {
1069 my ($self) = @_; 1429 my ($self) = @_;
1070 1430
1084 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1444 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1085 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1445 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1086 1446
1087 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1447 my ($value, $lo, $hi, $page) = @{$self->{range}};
1088 1448
1449 $hi = $value + 1 if $lo == $hi;
1450
1451 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1452 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1453
1089 $page = int $page * $w / ($hi - $lo); 1454 $page = int $page * $inner_w / ($hi - $lo);
1090 $value = int +($value - $lo) * $w / ($hi - $lo); 1455 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1091 1456
1092 $w -= $page; 1457 $w -= $page;
1093 $page &= ~1; 1458 $page &= ~1;
1094 glTranslate $page * 0.5, 0, 0; 1459 glTranslate $page * 0.5, 0, 0;
1460 $page ||= 2;
1095 1461
1096 glColor @$fg;
1097 glBegin GL_LINES;
1098 glVertex 0, 0; glVertex 0, $h;
1099 glVertex $w - 1, 0; glVertex $w - 1, $h;
1100 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1101 glEnd;
1102
1103 my $knob_a = $value - $page * 0.5; 1462 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1104 my $knob_b = $value + $page * 0.5; 1463 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1105 1464
1106 glBegin GL_QUADS; 1465 glEnable GL_BLEND;
1107 glColor @$fg; 1466 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1108 glVertex $knob_a, 0; 1467 glEnable GL_TEXTURE_2D;
1109 glVertex $knob_a, $h; 1468 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1110 glVertex $knob_b, $h;
1111 glVertex $knob_b, 0;
1112 1469
1113 if ($knob_a < $knob_b - 2) { 1470 # draw background
1114 glColor @$bg; 1471 $tex[1]->draw_quad (0, 0, $w, $h);
1115 glVertex $knob_a + 1, 1; 1472
1116 glVertex $knob_a + 1, $h - 1; 1473 # draw handle
1117 glVertex $knob_b - 1, $h - 1; 1474 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1118 glVertex $knob_b - 1, 1; 1475
1476 glDisable GL_BLEND;
1477 glDisable GL_TEXTURE_2D;
1478}
1479
1480#############################################################################
1481
1482package CFClient::UI::TextView;
1483
1484our @ISA = CFClient::UI::HBox::;
1485
1486use SDL::OpenGL;
1487
1488sub new {
1489 my $class = shift;
1490
1491 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE,
1495 @_,
1496
1497 layout => (new CFClient::Layout),
1498 par => [],
1499 height => 0,
1500 children => [
1501 (new CFClient::UI::Empty expand => 1),
1502 (new CFClient::UI::Slider vertical => 1),
1503 ],
1504 );
1505
1506 $self->{children}[1]->connect (changed => sub {
1507 $self->update;
1508 });
1509
1510 $self
1511}
1512
1513sub set_fontsize {
1514 my ($self, $fontsize) = @_;
1515
1516 $self->{fontsize} = $fontsize;
1517 $self->reflow;
1518}
1519
1520sub text_height {
1521 my ($self, $text) = @_;
1522
1523 my $layout = $self->{layout};
1524
1525 $layout->set_height ($self->{fontsize});
1526 $layout->set_width ($self->{w});
1527 $layout->set_text ($text);
1119 } 1528
1120 glEnd; 1529 ($layout->size)[1]
1121} 1530}
1122 1531
1123############################################################################# 1532sub reflow {
1533 my ($self) = @_;
1124 1534
1535 $self->{need_reflow}++;
1536 $self->update;
1537}
1538
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_;
1547
1548 $self->SUPER::size_allocate ($x, $y, $w, $h);
1549
1550 $self->{layout}->set_height ($self->{fontsize});
1551 $self->{layout}->set_width ($self->{w});
1552
1553 $self->reflow;
1554}
1555
1556sub add_paragraph {
1557 my ($self, $color, $text) = @_;
1558
1559 #TODO: intelligently "reformat" paragraph
1560
1561 my $height = $self->text_height ($text);
1562
1563 $self->{height} += $height;
1564
1565 push @{$self->{par}}, [$height, $color, $text];
1566
1567 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1568 $self->{children}[1]->update;
1569}
1570
1571sub update {
1572 my ($self) = @_;
1573
1574 $self->SUPER::update;
1575
1576 return unless $self->{h} > 0;
1577
1578 delete $self->{texture};
1579
1580 $TOPLEVEL->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) {
1582 my $height = 0;
1583
1584 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}};
1586
1587 $self->{height} = $height;
1588
1589 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1590
1591 delete $self->{texture};
1592 }
1593
1594 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1595 glClearColor 0, 0, 0, 1;
1596 glClear GL_COLOR_BUFFER_BIT;
1597
1598 glEnable GL_BLEND;
1599 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1600 glEnable GL_TEXTURE_2D;
1601 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1602
1603 my $top = int $self->{children}[1]{range}[0];
1604
1605 my $y0 = $top;
1606 my $y1 = $top + $self->{h};
1607
1608 my $y = 0;
1609
1610 my $layout = $self->{layout};
1611
1612 for my $par (@{$self->{par}}) {
1613 my $h = $par->[0];
1614
1615 if ($y0 < $y + $h && $y < $y1) {
1616 $layout->set_text ($par->[2]);
1617
1618 glColor @{ $par->[1] };
1619 my ($W, $H) = $layout->size;
1620 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1621 }
1622
1623 $y += $h;
1624 }
1625
1626 glDisable GL_TEXTURE_2D;
1627 glDisable GL_BLEND;
1628 };
1629 });
1630}
1631
1632sub _draw {
1633 my ($self) = @_;
1634
1635 if ($self->{texture}) {
1636 glEnable GL_TEXTURE_2D;
1637 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1638 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1639 glDisable GL_TEXTURE_2D;
1640 }
1641
1642 $self->{children}[1]->draw;
1643
1644}
1645
1646#############################################################################
1647
1125package CFClient::Widget::MapWidget; 1648package CFClient::UI::MapWidget;
1126 1649
1127use strict; 1650use strict;
1128 1651
1129use List::Util qw(min max); 1652use List::Util qw(min max);
1130 1653
1131use SDL; 1654use SDL;
1132use SDL::OpenGL; 1655use SDL::OpenGL;
1133use SDL::OpenGL::Constants;
1134 1656
1135our @ISA = CFClient::Widget::; 1657our @ISA = CFClient::UI::Base::;
1136 1658
1137sub new { 1659sub new {
1138 my $class = shift; 1660 my $class = shift;
1139 1661
1140 $class->SUPER::new ( 1662 $class->SUPER::new (
1141 z => -1, 1663 z => -1,
1664 can_focus => 1,
1142 list => (glGenLists 1), 1665 list => (glGenLists 1),
1143 @_ 1666 @_
1144 ) 1667 )
1145} 1668}
1146 1669
1147sub key_down { 1670sub key_down {
1149} 1672}
1150 1673
1151sub key_up { 1674sub key_up {
1152} 1675}
1153 1676
1677sub button_down {
1678 my ($self, $ev, $x, $y) = @_;
1679
1680 $self->focus_in;
1681
1682 if ($ev->button == 2) {
1683 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1684 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1685
1686 $self->{motion} = sub {
1687 my ($ev, $x, $y) = @_;
1688
1689 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1690
1691 $::CFG->{map_shift_x} = $bw + $x - $ox;
1692 $::CFG->{map_shift_y} = $bh + $y - $oy;
1693
1694 $self->update;
1695 };
1696 }
1697}
1698
1699sub button_up {
1700 my ($self, $ev, $x, $y) = @_;
1701
1702 delete $self->{motion};
1703}
1704
1705sub mouse_motion {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->{motion}->($ev, $x, $y) if $self->{motion};
1709}
1710
1154sub size_request { 1711sub size_request {
1155 ( 1712 (
1156 1 + int $::WIDTH / 32, 1713 1 + 32 * int $::WIDTH / 32,
1157 1 + int $::HEIGHT / 32, 1714 1 + 32 * int $::HEIGHT / 32,
1158 ) 1715 )
1159} 1716}
1160 1717
1161sub update { 1718sub update {
1162 my ($self) = @_; 1719 my ($self) = @_;
1163 1720
1164 $self->{need_update} = 1; 1721 $self->{need_update} = 1;
1722 $self->SUPER::update;
1165} 1723}
1166 1724
1167sub _draw { 1725sub draw {
1168 my ($self) = @_; 1726 my ($self) = @_;
1169 1727
1170 if (delete $self->{need_update}) { 1728 if (delete $self->{need_update}) {
1171 glNewList $self->{list}, GL_COMPILE; 1729 glNewList $self->{list}, GL_COMPILE;
1172 1730
1173 my $mx = $::CONN->{mapx}; 1731 if ($::MAP) {
1174 my $my = $::CONN->{mapy};
1175
1176 my $map = $::CONN->{map};
1177
1178 my ($xofs, $yofs);
1179
1180 my $sw = 1 + int $::WIDTH / 32; 1732 my $sw = int $::WIDTH / 32;
1181 my $sh = 1 + int $::HEIGHT / 32; 1733 my $sh = int $::HEIGHT / 32;
1182 1734
1183 if ($::CONN->{mapw} > $sw) { 1735 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1184 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1736 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1185 } else { 1737
1186 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1738 glTranslate $sx0 - 32, $sy0 - 32, 0;
1739
1740 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1741
1742 if ($::CFG->{fow_enable}) {
1743 if ($::CFG->{fow_smooth}) { # smooth fog of war
1744 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1745 glConvolutionFilter2D
1746 GL_CONVOLUTION_2D,
1747 GL_ALPHA,
1748 3, 3,
1749 GL_ALPHA, GL_FLOAT,
1750 pack "f*",
1751 0.1, 0.1, 0.1,
1752 0.1, 0.2, 0.1,
1753 0.1, 0.1, 0.1,
1754 ;
1755 glEnable GL_CONVOLUTION_2D;
1756 }
1757
1758 my $tex = new CFClient::Texture
1759 w => $w,
1760 h => $h,
1761 data => $data,
1762 internalformat => GL_ALPHA,
1763 format => GL_ALPHA;
1764
1765 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1766
1767 glEnable GL_BLEND;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771
1772 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1774
1775 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND;
1777 }
1778
1779 # HACK BEGIN
1780 {
1781 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1782 my ($w, $h) = (250, 250);
1783
1784 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788
1789 CFClient::Texture->new (
1790 w => $w,
1791 h => $h,
1792 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV
1794 )->draw_quad (100, 100);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799 # HACK END
1187 } 1800 }
1188 1801
1189 if ($::CONN->{maph} > $sh) {
1190 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1191 } else {
1192 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1193 }
1194
1195 glEnable GL_TEXTURE_2D;
1196 glEnable GL_BLEND;
1197 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1198
1199 my $sw4 = ($sw + 3) & ~3;
1200 my $darkness = "\x00" x ($sw4 * $sh);
1201
1202 for my $x (0 .. $sw - 1) {
1203 my $row = $map->[$x + $xofs];
1204 for my $y (0 .. $sh - 1) {
1205
1206 my $cell = $row->[$y + $yofs]
1207 or next;
1208
1209 my $dark = $cell->[0];
1210 if ($dark < 0) {
1211 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1212 } else {
1213 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1214 }
1215
1216 for my $num (grep $_, @$cell[1,2,3]) {
1217 my $tex = $::CONN->{face}[$num]{texture} || next;
1218
1219 my ($w, $h) = @$tex{qw(w h)};
1220
1221 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1222 }
1223 }
1224 }
1225
1226# if (1) { # higher quality darkness
1227# $lighting =~ s/(.)/$1$1$1/gs;
1228# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1229#
1230# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1231#
1232# $lighting = $pb->get_pixels;
1233# $lighting =~ s/(.)../$1/gs;
1234# }
1235
1236 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1237 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1238
1239 $darkness = new CFClient::Texture
1240 width => $sw4,
1241 height => $sh,
1242 data => $darkness,
1243 internalformat => GL_ALPHA,
1244 format => GL_ALPHA;
1245
1246 glColor 0.45, 0.45, 0.45, 1;
1247 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1248
1249 glDisable GL_TEXTURE_2D;
1250 glDisable GL_BLEND;
1251
1252 glEndList; 1802 glEndList;
1253 } 1803 }
1254 1804
1805 glPushMatrix;
1255 glCallList $self->{list}; 1806 glCallList $self->{list};
1807 glPopMatrix;
1808
1809 if ($FOCUS != $self) {
1810 glColor 64/255, 64/255, 64/255;
1811 glLogicOp GL_AND;
1812 glEnable GL_COLOR_LOGIC_OP;
1813 glBegin GL_QUADS;
1814 glVertex 0, 0;
1815 glVertex 0, $::HEIGHT;
1816 glVertex $::WIDTH, $::HEIGHT;
1817 glVertex $::WIDTH, 0;
1818 glEnd;
1819 glDisable GL_COLOR_LOGIC_OP;
1820 }
1256} 1821}
1257 1822
1258my %DIR = ( 1823my %DIR = (
1259 SDLK_KP8, [1, "north"], 1824 SDLK_KP8, [1, "north"],
1260 SDLK_KP9, [2, "northeast"], 1825 SDLK_KP9, [2, "northeast"],
1276 1841
1277 my $mod = $ev->key_mod; 1842 my $mod = $ev->key_mod;
1278 my $sym = $ev->key_sym; 1843 my $sym = $ev->key_sym;
1279 1844
1280 if ($sym == SDLK_KP5) { 1845 if ($sym == SDLK_KP5) {
1281 $::CONN->send ("command stay fire"); 1846 $::CONN->user_send ("command stay fire");
1847 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply");
1849 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/');
1282 } elsif (exists $DIR{$sym}) { 1853 } elsif (exists $DIR{$sym}) {
1283 if ($mod & KMOD_SHIFT) { 1854 if ($mod & KMOD_SHIFT) {
1284 $self->{shft}++; 1855 $self->{shft}++;
1285 $::CONN->send ("command fire $DIR{$sym}[0]"); 1856 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1286 } elsif ($mod & KMOD_CTRL) { 1857 } elsif ($mod & KMOD_CTRL) {
1287 $self->{ctrl}++; 1858 $self->{ctrl}++;
1288 $::CONN->send ("command run $DIR{$sym}[0]"); 1859 $::CONN->user_send ("command run $DIR{$sym}[0]");
1289 } else { 1860 } else {
1290 $::CONN->send ("command $DIR{$sym}[1]"); 1861 $::CONN->user_send ("command $DIR{$sym}[1]");
1291 } 1862 }
1292 } 1863 }
1293} 1864}
1294 1865
1295sub key_up { 1866sub key_up {
1297 1868
1298 my $mod = $ev->key_mod; 1869 my $mod = $ev->key_mod;
1299 my $sym = $ev->key_sym; 1870 my $sym = $ev->key_sym;
1300 1871
1301 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1302 $::CONN->send ("command fire_stop"); 1873 $::CONN->user_send ("command fire_stop");
1303 } 1874 }
1304 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1305 $::CONN->send ("command run_stop"); 1876 $::CONN->user_send ("command run_stop");
1306 } 1877 }
1307} 1878}
1308 1879
1309############################################################################# 1880#############################################################################
1310 1881
1311package CFClient::Widget::Animator; 1882package CFClient::UI::Animator;
1312 1883
1313use SDL::OpenGL; 1884use SDL::OpenGL;
1314 1885
1315our @ISA = CFClient::Widget::Bin::; 1886our @ISA = CFClient::UI::Bin::;
1316 1887
1317sub moveto { 1888sub moveto {
1318 my ($self, $x, $y) = @_; 1889 my ($self, $x, $y) = @_;
1319 1890
1320 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1891 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1348 glPopMatrix; 1919 glPopMatrix;
1349} 1920}
1350 1921
1351############################################################################# 1922#############################################################################
1352 1923
1924package CFClient::UI::Flopper;
1925
1926our @ISA = CFClient::UI::Button::;
1927
1928sub new {
1929 my $class = shift;
1930
1931 my $self = $class->SUPER::new (
1932 state => 0,
1933 connect_activate => \&toggle_flopper,
1934 @_
1935 );
1936
1937 if ($self->{state}) {
1938 $self->{state} = 0;
1939 $self->toggle_flopper;
1940 }
1941
1942 $self
1943}
1944
1945sub toggle_flopper {
1946 my ($self) = @_;
1947
1948 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other});
1951 $self->{other}->move (
1952 ($::WIDTH - $self->{other}{w}) * 0.5,
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other});
1957 }
1958}
1959
1960#############################################################################
1961
1353package CFClient::Widget::Toplevel; 1962package CFClient::UI::Toplevel;
1354 1963
1355our @ISA = CFClient::Widget::Container::; 1964our @ISA = CFClient::UI::Container::;
1965
1966use SDL::OpenGL;
1356 1967
1357sub size_request { 1968sub size_request {
1358 ($::WIDTH, $::HEIGHT) 1969 ($::WIDTH, $::HEIGHT)
1359} 1970}
1360 1971
1361sub size_allocate { 1972sub size_allocate {
1362 my ($self, $w, $h) = @_; 1973 my ($self, $x, $y, $w, $h) = @_;
1363 1974
1364 $self->SUPER::size_allocate ($w, $h); 1975 $self->_size_allocate ($x, $y, $w, $h);
1365 1976
1366 $_->size_allocate ($_->size_request) 1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1367 for @{$self->{children}}; 1978 for @{$self->{children}};
1368} 1979}
1369 1980
1370sub translate { 1981sub translate {
1371 my ($self, $x, $y) = @_; 1982 my ($self, $x, $y) = @_;
1374} 1985}
1375 1986
1376sub update { 1987sub update {
1377 my ($self) = @_; 1988 my ($self) = @_;
1378 1989
1379 $self->size_allocate ($self->size_request); 1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1380 ::refresh (); 1991 ::refresh ();
1381} 1992}
1382 1993
1383sub add { 1994sub add {
1384 my ($self, $widget) = @_; 1995 my ($self, $widget) = @_;
1385 1996
1386 $self->SUPER::add ($widget); 1997 $self->SUPER::add ($widget);
1387 1998
1388 $widget->size_allocate ($widget->size_request); 1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000}
2001
2002sub on_refresh {
2003 my ($self, $id, $cb) = @_;
2004
2005 $self->{refresh_hook}{$id} = $cb;
1389} 2006}
1390 2007
1391sub draw { 2008sub draw {
1392 my ($self) = @_; 2009 my ($self) = @_;
1393 2010
2011 while (my $rcb = delete $self->{refresh_hook}) {
2012 $_->() for values %$rcb;
2013 }
2014
2015 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2016 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2017 glClear GL_COLOR_BUFFER_BIT;
2018
2019 glMatrixMode GL_PROJECTION;
2020 glLoadIdentity;
2021 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2022 glMatrixMode GL_MODELVIEW;
2023 glLoadIdentity;
2024
1394 $self->_draw; 2025 $self->_draw;
1395} 2026}
1396 2027
1397############################################################################# 2028#############################################################################
1398 2029
1399package CFClient::Widget; 2030package CFClient::UI;
1400 2031
1401$TOPLEVEL = new CFClient::Widget::Toplevel; 2032$TOPLEVEL = new CFClient::UI::Toplevel;
1402 2033
14031 20341
1404 2035

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines