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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines