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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines