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.150 by elmex, Sun Apr 23 00:08:29 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines