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