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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines