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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines