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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines