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.55 by root, Mon Apr 10 19:34:04 2006 UTC vs.
Revision 1.78 by root, Tue Apr 11 22:49:13 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6use List::Util ();
6 7
7use SDL::OpenGL; 8use CFClient;
8use SDL::OpenGL::Constants;
9
10use Crossfire::Client;
11 9
12our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
13 11
14our $TOPLEVEL; 12our $TOPLEVEL;
15our $BUTTON_STATE; 13our $BUTTON_STATE;
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) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->translate ($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->motion_x, $ev->motion_y);
45 43
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47 45
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover;
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER;
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71} 71}
72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
78
79use SDL::OpenGL;
72 80
73sub new { 81sub new {
74 my $class = shift; 82 my $class = shift;
75 83
76 bless { @_ }, $class 84 bless {
85 x => 0,
86 y => 0,
87 z => 0,
88 w => -1,
89 h => -1,
90 @_
91 }, $class
77} 92}
78 93
79sub move { 94sub move {
80 my ($self, $x, $y, $z) = @_; 95 my ($self, $x, $y, $z) = @_;
81 $self->{x} = $x; 96 $self->{x} = $x;
90sub size_request { 105sub size_request {
91 require Carp; 106 require Carp;
92 Carp::confess "size_request is abtract"; 107 Carp::confess "size_request is abtract";
93} 108}
94 109
95sub size_allocate { 110sub _size_allocate {
96 my ($self, $w, $h) = @_; 111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
97 117
98 $self->{w} = $w; 118 $self->{w} = $w;
99 $self->{h} = $h; 119 $self->{h} = $h;
120
121 1
122}
123
124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
128}
129
130# translate global koordinates to local coordinate system
131sub translate {
132 my ($self, $x, $y) = @_;
133
134 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
100} 135}
101 136
102sub focus_in { 137sub focus_in {
103 my ($self) = @_; 138 my ($self) = @_;
139
140 return if $FOCUS == $self;
104 141
105 my $focus = $FOCUS; $FOCUS = $self; 142 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus; 143 $focus->update if $focus;
107 $FOCUS->update; 144 $FOCUS->update;
108} 145}
116 $focus->update if $focus; #? 153 $focus->update if $focus; #?
117} 154}
118 155
119sub mouse_motion { } 156sub mouse_motion { }
120sub button_up { } 157sub button_up { }
121sub button_down { }
122sub key_down { } 158sub key_down { }
123sub key_up { } 159sub key_up { }
160
161sub button_down {
162 my ($self, $ev, $x, $y) = @_;
163
164 $self->focus_in;
165}
124 166
125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 167sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 168sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 169sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 170sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 171sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
130 172
131sub draw { 173sub draw {
132 my ($self) = @_; 174 my ($self) = @_;
133 175
176 return unless $self->{h} && $self->{w};
177
134 glPushMatrix; 178 glPushMatrix;
135 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
136 $self->_draw; 180 $self->_draw;
181 glPopMatrix;
182
137 if ($self == $HOVER) { 183 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4; 184 my ($x, $y) = @$self{qw(x y)};
185
186 glColor 0, 0, 1, 0.2;
139 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
140 glBegin GL_QUADS; 189 glBegin GL_QUADS;
141 glVertex 0 , 0; 190 glVertex $x , $y;
142 glVertex $self->{w}, 0; 191 glVertex $x + $self->{w}, $y;
143 glVertex $self->{w}, $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
144 glVertex 0 , $self->{h}; 193 glVertex $x , $y + $self->{h};
145 glEnd; 194 glEnd;
146 glDisable GL_BLEND; 195 glDisable GL_BLEND;
147 } 196 }
148 glPopMatrix;
149} 197}
150 198
151sub _draw { 199sub _draw {
152 my ($self) = @_; 200 my ($self) = @_;
153 201
154 warn "no draw defined for $self\n"; 202 warn "no draw defined for $self\n";
155}
156
157sub bbox {
158 my ($self) = @_;
159 my ($w, $h) = $self->size_request;
160 (
161 $self->{x},
162 $self->{y},
163 $self->{x} = $w,
164 $self->{y} = $h
165 )
166} 203}
167 204
168sub find_widget { 205sub find_widget {
169 my ($self, $x, $y) = @_; 206 my ($self, $x, $y) = @_;
170 207
173 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 210 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
174 211
175 () 212 ()
176} 213}
177 214
178sub del_parent { $_[0]->{parent} = undef }
179
180sub set_parent { 215sub set_parent {
181 my ($self, $par) = @_; 216 my ($self, $par) = @_;
182 217
183 $self->{parent} = $par; 218 $self->{parent} = $par;
184 Scalar::Util::weaken $self->{parent}; 219 Scalar::Util::weaken $self->{parent};
193 228
194 $self->{parent}->update 229 $self->{parent}->update
195 if $self->{parent}; 230 if $self->{parent};
196} 231}
197 232
233sub connect {
234 my ($self, $signal, $cb) = @_;
235
236 push @{ $self->{cb}{$signal} }, $cb;
237}
238
239sub emit {
240 my ($self, $signal, @args) = @_;
241
242 $_->($self, @args)
243 for @{$self->{cb}{$signal} || []};
244}
245
198sub DESTROY { 246sub DESTROY {
199 my ($self) = @_; 247 my ($self) = @_;
200 248
201 #$self->deactivate; 249 #$self->deactivate;
202} 250}
203 251
204############################################################################# 252#############################################################################
205 253
206package Crossfire::Client::Widget::Container; 254package CFClient::UI::DrawBG;
207 255
208our @ISA = Crossfire::Client::Widget::; 256our @ISA = CFClient::UI::Base::;
257
258use strict;
259use SDL::OpenGL;
209 260
210sub new { 261sub new {
262 my $class = shift;
263
264 # range [value, low, high, page]
265
266 $class->SUPER::new (
267 bg => [0, 0, 0, 0.2],
268 active_bg => [1, 1, 1],
269 @_
270 )
271}
272
273sub _draw {
274 my ($self) = @_;
275
276 my ($w, $h) = @$self{qw(w h)};
277
278 glEnable GL_BLEND;
279 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
280 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
281
282 glBegin GL_QUADS;
283 glVertex 0 , 0;
284 glVertex 0 , $h;
285 glVertex $w, $h;
286 glVertex $w, 0;
287 glEnd;
288
289 glDisable GL_BLEND;
290}
291
292#############################################################################
293
294package CFClient::UI::Empty;
295
296our @ISA = CFClient::UI::Base::;
297
298sub size_request {
299 (0, 0)
300}
301
302sub draw { }
303
304#############################################################################
305
306package CFClient::UI::Container;
307
308our @ISA = CFClient::UI::Base::;
309
310sub new {
211 my ($class, @widgets) = @_; 311 my ($class, %arg) = @_;
212 312
313 my $children = delete $arg{children} || [];
314
213 my $self = $class->SUPER::new (children => []); 315 my $self = $class->SUPER::new (children => [], %arg);
214 $self->add ($_) for @widgets; 316 $self->add ($_) for @$children;
215 317
216 $self 318 $self
217} 319}
218 320
219sub add { 321sub add {
220 my ($self, $chld, $expand) = @_; 322 my ($self, $chld) = @_;
221 323
222 $chld->{expand} = $expand;
223 $chld->set_parent ($self); 324 $chld->set_parent ($self);
224 325
225 @{$self->{children}} = 326 $self->{children} = [
226 sort { $a->{z} <=> $b->{z} } 327 sort { $a->{z} <=> $b->{z} }
227 @{$self->{children}}, $chld; 328 @{$self->{children}}, $chld
329 ];
228 330
229 $self->size_allocate ($self->{w}, $self->{h}) 331 $self->{w} = $self->{h} = -1;
230 if $self->{w}; #TODO: check for "realised state" 332 $self->update;
231} 333}
232 334
233sub remove { 335sub remove {
234 my ($self, $widget) = @_; 336 my ($self, $widget) = @_;
235 337
236 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 338 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
237 339
238 $self->size_allocate ($self->{w}, $self->{h}); 340 $self->size_allocate (0, 0, $self->{w}, $self->{h});
239} 341}
240 342
241sub find_widget { 343sub find_widget {
242 my ($self, $x, $y) = @_; 344 my ($self, $x, $y) = @_;
243 345
260 $_->draw for @{$self->{children}}; 362 $_->draw for @{$self->{children}};
261} 363}
262 364
263############################################################################# 365#############################################################################
264 366
265package Crossfire::Client::Widget::Bin; 367package CFClient::UI::Bin;
266 368
267our @ISA = Crossfire::Client::Widget::Container::; 369our @ISA = CFClient::UI::Container::;
370
371sub new {
372 my ($class, %arg) = @_;
373
374 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
375
376 $class->SUPER::new (children => [$child], %arg)
377}
378
379sub add {
380 my ($self, $widget) = @_;
381
382 $self->{children} = [];
383
384 $self->SUPER::add ($widget);
385}
386
387sub remove {
388 my ($self, $widget) = @_;
389
390 $self->SUPER::remove ($widget);
391
392 $self->{children} = [new CFClient::UI::Empty]
393 unless @{$self->{children}};
394}
268 395
269sub child { $_[0]->{children}[0] } 396sub child { $_[0]->{children}[0] }
270 397
271sub size_request { 398sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 399 $_[0]{children}[0]->size_request
273} 400}
274 401
275sub size_allocate { 402sub size_allocate {
276 my ($self, $w, $h) = @_; 403 my ($self, $x, $y, $w, $h) = @_;
277 404
278 $self->SUPER::size_allocate ($w, $h); 405 $self->_size_allocate ($x, $y, $w, $h) or return;
406
279 $self->{children}[0]->size_allocate ($w, $h) 407 $self->{children}[0]->size_allocate (0, 0, $w, $h);
280 if $self->{children}[0]
281} 408}
282 409
283############################################################################# 410#############################################################################
284 411
285package Crossfire::Client::Widget::Window; 412package CFClient::UI::Window;
286 413
287our @ISA = Crossfire::Client::Widget::Bin::; 414our @ISA = CFClient::UI::Bin::;
288 415
289use SDL::OpenGL; 416use SDL::OpenGL;
290 417
291sub new { 418sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_; 419 my ($class, %arg) = @_;
293 420
294 my $self = $class->SUPER::new; 421 my $self = $class->SUPER::new (%arg);
295
296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
297} 422}
298 423
299sub update { 424sub update {
300 my ($self) = @_; 425 my ($self) = @_;
301 426
427 # we want to do this delayed...
302 $self->render_chld; 428 $self->render_chld;
303 $self->SUPER::update; 429 $self->SUPER::update;
304} 430}
305 431
306sub render_chld { 432sub render_chld {
307 my ($self) = @_; 433 my ($self) = @_;
308 434
309 $self->{texture} = 435 $self->{texture} =
310 Crossfire::Client::Texture->new_from_opengl ( 436 CFClient::Texture->new_from_opengl (
311 $self->{w}, $self->{h}, sub { $self->child->draw } 437 $self->{w}, $self->{h}, sub { $self->child->draw }
312 ); 438 );
313} 439}
314 440
315sub size_allocate { 441sub size_allocate {
316 my ($self, $w, $h) = @_; 442 my ($self, $x, $y, $w, $h) = @_;
317 443
318 $self->{w} = $w; 444 $self->_size_allocate ($x, $y, $w, $h) or return;
319 $self->{h} = $h;
320 445
321 $self->child->size_allocate ($w, $h); 446 $self->child->size_allocate (0, 0, $w, $h);
322 447
323 $self->render_chld; 448 $self->render_chld;
324} 449}
325 450
326sub _draw { 451sub _draw {
330 455
331 my $tex = $self->{texture} 456 my $tex = $self->{texture}
332 or return; 457 or return;
333 458
334 glEnable GL_BLEND; 459 glEnable GL_BLEND;
460 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
335 glEnable GL_TEXTURE_2D; 461 glEnable GL_TEXTURE_2D;
336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 462 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
337 glBindTexture GL_TEXTURE_2D, $tex->{name};
338 463
339 glBegin GL_QUADS; 464 $tex->draw_quad (0, 0, $w, $h);
340 glTexCoord 0, 0; glVertex 0, 0;
341 glTexCoord 0, 1; glVertex 0, $h;
342 glTexCoord 1, 1; glVertex $w, $h;
343 glTexCoord 1, 0; glVertex $w, 0;
344 glEnd;
345 465
346 glDisable GL_BLEND; 466 glDisable GL_BLEND;
347 glDisable GL_TEXTURE_2D; 467 glDisable GL_TEXTURE_2D;
348} 468}
349 469
350############################################################################# 470#############################################################################
351 471
352package Crossfire::Client::Widget::Frame; 472package CFClient::UI::Frame;
353 473
354our @ISA = Crossfire::Client::Widget::Bin::; 474our @ISA = CFClient::UI::Bin::;
355 475
356use SDL::OpenGL; 476use SDL::OpenGL;
357 477
358sub size_request { 478sub size_request {
359 my ($self) = @_; 479 my ($self) = @_;
364 484
365 map { $_ + 4 } $chld->size_request; 485 map { $_ + 4 } $chld->size_request;
366} 486}
367 487
368sub size_allocate { 488sub size_allocate {
369 my ($self, $w, $h) = @_; 489 my ($self, $x, $y, $w, $h) = @_;
370 490
371 $self->{w} = $w; 491 $self->_size_allocate ($x, $y, $w, $h) or return;
372 $self->{h} = $h;
373 492
374 $self->child->size_allocate ($w - 4, $h - 4); 493 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
375 $self->child->move (2, 2);
376} 494}
377 495
378sub _draw { 496sub _draw {
379 my ($self) = @_; 497 my ($self) = @_;
380 498
382 500
383 my ($w, $h) = $chld->size_request; 501 my ($w, $h) = $chld->size_request;
384 502
385 glBegin GL_QUADS; 503 glBegin GL_QUADS;
386 glColor 0, 0, 0; 504 glColor 0, 0, 0;
387 glTexCoord 0, 0; glVertex 0 , 0; 505 glVertex 0 , 0;
388 glTexCoord 0, 1; glVertex 0 , $h + 4; 506 glVertex 0 , $h + 4;
389 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 507 glVertex $w + 4 , $h + 4;
390 glTexCoord 1, 0; glVertex $w + 4 , 0; 508 glVertex $w + 4 , 0;
391 glEnd; 509 glEnd;
392 510
393 $chld->draw; 511 $chld->draw;
394} 512}
395 513
396############################################################################# 514#############################################################################
397 515
398package Crossfire::Client::Widget::FancyFrame; 516package CFClient::UI::FancyFrame;
399 517
400our @ISA = Crossfire::Client::Widget::Bin::; 518our @ISA = CFClient::UI::Bin::;
401 519
402use SDL::OpenGL; 520use SDL::OpenGL;
403 521
404my @tex = 522my @tex =
405 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 523 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
406 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 524 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
407 525
408sub size_request { 526sub size_request {
409 my ($self) = @_; 527 my ($self) = @_;
410 528
529 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
530
411 my ($w, $h) = $self->SUPER::size_request; 531 my ($w, $h) = $self->SUPER::size_request;
412 532
413 $h += $tex[1]->{height}; 533 $h += $tex[1]->{h};
414 $h += $tex[4]->{height}; 534 $h += $tex[4]->{h};
415 $w += $tex[2]->{width}; 535 $w += $tex[2]->{w};
416 $w += $tex[3]->{width}; 536 $w += $tex[3]->{w};
417 537
418 ($w, $h) 538 ($w, $h)
419} 539}
420 540
421sub size_allocate { 541sub size_allocate {
422 my ($self, $w, $h) = @_; 542 my ($self, $x, $y, $w, $h) = @_;
423 543
424 $self->SUPER::size_allocate ($w, $h); 544 $self->_size_allocate ($x, $y, $w, $h) or return;
425 545
426 $h -= $tex[1]->{height}; 546 $h -= $tex[1]{h};
427 $h -= $tex[4]->{height}; 547 $h -= $tex[4]{h};
428 $w -= $tex[2]->{width}; 548 $w -= $tex[2]{w};
429 $w -= $tex[3]->{width}; 549 $w -= $tex[3]{w};
430 550
431 $h = $h < 0 ? 0 : $h; 551 $h = $h < 0 ? 0 : $h;
432 $w = $w < 0 ? 0 : $w; 552 $w = $w < 0 ? 0 : $w;
433 553
434 $self->child->size_allocate ($w, $h); 554 my $child = $self->child;
435 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 555
556 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
557}
558
559sub button_down {
560 my ($self, $ev, $x, $y) = @_;
561
562 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
563 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
564
565 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
566 my ($bw, $bh) = ($self->{w}, $self->{h});
567
568 $self->{motion} = sub {
569 my ($ev, $x, $y) = @_;
570
571 ($x, $y) = ($ev->motion_x, $ev->motion_y);
572
573 $self->{user_w} = $bw + $x - $ox;
574 $self->{user_h} = $bh + $y - $oy;
575 $self->update;
576 };
577
578 } elsif ($x >= 0 && $x < $self->{w}
579 && $y >= 0 && $y < $tex[1]{h}) {
580
581 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
582 my ($bx, $by) = ($self->{x}, $self->{y});
583
584 $self->{motion} = sub {
585 my ($ev, $x, $y) = @_;
586
587 ($x, $y) = ($ev->motion_x, $ev->motion_y);
588
589 $self->move ($bx + $x - $ox, $by + $y - $oy);
590 $self->update;
591 };
592 }
593}
594
595sub button_up {
596 my ($self, $ev, $x, $y) = @_;
597
598 delete $self->{motion};
599}
600
601sub mouse_motion {
602 my ($self, $ev, $x, $y) = @_;
603
604 $self->{motion}->($ev, $x, $y) if $self->{motion};
436} 605}
437 606
438sub _draw { 607sub _draw {
439 my ($self) = @_; 608 my ($self) = @_;
440 609
441 my ($w, $h) = ($self->{w}, $self->{h}); 610 my ($w, $h) = ($self->{w}, $self->{h});
442 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 611 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
443 612
444 glEnable GL_BLEND; 613 glEnable GL_BLEND;
614 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
445 glEnable GL_TEXTURE_2D; 615 glEnable GL_TEXTURE_2D;
446 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 616 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
448 617
449 my $top = $tex[1]; 618 my $top = $tex[1];
450 glBindTexture GL_TEXTURE_2D, $top->{name}; 619 $top->draw_quad (0, 0, $w, $top->{h});
451
452 glBegin GL_QUADS;
453 glTexCoord 0, 0; glVertex 0 , 0;
454 glTexCoord 0, 1; glVertex 0 , $top->{height};
455 glTexCoord 1, 1; glVertex $w , $top->{height};
456 glTexCoord 1, 0; glVertex $w , 0;
457 glEnd;
458 620
459 my $left = $tex[3]; 621 my $left = $tex[3];
460 glBindTexture GL_TEXTURE_2D, $left->{name}; 622 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
461
462 glBegin GL_QUADS;
463 glTexCoord 0, 0; glVertex 0 , $top->{height};
464 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
465 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
466 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
467 glEnd;
468 623
469 my $right = $tex[2]; 624 my $right = $tex[2];
470 glBindTexture GL_TEXTURE_2D, $right->{name}; 625 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
471
472 glBegin GL_QUADS;
473 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
474 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
475 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
476 glTexCoord 1, 0; glVertex $w , $top->{height};
477 glEnd;
478 626
479 my $bottom = $tex[4]; 627 my $bottom = $tex[4];
480 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 628 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
481
482 glBegin GL_QUADS;
483 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
484 glTexCoord 0, 1; glVertex 0 , $h;
485 glTexCoord 1, 1; glVertex $w , $h;
486 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
487 glEnd;
488 629
489 my $bg = $tex[0]; 630 my $bg = $tex[0];
631
490 glBindTexture GL_TEXTURE_2D, $bg->{name}; 632 glBindTexture GL_TEXTURE_2D, $bg->{name};
491 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 633 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
492 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 634 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
493 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 635 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
494 636
495 my $rep_x = $cw / $bg->{width}; 637 my $rep_x = $cw / $bg->{w};
496 my $rep_y = $ch / $bg->{height}; 638 my $rep_y = $ch / $bg->{h};
497 639
498 glBegin GL_QUADS; 640 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
499 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
500 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
501 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
502 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
503 glEnd;
504 641
642 glDisable GL_TEXTURE_2D;
505 glDisable GL_BLEND; 643 glDisable GL_BLEND;
506 glDisable GL_TEXTURE_2D;
507 644
508 $self->child->draw; 645 $self->child->draw;
509 646
510} 647}
511 648
512############################################################################# 649#############################################################################
513 650
514package Crossfire::Client::Widget::Table; 651package CFClient::UI::Table;
515 652
516our @ISA = Crossfire::Client::Widget::Bin::; 653our @ISA = CFClient::UI::Base::;
654
655use List::Util qw(max sum);
517 656
518use SDL::OpenGL; 657use SDL::OpenGL;
658
659sub new {
660 my $class = shift;
661
662 $class->SUPER::new (
663 col_expand => [],
664 @_
665 )
666}
519 667
520sub add { 668sub add {
521 my ($self, $x, $y, $chld) = @_; 669 my ($self, $x, $y, $chld) = @_;
522 my $old_chld = $self->{children}[$y][$x];
523 670
524 $self->{children}[$y][$x] = $chld; 671 $self->{children}[$y][$x] = $chld;
525 $chld->set_parent ($self); 672 $chld->set_parent ($self);
673
674 $self->{w} = $self->{h} = -1;
526 $self->update; 675 $self->update;
527} 676}
528 677
529sub max_row_height { 678sub get_wh {
530 my ($self, $row) = @_; 679 my ($self) = @_;
531 680
532 my $hs = 0; 681 my (@w, @h);
533 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 682
683 for my $y (0 .. $#{$self->{children}}) {
534 my $c = $self->{children}->[$row]->[$xi]; 684 my $row = $self->{children}[$y]
535 if ($c) { 685 or next;
686
687 for my $x (0 .. $#$row) {
688 my $widget = $row->[$x]
689 or next;
536 my ($w, $h) = $c->size_request; 690 my ($w, $h) = $widget->size_request;
537 if ($hs < $h) { $hs = $h } 691
692 $w[$x] = max $w[$x], $w;
693 $h[$y] = max $h[$y], $h;
538 } 694 }
539 } 695 }
540 return $hs;
541}
542 696
543sub max_col_width { 697 (\@w, \@h)
698}
699
700sub size_request {
544 my ($self, $col) = @_; 701 my ($self) = @_;
545 702
703 my ($ws, $hs) = $self->get_wh;
704
705 (
706 (sum @$ws),
707 (sum @$hs),
708 )
709}
710
711sub size_allocate {
712 my ($self, $x, $y, $w, $h) = @_;
713
714 $self->_size_allocate ($x, $y, $w, $h) or return;
715
716 my ($ws, $hs) = $self->get_wh;
717
718 my $req_w = sum @$ws;
719 my $req_h = sum @$hs;
720
721 # TODO: nicer code && do row_expand
722 my @col_expand = @{$self->{col_expand}};
723 @col_expand = (1) x @$ws unless @col_expand;
724 my $col_expand = (sum @col_expand) || 1;
725
726 # linearly scale sizes
727 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
728 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
729
730 my $y;
731
732 for my $r (0 .. $#{$self->{children}}) {
733 my $row = $self->{children}[$r]
734 or next;
735
546 my $ws = 0; 736 my $x = 0;
547 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 737 my $row_h = $hs->[$r];
548 my $c = ($self->{children}->[$yi] || [])->[$col]; 738
549 if ($c) { 739 for my $c (0 .. $#$row) {
550 my ($w, $h) = $c->size_request; 740 my $widget = $row->[$c]
551 if ($ws < $w) { $ws = $w } 741 or next;
742
743 my $col_w = $ws->[$c];
744
745 $widget->size_allocate ($x, $y, $col_w, $row_h);
746
747 $x += $col_w;
552 } 748 }
749
750 $y += $row_h;
553 } 751 }
554 return $ws; 752
555} 753}
754
755sub find_widget {
756 my ($self, $x, $y) = @_;
757
758 $x -= $self->{x};
759 $y -= $self->{y};
760
761 my $res;
762
763 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
764 $res = $_->find_widget ($x, $y)
765 and return $res;
766 }
767
768 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
769}
770
771sub _draw {
772 my ($self) = @_;
773
774 for (grep $_, @{$self->{children}}) {
775 $_->draw for grep $_, @$_;
776 }
777}
778
779#############################################################################
780
781package CFClient::UI::HBox;
782
783# TODO: wrap into common Box base class
784
785our @ISA = CFClient::UI::Container::;
556 786
557sub size_request { 787sub size_request {
558 my ($self) = @_; 788 my ($self) = @_;
559 789
790 my @alloc = map [$_->size_request], @{$self->{children}};
791
792 (
793 (List::Util::sum map $_->[0], @alloc),
794 (List::Util::max map $_->[1], @alloc),
795 )
796}
797
798sub size_allocate {
799 my ($self, $x, $y, $w, $h) = @_;
800
801 $self->_size_allocate ($x, $y, $w, $h) or return;
802
803 return unless $self->{w};
804
560 my ($hs, $ws) = (0, 0); 805 ($h, $w) = ($w, $h);
561 806
562 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 807 my $children = $self->{children};
563 $hs += $self->max_row_height ($yi);
564 }
565 808
566 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 809 my @h = map +($_->size_request)[0], @$children;
567 my $wm = 0; 810
568 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 811 my $req_h = List::Util::sum @h;
569 $wm += $self->max_col_width ($xi) 812
813 if ($req_h > $h) {
814 # ah well, not enough space
815 $_ *= $h / $req_h for @h;
816 } else {
817 my $exp = List::Util::sum map $_->{expand}, @$children;
818 $exp ||= 1;
819
820 for (0 .. $#$children) {
821 my $child = $children->[$_];
822
823 my $alloc_h = $h[$_];
824 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
825 $h[$_] = $alloc_h;
570 } 826 }
571 if ($ws < $wm) { $ws = $wm }
572 } 827 }
573
574 return ($ws, $hs);
575}
576
577sub _draw {
578 my ($self) = @_;
579 828
580 my $y = 0; 829 my $y = 0;
581 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 830 for (0 .. $#$children) {
831 my $child = $children->[$_];
582 my $x = 0; 832 my $h = $h[$_];
833 $child->size_allocate ($y, 0, $h, $w);
583 834
584 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 835 $y += $h;
585
586 my $c = $self->{children}->[$yi]->[$xi];
587 if ($c) {
588 $c->move ($x, $y, 0); #TODO: Move to size_request
589 $c->draw if $c;
590 }
591
592 $x += $self->max_col_width ($xi);
593 }
594
595 $y += $self->max_row_height ($yi);
596 } 836 }
597} 837}
598 838
599############################################################################# 839#############################################################################
600 840
601package Crossfire::Client::Widget::VBox; 841package CFClient::UI::VBox;
602 842
843# TODO: wrap into common Box base class
844
603our @ISA = Crossfire::Client::Widget::Container::; 845our @ISA = CFClient::UI::Container::;
604
605use SDL::OpenGL;
606 846
607sub size_request { 847sub size_request {
608 my ($self) = @_; 848 my ($self) = @_;
609 849
610 my @alloc = map [$_->size_request], @{$self->{children}}; 850 my @alloc = map [$_->size_request], @{$self->{children}};
614 (List::Util::sum map $_->[1], @alloc), 854 (List::Util::sum map $_->[1], @alloc),
615 ) 855 )
616} 856}
617 857
618sub size_allocate { 858sub size_allocate {
619 my ($self, $w, $h) = @_; 859 my ($self, $x, $y, $w, $h) = @_;
620 860
621 $self->w ($w); 861 $self->_size_allocate ($x, $y, $w, $h) or return;
622 $self->h ($h);
623 862
624 my $exp; 863 return unless $self->{h};
625 my @oth; 864
626 # find expand widget 865 my $children = $self->{children};
627 for (@{$self->{children}}) { 866
628 if ($_->{expand}) { 867 my @h = map +($_->size_request)[1], @$children;
868
869 my $req_h = List::Util::sum @h;
870
871 if ($req_h > $h) {
872 # ah well, not enough space
873 $_ *= $h / $req_h for @h;
874 } else {
875 my $exp = List::Util::sum map $_->{expand}, @$children;
629 $exp = $_; 876 $exp ||= 1;
630 last; 877
878 for (0 .. $#$children) {
879 my $child = $children->[$_];
880
881 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
631 } 882 }
632 push @oth, $_;
633 }
634
635 my ($ow, $oh);
636
637 # get sizes of other widgets
638 for (@oth) {
639 my ($w, $h) = $_->size_request;
640 $oh += $h;
641 if ($ow < $w) { $ow = $w }
642 } 883 }
643 884
644 my $y = 0; 885 my $y = 0;
645 for (@{$self->{children}}) { 886 for (0 .. $#$children) {
646 $_->move (0, $y); 887 my $child = $children->[$_];
647 888 my $h = $h[$_];
648 if ($_ == $exp) {
649 $_->size_allocate ($w, $h - $oh);
650 $y += $h - $oh;
651 } else {
652 my ($cw, $h) = $_->size_request;
653 $_->size_allocate ($w, $h); 889 $child->size_allocate (0, $y, $w, $h);
890
654 $y += $h; 891 $y += $h;
655 }
656 } 892 }
657} 893}
658 894
659############################################################################# 895#############################################################################
660 896
661package Crossfire::Client::Widget::Label; 897package CFClient::UI::Label;
662 898
663our @ISA = Crossfire::Client::Widget::; 899our @ISA = CFClient::UI::Base::;
664 900
665use SDL::OpenGL; 901use SDL::OpenGL;
666 902
667sub new { 903sub new {
668 my ($class, $x, $y, $z, $height, $text) = @_; 904 my ($class, %arg) = @_;
669 905
906 my $self = $class->SUPER::new (
907 fg => [1, 1, 1],
670 $height ||= $::FONTSIZE; 908 height => $::FONTSIZE,
909 text => "",
910 align => -1,
911 padding => 2,
912 layout => new CFClient::Layout,
913 %arg
914 );
671 915
672 # TODO: color, and make height, xyz etc. optional
673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
674
675 $self->set_text ($text); 916 $self->set_text ($self->{text});
676 917
677 $self 918 $self
919}
920
921sub escape_text {
922 local $_ = $_[1];
923
924 s/&/&amp;/g;
925 s/>/&gt;/g;
926 s/</&lt;/g;
927
928 $_[1]
678} 929}
679 930
680sub set_text { 931sub set_text {
681 my ($self, $text) = @_; 932 my ($self, $text) = @_;
682 933
683 $self->{text} = $text; 934 $self->{text} = $text;
684 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 935 $self->{layout}->set_markup ($text);
685 936
937 delete $self->{texture};
686 $self->update; 938 $self->update;
687} 939}
688 940
689sub get_text { 941sub get_text {
690 my ($self, $text) = @_; 942 my ($self, $text) = @_;
693} 945}
694 946
695sub size_request { 947sub size_request {
696 my ($self) = @_; 948 my ($self) = @_;
697 949
698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 950 $self->{layout}->set_width;
699 ( 951 $self->{layout}->set_height ($self->{height});
700 $self->{texture}{width}, 952 my ($w, $h) = $self->{layout}->size;
701 $self->{texture}{height},
702 )
703 } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705 953
706 ($w, $h)
707 } 954 (
955 $w + $self->{padding} * 2,
956 $h + $self->{padding} * 2,
957 )
958}
708 959
960sub size_allocate {
961 my ($self, $x, $y, $w, $h) = @_;
962
963 $self->_size_allocate ($x, $y, $w, $h) or return;
964
965 delete $self->{texture};
966}
967
968sub update {
969 my ($self) = @_;
970
971 delete $self->{texture};
972 $self->SUPER::update;
709} 973}
710 974
711sub _draw { 975sub _draw {
712 my ($self) = @_; 976 my ($self) = @_;
713 977
714 my $tex = $self->{texture}; 978 my $tex = $self->{texture} ||= do {
979 $self->{layout}->set_width ($self->{w});
980 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{height});
981 new_from_layout CFClient::Texture $self->{layout}
982 };
715 983
716 glEnable GL_BLEND; 984 glEnable GL_BLEND;
985 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
717 glEnable GL_TEXTURE_2D; 986 glEnable GL_TEXTURE_2D;
718 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
719 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 987 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
720 glBindTexture GL_TEXTURE_2D, $tex->{name};
721 988
722 glColor 1, 0, 0, 1; # TODO color 989 glColor @{$self->{fg}};
723 990
724 glBegin GL_QUADS; 991 my $x =
725 glTexCoord 0, 0; glVertex 0 , 0; 992 $self->{align} < 0 ? $self->{padding}
726 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 993 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
727 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 994 : ($self->{w} - $tex->{w}) * 0.5;
728 glTexCoord 1, 0; glVertex $tex->{width}, 0;
729 glEnd;
730 995
996 $tex->draw_quad ($x, $self->{padding});
997
998 glDisable GL_TEXTURE_2D;
731 glDisable GL_BLEND; 999 glDisable GL_BLEND;
732 glDisable GL_TEXTURE_2D;
733} 1000}
734 1001
735############################################################################# 1002#############################################################################
736 1003
737package Crossfire::Client::Widget::Entry; 1004package CFClient::UI::Entry;
738 1005
739our @ISA = Crossfire::Client::Widget::Label::; 1006our @ISA = CFClient::UI::Label::;
740 1007
741use SDL; 1008use SDL;
742use SDL::OpenGL; 1009use SDL::OpenGL;
743 1010
1011sub new {
1012 my $class = shift;
1013
1014 $class->SUPER::new (
1015 fg => [1, 1, 1],
1016 bg => [0, 0, 0, 0.2],
1017 active_bg => [1, 1, 1],
1018 active_fg => [0, 0, 0],
1019 @_
1020 )
1021}
1022
1023sub _set_text {
1024 my ($self, $text) = @_;
1025
1026 $self->{last_activity} = $::NOW;
1027
1028 $self->{text} = $text;
1029 $self->{layout}->set_width ($self->{w});
1030
1031 $text =~ s/./*/g if $self->{hidden};
1032
1033 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
1034
1035 $text = substr $text, 0, $self->{cursor};
1036 utf8::encode $text;
1037
1038 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1039}
1040
1041sub size_request {
1042 my ($self) = @_;
1043
1044 my ($w, $h) = $self->SUPER::size_request;
1045
1046 ($w + 1, $h) # add 1 for cursor
1047}
1048
1049sub size_allocate {
1050 my ($self, $x, $y, $w, $h) = @_;
1051
1052 $self->SUPER::size_allocate ($x, $y, $w, $h);
1053
1054 $self->_set_text ($self->{text});
1055}
1056
1057sub set_text {
1058 my ($self, $text) = @_;
1059
1060 $self->{cursor} = length $text;
1061 $self->_set_text ($text);
1062 $self->update;
1063}
1064
744sub key_down { 1065sub key_down {
745 my ($self, $ev) = @_; 1066 my ($self, $ev) = @_;
746 1067
747 my $mod = $ev->key_mod; 1068 my $mod = $ev->key_mod;
748 my $sym = $ev->key_sym; 1069 my $sym = $ev->key_sym;
750 my $uni = $ev->key_unicode; 1071 my $uni = $ev->key_unicode;
751 1072
752 my $text = $self->get_text; 1073 my $text = $self->get_text;
753 1074
754 if ($sym == SDLK_BACKSPACE) { 1075 if ($sym == SDLK_BACKSPACE) {
755 substr $text, -1, 1, ''; 1076 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1077 } elsif ($sym == SDLK_DELETE) {
1078 substr $text, $self->{cursor}, 1, "";
1079 } elsif ($sym == SDLK_LEFT) {
1080 --$self->{cursor} if $self->{cursor};
1081 } elsif ($sym == SDLK_RIGHT) {
1082 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1083 } elsif ($sym == SDLK_HOME) {
1084 $self->{cursor} = 0;
1085 } elsif ($sym == SDLK_END) {
1086 $self->{cursor} = length $text;
756 } elsif ($uni) { 1087 } elsif ($uni) {
757 $text .= chr $uni; 1088 substr $text, $self->{cursor}++, 0, chr $uni;
758 print "$uni <$text>\n";#d#
759 } 1089 }
760 1090
761 $self->set_text ($text); 1091 $self->_set_text ($text);
1092 $self->update;
1093}
1094
1095sub focus_in {
1096 my ($self) = @_;
1097
1098 $self->{last_activity} = $::NOW;
1099
1100 $self->SUPER::focus_in;
762} 1101}
763 1102
764sub button_down { 1103sub button_down {
765 my ($self, $ev) = @_; 1104 my ($self, $ev, $x, $y) = @_;
766 1105
767 $self->focus_in; 1106 $self->SUPER::button_down ($ev, $x, $y);
1107
1108 my $idx = $self->{layout}->xy_to_index ($x, $y);
1109
1110 # byte-index to char-index
1111 my $text = $self->{text};
1112 utf8::encode $text;
1113 $self->{cursor} = length substr $text, 0, $idx;
1114
1115 $self->_set_text ($self->{text});
1116 $self->update;
1117}
1118
1119sub mouse_motion {
1120 my ($self, $ev, $x, $y) = @_;
1121# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
768} 1122}
769 1123
770sub _draw { 1124sub _draw {
771 my ($self) = @_; 1125 my ($self) = @_;
772 1126
1127 local $self->{fg} = $self->{fg};
1128
773 if ($FOCUS == $self) { 1129 if ($FOCUS == $self) {
774 glColor 1, 1, 1; 1130 glColor @{$self->{active_bg}};
1131 $self->{fg} = $self->{active_fg};
775 } else { 1132 } else {
776 glColor 0.7, 0.7, 0.7; 1133 glColor @{$self->{bg}};
777 } 1134 }
778 1135
1136 glEnable GL_BLEND;
1137 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
779 glBegin GL_QUADS; 1138 glBegin GL_QUADS;
780 glVertex 0 , 0; 1139 glVertex 0 , 0;
781 glVertex 0 , $self->{h} - 1; 1140 glVertex 0 , $self->{h};
782 glVertex $self->{w} - 1, $self->{h} - 1; 1141 glVertex $self->{w}, $self->{h};
783 glVertex $self->{w} - 1, 0; 1142 glVertex $self->{w}, 0;
784 glEnd; 1143 glEnd;
1144 glDisable GL_BLEND;
785 1145
786 $self->SUPER::_draw; 1146 $self->SUPER::_draw;
787}
788 1147
789############################################################################# 1148 #TODO: force update every cursor change :(
1149 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1150 glColor @{$self->{fg}};
1151 glBegin GL_LINES;
1152 glVertex $self->{cur_x}, $self->{cur_y};
1153 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1154 glEnd;
1155 }
1156}
790 1157
1158#############################################################################
1159
1160package CFClient::UI::Slider;
1161
1162use strict;
1163
1164use SDL::OpenGL;
1165
1166our @ISA = CFClient::UI::DrawBG::;
1167
1168sub new {
1169 my $class = shift;
1170
1171 # range [value, low, high, page]
1172
1173 my $self = $class->SUPER::new (
1174 fg => [1, 1, 1],
1175 active_fg => [0, 0, 0],
1176 range => [0, 0, 100, 10],
1177 req_w => 40,
1178 req_h => 10,
1179 vertical => 0,
1180 @_
1181 );
1182
1183 $self
1184}
1185
1186sub size_request {
1187 my ($self) = @_;
1188
1189 my $w = $self->{req_w};
1190 my $h = $self->{req_h};
1191
1192 $self->{vertical} ? ($h, $w) : ($w, $h)
1193}
1194
1195sub button_down {
1196 my ($self, $ev, $x, $y) = @_;
1197
1198 $self->SUPER::button_down ($ev, $x, $y);
1199 $self->mouse_motion ($ev, $x, $y);
1200}
1201
1202sub mouse_motion {
1203 my ($self, $ev, $x, $y) = @_;
1204
1205 if ($GRAB == $self) {
1206 my ($value, $lo, $hi, $page) = @{$self->{range}};
1207
1208 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1209
1210 $x = $x * ($hi - $lo) / $w + $lo;
1211 $x = $lo if $x < $lo;
1212 $x = $hi - $page if $x > $hi - $page;
1213 $self->{range}[0] = $x;
1214
1215 $self->emit (changed => $x);
1216 $self->update;
1217 }
1218}
1219
1220sub _draw {
1221 my ($self) = @_;
1222
1223 $self->SUPER::_draw ();
1224
1225 my ($w, $h) = @$self{qw(w h)};
1226
1227 if ($self->{vertical}) {
1228 # draw a vertical slider like a rotated horizontal slider
1229
1230 glRotate 90, 0, 0, 1;
1231 glTranslate 0, -$self->{w}, 0;
1232
1233 ($w, $h) = ($h, $w);
1234 }
1235
1236 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1237 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1238
1239 my ($value, $lo, $hi, $page) = @{$self->{range}};
1240
1241 $page = int $page * $w / ($hi - $lo);
1242 $value = int +($value - $lo) * $w / ($hi - $lo);
1243
1244 $w -= $page;
1245 $page &= ~1;
1246 glTranslate $page * 0.5, 0, 0;
1247
1248 glColor @$fg;
1249 glBegin GL_LINES;
1250 glVertex 0, 0; glVertex 0, $h;
1251 glVertex $w - 1, 0; glVertex $w - 1, $h;
1252 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1253 glEnd;
1254
1255 my $knob_a = $value - $page * 0.5;
1256 my $knob_b = $value + $page * 0.5;
1257
1258 glBegin GL_QUADS;
1259 glColor @$fg;
1260 glVertex $knob_a, 0;
1261 glVertex $knob_a, $h;
1262 glVertex $knob_b, $h;
1263 glVertex $knob_b, 0;
1264
1265 if ($knob_a < $knob_b - 2) {
1266 glColor @$bg;
1267 glVertex $knob_a + 1, 1;
1268 glVertex $knob_a + 1, $h - 1;
1269 glVertex $knob_b - 1, $h - 1;
1270 glVertex $knob_b - 1, 1;
1271 }
1272 glEnd;
1273}
1274
1275#############################################################################
1276
791package Crossfire::Client::Widget::MapWidget; 1277package CFClient::UI::MapWidget;
792 1278
793use strict; 1279use strict;
794 1280
795use List::Util qw(min max); 1281use List::Util qw(min max);
796 1282
797use SDL; 1283use SDL;
798use SDL::OpenGL; 1284use SDL::OpenGL;
799use SDL::OpenGL::Constants;
800 1285
801our @ISA = Crossfire::Client::Widget::; 1286our @ISA = CFClient::UI::Base::;
1287
1288sub new {
1289 my $class = shift;
1290
1291 $class->SUPER::new (
1292 z => -1,
1293 list => (glGenLists 1),
1294 @_
1295 )
1296}
802 1297
803sub key_down { 1298sub key_down {
804 print "MAPKEYDOWN\n"; 1299 print "MAPKEYDOWN\n";
805} 1300}
806 1301
807sub key_up { 1302sub key_up {
808} 1303}
809 1304
810sub size_request { 1305sub size_request {
811 ( 1306 (
812 1 + int $::WIDTH / 32, 1307 1 + 32 * int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32, 1308 1 + 32 * int $::HEIGHT / 32,
814 ) 1309 )
815} 1310}
816 1311
1312sub update {
1313 my ($self) = @_;
1314
1315 $self->{need_update} = 1;
1316 $self->SUPER::update;
1317}
1318
817sub _draw { 1319sub draw {
818 my ($self) = @_; 1320 my ($self) = @_;
819 1321
1322 if (delete $self->{need_update}) {
1323 glNewList $self->{list}, GL_COMPILE;
1324
820 my $mx = $::CONN->{mapx}; 1325 my $mx = $::CONN->{mapx};
821 my $my = $::CONN->{mapy}; 1326 my $my = $::CONN->{mapy};
822 1327
823 my $map = $::CONN->{map}; 1328 my $map = $::CONN->{map};
824 1329
825 my ($xofs, $yofs); 1330 my ($xofs, $yofs);
826 1331
827 my $sw = 1 + int $::WIDTH / 32; 1332 my $sw = 1 + int $::WIDTH / 32;
828 my $sh = 1 + int $::HEIGHT / 32; 1333 my $sh = 1 + int $::HEIGHT / 32;
829 1334
830 if ($::CONN->{mapw} > $sw) { 1335 if ($::CONN->{mapw} > $sw) {
831 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1336 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
832 } else { 1337 } else {
833 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1338 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
834 } 1339 }
835 1340
836 if ($::CONN->{maph} > $sh) { 1341 if ($::CONN->{maph} > $sh) {
837 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1342 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
838 } else { 1343 } else {
839 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1344 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
840 } 1345 }
841 1346
842 glEnable GL_TEXTURE_2D; 1347 glEnable GL_TEXTURE_2D;
843 glEnable GL_BLEND; 1348 glEnable GL_BLEND;
1349 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
844 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1350 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
845 1351
846 my $sw4 = ($sw + 3) & ~3; 1352 my $sw4 = ($sw + 3) & ~3;
847 my $lighting = "\x00" x ($sw4 * $sh); 1353 my $darkness = "\x00" x ($sw4 * $sh);
848 1354
849 for my $x (0 .. $sw - 1) { 1355 for my $x (0 .. $sw - 1) {
1356 my $row = $map->[$x + $xofs];
850 for my $y (0 .. $sh - 1) { 1357 for my $y (0 .. $sh - 1) {
851 1358
852 my $cell = $map->[$x + $xofs][$y + $yofs] 1359 my $cell = $row->[$y + $yofs]
853 or next; 1360 or next;
854 1361
855 my $darkness = $cell->[0] * (1 / 255); 1362 my $dark = $cell->[0];
856 if ($darkness < 0) { 1363 if ($dark < 0) {
857 $darkness = $cell->[1] ? 0.1 : 0; 1364 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1365 } else {
1366 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
858 } 1367 }
859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
860 1368
861 for my $num (grep $_, @$cell[1,2,3]) { 1369 for my $num (grep $_, @$cell[1,2,3]) {
862 my $tex = $::CONN->{face}[$num]{texture} || next; 1370 my $tex = $::CONN->{face}[$num]{texture} || next;
1371
1372 my ($w, $h) = @$tex{qw(w h)};
863 1373
864 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1374 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
865
866 my $w = $tex->{width};
867 my $h = $tex->{height};
868
869 my $px = ($x + 1) * 32 - $w;
870 my $py = ($y + 1) * 32 - $h;
871
872 glBegin GL_QUADS;
873 glTexCoord 0, 0; glVertex $px , $py;
874 glTexCoord 0, 1; glVertex $px , $py + $h;
875 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
876 glTexCoord 1, 0; glVertex $px + $w, $py;
877 glEnd; 1375 }
878 } 1376 }
879 } 1377 }
880 }
881 1378
882# if (1) { # higher quality darkness 1379# if (1) { # higher quality darkness
883# $lighting =~ s/(.)/$1$1$1/gs; 1380# $lighting =~ s/(.)/$1$1$1/gs;
884# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 1381# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
885# 1382#
887# 1384#
888# $lighting = $pb->get_pixels; 1385# $lighting = $pb->get_pixels;
889# $lighting =~ s/(.)../$1/gs; 1386# $lighting =~ s/(.)../$1/gs;
890# } 1387# }
891 1388
892 $lighting = new Crossfire::Client::Texture
893 width => $sw4,
894 height => $sh,
895 data => $lighting,
896 internalformat => GL_ALPHA,
897 format => GL_ALPHA;
898
899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
900 glColor 0.45, 0.45, 0.45, 1;
901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1389 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
904 glBegin GL_QUADS;
905 glTexCoord 0, 0; glVertex 0 , 0;
906 glTexCoord 0, 1; glVertex 0 , $sh * 32;
907 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
908 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
909 glEnd;
910 1390
1391 $darkness = new CFClient::Texture
1392 w => $sw4,
1393 h => $sh,
1394 data => $darkness,
1395 internalformat => GL_ALPHA,
1396 format => GL_ALPHA;
1397
1398 glColor 0.45, 0.45, 0.45, 1;
1399 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1400
911 glDisable GL_TEXTURE_2D; 1401 glDisable GL_TEXTURE_2D;
912 glDisable GL_BLEND; 1402 glDisable GL_BLEND;
1403
1404 glEndList;
1405 }
1406
1407 glCallList $self->{list};
913} 1408}
914 1409
915my %DIR = ( 1410my %DIR = (
916 SDLK_KP8, [1, "north"], 1411 SDLK_KP8, [1, "north"],
917 SDLK_KP9, [2, "northeast"], 1412 SDLK_KP9, [2, "northeast"],
963 } 1458 }
964} 1459}
965 1460
966############################################################################# 1461#############################################################################
967 1462
968package Crossfire::Client::Widget::Animator; 1463package CFClient::UI::Animator;
969 1464
970use SDL::OpenGL; 1465use SDL::OpenGL;
971 1466
972our @ISA = Crossfire::Client::Widget::Bin::; 1467our @ISA = CFClient::UI::Bin::;
973 1468
974sub moveto { 1469sub moveto {
975 my ($self, $x, $y) = @_; 1470 my ($self, $x, $y) = @_;
976 1471
977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1472 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
978 $self->{speed} = 2; 1473 $self->{speed} = 0.001;
979 $self->{time} = 1; 1474 $self->{time} = 1;
980 1475
981 ::animation_start $self; 1476 ::animation_start $self;
982} 1477}
983 1478
1005 glPopMatrix; 1500 glPopMatrix;
1006} 1501}
1007 1502
1008############################################################################# 1503#############################################################################
1009 1504
1010package Crossfire::Client::Widget::Toplevel; 1505package CFClient::UI::Toplevel;
1011 1506
1012our @ISA = Crossfire::Client::Widget::Container::; 1507our @ISA = CFClient::UI::Container::;
1013 1508
1014sub size_request { 1509sub size_request {
1015 ($::WIDTH, $::HEIGHT) 1510 ($::WIDTH, $::HEIGHT)
1016} 1511}
1017 1512
1018sub size_allocate { 1513sub size_allocate {
1019 my ($self, $w, $h) = @_; 1514 my ($self, $x, $y, $w, $h) = @_;
1020 1515
1021 $self->SUPER::size_allocate ($w, $h); 1516 $self->_size_allocate ($x, $y, $w, $h);
1022 1517
1023 $_->size_allocate ($_->size_request) 1518 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1024 for @{$self->{children}}; 1519 for @{$self->{children}};
1025} 1520}
1026 1521
1522sub translate {
1523 my ($self, $x, $y) = @_;
1524
1525 ($x, $y)
1526}
1527
1027sub update { 1528sub update {
1028 my ($self) = @_; 1529 my ($self) = @_;
1029 1530
1030 $self->size_allocate ($self->size_request); 1531 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1031 ::refresh (); 1532 ::refresh ();
1032} 1533}
1033 1534
1034sub add { 1535sub add {
1035 my ($self, $widget) = @_; 1536 my ($self, $widget) = @_;
1036 1537
1037 $self->SUPER::add ($widget); 1538 $self->SUPER::add ($widget);
1038 1539
1039 $widget->size_allocate ($widget->size_request); 1540 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1040} 1541}
1041 1542
1042sub draw { 1543sub draw {
1043 my ($self) = @_; 1544 my ($self) = @_;
1044 1545
1045 $self->_draw; 1546 $self->_draw;
1046} 1547}
1047 1548
1048############################################################################# 1549#############################################################################
1049 1550
1050package Crossfire::Client::Widget; 1551package CFClient::UI;
1051 1552
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1553$TOPLEVEL = new CFClient::UI::Toplevel;
1053 1554
10541 15551
1055 1556

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines