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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines