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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines