ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.38 by root, Sun Apr 9 21:34:49 2006 UTC vs.
Revision 1.59 by root, Tue Apr 11 13:05:12 2006 UTC

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 Crossfire::Client;
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 { @_ }, $class
37 Carp::confess "size_request is abtract"; 94 Carp::confess "size_request is abtract";
38} 95}
39 96
40sub size_allocate { 97sub size_allocate {
41 my ($self, $w, $h) = @_; 98 my ($self, $w, $h) = @_;
99
42 $self->w ($w); 100 $self->{w} = $w;
43 $self->h ($h); 101 $self->{h} = $h;
102}
103
104# translate global koordinates to local coordinate system
105sub translate {
106 my ($self, $x, $y) = @_;
107
108 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
44} 109}
45 110
46sub focus_in { 111sub focus_in {
47 my ($widget) = @_; 112 my ($self) = @_;
48 $FOCUS = $widget; 113
114 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus;
116 $FOCUS->update;
49} 117}
50 118
51sub focus_out { 119sub focus_out {
52 my ($widget) = @_; 120 my ($self) = @_;
53}
54 121
55sub key_down { 122 return unless $FOCUS == $self;
56 my ($widget, $sdlev) = @_;
57}
58 123
59sub key_up { 124 my $focus = $FOCUS; undef $FOCUS;
60 my ($widget, $sdlev) = @_; 125 $focus->update if $focus; #?
61} 126}
62 127
128sub mouse_motion { }
129sub button_up { }
63sub button_down { 130sub button_down { }
64 my ($widget, $sdlev) = @_; 131sub key_down { }
65} 132sub key_up { }
66 133
67sub button_up {
68 my ($widget, $sdlev) = @_;
69}
70
71sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } 134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
72sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } 135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
73sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
74sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
75sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
76 139
77sub draw { 140sub draw {
78 my ($self) = @_; 141 my ($self) = @_;
79 142
80 glPushMatrix; 143 glPushMatrix;
81 glTranslate $self->{x}, $self->{y}, 0; 144 glTranslate $self->{x}, $self->{y}, 0;
82 $self->_draw; 145 $self->_draw;
146 if ($self == $HOVER) {
147 glColor 1, 1, 1, 0.4;
148 glEnable GL_BLEND;
149 glBegin GL_QUADS;
150 glVertex 0 , 0;
151 glVertex $self->{w}, 0;
152 glVertex $self->{w}, $self->{h};
153 glVertex 0 , $self->{h};
154 glEnd;
155 glDisable GL_BLEND;
156 }
83 glPopMatrix; 157 glPopMatrix;
84} 158}
85 159
86sub _draw { 160sub _draw {
87 my ($self) = @_; 161 my ($self) = @_;
134 my ($self) = @_; 208 my ($self) = @_;
135 209
136 #$self->deactivate; 210 #$self->deactivate;
137} 211}
138 212
213#############################################################################
214
139package Crossfire::Client::Widget::Container; 215package Crossfire::Client::Widget::Container;
140 216
141our @ISA = Crossfire::Client::Widget::; 217our @ISA = Crossfire::Client::Widget::;
142 218
143sub new { 219sub new {
157 233
158 @{$self->{children}} = 234 @{$self->{children}} =
159 sort { $a->{z} <=> $b->{z} } 235 sort { $a->{z} <=> $b->{z} }
160 @{$self->{children}}, $chld; 236 @{$self->{children}}, $chld;
161 237
162 $self->size_allocate ($self->{w}, $self->{h}); 238 $self->size_allocate ($self->{w}, $self->{h})
239 if $self->{w}; #TODO: check for "realised state"
163} 240}
164 241
165sub remove { 242sub remove {
166 my ($self, $widget) = @_; 243 my ($self, $widget) = @_;
167 244
171} 248}
172 249
173sub find_widget { 250sub find_widget {
174 my ($self, $x, $y) = @_; 251 my ($self, $x, $y) = @_;
175 252
253 $x -= $self->{x};
254 $y -= $self->{y};
255
176 my $res; 256 my $res;
177 257
178 for (@{ $self->{children} }) { 258 for (reverse @{ $self->{children} }) {
179 $res = $_->find_widget ($x, $y) 259 $res = $_->find_widget ($x, $y)
180 and return $res; 260 and return $res;
181 } 261 }
182 262
183 () 263 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
184}
185
186sub size_request {
187 my ($self) = @_;
188
189 my ($hs, $ws) = (0, 0);
190 for (@{$self->{children} || []}) {
191 my ($w, $h) = $_->size_request;
192 $hs += $h;
193 if ($ws < $w) { $ws = $w }
194 }
195
196 return ($ws, $hs);
197} 264}
198 265
199sub _draw { 266sub _draw {
200 my ($self) = @_; 267 my ($self) = @_;
201 268
202 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
203} 270}
204 271
272#############################################################################
273
205package Crossfire::Client::Widget::Bin; 274package Crossfire::Client::Widget::Bin;
206 275
207our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = Crossfire::Client::Widget::Container::;
208 277
209sub get { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
210 279
211sub size_request { 280sub size_request {
212 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
213} 282}
214 283
215sub size_allocate { 284sub size_allocate {
216 my ($self, $w, $h) = @_; 285 my ($self, $w, $h) = @_;
286
217 $self->SUPER::size_allocate ($w, $h); 287 $self->SUPER::size_allocate ($w, $h);
218 $self->{children}[0]->size_allocate ($w, $h) 288 $self->{children}[0]->size_allocate ($w, $h)
219 if $self->{children}[0] 289 if $self->{children}[0]
220} 290}
221 291
292#############################################################################
293
222package Crossfire::Client::Widget::Toplevel; 294package Crossfire::Client::Widget::Window;
223 295
224our @ISA = Crossfire::Client::Widget::Container::; 296our @ISA = Crossfire::Client::Widget::Bin::;
297
298use SDL::OpenGL;
299
300sub new {
301 my ($class, $x, $y, $z, $w, $h) = @_;
302
303 my $self = $class->SUPER::new;
304
305 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
306}
225 307
226sub update { 308sub update {
227 my ($self) = @_; 309 my ($self) = @_;
228 310
229 ::refresh ();
230}
231
232package Crossfire::Client::Widget::Window;
233
234our @ISA = Crossfire::Client::Widget::Bin::;
235
236use SDL::OpenGL;
237
238sub add {
239 my ($self, $chld) = @_;
240 warn "ADD $chld\n";
241 $self->SUPER::add ($chld);
242 $chld->set_parent ($self);
243}
244
245sub remove {
246 my ($self) = @_;
247 # TODO FIXME: removing a child from a window will crash, see render_chld
248 # $self->update;
249}
250
251sub update {
252 my ($self) = @_;
253 $self->render_chld; 311 $self->render_chld;
312 $self->SUPER::update;
254} 313}
255 314
256sub render_chld { 315sub render_chld {
257 my ($self) = @_; 316 my ($self) = @_;
258 my $chld = $self->get;
259 my ($w, $h) = $self->size_request;
260 317
261 require Carp;
262 Carp::cluck "RENDERCHI $w $h";
263 warn "RENDERCHI $w $h\n";
264 $self->{texture} = 318 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 319 Crossfire::Client::Texture->new_from_opengl (
266 $w, $h, sub { $chld->draw } 320 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 321 );
268 $self->{texture}->upload;
269}
270
271sub size_request {
272 my ($self) = @_;
273 ($self->w, $self->h)
274} 322}
275 323
276sub size_allocate { 324sub size_allocate {
277 my ($self, $w, $h) = @_; 325 my ($self, $w, $h) = @_;
278 326
279 $self->w ($w); 327 $self->{w} = $w;
280 $self->h ($h); 328 $self->{h} = $h;
329
281 $self->get->size_allocate ($w, $h); 330 $self->child->size_allocate ($w, $h);
282 331
283 $self->update; #TODO: Move this to the size_request event propably? 332 $self->render_chld;
284} 333}
285 334
286sub _draw { 335sub _draw {
287 my ($self) = @_; 336 my ($self) = @_;
288 337
292 or return; 341 or return;
293 342
294 glEnable GL_BLEND; 343 glEnable GL_BLEND;
295 glEnable GL_TEXTURE_2D; 344 glEnable GL_TEXTURE_2D;
296 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 345 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
297 glBindTexture GL_TEXTURE_2D, $tex->{name};
298 346
299 glBegin GL_QUADS; 347 $tex->draw_quad (0, 0, $w, $h);
300 glTexCoord 0, 0; glVertex 0, 0;
301 glTexCoord 0, 1; glVertex 0, $h;
302 glTexCoord 1, 1; glVertex $w, $h;
303 glTexCoord 1, 0; glVertex $w, 0;
304 glEnd;
305 348
306 glDisable GL_BLEND; 349 glDisable GL_BLEND;
307 glDisable GL_TEXTURE_2D; 350 glDisable GL_TEXTURE_2D;
308} 351}
309 352
353#############################################################################
354
310package Crossfire::Client::Widget::Frame; 355package Crossfire::Client::Widget::Frame;
311 356
312our @ISA = Crossfire::Client::Widget::Bin::; 357our @ISA = Crossfire::Client::Widget::Bin::;
313 358
314use SDL::OpenGL; 359use SDL::OpenGL;
315 360
316sub size_request { 361sub size_request {
317 my ($self) = @_; 362 my ($self) = @_;
318 my $chld = $self->get 363 my $chld = $self->child
319 or return (0, 0); 364 or return (0, 0);
320 365
321 $chld->move (2, 2); 366 $chld->move (2, 2);
322 367
323 map { $_ + 4 } $chld->size_request; 368 map { $_ + 4 } $chld->size_request;
324} 369}
325 370
326sub size_allocate { 371sub size_allocate {
327 my ($self, $w, $h) = @_; 372 my ($self, $w, $h) = @_;
328 373
329 $self->w ($w); 374 $self->{w} = $w;
330 $self->h ($h); 375 $self->{h} = $h;
331 376
332 $self->get->size_allocate ($w - 4, $h - 4); 377 $self->child->size_allocate ($w - 4, $h - 4);
333 $self->get->move (2, 2); 378 $self->child->move (2, 2);
334} 379}
335 380
336sub _draw { 381sub _draw {
337 my ($self) = @_; 382 my ($self) = @_;
338 383
339 my $chld = $self->get; 384 my $chld = $self->child;
340 385
341 my ($w, $h) = $chld->size_request; 386 my ($w, $h) = $chld->size_request;
342 387
343 glBegin GL_QUADS; 388 glBegin GL_QUADS;
344 glColor 0, 0, 0; 389 glColor 0, 0, 0;
345 glTexCoord 0, 0; glVertex 0 , 0; 390 glVertex 0 , 0;
346 glTexCoord 0, 1; glVertex 0 , $h + 4; 391 glVertex 0 , $h + 4;
347 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 392 glVertex $w + 4 , $h + 4;
348 glTexCoord 1, 0; glVertex $w + 4 , 0; 393 glVertex $w + 4 , 0;
349 glEnd; 394 glEnd;
350 395
351 $chld->draw; 396 $chld->draw;
352} 397}
353 398
399#############################################################################
400
354package Crossfire::Client::Widget::FancyFrame; 401package Crossfire::Client::Widget::FancyFrame;
355 402
356our @ISA = Crossfire::Client::Widget::Frame::; 403our @ISA = Crossfire::Client::Widget::Bin::;
357 404
358use SDL::OpenGL; 405use SDL::OpenGL;
359 406
360sub new { 407my @tex =
361 my ($self, $theme) = @_;
362 $self = $self->SUPER::new;
363
364 $self->{txts} = [
365 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 408 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
366 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 409 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
367 ];
368 $self
369}
370 410
371sub size_request { 411sub size_request {
372 my ($self) = @_; 412 my ($self) = @_;
413
373 my ($w, $h) = $self->get->size_request; 414 my ($w, $h) = $self->SUPER::size_request;
374 415
375 $h += $self->{txts}->[1]->{height}; 416 $h += $tex[1]->{height};
376 $h += $self->{txts}->[4]->{height}; 417 $h += $tex[4]->{height};
377 $w += $self->{txts}->[2]->{width}; 418 $w += $tex[2]->{width};
378 $w += $self->{txts}->[3]->{width}; 419 $w += $tex[3]->{width};
379 420
380 ($w, $h) 421 ($w, $h)
381} 422}
382 423
383sub size_allocate { 424sub size_allocate {
384 my ($self, $w, $h) = @_; 425 my ($self, $w, $h) = @_;
385 426
386 $self->w ($w); 427 $self->SUPER::size_allocate ($w, $h);
387 $self->h ($h); 428
388 $h -= $self->{txts}->[1]->{height}; 429 $h -= $tex[1]->{height};
389 $h -= $self->{txts}->[4]->{height}; 430 $h -= $tex[4]->{height};
390 $w -= $self->{txts}->[2]->{width}; 431 $w -= $tex[2]->{width};
391 $w -= $self->{txts}->[3]->{width}; 432 $w -= $tex[3]->{width};
392 433
393 $h = $h < 0 ? 0 : $h; 434 $h = $h < 0 ? 0 : $h;
394 $w = $w < 0 ? 0 : $w; 435 $w = $w < 0 ? 0 : $w;
395 warn "CHILD:$w $h\n"; 436
396 $self->get->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
397 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 438 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
398} 439}
399 440
400sub _draw { 441sub _draw {
401 my ($self) = @_; 442 my ($self) = @_;
402 443
403 my ($w, $h) = ($self->w, $self->h); 444 my ($w, $h) = ($self->{w}, $self->{h});
404 my ($cw, $ch) = ($self->get->w, $self->get->h); 445 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
405 446
406 glEnable GL_BLEND; 447 glEnable GL_BLEND;
407 glEnable GL_TEXTURE_2D; 448 glEnable GL_TEXTURE_2D;
408 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 450 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
410 451
411 my $top = $self->{txts}->[1]; 452 my $top = $tex[1];
412 glBindTexture GL_TEXTURE_2D, $top->{name}; 453 $top->draw_quad (0, 0, $w, $top->{height});
413 454
414 glBegin GL_QUADS;
415 glTexCoord 0, 0; glVertex 0 , 0;
416 glTexCoord 0, 1; glVertex 0 , $top->{height};
417 glTexCoord 1, 1; glVertex $w , $top->{height};
418 glTexCoord 1, 0; glVertex $w , 0;
419 glEnd;
420
421 my $left = $self->{txts}->[3]; 455 my $left = $tex[3];
422 glBindTexture GL_TEXTURE_2D, $left->{name}; 456 $left->draw_quad (0, $top->{height}, $left->{width}, $ch);
423 457
424 glBegin GL_QUADS;
425 glTexCoord 0, 0; glVertex 0 , $top->{height};
426 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
427 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
428 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
429 glEnd;
430
431 my $right = $self->{txts}->[2]; 458 my $right = $tex[2];
432 glBindTexture GL_TEXTURE_2D, $right->{name}; 459 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch);
433 460
434 glBegin GL_QUADS;
435 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
436 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
437 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
438 glTexCoord 1, 0; glVertex $w , $top->{height};
439 glEnd;
440
441 my $bottom = $self->{txts}->[4]; 461 my $bottom = $tex[4];
442 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 462 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height});
443 463
444 glBegin GL_QUADS; 464 my $bg = $tex[0];
445 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
446 glTexCoord 0, 1; glVertex 0 , $h;
447 glTexCoord 1, 1; glVertex $w , $h;
448 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
449 glEnd;
450
451 my $bg = $self->{txts}->[0];
452 glBindTexture GL_TEXTURE_2D, $bg->{name}; 465 glBindTexture GL_TEXTURE_2D, $bg->{name};
453 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
454 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
455 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
456 469
457 my $rep_x = $cw / $bg->{width}; 470 my $rep_x = $cw / $bg->{width};
458 my $rep_y = $ch / $bg->{height}; 471 my $rep_y = $ch / $bg->{height};
459 472
460 glBegin GL_QUADS; 473 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch);
461 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
462 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
463 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
464 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
465 glEnd;
466 474
467 glDisable GL_BLEND; 475 glDisable GL_BLEND;
468 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
469 477
470 $self->get->draw; 478 $self->child->draw;
471 479
472} 480}
481
482#############################################################################
473 483
474package Crossfire::Client::Widget::Table; 484package Crossfire::Client::Widget::Table;
475 485
476our @ISA = Crossfire::Client::Widget::Bin::; 486our @ISA = Crossfire::Client::Widget::Bin::;
477 487
554 564
555 $y += $self->max_row_height ($yi); 565 $y += $self->max_row_height ($yi);
556 } 566 }
557} 567}
558 568
569#############################################################################
570
559package Crossfire::Client::Widget::VBox; 571package Crossfire::Client::Widget::VBox;
560 572
561our @ISA = Crossfire::Client::Widget::Container::; 573our @ISA = Crossfire::Client::Widget::Container::;
562 574
563use SDL::OpenGL; 575use SDL::OpenGL;
576
577sub size_request {
578 my ($self) = @_;
579
580 my @alloc = map [$_->size_request], @{$self->{children}};
581
582 (
583 (List::Util::max map $_->[0], @alloc),
584 (List::Util::sum map $_->[1], @alloc),
585 )
586}
564 587
565sub size_allocate { 588sub size_allocate {
566 my ($self, $w, $h) = @_; 589 my ($self, $w, $h) = @_;
567 590
568 $self->w ($w); 591 $self->w ($w);
601 $y += $h; 624 $y += $h;
602 } 625 }
603 } 626 }
604} 627}
605 628
606sub _draw { 629#############################################################################
607 my ($self) = @_;
608
609 my ($x, $y);
610 for (@{$self->{children} || []}) {
611 $_->draw;
612 $y += $_->h;
613 }
614}
615 630
616package Crossfire::Client::Widget::Label; 631package Crossfire::Client::Widget::Label;
617 632
618our @ISA = Crossfire::Client::Widget::; 633our @ISA = Crossfire::Client::Widget::;
619 634
620use SDL::OpenGL; 635use SDL::OpenGL;
621 636
622sub new { 637sub new {
623 my ($class, $x, $y, $z, $height, $text) = @_; 638 my ($class, $x, $y, $z, $height, $text) = @_;
624 639
640 $height ||= $::FONTSIZE;
641
625 # TODO: color, and make height, xyz etc. optional 642 # TODO: color, and make height, xyz etc. optional
626 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 643 my $self = $class->SUPER::new (
644 x => $x,
645 y => $y,
646 z => $z,
647 height => $height,
648 layout => new Crossfire::Client::Layout,
649 );
627 650
628 $self->set_text ($text); 651 $self->set_text ($text);
629 652
630 $self 653 $self
631} 654}
632 655
633sub set_text { 656sub set_text {
634 my ($self, $text) = @_; 657 my ($self, $text) = @_;
635 658
636 $self->{text} = $text; 659 $self->{text} = $text;
637 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 660 $self->{layout}->set_markup ($text);
638 661
639 $self->update; 662 delete $self->{texture};
640} 663}
641 664
642sub get_text { 665sub get_text {
643 my ($self, $text) = @_; 666 my ($self, $text) = @_;
644 667
646} 669}
647 670
648sub size_request { 671sub size_request {
649 my ($self) = @_; 672 my ($self) = @_;
650 673
651 ( 674 $self->{layout}->set_width;
675 $self->{layout}->size
676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
677# (
652 $self->{texture}{width}, 678# $self->{texture}{width},
653 $self->{texture}{height}, 679# $self->{texture}{height},
654 ) 680# )
681# } else {
682# my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
683#
684# ($w, $h)
685# }
686}
687
688sub size_allocate {
689 my ($self, $w, $h) = @_;
690
691 $self->SUPER::size_allocate ($w, $h);
692 delete $self->{texture};
655} 693}
656 694
657sub _draw { 695sub _draw {
658 my ($self) = @_; 696 my ($self) = @_;
659 697
660 my $tex = $self->{texture}; 698 my $tex = $self->{texture} ||= do {
699 $self->{layout}->set_width ($self->{w});
700 new_from_layout Crossfire::Client::Texture $self->{layout};
701 };
661 702
662 glEnable GL_BLEND; 703 glEnable GL_BLEND;
663 glEnable GL_TEXTURE_2D; 704 glEnable GL_TEXTURE_2D;
664 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
666 glBindTexture GL_TEXTURE_2D, $tex->{name};
667 707
668 glColor 1, 0, 0, 1; # TODO color 708 glColor 1, 0, 0, 1; # TODO color
669 709
670 glBegin GL_QUADS; 710 $tex->draw_quad (0, 0);
671 glTexCoord 0, 0; glVertex 0 , 0;
672 glTexCoord 0, 1; glVertex 0 , $tex->{height};
673 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
674 glTexCoord 1, 0; glVertex $tex->{width}, 0;
675 glEnd;
676 711
677 glDisable GL_BLEND; 712 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
679} 714}
680 715
716#############################################################################
717
681package Crossfire::Client::Widget::TextEntry; 718package Crossfire::Client::Widget::Entry;
682 719
683our @ISA = Crossfire::Client::Widget::Label::; 720our @ISA = Crossfire::Client::Widget::Label::;
684 721
685use SDL; 722use SDL;
686use SDL::OpenGL; 723use SDL::OpenGL;
689 my ($self, $ev) = @_; 726 my ($self, $ev) = @_;
690 727
691 my $mod = $ev->key_mod; 728 my $mod = $ev->key_mod;
692 my $sym = $ev->key_sym; 729 my $sym = $ev->key_sym;
693 730
694 $ev->set_unicode (1);
695 my $uni = $ev->key_unicode; 731 my $uni = $ev->key_unicode;
696 732
697 my $text = $self->get_text; 733 my $text = $self->get_text;
698 734
699 if ($sym == SDLK_BACKSPACE) { 735 if ($sym == SDLK_BACKSPACE) {
700 substr $text, -1, 1, ''; 736 substr $text, -1, 1, '';
701
702 } elsif ($uni) { 737 } elsif ($uni) {
703 $text .= chr $uni; 738 $text .= chr $uni;
704 } 739 }
740
705 $self->set_text ($text); 741 $self->set_text ($text);
706} 742}
743
744sub button_down {
745 my ($self, $ev) = @_;
746
747 $self->focus_in;
748}
749
750sub mouse_motion {
751 my ($self, $ev, $x, $y) = @_;
752 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
753}
754
755sub _draw {
756 my ($self) = @_;
757
758 if ($FOCUS == $self) {
759 glColor 1, 1, 1;
760 } else {
761 glColor 0.7, 0.7, 0.7;
762 }
763
764 glBegin GL_QUADS;
765 glVertex 0 , 0;
766 glVertex 0 , $self->{h} - 1;
767 glVertex $self->{w} - 1, $self->{h} - 1;
768 glVertex $self->{w} - 1, 0;
769 glEnd;
770
771 $self->SUPER::_draw;
772}
773
774#############################################################################
707 775
708package Crossfire::Client::Widget::MapWidget; 776package Crossfire::Client::Widget::MapWidget;
709 777
710use strict; 778use strict;
711 779
723 791
724sub key_up { 792sub key_up {
725} 793}
726 794
727sub size_request { 795sub size_request {
728 796 (
729} 797 1 + int $::WIDTH / 32,
730 798 1 + int $::HEIGHT / 32,
731sub size_allocate { 799 )
732} 800}
733 801
734sub _draw { 802sub _draw {
735 my ($self) = @_; 803 my ($self) = @_;
736 804
759 glEnable GL_TEXTURE_2D; 827 glEnable GL_TEXTURE_2D;
760 glEnable GL_BLEND; 828 glEnable GL_BLEND;
761 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 829 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
762 830
763 my $sw4 = ($sw + 3) & ~3; 831 my $sw4 = ($sw + 3) & ~3;
764 my $lighting = "\x00" x ($sw4 * $sh); 832 my $darkness = "\x00" x ($sw4 * $sh);
765 833
766 for my $x (0 .. $sw - 1) { 834 for my $x (0 .. $sw - 1) {
835 my $row = $map->[$x + $xofs];
767 for my $y (0 .. $sh - 1) { 836 for my $y (0 .. $sh - 1) {
768 837
769 my $cell = $map->[$x + $xofs][$y + $yofs] 838 my $cell = $row->[$y + $yofs]
770 or next; 839 or next;
771 840
772 my $darkness = $cell->[0] * (1 / 255); 841 my $dark = $cell->[0];
773 if ($darkness < 0) { 842 if ($dark < 0) {
774 $darkness = 0.15; 843 substr $darkness, $y * $sw4 + $x, 1, chr 224;
844 } else {
845 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
775 } 846 }
776 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
777 847
778 for my $num (grep $_, @$cell[1,2,3]) { 848 for my $num (grep $_, @$cell[1,2,3]) {
779 my $tex = $::CONN->{face}[$num]{texture} || next; 849 my $tex = $::CONN->{face}[$num]{texture} || next;
780 850
781 glBindTexture GL_TEXTURE_2D, $tex->{name};
782
783 my $w = $tex->{width}; 851 my $w = $tex->{width};
784 my $h = $tex->{height}; 852 my $h = $tex->{height};
785 853
786 my $px = ($x + 1) * 32 - $w; 854 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
787 my $py = ($y + 1) * 32 - $h;
788
789 glBegin GL_QUADS;
790 glTexCoord 0, 0; glVertex $px , $py;
791 glTexCoord 0, 1; glVertex $px , $py + $h;
792 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
793 glTexCoord 1, 0; glVertex $px + $w, $py;
794 glEnd;
795 } 855 }
796 } 856 }
797 } 857 }
798 858
799# if (1) { # higher quality darkness 859# if (1) { # higher quality darkness
804# 864#
805# $lighting = $pb->get_pixels; 865# $lighting = $pb->get_pixels;
806# $lighting =~ s/(.)../$1/gs; 866# $lighting =~ s/(.)../$1/gs;
807# } 867# }
808 868
869 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
870 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
871
809 $lighting = new Crossfire::Client::Texture 872 $darkness = new Crossfire::Client::Texture
810 width => $sw4, 873 width => $sw4,
811 height => $sh, 874 height => $sh,
812 data => $lighting, 875 data => $darkness,
813 internalformat => GL_ALPHA4, 876 internalformat => GL_ALPHA,
814 format => GL_ALPHA; 877 format => GL_ALPHA;
815 878
816 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 879 glColor 0.45, 0.45, 0.45, 1;
817 glColor 0, 0, 0, 0.75; 880 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
818 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
819 glBindTexture GL_TEXTURE_2D, $lighting->{name};
820 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
821 glBegin GL_QUADS;
822 glTexCoord 0, 0; glVertex 0 , 0;
823 glTexCoord 0, 1; glVertex 0 , $sh * 32;
824 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
825 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
826 glEnd;
827 881
828 glDisable GL_TEXTURE_2D; 882 glDisable GL_TEXTURE_2D;
829 glDisable GL_BLEND; 883 glDisable GL_BLEND;
830} 884}
831 885
878 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 932 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
879 $::CONN->send ("command run_stop"); 933 $::CONN->send ("command run_stop");
880 } 934 }
881} 935}
882 936
937#############################################################################
938
883package Crossfire::Client::Widget::Animator; 939package Crossfire::Client::Widget::Animator;
884 940
885use SDL::OpenGL; 941use SDL::OpenGL;
886 942
887our @ISA = Crossfire::Client::Widget::Bin::; 943our @ISA = Crossfire::Client::Widget::Bin::;
888 944
889sub moveto { 945sub moveto {
890 my ($self, $x, $y) = @_; 946 my ($self, $x, $y) = @_;
891 947
892 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 948 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
893 $self->{speed} = 0.2; 949 $self->{speed} = 0.001;
894 $self->{time} = 1; 950 $self->{time} = 1;
895 951
896 ::animation_start $self; 952 ::animation_start $self;
897} 953}
898 954
913 969
914sub _draw { 970sub _draw {
915 my ($self) = @_; 971 my ($self) = @_;
916 972
917 glPushMatrix; 973 glPushMatrix;
918 glRotate $self->{time} * 10000, 0, 1, 0; 974 glRotate $self->{time} * 1000, 0, 1, 0;
919 $self->{children}[0]->draw; 975 $self->{children}[0]->draw;
920 glPopMatrix; 976 glPopMatrix;
921} 977}
922 978
9231; 979#############################################################################
924 980
981package Crossfire::Client::Widget::Toplevel;
982
983our @ISA = Crossfire::Client::Widget::Container::;
984
985sub size_request {
986 ($::WIDTH, $::HEIGHT)
987}
988
989sub size_allocate {
990 my ($self, $w, $h) = @_;
991
992 $self->SUPER::size_allocate ($w, $h);
993
994 $_->size_allocate ($_->size_request)
995 for @{$self->{children}};
996}
997
998sub translate {
999 my ($self, $x, $y) = @_;
1000
1001 ($x, $y)
1002}
1003
1004sub update {
1005 my ($self) = @_;
1006
1007 $self->size_allocate ($self->size_request);
1008 ::refresh ();
1009}
1010
1011sub add {
1012 my ($self, $widget) = @_;
1013
1014 $self->SUPER::add ($widget);
1015
1016 $widget->size_allocate ($widget->size_request);
1017}
1018
1019sub draw {
1020 my ($self) = @_;
1021
1022 $self->_draw;
1023}
1024
1025#############################################################################
1026
1027package Crossfire::Client::Widget;
1028
1029$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1030
10311
1032

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines