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.107 by root, Fri Apr 14 20:27:35 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 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 $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 = $TOPLEVEL->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->translate ($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 || $TOPLEVEL->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->translate ($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 || $TOPLEVEL->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};
68 }
69 99
100 check_tooltip;
101 }
102
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
104}
105
106# convert position array to integers
107sub harmonize {
108 my ($vals) = @_;
109
110 my $rem = 0;
111
112 for (@$vals) {
113 my $i = int $_ + $rem;
114 $rem += $_ - $i;
115 $_ = $i;
116 }
71} 117}
72 118
73############################################################################# 119#############################################################################
74 120
75package CFClient::UI::Base; 121package CFClient::UI::Base;
76 122
77use strict; 123use strict;
78 124
79use SDL::OpenGL; 125use CFClient::OpenGL;
80 126
81sub new { 127sub new {
82 my $class = shift; 128 my $class = shift;
83 129
84 my $self = bless { 130 my $self = bless {
85 x => 0, 131 x => 0,
86 y => 0, 132 y => 0,
87 z => 0, 133 z => 0,
88 w => -1, 134 can_events => 1,
89 h => -1,
90 @_ 135 @_
91 }, $class; 136 }, $class;
92 137
93 for (keys %$self) { 138 for (keys %$self) {
94 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
97 } 142 }
98 143
99 $self 144 $self
100} 145}
101 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
102sub move { 170sub move {
103 my ($self, $x, $y, $z) = @_; 171 my ($self, $x, $y, $z) = @_;
172
104 $self->{x} = $x; 173 $self->{x} = int $x;
105 $self->{y} = $y; 174 $self->{y} = int $y;
106 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
107}
108 176
109sub needs_redraw { 177 $self->update;
110 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;
111} 187}
112 188
113sub size_request { 189sub size_request {
114 require Carp; 190 require Carp;
115 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
116} 192}
117 193
118sub _size_allocate { 194sub configure {
119 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
120 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) {
121 $self->{x} = $x; 210 $self->{x} = $x;
122 $self->{y} = $y; 211 $self->{y} = $y;
212 $self->update;
213 }
123 214
124 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
125
126 $self->{w} = $w; 216 $self->{w} = $w;
127 $self->{h} = $h; 217 $self->{h} = $h;
128 218
219 $self->size_allocate ($w, $h);
220 $self->update;
129 1 221 }
130} 222}
131 223
132sub size_allocate { 224sub size_allocate {
225 # nothing to be done
226}
227
228sub set_max_size {
133 my ($self, $x, $y, $w, $h) = @_; 229 my ($self, $w, $h) = @_;
134 230
135 $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;
233}
234
235# return top left coordinates
236sub _topleft {
237 my ($self, $x, $y) = @_;
238
239 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 240}
137 241
138# translate global coordinates to local coordinate system 242# translate global coordinates to local coordinate system
139sub translate { 243sub coord2local {
140 my ($self, $x, $y) = @_; 244 my ($self, $x, $y) = @_;
141 245
142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 246 my ($X, $Y) = $self->_topleft;
247 ($x - $X, $y - $Y)
248}
249
250# translate local coordinates to global coordinate system
251sub coord2global {
252 my ($self, $x, $y) = @_;
253
254 my ($X, $Y) = $self->_topleft;
255 ($x + $X, $y + $Y)
143} 256}
144 257
145sub focus_in { 258sub focus_in {
146 my ($self) = @_; 259 my ($self) = @_;
147 260
148 return if $FOCUS == $self; 261 return if $FOCUS == $self;
149 return unless $self->{can_focus}; 262 return unless $self->{can_focus};
150 263
151 my $focus = $FOCUS; $FOCUS = $self; 264 my $focus = $FOCUS; $FOCUS = $self;
265
266 $self->emit (focus_in => $focus);
267
152 $focus->update if $focus; 268 $focus->update if $focus;
153 $FOCUS->update; 269 $FOCUS->update;
154} 270}
155 271
156sub focus_out { 272sub focus_out {
157 my ($self) = @_; 273 my ($self) = @_;
158 274
159 return unless $FOCUS == $self; 275 return unless $FOCUS == $self;
160 276
161 my $focus = $FOCUS; undef $FOCUS; 277 my $focus = $FOCUS; undef $FOCUS;
278
279 $self->emit (focus_out => $focus);
280
162 $focus->update if $focus; #? 281 $focus->update if $focus; #?
163} 282}
164 283
165sub mouse_motion { } 284sub mouse_motion { }
166sub button_up { } 285sub button_up { }
190 glPopMatrix; 309 glPopMatrix;
191 310
192 if ($self == $HOVER && $self->{can_hover}) { 311 if ($self == $HOVER && $self->{can_hover}) {
193 my ($x, $y) = @$self{qw(x y)}; 312 my ($x, $y) = @$self{qw(x y)};
194 313
195 glColor 0, 0, 1, 0.2; 314 glColor 1, 0.8, 0.5, 0.2;
196 glEnable GL_BLEND; 315 glEnable GL_BLEND;
197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 316 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
198 glBegin GL_QUADS; 317 glBegin GL_QUADS;
199 glVertex $x , $y; 318 glVertex $x , $y;
200 glVertex $x + $self->{w}, $y; 319 glVertex $x + $self->{w}, $y;
201 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
202 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
203 glEnd; 322 glEnd;
204 glDisable GL_BLEND; 323 glDisable GL_BLEND;
205 } 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 }
206} 339}
207 340
208sub _draw { 341sub _draw {
209 my ($self) = @_; 342 my ($self) = @_;
210 343
211 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
212} 345}
213 346
214sub find_widget { 347sub find_widget {
215 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
216 351
217 return $self 352 return $self
218 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
219 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
220 355
221 () 356 ()
222} 357}
223 358
224sub set_parent { 359sub set_parent {
225 my ($self, $par) = @_; 360 my ($self, $parent) = @_;
226 361
227 $self->{parent} = $par;
228 Scalar::Util::weaken $self->{parent}; 362 Scalar::Util::weaken ($self->{parent} = $parent);
229} 363}
230 364
231sub get_parent { 365sub check_size {
366 my ($self) = @_;
367
232 $_[0]->{parent} 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 }
233} 389}
234 390
235sub update { 391sub update {
236 my ($self) = @_; 392 my ($self) = @_;
237 393
240} 396}
241 397
242sub connect { 398sub connect {
243 my ($self, $signal, $cb) = @_; 399 my ($self, $signal, $cb) = @_;
244 400
245 push @{ $self->{cb}{$signal} }, $cb; 401 push @{ $self->{signal_cb}{$signal} }, $cb;
246} 402}
247 403
248sub emit { 404sub emit {
249 my ($self, $signal, @args) = @_; 405 my ($self, $signal, @args) = @_;
250 406
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
251 $_->($self, @args) 408 $cb->($self, @args);
252 for @{$self->{cb}{$signal} || []}; 409 }
253} 410}
254 411
255sub DESTROY { 412sub DESTROY {
256 my ($self) = @_; 413 my ($self) = @_;
257 414
263package CFClient::UI::DrawBG; 420package CFClient::UI::DrawBG;
264 421
265our @ISA = CFClient::UI::Base::; 422our @ISA = CFClient::UI::Base::;
266 423
267use strict; 424use strict;
268use SDL::OpenGL; 425use CFClient::OpenGL;
269 426
270sub new { 427sub new {
271 my $class = shift; 428 my $class = shift;
272 429
273 # range [value, low, high, page] 430 # range [value, low, high, page]
302 459
303package CFClient::UI::Empty; 460package CFClient::UI::Empty;
304 461
305our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
306 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
307sub size_request { 469sub size_request {
308 (0, 0) 470 (0, 0)
309} 471}
310 472
311sub draw { } 473sub draw { }
319sub new { 481sub new {
320 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
321 483
322 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
323 485
324 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
325 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
326 488
327 $self 489 $self
328} 490}
329 491
330sub add { 492sub add {
331 my ($self, $chld) = @_; 493 my ($self, $child) = @_;
332 494
333 $chld->set_parent ($self); 495 $child->set_parent ($self);
496
497 use sort 'stable';
334 498
335 $self->{children} = [ 499 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 500 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 501 @{$self->{children}}, $child
338 ]; 502 ];
339 503
340 $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;
341 $self->update; 515 $self->update;
342} 516}
343 517
344sub remove { 518sub clear {
345 my ($self, $widget) = @_; 519 my ($self) = @_;
346 520
347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 521 delete $_->{parent}
522 for @{ delete $self->{children} };
348 523
349 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 524 $self->{children} = [];
350} 525}
351 526
352sub find_widget { 527sub find_widget {
353 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
354 529
384 559
385 $class->SUPER::new (children => [$child], %arg) 560 $class->SUPER::new (children => [$child], %arg)
386} 561}
387 562
388sub add { 563sub add {
389 my ($self, $widget) = @_; 564 my ($self, $child) = @_;
390 565
391 $self->{children} = []; 566 $self->{children} = [];
392 567
393 $self->SUPER::add ($widget); 568 $self->SUPER::add ($child);
394} 569}
395 570
396sub remove { 571sub remove {
397 my ($self, $widget) = @_; 572 my ($self, $widget) = @_;
398 573
407sub size_request { 582sub size_request {
408 $_[0]{children}[0]->size_request 583 $_[0]{children}[0]->size_request
409} 584}
410 585
411sub size_allocate { 586sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_; 587 my ($self, $w, $h) = @_;
413 588
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h); 589 $self->{children}[0]->configure (0, 0, $w, $h);
417} 590}
418 591
419############################################################################# 592#############################################################################
420 593
421package CFClient::UI::Window; 594package CFClient::UI::Window;
422 595
423our @ISA = CFClient::UI::Bin::; 596our @ISA = CFClient::UI::Bin::;
424 597
425use SDL::OpenGL; 598use CFClient::OpenGL;
426 599
427sub new { 600sub new {
428 my ($class, %arg) = @_; 601 my ($class, %arg) = @_;
429 602
430 my $self = $class->SUPER::new (%arg); 603 my $self = $class->SUPER::new (%arg);
447 $self->child->draw; 620 $self->child->draw;
448 }; 621 };
449} 622}
450 623
451sub size_allocate { 624sub size_allocate {
452 my ($self, $x, $y, $w, $h) = @_; 625 my ($self, $w, $h) = @_;
453 626
454 $self->_size_allocate ($x, $y, $w, $h) or return;
455
456 $self->child->size_allocate (0, 0, $w, $h); 627 $self->child->configure (0, 0, $w, $h);
457 628
458 $self->render_chld; 629 $self->render_chld;
459} 630}
460 631
461sub _draw { 632sub _draw {
477 glDisable GL_TEXTURE_2D; 648 glDisable GL_TEXTURE_2D;
478} 649}
479 650
480############################################################################# 651#############################################################################
481 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
482package CFClient::UI::Frame; 677package CFClient::UI::Frame;
483 678
484our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
485 680
486use SDL::OpenGL; 681use CFClient::OpenGL;
487
488sub size_request {
489 my ($self) = @_;
490 my $chld = $self->child
491 or return (0, 0);
492
493 $chld->move (2, 2);
494
495 map { $_ + 4 } $chld->size_request;
496}
497
498sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_;
500
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
504}
505
506sub _draw {
507 my ($self) = @_;
508
509 my $chld = $self->child;
510
511 my ($w, $h) = $chld->size_request;
512
513 glBegin GL_QUADS;
514 glColor 0, 0, 0;
515 glVertex 0 , 0;
516 glVertex 0 , $h + 4;
517 glVertex $w + 4 , $h + 4;
518 glVertex $w + 4 , 0;
519 glEnd;
520
521 $chld->draw;
522}
523
524#############################################################################
525
526package CFClient::UI::FancyFrame;
527
528our @ISA = CFClient::UI::Bin::;
529
530use SDL::OpenGL;
531
532my @tex =
533 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
534 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
535 682
536sub new { 683sub new {
537 my $class = shift; 684 my $class = shift;
538 685
539 # TODO: user_x, user_y, overwrite moveto?
540
541 $class->SUPER::new ( 686 my $self = $class->SUPER::new (
542 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
543 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
544 border => $::FONTSIZE * 0.8, 689 border => 0.8,
545 @_ 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,
546 ) 766 )
547} 767}
548 768
549sub size_request {
550 my ($self) = @_;
551
552 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
553
554 my ($w, $h) = $self->SUPER::size_request;
555
556 (
557 $w + $self->{border} * 2,
558 $h + $self->{border} * 2,
559 )
560}
561
562sub size_allocate { 769sub size_allocate {
563 my ($self, $x, $y, $w, $h) = @_; 770 my ($self, $w, $h) = @_;
564 771
565 $self->_size_allocate ($x, $y, $w, $h) or return;
566
567 $h -= List::Util::max 0, $self->{border} * 2; 772 $h -= List::Util::max 0, $self->border * 2;
568 $w -= List::Util::max 0, $self->{border} * 2; 773 $w -= List::Util::max 0, $self->border * 2;
569 774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
570 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 778 $self->child->configure ($self->border, $self->border, $w, $h);
571} 779}
572 780
573sub button_down { 781sub button_down {
574 my ($self, $ev, $x, $y) = @_; 782 my ($self, $ev, $x, $y) = @_;
575 783
784 my $border = $self->border;
785
576 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 786 if ($x < $self->{w} && $x >= $self->{w} - $border
577 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 787 && $y < $self->{h} && $y >= $self->{h} - $border) {
578 788
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
580 my ($bw, $bh) = ($self->{w}, $self->{h}); 790 my ($bw, $bh) = ($self->{w}, $self->{h});
581 791
582 $self->{motion} = sub { 792 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_; 793 my ($ev, $x, $y) = @_;
584 794
585 ($x, $y) = ($ev->motion_x, $ev->motion_y); 795 ($x, $y) = ($ev->{x}, $ev->{y});
586 796
587 $self->{user_w} = $bw + $x - $ox; 797 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy; 798 $self->{user_h} = $bh + $y - $oy;
589 $self->update; 799 $self->check_size;
590 }; 800 };
591 801
592 } elsif ($x >= 0 && $x < $self->{w} 802 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $self->{border}) { 803 && $y >= 0 && $y < $border) {
594 804
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
596 my ($bx, $by) = ($self->{x}, $self->{y}); 806 my ($bx, $by) = ($self->{x}, $self->{y});
597 807
598 $self->{motion} = sub { 808 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_; 809 my ($ev, $x, $y) = @_;
600 810
601 ($x, $y) = ($ev->motion_x, $ev->motion_y); 811 ($x, $y) = ($ev->{x}, $ev->{y});
602 812
603 $self->move ($bx + $x - $ox, $by + $y - $oy); 813 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update; 814 $self->update;
605 }; 815 };
606 } 816 }
627 glEnable GL_BLEND; 837 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 838 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D; 839 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
631 841
842 my $border = $self->border;
843
632 glColor @{ $self->{border_bg} }; 844 glColor @{ $self->{border_bg} };
633 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 845 $tex[1]->draw_quad (0, 0, $w, $border);
634 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 846 $tex[3]->draw_quad (0, $border, $border, $ch);
635 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 847 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
636 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 848 $tex[4]->draw_quad (0, $h - $border, $w, $border);
637 849
638 my $bg = $tex[0]; 850 my $bg = $tex[0];
639 851
640 # TODO: repeat texture not scale 852 # TODO: repeat texture not scale
641 my $rep_x = $cw / $bg->{w}; 853 my $rep_x = $cw / $bg->{w};
644 glColor @{ $self->{bg} }; 856 glColor @{ $self->{bg} };
645 857
646 $bg->{s} = $rep_x; 858 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y; 859 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1; 860 $bg->{wrap_mode} = 1;
649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 861 $bg->draw_quad ($border, $border, $cw, $ch);
650 862
651 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
652 glDisable GL_BLEND; 864 glDisable GL_BLEND;
653 865
866 $self->{title}->draw if $self->{title};
654 $self->child->draw; 867 $self->child->draw;
655} 868}
656 869
657############################################################################# 870#############################################################################
658 871
660 873
661our @ISA = CFClient::UI::Base::; 874our @ISA = CFClient::UI::Base::;
662 875
663use List::Util qw(max sum); 876use List::Util qw(max sum);
664 877
665use SDL::OpenGL; 878use CFClient::OpenGL;
666 879
667sub new { 880sub new {
668 my $class = shift; 881 my $class = shift;
669 882
670 $class->SUPER::new ( 883 $class->SUPER::new (
672 @_ 885 @_
673 ) 886 )
674} 887}
675 888
676sub add { 889sub add {
677 my ($self, $x, $y, $chld) = @_; 890 my ($self, $x, $y, $child) = @_;
678 891
892 $child->set_parent ($self);
679 $self->{children}[$y][$x] = $chld; 893 $self->{children}[$y][$x] = $child;
680 $chld->set_parent ($self);
681 894
682 $self->{w} = $self->{h} = -1; 895 $child->check_size;
896}
897
898# TODO: move to container class maybe? send childs a signal on removal?
899sub clear {
900 my ($self) = @_;
901
902 delete $self->{children};
683 $self->update; 903 $self->update;
684} 904}
685 905
686sub get_wh { 906sub get_wh {
687 my ($self) = @_; 907 my ($self) = @_;
693 or next; 913 or next;
694 914
695 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
696 my $widget = $row->[$x] 916 my $widget = $row->[$x]
697 or next; 917 or next;
698 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
699 919
700 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
701 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
702 } 922 }
703 } 923 }
715 (sum @$hs), 935 (sum @$hs),
716 ) 936 )
717} 937}
718 938
719sub size_allocate { 939sub size_allocate {
720 my ($self, $x, $y, $w, $h) = @_; 940 my ($self, $w, $h) = @_;
721
722 $self->_size_allocate ($x, $y, $w, $h) or return;
723 941
724 my ($ws, $hs) = $self->get_wh; 942 my ($ws, $hs) = $self->get_wh;
725 943
726 my $req_w = sum @$ws; 944 my $req_w = sum @$ws;
727 my $req_h = sum @$hs; 945 my $req_h = sum @$hs;
733 951
734 # linearly scale sizes 952 # linearly scale sizes
735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 953 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 954 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737 955
738 $_ = int $_ for @$ws; #TODO: avoid rounding problems 956 CFClient::UI::harmonize $ws;
739 $_ = int $_ for @$hs; #TODO: avoid rounding problems 957 CFClient::UI::harmonize $hs;
740 958
741 my $y; 959 my $y;
742 960
743 for my $r (0 .. $#{$self->{children}}) { 961 for my $r (0 .. $#{$self->{children}}) {
744 my $row = $self->{children}[$r] 962 my $row = $self->{children}[$r]
749 967
750 for my $c (0 .. $#$row) { 968 for my $c (0 .. $#$row) {
751 my $col_w = $ws->[$c]; 969 my $col_w = $ws->[$c];
752 970
753 if (my $widget = $row->[$c]) { 971 if (my $widget = $row->[$c]) {
754 $widget->size_allocate ($x, $y, $col_w, $row_h); 972 $widget->configure ($x, $y, $col_w, $row_h);
755 } 973 }
756 974
757 $x += $col_w; 975 $x += $col_w;
758 } 976 }
759 977
804 (List::Util::max map $_->[1], @alloc), 1022 (List::Util::max map $_->[1], @alloc),
805 ) 1023 )
806} 1024}
807 1025
808sub size_allocate { 1026sub size_allocate {
809 my ($self, $x, $y, $w, $h) = @_; 1027 my ($self, $w, $h) = @_;
810
811 $self->_size_allocate ($x, $y, $w, $h);
812 1028
813 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
814 1030
815 my $children = $self->{children}; 1031 my $children = $self->{children};
816 1032
817 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
818 1034
819 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
820 1036
821 if ($req_h > $h) { 1037 if ($req_h > $h) {
822 # ah well, not enough space 1038 # ah well, not enough space
832 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1048 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
833 $h[$_] = $alloc_h; 1049 $h[$_] = $alloc_h;
834 } 1050 }
835 } 1051 }
836 1052
1053 CFClient::UI::harmonize \@h;
1054
837 my $y = 0; 1055 my $y = 0;
838 for (0 .. $#$children) { 1056 for (0 .. $#$children) {
839 my $child = $children->[$_]; 1057 my $child = $children->[$_];
840 my $h = int $h[$_]; 1058 my $h = $h[$_];
841 $child->size_allocate ($y, 0, $h, $w); 1059 $child->configure ($y, 0, $h, $w);
842 1060
843 $y += $h; 1061 $y += $h;
844 } 1062 }
1063
1064 1
845} 1065}
846 1066
847############################################################################# 1067#############################################################################
848 1068
849package CFClient::UI::VBox; 1069package CFClient::UI::VBox;
862 (List::Util::sum map $_->[1], @alloc), 1082 (List::Util::sum map $_->[1], @alloc),
863 ) 1083 )
864} 1084}
865 1085
866sub size_allocate { 1086sub size_allocate {
867 my ($self, $x, $y, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
868
869 $self->_size_allocate ($x, $y, $w, $h);
870 1088
871 my $children = $self->{children}; 1089 my $children = $self->{children};
872 1090
873 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
874 1092
875 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
876 1094
877 if ($req_h > $h) { 1095 if ($req_h > $h) {
878 # ah well, not enough space 1096 # ah well, not enough space
886 1104
887 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1105 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
888 } 1106 }
889 } 1107 }
890 1108
1109 CFClient::UI::harmonize \@h;
1110
891 my $y = 0; 1111 my $y = 0;
892 for (0 .. $#$children) { 1112 for (0 .. $#$children) {
893 my $child = $children->[$_]; 1113 my $child = $children->[$_];
894 my $h = int $h[$_]; 1114 my $h = $h[$_];
895 $child->size_allocate (0, $y, $w, $h); 1115 $child->configure (0, $y, $w, $h);
896 1116
897 $y += $h; 1117 $y += $h;
898 } 1118 }
1119
1120 1
899} 1121}
900 1122
901############################################################################# 1123#############################################################################
902 1124
903package CFClient::UI::Label; 1125package CFClient::UI::Label;
904 1126
905our @ISA = CFClient::UI::Base::; 1127our @ISA = CFClient::UI::Base::;
906 1128
907use SDL::OpenGL; 1129use CFClient::OpenGL;
908 1130
909sub new { 1131sub new {
910 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
911 1133
912 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
913 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
914 fontsize => $::FONTSIZE, 1137 fontsize => 1,
915 text => "", 1138 text => "",
916 align => -1, 1139 align => -1,
1140 valign => -1,
917 padding => 2, 1141 padding => 2,
918 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
919 %arg 1144 %arg
920 ); 1145 );
921 1146
922 $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};
923 1155
924 $self 1156 $self
925} 1157}
926 1158
927sub escape_text { 1159sub escape {
928 local $_ = $_[1]; 1160 local $_ = $_[1];
929 1161
930 s/&/&amp;/g; 1162 s/&/&amp;/g;
931 s/>/&gt;/g; 1163 s/>/&gt;/g;
932 s/</&lt;/g; 1164 s/</&lt;/g;
935} 1167}
936 1168
937sub set_text { 1169sub set_text {
938 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
939 1171
940 $self->{text} = $text;
941 $self->{layout}->set_markup ($text); 1172 $self->{layout}->set_text ($text);
942 1173
943 delete $self->{texture}; 1174 delete $self->{texture};
944# $self->{w} = $self->{h} = -1; 1175 $self->check_size;
945 $self->update; 1176 $self->update;
946} 1177}
947 1178
948sub get_text { 1179sub set_markup {
949 my ($self, $text) = @_; 1180 my ($self, $markup) = @_;
950 1181
951 $self->{text} 1182 $self->{layout}->set_markup ($markup);
1183
1184 delete $self->{texture};
1185 $self->check_size;
1186 $self->update;
952} 1187}
953 1188
954sub size_request { 1189sub size_request {
955 my ($self) = @_; 1190 my ($self) = @_;
956 1191
957 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
958 $self->{layout}->set_height ($self->{fontsize}); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1195
959 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 }
960 1207
961 ( 1208 (
962 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
963 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
964 ) 1211 )
965} 1212}
966 1213
967sub size_allocate { 1214sub size_allocate {
968 my ($self, $x, $y, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
969
970 $self->_size_allocate ($x, $y, $w, $h) or return;
971 1216
972 delete $self->{texture}; 1217 delete $self->{texture};
973} 1218}
974 1219
975sub update { 1220sub set_fontsize {
976 my ($self) = @_; 1221 my ($self, $fontsize) = @_;
977 1222
1223 $self->{fontsize} = $fontsize;
978 delete $self->{texture}; 1224 delete $self->{texture};
1225 $self->check_size;
979 $self->SUPER::update; 1226 $self->update;
980} 1227}
981 1228
982sub _draw { 1229sub _draw {
983 my ($self) = @_; 1230 my ($self) = @_;
984 1231
985 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
986 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
987 $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);
988 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
989 }; 1237 };
990 1238
991 glEnable GL_BLEND; 1239 glEnable GL_BLEND;
992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
993 glEnable GL_TEXTURE_2D; 1241 glEnable GL_TEXTURE_2D;
994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
995 1243
996 glColor @{$self->{fg}}; 1244 glColor @{$self->{fg}};
997 1245
998 my $x = 1246 $self->{ox} = int (
999 $self->{align} < 0 ? $self->{padding} 1247 $self->{align} < 0 ? $self->{padding}
1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1248 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1001 : ($self->{w} - $tex->{w}) * 0.5; 1249 : ($self->{w} - $tex->{w}) * 0.5
1250 );
1002 1251
1003 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1252 $self->{oy} = int (
1253 $self->{valign} < 0 ? $self->{padding}
1254 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1255 : ($self->{h} - $tex->{h}) * 0.5
1256 );
1257
1258 $tex->draw_quad ($self->{ox}, $self->{oy});
1004 1259
1005 glDisable GL_TEXTURE_2D; 1260 glDisable GL_TEXTURE_2D;
1006 glDisable GL_BLEND; 1261 glDisable GL_BLEND;
1007} 1262}
1008 1263
1009############################################################################# 1264#############################################################################
1010 1265
1011package CFClient::UI::Entry; 1266package CFClient::UI::EntryBase;
1012 1267
1013our @ISA = CFClient::UI::Label::; 1268our @ISA = CFClient::UI::Label::;
1014 1269
1015use SDL; 1270use CFClient::OpenGL;
1016use SDL::OpenGL;
1017 1271
1018sub new { 1272sub new {
1019 my $class = shift; 1273 my $class = shift;
1020 1274
1021 $class->SUPER::new ( 1275 $class->SUPER::new (
1023 bg => [0, 0, 0, 0.2], 1277 bg => [0, 0, 0, 0.2],
1024 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1025 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1026 can_hover => 1, 1280 can_hover => 1,
1027 can_focus => 1, 1281 can_focus => 1,
1282 valign => 0,
1283 can_events => 1,
1028 @_ 1284 @_
1029 ) 1285 )
1030} 1286}
1031 1287
1032sub _set_text { 1288sub _set_text {
1033 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1034 1290
1035 my $old_text = $self->{text}; 1291 delete $self->{cur_h};
1292
1293 return if $self->{text} eq $text;
1294
1295 delete $self->{texture};
1036 1296
1037 $self->{last_activity} = $::NOW; 1297 $self->{last_activity} = $::NOW;
1038
1039 $self->{text} = $text; 1298 $self->{text} = $text;
1040 $self->{layout}->set_width ($self->{w});
1041 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1042 1299
1043 $text =~ s/./*/g if $self->{hidden}; 1300 $text =~ s/./*/g if $self->{hidden};
1301 $self->{layout}->set_text ("$text ");
1044 1302
1045 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1303 $self->emit (changed => $self->{text});
1304}
1046 1305
1047 $text = substr $text, 0, $self->{cursor}; 1306sub get_text {
1048 utf8::encode $text; 1307 $_[0]{text}
1049
1050 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1051
1052 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1053 if $old_text ne $self->{text};
1054} 1308}
1055 1309
1056sub size_request { 1310sub size_request {
1057 my ($self) = @_; 1311 my ($self) = @_;
1058 1312
1060 1314
1061 ($w + 1, $h) # add 1 for cursor 1315 ($w + 1, $h) # add 1 for cursor
1062} 1316}
1063 1317
1064sub size_allocate { 1318sub size_allocate {
1065 my ($self, $x, $y, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1066 1320
1067 $self->SUPER::size_allocate ($x, $y, $w, $h); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1068
1069 $self->_set_text ($self->{text});
1070} 1322}
1071 1323
1072sub set_text { 1324sub set_text {
1073 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1074 1326
1078} 1330}
1079 1331
1080sub key_down { 1332sub key_down {
1081 my ($self, $ev) = @_; 1333 my ($self, $ev) = @_;
1082 1334
1083 my $mod = $ev->key_mod; 1335 my $mod = $ev->{mod};
1084 my $sym = $ev->key_sym; 1336 my $sym = $ev->{sym};
1085
1086 my $uni = $ev->key_unicode; 1337 my $uni = $ev->{unicode};
1087 1338
1088 my $text = $self->get_text; 1339 my $text = $self->get_text;
1089 1340
1090 if ($sym == SDLK_BACKSPACE) { 1341 if ($sym == 8) {
1091 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1342 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1092 } elsif ($sym == SDLK_DELETE) { 1343 } elsif ($sym == 127) {
1093 substr $text, $self->{cursor}, 1, ""; 1344 substr $text, $self->{cursor}, 1, "";
1094 } elsif ($sym == SDLK_LEFT) { 1345 } elsif ($sym == CFClient::SDLK_LEFT) {
1095 --$self->{cursor} if $self->{cursor}; 1346 --$self->{cursor} if $self->{cursor};
1096 } elsif ($sym == SDLK_RIGHT) { 1347 } elsif ($sym == CFClient::SDLK_RIGHT) {
1097 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1348 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1098 } elsif ($sym == SDLK_HOME) { 1349 } elsif ($sym == CFClient::SDLK_HOME) {
1099 $self->{cursor} = 0; 1350 $self->{cursor} = 0;
1100 } elsif ($sym == SDLK_END) { 1351 } elsif ($sym == CFClient::SDLK_END) {
1101 $self->{cursor} = length $text; 1352 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) { 1353 } elsif ($sym == 27) {
1103 $self->emit ('escape'); 1354 $self->emit ('escape');
1104 } elsif ($uni) { 1355 } elsif ($uni) {
1105 substr $text, $self->{cursor}++, 0, chr $uni; 1356 substr $text, $self->{cursor}++, 0, chr $uni;
1106 } 1357 }
1107 1358
1162 1413
1163 $self->SUPER::_draw; 1414 $self->SUPER::_draw;
1164 1415
1165 #TODO: force update every cursor change :( 1416 #TODO: force update every cursor change :(
1166 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
1167 glColor @{$self->{fg}}; 1426 glColor @{$self->{fg}};
1168 glBegin GL_LINES; 1427 glBegin GL_LINES;
1169 glVertex $self->{cur_x}, $self->{cur_y};
1170 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};
1171 glEnd; 1430 glEnd;
1172 } 1431 }
1173} 1432}
1174 1433
1175package CFClient::UI::LineEntry; 1434package CFClient::UI::Entry;
1176 1435
1177our @ISA = CFClient::UI::Entry::; 1436our @ISA = CFClient::UI::EntryBase::;
1178 1437
1179use SDL; 1438use CFClient::OpenGL;
1180use SDL::OpenGL;
1181 1439
1182sub key_down { 1440sub key_down {
1183 my ($self, $ev) = @_; 1441 my ($self, $ev) = @_;
1184 1442
1185 my $sym = $ev->key_sym; 1443 my $sym = $ev->{sym};
1186 1444
1187 if ($sym == SDLK_RETURN) { 1445 if ($sym == 13) {
1188 $self->emit (activate => $self->get_text); 1446 $self->emit (activate => $self->get_text);
1189 $self->update; 1447 $self->update;
1190 1448
1191 } else { 1449 } else {
1192 $self->SUPER::key_down ($ev); 1450 $self->SUPER::key_down ($ev);
1198 1456
1199package CFClient::UI::Button; 1457package CFClient::UI::Button;
1200 1458
1201our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1202 1460
1203use SDL; 1461use CFClient::OpenGL;
1204use SDL::OpenGL;
1205 1462
1206my @tex = 1463my @tex =
1207 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1208 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1209 1466
1210sub new { 1467sub new {
1211 my $class = shift; 1468 my $class = shift;
1212 1469
1213 $class->SUPER::new ( 1470 $class->SUPER::new (
1214 padding => 4, 1471 padding => 4,
1215 fg => [1, 1, 1], 1472 fg => [1, 1, 1],
1216 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1217 active_fg => [1, 1, 0], 1474 active_fg => [0, 0, 1],
1218 can_hover => 1, 1475 can_hover => 1,
1476 align => 0,
1477 valign => 0,
1478 can_events => 1,
1219 @_ 1479 @_
1220 ) 1480 )
1221} 1481}
1222 1482
1223sub button_up { 1483sub button_up {
1231 1491
1232sub _draw { 1492sub _draw {
1233 my ($self) = @_; 1493 my ($self) = @_;
1234 1494
1235 local $self->{fg} = $self->{fg}; 1495 local $self->{fg} = $self->{fg};
1236 my $tex = $tex[0];
1237
1238 glEnable GL_BLEND;
1239 glEnable GL_TEXTURE_2D;
1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1241 1496
1242 if ($GRAB == $self) { 1497 if ($GRAB == $self) {
1243 $self->{fg} = $self->{active_fg}; 1498 $self->{fg} = $self->{active_fg};
1244 } 1499 }
1245 1500
1246 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;
1247 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;
1248 1506
1249 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1507 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1250 1508
1251 glDisable GL_TEXTURE_2D; 1509 glDisable GL_TEXTURE_2D;
1252 glDisable GL_BLEND; 1510 glDisable GL_BLEND;
1253 1511
1254 $self->SUPER::_draw; 1512 $self->SUPER::_draw;
1259package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1260 1518
1261our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1262 1520
1263my @tex = 1521my @tex =
1264 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1265 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1266 1524
1267use SDL; 1525use CFClient::OpenGL;
1268use SDL::OpenGL;
1269 1526
1270sub new { 1527sub new {
1271 my $class = shift; 1528 my $class = shift;
1272 1529
1273 $class->SUPER::new ( 1530 $class->SUPER::new (
1284 my ($self) = @_; 1541 my ($self) = @_;
1285 1542
1286 ($self->{padding} * 2 + 6) x 2 1543 ($self->{padding} * 2 + 6) x 2
1287} 1544}
1288 1545
1289sub size_allocate {
1290 my ($self, $x, $y, $w, $h) = @_;
1291
1292 $self->_size_allocate ($x, $y, $w, $h);
1293}
1294
1295sub button_down { 1546sub button_down {
1296 my ($self, $ev, $x, $y) = @_; 1547 my ($self, $ev, $x, $y) = @_;
1297 1548
1298 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1549 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1299 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1550 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1325 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1326} 1577}
1327 1578
1328############################################################################# 1579#############################################################################
1329 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
1330package CFClient::UI::Slider; 1817package CFClient::UI::Slider;
1331 1818
1332use strict; 1819use strict;
1333 1820
1334use SDL::OpenGL; 1821use CFClient::OpenGL;
1335 1822
1336our @ISA = CFClient::UI::DrawBG::; 1823our @ISA = CFClient::UI::DrawBG::;
1337 1824
1338my @tex = 1825my @tex =
1339 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1349 # TODO: calculations are off 1836 # TODO: calculations are off
1350 my $self = $class->SUPER::new ( 1837 my $self = $class->SUPER::new (
1351 fg => [1, 1, 1], 1838 fg => [1, 1, 1],
1352 active_fg => [0, 0, 0], 1839 active_fg => [0, 0, 0],
1353 range => [0, 0, 100, 10], 1840 range => [0, 0, 100, 10],
1354 req_w => 40, 1841 req_w => 20,
1355 req_h => 13, 1842 req_h => 20,
1356 vertical => 0, 1843 vertical => 0,
1357 can_hover => 1, 1844 can_hover => 1,
1358 inner_pad => 5, 1845 inner_pad => 5,
1359 @_ 1846 @_
1360 ); 1847 );
1464 1951
1465package CFClient::UI::TextView; 1952package CFClient::UI::TextView;
1466 1953
1467our @ISA = CFClient::UI::HBox::; 1954our @ISA = CFClient::UI::HBox::;
1468 1955
1469use SDL::OpenGL; 1956use CFClient::OpenGL;
1470 1957
1471sub new { 1958sub new {
1472 my $class = shift; 1959 my $class = shift;
1473 1960
1474 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1475 req_w => $::WIDTH / 6,
1476 req_h => $::HEIGHT / 6,
1477 fontsize => $::FONTSIZE, 1962 fontsize => 1,
1963 #font => default_font
1478 @_, 1964 @_,
1479 1965
1480 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1481 par => [], 1967 par => [],
1482 height => 0, 1968 height => 0,
1503sub text_height { 1989sub text_height {
1504 my ($self, $text) = @_; 1990 my ($self, $text) = @_;
1505 1991
1506 my $layout = $self->{layout}; 1992 my $layout = $self->{layout};
1507 1993
1508 $layout->set_height ($self->{fontsize}); 1994 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1509 $layout->set_width ($self->{w}); 1995 $layout->set_width ($self->{w});
1510 $layout->set_text ($text); 1996 $layout->set_text ($text);
1511 1997
1512 ($layout->size)[1] 1998 ($layout->size)[1]
1513} 1999}
1517 2003
1518 $self->{need_reflow}++; 2004 $self->{need_reflow}++;
1519 $self->update; 2005 $self->update;
1520} 2006}
1521 2007
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{req_w}, $self->{req_h})
1526}
1527
1528sub size_allocate { 2008sub size_allocate {
1529 my ($self, $x, $y, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1530 2010
1531 $self->SUPER::size_allocate ($x, $y, $w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1532 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1533 $self->{layout}->set_height ($self->{fontsize}); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1534 $self->{layout}->set_width ($self->{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1535 2016
1536 $self->reflow; 2017 $self->reflow;
1537} 2018}
1538 2019
1539sub add_paragraph { 2020sub add_paragraph {
1558 2039
1559 return unless $self->{h} > 0; 2040 return unless $self->{h} > 0;
1560 2041
1561 delete $self->{texture}; 2042 delete $self->{texture};
1562 2043
1563 $TOPLEVEL->on_refresh ($self, sub { 2044 $ROOT->on_refresh ($self, sub {
1564 if (delete $self->{need_reflow}) { 2045 if (delete $self->{need_reflow}) {
1565 my $height = 0; 2046 my $height = 0;
1566 2047
1567 $height += $_->[0] = $self->text_height ($_->[2]) 2048 $height += $_->[0] = $self->text_height ($_->[2])
1568 for @{$self->{par}}; 2049 for @{$self->{par}};
1589 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1590 2071
1591 my $y = 0; 2072 my $y = 0;
1592 2073
1593 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1594 2077
1595 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1596 my $h = $par->[0]; 2079 my $h = $par->[0];
1597 2080
1598 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
1626 2109
1627} 2110}
1628 2111
1629############################################################################# 2112#############################################################################
1630 2113
1631package CFClient::UI::MapWidget;
1632
1633use strict;
1634
1635use List::Util qw(min max);
1636
1637use SDL;
1638use SDL::OpenGL;
1639
1640our @ISA = CFClient::UI::Base::;
1641
1642sub new {
1643 my $class = shift;
1644
1645 $class->SUPER::new (
1646 z => -1,
1647 can_focus => 1,
1648 list => (glGenLists 1),
1649 @_
1650 )
1651}
1652
1653sub key_down {
1654 print "MAPKEYDOWN\n";
1655}
1656
1657sub key_up {
1658}
1659
1660sub size_request {
1661 (
1662 1 + 32 * int $::WIDTH / 32,
1663 1 + 32 * int $::HEIGHT / 32,
1664 )
1665}
1666
1667sub update {
1668 my ($self) = @_;
1669
1670 $self->{need_update} = 1;
1671 $self->SUPER::update;
1672}
1673
1674sub draw {
1675 my ($self) = @_;
1676
1677 $self->{need_update}++;#d#
1678 if (delete $self->{need_update}) {
1679 glNewList $self->{list}, GL_COMPILE;
1680
1681 my $sw = int $::WIDTH / 32;
1682 my $sh = int $::HEIGHT / 32;
1683
1684 if ($::MAP) {
1685 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1686
1687 if ($::CFG->{fow_enable}) {
1688 if ($::CFG->{fow_smooth}) { # smooth fog of war
1689 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1690 glConvolutionFilter2D
1691 GL_CONVOLUTION_2D,
1692 GL_ALPHA,
1693 3, 3,
1694 GL_ALPHA, GL_FLOAT,
1695 pack "f*",
1696 0.1, 0.1, 0.1,
1697 0.1, 0.2, 0.1,
1698 0.1, 0.1, 0.1,
1699 ;
1700 glEnable GL_CONVOLUTION_2D;
1701 }
1702
1703 my $tex = new CFClient::Texture
1704 w => $w,
1705 h => $h,
1706 data => $data,
1707 internalformat => GL_ALPHA,
1708 format => GL_ALPHA;
1709
1710 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1711
1712 glEnable GL_BLEND;
1713 glEnable GL_TEXTURE_2D;
1714 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1715
1716 glColor +($::CFG->{fow_intensity}) x 3, 1;
1717 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1718
1719 glDisable GL_TEXTURE_2D;
1720 glDisable GL_BLEND;
1721 }
1722 }
1723
1724 glEndList;
1725 }
1726
1727 glCallList $self->{list};
1728
1729 if ($FOCUS != $self) {
1730 glColor 64/255, 64/255, 64/255;
1731 glLogicOp GL_AND;
1732 glEnable GL_COLOR_LOGIC_OP;
1733 glBegin GL_QUADS;
1734 glVertex 0, 0;
1735 glVertex 0, $::HEIGHT;
1736 glVertex $::WIDTH, $::HEIGHT;
1737 glVertex $::WIDTH, 0;
1738 glEnd;
1739 glDisable GL_COLOR_LOGIC_OP;
1740 }
1741}
1742
1743my %DIR = (
1744 SDLK_KP8, [1, "north"],
1745 SDLK_KP9, [2, "northeast"],
1746 SDLK_KP6, [3, "east"],
1747 SDLK_KP3, [4, "southeast"],
1748 SDLK_KP2, [5, "south"],
1749 SDLK_KP1, [6, "southwest"],
1750 SDLK_KP4, [7, "west"],
1751 SDLK_KP7, [8, "northwest"],
1752
1753 SDLK_UP, [1, "north"],
1754 SDLK_RIGHT, [3, "east"],
1755 SDLK_DOWN, [5, "south"],
1756 SDLK_LEFT, [7, "west"],
1757);
1758
1759sub key_down {
1760 my ($self, $ev) = @_;
1761
1762 my $mod = $ev->key_mod;
1763 my $sym = $ev->key_sym;
1764
1765 if ($sym == SDLK_KP5) {
1766 $::CONN->user_send ("command stay fire");
1767 } elsif ($sym == SDLK_a) {
1768 $::CONN->user_send ("command apply");
1769 } elsif ($sym == SDLK_QUOTE) {
1770 $self->emit ('activate_console');
1771 } elsif ($sym == SDLK_SLASH) {
1772 $self->emit ('activate_console' => '/');
1773 } elsif (exists $DIR{$sym}) {
1774 if ($mod & KMOD_SHIFT) {
1775 $self->{shft}++;
1776 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1777 } elsif ($mod & KMOD_CTRL) {
1778 $self->{ctrl}++;
1779 $::CONN->user_send ("command run $DIR{$sym}[0]");
1780 } else {
1781 $::CONN->user_send ("command $DIR{$sym}[1]");
1782 }
1783 }
1784}
1785
1786sub key_up {
1787 my ($self, $ev) = @_;
1788
1789 my $mod = $ev->key_mod;
1790 my $sym = $ev->key_sym;
1791
1792 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1793 $::CONN->user_send ("command fire_stop");
1794 }
1795 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1796 $::CONN->user_send ("command run_stop");
1797 }
1798}
1799
1800#############################################################################
1801
1802package CFClient::UI::Animator; 2114package CFClient::UI::Animator;
1803 2115
1804use SDL::OpenGL; 2116use CFClient::OpenGL;
1805 2117
1806our @ISA = CFClient::UI::Bin::; 2118our @ISA = CFClient::UI::Bin::;
1807 2119
1808sub moveto { 2120sub moveto {
1809 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
1849 my $class = shift; 2161 my $class = shift;
1850 2162
1851 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
1852 state => 0, 2164 state => 0,
1853 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
1854 @_ 2167 @_
1855 ); 2168 );
1856 2169
1857 if ($self->{state}) { 2170 if ($self->{state}) {
1858 $self->{state} = 0; 2171 $self->{state} = 0;
1865sub toggle_flopper { 2178sub toggle_flopper {
1866 my ($self) = @_; 2179 my ($self) = @_;
1867 2180
1868 # TODO: use animation 2181 # TODO: use animation
1869 if ($self->{state} = !$self->{state}) { 2182 if ($self->{state} = !$self->{state}) {
1870 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2183 $CFClient::UI::ROOT->add ($self->{other});
1871 $self->{other}->move ( 2184 $self->{other}->move ($self->coord2global (0, $self->{h}));
1872 ($::WIDTH - $self->{other}{w}) * 0.5, 2185 $self->emit ("open");
1873 ($::HEIGHT - $self->{other}{h}) * 0.5,
1874 );
1875 } else { 2186 } else {
1876 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2187 $CFClient::UI::ROOT->remove ($self->{other});
2188 $self->emit ("close");
2189 }
2190
2191 $self->emit (changed => $self->{state});
2192}
2193
2194#############################################################################
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;
1877 } 2250
1878} 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;
1879 2260
1880############################################################################# 2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
1881 2264
2265#############################################################################
2266
1882package CFClient::UI::Toplevel; 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
2305package CFClient::UI::Root;
1883 2306
1884our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
1885 2308
1886use SDL::OpenGL; 2309use CFClient::OpenGL;
2310
2311sub check_size {
2312 my ($self) = @_;
2313
2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2315}
1887 2316
1888sub size_request { 2317sub size_request {
1889 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
1890} 2319}
1891 2320
1892sub size_allocate { 2321sub configure {
1893 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
1894 2323
1895 $self->_size_allocate ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
1896 2325
1897 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1898 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
1899} 2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1900 2328
1901sub translate { 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 }
2333}
2334
2335sub _topleft {
1902 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
1903 2337
1904 ($x, $y) 2338 ($x, $y)
1905} 2339}
1906 2340
1907sub update { 2341sub update {
1908 my ($self) = @_; 2342 my ($self) = @_;
1909 2343
1910 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2344 $self->check_size;
1911 ::refresh (); 2345 ::refresh ();
1912} 2346}
1913 2347
1914sub add { 2348sub add {
1915 my ($self, $widget) = @_; 2349 my ($self, $child) = @_;
1916 2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
2354
1917 $self->SUPER::add ($widget); 2355 $self->SUPER::add ($child);
1918
1919 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1920} 2356}
1921 2357
1922sub on_refresh { 2358sub on_refresh {
1923 my ($self, $id, $cb) = @_; 2359 my ($self, $id, $cb) = @_;
1924 2360
1947 2383
1948############################################################################# 2384#############################################################################
1949 2385
1950package CFClient::UI; 2386package CFClient::UI;
1951 2387
2388$ROOT = new CFClient::UI::Root;
1952$TOPLEVEL = new CFClient::UI::Toplevel; 2389$TOOLTIP = new CFClient::UI::Tooltip;
1953 2390
19541 23911
1955 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines