ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.75 by root, Tue Apr 11 20:44:49 2006 UTC vs.
Revision 1.140 by root, Thu Apr 20 04:20:52 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 $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $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 }
71} 84}
72 85
73############################################################################# 86#############################################################################
74 87
75package CFClient::UI::Base; 88package CFClient::UI::Base;
76 89
77use strict; 90use strict;
78 91
79use SDL::OpenGL; 92use CFClient::OpenGL;
80 93
81sub new { 94sub new {
82 my $class = shift; 95 my $class = shift;
83 96
84 bless { 97 my $self = bless {
85 x => 0, 98 x => 0,
86 y => 0, 99 y => 0,
87 z => 0, 100 z => 0,
88 w => -1,
89 h => -1,
90 @_ 101 @_
91 }, $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);
92} 127}
93 128
94sub move { 129sub move {
95 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
96 $self->{x} = $x; 132 $self->{x} = int $x;
97 $self->{y} = $y; 133 $self->{y} = int $y;
98 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
99} 137}
100 138
101sub needs_redraw { 139sub needs_redraw {
102 0 140 0
103} 141}
105sub size_request { 143sub size_request {
106 require Carp; 144 require Carp;
107 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abtract";
108} 146}
109 147
110sub _size_allocate { 148sub configure {
111 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
112 150
113 $self->{x} = $x; 151 $self->{x} = $x;
114 $self->{y} = $y; 152 $self->{y} = $y;
115 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
116 return unless $self->{w} != $w || $self->{h} != $h; 159 return unless $self->{w} != $w || $self->{h} != $h;
117 160
118 $self->{w} = $w; 161 $self->{w} = $w;
119 $self->{h} = $h; 162 $self->{h} = $h;
120 163
121 1 164 $self->size_allocate ($w, $h);
165 $self->update;
122} 166}
123 167
124sub size_allocate { 168sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_; 169 # nothing to be done
126
127 $self->_size_allocate ($x, $y, $w, $h);
128} 170}
129 171
130# translate global koordinates to local coordinate system 172# return top left coordinates
131sub translate { 173sub _topleft {
132 my ($self, $x, $y) = @_; 174 my ($self, $x, $y) = @_;
133 175
134 $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)
135} 193}
136 194
137sub focus_in { 195sub focus_in {
138 my ($self) = @_; 196 my ($self) = @_;
139 197
140 return if $FOCUS == $self; 198 return if $FOCUS == $self;
199 return unless $self->{can_focus};
141 200
142 my $focus = $FOCUS; $FOCUS = $self; 201 my $focus = $FOCUS; $FOCUS = $self;
202
203 $self->emit (focus_in => $focus);
204
143 $focus->update if $focus; 205 $focus->update if $focus;
144 $FOCUS->update; 206 $FOCUS->update;
145} 207}
146 208
147sub focus_out { 209sub focus_out {
148 my ($self) = @_; 210 my ($self) = @_;
149 211
150 return unless $FOCUS == $self; 212 return unless $FOCUS == $self;
151 213
152 my $focus = $FOCUS; undef $FOCUS; 214 my $focus = $FOCUS; undef $FOCUS;
215
216 $self->emit (focus_out => $focus);
217
153 $focus->update if $focus; #? 218 $focus->update if $focus; #?
154} 219}
155 220
156sub mouse_motion { } 221sub mouse_motion { }
157sub button_up { } 222sub button_up { }
178 glPushMatrix; 243 glPushMatrix;
179 glTranslate $self->{x}, $self->{y}, 0; 244 glTranslate $self->{x}, $self->{y}, 0;
180 $self->_draw; 245 $self->_draw;
181 glPopMatrix; 246 glPopMatrix;
182 247
183 if ($self == $HOVER) { 248 if ($self == $HOVER && $self->{can_hover}) {
184 my ($x, $y) = @$self{qw(x y)}; 249 my ($x, $y) = @$self{qw(x y)};
185 250
186 glColor 1, 1, 1, 0.1; 251 glColor 1, 0.8, 0.5, 0.2;
187 glEnable GL_BLEND; 252 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 253 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
189 glBegin GL_QUADS; 254 glBegin GL_QUADS;
190 glVertex $x , $y; 255 glVertex $x , $y;
191 glVertex $x + $self->{w}, $y; 256 glVertex $x + $self->{w}, $y;
200 my ($self) = @_; 265 my ($self) = @_;
201 266
202 warn "no draw defined for $self\n"; 267 warn "no draw defined for $self\n";
203} 268}
204 269
205sub bbox {
206 my ($self) = @_;
207 my ($w, $h) = $self->size_request;
208 (
209 $self->{x},
210 $self->{y},
211 $self->{x} = $w,
212 $self->{y} = $h
213 )
214}
215
216sub find_widget { 270sub find_widget {
217 my ($self, $x, $y) = @_; 271 my ($self, $x, $y) = @_;
218 272
219 return $self 273 return $self
220 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 274 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
221 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 275 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
222 276
223 () 277 ()
224} 278}
225 279
226sub del_parent { $_[0]->{parent} = undef }
227
228sub set_parent { 280sub set_parent {
229 my ($self, $par) = @_; 281 my ($self, $parent) = @_;
230 282
231 $self->{parent} = $par;
232 Scalar::Util::weaken $self->{parent}; 283 Scalar::Util::weaken ($self->{parent} = $parent);
233} 284}
234 285
235sub get_parent { 286sub check_size {
236 $_[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 }
237} 298}
238 299
239sub update { 300sub update {
240 my ($self) = @_; 301 my ($self) = @_;
241 302
244} 305}
245 306
246sub connect { 307sub connect {
247 my ($self, $signal, $cb) = @_; 308 my ($self, $signal, $cb) = @_;
248 309
249 push @{ $self->{cb}{$signal} }, $cb; 310 push @{ $self->{signal_cb}{$signal} }, $cb;
250} 311}
251 312
252sub emit { 313sub emit {
253 my ($self, $signal, @args) = @_; 314 my ($self, $signal, @args) = @_;
254 315
316 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
255 $_->($self, @args) 317 $cb->($self, @args);
256 for @{$self->{cb}{$signal} || []}; 318 }
257} 319}
258 320
259sub DESTROY { 321sub DESTROY {
260 my ($self) = @_; 322 my ($self) = @_;
261 323
267package CFClient::UI::DrawBG; 329package CFClient::UI::DrawBG;
268 330
269our @ISA = CFClient::UI::Base::; 331our @ISA = CFClient::UI::Base::;
270 332
271use strict; 333use strict;
272use SDL::OpenGL; 334use CFClient::OpenGL;
273 335
274sub new { 336sub new {
275 my $class = shift; 337 my $class = shift;
276 338
277 # range [value, low, high, page] 339 # range [value, low, high, page]
278 340
279 $class->SUPER::new ( 341 $class->SUPER::new (
280 bg => [0, 0, 0, 0.4], 342 bg => [0, 0, 0, 0.2],
281 active_bg => [1, 1, 1], 343 active_bg => [1, 1, 1, 0.5],
282 @_ 344 @_
283 ) 345 )
284} 346}
285 347
286sub _draw { 348sub _draw {
287 my ($self) = @_; 349 my ($self) = @_;
288 350
289 my ($w, $h) = @$self{qw(w h)}; 351 my ($w, $h) = @$self{qw(w h)};
290 352
353 glEnable GL_BLEND;
354 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
291 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 355 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
356
292 glBegin GL_QUADS; 357 glBegin GL_QUADS;
293 glVertex 0 , 0; 358 glVertex 0 , 0;
294 glVertex 0 , $h; 359 glVertex 0 , $h;
295 glVertex $w, $h; 360 glVertex $w, $h;
296 glVertex $w, 0; 361 glVertex $w, 0;
297 glEnd; 362 glEnd;
363
364 glDisable GL_BLEND;
298} 365}
299 366
300############################################################################# 367#############################################################################
301 368
302package CFClient::UI::Empty; 369package CFClient::UI::Empty;
325 392
326 $self 393 $self
327} 394}
328 395
329sub add { 396sub add {
330 my ($self, $chld, $expand) = @_; 397 my ($self, $child) = @_;
331 398
332 $chld->{expand} = $expand;
333 $chld->set_parent ($self); 399 $child->set_parent ($self);
400
401 use sort 'stable';
334 402
335 $self->{children} = [ 403 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 404 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 405 @{$self->{children}}, $child
338 ]; 406 ];
339 407
340 $self->{w} = $self->{h} = -1; 408 $child->check_size;
341 $self->update;
342} 409}
343 410
344sub remove { 411sub remove {
345 my ($self, $widget) = @_; 412 my ($self, $child) = @_;
346 413
414 delete $child->{parent};
415
347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 416 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
348 417
349 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 418 $self->check_size;
350} 419}
351 420
352sub find_widget { 421sub find_widget {
353 my ($self, $x, $y) = @_; 422 my ($self, $x, $y) = @_;
354 423
384 453
385 $class->SUPER::new (children => [$child], %arg) 454 $class->SUPER::new (children => [$child], %arg)
386} 455}
387 456
388sub add { 457sub add {
389 my ($self, $widget) = @_; 458 my ($self, $child) = @_;
390 459
391 $self->{children} = []; 460 $self->{children} = [];
392 461
393 $self->SUPER::add ($widget); 462 $self->SUPER::add ($child);
394} 463}
395 464
396sub remove { 465sub remove {
397 my ($self, $widget) = @_; 466 my ($self, $widget) = @_;
398 467
407sub size_request { 476sub size_request {
408 $_[0]{children}[0]->size_request 477 $_[0]{children}[0]->size_request
409} 478}
410 479
411sub size_allocate { 480sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_; 481 my ($self, $w, $h) = @_;
413 482
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h); 483 $self->{children}[0]->configure (0, 0, $w, $h);
417} 484}
418 485
419############################################################################# 486#############################################################################
420 487
421package CFClient::UI::Window; 488package CFClient::UI::Window;
422 489
423our @ISA = CFClient::UI::Bin::; 490our @ISA = CFClient::UI::Bin::;
424 491
425use SDL::OpenGL; 492use CFClient::OpenGL;
426 493
427sub new { 494sub new {
428 my ($class, %arg) = @_; 495 my ($class, %arg) = @_;
429 496
430 my $self = $class->SUPER::new (%arg); 497 my $self = $class->SUPER::new (%arg);
439} 506}
440 507
441sub render_chld { 508sub render_chld {
442 my ($self) = @_; 509 my ($self) = @_;
443 510
444 $self->{texture} = 511 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
445 CFClient::Texture->new_from_opengl ( 512 glClearColor 0, 0, 0, 1;
446 $self->{w}, $self->{h}, sub { $self->child->draw } 513 glClear GL_COLOR_BUFFER_BIT;
447 ); 514 $self->child->draw;
515 };
448} 516}
449 517
450sub size_allocate { 518sub size_allocate {
451 my ($self, $x, $y, $w, $h) = @_; 519 my ($self, $w, $h) = @_;
452 520
453 $self->_size_allocate ($x, $y, $w, $h) or return;
454
455 $self->child->size_allocate (0, 0, $w, $h); 521 $self->child->configure (0, 0, $w, $h);
456 522
457 $self->render_chld; 523 $self->render_chld;
458} 524}
459 525
460sub _draw { 526sub _draw {
476 glDisable GL_TEXTURE_2D; 542 glDisable GL_TEXTURE_2D;
477} 543}
478 544
479############################################################################# 545#############################################################################
480 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
481package CFClient::UI::Frame; 571package CFClient::UI::Frame;
482 572
483our @ISA = CFClient::UI::Bin::; 573our @ISA = CFClient::UI::Bin::;
484 574
485use SDL::OpenGL; 575use CFClient::OpenGL;
486 576
487sub size_request { 577sub size_request {
488 my ($self) = @_; 578 my ($self) = @_;
489 my $chld = $self->child 579 my $chld = $self->child
490 or return (0, 0); 580 or return (0, 0);
495} 585}
496 586
497sub size_allocate { 587sub size_allocate {
498 my ($self, $x, $y, $w, $h) = @_; 588 my ($self, $x, $y, $w, $h) = @_;
499 589
500 $self->_size_allocate ($x, $y, $w, $h) or return;
501
502 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 590 $self->child->configure (2, 2, $w - 4, $h - 4);
503} 591}
504 592
505sub _draw { 593sub _draw {
506 my ($self) = @_; 594 my ($self) = @_;
507 595
524 612
525package CFClient::UI::FancyFrame; 613package CFClient::UI::FancyFrame;
526 614
527our @ISA = CFClient::UI::Bin::; 615our @ISA = CFClient::UI::Bin::;
528 616
529use SDL::OpenGL; 617use CFClient::OpenGL;
530 618
531my @tex = 619my @tex =
532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
533 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
534 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
535sub size_request { 640sub size_request {
536 my ($self) = @_; 641 my ($self) = @_;
537 642
643 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
644
538 my ($w, $h) = $self->SUPER::size_request; 645 my ($w, $h) = $self->SUPER::size_request;
539 646
540 $h += $tex[1]->{h}; 647 (
541 $h += $tex[4]->{h}; 648 $w + $self->border * 2,
542 $w += $tex[2]->{w}; 649 $h + $self->border * 2,
543 $w += $tex[3]->{w}; 650 )
544
545 ($w, $h)
546} 651}
547 652
548sub size_allocate { 653sub size_allocate {
549 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
550 655
551 $self->_size_allocate ($x, $y, $w, $h) or return; 656 $h -= List::Util::max 0, $self->border * 2;
657 $w -= List::Util::max 0, $self->border * 2;
552 658
553 $h -= $tex[1]->{h}; 659 $self->child->configure ($self->border, $self->border, $w, $h);
554 $h -= $tex[4]->{h}; 660}
555 $w -= $tex[2]->{w};
556 $w -= $tex[3]->{w};
557 661
558 $h = $h < 0 ? 0 : $h; 662sub button_down {
559 $w = $w < 0 ? 0 : $w; 663 my ($self, $ev, $x, $y) = @_;
560 664
561 my $child = $self->child; 665 my $border = $self->border;
562 666
563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); 667 if ($x < $self->{w} && $x >= $self->{w} - $border
668 && $y < $self->{h} && $y >= $self->{h} - $border) {
669
670 my ($ox, $oy) = ($ev->{x}, $ev->{y});
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};
564} 710}
565 711
566sub _draw { 712sub _draw {
567 my ($self) = @_; 713 my ($self) = @_;
568 714
569 my ($w, $h) = ($self->{w}, $self->{h}); 715 my ($w, $h ) = ($self->{w}, $self->{h});
570 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 716 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
717
718 glEnable GL_BLEND;
719 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
720 glEnable GL_TEXTURE_2D;
721 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
722
723 my $border = $self->border;
724
725 glColor @{ $self->{border_bg} };
726 $tex[1]->draw_quad (0, 0, $w, $border);
727 $tex[3]->draw_quad (0, $border, $border, $ch);
728 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
729 $tex[4]->draw_quad (0, $h - $border, $w, $border);
730
731 my $bg = $tex[0];
732
733 # TODO: repeat texture not scale
734 my $rep_x = $cw / $bg->{w};
735 my $rep_y = $ch / $bg->{h};
736
737 glColor @{ $self->{bg} };
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;
745 glDisable GL_BLEND;
746
747 $self->child->draw;
748}
749
750#############################################################################
751
752package CFClient::UI::Table;
753
754our @ISA = CFClient::UI::Base::;
755
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}
768
769sub add {
770 my ($self, $x, $y, $child) = @_;
771
772 $child->set_parent ($self);
773 $self->{children}[$y][$x] = $child;
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};
783 $self->update;
784}
785
786sub get_wh {
787 my ($self) = @_;
788
789 my (@w, @h);
790
791 for my $y (0 .. $#{$self->{children}}) {
792 my $row = $self->{children}[$y]
793 or next;
794
795 for my $x (0 .. $#$row) {
796 my $widget = $row->[$x]
797 or next;
798 my ($w, $h) = $widget->size_request;
799
800 $w[$x] = max $w[$x], $w;
801 $h[$y] = max $h[$y], $h;
802 }
803 }
804
805 (\@w, \@h)
806}
807
808sub size_request {
809 my ($self) = @_;
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
845 my $x = 0;
846 my $row_h = $hs->[$r];
847
848 for my $c (0 .. $#$row) {
849 my $col_w = $ws->[$c];
850
851 if (my $widget = $row->[$c]) {
852 $widget->configure ($x, $y, $col_w, $row_h);
853 }
854
855 $x += $col_w;
856 }
857
858 $y += $row_h;
859 }
860
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::;
894
895sub size_request {
896 my ($self) = @_;
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
909 ($h, $w) = ($w, $h);
910
911 my $children = $self->{children};
912
913 my @h = map +($_->size_request)[0], @$children;
914
915 my $req_h = List::Util::sum @h;
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;
930 }
931 }
932
933 CFClient::UI::harmonize \@h;
934
935 my $y = 0;
936 for (0 .. $#$children) {
937 my $child = $children->[$_];
938 my $h = $h[$_];
939 $child->configure ($y, 0, $h, $w);
940
941 $y += $h;
942 }
943
944 1
945}
946
947#############################################################################
948
949package CFClient::UI::VBox;
950
951# TODO: wrap into common Box base class
952
953our @ISA = CFClient::UI::Container::;
954
955sub size_request {
956 my ($self) = @_;
957
958 my @alloc = map [$_->size_request], @{$self->{children}};
959
960 (
961 (List::Util::max map $_->[0], @alloc),
962 (List::Util::sum map $_->[1], @alloc),
963 )
964}
965
966sub size_allocate {
967 my ($self, $w, $h) = @_;
968
969 my $children = $self->{children};
970
971 my @h = map +($_->size_request)[1], @$children;
972
973 my $req_h = List::Util::sum @h;
974
975 if ($req_h > $h) {
976 # ah well, not enough space
977 $_ *= $h / $req_h for @h;
978 } else {
979 my $exp = List::Util::sum map $_->{expand}, @$children;
980 $exp ||= 1;
981
982 for (0 .. $#$children) {
983 my $child = $children->[$_];
984
985 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
986 }
987 }
988
989 CFClient::UI::harmonize \@h;
990
991 my $y = 0;
992 for (0 .. $#$children) {
993 my $child = $children->[$_];
994 my $h = $h[$_];
995 $child->configure (0, $y, $w, $h);
996
997 $y += $h;
998 }
999
1000 1
1001}
1002
1003#############################################################################
1004
1005package CFClient::UI::Label;
1006
1007our @ISA = CFClient::UI::Base::;
1008
1009use CFClient::OpenGL;
1010
1011sub new {
1012 my ($class, %arg) = @_;
1013
1014 my $self = $class->SUPER::new (
1015 fg => [1, 1, 1],
1016 fontsize => 1,
1017 text => "",
1018 align => -1,
1019 valign => -1,
1020 padding => 2,
1021 layout => new CFClient::Layout,
1022 %arg
1023 );
1024
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};
1029
1030 $self
1031}
1032
1033sub escape_text {
1034 local $_ = $_[1];
1035
1036 s/&/&amp;/g;
1037 s/>/&gt;/g;
1038 s/</&lt;/g;
1039
1040 $_[1]
1041}
1042
1043sub set_text {
1044 my ($self, $text) = @_;
1045
1046 $self->{layout}->set_text ($text);
1047
1048 delete $self->{texture};
1049 $self->update;
1050}
1051
1052sub set_markup {
1053 my ($self, $markup) = @_;
1054
1055 $self->{layout}->set_markup ($markup);
1056
1057 delete $self->{texture};
1058 $self->update;
1059}
1060
1061sub size_request {
1062 my ($self) = @_;
1063
1064 $self->{layout}->set_width;
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066
1067 my ($w, $h) = $self->{layout}->size;
1068
1069 (
1070 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2,
1072 )
1073}
1074
1075sub size_allocate {
1076 my ($self, $w, $h) = @_;
1077
1078 delete $self->{texture};
1079}
1080
1081sub _draw {
1082 my ($self) = @_;
1083
1084 my $tex = $self->{texture} ||= do {
1085 $self->{layout}->set_width ($self->{w});
1086 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1087 new_from_layout CFClient::Texture $self->{layout}
1088 };
571 1089
572 glEnable GL_BLEND; 1090 glEnable GL_BLEND;
573 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1091 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
574 glEnable GL_TEXTURE_2D; 1092 glEnable GL_TEXTURE_2D;
575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1093 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
576 1094
577 my $top = $tex[1]; 1095 glColor @{$self->{fg}};
578 $top->draw_quad (0, 0, $w, $top->{h});
579 1096
580 my $left = $tex[3]; 1097 $self->{ox} = int (
581 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 1098 $self->{align} < 0 ? $self->{padding}
582 1099 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
583 my $right = $tex[2]; 1100 : ($self->{w} - $tex->{w}) * 0.5
584 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
585
586 my $bottom = $tex[4];
587 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
588
589 my $bg = $tex[0];
590 glBindTexture GL_TEXTURE_2D, $bg->{name};
591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
592 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
594
595 my $rep_x = $cw / $bg->{w};
596 my $rep_y = $ch / $bg->{h};
597
598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
599
600 glDisable GL_BLEND;
601 glDisable GL_TEXTURE_2D;
602
603 $self->child->draw;
604
605}
606
607#############################################################################
608
609package CFClient::UI::Table;
610
611our @ISA = CFClient::UI::Base::;
612
613use List::Util qw(max sum);
614
615use SDL::OpenGL;
616
617sub add {
618 my ($self, $x, $y, $chld) = @_;
619
620 $self->{children}[$y][$x] = $chld;
621 $chld->set_parent ($self);
622
623 $self->{w} = $self->{h} = -1;
624 $self->update;
625}
626
627sub get_wh {
628 my ($self) = @_;
629
630 my (@w, @h);
631
632 for my $y (0 .. $#{$self->{children}}) {
633 my $row = $self->{children}[$y]
634 or next;
635
636 for my $x (0 .. $#$row) {
637 my $widget = $row->[$x]
638 or next;
639 my ($w, $h) = $widget->size_request;
640
641 $w[$x] = max $w[$x], $w;
642 $h[$y] = max $h[$y], $h;
643 }
644 }
645
646 (\@w, \@h)
647}
648
649sub size_request {
650 my ($self) = @_;
651
652 my ($ws, $hs) = $self->get_wh;
653
654 (
655 (List::Util::sum @$ws),
656 (List::Util::sum @$hs),
657 )
658}
659
660sub size_allocate {
661 my ($self, $x, $y, $w, $h) = @_;
662
663 $self->_size_allocate ($x, $y, $w, $h) or return;
664
665 my ($ws, $hs) = $self->get_wh;
666
667 my $req_w = List::Util::sum @$ws;
668 my $req_h = List::Util::sum @$hs;
669
670 # linearly scale sizes
671 $_ *= $req_w / $w for @$ws;
672 $_ *= $req_h / $h for @$hs;
673
674 my $y;
675
676 for my $r (0 .. $#{$self->{children}}) {
677 my $row = $self->{children}[$r]
678 or next;
679
680 my $x = 0;
681 my $row_h = $hs->[$r];
682
683 for my $c (0 .. $#$row) {
684 my $widget = $row->[$c]
685 or next;
686
687 my $col_w = $ws->[$c];
688
689 $widget->size_allocate ($x, $y, $col_w, $row_h);
690
691 $x += $col_w;
692 }
693
694 $y += $row_h;
695 }
696
697}
698
699sub _draw {
700 my ($self) = @_;
701
702 for (grep $_, @{$self->{children}}) {
703 $_->draw for grep $_, @$_;
704 }
705}
706
707#############################################################################
708
709package CFClient::UI::VBox;
710
711our @ISA = CFClient::UI::Container::;
712
713use SDL::OpenGL;
714
715sub size_request {
716 my ($self) = @_;
717
718 my @alloc = map [$_->size_request], @{$self->{children}};
719
720 (
721 (List::Util::max map $_->[0], @alloc),
722 (List::Util::sum map $_->[1], @alloc),
723 )
724}
725
726sub size_allocate {
727 my ($self, $x, $y, $w, $h) = @_;
728
729 $self->_size_allocate ($x, $y, $w, $h) or return;
730
731 return unless $self->{h};
732
733 my $children = $self->{children};
734
735 my @h = map +($_->size_request)[1], @$children;
736
737 my $req_h = List::Util::sum @h;
738
739 if ($req_h > $h) {
740 # ah well, not enough space
741 $_ = $h[$_] * $h / $req_h for @h;
742 } else {
743 my @exp = grep $_->{expand}, @$children;
744 @exp = @$children unless @exp;
745
746 my %exp = map +($_ => 1), @exp;
747
748 for (0 .. $#$children) {
749 my $child = $children->[$_];
750
751 my $alloc_h = $h[$_];
752 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
753 $h[$_] = $alloc_h;
754 }
755 }
756
757 my $y = 0;
758 for (0 .. $#$children) {
759 my $child = $children->[$_];
760 my $h = $h[$_];
761 $child->size_allocate (0, $y, $w, $h);
762
763 $y += $h;
764 }
765}
766
767#############################################################################
768
769package CFClient::UI::Label;
770
771our @ISA = CFClient::UI::Base::;
772
773use SDL::OpenGL;
774
775sub new {
776 my ($class, %arg) = @_;
777
778 my $self = $class->SUPER::new (
779 fg => [1, 1, 1],
780 height => $::FONTSIZE,
781 text => "",
782 align => -1,
783 layout => new CFClient::Layout,
784 %arg
785 ); 1101 );
786 1102
787 $self->set_text ($self->{text}); 1103 $self->{oy} = int (
788
789 $self
790}
791
792sub escape_text {
793 local $_ = $_[1];
794
795 s/&/&amp;/g;
796 s/>/&gt;/g;
797 s/</&lt;/g;
798
799 $_[1]
800}
801
802sub set_text {
803 my ($self, $text) = @_;
804
805 $self->{text} = $text;
806 $self->{layout}->set_markup ($text);
807
808 delete $self->{texture};
809 $self->update;
810}
811
812sub get_text {
813 my ($self, $text) = @_;
814
815 $self->{text}
816}
817
818sub size_request {
819 my ($self) = @_;
820
821 $self->{layout}->set_width;
822 $self->{layout}->set_height ($self->{height});
823 $self->{layout}->size
824# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
825# (
826# $self->{texture}{w},
827# $self->{texture}{h},
828# )
829# } else {
830# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
831#
832# ($w, $h)
833# }
834}
835
836sub size_allocate {
837 my ($self, $x, $y, $w, $h) = @_;
838
839 $self->_size_allocate ($x, $y, $w, $h) or return;
840
841 delete $self->{texture};
842}
843
844sub update {
845 my ($self) = @_;
846
847 delete $self->{texture};
848 $self->SUPER::update;
849}
850
851sub _draw {
852 my ($self) = @_;
853
854 my $tex = $self->{texture} ||= do {
855 $self->{layout}->set_width ($self->{w});
856 new_from_layout CFClient::Texture $self->{layout}
857 };
858
859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
861 glEnable GL_TEXTURE_2D;
862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
863
864 glColor @{$self->{fg}};
865
866 my $x =
867 $self->{align} < 0 ? 0 1104 $self->{valign} < 0 ? $self->{padding}
868 : $self->{align} > 0 ? $self->{w} - $tex->{w} 1105 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
869 : ($self->{w} - $tex->{w}) * 0.5; 1106 : ($self->{h} - $tex->{h}) * 0.5
1107 );
870 1108
871 $tex->draw_quad ($x, 0); 1109 $tex->draw_quad ($self->{ox}, $self->{oy});
872 1110
873 glDisable GL_TEXTURE_2D; 1111 glDisable GL_TEXTURE_2D;
874 glDisable GL_BLEND; 1112 glDisable GL_BLEND;
875} 1113}
876 1114
877############################################################################# 1115#############################################################################
878 1116
879package CFClient::UI::Entry; 1117package CFClient::UI::EntryBase;
880 1118
881our @ISA = CFClient::UI::Label::; 1119our @ISA = CFClient::UI::Label::;
882 1120
883use SDL; 1121use CFClient::OpenGL;
884use SDL::OpenGL;
885 1122
886sub new { 1123sub new {
887 my $class = shift; 1124 my $class = shift;
888 1125
889 $class->SUPER::new ( 1126 $class->SUPER::new (
890 fg => [1, 1, 1], 1127 fg => [1, 1, 1],
891 bg => [0, 0, 0, 0.4], 1128 bg => [0, 0, 0, 0.2],
892 active_bg => [1, 1, 1], 1129 active_bg => [1, 1, 1, 0.5],
893 active_fg => [0, 0, 0], 1130 active_fg => [0, 0, 0],
1131 can_hover => 1,
1132 can_focus => 1,
1133 valign => 0,
894 @_ 1134 @_
895 ) 1135 )
896} 1136}
897 1137
898sub _set_text { 1138sub _set_text {
899 my ($self, $text) = @_; 1139 my ($self, $text) = @_;
900 1140
1141 delete $self->{cur_h};
1142
1143 return if $self->{text} eq $text;
1144
1145 delete $self->{texture};
1146
901 $self->{last_activity} = $::NOW; 1147 $self->{last_activity} = $::NOW;
902
903 $self->{text} = $text; 1148 $self->{text} = $text;
904 $self->{layout}->set_width ($self->{w});
905 1149
906 $text =~ s/./*/g if $self->{hidden}; 1150 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text ");
907 1152
1153 $self->emit (changed => $self->{text});
1154}
908 1155
909 $self->{layout}->set_markup ($self->escape_text ($text)); 1156sub get_text {
910 1157 $_[0]{text}
911 $text = substr $text, 0, $self->{cursor};
912 utf8::encode $text;
913
914 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
915} 1158}
916 1159
917sub size_request { 1160sub size_request {
918 my ($self) = @_; 1161 my ($self) = @_;
919 1162
921 1164
922 ($w + 1, $h) # add 1 for cursor 1165 ($w + 1, $h) # add 1 for cursor
923} 1166}
924 1167
925sub size_allocate { 1168sub size_allocate {
926 my ($self, $x, $y, $w, $h) = @_; 1169 my ($self, $w, $h) = @_;
927
928 $self->SUPER::size_allocate ($x, $y, $w, $h);
929 1170
930 $self->_set_text ($self->{text}); 1171 $self->_set_text ($self->{text});
931} 1172}
932 1173
933sub set_text { 1174sub set_text {
939} 1180}
940 1181
941sub key_down { 1182sub key_down {
942 my ($self, $ev) = @_; 1183 my ($self, $ev) = @_;
943 1184
944 my $mod = $ev->key_mod; 1185 my $mod = $ev->{mod};
945 my $sym = $ev->key_sym; 1186 my $sym = $ev->{sym};
946
947 my $uni = $ev->key_unicode; 1187 my $uni = $ev->{unicode};
948 1188
949 my $text = $self->get_text; 1189 my $text = $self->get_text;
950 1190
951 if ($sym == SDLK_BACKSPACE) { 1191 if ($sym == 8) {
952 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1192 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
953 } elsif ($sym == SDLK_DELETE) { 1193 } elsif ($sym == 127) {
954 substr $text, $self->{cursor}, 1, ""; 1194 substr $text, $self->{cursor}, 1, "";
955 } elsif ($sym == SDLK_LEFT) { 1195 } elsif ($sym == CFClient::SDLK_LEFT) {
956 --$self->{cursor} if $self->{cursor}; 1196 --$self->{cursor} if $self->{cursor};
957 } elsif ($sym == SDLK_RIGHT) { 1197 } elsif ($sym == CFClient::SDLK_RIGHT) {
958 ++$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');
959 } elsif ($uni) { 1205 } elsif ($uni) {
960 substr $text, $self->{cursor}++, 0, chr $uni; 1206 substr $text, $self->{cursor}++, 0, chr $uni;
961 } 1207 }
962 1208
963 $self->_set_text ($text); 1209 $self->_set_text ($text);
978 $self->SUPER::button_down ($ev, $x, $y); 1224 $self->SUPER::button_down ($ev, $x, $y);
979 1225
980 my $idx = $self->{layout}->xy_to_index ($x, $y); 1226 my $idx = $self->{layout}->xy_to_index ($x, $y);
981 1227
982 # byte-index to char-index 1228 # byte-index to char-index
983 my $text = $self->{layout}; 1229 my $text = $self->{text};
984 utf8::encode $text; 1230 utf8::encode $text;
985 $self->{cursor} = length substr $text, 0, $idx; 1231 $self->{cursor} = length substr $text, 0, $idx;
986 1232
987 $self->_set_text ($self->{text}); 1233 $self->_set_text ($self->{text});
988 $self->update; 1234 $self->update;
1003 $self->{fg} = $self->{active_fg}; 1249 $self->{fg} = $self->{active_fg};
1004 } else { 1250 } else {
1005 glColor @{$self->{bg}}; 1251 glColor @{$self->{bg}};
1006 } 1252 }
1007 1253
1254 glEnable GL_BLEND;
1255 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1008 glBegin GL_QUADS; 1256 glBegin GL_QUADS;
1009 glVertex 0 , 0; 1257 glVertex 0 , 0;
1010 glVertex 0 , $self->{h}; 1258 glVertex 0 , $self->{h};
1011 glVertex $self->{w}, $self->{h}; 1259 glVertex $self->{w}, $self->{h};
1012 glVertex $self->{w}, 0; 1260 glVertex $self->{w}, 0;
1013 glEnd; 1261 glEnd;
1262 glDisable GL_BLEND;
1014 1263
1015 $self->SUPER::_draw; 1264 $self->SUPER::_draw;
1016 1265
1017 #TODO: force update every cursor change :( 1266 #TODO: force update every cursor change :(
1018 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
1019 glColor @{$self->{fg}}; 1276 glColor @{$self->{fg}};
1020 glBegin GL_LINES; 1277 glBegin GL_LINES;
1021 glVertex $self->{cur_x}, $self->{cur_y};
1022 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};
1023 glEnd; 1280 glEnd;
1024 } 1281 }
1025} 1282}
1026 1283
1027#############################################################################
1028
1029package CFClient::UI::Slider; 1284package CFClient::UI::Entry;
1030 1285
1031use strict;
1032
1033use SDL::OpenGL;
1034
1035our @ISA = CFClient::UI::DrawBG::; 1286our @ISA = CFClient::UI::EntryBase::;
1036 1287
1037sub size_request { 1288use CFClient::OpenGL;
1289
1290sub key_down {
1038 my ($self) = @_; 1291 my ($self, $ev) = @_;
1039 1292
1040 my $w = 50; 1293 my $sym = $ev->{sym};
1041 my $h = 10;
1042 1294
1043 $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
1044} 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);
1045 1316
1046sub new { 1317sub new {
1047 my $class = shift; 1318 my $class = shift;
1048 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
1049 # range [value, low, high, page] 1557 # range [value, low, high, page]
1050 1558
1559 # TODO: 0-width page
1560 # TODO: req_w/h are wrong with vertical
1561 # TODO: calculations are off
1051 $class->SUPER::new ( 1562 my $self = $class->SUPER::new (
1052 fg => [1, 1, 1], 1563 fg => [1, 1, 1],
1053 active_fg => [0, 0, 0], 1564 active_fg => [0, 0, 0],
1054 range => [0, 0, 100, 10], 1565 range => [0, 0, 100, 10],
1566 req_w => 20,
1567 req_h => 20,
1055 vertical => 1, 1568 vertical => 0,
1569 can_hover => 1,
1570 inner_pad => 5,
1056 @_ 1571 @_
1057 ) 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)
1058} 1584}
1059 1585
1060sub button_down { 1586sub button_down {
1061 my ($self, $ev, $x, $y) = @_; 1587 my ($self, $ev, $x, $y) = @_;
1062 1588
1070 if ($GRAB == $self) { 1596 if ($GRAB == $self) {
1071 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1597 my ($value, $lo, $hi, $page) = @{$self->{range}};
1072 1598
1073 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1599 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1074 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
1075 $x = $x * ($hi - $lo) / $w + $lo; 1605 $x = $x * ($hi - $lo) / $inner_w + $lo;
1076 $x = $lo if $x < $lo; 1606 $x = $lo if $x < $lo;
1077 $x = $hi - $page if $x > $hi - $page; 1607 $x = $hi - $page if $x > $hi - $page;
1078 $self->{range}[0] = $x; 1608 $self->{range}[0] = $x;
1079 1609
1080 $self->emit (changed => $x); 1610 $self->emit (changed => $x);
1081 $self->update; 1611 $self->update;
1082 } 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
1083} 1621}
1084 1622
1085sub _draw { 1623sub _draw {
1086 my ($self) = @_; 1624 my ($self) = @_;
1087 1625
1101 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1639 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1102 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1640 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1103 1641
1104 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1642 my ($value, $lo, $hi, $page) = @{$self->{range}};
1105 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
1106 $page = int $page * $w / ($hi - $lo); 1649 $page = int $page * $inner_w / ($hi - $lo);
1107 $value = int +($value - $lo) * $w / ($hi - $lo); 1650 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1108 1651
1109 $w -= $page; 1652 $w -= $page;
1110 $page &= ~1; 1653 $page &= ~1;
1111 glTranslate $page * 0.5, 0, 0; 1654 glTranslate $page * 0.5, 0, 0;
1655 $page ||= 2;
1112 1656
1113 glColor @$fg;
1114 glBegin GL_LINES;
1115 glVertex 0, 0; glVertex 0, $h;
1116 glVertex $w - 1, 0; glVertex $w - 1, $h;
1117 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1118 glEnd;
1119
1120 my $knob_a = $value - $page * 0.5; 1657 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1121 my $knob_b = $value + $page * 0.5; 1658 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1122 1659
1123 glBegin GL_QUADS; 1660 glEnable GL_BLEND;
1124 glColor @$fg; 1661 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1125 glVertex $knob_a, 0; 1662 glEnable GL_TEXTURE_2D;
1126 glVertex $knob_a, $h; 1663 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1127 glVertex $knob_b, $h;
1128 glVertex $knob_b, 0;
1129 1664
1130 if ($knob_a < $knob_b - 2) { 1665 # draw background
1131 glColor @$bg; 1666 $tex[1]->draw_quad (0, 0, $w, $h);
1132 glVertex $knob_a + 1, 1;
1133 glVertex $knob_a + 1, $h - 1;
1134 glVertex $knob_b - 1, $h - 1;
1135 glVertex $knob_b - 1, 1;
1136 }
1137 glEnd;
1138}
1139 1667
1140############################################################################# 1668 # draw handle
1669 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1141 1670
1671 glDisable GL_BLEND;
1672 glDisable GL_TEXTURE_2D;
1673}
1674
1675#############################################################################
1676
1142package CFClient::UI::MapWidget; 1677package CFClient::UI::TextView;
1143 1678
1144use strict;
1145
1146use List::Util qw(min max);
1147
1148use SDL;
1149use SDL::OpenGL;
1150
1151our @ISA = CFClient::UI::Base::; 1679our @ISA = CFClient::UI::HBox::;
1680
1681use CFClient::OpenGL;
1152 1682
1153sub new { 1683sub new {
1154 my $class = shift; 1684 my $class = shift;
1155 1685
1156 $class->SUPER::new ( 1686 my $self = $class->SUPER::new (
1157 z => -1, 1687 fontsize => 1,
1158 list => (glGenLists 1),
1159 @_ 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 ],
1160 ) 1697 );
1161}
1162 1698
1163sub key_down { 1699 $self->{children}[1]->connect (changed => sub {
1164 print "MAPKEYDOWN\n"; 1700 $self->update;
1165} 1701 });
1166 1702
1167sub key_up { 1703 $self
1168} 1704}
1169 1705
1170sub 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);
1171 ( 1721
1172 1 + int $::WIDTH / 32, 1722 ($layout->size)[1]
1173 1 + int $::HEIGHT / 32, 1723}
1174 ) 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;
1175} 1756}
1176 1757
1177sub update { 1758sub update {
1178 my ($self) = @_; 1759 my ($self) = @_;
1179 1760
1180 $self->{need_update} = 1;
1181 $self->SUPER::update; 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 });
1182} 1817}
1183 1818
1184sub _draw { 1819sub _draw {
1185 my ($self) = @_; 1820 my ($self) = @_;
1186 1821
1187 if (delete $self->{need_update}) { 1822 if ($self->{texture}) {
1188 glNewList $self->{list}, GL_COMPILE;
1189
1190 my $mx = $::CONN->{mapx};
1191 my $my = $::CONN->{mapy};
1192
1193 my $map = $::CONN->{map};
1194
1195 my ($xofs, $yofs);
1196
1197 my $sw = 1 + int $::WIDTH / 32;
1198 my $sh = 1 + int $::HEIGHT / 32;
1199
1200 if ($::CONN->{mapw} > $sw) {
1201 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1202 } else {
1203 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1204 }
1205
1206 if ($::CONN->{maph} > $sh) {
1207 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1208 } else {
1209 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1210 }
1211
1212 glEnable GL_TEXTURE_2D; 1823 glEnable GL_TEXTURE_2D;
1213 glEnable GL_BLEND;
1214 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1215 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1824 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1216 1825 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1217 my $sw4 = ($sw + 3) & ~3;
1218 my $darkness = "\x00" x ($sw4 * $sh);
1219
1220 for my $x (0 .. $sw - 1) {
1221 my $row = $map->[$x + $xofs];
1222 for my $y (0 .. $sh - 1) {
1223
1224 my $cell = $row->[$y + $yofs]
1225 or next;
1226
1227 my $dark = $cell->[0];
1228 if ($dark < 0) {
1229 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1230 } else {
1231 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1232 }
1233
1234 for my $num (grep $_, @$cell[1,2,3]) {
1235 my $tex = $::CONN->{face}[$num]{texture} || next;
1236
1237 my ($w, $h) = @$tex{qw(w h)};
1238
1239 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1240 }
1241 }
1242 }
1243
1244# if (1) { # higher quality darkness
1245# $lighting =~ s/(.)/$1$1$1/gs;
1246# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1247#
1248# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1249#
1250# $lighting = $pb->get_pixels;
1251# $lighting =~ s/(.)../$1/gs;
1252# }
1253
1254 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1255
1256 $darkness = new CFClient::Texture
1257 w => $sw4,
1258 h => $sh,
1259 data => $darkness,
1260 internalformat => GL_ALPHA,
1261 format => GL_ALPHA;
1262
1263 glColor 0.45, 0.45, 0.45, 1;
1264 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1265
1266 glDisable GL_TEXTURE_2D; 1826 glDisable GL_TEXTURE_2D;
1267 glDisable GL_BLEND;
1268
1269 glEndList;
1270 } 1827 }
1271 1828
1272 glCallList $self->{list}; 1829 $self->{children}[1]->draw;
1273}
1274 1830
1275my %DIR = (
1276 SDLK_KP8, [1, "north"],
1277 SDLK_KP9, [2, "northeast"],
1278 SDLK_KP6, [3, "east"],
1279 SDLK_KP3, [4, "southeast"],
1280 SDLK_KP2, [5, "south"],
1281 SDLK_KP1, [6, "southwest"],
1282 SDLK_KP4, [7, "west"],
1283 SDLK_KP7, [8, "northwest"],
1284
1285 SDLK_UP, [1, "north"],
1286 SDLK_RIGHT, [3, "east"],
1287 SDLK_DOWN, [5, "south"],
1288 SDLK_LEFT, [7, "west"],
1289);
1290
1291sub key_down {
1292 my ($self, $ev) = @_;
1293
1294 my $mod = $ev->key_mod;
1295 my $sym = $ev->key_sym;
1296
1297 if ($sym == SDLK_KP5) {
1298 $::CONN->send ("command stay fire");
1299 } elsif (exists $DIR{$sym}) {
1300 if ($mod & KMOD_SHIFT) {
1301 $self->{shft}++;
1302 $::CONN->send ("command fire $DIR{$sym}[0]");
1303 } elsif ($mod & KMOD_CTRL) {
1304 $self->{ctrl}++;
1305 $::CONN->send ("command run $DIR{$sym}[0]");
1306 } else {
1307 $::CONN->send ("command $DIR{$sym}[1]");
1308 }
1309 }
1310}
1311
1312sub key_up {
1313 my ($self, $ev) = @_;
1314
1315 my $mod = $ev->key_mod;
1316 my $sym = $ev->key_sym;
1317
1318 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1319 $::CONN->send ("command fire_stop");
1320 }
1321 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1322 $::CONN->send ("command run_stop");
1323 }
1324} 1831}
1325 1832
1326############################################################################# 1833#############################################################################
1327 1834
1328package CFClient::UI::Animator; 1835package CFClient::UI::Animator;
1329 1836
1330use SDL::OpenGL; 1837use CFClient::OpenGL;
1331 1838
1332our @ISA = CFClient::UI::Bin::; 1839our @ISA = CFClient::UI::Bin::;
1333 1840
1334sub moveto { 1841sub moveto {
1335 my ($self, $x, $y) = @_; 1842 my ($self, $x, $y) = @_;
1365 glPopMatrix; 1872 glPopMatrix;
1366} 1873}
1367 1874
1368############################################################################# 1875#############################################################################
1369 1876
1370package 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;
1371 1917
1372our @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}
1373 1927
1374sub size_request { 1928sub size_request {
1375 ($::WIDTH, $::HEIGHT) 1929 ($::WIDTH, $::HEIGHT)
1376} 1930}
1377 1931
1378sub size_allocate { 1932sub configure {
1379 my ($self, $x, $y, $w, $h) = @_; 1933 my ($self, $x, $y, $w, $h) = @_;
1380 1934
1381 $self->_size_allocate ($x, $y, $w, $h); 1935 $self->SUPER::configure ($x, $y, $w, $h);
1382 1936
1383 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1384 for @{$self->{children}}; 1938 for @{$self->{children}};
1385} 1939}
1386 1940
1387sub translate { 1941sub _topleft {
1388 my ($self, $x, $y) = @_; 1942 my ($self, $x, $y) = @_;
1389 1943
1390 ($x, $y) 1944 ($x, $y)
1391} 1945}
1392 1946
1393sub update { 1947sub update {
1394 my ($self) = @_; 1948 my ($self) = @_;
1395 1949
1396 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1950 $self->check_size;
1397 ::refresh (); 1951 ::refresh ();
1398} 1952}
1399 1953
1400sub add { 1954sub add {
1401 my ($self, $widget) = @_; 1955 my ($self, $child) = @_;
1402 1956
1403 $self->SUPER::add ($widget); 1957 $self->SUPER::add ($child);
1958}
1404 1959
1405 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 1960sub on_refresh {
1961 my ($self, $id, $cb) = @_;
1962
1963 $self->{refresh_hook}{$id} = $cb;
1406} 1964}
1407 1965
1408sub draw { 1966sub draw {
1409 my ($self) = @_; 1967 my ($self) = @_;
1410 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
1411 $self->_draw; 1983 $self->_draw;
1412} 1984}
1413 1985
1414############################################################################# 1986#############################################################################
1415 1987
1416package CFClient::UI; 1988package CFClient::UI;
1417 1989
1418$TOPLEVEL = new CFClient::UI::Toplevel; 1990$ROOT = new CFClient::UI::Root;
1419 1991
14201 19921
1421 1993

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines