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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines