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.34 by elmex, Sun Apr 9 02:44:51 2006 UTC vs.
Revision 1.55 by root, Mon Apr 10 19:34:04 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#our @ACTIVE_WIDGETS; 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets
13
14our $TOPLEVEL;
15our $BUTTON_STATE;
12 16
13# class methods for events 17# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 18sub feed_sdl_key_down_event {
15sub 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
16sub 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) if $GRAB;
40}
41
17sub 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 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab;
53 $GRAB->update if $GRAB;
54 }
55}
56
57sub feed_sdl_motion_event {
58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
62
63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget;
65
66 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev) if $HOVER;
71}
18 72
19sub new { 73sub new {
20 my $class = shift; 74 my $class = shift;
21 75
22 bless { @_ }, $class 76 bless { @_ }, $class
23} 77}
24
25#sub activate {
26# push @ACTIVE_WIDGETS, $_[0];
27# Scalar::Util::weaken $ACTIVE_WIDGETS[-1];
28#}
29
30#sub deactivate {
31# @ACTIVE_WIDGETS =
32# sort { $a->{z} <=> $b->{z} }
33# grep { $_ && $_ != $_[0] }
34# @ACTIVE_WIDGETS;
35#}
36 78
37sub move { 79sub move {
38 my ($self, $x, $y, $z) = @_; 80 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 81 $self->{x} = $x;
40 $self->{y} = $y; 82 $self->{y} = $y;
44sub needs_redraw { 86sub needs_redraw {
45 0 87 0
46} 88}
47 89
48sub size_request { 90sub size_request {
91 require Carp;
49 die "size_request is abtract"; 92 Carp::confess "size_request is abtract";
93}
94
95sub size_allocate {
96 my ($self, $w, $h) = @_;
97
98 $self->{w} = $w;
99 $self->{h} = $h;
50} 100}
51 101
52sub focus_in { 102sub focus_in {
53 my ($widget) = @_; 103 my ($self) = @_;
54 $FOCUS = $widget; 104
105 my $focus = $FOCUS; $FOCUS = $self;
106 $focus->update if $focus;
107 $FOCUS->update;
55} 108}
56 109
57sub focus_out { 110sub focus_out {
58 my ($widget) = @_; 111 my ($self) = @_;
59}
60 112
61sub key_down { 113 return unless $FOCUS == $self;
62 my ($widget, $sdlev) = @_;
63}
64 114
65sub key_up { 115 my $focus = $FOCUS; undef $FOCUS;
66 my ($widget, $sdlev) = @_; 116 $focus->update if $focus; #?
67} 117}
68 118
119sub mouse_motion { }
120sub button_up { }
69sub button_down { 121sub button_down { }
70 my ($widget, $sdlev) = @_; 122sub key_down { }
71} 123sub key_up { }
72 124
73sub button_up { 125sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
74 my ($widget, $sdlev) = @_; 126sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
75}
76
77sub w { $_[0]->{w} }
78sub h { $_[0]->{h} }
79sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 127sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
80sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 128sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
81sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 129sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
82 130
83sub draw { 131sub draw {
84 my ($self) = @_; 132 my ($self) = @_;
85 133
86 glPushMatrix; 134 glPushMatrix;
87 glTranslate $self->{x}, $self->{y}, 0; 135 glTranslate $self->{x}, $self->{y}, 0;
88 $self->_draw; 136 $self->_draw;
137 if ($self == $HOVER) {
138 glColor 1, 1, 1, 0.4;
139 glEnable GL_BLEND;
140 glBegin GL_QUADS;
141 glVertex 0 , 0;
142 glVertex $self->{w}, 0;
143 glVertex $self->{w}, $self->{h};
144 glVertex 0 , $self->{h};
145 glEnd;
146 glDisable GL_BLEND;
147 }
89 glPopMatrix; 148 glPopMatrix;
90} 149}
91 150
92sub _draw { 151sub _draw {
93 my ($widget) = @_; 152 my ($self) = @_;
153
154 warn "no draw defined for $self\n";
94} 155}
95 156
96sub bbox { 157sub bbox {
97 my ($self) = @_; 158 my ($self) = @_;
98 my ($w, $h) = $self->size_request; 159 my ($w, $h) = $self->size_request;
102 $self->{x} = $w, 163 $self->{x} = $w,
103 $self->{y} = $h 164 $self->{y} = $h
104 ) 165 )
105} 166}
106 167
168sub find_widget {
169 my ($self, $x, $y) = @_;
170
171 return $self
172 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
173 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
174
175 ()
176}
177
107sub del_parent { $_[0]->{parent} = undef } 178sub del_parent { $_[0]->{parent} = undef }
108 179
109sub set_parent { 180sub set_parent {
110 my ($self, $par) = @_; 181 my ($self, $par) = @_;
111 182
128 my ($self) = @_; 199 my ($self) = @_;
129 200
130 #$self->deactivate; 201 #$self->deactivate;
131} 202}
132 203
204#############################################################################
205
133package Crossfire::Client::Widget::Container; 206package Crossfire::Client::Widget::Container;
134 207
135our @ISA = Crossfire::Client::Widget::; 208our @ISA = Crossfire::Client::Widget::;
136 209
210sub new {
211 my ($class, @widgets) = @_;
212
213 my $self = $class->SUPER::new (children => []);
214 $self->add ($_) for @widgets;
215
216 $self
217}
218
219sub add {
220 my ($self, $chld, $expand) = @_;
221
222 $chld->{expand} = $expand;
223 $chld->set_parent ($self);
224
225 @{$self->{children}} =
226 sort { $a->{z} <=> $b->{z} }
227 @{$self->{children}}, $chld;
228
229 $self->size_allocate ($self->{w}, $self->{h})
230 if $self->{w}; #TODO: check for "realised state"
231}
232
233sub remove {
234 my ($self, $widget) = @_;
235
236 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
237
238 $self->size_allocate ($self->{w}, $self->{h});
239}
240
241sub find_widget {
242 my ($self, $x, $y) = @_;
243
244 $x -= $self->{x};
245 $y -= $self->{y};
246
247 my $res;
248
249 for (reverse @{ $self->{children} }) {
250 $res = $_->find_widget ($x, $y)
251 and return $res;
252 }
253
254 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
255}
256
257sub _draw {
258 my ($self) = @_;
259
260 $_->draw for @{$self->{children}};
261}
262
263#############################################################################
264
265package Crossfire::Client::Widget::Bin;
266
267our @ISA = Crossfire::Client::Widget::Container::;
268
269sub child { $_[0]->{children}[0] }
270
271sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0];
273}
274
275sub size_allocate {
276 my ($self, $w, $h) = @_;
277
278 $self->SUPER::size_allocate ($w, $h);
279 $self->{children}[0]->size_allocate ($w, $h)
280 if $self->{children}[0]
281}
282
283#############################################################################
284
285package Crossfire::Client::Widget::Window;
286
287our @ISA = Crossfire::Client::Widget::Bin::;
288
137use SDL::OpenGL; 289use SDL::OpenGL;
138 290
139sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update } 291sub new {
140sub get { $_[0]->{child} } 292 my ($class, $x, $y, $z, $w, $h) = @_;
141sub remove {
142 my ($self, $chld) = @_;
143 delete $self->{child}
144 if $self->{child} == $chld;
145}
146 293
147sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 294 my $self = $class->SUPER::new;
148 295
149sub _draw { die "Containers can't be drawn!" } 296 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
150
151package Crossfire::Client::Widget::Toplevel;
152
153our @ISA = Crossfire::Client::Widget::;
154
155use SDL::OpenGL;
156
157sub add {
158 my ($self, $chld) = @_;
159
160 push @{$self->{childs}}, $chld;
161 @{$self->{childs}} =
162 sort { $a->{z} <=> $b->{z} }
163 @{$self->{childs}};
164
165 $chld->set_parent ($self);
166}
167
168sub remove {
169 my ($self, $chld) = @_;
170 @{$self->{childs}} =
171 sort { $a->{z} <=> $b->{z} }
172 grep { $_ && $_ != $_[0] }
173 @{$self->{childs}}
174} 297}
175 298
176sub update { 299sub update {
177 my ($self) = @_; 300 my ($self) = @_;
178 ::refresh ();
179}
180 301
181sub _draw {
182 my ($self) = @_;
183
184 $_->draw for @{$self->{childs}};
185}
186
187package Crossfire::Client::Widget::Window;
188
189our @ISA = Crossfire::Client::Widget::Container::;
190
191use SDL::OpenGL;
192
193sub add {
194 my ($self, $chld) = @_;
195 $self->SUPER::add ($chld);
196 $chld->set_parent ($self);
197 $self->update; #TODO: Move this to the size_request event propably?
198}
199
200sub remove {
201 my ($self) = @_;
202 # TODO FIXME: removing a child from a window will crash, see render_chld
203 $self->update;
204}
205
206sub update {
207 my ($self) = @_;
208 $self->render_chld; 302 $self->render_chld;
303 $self->SUPER::update;
209} 304}
210 305
211sub render_chld { 306sub render_chld {
212 my ($self) = @_; 307 my ($self) = @_;
213 my $chld = $self->get;
214 my ($w, $h) = $self->size_request;
215 308
216 $self->{texture} = 309 $self->{texture} =
217 Crossfire::Client::Texture->new_from_opengl ( 310 Crossfire::Client::Texture->new_from_opengl (
218 $w, $h, sub { $chld->draw } 311 $self->{w}, $self->{h}, sub { $self->child->draw }
219 ); 312 );
220 $self->{texture}->upload;
221} 313}
222 314
223sub size_request { 315sub size_allocate {
224 my ($self) = @_; 316 my ($self, $w, $h) = @_;
225 my $chld = $self->get 317
226 or return (0, 0); 318 $self->{w} = $w;
227 $chld->size_request 319 $self->{h} = $h;
320
321 $self->child->size_allocate ($w, $h);
322
323 $self->render_chld;
228} 324}
229 325
230sub _draw { 326sub _draw {
231 my ($self) = @_; 327 my ($self) = @_;
232 328
233 my ($w, $h) = $self->size_request;#TODO# use width/height of texture 329 my ($w, $h) = ($self->w, $self->h);
234 330
235 my $tex = $self->{texture} 331 my $tex = $self->{texture}
236 or return; 332 or return;
237 333
238 glEnable GL_BLEND; 334 glEnable GL_BLEND;
239 glEnable GL_TEXTURE_2D; 335 glEnable GL_TEXTURE_2D;
240 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 336 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
241 glBindTexture GL_TEXTURE_2D, $tex->{name}; 337 glBindTexture GL_TEXTURE_2D, $tex->{name};
242
243 glColor 1, 0, 1;
244 338
245 glBegin GL_QUADS; 339 glBegin GL_QUADS;
246 glTexCoord 0, 0; glVertex 0, 0; 340 glTexCoord 0, 0; glVertex 0, 0;
247 glTexCoord 0, 1; glVertex 0, $h; 341 glTexCoord 0, 1; glVertex 0, $h;
248 glTexCoord 1, 1; glVertex $w, $h; 342 glTexCoord 1, 1; glVertex $w, $h;
251 345
252 glDisable GL_BLEND; 346 glDisable GL_BLEND;
253 glDisable GL_TEXTURE_2D; 347 glDisable GL_TEXTURE_2D;
254} 348}
255 349
350#############################################################################
351
256package Crossfire::Client::Widget::Frame; 352package Crossfire::Client::Widget::Frame;
257 353
258our @ISA = Crossfire::Client::Widget::Container::; 354our @ISA = Crossfire::Client::Widget::Bin::;
259 355
260use SDL::OpenGL; 356use SDL::OpenGL;
261 357
262sub size_request { 358sub size_request {
263 my ($self) = @_; 359 my ($self) = @_;
264 my $chld = $self->get 360 my $chld = $self->child
265 or return (0, 0); 361 or return (0, 0);
266 362
267 $chld->move (2, 2); 363 $chld->move (2, 2);
268 364
269 map { $_ + 4 } $chld->size_request; 365 map { $_ + 4 } $chld->size_request;
270} 366}
271 367
368sub size_allocate {
369 my ($self, $w, $h) = @_;
370
371 $self->{w} = $w;
372 $self->{h} = $h;
373
374 $self->child->size_allocate ($w - 4, $h - 4);
375 $self->child->move (2, 2);
376}
377
272sub _draw { 378sub _draw {
273 my ($self) = @_; 379 my ($self) = @_;
274 380
275 my $chld = $self->get; 381 my $chld = $self->child;
276 382
277 my ($w, $h) = $chld->size_request; 383 my ($w, $h) = $chld->size_request;
278 384
279 glBegin GL_QUADS; 385 glBegin GL_QUADS;
280 glColor 0, 0, 0; 386 glColor 0, 0, 0;
285 glEnd; 391 glEnd;
286 392
287 $chld->draw; 393 $chld->draw;
288} 394}
289 395
396#############################################################################
397
290package Crossfire::Client::Widget::FancyFrame; 398package Crossfire::Client::Widget::FancyFrame;
291 399
292our @ISA = Crossfire::Client::Widget::Frame::; 400our @ISA = Crossfire::Client::Widget::Bin::;
293 401
294use SDL::OpenGL; 402use SDL::OpenGL;
295 403
296sub new { 404my @tex =
297 my ($self, $theme) = @_;
298 $self = $self->SUPER::new;
299
300 $self->{txts} = [
301 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 405 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
302 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 406 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
303 ];
304 $self
305
306}
307 407
308sub size_request { 408sub size_request {
309 my ($self) = @_; 409 my ($self) = @_;
410
310 my ($w, $h) = $self->get->size_request; 411 my ($w, $h) = $self->SUPER::size_request;
311 412
312 $h += $self->{txts}->[1]->{height}; 413 $h += $tex[1]->{height};
313 $h += $self->{txts}->[4]->{height}; 414 $h += $tex[4]->{height};
314 $w += $self->{txts}->[2]->{width}; 415 $w += $tex[2]->{width};
315 $w += $self->{txts}->[3]->{width}; 416 $w += $tex[3]->{width};
316
317 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
318 417
319 ($w, $h) 418 ($w, $h)
320} 419}
321 420
421sub size_allocate {
422 my ($self, $w, $h) = @_;
423
424 $self->SUPER::size_allocate ($w, $h);
425
426 $h -= $tex[1]->{height};
427 $h -= $tex[4]->{height};
428 $w -= $tex[2]->{width};
429 $w -= $tex[3]->{width};
430
431 $h = $h < 0 ? 0 : $h;
432 $w = $w < 0 ? 0 : $w;
433
434 $self->child->size_allocate ($w, $h);
435 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
436}
437
322sub _draw { 438sub _draw {
323 my ($self) = @_; 439 my ($self) = @_;
324 440
325 my ($w, $h) = $self->size_request; 441 my ($w, $h) = ($self->{w}, $self->{h});
326 my ($cw, $ch) = $self->get->size_request; 442 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
327 443
328 glEnable GL_BLEND; 444 glEnable GL_BLEND;
329 glEnable GL_TEXTURE_2D; 445 glEnable GL_TEXTURE_2D;
330 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 446 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
331 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 447 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
332 448
333
334 my $top = $self->{txts}->[1]; 449 my $top = $tex[1];
335 glBindTexture GL_TEXTURE_2D, $top->{name}; 450 glBindTexture GL_TEXTURE_2D, $top->{name};
336 451
337 glColor 1, 1, 1, 0.8;
338
339 glBegin GL_QUADS; 452 glBegin GL_QUADS;
340 glTexCoord 0, 0; glVertex 0 , 0; 453 glTexCoord 0, 0; glVertex 0 , 0;
341 glTexCoord 0, 1; glVertex 0 , $top->{height}; 454 glTexCoord 0, 1; glVertex 0 , $top->{height};
342 glTexCoord 1, 1; glVertex $w , $top->{height}; 455 glTexCoord 1, 1; glVertex $w , $top->{height};
343 glTexCoord 1, 0; glVertex $w , 0; 456 glTexCoord 1, 0; glVertex $w , 0;
344 glEnd; 457 glEnd;
345 458
346 my $left = $self->{txts}->[3]; 459 my $left = $tex[3];
347 glBindTexture GL_TEXTURE_2D, $left->{name}; 460 glBindTexture GL_TEXTURE_2D, $left->{name};
348 461
349 glColor 1, 1, 1, 0.8;
350
351 glBegin GL_QUADS; 462 glBegin GL_QUADS;
352 glTexCoord 0, 0; glVertex 0 , $top->{height}; 463 glTexCoord 0, 0; glVertex 0 , $top->{height};
353 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 464 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
354 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 465 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
355 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 466 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
356 glEnd; 467 glEnd;
357 468
358 my $right = $self->{txts}->[2]; 469 my $right = $tex[2];
359 glBindTexture GL_TEXTURE_2D, $right->{name}; 470 glBindTexture GL_TEXTURE_2D, $right->{name};
360 471
361 glColor 1, 1, 1, 0.8;
362
363 glBegin GL_QUADS; 472 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 473 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
365 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 474 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
366 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 475 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
367 glTexCoord 1, 0; glVertex $w , $top->{height}; 476 glTexCoord 1, 0; glVertex $w , $top->{height};
368 glEnd; 477 glEnd;
369 478
370 my $bottom = $self->{txts}->[4]; 479 my $bottom = $tex[4];
371 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 480 glBindTexture GL_TEXTURE_2D, $bottom->{name};
372 481
373 glColor 1, 1, 1, 0.8;
374
375 glBegin GL_QUADS; 482 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 483 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
377 glTexCoord 0, 1; glVertex 0 , $h; 484 glTexCoord 0, 1; glVertex 0 , $h;
378 glTexCoord 1, 1; glVertex $w , $h; 485 glTexCoord 1, 1; glVertex $w , $h;
379 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 486 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
380 glEnd; 487 glEnd;
381 488
382 my $bg = $self->{txts}->[0]; 489 my $bg = $tex[0];
383 glBindTexture GL_TEXTURE_2D, $bg->{name}; 490 glBindTexture GL_TEXTURE_2D, $bg->{name};
491 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
384 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 492 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
385 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 493 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
386 494
387 glColor 1, 1, 1, 0.8;
388
389 my $rep_x = $cw / $bg->{width}; 495 my $rep_x = $cw / $bg->{width};
390 my $rep_y = $ch / $bg->{height}; 496 my $rep_y = $ch / $bg->{height};
391 497
392 glBegin GL_QUADS; 498 glBegin GL_QUADS;
393 glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; 499 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
394 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; 500 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
395 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; 501 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
396 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; 502 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
397 glEnd; 503 glEnd;
398
399 $self->get->draw;
400 504
401 glDisable GL_BLEND; 505 glDisable GL_BLEND;
402 glDisable GL_TEXTURE_2D; 506 glDisable GL_TEXTURE_2D;
507
508 $self->child->draw;
509
403} 510}
511
512#############################################################################
404 513
405package Crossfire::Client::Widget::Table; 514package Crossfire::Client::Widget::Table;
406 515
407our @ISA = Crossfire::Client::Widget::Container::; 516our @ISA = Crossfire::Client::Widget::Bin::;
408 517
409use SDL::OpenGL; 518use SDL::OpenGL;
410 519
411sub add { 520sub add {
412 my ($self, $x, $y, $chld) = @_; 521 my ($self, $x, $y, $chld) = @_;
413 my $old_chld = $self->{childs}[$y][$x]; 522 my $old_chld = $self->{children}[$y][$x];
414 523
415 $self->{childs}[$y][$x] = $chld; 524 $self->{children}[$y][$x] = $chld;
416 $chld->set_parent ($self); 525 $chld->set_parent ($self);
417 $self->update; 526 $self->update;
418} 527}
419 528
420sub max_row_height { 529sub max_row_height {
421 my ($self, $row) = @_; 530 my ($self, $row) = @_;
422 531
423 my $hs = 0; 532 my $hs = 0;
424 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 533 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
425 my $c = $self->{childs}->[$row]->[$xi]; 534 my $c = $self->{children}->[$row]->[$xi];
426 if ($c) { 535 if ($c) {
427 my ($w, $h) = $c->size_request; 536 my ($w, $h) = $c->size_request;
428 if ($hs < $h) { $hs = $h } 537 if ($hs < $h) { $hs = $h }
429 } 538 }
430 } 539 }
433 542
434sub max_col_width { 543sub max_col_width {
435 my ($self, $col) = @_; 544 my ($self, $col) = @_;
436 545
437 my $ws = 0; 546 my $ws = 0;
438 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 547 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
439 my $c = ($self->{childs}->[$yi] || [])->[$col]; 548 my $c = ($self->{children}->[$yi] || [])->[$col];
440 if ($c) { 549 if ($c) {
441 my ($w, $h) = $c->size_request; 550 my ($w, $h) = $c->size_request;
442 if ($ws < $w) { $ws = $w } 551 if ($ws < $w) { $ws = $w }
443 } 552 }
444 } 553 }
448sub size_request { 557sub size_request {
449 my ($self) = @_; 558 my ($self) = @_;
450 559
451 my ($hs, $ws) = (0, 0); 560 my ($hs, $ws) = (0, 0);
452 561
453 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 562 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
454 $hs += $self->max_row_height ($yi); 563 $hs += $self->max_row_height ($yi);
455 } 564 }
456 565
457 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 566 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
458 my $wm = 0; 567 my $wm = 0;
459 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 568 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
460 $wm += $self->max_col_width ($xi) 569 $wm += $self->max_col_width ($xi)
461 } 570 }
462 if ($ws < $wm) { $ws = $wm } 571 if ($ws < $wm) { $ws = $wm }
463 } 572 }
464 573
467 576
468sub _draw { 577sub _draw {
469 my ($self) = @_; 578 my ($self) = @_;
470 579
471 my $y = 0; 580 my $y = 0;
472 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 581 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
473 my $x = 0; 582 my $x = 0;
474 583
475 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 584 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
476 585
477 my $c = $self->{childs}->[$yi]->[$xi]; 586 my $c = $self->{children}->[$yi]->[$xi];
478 if ($c) { 587 if ($c) {
479 $c->move ($x, $y, 0); #TODO: Move to size_request 588 $c->move ($x, $y, 0); #TODO: Move to size_request
480 $c->draw if $c; 589 $c->draw if $c;
481 } 590 }
482 591
485 594
486 $y += $self->max_row_height ($yi); 595 $y += $self->max_row_height ($yi);
487 } 596 }
488} 597}
489 598
599#############################################################################
600
490package Crossfire::Client::Widget::VBox; 601package Crossfire::Client::Widget::VBox;
491 602
492our @ISA = Crossfire::Client::Widget::Container::; 603our @ISA = Crossfire::Client::Widget::Container::;
493 604
494use SDL::OpenGL; 605use SDL::OpenGL;
495 606
496sub add {
497 my ($self, $chld) = @_;
498 push @{$self->{childs}}, $chld;
499 $chld->set_parent ($self);
500 $self->update;
501}
502
503sub size_request { 607sub size_request {
504 my ($self) = @_; 608 my ($self) = @_;
505 609
506 my ($hs, $ws) = (0, 0); 610 my @alloc = map [$_->size_request], @{$self->{children}};
611
612 (
613 (List::Util::max map $_->[0], @alloc),
614 (List::Util::sum map $_->[1], @alloc),
615 )
616}
617
618sub size_allocate {
619 my ($self, $w, $h) = @_;
620
621 $self->w ($w);
622 $self->h ($h);
623
624 my $exp;
625 my @oth;
626 # find expand widget
507 for (@{$self->{childs} || []}) { 627 for (@{$self->{children}}) {
628 if ($_->{expand}) {
629 $exp = $_;
630 last;
631 }
632 push @oth, $_;
633 }
634
635 my ($ow, $oh);
636
637 # get sizes of other widgets
638 for (@oth) {
508 my ($w, $h) = $_->size_request; 639 my ($w, $h) = $_->size_request;
509 $hs += $h; 640 $oh += $h;
510 if ($ws < $w) { $ws = $w } 641 if ($ow < $w) { $ow = $w }
511 } 642 }
512 643
513 return ($ws, $hs); 644 my $y = 0;
514}
515
516sub _draw {
517 my ($self) = @_;
518
519 my ($x, $y);
520 for (@{$self->{childs} || []}) { 645 for (@{$self->{children}}) {
521 $_->move (0, $y, 0); #TODO: move to size_request 646 $_->move (0, $y);
522 $_->draw; 647
648 if ($_ == $exp) {
649 $_->size_allocate ($w, $h - $oh);
650 $y += $h - $oh;
651 } else {
523 my ($w, $h) = $_->size_request; 652 my ($cw, $h) = $_->size_request;
653 $_->size_allocate ($w, $h);
524 $y += $h; 654 $y += $h;
655 }
525 } 656 }
526} 657}
658
659#############################################################################
527 660
528package Crossfire::Client::Widget::Label; 661package Crossfire::Client::Widget::Label;
529 662
530our @ISA = Crossfire::Client::Widget::; 663our @ISA = Crossfire::Client::Widget::;
531 664
532use SDL::OpenGL; 665use SDL::OpenGL;
533 666
534sub new { 667sub new {
535 my ($class, $x, $y, $z, $height, $text) = @_; 668 my ($class, $x, $y, $z, $height, $text) = @_;
536 669
670 $height ||= $::FONTSIZE;
671
537 # TODO: color, and make height, xyz etc. optional 672 # TODO: color, and make height, xyz etc. optional
538 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 673 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height);
539 674
540 $self->set_text ($text); 675 $self->set_text ($text);
541 676
558} 693}
559 694
560sub size_request { 695sub size_request {
561 my ($self) = @_; 696 my ($self) = @_;
562 697
563 ( 698 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
699 (
564 $self->{texture}{width}, 700 $self->{texture}{width},
565 $self->{texture}{height}, 701 $self->{texture}{height},
702 )
703 } else {
704 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height};
705
706 ($w, $h)
566 ) 707 }
708
567} 709}
568 710
569sub _draw { 711sub _draw {
570 my ($self) = @_; 712 my ($self) = @_;
571 713
575 glEnable GL_TEXTURE_2D; 717 glEnable GL_TEXTURE_2D;
576 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 718 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
577 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 719 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
578 glBindTexture GL_TEXTURE_2D, $tex->{name}; 720 glBindTexture GL_TEXTURE_2D, $tex->{name};
579 721
580 glColor 1, 1, 1, 0.6; # TODO color 722 glColor 1, 0, 0, 1; # TODO color
581 723
582 glBegin GL_QUADS; 724 glBegin GL_QUADS;
583 glTexCoord 0, 0; glVertex 0 , 0; 725 glTexCoord 0, 0; glVertex 0 , 0;
584 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 726 glTexCoord 0, 1; glVertex 0 , $tex->{height};
585 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 727 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height};
588 730
589 glDisable GL_BLEND; 731 glDisable GL_BLEND;
590 glDisable GL_TEXTURE_2D; 732 glDisable GL_TEXTURE_2D;
591} 733}
592 734
735#############################################################################
736
593package Crossfire::Client::Widget::TextEntry; 737package Crossfire::Client::Widget::Entry;
594 738
595our @ISA = Crossfire::Client::Widget::Label::; 739our @ISA = Crossfire::Client::Widget::Label::;
596 740
597use SDL; 741use SDL;
598use SDL::OpenGL; 742use SDL::OpenGL;
601 my ($self, $ev) = @_; 745 my ($self, $ev) = @_;
602 746
603 my $mod = $ev->key_mod; 747 my $mod = $ev->key_mod;
604 my $sym = $ev->key_sym; 748 my $sym = $ev->key_sym;
605 749
606 $ev->set_unicode (1);
607 my $uni = $ev->key_unicode; 750 my $uni = $ev->key_unicode;
608 751
609 my $text = $self->get_text; 752 my $text = $self->get_text;
610 753
611 if ($sym == SDLK_BACKSPACE) { 754 if ($sym == SDLK_BACKSPACE) {
612 substr $text, -1, 1, ''; 755 substr $text, -1, 1, '';
613
614 } elsif ($uni) { 756 } elsif ($uni) {
615 $text .= chr $uni; 757 $text .= chr $uni;
758 print "$uni <$text>\n";#d#
616 } 759 }
760
617 $self->set_text ($text); 761 $self->set_text ($text);
618} 762}
619 763
764sub button_down {
765 my ($self, $ev) = @_;
620 766
621# XXX: TextView isn't neccessary with pango multiline text rendering 767 $self->focus_in;
622package Crossfire::Client::Widget::TextView;
623
624use strict;
625
626our @ISA = qw/Crossfire::Client::Widget/;
627
628use SDL::OpenGL;
629use SDL::OpenGL::Constants;
630
631sub new {
632 my ($class, $text, $h) = @_;
633 my $self = $class->SUPER::new ();
634
635 $self->{txt_height} = $h;
636 @{$self->{lines}} = split /\r?\n/, $text;
637
638 for (split /\r?\n/, $text) {
639 $self->add_line ($_);
640 }
641 $self
642} 768}
643 769
644#sub render_lines { 770sub _draw {
645# my ($self) = @_; 771 my ($self) = @_;
646# 772
647# $self->{txt_lines} = []; 773 if ($FOCUS == $self) {
648# 774 glColor 1, 1, 1;
649# for (@{$self->{lines}}) { 775 } else {
650# push @{$self->{txt_lines}}, 776 glColor 0.7, 0.7, 0.7;
651# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_;
652# } 777 }
653#}
654
655sub add_line {
656 my ($self, $line) = @_;
657 push @{$self->{lines}}, $line;
658
659 push @{$self->{txt_lines}},
660 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
661}
662
663sub size_request {
664 my ($self) = @_;
665
666 my $w = 0;
667 my $h = 0;
668
669 for (@{$self->{txt_lines}}) {
670 if ($w < $_->{width}) { $w = $_->{width} }
671 $h += $_->{height};
672 }
673
674 return ($w, $h);
675}
676
677sub draw_line {
678 my ($self, $tex, $y) = @_;
679
680 glBindTexture GL_TEXTURE_2D, $tex->{name};
681
682 glColor 1, 0, 1;
683 778
684 glBegin GL_QUADS; 779 glBegin GL_QUADS;
685 glTexCoord 0, 0; glVertex 0 , $y; 780 glVertex 0 , 0;
686 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height}; 781 glVertex 0 , $self->{h} - 1;
687 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height}; 782 glVertex $self->{w} - 1, $self->{h} - 1;
688 glTexCoord 1, 0; glVertex $tex->{width}, $y; 783 glVertex $self->{w} - 1, 0;
689 glEnd; 784 glEnd;
690}
691 785
692sub _draw { 786 $self->SUPER::_draw;
693 my ($self) = @_;
694
695 glEnable GL_BLEND;
696 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL;
698
699 my $l = 0;
700 for (@{$self->{txt_lines}}) {
701 $self->draw_line ($_, $l);
702 $l += $_->{height};
703 }
704
705 glDisable GL_BLEND;
706 glDisable GL_TEXTURE_2D;
707} 787}
788
789#############################################################################
708 790
709package Crossfire::Client::Widget::MapWidget; 791package Crossfire::Client::Widget::MapWidget;
710 792
711use strict; 793use strict;
712 794
721sub key_down { 803sub key_down {
722 print "MAPKEYDOWN\n"; 804 print "MAPKEYDOWN\n";
723} 805}
724 806
725sub key_up { 807sub key_up {
808}
809
810sub size_request {
811 (
812 1 + int $::WIDTH / 32,
813 1 + int $::HEIGHT / 32,
814 )
726} 815}
727 816
728sub _draw { 817sub _draw {
729 my ($self) = @_; 818 my ($self) = @_;
730 819
750 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 839 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
751 } 840 }
752 841
753 glEnable GL_TEXTURE_2D; 842 glEnable GL_TEXTURE_2D;
754 glEnable GL_BLEND; 843 glEnable GL_BLEND;
755 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 844 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
845
846 my $sw4 = ($sw + 3) & ~3;
847 my $lighting = "\x00" x ($sw4 * $sh);
756 848
757 for my $x (0 .. $sw - 1) { 849 for my $x (0 .. $sw - 1) {
758 for my $y (0 .. $sh - 1) { 850 for my $y (0 .. $sh - 1) {
759 851
760 my $cell = $map->[$x + $xofs][$y + $yofs] 852 my $cell = $map->[$x + $xofs][$y + $yofs]
761 or next; 853 or next;
762 854
763 my $darkness = $cell->[0] * (1 / 255); 855 my $darkness = $cell->[0] * (1 / 255);
764 if ($darkness < 0) { 856 if ($darkness < 0) {
765 glColor 0.3, 0.3, 0.3; 857 $darkness = $cell->[1] ? 0.1 : 0;
766 } else {
767 glColor $darkness, $darkness, $darkness;
768 } 858 }
859 substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255;
769 860
770 for my $num (grep $_, @$cell[1,2,3]) { 861 for my $num (grep $_, @$cell[1,2,3]) {
771 my $tex = $::CONN->{face}[$num]{texture} || next; 862 my $tex = $::CONN->{face}[$num]{texture} || next;
772 863
773 glBindTexture GL_TEXTURE_2D, $tex->{name}; 864 glBindTexture GL_TEXTURE_2D, $tex->{name};
785 glTexCoord 1, 0; glVertex $px + $w, $py; 876 glTexCoord 1, 0; glVertex $px + $w, $py;
786 glEnd; 877 glEnd;
787 } 878 }
788 } 879 }
789 } 880 }
881
882# if (1) { # higher quality darkness
883# $lighting =~ s/(.)/$1$1$1/gs;
884# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
885#
886# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
887#
888# $lighting = $pb->get_pixels;
889# $lighting =~ s/(.)../$1/gs;
890# }
891
892 $lighting = new Crossfire::Client::Texture
893 width => $sw4,
894 height => $sh,
895 data => $lighting,
896 internalformat => GL_ALPHA,
897 format => GL_ALPHA;
898
899 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
900 glColor 0.45, 0.45, 0.45, 1;
901 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
902 glBindTexture GL_TEXTURE_2D, $lighting->{name};
903 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
904 glBegin GL_QUADS;
905 glTexCoord 0, 0; glVertex 0 , 0;
906 glTexCoord 0, 1; glVertex 0 , $sh * 32;
907 glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32;
908 glTexCoord 1, 0; glVertex $sw4 * 32, 0;
909 glEnd;
790 910
791 glDisable GL_TEXTURE_2D; 911 glDisable GL_TEXTURE_2D;
792 glDisable GL_BLEND; 912 glDisable GL_BLEND;
793} 913}
794 914
841 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 961 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
842 $::CONN->send ("command run_stop"); 962 $::CONN->send ("command run_stop");
843 } 963 }
844} 964}
845 965
8461; 966#############################################################################
847 967
968package Crossfire::Client::Widget::Animator;
969
970use SDL::OpenGL;
971
972our @ISA = Crossfire::Client::Widget::Bin::;
973
974sub moveto {
975 my ($self, $x, $y) = @_;
976
977 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
978 $self->{speed} = 2;
979 $self->{time} = 1;
980
981 ::animation_start $self;
982}
983
984sub animate {
985 my ($self, $interval) = @_;
986
987 $self->{time} -= $interval * $self->{speed};
988 if ($self->{time} <= 0) {
989 $self->{time} = 0;
990 ::animation_stop $self;
991 }
992
993 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
994
995 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
996 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
997}
998
999sub _draw {
1000 my ($self) = @_;
1001
1002 glPushMatrix;
1003 glRotate $self->{time} * 1000, 0, 1, 0;
1004 $self->{children}[0]->draw;
1005 glPopMatrix;
1006}
1007
1008#############################################################################
1009
1010package Crossfire::Client::Widget::Toplevel;
1011
1012our @ISA = Crossfire::Client::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 update {
1028 my ($self) = @_;
1029
1030 $self->size_allocate ($self->size_request);
1031 ::refresh ();
1032}
1033
1034sub add {
1035 my ($self, $widget) = @_;
1036
1037 $self->SUPER::add ($widget);
1038
1039 $widget->size_allocate ($widget->size_request);
1040}
1041
1042sub draw {
1043 my ($self) = @_;
1044
1045 $self->_draw;
1046}
1047
1048#############################################################################
1049
1050package Crossfire::Client::Widget;
1051
1052$TOPLEVEL = new Crossfire::Client::Widget::Toplevel;
1053
10541
1055

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines