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.40 by root, Sun Apr 9 21:41:11 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
40sub size_allocate { 110sub _size_allocate {
41 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;
42 117
43 $self->{w} = $w; 118 $self->{w} = $w;
44 $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});
45} 135}
46 136
47sub focus_in { 137sub focus_in {
48 my ($widget) = @_; 138 my ($self) = @_;
49 $FOCUS = $widget; 139
140 return if $FOCUS == $self;
141
142 my $focus = $FOCUS; $FOCUS = $self;
143 $focus->update if $focus;
144 $FOCUS->update;
50} 145}
51 146
52sub focus_out { 147sub focus_out {
53 my ($widget) = @_; 148 my ($self) = @_;
54}
55 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 { }
56sub key_down { 158sub key_down { }
57 my ($widget, $sdlev) = @_;
58}
59
60sub key_up { 159sub key_up { }
61 my ($widget, $sdlev) = @_;
62}
63 160
64sub button_down { 161sub button_down {
65 my ($widget, $sdlev) = @_; 162 my ($self, $ev, $x, $y) = @_;
66}
67 163
68sub button_up { 164 $self->focus_in;
69 my ($widget, $sdlev) = @_;
70} 165}
71 166
72sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 167sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
73sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 168sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
74sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 169sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
75sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 170sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
76sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 171sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
77 172
78sub draw { 173sub draw {
79 my ($self) = @_; 174 my ($self) = @_;
175
176 return unless $self->{h} && $self->{w};
80 177
81 glPushMatrix; 178 glPushMatrix;
82 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
83 $self->_draw; 180 $self->_draw;
84 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 }
85} 197}
86 198
87sub _draw { 199sub _draw {
88 my ($self) = @_; 200 my ($self) = @_;
89 201
129 241
130 $self->{parent}->update 242 $self->{parent}->update
131 if $self->{parent}; 243 if $self->{parent};
132} 244}
133 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
134sub DESTROY { 259sub DESTROY {
135 my ($self) = @_; 260 my ($self) = @_;
136 261
137 #$self->deactivate; 262 #$self->deactivate;
138} 263}
139 264
140############################################################################# 265#############################################################################
141 266
142package Crossfire::Client::Widget::Container; 267package CFClient::UI::DrawBG;
143 268
144our @ISA = Crossfire::Client::Widget::; 269our @ISA = CFClient::UI::Base::;
270
271use strict;
272use SDL::OpenGL;
145 273
146sub 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 {
147 my ($class, @widgets) = @_; 319 my ($class, %arg) = @_;
148 320
321 my $children = delete $arg{children} || [];
322
149 my $self = $class->SUPER::new (children => []); 323 my $self = $class->SUPER::new (children => [], %arg);
150 $self->add ($_) for @widgets; 324 $self->add ($_) for @$children;
151 325
152 $self 326 $self
153} 327}
154 328
155sub add { 329sub add {
156 my ($self, $chld, $expand) = @_; 330 my ($self, $chld, $expand) = @_;
157 331
158 $chld->{expand} = $expand; 332 $chld->{expand} = $expand;
159 $chld->set_parent ($self); 333 $chld->set_parent ($self);
160 334
161 @{$self->{children}} = 335 $self->{children} = [
162 sort { $a->{z} <=> $b->{z} } 336 sort { $a->{z} <=> $b->{z} }
163 @{$self->{children}}, $chld; 337 @{$self->{children}}, $chld
338 ];
164 339
165 $self->size_allocate ($self->{w}, $self->{h}); 340 $self->{w} = $self->{h} = -1;
341 $self->update;
166} 342}
167 343
168sub remove { 344sub remove {
169 my ($self, $widget) = @_; 345 my ($self, $widget) = @_;
170 346
171 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
172 348
173 $self->size_allocate ($self->{w}, $self->{h}); 349 $self->size_allocate (0, 0, $self->{w}, $self->{h});
174} 350}
175 351
176sub find_widget { 352sub find_widget {
177 my ($self, $x, $y) = @_; 353 my ($self, $x, $y) = @_;
178 354
355 $x -= $self->{x};
356 $y -= $self->{y};
357
179 my $res; 358 my $res;
180 359
181 for (@{ $self->{children} }) { 360 for (reverse @{ $self->{children} }) {
182 $res = $_->find_widget ($x, $y) 361 $res = $_->find_widget ($x, $y)
183 and return $res; 362 and return $res;
184 } 363 }
185 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} = [];
186 () 392
393 $self->SUPER::add ($widget);
187} 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] }
188 406
189sub size_request { 407sub size_request {
190 my ($self) = @_;
191
192 my ($hs, $ws) = (0, 0);
193 for (@{$self->{children} || []}) {
194 my ($w, $h) = $_->size_request;
195 $hs += $h;
196 if ($ws < $w) { $ws = $w }
197 }
198
199 return ($ws, $hs);
200}
201
202sub _draw {
203 my ($self) = @_;
204
205 $_->draw for @{$self->{children}};
206}
207
208#############################################################################
209
210package Crossfire::Client::Widget::Bin;
211
212our @ISA = Crossfire::Client::Widget::Container::;
213
214sub child { $_[0]->{children}[0] }
215
216sub size_request {
217 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 408 $_[0]{children}[0]->size_request
218} 409}
219 410
220sub size_allocate { 411sub size_allocate {
221 my ($self, $w, $h) = @_; 412 my ($self, $x, $y, $w, $h) = @_;
222 $self->SUPER::size_allocate ($w, $h); 413
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
223 $self->{children}[0]->size_allocate ($w, $h) 416 $self->{children}[0]->size_allocate (0, 0, $w, $h);
224 if $self->{children}[0]
225} 417}
226 418
227############################################################################# 419#############################################################################
228 420
229package Crossfire::Client::Widget::Toplevel; 421package CFClient::UI::Window;
230 422
231our @ISA = Crossfire::Client::Widget::Container::; 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}
232 432
233sub update { 433sub update {
234 my ($self) = @_; 434 my ($self) = @_;
235 435
236 ::refresh (); 436 # we want to do this delayed...
237}
238
239#############################################################################
240
241package Crossfire::Client::Widget::Window;
242
243our @ISA = Crossfire::Client::Widget::Bin::;
244
245use SDL::OpenGL;
246
247sub add {
248 my ($self, $chld) = @_;
249 warn "ADD $chld\n";
250 $self->SUPER::add ($chld);
251 $chld->set_parent ($self);
252}
253
254sub remove {
255 my ($self) = @_;
256 # TODO FIXME: removing a child from a window will crash, see render_chld
257 # $self->update;
258}
259
260sub update {
261 my ($self) = @_;
262 $self->render_chld; 437 $self->render_chld;
438 $self->SUPER::update;
263} 439}
264 440
265sub render_chld { 441sub render_chld {
266 my ($self) = @_; 442 my ($self) = @_;
267 my $chld = $self->get;
268 my ($w, $h) = $self->size_request;
269 443
270 require Carp;
271 Carp::cluck "RENDERCHI $w $h";
272 warn "RENDERCHI $w $h\n";
273 $self->{texture} = 444 $self->{texture} =
274 Crossfire::Client::Texture->new_from_opengl ( 445 CFClient::Texture->new_from_opengl (
275 $w, $h, sub { $chld->draw } 446 $self->{w}, $self->{h}, sub { $self->child->draw }
276 ); 447 );
277 $self->{texture}->upload;
278}
279
280sub size_request {
281 my ($self) = @_;
282 ($self->w, $self->h)
283} 448}
284 449
285sub size_allocate { 450sub size_allocate {
286 my ($self, $w, $h) = @_; 451 my ($self, $x, $y, $w, $h) = @_;
287 452
288 $self->w ($w); 453 $self->_size_allocate ($x, $y, $w, $h) or return;
289 $self->h ($h); 454
290 $self->get->size_allocate ($w, $h); 455 $self->child->size_allocate (0, 0, $w, $h);
291 456
292 $self->update; #TODO: Move this to the size_request event propably? 457 $self->render_chld;
293} 458}
294 459
295sub _draw { 460sub _draw {
296 my ($self) = @_; 461 my ($self) = @_;
297 462
299 464
300 my $tex = $self->{texture} 465 my $tex = $self->{texture}
301 or return; 466 or return;
302 467
303 glEnable GL_BLEND; 468 glEnable GL_BLEND;
469 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
304 glEnable GL_TEXTURE_2D; 470 glEnable GL_TEXTURE_2D;
305 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 471 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
306 glBindTexture GL_TEXTURE_2D, $tex->{name};
307 472
308 glBegin GL_QUADS; 473 $tex->draw_quad (0, 0, $w, $h);
309 glTexCoord 0, 0; glVertex 0, 0;
310 glTexCoord 0, 1; glVertex 0, $h;
311 glTexCoord 1, 1; glVertex $w, $h;
312 glTexCoord 1, 0; glVertex $w, 0;
313 glEnd;
314 474
315 glDisable GL_BLEND; 475 glDisable GL_BLEND;
316 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
317} 477}
318 478
319############################################################################# 479#############################################################################
320 480
321package Crossfire::Client::Widget::Frame; 481package CFClient::UI::Frame;
322 482
323our @ISA = Crossfire::Client::Widget::Bin::; 483our @ISA = CFClient::UI::Bin::;
324 484
325use SDL::OpenGL; 485use SDL::OpenGL;
326 486
327sub size_request { 487sub size_request {
328 my ($self) = @_; 488 my ($self) = @_;
333 493
334 map { $_ + 4 } $chld->size_request; 494 map { $_ + 4 } $chld->size_request;
335} 495}
336 496
337sub size_allocate { 497sub size_allocate {
338 my ($self, $w, $h) = @_; 498 my ($self, $x, $y, $w, $h) = @_;
499
500 $self->_size_allocate ($x, $y, $w, $h) or return;
339 501
340 $self->w ($w);
341 $self->h ($h);
342
343 $self->child->size_allocate ($w - 4, $h - 4); 502 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
344 $self->child->move (2, 2);
345} 503}
346 504
347sub _draw { 505sub _draw {
348 my ($self) = @_; 506 my ($self) = @_;
349 507
351 509
352 my ($w, $h) = $chld->size_request; 510 my ($w, $h) = $chld->size_request;
353 511
354 glBegin GL_QUADS; 512 glBegin GL_QUADS;
355 glColor 0, 0, 0; 513 glColor 0, 0, 0;
356 glTexCoord 0, 0; glVertex 0 , 0; 514 glVertex 0 , 0;
357 glTexCoord 0, 1; glVertex 0 , $h + 4; 515 glVertex 0 , $h + 4;
358 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 516 glVertex $w + 4 , $h + 4;
359 glTexCoord 1, 0; glVertex $w + 4 , 0; 517 glVertex $w + 4 , 0;
360 glEnd; 518 glEnd;
361 519
362 $chld->draw; 520 $chld->draw;
363} 521}
364 522
365############################################################################# 523#############################################################################
366 524
367package Crossfire::Client::Widget::FancyFrame; 525package CFClient::UI::FancyFrame;
368 526
369our @ISA = Crossfire::Client::Widget::Frame::; 527our @ISA = CFClient::UI::Bin::;
370 528
371use SDL::OpenGL; 529use SDL::OpenGL;
372 530
373sub new { 531my @tex =
374 my ($self, $theme) = @_;
375 $self = $self->SUPER::new;
376
377 $self->{txts} = [
378 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
379 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);
380 ];
381 $self
382}
383 534
384sub size_request { 535sub size_request {
385 my ($self) = @_; 536 my ($self) = @_;
386 537
387 my ($w, $h) = $self->SUPER::size_request; 538 my ($w, $h) = $self->SUPER::size_request;
388 539
389 $h += $self->{txts}->[1]->{height}; 540 $h += $tex[1]->{h};
390 $h += $self->{txts}->[4]->{height}; 541 $h += $tex[4]->{h};
391 $w += $self->{txts}->[2]->{width}; 542 $w += $tex[2]->{w};
392 $w += $self->{txts}->[3]->{width}; 543 $w += $tex[3]->{w};
393 544
394 ($w, $h) 545 ($w, $h)
395} 546}
396 547
397sub size_allocate { 548sub size_allocate {
398 my ($self, $w, $h) = @_; 549 my ($self, $x, $y, $w, $h) = @_;
399 550
400 $self->SUPER::size_allocate ($w, $h); 551 $self->_size_allocate ($x, $y, $w, $h) or return;
401 552
402 $h -= $self->{txts}->[1]->{height}; 553 $h -= $tex[1]->{h};
403 $h -= $self->{txts}->[4]->{height}; 554 $h -= $tex[4]->{h};
404 $w -= $self->{txts}->[2]->{width}; 555 $w -= $tex[2]->{w};
405 $w -= $self->{txts}->[3]->{width}; 556 $w -= $tex[3]->{w};
406 557
407 $h = $h < 0 ? 0 : $h; 558 $h = $h < 0 ? 0 : $h;
408 $w = $w < 0 ? 0 : $w; 559 $w = $w < 0 ? 0 : $w;
409 warn "CHILD:$w $h\n"; 560
410 $self->child->size_allocate ($w, $h); 561 my $child = $self->child;
411 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 562
563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
412} 564}
413 565
414sub _draw { 566sub _draw {
415 my ($self) = @_; 567 my ($self) = @_;
416 568
417 my ($w, $h) = ($self->w, $self->h); 569 my ($w, $h) = ($self->{w}, $self->{h});
418 my ($cw, $ch) = ($self->child->w, $self->child->h); 570 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
419 571
420 glEnable GL_BLEND; 572 glEnable GL_BLEND;
573 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
421 glEnable GL_TEXTURE_2D; 574 glEnable GL_TEXTURE_2D;
422 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
423 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
424 576
425 my $top = $self->{txts}->[1]; 577 my $top = $tex[1];
426 glBindTexture GL_TEXTURE_2D, $top->{name}; 578 $top->draw_quad (0, 0, $w, $top->{h});
427 579
428 glBegin GL_QUADS;
429 glTexCoord 0, 0; glVertex 0 , 0;
430 glTexCoord 0, 1; glVertex 0 , $top->{height};
431 glTexCoord 1, 1; glVertex $w , $top->{height};
432 glTexCoord 1, 0; glVertex $w , 0;
433 glEnd;
434
435 my $left = $self->{txts}->[3]; 580 my $left = $tex[3];
436 glBindTexture GL_TEXTURE_2D, $left->{name}; 581 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
437 582
438 glBegin GL_QUADS;
439 glTexCoord 0, 0; glVertex 0 , $top->{height};
440 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
441 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
442 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
443 glEnd;
444
445 my $right = $self->{txts}->[2]; 583 my $right = $tex[2];
446 glBindTexture GL_TEXTURE_2D, $right->{name}; 584 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
447 585
448 glBegin GL_QUADS;
449 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
450 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
451 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
452 glTexCoord 1, 0; glVertex $w , $top->{height};
453 glEnd;
454
455 my $bottom = $self->{txts}->[4]; 586 my $bottom = $tex[4];
456 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 587 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
457 588
458 glBegin GL_QUADS; 589 my $bg = $tex[0];
459 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
460 glTexCoord 0, 1; glVertex 0 , $h;
461 glTexCoord 1, 1; glVertex $w , $h;
462 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
463 glEnd;
464
465 my $bg = $self->{txts}->[0];
466 glBindTexture GL_TEXTURE_2D, $bg->{name}; 590 glBindTexture GL_TEXTURE_2D, $bg->{name};
467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 592 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
469 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 593 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
470 594
471 my $rep_x = $cw / $bg->{width}; 595 my $rep_x = $cw / $bg->{w};
472 my $rep_y = $ch / $bg->{height}; 596 my $rep_y = $ch / $bg->{h};
473 597
474 glBegin GL_QUADS; 598 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
475 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
476 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
477 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
478 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
479 glEnd;
480 599
481 glDisable GL_BLEND; 600 glDisable GL_BLEND;
482 glDisable GL_TEXTURE_2D; 601 glDisable GL_TEXTURE_2D;
483 602
484 $self->child->draw; 603 $self->child->draw;
485 604
486} 605}
487 606
488############################################################################# 607#############################################################################
489 608
490package Crossfire::Client::Widget::Table; 609package CFClient::UI::Table;
491 610
492our @ISA = Crossfire::Client::Widget::Bin::; 611our @ISA = CFClient::UI::Base::;
612
613use List::Util qw(max sum);
493 614
494use SDL::OpenGL; 615use SDL::OpenGL;
495 616
496sub add { 617sub add {
497 my ($self, $x, $y, $chld) = @_; 618 my ($self, $x, $y, $chld) = @_;
498 my $old_chld = $self->{children}[$y][$x];
499 619
500 $self->{children}[$y][$x] = $chld; 620 $self->{children}[$y][$x] = $chld;
501 $chld->set_parent ($self); 621 $chld->set_parent ($self);
622
623 $self->{w} = $self->{h} = -1;
502 $self->update; 624 $self->update;
503} 625}
504 626
505sub max_row_height { 627sub get_wh {
506 my ($self, $row) = @_; 628 my ($self) = @_;
507 629
508 my $hs = 0; 630 my (@w, @h);
509 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 631
632 for my $y (0 .. $#{$self->{children}}) {
510 my $c = $self->{children}->[$row]->[$xi]; 633 my $row = $self->{children}[$y]
511 if ($c) { 634 or next;
635
636 for my $x (0 .. $#$row) {
637 my $widget = $row->[$x]
638 or next;
512 my ($w, $h) = $c->size_request; 639 my ($w, $h) = $widget->size_request;
513 if ($hs < $h) { $hs = $h } 640
641 $w[$x] = max $w[$x], $w;
642 $h[$y] = max $h[$y], $h;
514 } 643 }
515 } 644 }
516 return $hs;
517}
518 645
519sub max_col_width { 646 (\@w, \@h)
647}
648
649sub size_request {
520 my ($self, $col) = @_; 650 my ($self) = @_;
521 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
522 my $ws = 0; 680 my $x = 0;
523 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 681 my $row_h = $hs->[$r];
524 my $c = ($self->{children}->[$yi] || [])->[$col]; 682
525 if ($c) { 683 for my $c (0 .. $#$row) {
526 my ($w, $h) = $c->size_request; 684 my $widget = $row->[$c]
527 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;
528 } 692 }
693
694 $y += $row_h;
529 } 695 }
530 return $ws; 696
531} 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;
532 714
533sub size_request { 715sub size_request {
534 my ($self) = @_; 716 my ($self) = @_;
535 717
536 my ($hs, $ws) = (0, 0); 718 my @alloc = map [$_->size_request], @{$self->{children}};
537 719
538 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
539 $hs += $self->max_row_height ($yi);
540 } 720 (
721 (List::Util::max map $_->[0], @alloc),
722 (List::Util::sum map $_->[1], @alloc),
723 )
724}
541 725
542 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 726sub size_allocate {
543 my $wm = 0; 727 my ($self, $x, $y, $w, $h) = @_;
544 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 728
545 $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;
546 } 754 }
547 if ($ws < $wm) { $ws = $wm }
548 } 755 }
549
550 return ($ws, $hs);
551}
552
553sub _draw {
554 my ($self) = @_;
555 756
556 my $y = 0; 757 my $y = 0;
557 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 758 for (0 .. $#$children) {
759 my $child = $children->[$_];
558 my $x = 0; 760 my $h = $h[$_];
761 $child->size_allocate (0, $y, $w, $h);
559 762
560 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { 763 $y += $h;
561
562 my $c = $self->{children}->[$yi]->[$xi];
563 if ($c) {
564 $c->move ($x, $y, 0); #TODO: Move to size_request
565 $c->draw if $c;
566 }
567
568 $x += $self->max_col_width ($xi);
569 }
570
571 $y += $self->max_row_height ($yi);
572 } 764 }
573} 765}
574 766
575############################################################################# 767#############################################################################
576 768
577package Crossfire::Client::Widget::VBox; 769package CFClient::UI::Label;
578 770
579our @ISA = Crossfire::Client::Widget::Container::; 771our @ISA = CFClient::UI::Base::;
580 772
581use SDL::OpenGL; 773use SDL::OpenGL;
582 774
583sub size_allocate {
584 my ($self, $w, $h) = @_;
585
586 $self->w ($w);
587 $self->h ($h);
588
589 my $exp;
590 my @oth;
591 # find expand widget
592 for (@{$self->{children}}) {
593 if ($_->{expand}) {
594 $exp = $_;
595 last;
596 }
597 push @oth, $_;
598 }
599
600 my ($ow, $oh);
601
602 # get sizes of other widgets
603 for (@oth) {
604 my ($w, $h) = $_->size_request;
605 $oh += $h;
606 if ($ow < $w) { $ow = $w }
607 }
608
609 my $y = 0;
610 for (@{$self->{children}}) {
611 $_->move (0, $y);
612
613 if ($_ == $exp) {
614 $_->size_allocate ($w, $h - $oh);
615 $y += $h - $oh;
616 } else {
617 my ($cw, $h) = $_->size_request;
618 $_->size_allocate ($w, $h);
619 $y += $h;
620 }
621 }
622}
623
624sub _draw {
625 my ($self) = @_;
626
627 my ($x, $y);
628 for (@{$self->{children} || []}) {
629 $_->draw;
630 $y += $_->h;
631 }
632}
633
634#############################################################################
635
636package Crossfire::Client::Widget::Label;
637
638our @ISA = Crossfire::Client::Widget::;
639
640use SDL::OpenGL;
641
642sub new { 775sub new {
643 my ($class, $x, $y, $z, $height, $text) = @_; 776 my ($class, %arg) = @_;
644 777
645 # TODO: color, and make height, xyz etc. optional 778 my $self = $class->SUPER::new (
646 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 );
647 786
648 $self->set_text ($text); 787 $self->set_text ($self->{text});
649 788
650 $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]
651} 800}
652 801
653sub set_text { 802sub set_text {
654 my ($self, $text) = @_; 803 my ($self, $text) = @_;
655 804
656 $self->{text} = $text; 805 $self->{text} = $text;
657 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 806 $self->{layout}->set_markup ($text);
658 807
808 delete $self->{texture};
659 $self->update; 809 $self->update;
660} 810}
661 811
662sub get_text { 812sub get_text {
663 my ($self, $text) = @_; 813 my ($self, $text) = @_;
666} 816}
667 817
668sub size_request { 818sub size_request {
669 my ($self) = @_; 819 my ($self) = @_;
670 820
671 ( 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# (
672 $self->{texture}{width}, 826# $self->{texture}{w},
673 $self->{texture}{height}, 827# $self->{texture}{h},
674 ) 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;
675} 849}
676 850
677sub _draw { 851sub _draw {
678 my ($self) = @_; 852 my ($self) = @_;
679 853
680 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 };
681 858
682 glEnable GL_BLEND; 859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
683 glEnable GL_TEXTURE_2D; 861 glEnable GL_TEXTURE_2D;
684 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
685 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
686 glBindTexture GL_TEXTURE_2D, $tex->{name};
687 863
688 glColor 1, 0, 0, 1; # TODO color 864 glColor @{$self->{fg}};
689 865
690 glBegin GL_QUADS; 866 my $x =
691 glTexCoord 0, 0; glVertex 0 , 0; 867 $self->{align} < 0 ? 0
692 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 868 : $self->{align} > 0 ? $self->{w} - $tex->{w}
693 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 869 : ($self->{w} - $tex->{w}) * 0.5;
694 glTexCoord 1, 0; glVertex $tex->{width}, 0;
695 glEnd;
696 870
871 $tex->draw_quad ($x, 0);
872
873 glDisable GL_TEXTURE_2D;
697 glDisable GL_BLEND; 874 glDisable GL_BLEND;
698 glDisable GL_TEXTURE_2D;
699} 875}
700 876
701############################################################################# 877#############################################################################
702 878
703package Crossfire::Client::Widget::TextEntry; 879package CFClient::UI::Entry;
704 880
705our @ISA = Crossfire::Client::Widget::Label::; 881our @ISA = CFClient::UI::Label::;
706 882
707use SDL; 883use SDL;
708use SDL::OpenGL; 884use SDL::OpenGL;
709 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
710sub key_down { 941sub key_down {
711 my ($self, $ev) = @_; 942 my ($self, $ev) = @_;
712 943
713 my $mod = $ev->key_mod; 944 my $mod = $ev->key_mod;
714 my $sym = $ev->key_sym; 945 my $sym = $ev->key_sym;
715 946
716 $ev->set_unicode (1);
717 my $uni = $ev->key_unicode; 947 my $uni = $ev->key_unicode;
718 948
719 my $text = $self->get_text; 949 my $text = $self->get_text;
720 950
721 if ($sym == SDLK_BACKSPACE) { 951 if ($sym == SDLK_BACKSPACE) {
722 substr $text, -1, 1, ''; 952 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
723 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};
724 } elsif ($uni) { 959 } elsif ($uni) {
725 $text .= chr $uni; 960 substr $text, $self->{cursor}++, 0, chr $uni;
726 } 961 }
962
727 $self->set_text ($text); 963 $self->_set_text ($text);
964 $self->update;
728} 965}
729 966
730############################################################################# 967sub focus_in {
968 my ($self) = @_;
731 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
732package Crossfire::Client::Widget::MapWidget; 1142package CFClient::UI::MapWidget;
733 1143
734use strict; 1144use strict;
735 1145
736use List::Util qw(min max); 1146use List::Util qw(min max);
737 1147
738use SDL; 1148use SDL;
739use SDL::OpenGL; 1149use SDL::OpenGL;
740use SDL::OpenGL::Constants;
741 1150
742our @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}
743 1162
744sub key_down { 1163sub key_down {
745 print "MAPKEYDOWN\n"; 1164 print "MAPKEYDOWN\n";
746} 1165}
747 1166
748sub key_up { 1167sub key_up {
749} 1168}
750 1169
751sub size_request { 1170sub size_request {
752 1171 (
1172 1 + int $::WIDTH / 32,
1173 1 + int $::HEIGHT / 32,
1174 )
753} 1175}
754 1176
755sub size_allocate { 1177sub update {
1178 my ($self) = @_;
1179
1180 $self->{need_update} = 1;
1181 $self->SUPER::update;
756} 1182}
757 1183
758sub _draw { 1184sub _draw {
759 my ($self) = @_; 1185 my ($self) = @_;
760 1186
1187 if (delete $self->{need_update}) {
1188 glNewList $self->{list}, GL_COMPILE;
1189
761 my $mx = $::CONN->{mapx}; 1190 my $mx = $::CONN->{mapx};
762 my $my = $::CONN->{mapy}; 1191 my $my = $::CONN->{mapy};
763 1192
764 my $map = $::CONN->{map}; 1193 my $map = $::CONN->{map};
765 1194
766 my ($xofs, $yofs); 1195 my ($xofs, $yofs);
767 1196
768 my $sw = 1 + int $::WIDTH / 32; 1197 my $sw = 1 + int $::WIDTH / 32;
769 my $sh = 1 + int $::HEIGHT / 32; 1198 my $sh = 1 + int $::HEIGHT / 32;
770 1199
771 if ($::CONN->{mapw} > $sw) { 1200 if ($::CONN->{mapw} > $sw) {
772 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1201 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
773 } else { 1202 } else {
774 $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};
775 } 1204 }
776 1205
777 if ($::CONN->{maph} > $sh) { 1206 if ($::CONN->{maph} > $sh) {
778 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1207 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
779 } else { 1208 } else {
780 $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};
781 } 1210 }
782 1211
783 glEnable GL_TEXTURE_2D; 1212 glEnable GL_TEXTURE_2D;
784 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1214 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
785 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1215 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
786 1216
787 my $sw4 = ($sw + 3) & ~3; 1217 my $sw4 = ($sw + 3) & ~3;
788 my $lighting = "\x00" x ($sw4 * $sh); 1218 my $darkness = "\x00" x ($sw4 * $sh);
789 1219
790 for my $x (0 .. $sw - 1) { 1220 for my $x (0 .. $sw - 1) {
1221 my $row = $map->[$x + $xofs];
791 for my $y (0 .. $sh - 1) { 1222 for my $y (0 .. $sh - 1) {
792 1223
793 my $cell = $map->[$x + $xofs][$y + $yofs] 1224 my $cell = $row->[$y + $yofs]
794 or next; 1225 or next;
795 1226
796 my $darkness = $cell->[0] * (1 / 255); 1227 my $dark = $cell->[0];
797 if ($darkness < 0) { 1228 if ($dark < 0) {
798 $darkness = 0.15; 1229 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1230 } else {
1231 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
799 } 1232 }
800 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
801 1233
802 for my $num (grep $_, @$cell[1,2,3]) { 1234 for my $num (grep $_, @$cell[1,2,3]) {
803 my $tex = $::CONN->{face}[$num]{texture} || next; 1235 my $tex = $::CONN->{face}[$num]{texture} || next;
1236
1237 my ($w, $h) = @$tex{qw(w h)};
804 1238
805 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1239 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
806
807 my $w = $tex->{width};
808 my $h = $tex->{height};
809
810 my $px = ($x + 1) * 32 - $w;
811 my $py = ($y + 1) * 32 - $h;
812
813 glBegin GL_QUADS;
814 glTexCoord 0, 0; glVertex $px , $py;
815 glTexCoord 0, 1; glVertex $px , $py + $h;
816 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
817 glTexCoord 1, 0; glVertex $px + $w, $py;
818 glEnd; 1240 }
819 } 1241 }
820 } 1242 }
821 }
822 1243
823# if (1) { # higher quality darkness 1244# if (1) { # higher quality darkness
824# $lighting =~ s/(.)/$1$1$1/gs; 1245# $lighting =~ s/(.)/$1$1$1/gs;
825# 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;
826# 1247#
828# 1249#
829# $lighting = $pb->get_pixels; 1250# $lighting = $pb->get_pixels;
830# $lighting =~ s/(.)../$1/gs; 1251# $lighting =~ s/(.)../$1/gs;
831# } 1252# }
832 1253
833 $lighting = new Crossfire::Client::Texture
834 width => $sw4,
835 height => $sh,
836 data => $lighting,
837 internalformat => GL_ALPHA4,
838 format => GL_ALPHA;
839
840 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
841 glColor 0, 0, 0, 0.75;
842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1254 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
843 glBindTexture GL_TEXTURE_2D, $lighting->{name};
844 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
845 glBegin GL_QUADS;
846 glTexCoord 0, 0; glVertex 0 , 0;
847 glTexCoord 0, 1; glVertex 0 , $sh * 32;
848 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
849 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
850 glEnd;
851 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
852 glDisable GL_TEXTURE_2D; 1266 glDisable GL_TEXTURE_2D;
853 glDisable GL_BLEND; 1267 glDisable GL_BLEND;
1268
1269 glEndList;
1270 }
1271
1272 glCallList $self->{list};
854} 1273}
855 1274
856my %DIR = ( 1275my %DIR = (
857 SDLK_KP8, [1, "north"], 1276 SDLK_KP8, [1, "north"],
858 SDLK_KP9, [2, "northeast"], 1277 SDLK_KP9, [2, "northeast"],
904 } 1323 }
905} 1324}
906 1325
907############################################################################# 1326#############################################################################
908 1327
909package Crossfire::Client::Widget::Animator; 1328package CFClient::UI::Animator;
910 1329
911use SDL::OpenGL; 1330use SDL::OpenGL;
912 1331
913our @ISA = Crossfire::Client::Widget::Bin::; 1332our @ISA = CFClient::UI::Bin::;
914 1333
915sub moveto { 1334sub moveto {
916 my ($self, $x, $y) = @_; 1335 my ($self, $x, $y) = @_;
917 1336
918 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1337 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
919 $self->{speed} = 0.2; 1338 $self->{speed} = 0.001;
920 $self->{time} = 1; 1339 $self->{time} = 1;
921 1340
922 ::animation_start $self; 1341 ::animation_start $self;
923} 1342}
924 1343
939 1358
940sub _draw { 1359sub _draw {
941 my ($self) = @_; 1360 my ($self) = @_;
942 1361
943 glPushMatrix; 1362 glPushMatrix;
944 glRotate $self->{time} * 10000, 0, 1, 0; 1363 glRotate $self->{time} * 1000, 0, 1, 0;
945 $self->{children}[0]->draw; 1364 $self->{children}[0]->draw;
946 glPopMatrix; 1365 glPopMatrix;
947} 1366}
948 1367
9491; 1368#############################################################################
950 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