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.69 by root, Tue Apr 11 17:32:14 2006 UTC vs.
Revision 1.140 by root, Thu Apr 20 04:20:52 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines