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.73 by root, Tue Apr 11 18:06:53 2006 UTC vs.
Revision 1.140 by root, Thu Apr 20 04:20:52 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines