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.83 by root, Wed Apr 12 12:16:58 2006 UTC vs.
Revision 1.150 by elmex, Sun Apr 23 00:08:29 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
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 }
71} 84}
72 85
73############################################################################# 86#############################################################################
74 87
75package CFClient::UI::Base; 88package CFClient::UI::Base;
76 89
77use strict; 90use strict;
78 91
79use SDL::OpenGL; 92use CFClient::OpenGL;
80 93
81sub new { 94sub new {
82 my $class = shift; 95 my $class = shift;
83 96
84 my $self = bless { 97 my $self = bless {
85 x => 0, 98 x => 0,
86 y => 0, 99 y => 0,
87 z => 0, 100 z => 0,
88 w => -1, 101 can_events => 1,
89 h => -1,
90 @_ 102 @_
91 }, $class; 103 }, $class;
92 104
93 for (keys %$self) { 105 for (keys %$self) {
94 if (/^connect_(.*)$/) { 106 if (/^connect_(.*)$/) {
97 } 109 }
98 110
99 $self 111 $self
100} 112}
101 113
114sub show {
115 my ($self) = @_;
116
117 return if $self->{parent};
118
119 $CFClient::UI::ROOT->add ($self);
120}
121
122sub hide {
123 my ($self) = @_;
124
125 return unless $self->{parent};
126
127 $self->{parent}->remove ($self);
128}
129
102sub move { 130sub move {
103 my ($self, $x, $y, $z) = @_; 131 my ($self, $x, $y, $z) = @_;
132
104 $self->{x} = $x; 133 $self->{x} = int $x;
105 $self->{y} = $y; 134 $self->{y} = int $y;
106 $self->{z} = $z if defined $z; 135 $self->{z} = $z if defined $z;
136
137 $self->update;
107} 138}
108 139
109sub needs_redraw { 140sub needs_redraw {
110 0 141 0
111} 142}
112 143
113sub size_request { 144sub size_request {
114 require Carp; 145 require Carp;
115 Carp::confess "size_request is abtract"; 146 Carp::confess "size_request is abstract";
116} 147}
117 148
118sub _size_allocate { 149sub configure {
119 my ($self, $x, $y, $w, $h) = @_; 150 my ($self, $x, $y, $w, $h) = @_;
120 151
152 if ($self->{aspect}) {
153 my $w2 = List::Util::min $w, int $h * $self->{aspect};
154 my $h2 = List::Util::min $h, int $w / $self->{aspect};
155
156 # use alignment to adjust x, y
157
158 $x += int +($w - $w2) * 0.5;
159 $y += int +($h - $h2) * 0.5;
160
161 ($w, $h) = ($w2, $h2);
162 }
163
164 if ($self->{x} != $x || $self->{y} != $y) {
121 $self->{x} = $x; 165 $self->{x} = $x;
122 $self->{y} = $y; 166 $self->{y} = $y;
167 $self->update;
168 }
123 169
124 return unless $self->{w} != $w || $self->{h} != $h; 170 if ($self->{w} != $w || $self->{h} != $h) {
125
126 $self->{w} = $w; 171 $self->{w} = $w;
127 $self->{h} = $h; 172 $self->{h} = $h;
128 173
174 $self->size_allocate ($w, $h);
175 $self->update;
129 1 176 }
130} 177}
131 178
132sub size_allocate { 179sub size_allocate {
180 # nothing to be done
181}
182
183# return top left coordinates
184sub _topleft {
133 my ($self, $x, $y, $w, $h) = @_; 185 my ($self, $x, $y) = @_;
134 186
135 $self->_size_allocate ($x, $y, $w, $h); 187 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 188}
137 189
138# translate global coordinates to local coordinate system 190# translate global coordinates to local coordinate system
139sub translate { 191sub coord2local {
140 my ($self, $x, $y) = @_; 192 my ($self, $x, $y) = @_;
141 193
142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 194 my ($X, $Y) = $self->_topleft;
195 ($x - $X, $y - $Y)
196}
197
198# translate local coordinates to global coordinate system
199sub coord2global {
200 my ($self, $x, $y) = @_;
201
202 my ($X, $Y) = $self->_topleft;
203 ($x + $X, $y + $Y)
143} 204}
144 205
145sub focus_in { 206sub focus_in {
146 my ($self) = @_; 207 my ($self) = @_;
147 208
148 return if $FOCUS == $self; 209 return if $FOCUS == $self;
210 return unless $self->{can_focus};
149 211
150 my $focus = $FOCUS; $FOCUS = $self; 212 my $focus = $FOCUS; $FOCUS = $self;
213
214 $self->emit (focus_in => $focus);
215
151 $focus->update if $focus; 216 $focus->update if $focus;
152 $FOCUS->update; 217 $FOCUS->update;
153} 218}
154 219
155sub focus_out { 220sub focus_out {
156 my ($self) = @_; 221 my ($self) = @_;
157 222
158 return unless $FOCUS == $self; 223 return unless $FOCUS == $self;
159 224
160 my $focus = $FOCUS; undef $FOCUS; 225 my $focus = $FOCUS; undef $FOCUS;
226
227 $self->emit (focus_out => $focus);
228
161 $focus->update if $focus; #? 229 $focus->update if $focus; #?
162} 230}
163 231
164sub mouse_motion { } 232sub mouse_motion { }
165sub button_up { } 233sub button_up { }
186 glPushMatrix; 254 glPushMatrix;
187 glTranslate $self->{x}, $self->{y}, 0; 255 glTranslate $self->{x}, $self->{y}, 0;
188 $self->_draw; 256 $self->_draw;
189 glPopMatrix; 257 glPopMatrix;
190 258
191 if ($self == $HOVER) { 259 if ($self == $HOVER && $self->{can_hover}) {
192 my ($x, $y) = @$self{qw(x y)}; 260 my ($x, $y) = @$self{qw(x y)};
193 261
194 glColor 0, 0, 1, 0.2; 262 glColor 1, 0.8, 0.5, 0.2;
195 glEnable GL_BLEND; 263 glEnable GL_BLEND;
196 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 264 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
197 glBegin GL_QUADS; 265 glBegin GL_QUADS;
198 glVertex $x , $y; 266 glVertex $x , $y;
199 glVertex $x + $self->{w}, $y; 267 glVertex $x + $self->{w}, $y;
211} 279}
212 280
213sub find_widget { 281sub find_widget {
214 my ($self, $x, $y) = @_; 282 my ($self, $x, $y) = @_;
215 283
284 return () unless $self->{can_events};
285
216 return $self 286 return $self
217 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 287 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
218 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 288 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
219 289
220 () 290 ()
221} 291}
222 292
223sub set_parent { 293sub set_parent {
224 my ($self, $par) = @_; 294 my ($self, $parent) = @_;
225 295
226 $self->{parent} = $par;
227 Scalar::Util::weaken $self->{parent}; 296 Scalar::Util::weaken ($self->{parent} = $parent);
228} 297}
229 298
230sub get_parent { 299sub check_size {
231 $_[0]->{parent} 300 my ($self) = @_;
301
302 return unless $self->{parent};
303
304 my ($w, $h) = $self->size_request;
305
306 if ($w != $self->{req_w} || $h != $self->{req_h}) {
307 $self->{req_w} = $w;
308 $self->{req_h} = $h;
309
310 $self->{parent}->check_size;
311 }
232} 312}
233 313
234sub update { 314sub update {
235 my ($self) = @_; 315 my ($self) = @_;
236 316
239} 319}
240 320
241sub connect { 321sub connect {
242 my ($self, $signal, $cb) = @_; 322 my ($self, $signal, $cb) = @_;
243 323
244 push @{ $self->{cb}{$signal} }, $cb; 324 push @{ $self->{signal_cb}{$signal} }, $cb;
245} 325}
246 326
247sub emit { 327sub emit {
248 my ($self, $signal, @args) = @_; 328 my ($self, $signal, @args) = @_;
249 329
330 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
250 $_->($self, @args) 331 $cb->($self, @args);
251 for @{$self->{cb}{$signal} || []}; 332 }
252} 333}
253 334
254sub DESTROY { 335sub DESTROY {
255 my ($self) = @_; 336 my ($self) = @_;
256 337
262package CFClient::UI::DrawBG; 343package CFClient::UI::DrawBG;
263 344
264our @ISA = CFClient::UI::Base::; 345our @ISA = CFClient::UI::Base::;
265 346
266use strict; 347use strict;
267use SDL::OpenGL; 348use CFClient::OpenGL;
268 349
269sub new { 350sub new {
270 my $class = shift; 351 my $class = shift;
271 352
272 # range [value, low, high, page] 353 # range [value, low, high, page]
301 382
302package CFClient::UI::Empty; 383package CFClient::UI::Empty;
303 384
304our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
305 386
387sub new {
388 my ($class, %arg) = @_;
389 $class->SUPER::new (can_events => 0, %arg);
390}
391
306sub size_request { 392sub size_request {
307 (0, 0) 393 (0, 0)
308} 394}
309 395
310sub draw { } 396sub draw { }
318sub new { 404sub new {
319 my ($class, %arg) = @_; 405 my ($class, %arg) = @_;
320 406
321 my $children = delete $arg{children} || []; 407 my $children = delete $arg{children} || [];
322 408
323 my $self = $class->SUPER::new (children => [], %arg); 409 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
324 $self->add ($_) for @$children; 410 $self->add ($_) for @$children;
325 411
326 $self 412 $self
327} 413}
328 414
329sub add { 415sub add {
330 my ($self, $chld) = @_; 416 my ($self, $child) = @_;
331 417
332 $chld->set_parent ($self); 418 $child->set_parent ($self);
419
420 use sort 'stable';
333 421
334 $self->{children} = [ 422 $self->{children} = [
335 sort { $a->{z} <=> $b->{z} } 423 sort { $a->{z} <=> $b->{z} }
336 @{$self->{children}}, $chld 424 @{$self->{children}}, $child
337 ]; 425 ];
338 426
339 $self->{w} = $self->{h} = -1; 427 $child->check_size;
340 $self->update;
341} 428}
342 429
343sub remove { 430sub remove {
344 my ($self, $widget) = @_; 431 my ($self, $child) = @_;
345 432
433 delete $child->{parent};
434
346 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 435 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
347 436
348 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 437 $self->check_size;
349} 438}
350 439
351sub find_widget { 440sub find_widget {
352 my ($self, $x, $y) = @_; 441 my ($self, $x, $y) = @_;
353 442
383 472
384 $class->SUPER::new (children => [$child], %arg) 473 $class->SUPER::new (children => [$child], %arg)
385} 474}
386 475
387sub add { 476sub add {
388 my ($self, $widget) = @_; 477 my ($self, $child) = @_;
389 478
390 $self->{children} = []; 479 $self->{children} = [];
391 480
392 $self->SUPER::add ($widget); 481 $self->SUPER::add ($child);
393} 482}
394 483
395sub remove { 484sub remove {
396 my ($self, $widget) = @_; 485 my ($self, $widget) = @_;
397 486
406sub size_request { 495sub size_request {
407 $_[0]{children}[0]->size_request 496 $_[0]{children}[0]->size_request
408} 497}
409 498
410sub size_allocate { 499sub size_allocate {
411 my ($self, $x, $y, $w, $h) = @_; 500 my ($self, $w, $h) = @_;
412 501
413 $self->_size_allocate ($x, $y, $w, $h) or return;
414
415 $self->{children}[0]->size_allocate (0, 0, $w, $h); 502 $self->{children}[0]->configure (0, 0, $w, $h);
416} 503}
417 504
418############################################################################# 505#############################################################################
419 506
420package CFClient::UI::Window; 507package CFClient::UI::Window;
421 508
422our @ISA = CFClient::UI::Bin::; 509our @ISA = CFClient::UI::Bin::;
423 510
424use SDL::OpenGL; 511use CFClient::OpenGL;
425 512
426sub new { 513sub new {
427 my ($class, %arg) = @_; 514 my ($class, %arg) = @_;
428 515
429 my $self = $class->SUPER::new (%arg); 516 my $self = $class->SUPER::new (%arg);
438} 525}
439 526
440sub render_chld { 527sub render_chld {
441 my ($self) = @_; 528 my ($self) = @_;
442 529
443 $self->{texture} = 530 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
444 CFClient::Texture->new_from_opengl ( 531 glClearColor 0, 0, 0, 1;
445 $self->{w}, $self->{h}, sub { $self->child->draw } 532 glClear GL_COLOR_BUFFER_BIT;
446 ); 533 $self->child->draw;
534 };
447} 535}
448 536
449sub size_allocate { 537sub size_allocate {
450 my ($self, $x, $y, $w, $h) = @_; 538 my ($self, $w, $h) = @_;
451 539
452 $self->_size_allocate ($x, $y, $w, $h) or return;
453
454 $self->child->size_allocate (0, 0, $w, $h); 540 $self->child->configure (0, 0, $w, $h);
455 541
456 $self->render_chld; 542 $self->render_chld;
457} 543}
458 544
459sub _draw { 545sub _draw {
475 glDisable GL_TEXTURE_2D; 561 glDisable GL_TEXTURE_2D;
476} 562}
477 563
478############################################################################# 564#############################################################################
479 565
566package CFClient::UI::ViewPort;
567
568our @ISA = CFClient::UI::Window::;
569
570sub new { die }
571
572sub size_request {
573 my ($self) = @_;
574
575 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
576 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
577
578 @$self{qw(child_w child_h)}
579}
580
581sub _draw {
582 my ($self) = @_;
583
584 $self->{children}[1]->draw;
585}
586
587
588#############################################################################
589
480package CFClient::UI::Frame; 590package CFClient::UI::Frame;
481 591
482our @ISA = CFClient::UI::Bin::; 592our @ISA = CFClient::UI::Bin::;
483 593
484use SDL::OpenGL; 594use CFClient::OpenGL;
595
596sub new {
597 my $class = shift;
598
599 my $self = $class->SUPER::new (
600 bg => [1, 1, 1, 1],
601 border_bg => [1, 1, 1, 1],
602 border => 0.8,
603 @_
604 );
605
606 $self
607}
608
609sub set_size {
610 my ($self, $w, $h) = @_;
611 $self->{req_w} = $w;
612 $self->{req_h} = $h;
613 $self->check_size;
614}
485 615
486sub size_request { 616sub size_request {
487 my ($self) = @_; 617 my ($self) = @_;
488 my $chld = $self->child 618 ($self->{req_w}, $self->{req_h})
489 or return (0, 0);
490
491 $chld->move (2, 2);
492
493 map { $_ + 4 } $chld->size_request;
494} 619}
495 620
496sub size_allocate { 621sub size_allocate {
497 my ($self, $x, $y, $w, $h) = @_; 622 my ($self, $w, $h) = @_;
498 623 $self->{w} = $w;
499 $self->_size_allocate ($x, $y, $w, $h) or return; 624 $self->{h} = $h;
500 625 $self->child->configure (0, 0, $w, $h);
501 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
502} 626}
503 627
504sub _draw { 628sub _draw {
505 my ($self) = @_; 629 my ($self) = @_;
506 630
507 my $chld = $self->child;
508
509 my ($w, $h) = $chld->size_request;
510
511 glBegin GL_QUADS;
512 glColor 0, 0, 0;
513 glVertex 0 , 0;
514 glVertex 0 , $h + 4;
515 glVertex $w + 4 , $h + 4;
516 glVertex $w + 4 , 0;
517 glEnd;
518
519 $chld->draw;
520}
521
522#############################################################################
523
524package CFClient::UI::FancyFrame;
525
526our @ISA = CFClient::UI::Bin::;
527
528use SDL::OpenGL;
529
530my @tex =
531 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
532 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
533
534sub size_request {
535 my ($self) = @_;
536
537 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
538
539 my ($w, $h) = $self->SUPER::size_request;
540
541 $h += $tex[1]->{h};
542 $h += $tex[4]->{h};
543 $w += $tex[2]->{w};
544 $w += $tex[3]->{w};
545
546 ($w, $h)
547}
548
549sub size_allocate {
550 my ($self, $x, $y, $w, $h) = @_;
551
552 $self->_size_allocate ($x, $y, $w, $h) or return;
553
554 $h -= $tex[1]{h};
555 $h -= $tex[4]{h};
556 $w -= $tex[2]{w};
557 $w -= $tex[3]{w};
558
559 $h = $h < 0 ? 0 : $h;
560 $w = $w < 0 ? 0 : $w;
561
562 my $child = $self->child;
563
564 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
565}
566
567sub button_down {
568 my ($self, $ev, $x, $y) = @_;
569
570 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
571 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
572
573 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
574 my ($bw, $bh) = ($self->{w}, $self->{h});
575
576 $self->{motion} = sub {
577 my ($ev, $x, $y) = @_;
578
579 ($x, $y) = ($ev->motion_x, $ev->motion_y);
580
581 $self->{user_w} = $bw + $x - $ox;
582 $self->{user_h} = $bh + $y - $oy;
583 $self->update;
584 };
585
586 } elsif ($x >= 0 && $x < $self->{w}
587 && $y >= 0 && $y < $tex[1]{h}) {
588
589 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
590 my ($bx, $by) = ($self->{x}, $self->{y});
591
592 $self->{motion} = sub {
593 my ($ev, $x, $y) = @_;
594
595 ($x, $y) = ($ev->motion_x, $ev->motion_y);
596
597 $self->move ($bx + $x - $ox, $by + $y - $oy);
598 $self->update;
599 };
600 }
601}
602
603sub button_up {
604 my ($self, $ev, $x, $y) = @_;
605
606 delete $self->{motion};
607}
608
609sub mouse_motion {
610 my ($self, $ev, $x, $y) = @_;
611
612 $self->{motion}->($ev, $x, $y) if $self->{motion};
613}
614
615sub _draw {
616 my ($self) = @_;
617
618 my ($w, $h) = ($self->{w}, $self->{h}); 631 my ($w, $h) = ($self->{w}, $self->{h});
619 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
620 632
621 glEnable GL_BLEND; 633 glEnable GL_BLEND;
622 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 634 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
623 glEnable GL_TEXTURE_2D; 635 glEnable GL_TEXTURE_2D;
624 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 636 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
625 637
626 my $top = $tex[1]; 638# glBegin GL_QUADS;
639# glColor 0, 0, 0, 0;
640# glVertex 0 , 0;
641# glVertex 0 , $h;
642# glVertex $w, $h;
643# glVertex $w, 0;
644# glEnd;
645
646
647 $self->child->draw;
648 glDisable GL_BLEND;
649 glDisable GL_TEXTURE_2D;
650}
651
652#############################################################################
653
654package CFClient::UI::FancyFrame;
655
656our @ISA = CFClient::UI::Bin::;
657
658use CFClient::OpenGL;
659
660my @tex =
661 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
662 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
663
664sub new {
665 my $class = shift;
666
667 # TODO: user_x, user_y, overwrite moveto?
668
669 my $self = $class->SUPER::new (
670 bg => [1, 1, 1, 1],
671 border_bg => [1, 1, 1, 1],
672 border => 0.8,
673 can_events => 1,
674 @_
675 );
676
677 $self->{title} &&= new CFClient::UI::Label
678 align => 0,
679 valign => 1,
680 text => $self->{title},
681 fontsize => 1;
682
683 $self
684}
685
686sub border {
687 int $_[0]{border} * $::FONTSIZE
688}
689
690sub size_request {
691 my ($self) = @_;
692
693 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
694
695 my ($w, $h) = $self->SUPER::size_request;
696
697 (
698 $w + $self->border * 2,
699 $h + $self->border * 2,
700 )
701}
702
703sub size_allocate {
704 my ($self, $w, $h) = @_;
705
706 $h -= List::Util::max 0, $self->border * 2;
707 $w -= List::Util::max 0, $self->border * 2;
708
709 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
710 if $self->{title};
711
712 $self->child->configure ($self->border, $self->border, $w, $h);
713}
714
715sub button_down {
716 my ($self, $ev, $x, $y) = @_;
717
718 my $border = $self->border;
719
720 if ($x < $self->{w} && $x >= $self->{w} - $border
721 && $y < $self->{h} && $y >= $self->{h} - $border) {
722
723 my ($ox, $oy) = ($ev->{x}, $ev->{y});
724 my ($bw, $bh) = ($self->{w}, $self->{h});
725
726 $self->{motion} = sub {
727 my ($ev, $x, $y) = @_;
728
729 ($x, $y) = ($ev->{x}, $ev->{y});
730
731 $self->{user_w} = $bw + $x - $ox;
732 $self->{user_h} = $bh + $y - $oy;
733 $self->check_size;
734 };
735
736 } elsif ($x >= 0 && $x < $self->{w}
737 && $y >= 0 && $y < $border) {
738
739 my ($ox, $oy) = ($ev->{x}, $ev->{y});
740 my ($bx, $by) = ($self->{x}, $self->{y});
741
742 $self->{motion} = sub {
743 my ($ev, $x, $y) = @_;
744
745 ($x, $y) = ($ev->{x}, $ev->{y});
746
747 $self->move ($bx + $x - $ox, $by + $y - $oy);
748 $self->update;
749 };
750 }
751}
752
753sub button_up {
754 my ($self, $ev, $x, $y) = @_;
755
756 delete $self->{motion};
757}
758
759sub mouse_motion {
760 my ($self, $ev, $x, $y) = @_;
761
762 $self->{motion}->($ev, $x, $y) if $self->{motion};
763}
764
765sub _draw {
766 my ($self) = @_;
767
768 my ($w, $h ) = ($self->{w}, $self->{h});
769 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
770
771 glEnable GL_BLEND;
772 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
773 glEnable GL_TEXTURE_2D;
774 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
775
776 my $border = $self->border;
777
778 glColor @{ $self->{border_bg} };
627 $top->draw_quad (0, 0, $w, $top->{h}); 779 $tex[1]->draw_quad (0, 0, $w, $border);
628 780 $tex[3]->draw_quad (0, $border, $border, $ch);
629 my $left = $tex[3]; 781 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
630 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 782 $tex[4]->draw_quad (0, $h - $border, $w, $border);
631
632 my $right = $tex[2];
633 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
634
635 my $bottom = $tex[4];
636 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
637 783
638 my $bg = $tex[0]; 784 my $bg = $tex[0];
639 785
640 glBindTexture GL_TEXTURE_2D, $bg->{name}; 786 # TODO: repeat texture not scale
641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
642
643 my $rep_x = $cw / $bg->{w}; 787 my $rep_x = $cw / $bg->{w};
644 my $rep_y = $ch / $bg->{h}; 788 my $rep_y = $ch / $bg->{h};
645 789
646 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 790 glColor @{ $self->{bg} };
791
792 $bg->{s} = $rep_x;
793 $bg->{t} = $rep_y;
794 $bg->{wrap_mode} = 1;
795 $bg->draw_quad ($border, $border, $cw, $ch);
647 796
648 glDisable GL_TEXTURE_2D; 797 glDisable GL_TEXTURE_2D;
649 glDisable GL_BLEND; 798 glDisable GL_BLEND;
650 799
800 $self->{title}->draw if $self->{title};
651 $self->child->draw; 801 $self->child->draw;
652
653} 802}
654 803
655############################################################################# 804#############################################################################
656 805
657package CFClient::UI::Table; 806package CFClient::UI::Table;
658 807
659our @ISA = CFClient::UI::Base::; 808our @ISA = CFClient::UI::Base::;
660 809
661use List::Util qw(max sum); 810use List::Util qw(max sum);
662 811
663use SDL::OpenGL; 812use CFClient::OpenGL;
664 813
665sub new { 814sub new {
666 my $class = shift; 815 my $class = shift;
667 816
668 $class->SUPER::new ( 817 $class->SUPER::new (
670 @_ 819 @_
671 ) 820 )
672} 821}
673 822
674sub add { 823sub add {
675 my ($self, $x, $y, $chld) = @_; 824 my ($self, $x, $y, $child) = @_;
676 825
826 $child->set_parent ($self);
677 $self->{children}[$y][$x] = $chld; 827 $self->{children}[$y][$x] = $child;
678 $chld->set_parent ($self);
679 828
680 $self->{w} = $self->{h} = -1; 829 $child->check_size;
830}
831
832# TODO: move to container class maybe? send childs a signal on removal?
833sub clear {
834 my ($self) = @_;
835
836 delete $self->{children};
681 $self->update; 837 $self->update;
682} 838}
683 839
684sub get_wh { 840sub get_wh {
685 my ($self) = @_; 841 my ($self) = @_;
691 or next; 847 or next;
692 848
693 for my $x (0 .. $#$row) { 849 for my $x (0 .. $#$row) {
694 my $widget = $row->[$x] 850 my $widget = $row->[$x]
695 or next; 851 or next;
696 my ($w, $h) = $widget->size_request; 852 my ($w, $h) = @$widget{qw(req_w req_h)};
697 853
698 $w[$x] = max $w[$x], $w; 854 $w[$x] = max $w[$x], $w;
699 $h[$y] = max $h[$y], $h; 855 $h[$y] = max $h[$y], $h;
700 } 856 }
701 } 857 }
713 (sum @$hs), 869 (sum @$hs),
714 ) 870 )
715} 871}
716 872
717sub size_allocate { 873sub size_allocate {
718 my ($self, $x, $y, $w, $h) = @_; 874 my ($self, $w, $h) = @_;
719
720 $self->_size_allocate ($x, $y, $w, $h) or return;
721 875
722 my ($ws, $hs) = $self->get_wh; 876 my ($ws, $hs) = $self->get_wh;
723 877
724 my $req_w = sum @$ws; 878 my $req_w = sum @$ws;
725 my $req_h = sum @$hs; 879 my $req_h = sum @$hs;
730 my $col_expand = (sum @col_expand) || 1; 884 my $col_expand = (sum @col_expand) || 1;
731 885
732 # linearly scale sizes 886 # linearly scale sizes
733 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 887 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
734 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 888 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
889
890 CFClient::UI::harmonize $ws;
891 CFClient::UI::harmonize $hs;
735 892
736 my $y; 893 my $y;
737 894
738 for my $r (0 .. $#{$self->{children}}) { 895 for my $r (0 .. $#{$self->{children}}) {
739 my $row = $self->{children}[$r] 896 my $row = $self->{children}[$r]
744 901
745 for my $c (0 .. $#$row) { 902 for my $c (0 .. $#$row) {
746 my $col_w = $ws->[$c]; 903 my $col_w = $ws->[$c];
747 904
748 if (my $widget = $row->[$c]) { 905 if (my $widget = $row->[$c]) {
749 $widget->size_allocate ($x, $y, $col_w, $row_h); 906 $widget->configure ($x, $y, $col_w, $row_h);
750 } 907 }
751 908
752 $x += $col_w; 909 $x += $col_w;
753 } 910 }
754 911
799 (List::Util::max map $_->[1], @alloc), 956 (List::Util::max map $_->[1], @alloc),
800 ) 957 )
801} 958}
802 959
803sub size_allocate { 960sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 961 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h) or return;
807
808 return unless $self->{w};
809 962
810 ($h, $w) = ($w, $h); 963 ($h, $w) = ($w, $h);
811 964
812 my $children = $self->{children}; 965 my $children = $self->{children};
813 966
814 my @h = map +($_->size_request)[0], @$children; 967 my @h = map $_->{req_w}, @$children;
815 968
816 my $req_h = List::Util::sum @h; 969 my $req_h = List::Util::sum @h;
817 970
818 if ($req_h > $h) { 971 if ($req_h > $h) {
819 # ah well, not enough space 972 # ah well, not enough space
829 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 982 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
830 $h[$_] = $alloc_h; 983 $h[$_] = $alloc_h;
831 } 984 }
832 } 985 }
833 986
987 CFClient::UI::harmonize \@h;
988
834 my $y = 0; 989 my $y = 0;
835 for (0 .. $#$children) { 990 for (0 .. $#$children) {
836 my $child = $children->[$_]; 991 my $child = $children->[$_];
837 my $h = $h[$_]; 992 my $h = $h[$_];
838 $child->size_allocate ($y, 0, $h, $w); 993 $child->configure ($y, 0, $h, $w);
839 994
840 $y += $h; 995 $y += $h;
841 } 996 }
997
998 1
842} 999}
843 1000
844############################################################################# 1001#############################################################################
845 1002
846package CFClient::UI::VBox; 1003package CFClient::UI::VBox;
859 (List::Util::sum map $_->[1], @alloc), 1016 (List::Util::sum map $_->[1], @alloc),
860 ) 1017 )
861} 1018}
862 1019
863sub size_allocate { 1020sub size_allocate {
864 my ($self, $x, $y, $w, $h) = @_; 1021 my ($self, $w, $h) = @_;
865
866 $self->_size_allocate ($x, $y, $w, $h) or return;
867
868 return unless $self->{h};
869 1022
870 my $children = $self->{children}; 1023 my $children = $self->{children};
871 1024
872 my @h = map +($_->size_request)[1], @$children; 1025 my @h = map $_->{req_h}, @$children;
873 1026
874 my $req_h = List::Util::sum @h; 1027 my $req_h = List::Util::sum @h;
875 1028
876 if ($req_h > $h) { 1029 if ($req_h > $h) {
877 # ah well, not enough space 1030 # ah well, not enough space
885 1038
886 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1039 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
887 } 1040 }
888 } 1041 }
889 1042
1043 CFClient::UI::harmonize \@h;
1044
890 my $y = 0; 1045 my $y = 0;
891 for (0 .. $#$children) { 1046 for (0 .. $#$children) {
892 my $child = $children->[$_]; 1047 my $child = $children->[$_];
893 my $h = $h[$_]; 1048 my $h = $h[$_];
894 $child->size_allocate (0, $y, $w, $h); 1049 $child->configure (0, $y, $w, $h);
895 1050
896 $y += $h; 1051 $y += $h;
897 } 1052 }
1053
1054 1
898} 1055}
899 1056
900############################################################################# 1057#############################################################################
901 1058
902package CFClient::UI::Label; 1059package CFClient::UI::Label;
903 1060
904our @ISA = CFClient::UI::Base::; 1061our @ISA = CFClient::UI::Base::;
905 1062
906use SDL::OpenGL; 1063use CFClient::OpenGL;
907 1064
908sub new { 1065sub new {
909 my ($class, %arg) = @_; 1066 my ($class, %arg) = @_;
910 1067
911 my $self = $class->SUPER::new ( 1068 my $self = $class->SUPER::new (
912 fg => [1, 1, 1], 1069 fg => [1, 1, 1],
913 height => $::FONTSIZE, 1070 fontsize => 1,
914 text => "", 1071 text => "",
915 align => -1, 1072 align => -1,
1073 valign => -1,
916 padding => 2, 1074 padding => 2,
917 layout => new CFClient::Layout, 1075 layout => new CFClient::Layout,
1076 can_events => 0,
918 %arg 1077 %arg
919 ); 1078 );
920 1079
921 $self->set_text ($self->{text}); 1080 if (exists $self->{template}) {
1081 my $layout = new CFClient::Layout;
1082 $layout->set_text (delete $self->{template});
1083 $self->{template} = $layout;
1084 }
1085
1086 $self->set_text (delete $self->{text}) if exists $self->{text};
1087 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
922 1088
923 $self 1089 $self
924} 1090}
925 1091
926sub escape_text { 1092sub escape_text {
934} 1100}
935 1101
936sub set_text { 1102sub set_text {
937 my ($self, $text) = @_; 1103 my ($self, $text) = @_;
938 1104
939 $self->{text} = $text;
940 $self->{layout}->set_markup ($text); 1105 $self->{layout}->set_text ($text);
941 1106
942 delete $self->{texture}; 1107 delete $self->{texture};
943# $self->{w} = $self->{h} = -1; 1108 $self->check_size;
944 $self->update; 1109 $self->update;
945} 1110}
946 1111
947sub get_text { 1112sub set_markup {
948 my ($self, $text) = @_; 1113 my ($self, $markup) = @_;
949 1114
950 $self->{text} 1115 $self->{layout}->set_markup ($markup);
1116
1117 delete $self->{texture};
1118 $self->check_size;
1119 $self->update;
951} 1120}
952 1121
953sub size_request { 1122sub size_request {
954 my ($self) = @_; 1123 my ($self) = @_;
955 1124
956 $self->{layout}->set_width; 1125 $self->{layout}->set_width;
957 $self->{layout}->set_height ($self->{height}); 1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1127
958 my ($w, $h) = $self->{layout}->size; 1128 my ($w, $h) = $self->{layout}->size;
1129
1130 if (exists $self->{template}) {
1131 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1132
1133 my ($w2, $h2) = $self->{template}->size;
1134
1135 $w = List::Util::max $w, $w2;
1136 $h = List::Util::max $h, $h2;
1137 }
959 1138
960 ( 1139 (
961 $w + $self->{padding} * 2, 1140 $w + $self->{padding} * 2,
962 $h + $self->{padding} * 2, 1141 $h + $self->{padding} * 2,
963 ) 1142 )
964} 1143}
965 1144
966sub size_allocate { 1145sub size_allocate {
967 my ($self, $x, $y, $w, $h) = @_; 1146 my ($self, $w, $h) = @_;
968
969 $self->_size_allocate ($x, $y, $w, $h) or return;
970 1147
971 delete $self->{texture}; 1148 delete $self->{texture};
972} 1149}
973 1150
974sub update { 1151sub set_fontsize {
975 my ($self) = @_; 1152 my ($self, $fontsize) = @_;
976 1153
977 delete $self->{texture}; 1154 $self->{fontsize} = $fontsize;
978 $self->SUPER::update; 1155 $self->check_size;
979} 1156}
980 1157
981sub _draw { 1158sub _draw {
982 my ($self) = @_; 1159 my ($self) = @_;
983 1160
984 my $tex = $self->{texture} ||= do { 1161 my $tex = $self->{texture} ||= do {
985 $self->{layout}->set_width ($self->{w}); 1162 $self->{layout}->set_width ($self->{w});
986 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1163 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
987 new_from_layout CFClient::Texture $self->{layout} 1164 new_from_layout CFClient::Texture $self->{layout}
988 }; 1165 };
989 1166
990 glEnable GL_BLEND; 1167 glEnable GL_BLEND;
991 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1168 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
992 glEnable GL_TEXTURE_2D; 1169 glEnable GL_TEXTURE_2D;
993 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
994 1171
995 glColor @{$self->{fg}}; 1172 glColor @{$self->{fg}};
996 1173
997 my $x = 1174 $self->{ox} = int (
998 $self->{align} < 0 ? $self->{padding} 1175 $self->{align} < 0 ? $self->{padding}
999 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1176 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1000 : ($self->{w} - $tex->{w}) * 0.5; 1177 : ($self->{w} - $tex->{w}) * 0.5
1178 );
1001 1179
1002 glTranslate $x, $self->{padding}, 0; 1180 $self->{oy} = int (
1003 $tex->draw_quad (0, 0); 1181 $self->{valign} < 0 ? $self->{padding}
1182 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1183 : ($self->{h} - $tex->{h}) * 0.5
1184 );
1185
1186 $tex->draw_quad ($self->{ox}, $self->{oy});
1004 1187
1005 glDisable GL_TEXTURE_2D; 1188 glDisable GL_TEXTURE_2D;
1006 glDisable GL_BLEND; 1189 glDisable GL_BLEND;
1007} 1190}
1008 1191
1009############################################################################# 1192#############################################################################
1010 1193
1011package CFClient::UI::Entry; 1194package CFClient::UI::EntryBase;
1012 1195
1013our @ISA = CFClient::UI::Label::; 1196our @ISA = CFClient::UI::Label::;
1014 1197
1015use SDL; 1198use CFClient::OpenGL;
1016use SDL::OpenGL;
1017 1199
1018sub new { 1200sub new {
1019 my $class = shift; 1201 my $class = shift;
1020 1202
1021 $class->SUPER::new ( 1203 $class->SUPER::new (
1022 fg => [1, 1, 1], 1204 fg => [1, 1, 1],
1023 bg => [0, 0, 0, 0.2], 1205 bg => [0, 0, 0, 0.2],
1024 active_bg => [1, 1, 1, 0.5], 1206 active_bg => [1, 1, 1, 0.5],
1025 active_fg => [0, 0, 0], 1207 active_fg => [0, 0, 0],
1208 can_hover => 1,
1209 can_focus => 1,
1210 valign => 0,
1211 can_events => 1,
1026 @_ 1212 @_
1027 ) 1213 )
1028} 1214}
1029 1215
1030sub _set_text { 1216sub _set_text {
1031 my ($self, $text) = @_; 1217 my ($self, $text) = @_;
1032 1218
1219 delete $self->{cur_h};
1220
1221 return if $self->{text} eq $text;
1222
1223 delete $self->{texture};
1224
1033 $self->{last_activity} = $::NOW; 1225 $self->{last_activity} = $::NOW;
1034
1035 $self->{text} = $text; 1226 $self->{text} = $text;
1036 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
1038 1227
1039 $text =~ s/./*/g if $self->{hidden}; 1228 $text =~ s/./*/g if $self->{hidden};
1229 $self->{layout}->set_text ("$text ");
1040 1230
1041 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1231 $self->emit (changed => $self->{text});
1232}
1042 1233
1043 $text = substr $text, 0, $self->{cursor}; 1234sub get_text {
1044 utf8::encode $text; 1235 $_[0]{text}
1045
1046 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1047} 1236}
1048 1237
1049sub size_request { 1238sub size_request {
1050 my ($self) = @_; 1239 my ($self) = @_;
1051 1240
1053 1242
1054 ($w + 1, $h) # add 1 for cursor 1243 ($w + 1, $h) # add 1 for cursor
1055} 1244}
1056 1245
1057sub size_allocate { 1246sub size_allocate {
1058 my ($self, $x, $y, $w, $h) = @_; 1247 my ($self, $w, $h) = @_;
1059 1248
1060 $self->SUPER::size_allocate ($x, $y, $w, $h); 1249 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1061
1062 $self->_set_text ($self->{text});
1063} 1250}
1064 1251
1065sub set_text { 1252sub set_text {
1066 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1067 1254
1071} 1258}
1072 1259
1073sub key_down { 1260sub key_down {
1074 my ($self, $ev) = @_; 1261 my ($self, $ev) = @_;
1075 1262
1076 my $mod = $ev->key_mod; 1263 my $mod = $ev->{mod};
1077 my $sym = $ev->key_sym; 1264 my $sym = $ev->{sym};
1078
1079 my $uni = $ev->key_unicode; 1265 my $uni = $ev->{unicode};
1080 1266
1081 my $text = $self->get_text; 1267 my $text = $self->get_text;
1082 1268
1083 if ($sym == SDLK_BACKSPACE) { 1269 if ($sym == 8) {
1084 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1270 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1085 } elsif ($sym == SDLK_DELETE) { 1271 } elsif ($sym == 127) {
1086 substr $text, $self->{cursor}, 1, ""; 1272 substr $text, $self->{cursor}, 1, "";
1087 } elsif ($sym == SDLK_LEFT) { 1273 } elsif ($sym == CFClient::SDLK_LEFT) {
1088 --$self->{cursor} if $self->{cursor}; 1274 --$self->{cursor} if $self->{cursor};
1089 } elsif ($sym == SDLK_RIGHT) { 1275 } elsif ($sym == CFClient::SDLK_RIGHT) {
1090 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1276 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1091 } elsif ($sym == SDLK_HOME) { 1277 } elsif ($sym == CFClient::SDLK_HOME) {
1092 $self->{cursor} = 0; 1278 $self->{cursor} = 0;
1093 } elsif ($sym == SDLK_END) { 1279 } elsif ($sym == CFClient::SDLK_END) {
1094 $self->{cursor} = length $text; 1280 $self->{cursor} = length $text;
1281 } elsif ($sym == 27) {
1282 $self->emit ('escape');
1095 } elsif ($uni) { 1283 } elsif ($uni) {
1096 substr $text, $self->{cursor}++, 0, chr $uni; 1284 substr $text, $self->{cursor}++, 0, chr $uni;
1097 } 1285 }
1098 1286
1099 $self->_set_text ($text); 1287 $self->_set_text ($text);
1153 1341
1154 $self->SUPER::_draw; 1342 $self->SUPER::_draw;
1155 1343
1156 #TODO: force update every cursor change :( 1344 #TODO: force update every cursor change :(
1157 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1345 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1346
1347 unless (exists $self->{cur_h}) {
1348 my $text = substr $self->{text}, 0, $self->{cursor};
1349 utf8::encode $text;
1350
1351 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1352 }
1353
1158 glColor @{$self->{fg}}; 1354 glColor @{$self->{fg}};
1159 glBegin GL_LINES; 1355 glBegin GL_LINES;
1160 glVertex $self->{cur_x}, $self->{cur_y};
1161 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1356 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1357 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1162 glEnd; 1358 glEnd;
1163 } 1359 }
1164} 1360}
1165 1361
1362package CFClient::UI::Entry;
1363
1364our @ISA = CFClient::UI::EntryBase::;
1365
1366use CFClient::OpenGL;
1367
1368sub key_down {
1369 my ($self, $ev) = @_;
1370
1371 my $sym = $ev->{sym};
1372
1373 if ($sym == 13) {
1374 $self->emit (activate => $self->get_text);
1375 $self->update;
1376
1377 } else {
1378 $self->SUPER::key_down ($ev);
1379 }
1380
1381}
1382
1166############################################################################# 1383#############################################################################
1167 1384
1168package CFClient::UI::Button; 1385package CFClient::UI::Button;
1169 1386
1170our @ISA = CFClient::UI::Label::; 1387our @ISA = CFClient::UI::Label::;
1171 1388
1172use SDL; 1389use CFClient::OpenGL;
1173use SDL::OpenGL; 1390
1391my @tex =
1392 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1393 qw(b1_button_active.png);
1174 1394
1175sub new { 1395sub new {
1176 my $class = shift; 1396 my $class = shift;
1177 1397
1178 $class->SUPER::new ( 1398 $class->SUPER::new (
1179 padding => 4, 1399 padding => 4,
1180 fg => [1, 1, 1], 1400 fg => [1, 1, 1],
1181 bg => [1, 1, 1, 0.2], 1401 bg => [1, 1, 1, 0.2],
1182 active_bg => [0, 0, 0, 0.5], 1402 active_fg => [0, 0, 1],
1183 active_fg => [1, 1, 0], 1403 can_hover => 1,
1184 border_fg => [1, 1, 0], 1404 align => 0,
1405 valign => 0,
1406 can_events => 1,
1185 @_ 1407 @_
1186 ) 1408 )
1187} 1409}
1188 1410
1189sub button_up { 1411sub button_up {
1198sub _draw { 1420sub _draw {
1199 my ($self) = @_; 1421 my ($self) = @_;
1200 1422
1201 local $self->{fg} = $self->{fg}; 1423 local $self->{fg} = $self->{fg};
1202 1424
1425 if ($GRAB == $self) {
1426 $self->{fg} = $self->{active_fg};
1427 }
1428
1203 glEnable GL_BLEND; 1429 glEnable GL_BLEND;
1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1430 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1431 glEnable GL_TEXTURE_2D;
1432 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1433 glColor 0, 0, 0, 1;
1205 1434
1206 glTranslate 0.375, 0.375, 0; # make line and polyogon coordinates behave similarly 1435 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1207 1436
1208 glBegin GL_LINE_LOOP; 1437 glDisable GL_TEXTURE_2D;
1209 glColor @{$self->{border_fg}};
1210 glVertex 1 , 1;
1211 glVertex 1 , $self->{h} - 2;
1212 glVertex $self->{w} - 2, $self->{h} - 2;
1213 glVertex $self->{w} - 2, 1;
1214 glEnd;
1215
1216 if ($GRAB == $self) {
1217 glColor @{$self->{active_bg}};
1218 $self->{fg} = $self->{active_fg};
1219 } else {
1220 glColor @{$self->{bg}};
1221 }
1222
1223 glBegin GL_QUADS;
1224 glVertex 2 , 2;
1225 glVertex 2 , $self->{h} - 2;
1226 glVertex $self->{w} - 2, $self->{h} - 2;
1227 glVertex $self->{w} - 2, 2;
1228 glEnd;
1229 glDisable GL_BLEND; 1438 glDisable GL_BLEND;
1230 1439
1231 $self->SUPER::_draw; 1440 $self->SUPER::_draw;
1232} 1441}
1233 1442
1234############################################################################# 1443#############################################################################
1235 1444
1236package CFClient::UI::Slider; 1445package CFClient::UI::CheckBox;
1237
1238use strict;
1239
1240use SDL::OpenGL;
1241 1446
1242our @ISA = CFClient::UI::DrawBG::; 1447our @ISA = CFClient::UI::DrawBG::;
1448
1449my @tex =
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1451 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1452
1453use CFClient::OpenGL;
1243 1454
1244sub new { 1455sub new {
1245 my $class = shift; 1456 my $class = shift;
1246 1457
1458 $class->SUPER::new (
1459 padding => 2,
1460 fg => [1, 1, 1],
1461 active_fg => [1, 1, 0],
1462 state => 0,
1463 can_hover => 1,
1464 @_
1465 )
1466}
1467
1468sub size_request {
1469 my ($self) = @_;
1470
1471 ($self->{padding} * 2 + 6) x 2
1472}
1473
1474sub button_down {
1475 my ($self, $ev, $x, $y) = @_;
1476
1477 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1478 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1479 $self->{state} = !$self->{state};
1480 $self->emit (changed => $self->{state});
1481 }
1482}
1483
1484sub _draw {
1485 my ($self) = @_;
1486
1487 $self->SUPER::_draw;
1488
1489 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1490
1491 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1492
1493 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1494
1495 glEnable GL_BLEND;
1496 glEnable GL_TEXTURE_2D;
1497 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1498
1499 my $tex = $self->{state} ? $tex[1] : $tex[0];
1500
1501 $tex->draw_quad (0, 0, $s, $s);
1502
1503 glDisable GL_TEXTURE_2D;
1504 glDisable GL_BLEND;
1505}
1506
1507#############################################################################
1508
1509package CFClient::UI::Image;
1510
1511our @ISA = CFClient::UI::Base::;
1512
1513use CFClient::OpenGL;
1514use Carp qw/confess/;
1515
1516our %loaded_images;
1517
1518sub new {
1519 my $class = shift;
1520
1521 my $self = $class->SUPER::new (can_events => 0, @_);
1522
1523 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1524
1525 $loaded_images{$self->{image}} ||=
1526 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1527
1528 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1529
1530 Scalar::Util::weaken $loaded_images{$self->{image}};
1531
1532 $self->{aspect} = $tex->{w} / $tex->{h};
1533
1534 $self
1535}
1536
1537sub size_request {
1538 my ($self) = @_;
1539
1540 ($self->{tex}->{w}, $self->{tex}->{h})
1541}
1542
1543sub _draw {
1544 my ($self) = @_;
1545
1546 my $tex = $self->{tex};
1547
1548 my ($w, $h) = ($self->{w}, $self->{h});
1549
1550 if ($self->{rot90}) {
1551 glRotate 90, 0, 0, 1;
1552 glTranslate 0, -$self->{w}, 0;
1553
1554 ($w, $h) = ($h, $w);
1555 }
1556
1557 glEnable GL_BLEND;
1558 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1559 glEnable GL_TEXTURE_2D;
1560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1561
1562 $tex->draw_quad (0, 0, $w, $h);
1563
1564 glDisable GL_BLEND;
1565 glDisable GL_TEXTURE_2D;
1566}
1567
1568#############################################################################
1569
1570package CFClient::UI::VGauge;
1571
1572our @ISA = CFClient::UI::Base::;
1573
1574use CFClient::OpenGL;
1575
1576my %tex = (
1577 food => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1580 ],
1581 grace => [
1582 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1583 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1584 ],
1585 hp => [
1586 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1587 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1588 ],
1589 mana => [
1590 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1591 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1592 ],
1593);
1594
1595# eg. VGauge->new (gauge => 'food'), default gauge: food
1596sub new {
1597 my $class = shift;
1598
1599 my $self = $class->SUPER::new (
1600 type => 'food',
1601 @_
1602 );
1603
1604 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 #my $tex = $tex{$self->{type}}[0];
1613 #@$tex{qw(w h)}
1614 (0, 0)
1615}
1616
1617sub set_max {
1618 my ($self, $max) = @_;
1619
1620 $self->{max_val} = $max;
1621}
1622
1623sub set_value {
1624 my ($self, $val, $max) = @_;
1625
1626 $self->set_max ($max)
1627 if defined $max;
1628
1629 $max = $self->{max_val};
1630 $self->{val} = $val;
1631
1632 $self->update;
1633}
1634
1635sub _draw {
1636 my ($self) = @_;
1637
1638 my $tex = $tex{$self->{type}};
1639
1640 my ($w, $h) = ($self->{w}, $self->{h});
1641
1642 if ($self->{vertical}) {
1643 glRotate 90, 0, 0, 1;
1644 glTranslate 0, -$self->{w}, 0;
1645
1646 ($w, $h) = ($h, $w);
1647 }
1648
1649 my $ycut = $self->{val} / ($self->{max_val} || 1);
1650 $ycut = 1 if $self->{val} > $self->{max_val};
1651
1652 my $t1 = $tex->[0];
1653 my $t2 = $tex->[1];
1654
1655 glEnable GL_BLEND;
1656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1657 glEnable GL_TEXTURE_2D;
1658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1659
1660 my $h1 = $self->{h} - $ycut * $self->{h};
1661 my $h2 = $ycut * $self->{h};
1662
1663 glBindTexture GL_TEXTURE_2D, $t1->{name};
1664 glBegin GL_QUADS;
1665 glTexCoord 0 , 0; glVertex 0 , 0;
1666 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1667 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1668 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1669 glEnd;
1670
1671 glBindTexture GL_TEXTURE_2D, $t2->{name};
1672 glBegin GL_QUADS;
1673 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1674 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1675 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1676 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1677 glEnd;
1678
1679 glDisable GL_BLEND;
1680 glDisable GL_TEXTURE_2D;
1681}
1682
1683#############################################################################
1684
1685package CFClient::UI::Gauge;
1686
1687our @ISA = CFClient::UI::VBox::;
1688
1689sub new {
1690 my ($class, %arg) = shift;
1691
1692 my $self = $class->SUPER::new (
1693 @_,
1694 );
1695
1696 $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1697 $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1698 $self->add ($self->{value});
1699 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1700 $self->add ($self->{max});
1701
1702 $self
1703}
1704
1705sub size_request {
1706 my ($self) = @_;
1707 (($self->{max}->size_request)[0], 0)
1708}
1709
1710sub set_fontsize {
1711 my ($self, $fsize) = @_;
1712
1713 $self->{value}->set_fontsize ($fsize);
1714 $self->{max} ->set_fontsize ($fsize);
1715}
1716
1717sub set_value {
1718 my ($self, $val, $max) = @_;
1719
1720 $self->set_max ($max)
1721 if defined $max;
1722
1723 $self->{gauge}->set_value ($val, $max);
1724 $self->{value}->set_text ($val);
1725}
1726
1727sub set_max {
1728 my ($self, $max) = @_;
1729
1730 $self->{gauge}->set_max ($max);
1731 $self->{max}->set_text ($max);
1732}
1733
1734#############################################################################
1735
1736package CFClient::UI::Slider;
1737
1738use strict;
1739
1740use CFClient::OpenGL;
1741
1742our @ISA = CFClient::UI::DrawBG::;
1743
1744my @tex =
1745 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1746 qw(s1_slider.png s1_slider_bg.png);
1747
1748sub new {
1749 my $class = shift;
1750
1247 # range [value, low, high, page] 1751 # range [value, low, high, page]
1248 1752
1753 # TODO: 0-width page
1754 # TODO: req_w/h are wrong with vertical
1755 # TODO: calculations are off
1249 my $self = $class->SUPER::new ( 1756 my $self = $class->SUPER::new (
1250 fg => [1, 1, 1], 1757 fg => [1, 1, 1],
1251 active_fg => [0, 0, 0], 1758 active_fg => [0, 0, 0],
1252 range => [0, 0, 100, 10], 1759 range => [0, 0, 100, 10],
1253 req_w => 40, 1760 req_w => 20,
1254 req_h => 10, 1761 req_h => 20,
1255 vertical => 0, 1762 vertical => 0,
1763 can_hover => 1,
1764 inner_pad => 5,
1256 @_ 1765 @_
1257 ); 1766 );
1258 1767
1259 $self 1768 $self
1260} 1769}
1281 if ($GRAB == $self) { 1790 if ($GRAB == $self) {
1282 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1791 my ($value, $lo, $hi, $page) = @{$self->{range}};
1283 1792
1284 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1793 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1285 1794
1795 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1796 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1797
1798 $x -= $inner_pad_px; # substract the padding
1286 $x = $x * ($hi - $lo) / $w + $lo; 1799 $x = $x * ($hi - $lo) / $inner_w + $lo;
1287 $x = $lo if $x < $lo; 1800 $x = $lo if $x < $lo;
1288 $x = $hi - $page if $x > $hi - $page; 1801 $x = $hi - $page if $x > $hi - $page;
1289 $self->{range}[0] = $x; 1802 $self->{range}[0] = $x;
1290 1803
1291 $self->emit (changed => $x); 1804 $self->emit (changed => $x);
1292 $self->update; 1805 $self->update;
1293 } 1806 }
1807}
1808
1809# the inner_* stuff is for generating a padding for the slider handle,
1810# so that the handle doesn't leave the texture. This calculation isn't 100%
1811# correct propably, but it does the job for now
1812sub _calc_inner_pad_px {
1813 my ($self, $w) = @_;
1814 ($w / 100) * $self->{inner_pad} # % to pixels
1294} 1815}
1295 1816
1296sub _draw { 1817sub _draw {
1297 my ($self) = @_; 1818 my ($self) = @_;
1298 1819
1312 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1833 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1313 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1834 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1314 1835
1315 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1836 my ($value, $lo, $hi, $page) = @{$self->{range}};
1316 1837
1838 $hi = $value + 1 if $lo == $hi;
1839
1840 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1841 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1842
1317 $page = int $page * $w / ($hi - $lo); 1843 $page = int $page * $inner_w / ($hi - $lo);
1318 $value = int +($value - $lo) * $w / ($hi - $lo); 1844 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1319 1845
1320 $w -= $page; 1846 $w -= $page;
1321 $page &= ~1; 1847 $page &= ~1;
1322 glTranslate $page * 0.5, 0, 0; 1848 glTranslate $page * 0.5, 0, 0;
1323 $page ||= 2; 1849 $page ||= 2;
1324 1850
1325 glColor @$fg;
1326 glBegin GL_LINES;
1327 glVertex 0, 0; glVertex 0, $h;
1328 glVertex $w - 1, 0; glVertex $w - 1, $h;
1329 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1330 glEnd;
1331
1332 my $knob_a = $value - $page * 0.5; 1851 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1333 my $knob_b = $value + $page * 0.5; 1852 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1334 1853
1335 glBegin GL_QUADS; 1854 glEnable GL_BLEND;
1336 glColor @$fg; 1855 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1337 glVertex $knob_a, 0; 1856 glEnable GL_TEXTURE_2D;
1338 glVertex $knob_a, $h; 1857 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1339 glVertex $knob_b, $h;
1340 glVertex $knob_b, 0;
1341 1858
1342 if ($knob_a < $knob_b - 2) { 1859 # draw background
1343 glColor @$bg; 1860 $tex[1]->draw_quad (0, 0, $w, $h);
1344 glVertex $knob_a + 1, 1;
1345 glVertex $knob_a + 1, $h - 1;
1346 glVertex $knob_b - 1, $h - 1;
1347 glVertex $knob_b - 1, 1;
1348 }
1349 glEnd;
1350}
1351 1861
1352############################################################################# 1862 # draw handle
1863 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1353 1864
1865 glDisable GL_BLEND;
1866 glDisable GL_TEXTURE_2D;
1867}
1868
1869#############################################################################
1870
1354package CFClient::UI::MapWidget; 1871package CFClient::UI::TextView;
1355 1872
1356use strict;
1357
1358use List::Util qw(min max);
1359
1360use SDL;
1361use SDL::OpenGL;
1362
1363our @ISA = CFClient::UI::Base::; 1873our @ISA = CFClient::UI::HBox::;
1874
1875use CFClient::OpenGL;
1364 1876
1365sub new { 1877sub new {
1366 my $class = shift; 1878 my $class = shift;
1367 1879
1368 $class->SUPER::new ( 1880 my $self = $class->SUPER::new (
1369 z => -1, 1881 fontsize => 1,
1370 list => (glGenLists 1),
1371 @_ 1882 @_,
1883
1884 layout => (new CFClient::Layout),
1885 par => [],
1886 height => 0,
1887 children => [
1888 (new CFClient::UI::Empty expand => 1),
1889 (new CFClient::UI::Slider vertical => 1),
1890 ],
1372 ) 1891 );
1373}
1374 1892
1375sub key_down { 1893 $self->{children}[1]->connect (changed => sub {
1376 print "MAPKEYDOWN\n"; 1894 $self->update;
1377} 1895 });
1378 1896
1379sub key_up { 1897 $self
1380} 1898}
1381 1899
1382sub size_request { 1900sub set_fontsize {
1901 my ($self, $fontsize) = @_;
1902
1903 $self->{fontsize} = $fontsize;
1904 $self->reflow;
1905}
1906
1907sub text_height {
1908 my ($self, $text) = @_;
1909
1910 my $layout = $self->{layout};
1911
1912 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1913 $layout->set_width ($self->{w});
1914 $layout->set_text ($text);
1383 ( 1915
1384 1 + 32 * int $::WIDTH / 32, 1916 ($layout->size)[1]
1385 1 + 32 * int $::HEIGHT / 32, 1917}
1386 ) 1918
1919sub reflow {
1920 my ($self) = @_;
1921
1922 $self->{need_reflow}++;
1923 $self->update;
1924}
1925
1926sub size_allocate {
1927 my ($self, $w, $h) = @_;
1928
1929 $self->SUPER::size_allocate ($w, $h);
1930
1931 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1932 $self->{layout}->set_width ($self->{children}[0]{w});
1933
1934 $self->reflow;
1935}
1936
1937sub add_paragraph {
1938 my ($self, $color, $text) = @_;
1939
1940 #TODO: intelligently "reformat" paragraph
1941
1942 my $height = $self->text_height ($text);
1943
1944 $self->{height} += $height;
1945
1946 push @{$self->{par}}, [$height, $color, $text];
1947
1948 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1949 $self->{children}[1]->update;
1387} 1950}
1388 1951
1389sub update { 1952sub update {
1390 my ($self) = @_; 1953 my ($self) = @_;
1391 1954
1392 $self->{need_update} = 1;
1393 $self->SUPER::update; 1955 $self->SUPER::update;
1394}
1395 1956
1396sub draw { 1957 return unless $self->{h} > 0;
1397 my ($self) = @_;
1398 1958
1959 delete $self->{texture};
1960
1961 $ROOT->on_refresh ($self, sub {
1399 if (delete $self->{need_update}) { 1962 if (delete $self->{need_reflow}) {
1400 glNewList $self->{list}, GL_COMPILE; 1963 my $height = 0;
1401 1964
1402 my $mx = $::CONN->{mapx}; 1965 $height += $_->[0] = $self->text_height ($_->[2])
1403 my $my = $::CONN->{mapy}; 1966 for @{$self->{par}};
1404 1967
1405 my $map = $::CONN->{map}; 1968 $self->{height} = $height;
1406 1969
1407 my ($xofs, $yofs); 1970 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1408 1971
1409 my $sw = 1 + int $::WIDTH / 32; 1972 delete $self->{texture};
1410 my $sh = 1 + int $::HEIGHT / 32;
1411
1412 if ($::CONN->{mapw} > $sw) {
1413 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1414 } else {
1415 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1416 } 1973 }
1417 1974
1418 if ($::CONN->{maph} > $sh) { 1975 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1419 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1976 glClearColor 0, 0, 0, 1;
1420 } else { 1977 glClear GL_COLOR_BUFFER_BIT;
1421 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1978
1979 glEnable GL_BLEND;
1980 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1981 glEnable GL_TEXTURE_2D;
1982 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1983
1984 my $top = int $self->{children}[1]{range}[0];
1985
1986 my $y0 = $top;
1987 my $y1 = $top + $self->{h};
1988
1989 my $y = 0;
1990
1991 my $layout = $self->{layout};
1992
1993 for my $par (@{$self->{par}}) {
1994 my $h = $par->[0];
1995
1996 if ($y0 < $y + $h && $y < $y1) {
1997 $layout->set_text ($par->[2]);
1998
1999 glColor @{ $par->[1] };
2000 my ($W, $H) = $layout->size;
2001 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
2002 }
2003
2004 $y += $h;
2005 }
2006
2007 glDisable GL_TEXTURE_2D;
2008 glDisable GL_BLEND;
1422 } 2009 };
2010 });
2011}
1423 2012
2013sub _draw {
2014 my ($self) = @_;
2015
2016 if ($self->{texture}) {
1424 glEnable GL_TEXTURE_2D; 2017 glEnable GL_TEXTURE_2D;
1425 glEnable GL_BLEND;
1426 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1427 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2018 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1428 2019 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1429 my $sw4 = ($sw + 3) & ~3;
1430 my $darkness = "\x00" x ($sw4 * $sh);
1431
1432 for my $x (0 .. $sw - 1) {
1433 my $row = $map->[$x + $xofs];
1434 for my $y (0 .. $sh - 1) {
1435
1436 my $cell = $row->[$y + $yofs]
1437 or next;
1438
1439 my $dark = $cell->[0];
1440 if ($dark < 0) {
1441 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1442 } else {
1443 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1444 }
1445
1446 for my $num (grep $_, @$cell[1,2,3]) {
1447 my $tex = $::CONN->{face}[$num]{texture} || next;
1448
1449 my ($w, $h) = @$tex{qw(w h)};
1450
1451 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1452 }
1453 }
1454 }
1455
1456# if (1) { # higher quality darkness
1457# $lighting =~ s/(.)/$1$1$1/gs;
1458# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1459#
1460# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1461#
1462# $lighting = $pb->get_pixels;
1463# $lighting =~ s/(.)../$1/gs;
1464# }
1465
1466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1467
1468 $darkness = new CFClient::Texture
1469 w => $sw4,
1470 h => $sh,
1471 data => $darkness,
1472 internalformat => GL_ALPHA,
1473 format => GL_ALPHA;
1474
1475 glColor 0.45, 0.45, 0.45, 1;
1476 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1477
1478 glDisable GL_TEXTURE_2D; 2020 glDisable GL_TEXTURE_2D;
1479 glDisable GL_BLEND;
1480
1481 glEndList;
1482 } 2021 }
1483 2022
1484 glCallList $self->{list}; 2023 $self->{children}[1]->draw;
1485}
1486 2024
1487my %DIR = (
1488 SDLK_KP8, [1, "north"],
1489 SDLK_KP9, [2, "northeast"],
1490 SDLK_KP6, [3, "east"],
1491 SDLK_KP3, [4, "southeast"],
1492 SDLK_KP2, [5, "south"],
1493 SDLK_KP1, [6, "southwest"],
1494 SDLK_KP4, [7, "west"],
1495 SDLK_KP7, [8, "northwest"],
1496
1497 SDLK_UP, [1, "north"],
1498 SDLK_RIGHT, [3, "east"],
1499 SDLK_DOWN, [5, "south"],
1500 SDLK_LEFT, [7, "west"],
1501);
1502
1503sub key_down {
1504 my ($self, $ev) = @_;
1505
1506 my $mod = $ev->key_mod;
1507 my $sym = $ev->key_sym;
1508
1509 if ($sym == SDLK_KP5) {
1510 $::CONN->send ("command stay fire");
1511 } elsif (exists $DIR{$sym}) {
1512 if ($mod & KMOD_SHIFT) {
1513 $self->{shft}++;
1514 $::CONN->send ("command fire $DIR{$sym}[0]");
1515 } elsif ($mod & KMOD_CTRL) {
1516 $self->{ctrl}++;
1517 $::CONN->send ("command run $DIR{$sym}[0]");
1518 } else {
1519 $::CONN->send ("command $DIR{$sym}[1]");
1520 }
1521 }
1522}
1523
1524sub key_up {
1525 my ($self, $ev) = @_;
1526
1527 my $mod = $ev->key_mod;
1528 my $sym = $ev->key_sym;
1529
1530 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1531 $::CONN->send ("command fire_stop");
1532 }
1533 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1534 $::CONN->send ("command run_stop");
1535 }
1536} 2025}
1537 2026
1538############################################################################# 2027#############################################################################
1539 2028
1540package CFClient::UI::Animator; 2029package CFClient::UI::Animator;
1541 2030
1542use SDL::OpenGL; 2031use CFClient::OpenGL;
1543 2032
1544our @ISA = CFClient::UI::Bin::; 2033our @ISA = CFClient::UI::Bin::;
1545 2034
1546sub moveto { 2035sub moveto {
1547 my ($self, $x, $y) = @_; 2036 my ($self, $x, $y) = @_;
1577 glPopMatrix; 2066 glPopMatrix;
1578} 2067}
1579 2068
1580############################################################################# 2069#############################################################################
1581 2070
1582package CFClient::UI::Toplevel; 2071package CFClient::UI::Flopper;
2072
2073our @ISA = CFClient::UI::Button::;
2074
2075sub new {
2076 my $class = shift;
2077
2078 my $self = $class->SUPER::new (
2079 state => 0,
2080 connect_activate => \&toggle_flopper,
2081 can_events => 1,
2082 @_
2083 );
2084
2085 if ($self->{state}) {
2086 $self->{state} = 0;
2087 $self->toggle_flopper;
2088 }
2089
2090 $self
2091}
2092
2093sub toggle_flopper {
2094 my ($self) = @_;
2095
2096 # TODO: use animation
2097 if ($self->{state} = !$self->{state}) {
2098 $CFClient::UI::ROOT->add ($self->{other});
2099 $self->{other}->move ($self->coord2global (0, $self->{h}));
2100 $self->emit ("open");
2101 } else {
2102 $CFClient::UI::ROOT->remove ($self->{other});
2103 $self->emit ("close");
2104 }
2105
2106 $self->emit (changed => $self->{state});
2107}
2108
2109#############################################################################
2110
2111package CFClient::UI::Root;
1583 2112
1584our @ISA = CFClient::UI::Container::; 2113our @ISA = CFClient::UI::Container::;
2114
2115use CFClient::OpenGL;
2116
2117sub check_size {
2118 my ($self) = @_;
2119
2120 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2121}
1585 2122
1586sub size_request { 2123sub size_request {
1587 ($::WIDTH, $::HEIGHT) 2124 ($::WIDTH, $::HEIGHT)
1588} 2125}
1589 2126
1590sub size_allocate { 2127sub configure {
1591 my ($self, $x, $y, $w, $h) = @_; 2128 my ($self, $x, $y, $w, $h) = @_;
1592 2129
1593 $self->_size_allocate ($x, $y, $w, $h); 2130 $self->SUPER::configure ($x, $y, $w, $h);
1594 2131
1595 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1596 for @{$self->{children}}; 2132 for my $child (@{$self->{children}}) {
1597} 2133 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1598 2134
1599sub translate { 2135 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2136 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2137 $child->configure ($X, $Y, $W,$H);
2138 }
2139}
2140
2141sub _topleft {
1600 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
1601 2143
1602 ($x, $y) 2144 ($x, $y)
1603} 2145}
1604 2146
1605sub update { 2147sub update {
1606 my ($self) = @_; 2148 my ($self) = @_;
1607 2149
1608 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2150 $self->check_size;
1609 ::refresh (); 2151 ::refresh ();
1610} 2152}
1611 2153
1612sub add { 2154sub add {
1613 my ($self, $widget) = @_; 2155 my ($self, $child) = @_;
1614 2156
2157 # integerize window positions
2158 $child->{x} = int $child->{x};
2159 $child->{y} = int $child->{y};
2160
1615 $self->SUPER::add ($widget); 2161 $self->SUPER::add ($child);
2162}
1616 2163
1617 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2164sub on_refresh {
2165 my ($self, $id, $cb) = @_;
2166
2167 $self->{refresh_hook}{$id} = $cb;
1618} 2168}
1619 2169
1620sub draw { 2170sub draw {
1621 my ($self) = @_; 2171 my ($self) = @_;
1622 2172
2173 while (my $rcb = delete $self->{refresh_hook}) {
2174 $_->() for values %$rcb;
2175 }
2176
2177 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2178 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2179 glClear GL_COLOR_BUFFER_BIT;
2180
2181 glMatrixMode GL_PROJECTION;
2182 glLoadIdentity;
2183 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2184 glMatrixMode GL_MODELVIEW;
2185 glLoadIdentity;
2186
1623 $self->_draw; 2187 $self->_draw;
1624} 2188}
1625 2189
1626############################################################################# 2190#############################################################################
1627 2191
1628package CFClient::UI; 2192package CFClient::UI;
1629 2193
1630$TOPLEVEL = new CFClient::UI::Toplevel; 2194$ROOT = new CFClient::UI::Root;
1631 2195
16321 21961
1633 2197

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines