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.39 by root, Sun Apr 9 21:39:08 2006 UTC vs.
Revision 1.73 by root, Tue Apr 11 18:06:53 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines