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.65 by root, Tue Apr 11 14:04:27 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10our $FOCUS; # the widget with current focus 10use CFClient;
11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
11 16
12# class methods for events 17# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
14sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 19 $FOCUS->key_down ($_[0]) if $FOCUS;
20}
21
22sub feed_sdl_key_up_event {
23 $FOCUS->key_up ($_[0]) if $FOCUS;
24}
25
15sub feed_sdl_button_down_event { } 26sub feed_sdl_button_down_event {
27 my ($ev) = @_;
28 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
29
30 if (!$BUTTON_STATE) {
31 my $widget = $TOPLEVEL->find_widget ($x, $y);
32
33 $GRAB = $widget;
34 $GRAB->update if $GRAB;
35 }
36
37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38
39 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
40}
41
16sub feed_sdl_button_up_event { } 42sub feed_sdl_button_up_event {
43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
51
52 if (!$BUTTON_STATE) {
53 my $grab = $GRAB; undef $GRAB;
54 $grab->update if $grab;
55 $GRAB->update if $GRAB;
56 }
57}
58
59sub feed_sdl_motion_event {
60 my ($ev) = @_;
61 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
62
63 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
64
65 if ($widget != $HOVER) {
66 my $hover = $HOVER; $HOVER = $widget;
67
68 $hover->update if $hover;
69 $HOVER->update if $HOVER;
70 }
71
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73}
17 74
18sub new { 75sub new {
19 my $class = shift; 76 my $class = shift;
20 77
21 bless { @_ }, $class 78 bless {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
22} 84}
23 85
24sub move { 86sub move {
25 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
26 $self->{x} = $x; 88 $self->{x} = $x;
42 104
43 $self->{w} = $w; 105 $self->{w} = $w;
44 $self->{h} = $h; 106 $self->{h} = $h;
45} 107}
46 108
109# translate global koordinates to local coordinate system
110sub translate {
111 my ($self, $x, $y) = @_;
112
113 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
114}
115
47sub focus_in { 116sub focus_in {
48 my ($widget) = @_; 117 my ($self) = @_;
49 $FOCUS = $widget; 118
119 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus;
121 $FOCUS->update;
50} 122}
51 123
52sub focus_out { 124sub focus_out {
53 my ($widget) = @_; 125 my ($self) = @_;
54}
55 126
56sub key_down { 127 return unless $FOCUS == $self;
57 my ($widget, $sdlev) = @_;
58}
59 128
60sub key_up { 129 my $focus = $FOCUS; undef $FOCUS;
61 my ($widget, $sdlev) = @_; 130 $focus->update if $focus; #?
62} 131}
63 132
133sub mouse_motion { }
134sub button_up { }
64sub button_down { 135sub button_down { }
65 my ($widget, $sdlev) = @_; 136sub key_down { }
66} 137sub key_up { }
67 138
68sub button_up {
69 my ($widget, $sdlev) = @_;
70}
71
72sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
73sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
74sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
75sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
76sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
77 144
78sub draw { 145sub draw {
79 my ($self) = @_; 146 my ($self) = @_;
80 147
81 glPushMatrix; 148 glPushMatrix;
82 glTranslate $self->{x}, $self->{y}, 0; 149 glTranslate $self->{x}, $self->{y}, 0;
83 $self->_draw; 150 $self->_draw;
151 if ($self == $HOVER) {
152 glColor 1, 1, 1, 0.4;
153 glEnable GL_BLEND;
154 glBegin GL_QUADS;
155 glVertex 0 , 0;
156 glVertex $self->{w}, 0;
157 glVertex $self->{w}, $self->{h};
158 glVertex 0 , $self->{h};
159 glEnd;
160 glDisable GL_BLEND;
161 }
84 glPopMatrix; 162 glPopMatrix;
85} 163}
86 164
87sub _draw { 165sub _draw {
88 my ($self) = @_; 166 my ($self) = @_;
137 #$self->deactivate; 215 #$self->deactivate;
138} 216}
139 217
140############################################################################# 218#############################################################################
141 219
142package Crossfire::Client::Widget::Container; 220package CFClient::Widget::Container;
143 221
144our @ISA = Crossfire::Client::Widget::; 222our @ISA = CFClient::Widget::;
145 223
146sub new { 224sub new {
147 my ($class, @widgets) = @_; 225 my ($class, %arg) = @_;
148 226
227 my $children = delete $arg{children} || [];
228
149 my $self = $class->SUPER::new (children => []); 229 my $self = $class->SUPER::new (children => [], %arg);
150 $self->add ($_) for @widgets; 230 $self->add ($_) for @$children;
151 231
152 $self 232 $self
153} 233}
154 234
155sub add { 235sub add {
160 240
161 @{$self->{children}} = 241 @{$self->{children}} =
162 sort { $a->{z} <=> $b->{z} } 242 sort { $a->{z} <=> $b->{z} }
163 @{$self->{children}}, $chld; 243 @{$self->{children}}, $chld;
164 244
165 $self->size_allocate ($self->{w}, $self->{h}); 245 $self->size_allocate ($self->{w}, $self->{h})
246 if $self->{w}; #TODO: check for "realised state"
166} 247}
167 248
168sub remove { 249sub remove {
169 my ($self, $widget) = @_; 250 my ($self, $widget) = @_;
170 251
174} 255}
175 256
176sub find_widget { 257sub find_widget {
177 my ($self, $x, $y) = @_; 258 my ($self, $x, $y) = @_;
178 259
260 $x -= $self->{x};
261 $y -= $self->{y};
262
179 my $res; 263 my $res;
180 264
181 for (@{ $self->{children} }) { 265 for (reverse @{ $self->{children} }) {
182 $res = $_->find_widget ($x, $y) 266 $res = $_->find_widget ($x, $y)
183 and return $res; 267 and return $res;
184 } 268 }
185 269
186 () 270 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
187}
188
189sub 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} 271}
201 272
202sub _draw { 273sub _draw {
203 my ($self) = @_; 274 my ($self) = @_;
204 275
205 $_->draw for @{$self->{children}}; 276 $_->draw for @{$self->{children}};
206} 277}
207 278
208############################################################################# 279#############################################################################
209 280
210package Crossfire::Client::Widget::Bin; 281package CFClient::Widget::Bin;
211 282
212our @ISA = Crossfire::Client::Widget::Container::; 283our @ISA = CFClient::Widget::Container::;
213 284
214sub child { $_[0]->{children}[0] } 285sub child { $_[0]->{children}[0] }
215 286
216sub size_request { 287sub size_request {
217 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 288 $_[0]{children}[0]->size_request if $_[0]{children}[0];
218} 289}
219 290
220sub size_allocate { 291sub size_allocate {
221 my ($self, $w, $h) = @_; 292 my ($self, $w, $h) = @_;
293
222 $self->SUPER::size_allocate ($w, $h); 294 $self->SUPER::size_allocate ($w, $h);
223 $self->{children}[0]->size_allocate ($w, $h) 295 $self->{children}[0]->size_allocate ($w, $h)
224 if $self->{children}[0] 296 if $self->{children}[0]
225} 297}
226 298
227############################################################################# 299#############################################################################
228 300
229package Crossfire::Client::Widget::Toplevel; 301package CFClient::Widget::Window;
230 302
231our @ISA = Crossfire::Client::Widget::Container::; 303our @ISA = CFClient::Widget::Bin::;
304
305use SDL::OpenGL;
306
307sub new {
308 my ($class, %arg) = @_;
309
310 my $self = $class->SUPER::new (%arg);
311}
232 312
233sub update { 313sub update {
234 my ($self) = @_; 314 my ($self) = @_;
235 315
236 ::refresh (); 316 # 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; 317 $self->render_chld;
318 $self->SUPER::update;
263} 319}
264 320
265sub render_chld { 321sub render_chld {
266 my ($self) = @_; 322 my ($self) = @_;
267 my $chld = $self->get;
268 my ($w, $h) = $self->size_request;
269 323
270 require Carp;
271 Carp::cluck "RENDERCHI $w $h";
272 warn "RENDERCHI $w $h\n";
273 $self->{texture} = 324 $self->{texture} =
274 Crossfire::Client::Texture->new_from_opengl ( 325 CFClient::Texture->new_from_opengl (
275 $w, $h, sub { $chld->draw } 326 $self->{w}, $self->{h}, sub { $self->child->draw }
276 ); 327 );
277 $self->{texture}->upload;
278}
279
280sub size_request {
281 my ($self) = @_;
282 ($self->w, $self->h)
283} 328}
284 329
285sub size_allocate { 330sub size_allocate {
286 my ($self, $w, $h) = @_; 331 my ($self, $w, $h) = @_;
287 332
288 $self->w ($w); 333 $self->{w} = $w;
289 $self->h ($h); 334 $self->{h} = $h;
335
290 $self->get->size_allocate ($w, $h); 336 $self->child->size_allocate ($w, $h);
291 337
292 $self->update; #TODO: Move this to the size_request event propably? 338 $self->render_chld;
293} 339}
294 340
295sub _draw { 341sub _draw {
296 my ($self) = @_; 342 my ($self) = @_;
297 343
301 or return; 347 or return;
302 348
303 glEnable GL_BLEND; 349 glEnable GL_BLEND;
304 glEnable GL_TEXTURE_2D; 350 glEnable GL_TEXTURE_2D;
305 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 351 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
306 glBindTexture GL_TEXTURE_2D, $tex->{name};
307 352
308 glBegin GL_QUADS; 353 $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 354
315 glDisable GL_BLEND; 355 glDisable GL_BLEND;
316 glDisable GL_TEXTURE_2D; 356 glDisable GL_TEXTURE_2D;
317} 357}
318 358
319############################################################################# 359#############################################################################
320 360
321package Crossfire::Client::Widget::Frame; 361package CFClient::Widget::Frame;
322 362
323our @ISA = Crossfire::Client::Widget::Bin::; 363our @ISA = CFClient::Widget::Bin::;
324 364
325use SDL::OpenGL; 365use SDL::OpenGL;
326 366
327sub size_request { 367sub size_request {
328 my ($self) = @_; 368 my ($self) = @_;
334 map { $_ + 4 } $chld->size_request; 374 map { $_ + 4 } $chld->size_request;
335} 375}
336 376
337sub size_allocate { 377sub size_allocate {
338 my ($self, $w, $h) = @_; 378 my ($self, $w, $h) = @_;
339 379
340 $self->w ($w); 380 $self->{w} = $w;
341 $self->h ($h); 381 $self->{h} = $h;
342 382
343 $self->child->size_allocate ($w - 4, $h - 4); 383 $self->child->size_allocate ($w - 4, $h - 4);
344 $self->child->move (2, 2); 384 $self->child->move (2, 2);
345} 385}
346 386
351 391
352 my ($w, $h) = $chld->size_request; 392 my ($w, $h) = $chld->size_request;
353 393
354 glBegin GL_QUADS; 394 glBegin GL_QUADS;
355 glColor 0, 0, 0; 395 glColor 0, 0, 0;
356 glTexCoord 0, 0; glVertex 0 , 0; 396 glVertex 0 , 0;
357 glTexCoord 0, 1; glVertex 0 , $h + 4; 397 glVertex 0 , $h + 4;
358 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 398 glVertex $w + 4 , $h + 4;
359 glTexCoord 1, 0; glVertex $w + 4 , 0; 399 glVertex $w + 4 , 0;
360 glEnd; 400 glEnd;
361 401
362 $chld->draw; 402 $chld->draw;
363} 403}
364 404
365############################################################################# 405#############################################################################
366 406
367package Crossfire::Client::Widget::FancyFrame; 407package CFClient::Widget::FancyFrame;
368 408
369our @ISA = Crossfire::Client::Widget::Frame::; 409our @ISA = CFClient::Widget::Bin::;
370 410
371use SDL::OpenGL; 411use SDL::OpenGL;
372 412
373sub new { 413my @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 $_ } 414 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) 415 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 416
384sub size_request { 417sub size_request {
385 my ($self) = @_; 418 my ($self) = @_;
386 419
387 my ($w, $h) = $self->SUPER::size_request; 420 my ($w, $h) = $self->SUPER::size_request;
388 421
389 $h += $self->{txts}->[1]->{height}; 422 $h += $tex[1]->{height};
390 $h += $self->{txts}->[4]->{height}; 423 $h += $tex[4]->{height};
391 $w += $self->{txts}->[2]->{width}; 424 $w += $tex[2]->{width};
392 $w += $self->{txts}->[3]->{width}; 425 $w += $tex[3]->{width};
393 426
394 ($w, $h) 427 ($w, $h)
395} 428}
396 429
397sub size_allocate { 430sub size_allocate {
398 my ($self, $w, $h) = @_; 431 my ($self, $w, $h) = @_;
399 432
400 $self->SUPER::size_allocate ($w, $h); 433 $self->SUPER::size_allocate ($w, $h);
401 434
402 $h -= $self->{txts}->[1]->{height}; 435 $h -= $tex[1]->{height};
403 $h -= $self->{txts}->[4]->{height}; 436 $h -= $tex[4]->{height};
404 $w -= $self->{txts}->[2]->{width}; 437 $w -= $tex[2]->{width};
405 $w -= $self->{txts}->[3]->{width}; 438 $w -= $tex[3]->{width};
406 439
407 $h = $h < 0 ? 0 : $h; 440 $h = $h < 0 ? 0 : $h;
408 $w = $w < 0 ? 0 : $w; 441 $w = $w < 0 ? 0 : $w;
409 warn "CHILD:$w $h\n"; 442
410 $self->child->size_allocate ($w, $h); 443 $self->child->size_allocate ($w, $h);
411 $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 444 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
412} 445}
413 446
414sub _draw { 447sub _draw {
415 my ($self) = @_; 448 my ($self) = @_;
416 449
417 my ($w, $h) = ($self->w, $self->h); 450 my ($w, $h) = ($self->{w}, $self->{h});
418 my ($cw, $ch) = ($self->child->w, $self->child->h); 451 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
419 452
420 glEnable GL_BLEND; 453 glEnable GL_BLEND;
421 glEnable GL_TEXTURE_2D; 454 glEnable GL_TEXTURE_2D;
422 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 455 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
423 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
424 457
425 my $top = $self->{txts}->[1]; 458 my $top = $tex[1];
426 glBindTexture GL_TEXTURE_2D, $top->{name}; 459 $top->draw_quad (0, 0, $w, $top->{height});
427 460
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]; 461 my $left = $tex[3];
436 glBindTexture GL_TEXTURE_2D, $left->{name}; 462 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
437 463
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]; 464 my $right = $tex[2];
446 glBindTexture GL_TEXTURE_2D, $right->{name}; 465 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
447 466
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]; 467 my $bottom = $tex[4];
456 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 468 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
457 469
458 glBegin GL_QUADS; 470 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}; 471 glBindTexture GL_TEXTURE_2D, $bg->{name};
467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 472 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 473 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
469 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 474 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
470 475
471 my $rep_x = $cw / $bg->{width}; 476 my $rep_x = $cw / $bg->{width};
472 my $rep_y = $ch / $bg->{height}; 477 my $rep_y = $ch / $bg->{height};
473 478
474 glBegin GL_QUADS; 479 $bg->draw_quad ($left->{width}, $top->{height}, $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 480
481 glDisable GL_BLEND; 481 glDisable GL_BLEND;
482 glDisable GL_TEXTURE_2D; 482 glDisable GL_TEXTURE_2D;
483 483
484 $self->child->draw; 484 $self->child->draw;
485 485
486} 486}
487 487
488############################################################################# 488#############################################################################
489 489
490package Crossfire::Client::Widget::Table; 490package CFClient::Widget::Table;
491 491
492our @ISA = Crossfire::Client::Widget::Bin::; 492our @ISA = CFClient::Widget::Bin::;
493 493
494use SDL::OpenGL; 494use SDL::OpenGL;
495 495
496sub add { 496sub add {
497 my ($self, $x, $y, $chld) = @_; 497 my ($self, $x, $y, $chld) = @_;
572 } 572 }
573} 573}
574 574
575############################################################################# 575#############################################################################
576 576
577package Crossfire::Client::Widget::VBox; 577package CFClient::Widget::VBox;
578 578
579our @ISA = Crossfire::Client::Widget::Container::; 579our @ISA = CFClient::Widget::Container::;
580 580
581use SDL::OpenGL; 581use SDL::OpenGL;
582
583sub size_request {
584 my ($self) = @_;
585
586 my @alloc = map [$_->size_request], @{$self->{children}};
587
588 (
589 (List::Util::max map $_->[0], @alloc),
590 (List::Util::sum map $_->[1], @alloc),
591 )
592}
582 593
583sub size_allocate { 594sub size_allocate {
584 my ($self, $w, $h) = @_; 595 my ($self, $w, $h) = @_;
585 596
586 $self->w ($w); 597 $self->w ($w);
619 $y += $h; 630 $y += $h;
620 } 631 }
621 } 632 }
622} 633}
623 634
624sub _draw {
625 my ($self) = @_;
626
627 my ($x, $y);
628 for (@{$self->{children} || []}) {
629 $_->draw;
630 $y += $_->h;
631 }
632}
633
634############################################################################# 635#############################################################################
635 636
636package Crossfire::Client::Widget::Label; 637package CFClient::Widget::Label;
637 638
638our @ISA = Crossfire::Client::Widget::; 639our @ISA = CFClient::Widget::;
639 640
640use SDL::OpenGL; 641use SDL::OpenGL;
641 642
642sub new { 643sub new {
643 my ($class, $x, $y, $z, $height, $text) = @_; 644 my ($class, %arg) = @_;
644 645
645 # TODO: color, and make height, xyz etc. optional 646 # TODO: color, and make height, xyz etc. optional
646 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 647 my $self = $class->SUPER::new (
648 color => [1, 1, 1],
649 height => $::FONTSIZE,
650 text => "",
651 layout => new CFClient::Layout,
652 %arg
653 );
647 654
648 $self->set_text ($text); 655 $self->set_text ($self->{text});
649 656
650 $self 657 $self
651} 658}
652 659
653sub set_text { 660sub set_text {
654 my ($self, $text) = @_; 661 my ($self, $text) = @_;
655 662
656 $self->{text} = $text; 663 $self->{text} = $text;
657 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 664 $self->{layout}->set_markup ($text);
658 665
659 $self->update; 666 delete $self->{texture};
660} 667}
661 668
662sub get_text { 669sub get_text {
663 my ($self, $text) = @_; 670 my ($self, $text) = @_;
664 671
666} 673}
667 674
668sub size_request { 675sub size_request {
669 my ($self) = @_; 676 my ($self) = @_;
670 677
671 ( 678 $self->{layout}->set_width;
679 $self->{layout}->set_height ($self->{height});
680 $self->{layout}->size
681# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
682# (
672 $self->{texture}{width}, 683# $self->{texture}{width},
673 $self->{texture}{height}, 684# $self->{texture}{height},
674 ) 685# )
686# } else {
687# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
688#
689# ($w, $h)
690# }
691}
692
693sub size_allocate {
694 my ($self, $w, $h) = @_;
695
696 $self->SUPER::size_allocate ($w, $h);
697 delete $self->{texture};
675} 698}
676 699
677sub _draw { 700sub _draw {
678 my ($self) = @_; 701 my ($self) = @_;
679 702
680 my $tex = $self->{texture}; 703 my $tex = $self->{texture} ||= do {
704 $self->{layout}->set_width ($self->{w});
705 new_from_layout CFClient::Texture $self->{layout};
706 };
681 707
682 glEnable GL_BLEND; 708 glEnable GL_BLEND;
683 glEnable GL_TEXTURE_2D; 709 glEnable GL_TEXTURE_2D;
684 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
685 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 711 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
686 glBindTexture GL_TEXTURE_2D, $tex->{name};
687 712
688 glColor 1, 0, 0, 1; # TODO color 713 glColor @{$self->{color}};
689 714
690 glBegin GL_QUADS; 715 $tex->draw_quad (0, 0);
691 glTexCoord 0, 0; glVertex 0 , 0;
692 glTexCoord 0, 1; glVertex 0 , $tex->{height};
693 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
694 glTexCoord 1, 0; glVertex $tex->{width}, 0;
695 glEnd;
696 716
697 glDisable GL_BLEND; 717 glDisable GL_BLEND;
698 glDisable GL_TEXTURE_2D; 718 glDisable GL_TEXTURE_2D;
699} 719}
700 720
701############################################################################# 721#############################################################################
702 722
703package Crossfire::Client::Widget::TextEntry; 723package CFClient::Widget::Entry;
704 724
705our @ISA = Crossfire::Client::Widget::Label::; 725our @ISA = CFClient::Widget::Label::;
706 726
707use SDL; 727use SDL;
708use SDL::OpenGL; 728use SDL::OpenGL;
709 729
710sub key_down { 730sub key_down {
711 my ($self, $ev) = @_; 731 my ($self, $ev) = @_;
712 732
713 my $mod = $ev->key_mod; 733 my $mod = $ev->key_mod;
714 my $sym = $ev->key_sym; 734 my $sym = $ev->key_sym;
715 735
716 $ev->set_unicode (1);
717 my $uni = $ev->key_unicode; 736 my $uni = $ev->key_unicode;
718 737
719 my $text = $self->get_text; 738 my $text = $self->get_text;
720 739
721 if ($sym == SDLK_BACKSPACE) { 740 if ($sym == SDLK_BACKSPACE) {
722 substr $text, -1, 1, ''; 741 substr $text, -1, 1, '';
723
724 } elsif ($uni) { 742 } elsif ($uni) {
725 $text .= chr $uni; 743 $text .= chr $uni;
726 } 744 }
745
727 $self->set_text ($text); 746 $self->set_text ($text);
728} 747}
729 748
730############################################################################# 749sub button_down {
750 my ($self, $ev) = @_;
731 751
752 $self->focus_in;
753}
754
755sub mouse_motion {
756 my ($self, $ev, $x, $y) = @_;
757 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
758}
759
760sub _draw {
761 my ($self) = @_;
762
763 if ($FOCUS == $self) {
764 glColor 1, 1, 1;
765 } else {
766 glColor 0.7, 0.7, 0.7;
767 }
768
769 glBegin GL_QUADS;
770 glVertex 0 , 0;
771 glVertex 0 , $self->{h} - 1;
772 glVertex $self->{w} - 1, $self->{h} - 1;
773 glVertex $self->{w} - 1, 0;
774 glEnd;
775
776 $self->SUPER::_draw;
777}
778
779#############################################################################
780
732package Crossfire::Client::Widget::MapWidget; 781package CFClient::Widget::MapWidget;
733 782
734use strict; 783use strict;
735 784
736use List::Util qw(min max); 785use List::Util qw(min max);
737 786
738use SDL; 787use SDL;
739use SDL::OpenGL; 788use SDL::OpenGL;
740use SDL::OpenGL::Constants; 789use SDL::OpenGL::Constants;
741 790
742our @ISA = Crossfire::Client::Widget::; 791our @ISA = CFClient::Widget::;
792
793sub new {
794 my $class = shift;
795
796 $class->SUPER::new (
797 z => -1,
798 list => (glGenLists 1),
799 @_
800 )
801}
743 802
744sub key_down { 803sub key_down {
745 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
746} 805}
747 806
748sub key_up { 807sub key_up {
749} 808}
750 809
751sub size_request { 810sub size_request {
752 811 (
812 1 + int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32,
814 )
753} 815}
754 816
755sub size_allocate { 817sub update {
818 my ($self) = @_;
819
820 $self->{need_update} = 1;
756} 821}
757 822
758sub _draw { 823sub _draw {
759 my ($self) = @_; 824 my ($self) = @_;
760 825
826 if (delete $self->{need_update}) {
827 glNewList $self->{list}, GL_COMPILE;
828
761 my $mx = $::CONN->{mapx}; 829 my $mx = $::CONN->{mapx};
762 my $my = $::CONN->{mapy}; 830 my $my = $::CONN->{mapy};
763 831
764 my $map = $::CONN->{map}; 832 my $map = $::CONN->{map};
765 833
766 my ($xofs, $yofs); 834 my ($xofs, $yofs);
767 835
768 my $sw = 1 + int $::WIDTH / 32; 836 my $sw = 1 + int $::WIDTH / 32;
769 my $sh = 1 + int $::HEIGHT / 32; 837 my $sh = 1 + int $::HEIGHT / 32;
770 838
771 if ($::CONN->{mapw} > $sw) { 839 if ($::CONN->{mapw} > $sw) {
772 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 840 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
773 } else { 841 } else {
774 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 842 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
775 } 843 }
776 844
777 if ($::CONN->{maph} > $sh) { 845 if ($::CONN->{maph} > $sh) {
778 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 846 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
779 } else { 847 } else {
780 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 848 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
781 } 849 }
782 850
783 glEnable GL_TEXTURE_2D; 851 glEnable GL_TEXTURE_2D;
784 glEnable GL_BLEND; 852 glEnable GL_BLEND;
785 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 853 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
786 854
787 my $sw4 = ($sw + 3) & ~3; 855 my $sw4 = ($sw + 3) & ~3;
788 my $lighting = "\x00" x ($sw4 * $sh); 856 my $darkness = "\x00" x ($sw4 * $sh);
789 857
790 for my $x (0 .. $sw - 1) { 858 for my $x (0 .. $sw - 1) {
859 my $row = $map->[$x + $xofs];
791 for my $y (0 .. $sh - 1) { 860 for my $y (0 .. $sh - 1) {
792 861
793 my $cell = $map->[$x + $xofs][$y + $yofs] 862 my $cell = $row->[$y + $yofs]
794 or next; 863 or next;
795 864
796 my $darkness = $cell->[0] * (1 / 255); 865 my $dark = $cell->[0];
797 if ($darkness < 0) { 866 if ($dark < 0) {
798 $darkness = 0.15; 867 substr $darkness, $y * $sw4 + $x, 1, chr 224;
868 } else {
869 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
799 } 870 }
800 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
801 871
802 for my $num (grep $_, @$cell[1,2,3]) { 872 for my $num (grep $_, @$cell[1,2,3]) {
803 my $tex = $::CONN->{face}[$num]{texture} || next; 873 my $tex = $::CONN->{face}[$num]{texture} || next;
804 874
805 glBindTexture GL_TEXTURE_2D, $tex->{name};
806
807 my $w = $tex->{width}; 875 my $w = $tex->{width};
808 my $h = $tex->{height}; 876 my $h = $tex->{height};
809 877
810 my $px = ($x + 1) * 32 - $w; 878 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
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; 879 }
819 } 880 }
820 } 881 }
821 }
822 882
823# if (1) { # higher quality darkness 883# if (1) { # higher quality darkness
824# $lighting =~ s/(.)/$1$1$1/gs; 884# $lighting =~ s/(.)/$1$1$1/gs;
825# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 885# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
826# 886#
828# 888#
829# $lighting = $pb->get_pixels; 889# $lighting = $pb->get_pixels;
830# $lighting =~ s/(.)../$1/gs; 890# $lighting =~ s/(.)../$1/gs;
831# } 891# }
832 892
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; 893 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; 894 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 895
896 $darkness = new CFClient::Texture
897 width => $sw4,
898 height => $sh,
899 data => $darkness,
900 internalformat => GL_ALPHA,
901 format => GL_ALPHA;
902
903 glColor 0.45, 0.45, 0.45, 1;
904 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
905
852 glDisable GL_TEXTURE_2D; 906 glDisable GL_TEXTURE_2D;
853 glDisable GL_BLEND; 907 glDisable GL_BLEND;
908
909 glEndList;
910 }
911
912 glCallList $self->{list};
854} 913}
855 914
856my %DIR = ( 915my %DIR = (
857 SDLK_KP8, [1, "north"], 916 SDLK_KP8, [1, "north"],
858 SDLK_KP9, [2, "northeast"], 917 SDLK_KP9, [2, "northeast"],
904 } 963 }
905} 964}
906 965
907############################################################################# 966#############################################################################
908 967
909package Crossfire::Client::Widget::Animator; 968package CFClient::Widget::Animator;
910 969
911use SDL::OpenGL; 970use SDL::OpenGL;
912 971
913our @ISA = Crossfire::Client::Widget::Bin::; 972our @ISA = CFClient::Widget::Bin::;
914 973
915sub moveto { 974sub moveto {
916 my ($self, $x, $y) = @_; 975 my ($self, $x, $y) = @_;
917 976
918 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
919 $self->{speed} = 0.2; 978 $self->{speed} = 0.001;
920 $self->{time} = 1; 979 $self->{time} = 1;
921 980
922 ::animation_start $self; 981 ::animation_start $self;
923} 982}
924 983
939 998
940sub _draw { 999sub _draw {
941 my ($self) = @_; 1000 my ($self) = @_;
942 1001
943 glPushMatrix; 1002 glPushMatrix;
944 glRotate $self->{time} * 10000, 0, 1, 0; 1003 glRotate $self->{time} * 1000, 0, 1, 0;
945 $self->{children}[0]->draw; 1004 $self->{children}[0]->draw;
946 glPopMatrix; 1005 glPopMatrix;
947} 1006}
948 1007
9491; 1008#############################################################################
950 1009
1010package CFClient::Widget::Toplevel;
1011
1012our @ISA = CFClient::Widget::Container::;
1013
1014sub size_request {
1015 ($::WIDTH, $::HEIGHT)
1016}
1017
1018sub size_allocate {
1019 my ($self, $w, $h) = @_;
1020
1021 $self->SUPER::size_allocate ($w, $h);
1022
1023 $_->size_allocate ($_->size_request)
1024 for @{$self->{children}};
1025}
1026
1027sub translate {
1028 my ($self, $x, $y) = @_;
1029
1030 ($x, $y)
1031}
1032
1033sub update {
1034 my ($self) = @_;
1035
1036 $self->size_allocate ($self->size_request);
1037 ::refresh ();
1038}
1039
1040sub add {
1041 my ($self, $widget) = @_;
1042
1043 $self->SUPER::add ($widget);
1044
1045 $widget->size_allocate ($widget->size_request);
1046}
1047
1048sub draw {
1049 my ($self) = @_;
1050
1051 $self->_draw;
1052}
1053
1054#############################################################################
1055
1056package CFClient::Widget;
1057
1058$TOPLEVEL = new CFClient::Widget::Toplevel;
1059
10601
1061

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines