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.112 by root, Sat Apr 15 12:29:46 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 }
71} 115}
72 116
73# convert position array to integers 117# convert position array to integers
74sub harmonize { 118sub harmonize {
75 my ($vals) = @_; 119 my ($vals) = @_;
76 120
77 my $rem = 0; 121 my $rem = 0;
78 122
79 for ($vals) { 123 for (@$vals) {
80 my $i = int $_ + $rem; 124 my $i = int $_ + $rem;
81 $rem += $_ - $i; 125 $rem += $_ - $i;
82 $_ = $i; 126 $_ = $i;
83 } 127 }
84} 128}
87 131
88package CFClient::UI::Base; 132package CFClient::UI::Base;
89 133
90use strict; 134use strict;
91 135
92use SDL::OpenGL; 136use CFClient::OpenGL;
93 137
94sub new { 138sub new {
95 my $class = shift; 139 my $class = shift;
96 140
97 my $self = bless { 141 my $self = bless {
98 x => 0, 142 x => 0,
99 y => 0, 143 y => 0,
100 z => 0, 144 z => 0,
101 w => -1, 145 can_events => 1,
102 h => -1,
103 @_ 146 @_
104 }, $class; 147 }, $class;
105 148
106 for (keys %$self) { 149 for (keys %$self) {
107 if (/^connect_(.*)$/) { 150 if (/^connect_(.*)$/) {
110 } 153 }
111 154
112 $self 155 $self
113} 156}
114 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
115sub move { 183sub move {
116 my ($self, $x, $y, $z) = @_; 184 my ($self, $x, $y, $z) = @_;
185
117 $self->{x} = $x; 186 $self->{x} = int $x;
118 $self->{y} = $y; 187 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 188 $self->{z} = $z if defined $z;
120}
121 189
122sub needs_redraw { 190 $self->update;
123 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;
124} 200}
125 201
126sub size_request { 202sub size_request {
127 require Carp; 203 require Carp;
128 Carp::confess "size_request is abtract"; 204 Carp::confess "size_request is abstract";
129} 205}
130 206
131sub _size_allocate { 207sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 208 my ($self, $x, $y, $w, $h) = @_;
133 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) {
134 $self->{x} = $x; 223 $self->{x} = $x;
135 $self->{y} = $y; 224 $self->{y} = $y;
225 $self->update;
226 }
136 227
137 return unless $self->{w} != $w || $self->{h} != $h; 228 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 229 $self->{w} = $w;
140 $self->{h} = $h; 230 $self->{h} = $h;
141 231
232 $self->size_allocate ($w, $h);
233 $self->update;
142 1 234 }
143} 235}
144 236
145sub size_allocate { 237sub size_allocate {
238 # nothing to be done
239}
240
241sub children {
242}
243
244sub set_max_size {
146 my ($self, $x, $y, $w, $h) = @_; 245 my ($self, $w, $h) = @_;
147 246
148 $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});
149} 259}
150 260
151# translate global coordinates to local coordinate system 261# translate global coordinates to local coordinate system
152sub translate { 262sub coord2local {
153 my ($self, $x, $y) = @_; 263 my ($self, $x, $y) = @_;
154 264
155 $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)
156} 275}
157 276
158sub focus_in { 277sub focus_in {
159 my ($self) = @_; 278 my ($self) = @_;
160 279
161 return if $FOCUS == $self; 280 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 281 return unless $self->{can_focus};
163 282
164 my $focus = $FOCUS; $FOCUS = $self; 283 my $focus = $FOCUS; $FOCUS = $self;
284
285 $self->emit (focus_in => $focus);
286
165 $focus->update if $focus; 287 $focus->update if $focus;
166 $FOCUS->update; 288 $FOCUS->update;
167} 289}
168 290
169sub focus_out { 291sub focus_out {
170 my ($self) = @_; 292 my ($self) = @_;
171 293
172 return unless $FOCUS == $self; 294 return unless $FOCUS == $self;
173 295
174 my $focus = $FOCUS; undef $FOCUS; 296 my $focus = $FOCUS; undef $FOCUS;
297
298 $self->emit (focus_out => $focus);
299
175 $focus->update if $focus; #? 300 $focus->update if $focus; #?
176} 301}
177 302
178sub mouse_motion { } 303sub mouse_motion { }
179sub button_up { } 304sub button_up { }
203 glPopMatrix; 328 glPopMatrix;
204 329
205 if ($self == $HOVER && $self->{can_hover}) { 330 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 331 my ($x, $y) = @$self{qw(x y)};
207 332
208 glColor 0, 0, 1, 0.2; 333 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 334 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 335 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 336 glBegin GL_QUADS;
212 glVertex $x , $y; 337 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 338 glVertex $x + $self->{w}, $y;
214 glVertex $x + $self->{w}, $y + $self->{h}; 339 glVertex $x + $self->{w}, $y + $self->{h};
215 glVertex $x , $y + $self->{h}; 340 glVertex $x , $y + $self->{h};
216 glEnd; 341 glEnd;
217 glDisable GL_BLEND; 342 glDisable GL_BLEND;
218 } 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 }
219} 358}
220 359
221sub _draw { 360sub _draw {
222 my ($self) = @_; 361 my ($self) = @_;
223 362
224 warn "no draw defined for $self\n"; 363 warn "no draw defined for $self\n";
225} 364}
226 365
227sub find_widget { 366sub find_widget {
228 my ($self, $x, $y) = @_; 367 my ($self, $x, $y) = @_;
368
369 return () unless $self->{can_events};
229 370
230 return $self 371 return $self
231 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 372 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
232 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 373 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
233 374
234 () 375 ()
235} 376}
236 377
237sub set_parent { 378sub set_parent {
238 my ($self, $par) = @_; 379 my ($self, $parent) = @_;
239 380
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 381 Scalar::Util::weaken ($self->{parent} = $parent);
242} 382}
243 383
244sub get_parent { 384sub check_size {
385 my ($self) = @_;
386
245 $_[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 }
246} 408}
247 409
248sub update { 410sub update {
249 my ($self) = @_; 411 my ($self) = @_;
250 412
253} 415}
254 416
255sub connect { 417sub connect {
256 my ($self, $signal, $cb) = @_; 418 my ($self, $signal, $cb) = @_;
257 419
258 push @{ $self->{cb}{$signal} }, $cb; 420 push @{ $self->{signal_cb}{$signal} }, $cb;
259} 421}
260 422
261sub emit { 423sub emit {
262 my ($self, $signal, @args) = @_; 424 my ($self, $signal, @args) = @_;
263 425
264 $_->($self, @args) 426 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
265 for @{$self->{cb}{$signal} || []};
266} 427}
267 428
268sub DESTROY { 429sub DESTROY {
269 my ($self) = @_; 430 my ($self) = @_;
270 431
276package CFClient::UI::DrawBG; 437package CFClient::UI::DrawBG;
277 438
278our @ISA = CFClient::UI::Base::; 439our @ISA = CFClient::UI::Base::;
279 440
280use strict; 441use strict;
281use SDL::OpenGL; 442use CFClient::OpenGL;
282 443
283sub new { 444sub new {
284 my $class = shift; 445 my $class = shift;
285 446
286 # range [value, low, high, page] 447 # range [value, low, high, page]
315 476
316package CFClient::UI::Empty; 477package CFClient::UI::Empty;
317 478
318our @ISA = CFClient::UI::Base::; 479our @ISA = CFClient::UI::Base::;
319 480
481sub new {
482 my ($class, %arg) = @_;
483 $class->SUPER::new (can_events => 0, %arg);
484}
485
320sub size_request { 486sub size_request {
321 (0, 0) 487 (0, 0)
322} 488}
323 489
324sub draw { } 490sub draw { }
332sub new { 498sub new {
333 my ($class, %arg) = @_; 499 my ($class, %arg) = @_;
334 500
335 my $children = delete $arg{children} || []; 501 my $children = delete $arg{children} || [];
336 502
337 my $self = $class->SUPER::new (children => [], %arg); 503 my $self = $class->SUPER::new (
504 children => [],
505 can_events => 0,
506 %arg,
507 );
338 $self->add ($_) for @$children; 508 $self->add ($_) for @$children;
339 509
340 $self 510 $self
341} 511}
342 512
343sub add { 513sub add {
344 my ($self, $chld) = @_; 514 my ($self, $child) = @_;
345 515
346 $chld->set_parent ($self); 516 $child->set_parent ($self);
517
518 use sort 'stable';
347 519
348 $self->{children} = [ 520 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 521 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 522 @{$self->{children}}, $child
351 ]; 523 ];
352 524
353 $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;
354 $self->update; 541 $self->update;
355} 542}
356 543
357sub remove { 544sub clear {
358 my ($self, $widget) = @_; 545 my ($self) = @_;
359 546
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 547 my $children = delete $self->{children};
548 $self->{children} = [];
361 549
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 550 for (@$children) {
551 delete $_->{parent};
552 $_->hide;
553 }
363} 554}
364 555
365sub find_widget { 556sub find_widget {
366 my ($self, $x, $y) = @_; 557 my ($self, $x, $y) = @_;
367 558
397 588
398 $class->SUPER::new (children => [$child], %arg) 589 $class->SUPER::new (children => [$child], %arg)
399} 590}
400 591
401sub add { 592sub add {
402 my ($self, $widget) = @_; 593 my ($self, $child) = @_;
403 594
404 $self->{children} = []; 595 $self->{children} = [];
405 596
406 $self->SUPER::add ($widget); 597 $self->SUPER::add ($child);
407} 598}
408 599
409sub remove { 600sub remove {
410 my ($self, $widget) = @_; 601 my ($self, $widget) = @_;
411 602
420sub size_request { 611sub size_request {
421 $_[0]{children}[0]->size_request 612 $_[0]{children}[0]->size_request
422} 613}
423 614
424sub size_allocate { 615sub size_allocate {
425 my ($self, $x, $y, $w, $h) = @_; 616 my ($self, $w, $h) = @_;
426 617
427 $self->_size_allocate ($x, $y, $w, $h) or return;
428
429 $self->{children}[0]->size_allocate (0, 0, $w, $h); 618 $self->{children}[0]->configure (0, 0, $w, $h);
430} 619}
431 620
432############################################################################# 621#############################################################################
433 622
434package CFClient::UI::Window; 623package CFClient::UI::Window;
435 624
436our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
437 626
438use SDL::OpenGL; 627use CFClient::OpenGL;
439 628
440sub new { 629sub new {
441 my ($class, %arg) = @_; 630 my ($class, %arg) = @_;
442 631
443 my $self = $class->SUPER::new (%arg); 632 my $self = $class->SUPER::new (%arg);
460 $self->child->draw; 649 $self->child->draw;
461 }; 650 };
462} 651}
463 652
464sub size_allocate { 653sub size_allocate {
465 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
466 655
467 $self->_size_allocate ($x, $y, $w, $h) or return;
468
469 $self->child->size_allocate (0, 0, $w, $h); 656 $self->child->configure (0, 0, $w, $h);
470 657
471 $self->render_chld; 658 $self->render_chld;
472} 659}
473 660
474sub _draw { 661sub _draw {
490 glDisable GL_TEXTURE_2D; 677 glDisable GL_TEXTURE_2D;
491} 678}
492 679
493############################################################################# 680#############################################################################
494 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
495package CFClient::UI::Frame; 706package CFClient::UI::Frame;
496 707
497our @ISA = CFClient::UI::Bin::; 708our @ISA = CFClient::UI::Bin::;
498 709
499use SDL::OpenGL; 710use CFClient::OpenGL;
500
501sub size_request {
502 my ($self) = @_;
503 my $chld = $self->child
504 or return (0, 0);
505
506 $chld->move (2, 2);
507
508 map { $_ + 4 } $chld->size_request;
509}
510
511sub size_allocate {
512 my ($self, $x, $y, $w, $h) = @_;
513
514 $self->_size_allocate ($x, $y, $w, $h) or return;
515
516 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
517}
518
519sub _draw {
520 my ($self) = @_;
521
522 my $chld = $self->child;
523
524 my ($w, $h) = $chld->size_request;
525
526 glBegin GL_QUADS;
527 glColor 0, 0, 0;
528 glVertex 0 , 0;
529 glVertex 0 , $h + 4;
530 glVertex $w + 4 , $h + 4;
531 glVertex $w + 4 , 0;
532 glEnd;
533
534 $chld->draw;
535}
536
537#############################################################################
538
539package CFClient::UI::FancyFrame;
540
541our @ISA = CFClient::UI::Bin::;
542
543use SDL::OpenGL;
544
545my @tex =
546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
547 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
548 711
549sub new { 712sub new {
550 my $class = shift; 713 my $class = shift;
551 714
552 # TODO: user_x, user_y, overwrite moveto?
553
554 $class->SUPER::new ( 715 my $self = $class->SUPER::new (
555 bg => [1, 1, 1, 1], 716 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 717 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 718 border => 0.8,
558 @_ 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,
559 ) 795 )
560} 796}
561 797
562sub size_request {
563 my ($self) = @_;
564
565 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
566
567 my ($w, $h) = $self->SUPER::size_request;
568
569 (
570 $w + $self->{border} * 2,
571 $h + $self->{border} * 2,
572 )
573}
574
575sub size_allocate { 798sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 799 my ($self, $w, $h) = @_;
577 800
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $h -= List::Util::max 0, $self->{border} * 2; 801 $h -= List::Util::max 0, $self->border * 2;
581 $w -= List::Util::max 0, $self->{border} * 2; 802 $w -= List::Util::max 0, $self->border * 2;
582 803
804 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
805 if $self->{title};
806
583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 807 $self->child->configure ($self->border, $self->border, $w, $h);
584} 808}
585 809
586sub button_down { 810sub button_down {
587 my ($self, $ev, $x, $y) = @_; 811 my ($self, $ev, $x, $y) = @_;
588 812
813 my $border = $self->border;
814
589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 815 if ($x < $self->{w} && $x >= $self->{w} - $border
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 816 && $y < $self->{h} && $y >= $self->{h} - $border) {
591 817
592 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 818 my ($ox, $oy) = ($ev->{x}, $ev->{y});
593 my ($bw, $bh) = ($self->{w}, $self->{h}); 819 my ($bw, $bh) = ($self->{w}, $self->{h});
594 820
595 $self->{motion} = sub { 821 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_; 822 my ($ev, $x, $y) = @_;
597 823
598 ($x, $y) = ($ev->motion_x, $ev->motion_y); 824 ($x, $y) = ($ev->{x}, $ev->{y});
599 825
600 $self->{user_w} = $bw + $x - $ox; 826 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy; 827 $self->{user_h} = $bh + $y - $oy;
602 $self->update; 828 $self->check_size;
603 }; 829 };
604 830
605 } elsif ($x >= 0 && $x < $self->{w} 831 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) { 832 && $y >= 0 && $y < $border) {
607 833
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 834 my ($ox, $oy) = ($ev->{x}, $ev->{y});
609 my ($bx, $by) = ($self->{x}, $self->{y}); 835 my ($bx, $by) = ($self->{x}, $self->{y});
610 836
611 $self->{motion} = sub { 837 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_; 838 my ($ev, $x, $y) = @_;
613 839
614 ($x, $y) = ($ev->motion_x, $ev->motion_y); 840 ($x, $y) = ($ev->{x}, $ev->{y});
615 841
616 $self->move ($bx + $x - $ox, $by + $y - $oy); 842 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update; 843 $self->update;
618 }; 844 };
619 } 845 }
640 glEnable GL_BLEND; 866 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 867 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 868 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 869 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644 870
871 my $border = $self->border;
872
645 glColor @{ $self->{border_bg} }; 873 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 874 $tex[1]->draw_quad (0, 0, $w, $border);
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 875 $tex[3]->draw_quad (0, $border, $border, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 877 $tex[4]->draw_quad (0, $h - $border, $w, $border);
650 878
651 my $bg = $tex[0]; 879 my $bg = $tex[0];
652 880
653 # TODO: repeat texture not scale 881 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w}; 882 my $rep_x = $cw / $bg->{w};
657 glColor @{ $self->{bg} }; 885 glColor @{ $self->{bg} };
658 886
659 $bg->{s} = $rep_x; 887 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y; 888 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1; 889 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 890 $bg->draw_quad ($border, $border, $cw, $ch);
663 891
664 glDisable GL_TEXTURE_2D; 892 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND; 893 glDisable GL_BLEND;
666 894
895 $self->{title}->draw if $self->{title};
667 $self->child->draw; 896 $self->child->draw;
668} 897}
669 898
670############################################################################# 899#############################################################################
671 900
673 902
674our @ISA = CFClient::UI::Base::; 903our @ISA = CFClient::UI::Base::;
675 904
676use List::Util qw(max sum); 905use List::Util qw(max sum);
677 906
678use SDL::OpenGL; 907use CFClient::OpenGL;
679 908
680sub new { 909sub new {
681 my $class = shift; 910 my $class = shift;
682 911
683 $class->SUPER::new ( 912 $class->SUPER::new (
685 @_ 914 @_
686 ) 915 )
687} 916}
688 917
689sub add { 918sub add {
690 my ($self, $x, $y, $chld) = @_; 919 my ($self, $x, $y, $child) = @_;
691 920
921 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 922 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 923
695 $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
696 $self->update; 943 $self->update;
697} 944}
698 945
699sub get_wh { 946sub get_wh {
700 my ($self) = @_; 947 my ($self) = @_;
706 or next; 953 or next;
707 954
708 for my $x (0 .. $#$row) { 955 for my $x (0 .. $#$row) {
709 my $widget = $row->[$x] 956 my $widget = $row->[$x]
710 or next; 957 or next;
711 my ($w, $h) = $widget->size_request; 958 my ($w, $h) = @$widget{qw(req_w req_h)};
712 959
713 $w[$x] = max $w[$x], $w; 960 $w[$x] = max $w[$x], $w;
714 $h[$y] = max $h[$y], $h; 961 $h[$y] = max $h[$y], $h;
715 } 962 }
716 } 963 }
728 (sum @$hs), 975 (sum @$hs),
729 ) 976 )
730} 977}
731 978
732sub size_allocate { 979sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_; 980 my ($self, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736 981
737 my ($ws, $hs) = $self->get_wh; 982 my ($ws, $hs) = $self->get_wh;
738 983
739 my $req_w = sum @$ws; 984 my $req_w = sum @$ws;
740 my $req_h = sum @$hs; 985 my $req_h = sum @$hs;
762 1007
763 for my $c (0 .. $#$row) { 1008 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c]; 1009 my $col_w = $ws->[$c];
765 1010
766 if (my $widget = $row->[$c]) { 1011 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h); 1012 $widget->configure ($x, $y, $col_w, $row_h);
768 } 1013 }
769 1014
770 $x += $col_w; 1015 $x += $col_w;
771 } 1016 }
772 1017
817 (List::Util::max map $_->[1], @alloc), 1062 (List::Util::max map $_->[1], @alloc),
818 ) 1063 )
819} 1064}
820 1065
821sub size_allocate { 1066sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 1067 my ($self, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825 1068
826 ($h, $w) = ($w, $h); 1069 ($h, $w) = ($w, $h);
827 1070
828 my $children = $self->{children}; 1071 my $children = $self->{children};
829 1072
830 my @h = map +($_->size_request)[0], @$children; 1073 my @h = map $_->{req_w}, @$children;
831 1074
832 my $req_h = List::Util::sum @h; 1075 my $req_h = List::Util::sum @h;
833 1076
834 if ($req_h > $h) { 1077 if ($req_h > $h) {
835 # ah well, not enough space 1078 # ah well, not enough space
851 1094
852 my $y = 0; 1095 my $y = 0;
853 for (0 .. $#$children) { 1096 for (0 .. $#$children) {
854 my $child = $children->[$_]; 1097 my $child = $children->[$_];
855 my $h = $h[$_]; 1098 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 1099 $child->configure ($y, 0, $h, $w);
857 1100
858 $y += $h; 1101 $y += $h;
859 } 1102 }
1103
1104 1
860} 1105}
861 1106
862############################################################################# 1107#############################################################################
863 1108
864package CFClient::UI::VBox; 1109package CFClient::UI::VBox;
877 (List::Util::sum map $_->[1], @alloc), 1122 (List::Util::sum map $_->[1], @alloc),
878 ) 1123 )
879} 1124}
880 1125
881sub size_allocate { 1126sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 1127 my ($self, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885 1128
886 my $children = $self->{children}; 1129 my $children = $self->{children};
887 1130
888 my @h = map +($_->size_request)[1], @$children; 1131 my @h = map $_->{req_h}, @$children;
889 1132
890 my $req_h = List::Util::sum @h; 1133 my $req_h = List::Util::sum @h;
891 1134
892 if ($req_h > $h) { 1135 if ($req_h > $h) {
893 # ah well, not enough space 1136 # ah well, not enough space
907 1150
908 my $y = 0; 1151 my $y = 0;
909 for (0 .. $#$children) { 1152 for (0 .. $#$children) {
910 my $child = $children->[$_]; 1153 my $child = $children->[$_];
911 my $h = $h[$_]; 1154 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 1155 $child->configure (0, $y, $w, $h);
913 1156
914 $y += $h; 1157 $y += $h;
915 } 1158 }
1159
1160 1
916} 1161}
917 1162
918############################################################################# 1163#############################################################################
919 1164
920package CFClient::UI::Label; 1165package CFClient::UI::Label;
921 1166
922our @ISA = CFClient::UI::Base::; 1167our @ISA = CFClient::UI::Base::;
923 1168
924use SDL::OpenGL; 1169use CFClient::OpenGL;
925 1170
926sub new { 1171sub new {
927 my ($class, %arg) = @_; 1172 my ($class, %arg) = @_;
928 1173
929 my $self = $class->SUPER::new ( 1174 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1175 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1176 #font => default_font
1177 fontsize => 1,
932 text => "", 1178 text => "",
933 align => -1, 1179 align => -1,
1180 valign => -1,
934 padding => 2, 1181 padding => 2,
935 layout => new CFClient::Layout, 1182 layout => new CFClient::Layout,
1183 can_events => 0,
936 %arg 1184 %arg
937 ); 1185 );
938 1186
939 $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};
940 1195
941 $self 1196 $self
942} 1197}
943 1198
944sub escape_text { 1199sub escape {
945 local $_ = $_[1]; 1200 local $_ = $_[1];
946 1201
947 s/&/&amp;/g; 1202 s/&/&amp;/g;
948 s/>/&gt;/g; 1203 s/>/&gt;/g;
949 s/</&lt;/g; 1204 s/</&lt;/g;
952} 1207}
953 1208
954sub set_text { 1209sub set_text {
955 my ($self, $text) = @_; 1210 my ($self, $text) = @_;
956 1211
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1212 $self->{layout}->set_text ($text);
959 1213
960 delete $self->{texture}; 1214 delete $self->{texture};
961# $self->{w} = $self->{h} = -1; 1215 $self->check_size;
962 $self->update; 1216 $self->update;
963} 1217}
964 1218
965sub get_text { 1219sub set_markup {
966 my ($self, $text) = @_; 1220 my ($self, $markup) = @_;
967 1221
968 $self->{text} 1222 $self->{layout}->set_markup ($markup);
1223
1224 delete $self->{texture};
1225 $self->check_size;
1226 $self->update;
969} 1227}
970 1228
971sub size_request { 1229sub size_request {
972 my ($self) = @_; 1230 my ($self) = @_;
973 1231
974 $self->{layout}->set_width; 1232 $self->{layout}->set_font ($self->{font}) if $self->{font};
1233 $self->{layout}->set_width ($self->{max_w} || -1);
975 $self->{layout}->set_height ($self->{fontsize}); 1234 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1235
976 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 }
977 1247
978 ( 1248 (
979 $w + $self->{padding} * 2, 1249 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1250 $h + $self->{padding} * 2,
981 ) 1251 )
982} 1252}
983 1253
984sub size_allocate { 1254sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_; 1255 my ($self, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988 1256
989 delete $self->{texture}; 1257 delete $self->{texture};
990} 1258}
991 1259
992sub update { 1260sub set_fontsize {
993 my ($self) = @_; 1261 my ($self, $fontsize) = @_;
994 1262
1263 $self->{fontsize} = $fontsize;
995 delete $self->{texture}; 1264 delete $self->{texture};
1265 $self->check_size;
996 $self->SUPER::update; 1266 $self->update;
997} 1267}
998 1268
999sub _draw { 1269sub _draw {
1000 my ($self) = @_; 1270 my ($self) = @_;
1001 1271
1002 my $tex = $self->{texture} ||= do { 1272 my $tex = $self->{texture} ||= do {
1273 $self->{layout}->set_font ($self->{font}) if $self->{font};
1003 $self->{layout}->set_width ($self->{w}); 1274 $self->{layout}->set_width ($self->{w});
1004 $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);
1005 new_from_layout CFClient::Texture $self->{layout} 1276 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1277 };
1007 1278
1008 glEnable GL_BLEND; 1279 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1280 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1281 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1282 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1283
1013 glColor @{$self->{fg}}; 1284 glColor @{$self->{fg}};
1014 1285
1015 my $x = 1286 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1287 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1288 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1289 : ($self->{w} - $tex->{w}) * 0.5
1290 );
1019 1291
1020 $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});
1021 1299
1022 glDisable GL_TEXTURE_2D; 1300 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1301 glDisable GL_BLEND;
1024} 1302}
1025 1303
1026############################################################################# 1304#############################################################################
1027 1305
1028package CFClient::UI::Entry; 1306package CFClient::UI::EntryBase;
1029 1307
1030our @ISA = CFClient::UI::Label::; 1308our @ISA = CFClient::UI::Label::;
1031 1309
1032use SDL; 1310use CFClient::OpenGL;
1033use SDL::OpenGL;
1034 1311
1035sub new { 1312sub new {
1036 my $class = shift; 1313 my $class = shift;
1037 1314
1038 $class->SUPER::new ( 1315 $class->SUPER::new (
1039 fg => [1, 1, 1], 1316 fg => [1, 1, 1],
1040 bg => [0, 0, 0, 0.2], 1317 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1318 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1319 active_fg => [0, 0, 0],
1043 can_hover => 1, 1320 can_hover => 1,
1044 can_focus => 1, 1321 can_focus => 1,
1322 valign => 0,
1323 can_events => 1,
1045 @_ 1324 @_
1046 ) 1325 )
1047} 1326}
1048 1327
1049sub _set_text { 1328sub _set_text {
1050 my ($self, $text) = @_; 1329 my ($self, $text) = @_;
1051 1330
1052 my $old_text = $self->{text}; 1331 delete $self->{cur_h};
1332
1333 return if $self->{text} eq $text;
1334
1335 delete $self->{texture};
1053 1336
1054 $self->{last_activity} = $::NOW; 1337 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1338 $self->{text} = $text;
1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1059 1339
1060 $text =~ s/./*/g if $self->{hidden}; 1340 $text =~ s/./*/g if $self->{hidden};
1341 $self->{layout}->set_text ("$text ");
1061 1342
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1343 $self->emit (changed => $self->{text});
1344}
1063 1345
1064 $text = substr $text, 0, $self->{cursor}; 1346sub get_text {
1065 utf8::encode $text; 1347 $_[0]{text}
1066
1067 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1068
1069 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1070 if $old_text ne $self->{text};
1071} 1348}
1072 1349
1073sub size_request { 1350sub size_request {
1074 my ($self) = @_; 1351 my ($self) = @_;
1075 1352
1077 1354
1078 ($w + 1, $h) # add 1 for cursor 1355 ($w + 1, $h) # add 1 for cursor
1079} 1356}
1080 1357
1081sub size_allocate { 1358sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1359 my ($self, $w, $h) = @_;
1083 1360
1084 $self->SUPER::size_allocate ($x, $y, $w, $h); 1361 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1085
1086 $self->_set_text ($self->{text});
1087} 1362}
1088 1363
1089sub set_text { 1364sub set_text {
1090 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1091 1366
1095} 1370}
1096 1371
1097sub key_down { 1372sub key_down {
1098 my ($self, $ev) = @_; 1373 my ($self, $ev) = @_;
1099 1374
1100 my $mod = $ev->key_mod; 1375 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1376 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1377 my $uni = $ev->{unicode};
1104 1378
1105 my $text = $self->get_text; 1379 my $text = $self->get_text;
1106 1380
1107 if ($sym == SDLK_BACKSPACE) { 1381 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1382 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1383 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1384 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1385 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1386 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1387 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1388 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1389 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1390 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1391 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1392 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1393 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1394 $self->emit ('escape');
1121 } elsif ($uni) { 1395 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1396 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1397 }
1124 1398
1179 1453
1180 $self->SUPER::_draw; 1454 $self->SUPER::_draw;
1181 1455
1182 #TODO: force update every cursor change :( 1456 #TODO: force update every cursor change :(
1183 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
1184 glColor @{$self->{fg}}; 1466 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1467 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 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};
1188 glEnd; 1470 glEnd;
1189 } 1471 }
1190} 1472}
1191 1473
1192package CFClient::UI::LineEntry; 1474package CFClient::UI::Entry;
1193 1475
1194our @ISA = CFClient::UI::Entry::; 1476our @ISA = CFClient::UI::EntryBase::;
1195 1477
1196use SDL; 1478use CFClient::OpenGL;
1197use SDL::OpenGL;
1198 1479
1199sub key_down { 1480sub key_down {
1200 my ($self, $ev) = @_; 1481 my ($self, $ev) = @_;
1201 1482
1202 my $sym = $ev->key_sym; 1483 my $sym = $ev->{sym};
1203 1484
1204 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} = '';
1205 $self->emit (activate => $self->get_text); 1490 $self->emit (activate => $txt);
1206 $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 }
1207 1514
1208 } else { 1515 } else {
1209 $self->SUPER::key_down ($ev); 1516 $self->SUPER::key_down ($ev);
1210 } 1517 }
1211 1518
1215 1522
1216package CFClient::UI::Button; 1523package CFClient::UI::Button;
1217 1524
1218our @ISA = CFClient::UI::Label::; 1525our @ISA = CFClient::UI::Label::;
1219 1526
1220use SDL; 1527use CFClient::OpenGL;
1221use SDL::OpenGL;
1222 1528
1223my @tex = 1529my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1530 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1225 qw(b1_button_active.png); 1531 qw(b1_button_active.png);
1226 1532
1227sub new { 1533sub new {
1228 my $class = shift; 1534 my $class = shift;
1229 1535
1230 $class->SUPER::new ( 1536 $class->SUPER::new (
1231 padding => 4, 1537 padding => 4,
1232 fg => [1, 1, 1], 1538 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1539 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1540 active_fg => [0, 0, 1],
1235 can_hover => 1, 1541 can_hover => 1,
1542 align => 0,
1543 valign => 0,
1544 can_events => 1,
1236 @_ 1545 @_
1237 ) 1546 )
1238} 1547}
1239 1548
1240sub button_up { 1549sub button_up {
1248 1557
1249sub _draw { 1558sub _draw {
1250 my ($self) = @_; 1559 my ($self) = @_;
1251 1560
1252 local $self->{fg} = $self->{fg}; 1561 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258 1562
1259 if ($GRAB == $self) { 1563 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1564 $self->{fg} = $self->{active_fg};
1261 } 1565 }
1262 1566
1263 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;
1264 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;
1265 1572
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1573 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1574
1268 glDisable GL_TEXTURE_2D; 1575 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1270 1577
1271 $self->SUPER::_draw; 1578 $self->SUPER::_draw;
1276package CFClient::UI::CheckBox; 1583package CFClient::UI::CheckBox;
1277 1584
1278our @ISA = CFClient::UI::DrawBG::; 1585our @ISA = CFClient::UI::DrawBG::;
1279 1586
1280my @tex = 1587my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1588 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1589 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1590
1284use SDL; 1591use CFClient::OpenGL;
1285use SDL::OpenGL;
1286 1592
1287sub new { 1593sub new {
1288 my $class = shift; 1594 my $class = shift;
1289 1595
1290 $class->SUPER::new ( 1596 $class->SUPER::new (
1301 my ($self) = @_; 1607 my ($self) = @_;
1302 1608
1303 ($self->{padding} * 2 + 6) x 2 1609 ($self->{padding} * 2 + 6) x 2
1304} 1610}
1305 1611
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1612sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1613 my ($self, $ev, $x, $y) = @_;
1314 1614
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1615 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1616 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1342 glDisable GL_BLEND; 1642 glDisable GL_BLEND;
1343} 1643}
1344 1644
1345############################################################################# 1645#############################################################################
1346 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
1347package CFClient::UI::Slider; 1883package CFClient::UI::Slider;
1348 1884
1349use strict; 1885use strict;
1350 1886
1351use SDL::OpenGL; 1887use CFClient::OpenGL;
1352 1888
1353our @ISA = CFClient::UI::DrawBG::; 1889our @ISA = CFClient::UI::DrawBG::;
1354 1890
1355my @tex = 1891my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1892 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1366 # TODO: calculations are off 1902 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1904 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1905 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1906 range => [0, 0, 100, 10],
1371 req_w => 40, 1907 req_w => 20,
1372 req_h => 13, 1908 req_h => 20,
1373 vertical => 0, 1909 vertical => 0,
1374 can_hover => 1, 1910 can_hover => 1,
1375 inner_pad => 5, 1911 inner_pad => 5,
1376 @_ 1912 @_
1377 ); 1913 );
1481 2017
1482package CFClient::UI::TextView; 2018package CFClient::UI::TextView;
1483 2019
1484our @ISA = CFClient::UI::HBox::; 2020our @ISA = CFClient::UI::HBox::;
1485 2021
1486use SDL::OpenGL; 2022use CFClient::OpenGL;
1487 2023
1488sub new { 2024sub new {
1489 my $class = shift; 2025 my $class = shift;
1490 2026
1491 my $self = $class->SUPER::new ( 2027 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6, 2028 fontsize => 1,
1493 req_h => $::HEIGHT / 6, 2029 can_events => 0,
1494 fontsize => $::FONTSIZE, 2030 #font => default_font
1495 @_, 2031 @_,
1496 2032
1497 layout => (new CFClient::Layout), 2033 layout => (new CFClient::Layout),
1498 par => [], 2034 par => [],
1499 height => 0, 2035 height => 0,
1500 children => [ 2036 children => [
1501 (new CFClient::UI::Empty expand => 1), 2037 (new CFClient::UI::Empty expand => 1),
1502 (new CFClient::UI::Slider vertical => 1), 2038 (new CFClient::UI::Slider vertical => 1),
1503 ], 2039 ],
1504 ); 2040 );
1505 2041
1520sub text_height { 2056sub text_height {
1521 my ($self, $text) = @_; 2057 my ($self, $text) = @_;
1522 2058
1523 my $layout = $self->{layout}; 2059 my $layout = $self->{layout};
1524 2060
1525 $layout->set_height ($self->{fontsize}); 2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 2062 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 2063 $layout->set_text ($text);
1528 2064
1529 ($layout->size)[1] 2065 ($layout->size)[1]
1530} 2066}
1534 2070
1535 $self->{need_reflow}++; 2071 $self->{need_reflow}++;
1536 $self->update; 2072 $self->update;
1537} 2073}
1538 2074
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 2075sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 2076 my ($self, $w, $h) = @_;
1547 2077
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 2078 $self->SUPER::size_allocate ($w, $h);
1549 2079
2080 $self->{layout}->set_font ($self->{font}) if $self->{font};
1550 $self->{layout}->set_height ($self->{fontsize}); 2081 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 2082 $self->{layout}->set_width ($self->{children}[0]{w});
1552 2083
1553 $self->reflow; 2084 $self->reflow;
1554} 2085}
1555 2086
1556sub add_paragraph { 2087sub add_paragraph {
1575 2106
1576 return unless $self->{h} > 0; 2107 return unless $self->{h} > 0;
1577 2108
1578 delete $self->{texture}; 2109 delete $self->{texture};
1579 2110
1580 $TOPLEVEL->on_refresh ($self, sub { 2111 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 2112 if (delete $self->{need_reflow}) {
1582 my $height = 0; 2113 my $height = 0;
1583 2114
1584 $height += $_->[0] = $self->text_height ($_->[2]) 2115 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 2116 for @{$self->{par}};
1607 2138
1608 my $y = 0; 2139 my $y = 0;
1609 2140
1610 my $layout = $self->{layout}; 2141 my $layout = $self->{layout};
1611 2142
2143 $layout->set_font ($self->{font}) if $self->{font};
2144
1612 for my $par (@{$self->{par}}) { 2145 for my $par (@{$self->{par}}) {
1613 my $h = $par->[0]; 2146 my $h = $par->[0];
1614 2147
1615 if ($y0 < $y + $h && $y < $y1) { 2148 if ($y0 < $y + $h && $y < $y1) {
1616 $layout->set_text ($par->[2]); 2149 $layout->set_text ($par->[2]);
1633 my ($self) = @_; 2166 my ($self) = @_;
1634 2167
1635 if ($self->{texture}) { 2168 if ($self->{texture}) {
1636 glEnable GL_TEXTURE_2D; 2169 glEnable GL_TEXTURE_2D;
1637 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;
1638 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1639 glDisable GL_TEXTURE_2D; 2173 glDisable GL_TEXTURE_2D;
1640 } 2174 }
1641 2175
1642 $self->{children}[1]->draw; 2176 $self->{children}[1]->draw;
1643 2177
1644} 2178}
1645 2179
1646############################################################################# 2180#############################################################################
1647 2181
1648package CFClient::UI::MapWidget;
1649
1650use strict;
1651
1652use List::Util qw(min max);
1653
1654use SDL;
1655use SDL::OpenGL;
1656
1657our @ISA = CFClient::UI::Base::;
1658
1659sub new {
1660 my $class = shift;
1661
1662 $class->SUPER::new (
1663 z => -1,
1664 can_focus => 1,
1665 list => (glGenLists 1),
1666 @_
1667 )
1668}
1669
1670sub key_down {
1671 print "MAPKEYDOWN\n";
1672}
1673
1674sub key_up {
1675}
1676
1677sub button_down {
1678 my ($self, $ev, $x, $y) = @_;
1679
1680 $self->focus_in;
1681
1682 if ($ev->button == 2) {
1683 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1684 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1685
1686 $self->{motion} = sub {
1687 my ($ev, $x, $y) = @_;
1688
1689 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1690
1691 $::CFG->{map_shift_x} = $bw + $x - $ox;
1692 $::CFG->{map_shift_y} = $bh + $y - $oy;
1693
1694 $self->update;
1695 };
1696 }
1697}
1698
1699sub button_up {
1700 my ($self, $ev, $x, $y) = @_;
1701
1702 delete $self->{motion};
1703}
1704
1705sub mouse_motion {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->{motion}->($ev, $x, $y) if $self->{motion};
1709}
1710
1711sub size_request {
1712 (
1713 1 + 32 * int $::WIDTH / 32,
1714 1 + 32 * int $::HEIGHT / 32,
1715 )
1716}
1717
1718sub update {
1719 my ($self) = @_;
1720
1721 $self->{need_update} = 1;
1722 $self->SUPER::update;
1723}
1724
1725sub draw {
1726 my ($self) = @_;
1727
1728 if (delete $self->{need_update}) {
1729 glNewList $self->{list}, GL_COMPILE;
1730
1731 if ($::MAP) {
1732 my $sw = int $::WIDTH / 32;
1733 my $sh = int $::HEIGHT / 32;
1734
1735 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1736 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1737
1738 glTranslate $sx0 - 32, $sy0 - 32, 0;
1739
1740 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1741
1742 if ($::CFG->{fow_enable}) {
1743 if ($::CFG->{fow_smooth}) { # smooth fog of war
1744 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1745 glConvolutionFilter2D
1746 GL_CONVOLUTION_2D,
1747 GL_ALPHA,
1748 3, 3,
1749 GL_ALPHA, GL_FLOAT,
1750 pack "f*",
1751 0.1, 0.1, 0.1,
1752 0.1, 0.2, 0.1,
1753 0.1, 0.1, 0.1,
1754 ;
1755 glEnable GL_CONVOLUTION_2D;
1756 }
1757
1758 my $tex = new CFClient::Texture
1759 w => $w,
1760 h => $h,
1761 data => $data,
1762 internalformat => GL_ALPHA,
1763 format => GL_ALPHA;
1764
1765 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1766
1767 glEnable GL_BLEND;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771
1772 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1774
1775 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND;
1777 }
1778
1779 # HACK BEGIN
1780 {
1781 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1782 my ($w, $h) = (250, 250);
1783
1784 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788
1789 CFClient::Texture->new (
1790 w => $w,
1791 h => $h,
1792 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV
1794 )->draw_quad (100, 100);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799 # HACK END
1800 }
1801
1802 glEndList;
1803 }
1804
1805 glPushMatrix;
1806 glCallList $self->{list};
1807 glPopMatrix;
1808
1809 if ($FOCUS != $self) {
1810 glColor 64/255, 64/255, 64/255;
1811 glLogicOp GL_AND;
1812 glEnable GL_COLOR_LOGIC_OP;
1813 glBegin GL_QUADS;
1814 glVertex 0, 0;
1815 glVertex 0, $::HEIGHT;
1816 glVertex $::WIDTH, $::HEIGHT;
1817 glVertex $::WIDTH, 0;
1818 glEnd;
1819 glDisable GL_COLOR_LOGIC_OP;
1820 }
1821}
1822
1823my %DIR = (
1824 SDLK_KP8, [1, "north"],
1825 SDLK_KP9, [2, "northeast"],
1826 SDLK_KP6, [3, "east"],
1827 SDLK_KP3, [4, "southeast"],
1828 SDLK_KP2, [5, "south"],
1829 SDLK_KP1, [6, "southwest"],
1830 SDLK_KP4, [7, "west"],
1831 SDLK_KP7, [8, "northwest"],
1832
1833 SDLK_UP, [1, "north"],
1834 SDLK_RIGHT, [3, "east"],
1835 SDLK_DOWN, [5, "south"],
1836 SDLK_LEFT, [7, "west"],
1837);
1838
1839sub key_down {
1840 my ($self, $ev) = @_;
1841
1842 my $mod = $ev->key_mod;
1843 my $sym = $ev->key_sym;
1844
1845 if ($sym == SDLK_KP5) {
1846 $::CONN->user_send ("command stay fire");
1847 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply");
1849 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/');
1853 } elsif (exists $DIR{$sym}) {
1854 if ($mod & KMOD_SHIFT) {
1855 $self->{shft}++;
1856 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1857 } elsif ($mod & KMOD_CTRL) {
1858 $self->{ctrl}++;
1859 $::CONN->user_send ("command run $DIR{$sym}[0]");
1860 } else {
1861 $::CONN->user_send ("command $DIR{$sym}[1]");
1862 }
1863 }
1864}
1865
1866sub key_up {
1867 my ($self, $ev) = @_;
1868
1869 my $mod = $ev->key_mod;
1870 my $sym = $ev->key_sym;
1871
1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1873 $::CONN->user_send ("command fire_stop");
1874 }
1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1876 $::CONN->user_send ("command run_stop");
1877 }
1878}
1879
1880#############################################################################
1881
1882package CFClient::UI::Animator; 2182package CFClient::UI::Animator;
1883 2183
1884use SDL::OpenGL; 2184use CFClient::OpenGL;
1885 2185
1886our @ISA = CFClient::UI::Bin::; 2186our @ISA = CFClient::UI::Bin::;
1887 2187
1888sub moveto { 2188sub moveto {
1889 my ($self, $x, $y) = @_; 2189 my ($self, $x, $y) = @_;
1927 2227
1928sub new { 2228sub new {
1929 my $class = shift; 2229 my $class = shift;
1930 2230
1931 my $self = $class->SUPER::new ( 2231 my $self = $class->SUPER::new (
1932 state => 0, 2232 state => 0,
1933 connect_activate => \&toggle_flopper, 2233 connect_activate => \&toggle_flopper,
1934 @_ 2234 @_
1935 ); 2235 );
1936 2236
1937 if ($self->{state}) { 2237 if ($self->{state}) {
1945sub toggle_flopper { 2245sub toggle_flopper {
1946 my ($self) = @_; 2246 my ($self) = @_;
1947 2247
1948 # TODO: use animation 2248 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 2249 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2250 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 2251 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 2252 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 2253 } else {
1956 $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;
1957 } 2317
1958} 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;
1959 2327
1960############################################################################# 2328 glTranslate 2, 2;
2329 $self->SUPER::_draw;
2330}
1961 2331
2332#############################################################################
2333
1962package 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;
1963 2373
1964our @ISA = CFClient::UI::Container::; 2374our @ISA = CFClient::UI::Container::;
1965 2375
1966use SDL::OpenGL; 2376use CFClient::OpenGL;
2377
2378sub check_size {
2379 my ($self) = @_;
2380
2381 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2382}
1967 2383
1968sub size_request { 2384sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2385 ($::WIDTH, $::HEIGHT)
1970} 2386}
1971 2387
1972sub size_allocate { 2388sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 2389 my ($self, $x, $y, $w, $h) = @_;
1974 2390
1975 $self->_size_allocate ($x, $y, $w, $h); 2391 $self->SUPER::configure ($x, $y, $w, $h);
1976 2392
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2393 for my $child (@{$self->{children}}) {
1979} 2394 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1980 2395
1981sub 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 {
1982 my ($self, $x, $y) = @_; 2403 my ($self, $x, $y) = @_;
1983 2404
1984 ($x, $y) 2405 ($x, $y)
1985} 2406}
1986 2407
1987sub update { 2408sub update {
1988 my ($self) = @_; 2409 my ($self) = @_;
1989 2410
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2411 $self->check_size;
1991 ::refresh (); 2412 ::refresh ();
1992} 2413}
1993 2414
1994sub add { 2415sub add {
1995 my ($self, $widget) = @_; 2416 my ($self, $child) = @_;
1996 2417
2418 # integerize window positions
2419 $child->{x} = int $child->{x};
2420 $child->{y} = int $child->{y};
2421
1997 $self->SUPER::add ($widget); 2422 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 2423}
2001 2424
2002sub on_refresh { 2425sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2426 my ($self, $id, $cb) = @_;
2004 2427
2027 2450
2028############################################################################# 2451#############################################################################
2029 2452
2030package CFClient::UI; 2453package CFClient::UI;
2031 2454
2455$ROOT = new CFClient::UI::Root;
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2456$TOOLTIP = new CFClient::UI::Tooltip;
2033 2457
20341 24581
2035 2459

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines