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.118 by root, Sat Apr 15 23:25:00 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (length $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_markup ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
99
100 check_tooltip;
68 } 101 }
69 102
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 104}
72 105
87 120
88package CFClient::UI::Base; 121package CFClient::UI::Base;
89 122
90use strict; 123use strict;
91 124
92use SDL::OpenGL; 125use CFClient::OpenGL;
93 126
94sub new { 127sub new {
95 my $class = shift; 128 my $class = shift;
96 129
97 my $self = bless { 130 my $self = bless {
98 x => 0, 131 x => 0,
99 y => 0, 132 y => 0,
100 z => 0, 133 z => 0,
101 w => -1, 134 can_events => 1,
102 h => -1,
103 @_ 135 @_
104 }, $class; 136 }, $class;
105 137
106 for (keys %$self) { 138 for (keys %$self) {
107 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
110 } 142 }
111 143
112 $self 144 $self
113} 145}
114 146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
154sub show {
155 my ($self) = @_;
156
157 return if $self->{parent};
158
159 $CFClient::UI::ROOT->add ($self);
160}
161
162sub hide {
163 my ($self) = @_;
164
165 return unless $self->{parent};
166
167 $self->{parent}->remove ($self);
168}
169
115sub move { 170sub move {
116 my ($self, $x, $y, $z) = @_; 171 my ($self, $x, $y, $z) = @_;
172
117 $self->{x} = int $x; 173 $self->{x} = int $x;
118 $self->{y} = int $y; 174 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
120}
121 176
122sub needs_redraw { 177 $self->update;
123 0 178}
179
180sub set_size {
181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
124} 187}
125 188
126sub size_request { 189sub size_request {
127 require Carp; 190 require Carp;
128 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
129} 192}
130 193
131sub _size_allocate { 194sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
133 196
197 if ($self->{aspect}) {
198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
200
201 # use alignment to adjust x, y
202
203 $x += int +($w - $w2) * 0.5;
204 $y += int +($h - $h2) * 0.5;
205
206 ($w, $h) = ($w2, $h2);
207 }
208
209 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 210 $self->{x} = $x;
135 $self->{y} = $y; 211 $self->{y} = $y;
212 $self->update;
213 }
136 214
137 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 216 $self->{w} = $w;
140 $self->{h} = $h; 217 $self->{h} = $h;
141 218
219 $self->size_allocate ($w, $h);
220 $self->update;
142 1 221 }
143} 222}
144 223
145sub size_allocate { 224sub size_allocate {
225 # nothing to be done
226}
227
228sub set_max_size {
146 my ($self, $x, $y, $w, $h) = @_; 229 my ($self, $w, $h) = @_;
147 230
148 $self->_size_allocate ($x, $y, $w, $h); 231 delete $self->{max_w}; $self->{max_w} = $w if $w;
232 delete $self->{max_h}; $self->{max_h} = $h if $h;
149} 233}
150 234
151# return top left coordinates 235# return top left coordinates
152sub _topleft { 236sub _topleft {
153 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
176 260
177 return if $FOCUS == $self; 261 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 262 return unless $self->{can_focus};
179 263
180 my $focus = $FOCUS; $FOCUS = $self; 264 my $focus = $FOCUS; $FOCUS = $self;
265
266 $self->emit (focus_in => $focus);
267
181 $focus->update if $focus; 268 $focus->update if $focus;
182 $FOCUS->update; 269 $FOCUS->update;
183} 270}
184 271
185sub focus_out { 272sub focus_out {
186 my ($self) = @_; 273 my ($self) = @_;
187 274
188 return unless $FOCUS == $self; 275 return unless $FOCUS == $self;
189 276
190 my $focus = $FOCUS; undef $FOCUS; 277 my $focus = $FOCUS; undef $FOCUS;
278
279 $self->emit (focus_out => $focus);
280
191 $focus->update if $focus; #? 281 $focus->update if $focus; #?
192} 282}
193 283
194sub mouse_motion { } 284sub mouse_motion { }
195sub button_up { } 285sub button_up { }
219 glPopMatrix; 309 glPopMatrix;
220 310
221 if ($self == $HOVER && $self->{can_hover}) { 311 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 312 my ($x, $y) = @$self{qw(x y)};
223 313
224 glColor 0, 0, 1, 0.2; 314 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 315 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 316 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 317 glBegin GL_QUADS;
228 glVertex $x , $y; 318 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 319 glVertex $x + $self->{w}, $y;
230 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
231 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
232 glEnd; 322 glEnd;
233 glDisable GL_BLEND; 323 glDisable GL_BLEND;
234 } 324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
235} 339}
236 340
237sub _draw { 341sub _draw {
238 my ($self) = @_; 342 my ($self) = @_;
239 343
240 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
241} 345}
242 346
243sub find_widget { 347sub find_widget {
244 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
245 351
246 return $self 352 return $self
247 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
248 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
249 355
254 my ($self, $parent) = @_; 360 my ($self, $parent) = @_;
255 361
256 Scalar::Util::weaken ($self->{parent} = $parent); 362 Scalar::Util::weaken ($self->{parent} = $parent);
257} 363}
258 364
365sub check_size {
366 my ($self) = @_;
367
368 $self->{parent}
369 or return 1;
370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
376 $self->{req_w} = $w;
377 $self->{req_h} = $h;
378
379 $self->{parent}->check_size
380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
388 }
389}
390
259sub update { 391sub update {
260 my ($self) = @_; 392 my ($self) = @_;
261 393
262 $self->{parent}->update 394 $self->{parent}->update
263 if $self->{parent}; 395 if $self->{parent};
264} 396}
265 397
266sub connect { 398sub connect {
267 my ($self, $signal, $cb) = @_; 399 my ($self, $signal, $cb) = @_;
268 400
269 push @{ $self->{cb}{$signal} }, $cb; 401 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 402}
271 403
272sub emit { 404sub emit {
273 my ($self, $signal, @args) = @_; 405 my ($self, $signal, @args) = @_;
274 406
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
275 $_->($self, @args) 408 $cb->($self, @args);
276 for @{$self->{cb}{$signal} || []}; 409 }
277} 410}
278 411
279sub DESTROY { 412sub DESTROY {
280 my ($self) = @_; 413 my ($self) = @_;
281 414
287package CFClient::UI::DrawBG; 420package CFClient::UI::DrawBG;
288 421
289our @ISA = CFClient::UI::Base::; 422our @ISA = CFClient::UI::Base::;
290 423
291use strict; 424use strict;
292use SDL::OpenGL; 425use CFClient::OpenGL;
293 426
294sub new { 427sub new {
295 my $class = shift; 428 my $class = shift;
296 429
297 # range [value, low, high, page] 430 # range [value, low, high, page]
326 459
327package CFClient::UI::Empty; 460package CFClient::UI::Empty;
328 461
329our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
330 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
331sub size_request { 469sub size_request {
332 (0, 0) 470 (0, 0)
333} 471}
334 472
335sub draw { } 473sub draw { }
343sub new { 481sub new {
344 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
345 483
346 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
347 485
348 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
349 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
350 488
351 $self 489 $self
352} 490}
353 491
361 $self->{children} = [ 499 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 500 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 501 @{$self->{children}}, $child
364 ]; 502 ];
365 503
366 $self->{w} = $self->{h} = -1; 504 $child->check_size;
505}
506
507sub remove {
508 my ($self, $child) = @_;
509
510 delete $child->{parent};
511
512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
513
514 $self->check_size;
367 $self->update; 515 $self->update;
368} 516}
369 517
370sub remove { 518sub clear {
371 my ($self, $widget) = @_; 519 my ($self) = @_;
372 520
373 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 521 delete $_->{parent}
522 for @{ delete $self->{children} };
374 523
375 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 524 $self->{children} = [];
376} 525}
377 526
378sub find_widget { 527sub find_widget {
379 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
380 529
410 559
411 $class->SUPER::new (children => [$child], %arg) 560 $class->SUPER::new (children => [$child], %arg)
412} 561}
413 562
414sub add { 563sub add {
415 my ($self, $widget) = @_; 564 my ($self, $child) = @_;
416 565
417 $self->{children} = []; 566 $self->{children} = [];
418 567
419 $self->SUPER::add ($widget); 568 $self->SUPER::add ($child);
420} 569}
421 570
422sub remove { 571sub remove {
423 my ($self, $widget) = @_; 572 my ($self, $widget) = @_;
424 573
433sub size_request { 582sub size_request {
434 $_[0]{children}[0]->size_request 583 $_[0]{children}[0]->size_request
435} 584}
436 585
437sub size_allocate { 586sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 587 my ($self, $w, $h) = @_;
439 588
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 589 $self->{children}[0]->configure (0, 0, $w, $h);
443} 590}
444 591
445############################################################################# 592#############################################################################
446 593
447package CFClient::UI::Window; 594package CFClient::UI::Window;
448 595
449our @ISA = CFClient::UI::Bin::; 596our @ISA = CFClient::UI::Bin::;
450 597
451use SDL::OpenGL; 598use CFClient::OpenGL;
452 599
453sub new { 600sub new {
454 my ($class, %arg) = @_; 601 my ($class, %arg) = @_;
455 602
456 my $self = $class->SUPER::new (%arg); 603 my $self = $class->SUPER::new (%arg);
473 $self->child->draw; 620 $self->child->draw;
474 }; 621 };
475} 622}
476 623
477sub size_allocate { 624sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 625 my ($self, $w, $h) = @_;
479 626
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 627 $self->child->configure (0, 0, $w, $h);
483 628
484 $self->render_chld; 629 $self->render_chld;
485} 630}
486 631
487sub _draw { 632sub _draw {
503 glDisable GL_TEXTURE_2D; 648 glDisable GL_TEXTURE_2D;
504} 649}
505 650
506############################################################################# 651#############################################################################
507 652
653package CFClient::UI::ViewPort;
654
655our @ISA = CFClient::UI::Window::;
656
657sub new { die }
658
659sub size_request {
660 my ($self) = @_;
661
662 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
664
665 @$self{qw(child_w child_h)}
666}
667
668sub _draw {
669 my ($self) = @_;
670
671 $self->{children}[1]->draw;
672}
673
674
675#############################################################################
676
508package CFClient::UI::Frame; 677package CFClient::UI::Frame;
509 678
510our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
511 680
512use SDL::OpenGL; 681use CFClient::OpenGL;
513
514sub size_request {
515 my ($self) = @_;
516 my $chld = $self->child
517 or return (0, 0);
518
519 $chld->move (2, 2);
520
521 map { $_ + 4 } $chld->size_request;
522}
523
524sub size_allocate {
525 my ($self, $x, $y, $w, $h) = @_;
526
527 $self->_size_allocate ($x, $y, $w, $h) or return;
528
529 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
530}
531
532sub _draw {
533 my ($self) = @_;
534
535 my $chld = $self->child;
536
537 my ($w, $h) = $chld->size_request;
538
539 glBegin GL_QUADS;
540 glColor 0, 0, 0;
541 glVertex 0 , 0;
542 glVertex 0 , $h + 4;
543 glVertex $w + 4 , $h + 4;
544 glVertex $w + 4 , 0;
545 glEnd;
546
547 $chld->draw;
548}
549
550#############################################################################
551
552package CFClient::UI::FancyFrame;
553
554our @ISA = CFClient::UI::Bin::;
555
556use SDL::OpenGL;
557
558my @tex =
559 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
560 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
561 682
562sub new { 683sub new {
563 my $class = shift; 684 my $class = shift;
564 685
565 # TODO: user_x, user_y, overwrite moveto?
566
567 $class->SUPER::new ( 686 my $self = $class->SUPER::new (
568 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
570 border => int $::FONTSIZE * 0.8, 689 border => 0.8,
571 @_ 690 @_
691 );
692
693 $self
694}
695
696sub _draw {
697 my ($self) = @_;
698
699 my ($w, $h) = ($self->{w}, $self->{h});
700
701 glEnable GL_BLEND;
702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
703 glEnable GL_TEXTURE_2D;
704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
705
706# glBegin GL_QUADS;
707# glColor 0, 0, 0, 0;
708# glVertex 0 , 0;
709# glVertex 0 , $h;
710# glVertex $w, $h;
711# glVertex $w, 0;
712# glEnd;
713
714
715 $self->child->draw;
716 glDisable GL_BLEND;
717 glDisable GL_TEXTURE_2D;
718}
719
720#############################################################################
721
722package CFClient::UI::FancyFrame;
723
724our @ISA = CFClient::UI::Bin::;
725
726use CFClient::OpenGL;
727
728my @tex =
729 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
730 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
731
732sub new {
733 my $class = shift;
734
735 # TODO: user_x, user_y, overwrite moveto?
736
737 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1],
740 border => 0.8,
741 can_events => 1,
742 @_
743 );
744
745 $self->{title} &&= new CFClient::UI::Label
746 align => 0,
747 valign => 1,
748 text => $self->{title},
749 fontsize => 1;
750
751 $self
752}
753
754sub border {
755 int $_[0]{border} * $::FONTSIZE
756}
757
758sub size_request {
759 my ($self) = @_;
760
761 my ($w, $h) = $self->SUPER::size_request;
762
763 (
764 $w + $self->border * 2,
765 $h + $self->border * 2,
572 ) 766 )
573} 767}
574 768
575sub size_request {
576 my ($self) = @_;
577
578 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
579
580 my ($w, $h) = $self->SUPER::size_request;
581
582 (
583 $w + $self->{border} * 2,
584 $h + $self->{border} * 2,
585 )
586}
587
588sub size_allocate { 769sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 770 my ($self, $w, $h) = @_;
590 771
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 772 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 773 $w -= List::Util::max 0, $self->border * 2;
595 774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 778 $self->child->configure ($self->border, $self->border, $w, $h);
597} 779}
598 780
599sub button_down { 781sub button_down {
600 my ($self, $ev, $x, $y) = @_; 782 my ($self, $ev, $x, $y) = @_;
601 783
784 my $border = $self->border;
785
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 786 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 787 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 788
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 790 my ($bw, $bh) = ($self->{w}, $self->{h});
607 791
608 $self->{motion} = sub { 792 $self->{motion} = sub {
609 my ($ev, $x, $y) = @_; 793 my ($ev, $x, $y) = @_;
610 794
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 795 ($x, $y) = ($ev->{x}, $ev->{y});
612 796
613 $self->{user_w} = $bw + $x - $ox; 797 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 798 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 799 $self->check_size;
616 }; 800 };
617 801
618 } elsif ($x >= 0 && $x < $self->{w} 802 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 803 && $y >= 0 && $y < $border) {
620 804
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
622 my ($bx, $by) = ($self->{x}, $self->{y}); 806 my ($bx, $by) = ($self->{x}, $self->{y});
623 807
624 $self->{motion} = sub { 808 $self->{motion} = sub {
625 my ($ev, $x, $y) = @_; 809 my ($ev, $x, $y) = @_;
626 810
627 ($x, $y) = ($ev->motion_x, $ev->motion_y); 811 ($x, $y) = ($ev->{x}, $ev->{y});
628 812
629 $self->move ($bx + $x - $ox, $by + $y - $oy); 813 $self->move ($bx + $x - $ox, $by + $y - $oy);
630 $self->update; 814 $self->update;
631 }; 815 };
632 } 816 }
653 glEnable GL_BLEND; 837 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 838 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 839 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 841
842 my $border = $self->border;
843
658 glColor @{ $self->{border_bg} }; 844 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 845 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 846 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 847 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 848 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 849
664 my $bg = $tex[0]; 850 my $bg = $tex[0];
665 851
666 # TODO: repeat texture not scale 852 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 853 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 856 glColor @{ $self->{bg} };
671 857
672 $bg->{s} = $rep_x; 858 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 859 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 860 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 861 $bg->draw_quad ($border, $border, $cw, $ch);
676 862
677 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 864 glDisable GL_BLEND;
679 865
866 $self->{title}->draw if $self->{title};
680 $self->child->draw; 867 $self->child->draw;
681} 868}
682 869
683############################################################################# 870#############################################################################
684 871
686 873
687our @ISA = CFClient::UI::Base::; 874our @ISA = CFClient::UI::Base::;
688 875
689use List::Util qw(max sum); 876use List::Util qw(max sum);
690 877
691use SDL::OpenGL; 878use CFClient::OpenGL;
692 879
693sub new { 880sub new {
694 my $class = shift; 881 my $class = shift;
695 882
696 $class->SUPER::new ( 883 $class->SUPER::new (
703 my ($self, $x, $y, $child) = @_; 890 my ($self, $x, $y, $child) = @_;
704 891
705 $child->set_parent ($self); 892 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 893 $self->{children}[$y][$x] = $child;
707 894
708 $self->{w} = $self->{h} = -1; 895 $child->check_size;
709 $self->update;
710} 896}
711 897
712# TODO: move to container class maybe? send childs a signal on removal? 898# TODO: move to container class maybe? send childs a signal on removal?
713sub clear { 899sub clear {
714 my ($self) = @_; 900 my ($self) = @_;
727 or next; 913 or next;
728 914
729 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
730 my $widget = $row->[$x] 916 my $widget = $row->[$x]
731 or next; 917 or next;
732 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
733 919
734 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
735 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
736 } 922 }
737 } 923 }
749 (sum @$hs), 935 (sum @$hs),
750 ) 936 )
751} 937}
752 938
753sub size_allocate { 939sub size_allocate {
754 my ($self, $x, $y, $w, $h) = @_; 940 my ($self, $w, $h) = @_;
755
756 $self->_size_allocate ($x, $y, $w, $h) or return;
757 941
758 my ($ws, $hs) = $self->get_wh; 942 my ($ws, $hs) = $self->get_wh;
759 943
760 my $req_w = sum @$ws; 944 my $req_w = sum @$ws;
761 my $req_h = sum @$hs; 945 my $req_h = sum @$hs;
783 967
784 for my $c (0 .. $#$row) { 968 for my $c (0 .. $#$row) {
785 my $col_w = $ws->[$c]; 969 my $col_w = $ws->[$c];
786 970
787 if (my $widget = $row->[$c]) { 971 if (my $widget = $row->[$c]) {
788 $widget->size_allocate ($x, $y, $col_w, $row_h); 972 $widget->configure ($x, $y, $col_w, $row_h);
789 } 973 }
790 974
791 $x += $col_w; 975 $x += $col_w;
792 } 976 }
793 977
838 (List::Util::max map $_->[1], @alloc), 1022 (List::Util::max map $_->[1], @alloc),
839 ) 1023 )
840} 1024}
841 1025
842sub size_allocate { 1026sub size_allocate {
843 my ($self, $x, $y, $w, $h) = @_; 1027 my ($self, $w, $h) = @_;
844
845 $self->_size_allocate ($x, $y, $w, $h);
846 1028
847 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
848 1030
849 my $children = $self->{children}; 1031 my $children = $self->{children};
850 1032
851 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
852 1034
853 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
854 1036
855 if ($req_h > $h) { 1037 if ($req_h > $h) {
856 # ah well, not enough space 1038 # ah well, not enough space
872 1054
873 my $y = 0; 1055 my $y = 0;
874 for (0 .. $#$children) { 1056 for (0 .. $#$children) {
875 my $child = $children->[$_]; 1057 my $child = $children->[$_];
876 my $h = $h[$_]; 1058 my $h = $h[$_];
877 $child->size_allocate ($y, 0, $h, $w); 1059 $child->configure ($y, 0, $h, $w);
878 1060
879 $y += $h; 1061 $y += $h;
880 } 1062 }
1063
1064 1
881} 1065}
882 1066
883############################################################################# 1067#############################################################################
884 1068
885package CFClient::UI::VBox; 1069package CFClient::UI::VBox;
898 (List::Util::sum map $_->[1], @alloc), 1082 (List::Util::sum map $_->[1], @alloc),
899 ) 1083 )
900} 1084}
901 1085
902sub size_allocate { 1086sub size_allocate {
903 my ($self, $x, $y, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
904
905 $self->_size_allocate ($x, $y, $w, $h);
906 1088
907 my $children = $self->{children}; 1089 my $children = $self->{children};
908 1090
909 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
910 1092
911 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
912 1094
913 if ($req_h > $h) { 1095 if ($req_h > $h) {
914 # ah well, not enough space 1096 # ah well, not enough space
928 1110
929 my $y = 0; 1111 my $y = 0;
930 for (0 .. $#$children) { 1112 for (0 .. $#$children) {
931 my $child = $children->[$_]; 1113 my $child = $children->[$_];
932 my $h = $h[$_]; 1114 my $h = $h[$_];
933 $child->size_allocate (0, $y, $w, $h); 1115 $child->configure (0, $y, $w, $h);
934 1116
935 $y += $h; 1117 $y += $h;
936 } 1118 }
1119
1120 1
937} 1121}
938 1122
939############################################################################# 1123#############################################################################
940 1124
941package CFClient::UI::Label; 1125package CFClient::UI::Label;
942 1126
943our @ISA = CFClient::UI::Base::; 1127our @ISA = CFClient::UI::Base::;
944 1128
945use SDL::OpenGL; 1129use CFClient::OpenGL;
946 1130
947sub new { 1131sub new {
948 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
949 1133
950 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
951 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
952 fontsize => $::FONTSIZE, 1137 fontsize => 1,
953 text => "", 1138 text => "",
954 align => -1, 1139 align => -1,
955 valign => -1, 1140 valign => -1,
956 padding => 2, 1141 padding => 2,
957 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
958 %arg 1144 %arg
959 ); 1145 );
960 1146
961 $self->set_text ($self->{text}); 1147 if (exists $self->{template}) {
1148 my $layout = new CFClient::Layout;
1149 $layout->set_text (delete $self->{template});
1150 $self->{template} = $layout;
1151 }
1152
1153 $self->set_text (delete $self->{text}) if exists $self->{text};
1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
962 1155
963 $self 1156 $self
964} 1157}
965 1158
966sub escape_text { 1159sub escape {
967 local $_ = $_[1]; 1160 local $_ = $_[1];
968 1161
969 s/&/&amp;/g; 1162 s/&/&amp;/g;
970 s/>/&gt;/g; 1163 s/>/&gt;/g;
971 s/</&lt;/g; 1164 s/</&lt;/g;
974} 1167}
975 1168
976sub set_text { 1169sub set_text {
977 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
978 1171
979 $self->{text} = $text;
980 $self->{layout}->set_markup ($text); 1172 $self->{layout}->set_text ($text);
981 1173
982 delete $self->{texture}; 1174 delete $self->{texture};
983# $self->{w} = $self->{h} = -1; 1175 $self->check_size;
984 $self->update; 1176 $self->update;
985} 1177}
986 1178
987sub get_text { 1179sub set_markup {
988 my ($self, $text) = @_; 1180 my ($self, $markup) = @_;
989 1181
990 $self->{text} 1182 $self->{layout}->set_markup ($markup);
1183
1184 delete $self->{texture};
1185 $self->check_size;
1186 $self->update;
991} 1187}
992 1188
993sub size_request { 1189sub size_request {
994 my ($self) = @_; 1190 my ($self) = @_;
995 1191
996 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
997 $self->{layout}->set_height ($self->{fontsize}); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1195
998 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1197
1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1201
1202 my ($w2, $h2) = $self->{template}->size;
1203
1204 $w = List::Util::max $w, $w2;
1205 $h = List::Util::max $h, $h2;
1206 }
999 1207
1000 ( 1208 (
1001 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
1002 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
1003 ) 1211 )
1004} 1212}
1005 1213
1006sub size_allocate { 1214sub size_allocate {
1007 my ($self, $x, $y, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
1008
1009 $self->_size_allocate ($x, $y, $w, $h) or return;
1010 1216
1011 delete $self->{texture}; 1217 delete $self->{texture};
1012} 1218}
1013 1219
1014sub update { 1220sub set_fontsize {
1015 my ($self) = @_; 1221 my ($self, $fontsize) = @_;
1016 1222
1223 $self->{fontsize} = $fontsize;
1017 delete $self->{texture}; 1224 delete $self->{texture};
1225 $self->check_size;
1018 $self->SUPER::update; 1226 $self->update;
1019} 1227}
1020 1228
1021sub _draw { 1229sub _draw {
1022 my ($self) = @_; 1230 my ($self) = @_;
1023 1231
1024 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1025 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1026 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1027 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1028 }; 1237 };
1029 1238
1030 glEnable GL_BLEND; 1239 glEnable GL_BLEND;
1031 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1032 glEnable GL_TEXTURE_2D; 1241 glEnable GL_TEXTURE_2D;
1033 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1034 1243
1035 glColor @{$self->{fg}}; 1244 glColor @{$self->{fg}};
1036 1245
1037 my $x = 1246 $self->{ox} = int (
1038 $self->{align} < 0 ? $self->{padding} 1247 $self->{align} < 0 ? $self->{padding}
1039 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1248 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1040 : ($self->{w} - $tex->{w}) * 0.5; 1249 : ($self->{w} - $tex->{w}) * 0.5
1250 );
1041 1251
1042 my $y = 1252 $self->{oy} = int (
1043 $self->{valign} < 0 ? $self->{padding} 1253 $self->{valign} < 0 ? $self->{padding}
1044 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1254 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1045 : ($self->{h} - $tex->{h}) * 0.5; 1255 : ($self->{h} - $tex->{h}) * 0.5
1256 );
1046 1257
1047 $tex->draw_quad (int $x, int $y); 1258 $tex->draw_quad ($self->{ox}, $self->{oy});
1048 1259
1049 glDisable GL_TEXTURE_2D; 1260 glDisable GL_TEXTURE_2D;
1050 glDisable GL_BLEND; 1261 glDisable GL_BLEND;
1051} 1262}
1052 1263
1053############################################################################# 1264#############################################################################
1054 1265
1055package CFClient::UI::Entry; 1266package CFClient::UI::EntryBase;
1056 1267
1057our @ISA = CFClient::UI::Label::; 1268our @ISA = CFClient::UI::Label::;
1058 1269
1059use SDL; 1270use CFClient::OpenGL;
1060use SDL::OpenGL;
1061 1271
1062sub new { 1272sub new {
1063 my $class = shift; 1273 my $class = shift;
1064 1274
1065 $class->SUPER::new ( 1275 $class->SUPER::new (
1068 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1069 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1070 can_hover => 1, 1280 can_hover => 1,
1071 can_focus => 1, 1281 can_focus => 1,
1072 valign => 0, 1282 valign => 0,
1283 can_events => 1,
1073 @_ 1284 @_
1074 ) 1285 )
1075} 1286}
1076 1287
1077sub _set_text { 1288sub _set_text {
1078 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1079 1290
1080 my $old_text = $self->{text}; 1291 delete $self->{cur_h};
1292
1293 return if $self->{text} eq $text;
1294
1295 delete $self->{texture};
1081 1296
1082 $self->{last_activity} = $::NOW; 1297 $self->{last_activity} = $::NOW;
1083
1084 $self->{text} = $text; 1298 $self->{text} = $text;
1085 $self->{layout}->set_width ($self->{w});
1086 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1087 1299
1088 $text =~ s/./*/g if $self->{hidden}; 1300 $text =~ s/./*/g if $self->{hidden};
1301 $self->{layout}->set_text ("$text ");
1089 1302
1090 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1303 $self->emit (changed => $self->{text});
1304}
1091 1305
1092 $text = substr $text, 0, $self->{cursor}; 1306sub get_text {
1093 utf8::encode $text; 1307 $_[0]{text}
1094
1095 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1096
1097 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1098 if $old_text ne $self->{text};
1099} 1308}
1100 1309
1101sub size_request { 1310sub size_request {
1102 my ($self) = @_; 1311 my ($self) = @_;
1103 1312
1105 1314
1106 ($w + 1, $h) # add 1 for cursor 1315 ($w + 1, $h) # add 1 for cursor
1107} 1316}
1108 1317
1109sub size_allocate { 1318sub size_allocate {
1110 my ($self, $x, $y, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1111 1320
1112 $self->SUPER::size_allocate ($x, $y, $w, $h); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1113
1114 $self->_set_text ($self->{text});
1115} 1322}
1116 1323
1117sub set_text { 1324sub set_text {
1118 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1119 1326
1123} 1330}
1124 1331
1125sub key_down { 1332sub key_down {
1126 my ($self, $ev) = @_; 1333 my ($self, $ev) = @_;
1127 1334
1128 my $mod = $ev->key_mod; 1335 my $mod = $ev->{mod};
1129 my $sym = $ev->key_sym; 1336 my $sym = $ev->{sym};
1130
1131 my $uni = $ev->key_unicode; 1337 my $uni = $ev->{unicode};
1132 1338
1133 my $text = $self->get_text; 1339 my $text = $self->get_text;
1134 1340
1135 if ($sym == SDLK_BACKSPACE) { 1341 if ($sym == 8) {
1136 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1342 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1137 } elsif ($sym == SDLK_DELETE) { 1343 } elsif ($sym == 127) {
1138 substr $text, $self->{cursor}, 1, ""; 1344 substr $text, $self->{cursor}, 1, "";
1139 } elsif ($sym == SDLK_LEFT) { 1345 } elsif ($sym == CFClient::SDLK_LEFT) {
1140 --$self->{cursor} if $self->{cursor}; 1346 --$self->{cursor} if $self->{cursor};
1141 } elsif ($sym == SDLK_RIGHT) { 1347 } elsif ($sym == CFClient::SDLK_RIGHT) {
1142 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1348 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1143 } elsif ($sym == SDLK_HOME) { 1349 } elsif ($sym == CFClient::SDLK_HOME) {
1144 $self->{cursor} = 0; 1350 $self->{cursor} = 0;
1145 } elsif ($sym == SDLK_END) { 1351 } elsif ($sym == CFClient::SDLK_END) {
1146 $self->{cursor} = length $text; 1352 $self->{cursor} = length $text;
1147 } elsif ($sym == SDLK_ESCAPE) { 1353 } elsif ($sym == 27) {
1148 $self->emit ('escape'); 1354 $self->emit ('escape');
1149 } elsif ($uni) { 1355 } elsif ($uni) {
1150 substr $text, $self->{cursor}++, 0, chr $uni; 1356 substr $text, $self->{cursor}++, 0, chr $uni;
1151 } 1357 }
1152 1358
1207 1413
1208 $self->SUPER::_draw; 1414 $self->SUPER::_draw;
1209 1415
1210 #TODO: force update every cursor change :( 1416 #TODO: force update every cursor change :(
1211 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1417 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1418
1419 unless (exists $self->{cur_h}) {
1420 my $text = substr $self->{text}, 0, $self->{cursor};
1421 utf8::encode $text;
1422
1423 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1424 }
1425
1212 glColor @{$self->{fg}}; 1426 glColor @{$self->{fg}};
1213 glBegin GL_LINES; 1427 glBegin GL_LINES;
1214 glVertex $self->{cur_x}, $self->{cur_y};
1215 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1428 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1429 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1216 glEnd; 1430 glEnd;
1217 } 1431 }
1218} 1432}
1219 1433
1220package CFClient::UI::LineEntry; 1434package CFClient::UI::Entry;
1221 1435
1222our @ISA = CFClient::UI::Entry::; 1436our @ISA = CFClient::UI::EntryBase::;
1223 1437
1224use SDL; 1438use CFClient::OpenGL;
1225use SDL::OpenGL;
1226 1439
1227sub key_down { 1440sub key_down {
1228 my ($self, $ev) = @_; 1441 my ($self, $ev) = @_;
1229 1442
1230 my $sym = $ev->key_sym; 1443 my $sym = $ev->{sym};
1231 1444
1232 if ($sym == SDLK_RETURN) { 1445 if ($sym == 13) {
1233 $self->emit (activate => $self->get_text); 1446 $self->emit (activate => $self->get_text);
1234 $self->update; 1447 $self->update;
1235 1448
1236 } else { 1449 } else {
1237 $self->SUPER::key_down ($ev); 1450 $self->SUPER::key_down ($ev);
1243 1456
1244package CFClient::UI::Button; 1457package CFClient::UI::Button;
1245 1458
1246our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1247 1460
1248use SDL; 1461use CFClient::OpenGL;
1249use SDL::OpenGL;
1250 1462
1251my @tex = 1463my @tex =
1252 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1253 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1254 1466
1255sub new { 1467sub new {
1256 my $class = shift; 1468 my $class = shift;
1257 1469
1258 $class->SUPER::new ( 1470 $class->SUPER::new (
1259 padding => 4, 1471 padding => 4,
1260 fg => [1, 1, 1], 1472 fg => [1, 1, 1],
1261 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1262 active_fg => [1, 1, 0], 1474 active_fg => [0, 0, 1],
1263 can_hover => 1, 1475 can_hover => 1,
1476 align => 0,
1477 valign => 0,
1478 can_events => 1,
1264 @_ 1479 @_
1265 ) 1480 )
1266} 1481}
1267 1482
1268sub button_up { 1483sub button_up {
1276 1491
1277sub _draw { 1492sub _draw {
1278 my ($self) = @_; 1493 my ($self) = @_;
1279 1494
1280 local $self->{fg} = $self->{fg}; 1495 local $self->{fg} = $self->{fg};
1281 my $tex = $tex[0];
1282
1283 glEnable GL_BLEND;
1284 glEnable GL_TEXTURE_2D;
1285 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1286 1496
1287 if ($GRAB == $self) { 1497 if ($GRAB == $self) {
1288 $self->{fg} = $self->{active_fg}; 1498 $self->{fg} = $self->{active_fg};
1289 } 1499 }
1290 1500
1291 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1501 glEnable GL_BLEND;
1502 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1503 glEnable GL_TEXTURE_2D;
1292 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1504 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1505 glColor 0, 0, 0, 1;
1293 1506
1294 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1507 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1295 1508
1296 glDisable GL_TEXTURE_2D; 1509 glDisable GL_TEXTURE_2D;
1297 glDisable GL_BLEND; 1510 glDisable GL_BLEND;
1298 1511
1299 $self->SUPER::_draw; 1512 $self->SUPER::_draw;
1304package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1305 1518
1306our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1307 1520
1308my @tex = 1521my @tex =
1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1310 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1311 1524
1312use SDL; 1525use CFClient::OpenGL;
1313use SDL::OpenGL;
1314 1526
1315sub new { 1527sub new {
1316 my $class = shift; 1528 my $class = shift;
1317 1529
1318 $class->SUPER::new ( 1530 $class->SUPER::new (
1329 my ($self) = @_; 1541 my ($self) = @_;
1330 1542
1331 ($self->{padding} * 2 + 6) x 2 1543 ($self->{padding} * 2 + 6) x 2
1332} 1544}
1333 1545
1334sub size_allocate {
1335 my ($self, $x, $y, $w, $h) = @_;
1336
1337 $self->_size_allocate ($x, $y, $w, $h);
1338}
1339
1340sub button_down { 1546sub button_down {
1341 my ($self, $ev, $x, $y) = @_; 1547 my ($self, $ev, $x, $y) = @_;
1342 1548
1343 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1549 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1344 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1550 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1370 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1371} 1577}
1372 1578
1373############################################################################# 1579#############################################################################
1374 1580
1581package CFClient::UI::Image;
1582
1583our @ISA = CFClient::UI::Base::;
1584
1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1587
1588our %loaded_images;
1589
1590sub new {
1591 my $class = shift;
1592
1593 my $self = $class->SUPER::new (can_events => 0, @_);
1594
1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1596
1597 $loaded_images{$self->{image}} ||=
1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1599
1600 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1601
1602 Scalar::Util::weaken $loaded_images{$self->{image}};
1603
1604 $self->{aspect} = $tex->{w} / $tex->{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 ($self->{tex}->{w}, $self->{tex}->{h})
1613}
1614
1615sub _draw {
1616 my ($self) = @_;
1617
1618 my $tex = $self->{tex};
1619
1620 my ($w, $h) = ($self->{w}, $self->{h});
1621
1622 if ($self->{rot90}) {
1623 glRotate 90, 0, 0, 1;
1624 glTranslate 0, -$self->{w}, 0;
1625
1626 ($w, $h) = ($h, $w);
1627 }
1628
1629 glEnable GL_BLEND;
1630 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1631 glEnable GL_TEXTURE_2D;
1632 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1633
1634 $tex->draw_quad (0, 0, $w, $h);
1635
1636 glDisable GL_BLEND;
1637 glDisable GL_TEXTURE_2D;
1638}
1639
1640#############################################################################
1641
1642package CFClient::UI::VGauge;
1643
1644our @ISA = CFClient::UI::Base::;
1645
1646use List::Util qw(min max);
1647
1648use CFClient::OpenGL;
1649
1650my %tex = (
1651 food => [
1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1654 ],
1655 grace => [
1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1658 ],
1659 hp => [
1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1662 ],
1663 mana => [
1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1666 ],
1667);
1668
1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1670sub new {
1671 my $class = shift;
1672
1673 my $self = $class->SUPER::new (
1674 type => 'food',
1675 @_
1676 );
1677
1678 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1679
1680 $self
1681}
1682
1683sub size_request {
1684 my ($self) = @_;
1685
1686 #my $tex = $tex{$self->{type}}[0];
1687 #@$tex{qw(w h)}
1688 (0, 0)
1689}
1690
1691sub set_max {
1692 my ($self, $max) = @_;
1693
1694 $self->{max_val} = $max;
1695}
1696
1697sub set_value {
1698 my ($self, $val, $max) = @_;
1699
1700 $self->set_max ($max)
1701 if defined $max;
1702
1703 $max = $self->{max_val};
1704 $self->{val} = $val;
1705
1706 $self->update;
1707}
1708
1709sub _draw {
1710 my ($self) = @_;
1711
1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1714
1715 my ($w, $h) = ($self->{w}, $self->{h});
1716
1717 if ($self->{vertical}) {
1718 glRotate 90, 0, 0, 1;
1719 glTranslate 0, -$self->{w}, 0;
1720
1721 ($w, $h) = ($h, $w);
1722 }
1723
1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1725
1726 my $ycut1 = max 0, min 1, $ycut;
1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1729 my $h1 = $self->{h} * (1 - $ycut1);
1730 my $h2 = $self->{h} * (1 - $ycut2);
1731
1732 glEnable GL_BLEND;
1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1734 glEnable GL_TEXTURE_2D;
1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1736
1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1738 glBegin GL_QUADS;
1739 glTexCoord 0 , 0; glVertex 0 , 0;
1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1743 glEnd;
1744
1745 my $ycut1 = List::Util::min 1, $ycut;
1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1747 glBegin GL_QUADS;
1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1763
1764 glDisable GL_BLEND;
1765 glDisable GL_TEXTURE_2D;
1766}
1767
1768#############################################################################
1769
1770package CFClient::UI::Gauge;
1771
1772our @ISA = CFClient::UI::VBox::;
1773
1774sub new {
1775 my ($class, %arg) = @_;
1776
1777 my $self = $class->SUPER::new (
1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1782 );
1783
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1787
1788 $self
1789}
1790
1791sub set_fontsize {
1792 my ($self, $fsize) = @_;
1793
1794 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize);
1796}
1797
1798sub set_value {
1799 my ($self, $val, $max) = @_;
1800
1801 $self->set_max ($max)
1802 if defined $max;
1803
1804 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1813}
1814
1815#############################################################################
1816
1375package CFClient::UI::Slider; 1817package CFClient::UI::Slider;
1376 1818
1377use strict; 1819use strict;
1378 1820
1379use SDL::OpenGL; 1821use CFClient::OpenGL;
1380 1822
1381our @ISA = CFClient::UI::DrawBG::; 1823our @ISA = CFClient::UI::DrawBG::;
1382 1824
1383my @tex = 1825my @tex =
1384 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1394 # TODO: calculations are off 1836 # TODO: calculations are off
1395 my $self = $class->SUPER::new ( 1837 my $self = $class->SUPER::new (
1396 fg => [1, 1, 1], 1838 fg => [1, 1, 1],
1397 active_fg => [0, 0, 0], 1839 active_fg => [0, 0, 0],
1398 range => [0, 0, 100, 10], 1840 range => [0, 0, 100, 10],
1399 req_w => 40, 1841 req_w => 20,
1400 req_h => 13, 1842 req_h => 20,
1401 vertical => 0, 1843 vertical => 0,
1402 can_hover => 1, 1844 can_hover => 1,
1403 inner_pad => 5, 1845 inner_pad => 5,
1404 @_ 1846 @_
1405 ); 1847 );
1509 1951
1510package CFClient::UI::TextView; 1952package CFClient::UI::TextView;
1511 1953
1512our @ISA = CFClient::UI::HBox::; 1954our @ISA = CFClient::UI::HBox::;
1513 1955
1514use SDL::OpenGL; 1956use CFClient::OpenGL;
1515 1957
1516sub new { 1958sub new {
1517 my $class = shift; 1959 my $class = shift;
1518 1960
1519 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1520 req_w => $::WIDTH / 6,
1521 req_h => $::HEIGHT / 6,
1522 fontsize => $::FONTSIZE, 1962 fontsize => 1,
1963 #font => default_font
1523 @_, 1964 @_,
1524 1965
1525 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1526 par => [], 1967 par => [],
1527 height => 0, 1968 height => 0,
1548sub text_height { 1989sub text_height {
1549 my ($self, $text) = @_; 1990 my ($self, $text) = @_;
1550 1991
1551 my $layout = $self->{layout}; 1992 my $layout = $self->{layout};
1552 1993
1553 $layout->set_height ($self->{fontsize}); 1994 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1554 $layout->set_width ($self->{w}); 1995 $layout->set_width ($self->{w});
1555 $layout->set_text ($text); 1996 $layout->set_text ($text);
1556 1997
1557 ($layout->size)[1] 1998 ($layout->size)[1]
1558} 1999}
1562 2003
1563 $self->{need_reflow}++; 2004 $self->{need_reflow}++;
1564 $self->update; 2005 $self->update;
1565} 2006}
1566 2007
1567sub size_request {
1568 my ($self) = @_;
1569
1570 ($self->{req_w}, $self->{req_h})
1571}
1572
1573sub size_allocate { 2008sub size_allocate {
1574 my ($self, $x, $y, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1575 2010
1576 $self->SUPER::size_allocate ($x, $y, $w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1577 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1578 $self->{layout}->set_height ($self->{fontsize}); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1579 $self->{layout}->set_width ($self->{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1580 2016
1581 $self->reflow; 2017 $self->reflow;
1582} 2018}
1583 2019
1584sub add_paragraph { 2020sub add_paragraph {
1634 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1635 2071
1636 my $y = 0; 2072 my $y = 0;
1637 2073
1638 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1639 2077
1640 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1641 my $h = $par->[0]; 2079 my $h = $par->[0];
1642 2080
1643 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
1671 2109
1672} 2110}
1673 2111
1674############################################################################# 2112#############################################################################
1675 2113
1676package CFClient::UI::MapWidget;
1677
1678use strict;
1679
1680use List::Util qw(min max);
1681
1682use SDL;
1683use SDL::OpenGL;
1684
1685our @ISA = CFClient::UI::Base::;
1686
1687sub new {
1688 my $class = shift;
1689
1690 $class->SUPER::new (
1691 z => -1,
1692 can_focus => 1,
1693 list => (glGenLists 1),
1694 @_
1695 )
1696}
1697
1698sub key_down {
1699 print "MAPKEYDOWN\n";
1700}
1701
1702sub key_up {
1703}
1704
1705sub button_down {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->focus_in;
1709
1710 if ($ev->button == 2) {
1711 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1712 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1713
1714 $self->{motion} = sub {
1715 my ($ev, $x, $y) = @_;
1716
1717 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1718
1719 $::CFG->{map_shift_x} = $bw + $x - $ox;
1720 $::CFG->{map_shift_y} = $bh + $y - $oy;
1721
1722 $self->update;
1723 };
1724 }
1725}
1726
1727sub button_up {
1728 my ($self, $ev, $x, $y) = @_;
1729
1730 delete $self->{motion};
1731}
1732
1733sub mouse_motion {
1734 my ($self, $ev, $x, $y) = @_;
1735
1736 $self->{motion}->($ev, $x, $y) if $self->{motion};
1737}
1738
1739sub size_request {
1740 (
1741 1 + 32 * int $::WIDTH / 32,
1742 1 + 32 * int $::HEIGHT / 32,
1743 )
1744}
1745
1746sub update {
1747 my ($self) = @_;
1748
1749 $self->{need_update} = 1;
1750 $self->SUPER::update;
1751}
1752
1753sub draw {
1754 my ($self) = @_;
1755
1756 if (delete $self->{need_update}) {
1757 glNewList $self->{list}, GL_COMPILE;
1758
1759 if ($::MAP) {
1760 my $sw = int $::WIDTH / 32;
1761 my $sh = int $::HEIGHT / 32;
1762
1763 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1764 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1765
1766 glTranslate $sx0 - 32, $sy0 - 32, 0;
1767
1768 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1769
1770 if ($::CFG->{fow_enable}) {
1771 if ($::CFG->{fow_smooth}) { # smooth fog of war
1772 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1773 glConvolutionFilter2D
1774 GL_CONVOLUTION_2D,
1775 GL_ALPHA,
1776 3, 3,
1777 GL_ALPHA, GL_FLOAT,
1778 pack "f*",
1779 0.1, 0.1, 0.1,
1780 0.1, 0.2, 0.1,
1781 0.1, 0.1, 0.1,
1782 ;
1783 glEnable GL_CONVOLUTION_2D;
1784 }
1785
1786 $self->{fow_texture} = new CFClient::Texture
1787 w => $w,
1788 h => $h,
1789 data => $data,
1790 internalformat => GL_ALPHA,
1791 format => GL_ALPHA;
1792
1793 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1794
1795 glEnable GL_BLEND;
1796 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1797 glEnable GL_TEXTURE_2D;
1798 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1799
1800 glColor +($::CFG->{fow_intensity}) x 3, 1;
1801 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1802
1803 glDisable GL_TEXTURE_2D;
1804 glDisable GL_BLEND;
1805 }
1806
1807 # HACK BEGIN
1808 {
1809 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1810 my ($w, $h) = (250, 250);
1811
1812 glEnable GL_BLEND;
1813 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1814 glEnable GL_TEXTURE_2D;
1815 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1816
1817 $self->{mapmap_texture} =
1818 new CFClient::Texture
1819 w => $w,
1820 h => $h,
1821 data => $::MAP->mapmap ($w, $h),
1822 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1823
1824 $self->{mapmap_texture}->draw_quad (100, 100);
1825
1826 glDisable GL_TEXTURE_2D;
1827 glDisable GL_BLEND;
1828 }
1829 # HACK END
1830 }
1831
1832 glEndList;
1833 }
1834
1835 glPushMatrix;
1836 glCallList $self->{list};
1837 glPopMatrix;
1838
1839 if ($FOCUS != $self) {
1840 glColor 64/255, 64/255, 64/255;
1841 glLogicOp GL_AND;
1842 glEnable GL_COLOR_LOGIC_OP;
1843 glBegin GL_QUADS;
1844 glVertex 0, 0;
1845 glVertex 0, $::HEIGHT;
1846 glVertex $::WIDTH, $::HEIGHT;
1847 glVertex $::WIDTH, 0;
1848 glEnd;
1849 glDisable GL_COLOR_LOGIC_OP;
1850 }
1851}
1852
1853my %DIR = (
1854 SDLK_KP8, [1, "north"],
1855 SDLK_KP9, [2, "northeast"],
1856 SDLK_KP6, [3, "east"],
1857 SDLK_KP3, [4, "southeast"],
1858 SDLK_KP2, [5, "south"],
1859 SDLK_KP1, [6, "southwest"],
1860 SDLK_KP4, [7, "west"],
1861 SDLK_KP7, [8, "northwest"],
1862
1863 SDLK_UP, [1, "north"],
1864 SDLK_RIGHT, [3, "east"],
1865 SDLK_DOWN, [5, "south"],
1866 SDLK_LEFT, [7, "west"],
1867);
1868
1869sub key_down {
1870 my ($self, $ev) = @_;
1871
1872 my $mod = $ev->key_mod;
1873 my $sym = $ev->key_sym;
1874
1875 if ($sym == SDLK_KP5) {
1876 $::CONN->user_send ("command stay fire");
1877 } elsif ($sym == SDLK_a) {
1878 $::CONN->user_send ("command apply");
1879 } elsif ($sym == SDLK_QUOTE) {
1880 $self->emit ('activate_console');
1881 } elsif ($sym == SDLK_SLASH) {
1882 $self->emit ('activate_console' => '/');
1883 } elsif (exists $DIR{$sym}) {
1884 if ($mod & KMOD_SHIFT) {
1885 $self->{shft}++;
1886 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1887 } elsif ($mod & KMOD_CTRL) {
1888 $self->{ctrl}++;
1889 $::CONN->user_send ("command run $DIR{$sym}[0]");
1890 } else {
1891 $::CONN->user_send ("command $DIR{$sym}[1]");
1892 }
1893 }
1894}
1895
1896sub key_up {
1897 my ($self, $ev) = @_;
1898
1899 my $mod = $ev->key_mod;
1900 my $sym = $ev->key_sym;
1901
1902 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1903 $::CONN->user_send ("command fire_stop");
1904 }
1905 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1906 $::CONN->user_send ("command run_stop");
1907 }
1908}
1909
1910#############################################################################
1911
1912package CFClient::UI::Animator; 2114package CFClient::UI::Animator;
1913 2115
1914use SDL::OpenGL; 2116use CFClient::OpenGL;
1915 2117
1916our @ISA = CFClient::UI::Bin::; 2118our @ISA = CFClient::UI::Bin::;
1917 2119
1918sub moveto { 2120sub moveto {
1919 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
1959 my $class = shift; 2161 my $class = shift;
1960 2162
1961 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
1962 state => 0, 2164 state => 0,
1963 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
1964 @_ 2167 @_
1965 ); 2168 );
1966 2169
1967 if ($self->{state}) { 2170 if ($self->{state}) {
1968 $self->{state} = 0; 2171 $self->{state} = 0;
1988 $self->emit (changed => $self->{state}); 2191 $self->emit (changed => $self->{state});
1989} 2192}
1990 2193
1991############################################################################# 2194#############################################################################
1992 2195
2196package CFClient::UI::Tooltip;
2197
2198our @ISA = CFClient::UI::Bin::;
2199
2200use CFClient::OpenGL;
2201
2202sub new {
2203 my $class = shift;
2204
2205 $class->SUPER::new (
2206 @_,
2207 can_events => 0,
2208 )
2209}
2210
2211sub set_markup {
2212 my ($self, $text) = @_;
2213
2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2215 $self->{label}->set_markup ($text);
2216 $self->add ($self->{label});
2217}
2218
2219sub size_request {
2220 my ($self) = @_;
2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2225
2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2233}
2234
2235sub _draw {
2236 my ($self) = @_;
2237
2238 glPushMatrix;
2239 glTranslate 0.375, 0.375;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 glColor 1, 0.8, 0.4;
2244 glBegin GL_QUADS;
2245 glVertex 0 , 0;
2246 glVertex 0 , $h;
2247 glVertex $w, $h;
2248 glVertex $w, 0;
2249 glEnd;
2250
2251 glColor 0, 0, 0;
2252 glBegin GL_LINE_LOOP;
2253 glVertex 0 , 0;
2254 glVertex 0 , $h;
2255 glVertex $w, $h;
2256 glVertex $w, 0;
2257 glEnd;
2258
2259 glPopMatrix;
2260
2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
2264
2265#############################################################################
2266
2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
1993package CFClient::UI::Root; 2305package CFClient::UI::Root;
1994 2306
1995our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
1996 2308
1997use SDL::OpenGL; 2309use CFClient::OpenGL;
2310
2311sub check_size {
2312 my ($self) = @_;
2313
2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2315}
1998 2316
1999sub size_request { 2317sub size_request {
2000 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
2001} 2319}
2002 2320
2003sub size_allocate { 2321sub configure {
2004 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
2005 2323
2006 $self->_size_allocate ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
2007 2325
2008 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2009 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2328
2329 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H);
2332 }
2010} 2333}
2011 2334
2012sub _topleft { 2335sub _topleft {
2013 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
2014 2337
2016} 2339}
2017 2340
2018sub update { 2341sub update {
2019 my ($self) = @_; 2342 my ($self) = @_;
2020 2343
2021 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2344 $self->check_size;
2022 ::refresh (); 2345 ::refresh ();
2023} 2346}
2024 2347
2025sub add { 2348sub add {
2026 my ($self, $widget) = @_; 2349 my ($self, $child) = @_;
2027 2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
2354
2028 $self->SUPER::add ($widget); 2355 $self->SUPER::add ($child);
2029
2030 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2031} 2356}
2032 2357
2033sub on_refresh { 2358sub on_refresh {
2034 my ($self, $id, $cb) = @_; 2359 my ($self, $id, $cb) = @_;
2035 2360
2059############################################################################# 2384#############################################################################
2060 2385
2061package CFClient::UI; 2386package CFClient::UI;
2062 2387
2063$ROOT = new CFClient::UI::Root; 2388$ROOT = new CFClient::UI::Root;
2389$TOOLTIP = new CFClient::UI::Tooltip;
2064 2390
20651 23911
2066 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines