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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines