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.95 by root, Thu Apr 13 23:31:43 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; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $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;
262 return unless $self->{can_focus};
149 263
150 my $focus = $FOCUS; $FOCUS = $self; 264 my $focus = $FOCUS; $FOCUS = $self;
265
266 $self->emit (focus_in => $focus);
267
151 $focus->update if $focus; 268 $focus->update if $focus;
152 $FOCUS->update; 269 $FOCUS->update;
153} 270}
154 271
155sub focus_out { 272sub focus_out {
156 my ($self) = @_; 273 my ($self) = @_;
157 274
158 return unless $FOCUS == $self; 275 return unless $FOCUS == $self;
159 276
160 my $focus = $FOCUS; undef $FOCUS; 277 my $focus = $FOCUS; undef $FOCUS;
278
279 $self->emit (focus_out => $focus);
280
161 $focus->update if $focus; #? 281 $focus->update if $focus; #?
162} 282}
163 283
164sub mouse_motion { } 284sub mouse_motion { }
165sub button_up { } 285sub button_up { }
186 glPushMatrix; 306 glPushMatrix;
187 glTranslate $self->{x}, $self->{y}, 0; 307 glTranslate $self->{x}, $self->{y}, 0;
188 $self->_draw; 308 $self->_draw;
189 glPopMatrix; 309 glPopMatrix;
190 310
191 if ($self == $HOVER) { 311 if ($self == $HOVER && $self->{can_hover}) {
192 my ($x, $y) = @$self{qw(x y)}; 312 my ($x, $y) = @$self{qw(x y)};
193 313
194 glColor 0, 0, 1, 0.2; 314 glColor 1, 0.8, 0.5, 0.2;
195 glEnable GL_BLEND; 315 glEnable GL_BLEND;
196 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 316 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
197 glBegin GL_QUADS; 317 glBegin GL_QUADS;
198 glVertex $x , $y; 318 glVertex $x , $y;
199 glVertex $x + $self->{w}, $y; 319 glVertex $x + $self->{w}, $y;
200 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
201 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
202 glEnd; 322 glEnd;
203 glDisable GL_BLEND; 323 glDisable GL_BLEND;
204 } 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 }
205} 339}
206 340
207sub _draw { 341sub _draw {
208 my ($self) = @_; 342 my ($self) = @_;
209 343
210 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
211} 345}
212 346
213sub find_widget { 347sub find_widget {
214 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
215 351
216 return $self 352 return $self
217 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
218 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
219 355
220 () 356 ()
221} 357}
222 358
223sub set_parent { 359sub set_parent {
224 my ($self, $par) = @_; 360 my ($self, $parent) = @_;
225 361
226 $self->{parent} = $par;
227 Scalar::Util::weaken $self->{parent}; 362 Scalar::Util::weaken ($self->{parent} = $parent);
228} 363}
229 364
230sub get_parent { 365sub check_size {
366 my ($self) = @_;
367
231 $_[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 }
232} 389}
233 390
234sub update { 391sub update {
235 my ($self) = @_; 392 my ($self) = @_;
236 393
239} 396}
240 397
241sub connect { 398sub connect {
242 my ($self, $signal, $cb) = @_; 399 my ($self, $signal, $cb) = @_;
243 400
244 push @{ $self->{cb}{$signal} }, $cb; 401 push @{ $self->{signal_cb}{$signal} }, $cb;
245} 402}
246 403
247sub emit { 404sub emit {
248 my ($self, $signal, @args) = @_; 405 my ($self, $signal, @args) = @_;
249 406
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
250 $_->($self, @args) 408 $cb->($self, @args);
251 for @{$self->{cb}{$signal} || []}; 409 }
252} 410}
253 411
254sub DESTROY { 412sub DESTROY {
255 my ($self) = @_; 413 my ($self) = @_;
256 414
262package CFClient::UI::DrawBG; 420package CFClient::UI::DrawBG;
263 421
264our @ISA = CFClient::UI::Base::; 422our @ISA = CFClient::UI::Base::;
265 423
266use strict; 424use strict;
267use SDL::OpenGL; 425use CFClient::OpenGL;
268 426
269sub new { 427sub new {
270 my $class = shift; 428 my $class = shift;
271 429
272 # range [value, low, high, page] 430 # range [value, low, high, page]
301 459
302package CFClient::UI::Empty; 460package CFClient::UI::Empty;
303 461
304our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
305 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
306sub size_request { 469sub size_request {
307 (0, 0) 470 (0, 0)
308} 471}
309 472
310sub draw { } 473sub draw { }
318sub new { 481sub new {
319 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
320 483
321 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
322 485
323 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
324 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
325 488
326 $self 489 $self
327} 490}
328 491
329sub add { 492sub add {
330 my ($self, $chld) = @_; 493 my ($self, $child) = @_;
331 494
332 $chld->set_parent ($self); 495 $child->set_parent ($self);
496
497 use sort 'stable';
333 498
334 $self->{children} = [ 499 $self->{children} = [
335 sort { $a->{z} <=> $b->{z} } 500 sort { $a->{z} <=> $b->{z} }
336 @{$self->{children}}, $chld 501 @{$self->{children}}, $child
337 ]; 502 ];
338 503
339 $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;
340 $self->update; 515 $self->update;
341} 516}
342 517
343sub remove { 518sub clear {
344 my ($self, $widget) = @_; 519 my ($self) = @_;
345 520
346 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 521 delete $_->{parent}
522 for @{ delete $self->{children} };
347 523
348 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 524 $self->{children} = [];
349} 525}
350 526
351sub find_widget { 527sub find_widget {
352 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
353 529
383 559
384 $class->SUPER::new (children => [$child], %arg) 560 $class->SUPER::new (children => [$child], %arg)
385} 561}
386 562
387sub add { 563sub add {
388 my ($self, $widget) = @_; 564 my ($self, $child) = @_;
389 565
390 $self->{children} = []; 566 $self->{children} = [];
391 567
392 $self->SUPER::add ($widget); 568 $self->SUPER::add ($child);
393} 569}
394 570
395sub remove { 571sub remove {
396 my ($self, $widget) = @_; 572 my ($self, $widget) = @_;
397 573
406sub size_request { 582sub size_request {
407 $_[0]{children}[0]->size_request 583 $_[0]{children}[0]->size_request
408} 584}
409 585
410sub size_allocate { 586sub size_allocate {
411 my ($self, $x, $y, $w, $h) = @_; 587 my ($self, $w, $h) = @_;
412 588
413 $self->_size_allocate ($x, $y, $w, $h) or return;
414
415 $self->{children}[0]->size_allocate (0, 0, $w, $h); 589 $self->{children}[0]->configure (0, 0, $w, $h);
416} 590}
417 591
418############################################################################# 592#############################################################################
419 593
420package CFClient::UI::Window; 594package CFClient::UI::Window;
421 595
422our @ISA = CFClient::UI::Bin::; 596our @ISA = CFClient::UI::Bin::;
423 597
424use SDL::OpenGL; 598use CFClient::OpenGL;
425 599
426sub new { 600sub new {
427 my ($class, %arg) = @_; 601 my ($class, %arg) = @_;
428 602
429 my $self = $class->SUPER::new (%arg); 603 my $self = $class->SUPER::new (%arg);
438} 612}
439 613
440sub render_chld { 614sub render_chld {
441 my ($self) = @_; 615 my ($self) = @_;
442 616
443 $self->{texture} = 617 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
444 CFClient::Texture->new_from_opengl ( 618 glClearColor 0, 0, 0, 1;
445 $self->{w}, $self->{h}, sub { $self->child->draw } 619 glClear GL_COLOR_BUFFER_BIT;
446 ); 620 $self->child->draw;
621 };
447} 622}
448 623
449sub size_allocate { 624sub size_allocate {
450 my ($self, $x, $y, $w, $h) = @_; 625 my ($self, $w, $h) = @_;
451 626
452 $self->_size_allocate ($x, $y, $w, $h) or return;
453
454 $self->child->size_allocate (0, 0, $w, $h); 627 $self->child->configure (0, 0, $w, $h);
455 628
456 $self->render_chld; 629 $self->render_chld;
457} 630}
458 631
459sub _draw { 632sub _draw {
475 glDisable GL_TEXTURE_2D; 648 glDisable GL_TEXTURE_2D;
476} 649}
477 650
478############################################################################# 651#############################################################################
479 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
480package CFClient::UI::Frame; 677package CFClient::UI::Frame;
481 678
482our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
483 680
484use SDL::OpenGL; 681use CFClient::OpenGL;
485 682
486sub size_request { 683sub new {
487 my ($self) = @_; 684 my $class = shift;
488 my $chld = $self->child
489 or return (0, 0);
490 685
491 $chld->move (2, 2); 686 my $self = $class->SUPER::new (
492 687 bg => [1, 1, 1, 1],
493 map { $_ + 4 } $chld->size_request; 688 border_bg => [1, 1, 1, 1],
494} 689 border => 0.8,
495 690 @_
496sub size_allocate {
497 my ($self, $x, $y, $w, $h) = @_;
498 691 );
499 $self->_size_allocate ($x, $y, $w, $h) or return;
500 692
501 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 693 $self
502} 694}
503 695
504sub _draw { 696sub _draw {
505 my ($self) = @_; 697 my ($self) = @_;
506 698
507 my $chld = $self->child;
508
509 my ($w, $h) = $chld->size_request;
510
511 glBegin GL_QUADS;
512 glColor 0, 0, 0;
513 glVertex 0 , 0;
514 glVertex 0 , $h + 4;
515 glVertex $w + 4 , $h + 4;
516 glVertex $w + 4 , 0;
517 glEnd;
518
519 $chld->draw;
520}
521
522#############################################################################
523
524package CFClient::UI::FancyFrame;
525
526our @ISA = CFClient::UI::Bin::;
527
528use SDL::OpenGL;
529
530my @tex =
531 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
532 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
533
534sub size_request {
535 my ($self) = @_;
536
537 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
538
539 my ($w, $h) = $self->SUPER::size_request;
540
541 $h += $tex[1]->{h};
542 $h += $tex[4]->{h};
543 $w += $tex[2]->{w};
544 $w += $tex[3]->{w};
545
546 ($w, $h)
547}
548
549sub size_allocate {
550 my ($self, $x, $y, $w, $h) = @_;
551
552 $self->_size_allocate ($x, $y, $w, $h) or return;
553
554 $h -= $tex[1]{h};
555 $h -= $tex[4]{h};
556 $w -= $tex[2]{w};
557 $w -= $tex[3]{w};
558
559 $h = $h < 0 ? 0 : $h;
560 $w = $w < 0 ? 0 : $w;
561
562 my $child = $self->child;
563
564 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
565}
566
567sub button_down {
568 my ($self, $ev, $x, $y) = @_;
569
570 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
571 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
572
573 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
574 my ($bw, $bh) = ($self->{w}, $self->{h});
575
576 $self->{motion} = sub {
577 my ($ev, $x, $y) = @_;
578
579 ($x, $y) = ($ev->motion_x, $ev->motion_y);
580
581 $self->{user_w} = $bw + $x - $ox;
582 $self->{user_h} = $bh + $y - $oy;
583 $self->update;
584 };
585
586 } elsif ($x >= 0 && $x < $self->{w}
587 && $y >= 0 && $y < $tex[1]{h}) {
588
589 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
590 my ($bx, $by) = ($self->{x}, $self->{y});
591
592 $self->{motion} = sub {
593 my ($ev, $x, $y) = @_;
594
595 ($x, $y) = ($ev->motion_x, $ev->motion_y);
596
597 $self->move ($bx + $x - $ox, $by + $y - $oy);
598 $self->update;
599 };
600 }
601}
602
603sub button_up {
604 my ($self, $ev, $x, $y) = @_;
605
606 delete $self->{motion};
607}
608
609sub mouse_motion {
610 my ($self, $ev, $x, $y) = @_;
611
612 $self->{motion}->($ev, $x, $y) if $self->{motion};
613}
614
615sub _draw {
616 my ($self) = @_;
617
618 my ($w, $h) = ($self->{w}, $self->{h}); 699 my ($w, $h) = ($self->{w}, $self->{h});
619 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
620 700
621 glEnable GL_BLEND; 701 glEnable GL_BLEND;
622 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
623 glEnable GL_TEXTURE_2D; 703 glEnable GL_TEXTURE_2D;
624 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
625 705
626 my $top = $tex[1]; 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,
766 )
767}
768
769sub size_allocate {
770 my ($self, $w, $h) = @_;
771
772 $h -= List::Util::max 0, $self->border * 2;
773 $w -= List::Util::max 0, $self->border * 2;
774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
778 $self->child->configure ($self->border, $self->border, $w, $h);
779}
780
781sub button_down {
782 my ($self, $ev, $x, $y) = @_;
783
784 my $border = $self->border;
785
786 if ($x < $self->{w} && $x >= $self->{w} - $border
787 && $y < $self->{h} && $y >= $self->{h} - $border) {
788
789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
790 my ($bw, $bh) = ($self->{w}, $self->{h});
791
792 $self->{motion} = sub {
793 my ($ev, $x, $y) = @_;
794
795 ($x, $y) = ($ev->{x}, $ev->{y});
796
797 $self->{user_w} = $bw + $x - $ox;
798 $self->{user_h} = $bh + $y - $oy;
799 $self->check_size;
800 };
801
802 } elsif ($x >= 0 && $x < $self->{w}
803 && $y >= 0 && $y < $border) {
804
805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
806 my ($bx, $by) = ($self->{x}, $self->{y});
807
808 $self->{motion} = sub {
809 my ($ev, $x, $y) = @_;
810
811 ($x, $y) = ($ev->{x}, $ev->{y});
812
813 $self->move ($bx + $x - $ox, $by + $y - $oy);
814 $self->update;
815 };
816 }
817}
818
819sub button_up {
820 my ($self, $ev, $x, $y) = @_;
821
822 delete $self->{motion};
823}
824
825sub mouse_motion {
826 my ($self, $ev, $x, $y) = @_;
827
828 $self->{motion}->($ev, $x, $y) if $self->{motion};
829}
830
831sub _draw {
832 my ($self) = @_;
833
834 my ($w, $h ) = ($self->{w}, $self->{h});
835 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
836
837 glEnable GL_BLEND;
838 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
839 glEnable GL_TEXTURE_2D;
840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
841
842 my $border = $self->border;
843
844 glColor @{ $self->{border_bg} };
627 $top->draw_quad (0, 0, $w, $top->{h}); 845 $tex[1]->draw_quad (0, 0, $w, $border);
628 846 $tex[3]->draw_quad (0, $border, $border, $ch);
629 my $left = $tex[3]; 847 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
630 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 848 $tex[4]->draw_quad (0, $h - $border, $w, $border);
631
632 my $right = $tex[2];
633 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
634
635 my $bottom = $tex[4];
636 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
637 849
638 my $bg = $tex[0]; 850 my $bg = $tex[0];
639 851
640 glBindTexture GL_TEXTURE_2D, $bg->{name}; 852 # TODO: repeat texture not scale
641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
642
643 my $rep_x = $cw / $bg->{w}; 853 my $rep_x = $cw / $bg->{w};
644 my $rep_y = $ch / $bg->{h}; 854 my $rep_y = $ch / $bg->{h};
645 855
646 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 856 glColor @{ $self->{bg} };
857
858 $bg->{s} = $rep_x;
859 $bg->{t} = $rep_y;
860 $bg->{wrap_mode} = 1;
861 $bg->draw_quad ($border, $border, $cw, $ch);
647 862
648 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
649 glDisable GL_BLEND; 864 glDisable GL_BLEND;
650 865
866 $self->{title}->draw if $self->{title};
651 $self->child->draw; 867 $self->child->draw;
652
653} 868}
654 869
655############################################################################# 870#############################################################################
656 871
657package CFClient::UI::Table; 872package CFClient::UI::Table;
658 873
659our @ISA = CFClient::UI::Base::; 874our @ISA = CFClient::UI::Base::;
660 875
661use List::Util qw(max sum); 876use List::Util qw(max sum);
662 877
663use SDL::OpenGL; 878use CFClient::OpenGL;
664 879
665sub new { 880sub new {
666 my $class = shift; 881 my $class = shift;
667 882
668 $class->SUPER::new ( 883 $class->SUPER::new (
670 @_ 885 @_
671 ) 886 )
672} 887}
673 888
674sub add { 889sub add {
675 my ($self, $x, $y, $chld) = @_; 890 my ($self, $x, $y, $child) = @_;
676 891
892 $child->set_parent ($self);
677 $self->{children}[$y][$x] = $chld; 893 $self->{children}[$y][$x] = $child;
678 $chld->set_parent ($self);
679 894
680 $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};
681 $self->update; 903 $self->update;
682} 904}
683 905
684sub get_wh { 906sub get_wh {
685 my ($self) = @_; 907 my ($self) = @_;
691 or next; 913 or next;
692 914
693 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
694 my $widget = $row->[$x] 916 my $widget = $row->[$x]
695 or next; 917 or next;
696 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
697 919
698 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
699 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
700 } 922 }
701 } 923 }
713 (sum @$hs), 935 (sum @$hs),
714 ) 936 )
715} 937}
716 938
717sub size_allocate { 939sub size_allocate {
718 my ($self, $x, $y, $w, $h) = @_; 940 my ($self, $w, $h) = @_;
719
720 $self->_size_allocate ($x, $y, $w, $h) or return;
721 941
722 my ($ws, $hs) = $self->get_wh; 942 my ($ws, $hs) = $self->get_wh;
723 943
724 my $req_w = sum @$ws; 944 my $req_w = sum @$ws;
725 my $req_h = sum @$hs; 945 my $req_h = sum @$hs;
730 my $col_expand = (sum @col_expand) || 1; 950 my $col_expand = (sum @col_expand) || 1;
731 951
732 # linearly scale sizes 952 # linearly scale sizes
733 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 953 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
734 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 954 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
955
956 CFClient::UI::harmonize $ws;
957 CFClient::UI::harmonize $hs;
735 958
736 my $y; 959 my $y;
737 960
738 for my $r (0 .. $#{$self->{children}}) { 961 for my $r (0 .. $#{$self->{children}}) {
739 my $row = $self->{children}[$r] 962 my $row = $self->{children}[$r]
744 967
745 for my $c (0 .. $#$row) { 968 for my $c (0 .. $#$row) {
746 my $col_w = $ws->[$c]; 969 my $col_w = $ws->[$c];
747 970
748 if (my $widget = $row->[$c]) { 971 if (my $widget = $row->[$c]) {
749 $widget->size_allocate ($x, $y, $col_w, $row_h); 972 $widget->configure ($x, $y, $col_w, $row_h);
750 } 973 }
751 974
752 $x += $col_w; 975 $x += $col_w;
753 } 976 }
754 977
799 (List::Util::max map $_->[1], @alloc), 1022 (List::Util::max map $_->[1], @alloc),
800 ) 1023 )
801} 1024}
802 1025
803sub size_allocate { 1026sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 1027 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h);
807 1028
808 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
809 1030
810 my $children = $self->{children}; 1031 my $children = $self->{children};
811 1032
812 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
813 1034
814 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
815 1036
816 if ($req_h > $h) { 1037 if ($req_h > $h) {
817 # ah well, not enough space 1038 # ah well, not enough space
827 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1048 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
828 $h[$_] = $alloc_h; 1049 $h[$_] = $alloc_h;
829 } 1050 }
830 } 1051 }
831 1052
1053 CFClient::UI::harmonize \@h;
1054
832 my $y = 0; 1055 my $y = 0;
833 for (0 .. $#$children) { 1056 for (0 .. $#$children) {
834 my $child = $children->[$_]; 1057 my $child = $children->[$_];
835 my $h = $h[$_]; 1058 my $h = $h[$_];
836 $child->size_allocate ($y, 0, $h, $w); 1059 $child->configure ($y, 0, $h, $w);
837 1060
838 $y += $h; 1061 $y += $h;
839 } 1062 }
1063
1064 1
840} 1065}
841 1066
842############################################################################# 1067#############################################################################
843 1068
844package CFClient::UI::VBox; 1069package CFClient::UI::VBox;
857 (List::Util::sum map $_->[1], @alloc), 1082 (List::Util::sum map $_->[1], @alloc),
858 ) 1083 )
859} 1084}
860 1085
861sub size_allocate { 1086sub size_allocate {
862 my ($self, $x, $y, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
863
864 $self->_size_allocate ($x, $y, $w, $h);
865 1088
866 my $children = $self->{children}; 1089 my $children = $self->{children};
867 1090
868 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
869 1092
870 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
871 1094
872 if ($req_h > $h) { 1095 if ($req_h > $h) {
873 # ah well, not enough space 1096 # ah well, not enough space
881 1104
882 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1105 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
883 } 1106 }
884 } 1107 }
885 1108
1109 CFClient::UI::harmonize \@h;
1110
886 my $y = 0; 1111 my $y = 0;
887 for (0 .. $#$children) { 1112 for (0 .. $#$children) {
888 my $child = $children->[$_]; 1113 my $child = $children->[$_];
889 my $h = $h[$_]; 1114 my $h = $h[$_];
890 $child->size_allocate (0, $y, $w, $h); 1115 $child->configure (0, $y, $w, $h);
891 1116
892 $y += $h; 1117 $y += $h;
893 } 1118 }
1119
1120 1
894} 1121}
895 1122
896############################################################################# 1123#############################################################################
897 1124
898package CFClient::UI::Label; 1125package CFClient::UI::Label;
899 1126
900our @ISA = CFClient::UI::Base::; 1127our @ISA = CFClient::UI::Base::;
901 1128
902use SDL::OpenGL; 1129use CFClient::OpenGL;
903 1130
904sub new { 1131sub new {
905 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
906 1133
907 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
908 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
909 height => $::FONTSIZE, 1136 #font => default_font
1137 fontsize => 1,
910 text => "", 1138 text => "",
911 align => -1, 1139 align => -1,
1140 valign => -1,
912 padding => 2, 1141 padding => 2,
913 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
914 %arg 1144 %arg
915 ); 1145 );
916 1146
917 $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};
918 1155
919 $self 1156 $self
920} 1157}
921 1158
922sub escape_text { 1159sub escape {
923 local $_ = $_[1]; 1160 local $_ = $_[1];
924 1161
925 s/&/&amp;/g; 1162 s/&/&amp;/g;
926 s/>/&gt;/g; 1163 s/>/&gt;/g;
927 s/</&lt;/g; 1164 s/</&lt;/g;
930} 1167}
931 1168
932sub set_text { 1169sub set_text {
933 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
934 1171
935 $self->{text} = $text;
936 $self->{layout}->set_markup ($text); 1172 $self->{layout}->set_text ($text);
937 1173
938 delete $self->{texture}; 1174 delete $self->{texture};
939# $self->{w} = $self->{h} = -1; 1175 $self->check_size;
940 $self->update; 1176 $self->update;
941} 1177}
942 1178
943sub get_text { 1179sub set_markup {
944 my ($self, $text) = @_; 1180 my ($self, $markup) = @_;
945 1181
946 $self->{text} 1182 $self->{layout}->set_markup ($markup);
1183
1184 delete $self->{texture};
1185 $self->check_size;
1186 $self->update;
947} 1187}
948 1188
949sub size_request { 1189sub size_request {
950 my ($self) = @_; 1190 my ($self) = @_;
951 1191
952 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
953 $self->{layout}->set_height ($self->{height}); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1195
954 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 }
955 1207
956 ( 1208 (
957 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
958 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
959 ) 1211 )
960} 1212}
961 1213
962sub size_allocate { 1214sub size_allocate {
963 my ($self, $x, $y, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
964
965 $self->_size_allocate ($x, $y, $w, $h) or return;
966 1216
967 delete $self->{texture}; 1217 delete $self->{texture};
968} 1218}
969 1219
970sub update { 1220sub set_fontsize {
971 my ($self) = @_; 1221 my ($self, $fontsize) = @_;
972 1222
1223 $self->{fontsize} = $fontsize;
973 delete $self->{texture}; 1224 delete $self->{texture};
1225 $self->check_size;
974 $self->SUPER::update; 1226 $self->update;
975} 1227}
976 1228
977sub _draw { 1229sub _draw {
978 my ($self) = @_; 1230 my ($self) = @_;
979 1231
980 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
981 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
982 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
983 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
984 }; 1237 };
985 1238
986 glEnable GL_BLEND; 1239 glEnable GL_BLEND;
987 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
988 glEnable GL_TEXTURE_2D; 1241 glEnable GL_TEXTURE_2D;
989 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1242 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
990 1243
991 glColor @{$self->{fg}}; 1244 glColor @{$self->{fg}};
992 1245
993 my $x = 1246 $self->{ox} = int (
994 $self->{align} < 0 ? $self->{padding} 1247 $self->{align} < 0 ? $self->{padding}
995 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1248 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
996 : ($self->{w} - $tex->{w}) * 0.5; 1249 : ($self->{w} - $tex->{w}) * 0.5
1250 );
997 1251
998 glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0; 1252 $self->{oy} = int (
999 $tex->draw_quad (0, 0); 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});
1000 1259
1001 glDisable GL_TEXTURE_2D; 1260 glDisable GL_TEXTURE_2D;
1002 glDisable GL_BLEND; 1261 glDisable GL_BLEND;
1003} 1262}
1004 1263
1005############################################################################# 1264#############################################################################
1006 1265
1007package CFClient::UI::Entry; 1266package CFClient::UI::EntryBase;
1008 1267
1009our @ISA = CFClient::UI::Label::; 1268our @ISA = CFClient::UI::Label::;
1010 1269
1011use SDL; 1270use CFClient::OpenGL;
1012use SDL::OpenGL;
1013 1271
1014sub new { 1272sub new {
1015 my $class = shift; 1273 my $class = shift;
1016 1274
1017 $class->SUPER::new ( 1275 $class->SUPER::new (
1018 fg => [1, 1, 1], 1276 fg => [1, 1, 1],
1019 bg => [0, 0, 0, 0.2], 1277 bg => [0, 0, 0, 0.2],
1020 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1021 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1280 can_hover => 1,
1281 can_focus => 1,
1282 valign => 0,
1283 can_events => 1,
1022 @_ 1284 @_
1023 ) 1285 )
1024} 1286}
1025 1287
1026sub _set_text { 1288sub _set_text {
1027 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1028 1290
1291 delete $self->{cur_h};
1292
1293 return if $self->{text} eq $text;
1294
1295 delete $self->{texture};
1296
1029 $self->{last_activity} = $::NOW; 1297 $self->{last_activity} = $::NOW;
1030
1031 $self->{text} = $text; 1298 $self->{text} = $text;
1032 $self->{layout}->set_width ($self->{w});
1033 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
1034 1299
1035 $text =~ s/./*/g if $self->{hidden}; 1300 $text =~ s/./*/g if $self->{hidden};
1301 $self->{layout}->set_text ("$text ");
1036 1302
1037 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1303 $self->emit (changed => $self->{text});
1304}
1038 1305
1039 $text = substr $text, 0, $self->{cursor}; 1306sub get_text {
1040 utf8::encode $text; 1307 $_[0]{text}
1041
1042 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1043} 1308}
1044 1309
1045sub size_request { 1310sub size_request {
1046 my ($self) = @_; 1311 my ($self) = @_;
1047 1312
1049 1314
1050 ($w + 1, $h) # add 1 for cursor 1315 ($w + 1, $h) # add 1 for cursor
1051} 1316}
1052 1317
1053sub size_allocate { 1318sub size_allocate {
1054 my ($self, $x, $y, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1055 1320
1056 $self->SUPER::size_allocate ($x, $y, $w, $h); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1057
1058 $self->_set_text ($self->{text});
1059} 1322}
1060 1323
1061sub set_text { 1324sub set_text {
1062 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1063 1326
1067} 1330}
1068 1331
1069sub key_down { 1332sub key_down {
1070 my ($self, $ev) = @_; 1333 my ($self, $ev) = @_;
1071 1334
1072 my $mod = $ev->key_mod; 1335 my $mod = $ev->{mod};
1073 my $sym = $ev->key_sym; 1336 my $sym = $ev->{sym};
1074
1075 my $uni = $ev->key_unicode; 1337 my $uni = $ev->{unicode};
1076 1338
1077 my $text = $self->get_text; 1339 my $text = $self->get_text;
1078 1340
1079 if ($sym == SDLK_BACKSPACE) { 1341 if ($sym == 8) {
1080 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1342 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1081 } elsif ($sym == SDLK_DELETE) { 1343 } elsif ($sym == 127) {
1082 substr $text, $self->{cursor}, 1, ""; 1344 substr $text, $self->{cursor}, 1, "";
1083 } elsif ($sym == SDLK_LEFT) { 1345 } elsif ($sym == CFClient::SDLK_LEFT) {
1084 --$self->{cursor} if $self->{cursor}; 1346 --$self->{cursor} if $self->{cursor};
1085 } elsif ($sym == SDLK_RIGHT) { 1347 } elsif ($sym == CFClient::SDLK_RIGHT) {
1086 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1348 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1087 } elsif ($sym == SDLK_HOME) { 1349 } elsif ($sym == CFClient::SDLK_HOME) {
1088 $self->{cursor} = 0; 1350 $self->{cursor} = 0;
1089 } elsif ($sym == SDLK_END) { 1351 } elsif ($sym == CFClient::SDLK_END) {
1090 $self->{cursor} = length $text; 1352 $self->{cursor} = length $text;
1353 } elsif ($sym == 27) {
1354 $self->emit ('escape');
1091 } elsif ($uni) { 1355 } elsif ($uni) {
1092 substr $text, $self->{cursor}++, 0, chr $uni; 1356 substr $text, $self->{cursor}++, 0, chr $uni;
1093 } 1357 }
1094 1358
1095 $self->_set_text ($text); 1359 $self->_set_text ($text);
1149 1413
1150 $self->SUPER::_draw; 1414 $self->SUPER::_draw;
1151 1415
1152 #TODO: force update every cursor change :( 1416 #TODO: force update every cursor change :(
1153 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
1154 glColor @{$self->{fg}}; 1426 glColor @{$self->{fg}};
1155 glBegin GL_LINES; 1427 glBegin GL_LINES;
1156 glVertex $self->{cur_x}, $self->{cur_y};
1157 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};
1158 glEnd; 1430 glEnd;
1159 } 1431 }
1160} 1432}
1161 1433
1434package CFClient::UI::Entry;
1435
1436our @ISA = CFClient::UI::EntryBase::;
1437
1438use CFClient::OpenGL;
1439
1440sub key_down {
1441 my ($self, $ev) = @_;
1442
1443 my $sym = $ev->{sym};
1444
1445 if ($sym == 13) {
1446 $self->emit (activate => $self->get_text);
1447 $self->update;
1448
1449 } else {
1450 $self->SUPER::key_down ($ev);
1451 }
1452
1453}
1454
1162############################################################################# 1455#############################################################################
1163 1456
1164package CFClient::UI::Button; 1457package CFClient::UI::Button;
1165 1458
1166our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1167 1460
1168use SDL; 1461use CFClient::OpenGL;
1169use SDL::OpenGL;
1170 1462
1171my @tex = 1463my @tex =
1172 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1173 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1174 1466
1175sub new { 1467sub new {
1176 my $class = shift; 1468 my $class = shift;
1177 1469
1178 $class->SUPER::new ( 1470 $class->SUPER::new (
1179 padding => 4, 1471 padding => 4,
1180 fg => [1, 1, 1], 1472 fg => [1, 1, 1],
1181 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1182 active_fg => [1, 1, 0],
1183# active_bg => [0, 0, 0, 0.5], 1474 active_fg => [0, 0, 1],
1184# border_fg => [1, 1, 0], 1475 can_hover => 1,
1476 align => 0,
1477 valign => 0,
1478 can_events => 1,
1185 @_ 1479 @_
1186 ) 1480 )
1187} 1481}
1188 1482
1189sub button_up { 1483sub button_up {
1197 1491
1198sub _draw { 1492sub _draw {
1199 my ($self) = @_; 1493 my ($self) = @_;
1200 1494
1201 local $self->{fg} = $self->{fg}; 1495 local $self->{fg} = $self->{fg};
1202 my $tex = $tex[0];
1203
1204 glEnable GL_BLEND;
1205 glEnable GL_TEXTURE_2D;
1206 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1207 1496
1208 if ($GRAB == $self) { 1497 if ($GRAB == $self) {
1209 $self->{fg} = $self->{active_fg}; 1498 $self->{fg} = $self->{active_fg};
1210 } 1499 }
1211 1500
1212 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;
1213 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;
1214 1506
1215 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1507 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1216 1508
1217 glDisable GL_TEXTURE_2D; 1509 glDisable GL_TEXTURE_2D;
1218 glDisable GL_BLEND; 1510 glDisable GL_BLEND;
1219 1511
1220 $self->SUPER::_draw; 1512 $self->SUPER::_draw;
1224 1516
1225package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1226 1518
1227our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1228 1520
1229use SDL; 1521my @tex =
1230use SDL::OpenGL; 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1524
1525use CFClient::OpenGL;
1231 1526
1232sub new { 1527sub new {
1233 my $class = shift; 1528 my $class = shift;
1234 1529
1235 $class->SUPER::new ( 1530 $class->SUPER::new (
1236 padding => 2, 1531 padding => 2,
1237 fg => [1, 1, 1], 1532 fg => [1, 1, 1],
1238 active_fg => [1, 1, 0], 1533 active_fg => [1, 1, 0],
1239 state => 0, 1534 state => 0,
1535 can_hover => 1,
1240 @_ 1536 @_
1241 ) 1537 )
1242} 1538}
1243 1539
1244sub size_request { 1540sub size_request {
1245 my ($self) = @_; 1541 my ($self) = @_;
1246 1542
1247 ($self->{padding} * 2 + 6) x 2 1543 ($self->{padding} * 2 + 6) x 2
1248}
1249
1250sub size_allocate {
1251 my ($self, $x, $y, $w, $h) = @_;
1252
1253 $self->_size_allocate ($x, $y, $w, $h);
1254} 1544}
1255 1545
1256sub button_down { 1546sub button_down {
1257 my ($self, $ev, $x, $y) = @_; 1547 my ($self, $ev, $x, $y) = @_;
1258 1548
1269 $self->SUPER::_draw; 1559 $self->SUPER::_draw;
1270 1560
1271 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1561 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1272 1562
1273 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1563 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1274 1564
1275 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1565 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1276 1566
1277 glBegin GL_LINE_LOOP; 1567 glEnable GL_BLEND;
1278 glVertex 0 , 0; 1568 glEnable GL_TEXTURE_2D;
1279 glVertex 0 , $s; 1569 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1280 glVertex $s, $s;
1281 glVertex $s, 0;
1282 glEnd;
1283 1570
1284 if ($self->{state}) { 1571 my $tex = $self->{state} ? $tex[1] : $tex[0];
1285 glBegin GL_LINES;
1286 glVertex 0 , 0;
1287 glVertex $s, $s;
1288 glVertex $s, 0;
1289 glVertex 0 , $s;
1290 glEnd;
1291 }
1292}
1293 1572
1294############################################################################# 1573 $tex->draw_quad (0, 0, $s, $s);
1295 1574
1575 glDisable GL_TEXTURE_2D;
1576 glDisable GL_BLEND;
1577}
1578
1579#############################################################################
1580
1296package CFClient::UI::Slider; 1581package CFClient::UI::Image;
1297 1582
1298use strict;
1299
1300use SDL::OpenGL;
1301
1302our @ISA = CFClient::UI::DrawBG::; 1583our @ISA = CFClient::UI::Base::;
1584
1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1587
1588our %loaded_images;
1303 1589
1304sub new { 1590sub new {
1305 my $class = shift; 1591 my $class = shift;
1306 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
1817package CFClient::UI::Slider;
1818
1819use strict;
1820
1821use CFClient::OpenGL;
1822
1823our @ISA = CFClient::UI::DrawBG::;
1824
1825my @tex =
1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1827 qw(s1_slider.png s1_slider_bg.png);
1828
1829sub new {
1830 my $class = shift;
1831
1307 # range [value, low, high, page] 1832 # range [value, low, high, page]
1308 1833
1834 # TODO: 0-width page
1835 # TODO: req_w/h are wrong with vertical
1836 # TODO: calculations are off
1309 my $self = $class->SUPER::new ( 1837 my $self = $class->SUPER::new (
1310 fg => [1, 1, 1], 1838 fg => [1, 1, 1],
1311 active_fg => [0, 0, 0], 1839 active_fg => [0, 0, 0],
1312 range => [0, 0, 100, 10], 1840 range => [0, 0, 100, 10],
1313 req_w => 40, 1841 req_w => 20,
1314 req_h => 10, 1842 req_h => 20,
1315 vertical => 0, 1843 vertical => 0,
1844 can_hover => 1,
1845 inner_pad => 5,
1316 @_ 1846 @_
1317 ); 1847 );
1318 1848
1319 $self 1849 $self
1320} 1850}
1341 if ($GRAB == $self) { 1871 if ($GRAB == $self) {
1342 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1872 my ($value, $lo, $hi, $page) = @{$self->{range}};
1343 1873
1344 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1874 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1345 1875
1876 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1877 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1878
1879 $x -= $inner_pad_px; # substract the padding
1346 $x = $x * ($hi - $lo) / $w + $lo; 1880 $x = $x * ($hi - $lo) / $inner_w + $lo;
1347 $x = $lo if $x < $lo; 1881 $x = $lo if $x < $lo;
1348 $x = $hi - $page if $x > $hi - $page; 1882 $x = $hi - $page if $x > $hi - $page;
1349 $self->{range}[0] = $x; 1883 $self->{range}[0] = $x;
1350 1884
1351 $self->emit (changed => $x); 1885 $self->emit (changed => $x);
1352 $self->update; 1886 $self->update;
1353 } 1887 }
1888}
1889
1890# the inner_* stuff is for generating a padding for the slider handle,
1891# so that the handle doesn't leave the texture. This calculation isn't 100%
1892# correct propably, but it does the job for now
1893sub _calc_inner_pad_px {
1894 my ($self, $w) = @_;
1895 ($w / 100) * $self->{inner_pad} # % to pixels
1354} 1896}
1355 1897
1356sub _draw { 1898sub _draw {
1357 my ($self) = @_; 1899 my ($self) = @_;
1358 1900
1372 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1914 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1373 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1915 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1374 1916
1375 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1917 my ($value, $lo, $hi, $page) = @{$self->{range}};
1376 1918
1919 $hi = $value + 1 if $lo == $hi;
1920
1921 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1922 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1923
1377 $page = int $page * $w / ($hi - $lo); 1924 $page = int $page * $inner_w / ($hi - $lo);
1378 $value = int +($value - $lo) * $w / ($hi - $lo); 1925 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1379 1926
1380 $w -= $page; 1927 $w -= $page;
1381 $page &= ~1; 1928 $page &= ~1;
1382 glTranslate $page * 0.5, 0, 0; 1929 glTranslate $page * 0.5, 0, 0;
1383 $page ||= 2; 1930 $page ||= 2;
1384 1931
1385 glColor @$fg;
1386 glBegin GL_LINES;
1387 glVertex 0, 0; glVertex 0, $h;
1388 glVertex $w - 1, 0; glVertex $w - 1, $h;
1389 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1390 glEnd;
1391
1392 my $knob_a = $value - $page * 0.5; 1932 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1393 my $knob_b = $value + $page * 0.5; 1933 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1394 1934
1395 glBegin GL_QUADS; 1935 glEnable GL_BLEND;
1396 glColor @$fg; 1936 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1397 glVertex $knob_a, 0; 1937 glEnable GL_TEXTURE_2D;
1398 glVertex $knob_a, $h; 1938 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1399 glVertex $knob_b, $h;
1400 glVertex $knob_b, 0;
1401 1939
1402 if ($knob_a < $knob_b - 2) { 1940 # draw background
1403 glColor @$bg; 1941 $tex[1]->draw_quad (0, 0, $w, $h);
1404 glVertex $knob_a + 1, 1;
1405 glVertex $knob_a + 1, $h - 1;
1406 glVertex $knob_b - 1, $h - 1;
1407 glVertex $knob_b - 1, 1;
1408 }
1409 glEnd;
1410}
1411 1942
1412############################################################################# 1943 # draw handle
1944 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1413 1945
1946 glDisable GL_BLEND;
1947 glDisable GL_TEXTURE_2D;
1948}
1949
1950#############################################################################
1951
1414package CFClient::UI::MapWidget; 1952package CFClient::UI::TextView;
1415 1953
1416use strict;
1417
1418use List::Util qw(min max);
1419
1420use SDL;
1421use SDL::OpenGL;
1422
1423our @ISA = CFClient::UI::Base::; 1954our @ISA = CFClient::UI::HBox::;
1955
1956use CFClient::OpenGL;
1424 1957
1425sub new { 1958sub new {
1426 my $class = shift; 1959 my $class = shift;
1427 1960
1428 $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1429 z => -1, 1962 fontsize => 1,
1430 list => (glGenLists 1), 1963 #font => default_font
1431 @_ 1964 @_,
1965
1966 layout => (new CFClient::Layout),
1967 par => [],
1968 height => 0,
1969 children => [
1970 (new CFClient::UI::Empty expand => 1),
1971 (new CFClient::UI::Slider vertical => 1),
1972 ],
1432 ) 1973 );
1433}
1434 1974
1435sub key_down { 1975 $self->{children}[1]->connect (changed => sub {
1436 print "MAPKEYDOWN\n"; 1976 $self->update;
1437} 1977 });
1438 1978
1439sub key_up { 1979 $self
1440} 1980}
1441 1981
1442sub size_request { 1982sub set_fontsize {
1983 my ($self, $fontsize) = @_;
1984
1985 $self->{fontsize} = $fontsize;
1986 $self->reflow;
1987}
1988
1989sub text_height {
1990 my ($self, $text) = @_;
1991
1992 my $layout = $self->{layout};
1993
1994 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1995 $layout->set_width ($self->{w});
1996 $layout->set_text ($text);
1443 ( 1997
1444 1 + 32 * int $::WIDTH / 32, 1998 ($layout->size)[1]
1445 1 + 32 * int $::HEIGHT / 32, 1999}
1446 ) 2000
2001sub reflow {
2002 my ($self) = @_;
2003
2004 $self->{need_reflow}++;
2005 $self->update;
2006}
2007
2008sub size_allocate {
2009 my ($self, $w, $h) = @_;
2010
2011 $self->SUPER::size_allocate ($w, $h);
2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2015 $self->{layout}->set_width ($self->{children}[0]{w});
2016
2017 $self->reflow;
2018}
2019
2020sub add_paragraph {
2021 my ($self, $color, $text) = @_;
2022
2023 #TODO: intelligently "reformat" paragraph
2024
2025 my $height = $self->text_height ($text);
2026
2027 $self->{height} += $height;
2028
2029 push @{$self->{par}}, [$height, $color, $text];
2030
2031 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2032 $self->{children}[1]->update;
1447} 2033}
1448 2034
1449sub update { 2035sub update {
1450 my ($self) = @_; 2036 my ($self) = @_;
1451 2037
1452 $self->{need_update} = 1;
1453 $self->SUPER::update; 2038 $self->SUPER::update;
1454}
1455 2039
1456sub draw { 2040 return unless $self->{h} > 0;
1457 my ($self) = @_;
1458 2041
1459 $self->{need_update}++;#d# 2042 delete $self->{texture};
2043
2044 $ROOT->on_refresh ($self, sub {
1460 if (delete $self->{need_update}) { 2045 if (delete $self->{need_reflow}) {
1461 glNewList $self->{list}, GL_COMPILE; 2046 my $height = 0;
1462 2047
1463 my $sw = int $::WIDTH / 32; 2048 $height += $_->[0] = $self->text_height ($_->[2])
1464 my $sh = int $::HEIGHT / 32; 2049 for @{$self->{par}};
1465 2050
1466 if ($::MAP) { 2051 $self->{height} = $height;
1467 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1468 2052
1469 if ($::CFG->{fow_enable}) { 2053 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1470 if ($::CFG->{fow_smooth}) { # smooth fog of war 2054
1471 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; 2055 delete $self->{texture};
1472 glConvolutionFilter2D 2056 }
1473 GL_CONVOLUTION_2D, 2057
1474 GL_ALPHA, 2058 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1475 3, 3, 2059 glClearColor 0, 0, 0, 1;
1476 GL_ALPHA, GL_FLOAT, 2060 glClear GL_COLOR_BUFFER_BIT;
1477 pack "f*", 2061
1478 0.1, 0.1, 0.1, 2062 glEnable GL_BLEND;
1479 0.1, 0.2, 0.1, 2063 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1480 0.1, 0.1, 0.1, 2064 glEnable GL_TEXTURE_2D;
1481 ; 2065 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1482 glEnable GL_CONVOLUTION_2D; 2066
2067 my $top = int $self->{children}[1]{range}[0];
2068
2069 my $y0 = $top;
2070 my $y1 = $top + $self->{h};
2071
2072 my $y = 0;
2073
2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
2077
2078 for my $par (@{$self->{par}}) {
2079 my $h = $par->[0];
2080
2081 if ($y0 < $y + $h && $y < $y1) {
2082 $layout->set_text ($par->[2]);
2083
2084 glColor @{ $par->[1] };
2085 my ($W, $H) = $layout->size;
2086 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1483 } 2087 }
1484 2088
1485 my $tex = new CFClient::Texture 2089 $y += $h;
1486 w => $w,
1487 h => $h,
1488 data => $data,
1489 internalformat => GL_ALPHA,
1490 format => GL_ALPHA;
1491
1492 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1493
1494 glEnable GL_BLEND;
1495 glEnable GL_TEXTURE_2D;
1496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1497
1498 glColor +($::CFG->{fow_intensity}) x 3, 1;
1499 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1500
1501 glDisable GL_TEXTURE_2D;
1502 glDisable GL_BLEND;
1503 } 2090 }
1504 }
1505 2091
1506 glEndList; 2092 glDisable GL_TEXTURE_2D;
1507 }
1508
1509 glCallList $self->{list};
1510
1511 if ($FOCUS != $self) {
1512 glEnable GL_BLEND;
1513 glColor 0, 0, 1, 0.4;
1514 glBegin GL_QUADS;
1515 glVertex 0, 0;
1516 glVertex 0, $::HEIGHT;
1517 glVertex $::WIDTH, $::HEIGHT;
1518 glVertex $::WIDTH, 0;
1519 glEnd;
1520 glDisable GL_BLEND; 2093 glDisable GL_BLEND;
1521 }
1522}
1523
1524my %DIR = (
1525 SDLK_KP8, [1, "north"],
1526 SDLK_KP9, [2, "northeast"],
1527 SDLK_KP6, [3, "east"],
1528 SDLK_KP3, [4, "southeast"],
1529 SDLK_KP2, [5, "south"],
1530 SDLK_KP1, [6, "southwest"],
1531 SDLK_KP4, [7, "west"],
1532 SDLK_KP7, [8, "northwest"],
1533
1534 SDLK_UP, [1, "north"],
1535 SDLK_RIGHT, [3, "east"],
1536 SDLK_DOWN, [5, "south"],
1537 SDLK_LEFT, [7, "west"],
1538);
1539
1540sub key_down {
1541 my ($self, $ev) = @_;
1542
1543 my $mod = $ev->key_mod;
1544 my $sym = $ev->key_sym;
1545
1546 if ($sym == SDLK_KP5) {
1547 $::CONN->user_send ("command stay fire");
1548 } elsif ($sym == SDLK_a) {
1549 $::CONN->user_send ("command apply");
1550 } elsif (exists $DIR{$sym}) {
1551 if ($mod & KMOD_SHIFT) {
1552 $self->{shft}++;
1553 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1554 } elsif ($mod & KMOD_CTRL) {
1555 $self->{ctrl}++;
1556 $::CONN->user_send ("command run $DIR{$sym}[0]");
1557 } else {
1558 $::CONN->user_send ("command $DIR{$sym}[1]");
1559 } 2094 };
1560 } 2095 });
1561} 2096}
1562 2097
1563sub key_up { 2098sub _draw {
1564 my ($self, $ev) = @_; 2099 my ($self) = @_;
1565 2100
1566 my $mod = $ev->key_mod; 2101 if ($self->{texture}) {
1567 my $sym = $ev->key_sym; 2102 glEnable GL_TEXTURE_2D;
1568 2103 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1569 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 2104 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1570 $::CONN->user_send ("command fire_stop"); 2105 glDisable GL_TEXTURE_2D;
1571 } 2106 }
1572 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 2107
1573 $::CONN->user_send ("command run_stop"); 2108 $self->{children}[1]->draw;
1574 } 2109
1575} 2110}
1576 2111
1577############################################################################# 2112#############################################################################
1578 2113
1579package CFClient::UI::Animator; 2114package CFClient::UI::Animator;
1580 2115
1581use SDL::OpenGL; 2116use CFClient::OpenGL;
1582 2117
1583our @ISA = CFClient::UI::Bin::; 2118our @ISA = CFClient::UI::Bin::;
1584 2119
1585sub moveto { 2120sub moveto {
1586 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
1616 glPopMatrix; 2151 glPopMatrix;
1617} 2152}
1618 2153
1619############################################################################# 2154#############################################################################
1620 2155
1621package CFClient::UI::Toplevel; 2156package CFClient::UI::Flopper;
2157
2158our @ISA = CFClient::UI::Button::;
2159
2160sub new {
2161 my $class = shift;
2162
2163 my $self = $class->SUPER::new (
2164 state => 0,
2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
2167 @_
2168 );
2169
2170 if ($self->{state}) {
2171 $self->{state} = 0;
2172 $self->toggle_flopper;
2173 }
2174
2175 $self
2176}
2177
2178sub toggle_flopper {
2179 my ($self) = @_;
2180
2181 # TODO: use animation
2182 if ($self->{state} = !$self->{state}) {
2183 $CFClient::UI::ROOT->add ($self->{other});
2184 $self->{other}->move ($self->coord2global (0, $self->{h}));
2185 $self->emit ("open");
2186 } else {
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;
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
2305package CFClient::UI::Root;
1622 2306
1623our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
2308
2309use CFClient::OpenGL;
2310
2311sub check_size {
2312 my ($self) = @_;
2313
2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2315}
1624 2316
1625sub size_request { 2317sub size_request {
1626 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
1627} 2319}
1628 2320
1629sub size_allocate { 2321sub configure {
1630 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
1631 2323
1632 $self->_size_allocate ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
1633 2325
1634 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1635 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
1636} 2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1637 2328
1638sub 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 {
1639 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
1640 2337
1641 ($x, $y) 2338 ($x, $y)
1642} 2339}
1643 2340
1644sub update { 2341sub update {
1645 my ($self) = @_; 2342 my ($self) = @_;
1646 2343
1647 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2344 $self->check_size;
1648 ::refresh (); 2345 ::refresh ();
1649} 2346}
1650 2347
1651sub add { 2348sub add {
1652 my ($self, $widget) = @_; 2349 my ($self, $child) = @_;
1653 2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
2354
1654 $self->SUPER::add ($widget); 2355 $self->SUPER::add ($child);
2356}
1655 2357
1656 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2358sub on_refresh {
2359 my ($self, $id, $cb) = @_;
2360
2361 $self->{refresh_hook}{$id} = $cb;
1657} 2362}
1658 2363
1659sub draw { 2364sub draw {
1660 my ($self) = @_; 2365 my ($self) = @_;
1661 2366
2367 while (my $rcb = delete $self->{refresh_hook}) {
2368 $_->() for values %$rcb;
2369 }
2370
2371 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2372 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2373 glClear GL_COLOR_BUFFER_BIT;
2374
2375 glMatrixMode GL_PROJECTION;
2376 glLoadIdentity;
2377 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2378 glMatrixMode GL_MODELVIEW;
2379 glLoadIdentity;
2380
1662 $self->_draw; 2381 $self->_draw;
1663} 2382}
1664 2383
1665############################################################################# 2384#############################################################################
1666 2385
1667package CFClient::UI; 2386package CFClient::UI;
1668 2387
2388$ROOT = new CFClient::UI::Root;
1669$TOPLEVEL = new CFClient::UI::Toplevel; 2389$TOOLTIP = new CFClient::UI::Tooltip;
1670 2390
16711 23911
1672 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines