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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines