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.74 by root, Tue Apr 11 19:31:18 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6use List::Util ();
7
8use CFClient;
9
10our ($FOCUS, $HOVER, $GRAB); # various widgets
11
12our $TOPLEVEL;
13our $BUTTON_STATE;
14
15# class methods for events
16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS;
18}
19
20sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS;
22}
23
24sub feed_sdl_button_down_event {
25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
27
28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y);
30
31 $GRAB = $widget;
32 $GRAB->update if $GRAB;
33 }
34
35 $BUTTON_STATE |= 1 << ($ev->button - 1);
36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
38}
39
40sub feed_sdl_button_up_event {
41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
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, $HOVER->translate ($x, $y)) if $HOVER;
71}
72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
6 78
7use SDL::OpenGL; 79use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9
10our $FOCUS; # the widget with current focus
11#our @ACTIVE_WIDGETS;
12
13# class methods for events
14sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
15sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS }
16sub feed_sdl_button_down_event { }
17sub feed_sdl_button_up_event { }
18 80
19sub new { 81sub new {
20 my $class = shift; 82 my $class = shift;
21 83
22 bless { @_ }, $class 84 bless {
85 x => 0,
86 y => 0,
87 z => 0,
88 @_
89 }, $class
23} 90}
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 91
37sub move { 92sub move {
38 my ($self, $x, $y, $z) = @_; 93 my ($self, $x, $y, $z) = @_;
39 $self->{x} = $x; 94 $self->{x} = $x;
40 $self->{y} = $y; 95 $self->{y} = $y;
44sub needs_redraw { 99sub needs_redraw {
45 0 100 0
46} 101}
47 102
48sub size_request { 103sub size_request {
104 require Carp;
49 die "size_request is abtract"; 105 Carp::confess "size_request is abtract";
106}
107
108sub size_allocate {
109 my ($self, $w, $h) = @_;
110
111 $self->{w} = $w;
112 $self->{h} = $h;
113}
114
115# translate global koordinates to local coordinate system
116sub translate {
117 my ($self, $x, $y) = @_;
118
119 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
50} 120}
51 121
52sub focus_in { 122sub focus_in {
53 my ($widget) = @_; 123 my ($self) = @_;
54 $FOCUS = $widget; 124
125 return if $FOCUS == $self;
126
127 my $focus = $FOCUS; $FOCUS = $self;
128 $focus->update if $focus;
129 $FOCUS->update;
55} 130}
56 131
57sub focus_out { 132sub focus_out {
58 my ($widget) = @_; 133 my ($self) = @_;
59}
60 134
135 return unless $FOCUS == $self;
136
137 my $focus = $FOCUS; undef $FOCUS;
138 $focus->update if $focus; #?
139}
140
141sub mouse_motion { }
142sub button_up { }
61sub key_down { 143sub key_down { }
62 my ($widget, $sdlev) = @_;
63}
64
65sub key_up { 144sub key_up { }
66 my ($widget, $sdlev) = @_;
67}
68 145
69sub button_down { 146sub button_down {
70 my ($widget, $sdlev) = @_; 147 my ($self, $ev, $x, $y) = @_;
71}
72 148
73sub button_up { 149 $self->focus_in;
74 my ($widget, $sdlev) = @_;
75} 150}
76 151
77sub w { $_[0]->{w} } 152sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
78sub h { $_[0]->{h} } 153sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
79sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } 154sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
80sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } 155sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
81sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } 156sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
82 157
83sub draw { 158sub draw {
84 my ($self) = @_; 159 my ($self) = @_;
160
161 return unless $self->{h} && $self->{w};
85 162
86 glPushMatrix; 163 glPushMatrix;
87 glTranslate $self->{x}, $self->{y}, 0; 164 glTranslate $self->{x}, $self->{y}, 0;
88 $self->_draw; 165 $self->_draw;
89 glPopMatrix; 166 glPopMatrix;
167
168 if ($self == $HOVER) {
169 my ($x, $y) = @$self{qw(x y)};
170
171 glColor 1, 1, 1, 0.1;
172 glEnable GL_BLEND;
173 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
174 glBegin GL_QUADS;
175 glVertex $x , $y;
176 glVertex $x + $self->{w}, $y;
177 glVertex $x + $self->{w}, $y + $self->{h};
178 glVertex $x , $y + $self->{h};
179 glEnd;
180 glDisable GL_BLEND;
181 }
90} 182}
91 183
92sub _draw { 184sub _draw {
93 my ($widget) = @_; 185 my ($self) = @_;
186
187 warn "no draw defined for $self\n";
94} 188}
95 189
96sub bbox { 190sub bbox {
97 my ($self) = @_; 191 my ($self) = @_;
98 my ($w, $h) = $self->size_request; 192 my ($w, $h) = $self->size_request;
102 $self->{x} = $w, 196 $self->{x} = $w,
103 $self->{y} = $h 197 $self->{y} = $h
104 ) 198 )
105} 199}
106 200
201sub find_widget {
202 my ($self, $x, $y) = @_;
203
204 return $self
205 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
206 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
207
208 ()
209}
210
107sub del_parent { $_[0]->{parent} = undef } 211sub del_parent { $_[0]->{parent} = undef }
108 212
109sub set_parent { 213sub set_parent {
110 my ($self, $par) = @_; 214 my ($self, $par) = @_;
111 215
122 226
123 $self->{parent}->update 227 $self->{parent}->update
124 if $self->{parent}; 228 if $self->{parent};
125} 229}
126 230
231sub connect {
232 my ($self, $signal, $cb) = @_;
233
234 push @{ $self->{cb}{$signal} }, $cb;
235}
236
237sub emit {
238 my ($self, $signal, @args) = @_;
239
240 $_->($self, @args)
241 for @{$self->{cb}{$signal} || []};
242}
243
127sub DESTROY { 244sub DESTROY {
128 my ($self) = @_; 245 my ($self) = @_;
129 246
130 #$self->deactivate; 247 #$self->deactivate;
131} 248}
132 249
133package Crossfire::Client::Widget::Container; 250#############################################################################
134 251
135our @ISA = Crossfire::Client::Widget::; 252package CFClient::UI::DrawBG;
136 253
254our @ISA = CFClient::UI::Base::;
255
256use strict;
137use SDL::OpenGL; 257use SDL::OpenGL;
138 258
139sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update } 259sub new {
140sub get { $_[0]->{child} } 260 my $class = shift;
261
262 # range [value, low, high, page]
263
264 $class->SUPER::new (
265 bg => [0, 0, 0, 0.4],
266 active_bg => [1, 1, 1],
267 @_
268 )
269}
270
271sub _draw {
272 my ($self) = @_;
273
274 my ($w, $h) = @$self{qw(w h)};
275
276 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
277 glBegin GL_QUADS;
278 glVertex 0 , 0;
279 glVertex 0 , $h;
280 glVertex $w, $h;
281 glVertex $w, 0;
282 glEnd;
283}
284
285#############################################################################
286
287package CFClient::UI::Empty;
288
289our @ISA = CFClient::UI::Base::;
290
291sub size_request {
292 (0, 0)
293}
294
295sub draw { }
296
297#############################################################################
298
299package CFClient::UI::Container;
300
301our @ISA = CFClient::UI::Base::;
302
303sub new {
304 my ($class, %arg) = @_;
305
306 my $children = delete $arg{children} || [];
307
308 my $self = $class->SUPER::new (children => [], %arg);
309 $self->add ($_) for @$children;
310
311 $self
312}
313
314sub add {
315 my ($self, $chld, $expand) = @_;
316
317 $chld->{expand} = $expand;
318 $chld->set_parent ($self);
319
320 $self->{children} = [
321 sort { $a->{z} <=> $b->{z} }
322 @{$self->{children}}, $chld
323 ];
324
325 $self->size_allocate ($self->{w}, $self->{h})
326 if $self->{w}; #TODO: check for "realised state"
327}
328
141sub remove { 329sub remove {
330 my ($self, $widget) = @_;
331
332 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
333
334 $self->size_allocate ($self->{w}, $self->{h});
335}
336
337sub find_widget {
338 my ($self, $x, $y) = @_;
339
340 $x -= $self->{x};
341 $y -= $self->{y};
342
343 my $res;
344
345 for (reverse @{ $self->{children} }) {
346 $res = $_->find_widget ($x, $y)
347 and return $res;
348 }
349
350 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
351}
352
353sub _draw {
354 my ($self) = @_;
355
356 $_->draw for @{$self->{children}};
357}
358
359#############################################################################
360
361package CFClient::UI::Bin;
362
363our @ISA = CFClient::UI::Container::;
364
365sub new {
366 my ($class, %arg) = @_;
367
368 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
369
370 $class->SUPER::new (children => [$child], %arg)
371}
372
373sub add {
374 my ($self, $widget) = @_;
375
376 $self->{children} = [];
377
378 $self->SUPER::add ($widget);
379}
380
381sub remove {
382 my ($self, $widget) = @_;
383
384 $self->SUPER::remove ($widget);
385
386 $self->{children} = [new CFClient::UI::Empty]
387 unless @{$self->{children}};
388}
389
390sub child { $_[0]->{children}[0] }
391
392sub size_request {
393 $_[0]{children}[0]->size_request
394}
395
396sub size_allocate {
142 my ($self, $chld) = @_; 397 my ($self, $w, $h) = @_;
143 delete $self->{child}
144 if $self->{child} == $chld;
145}
146 398
147sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } 399 return unless $self->{w} != $w || $self->{h} != $h;
148 400
149sub _draw { die "Containers can't be drawn!" } 401 $self->SUPER::size_allocate ($w, $h);
402 $self->{children}[0]->size_allocate ($w, $h);
403}
150 404
151package Crossfire::Client::Widget::Toplevel; 405#############################################################################
152 406
153our @ISA = Crossfire::Client::Widget::; 407package CFClient::UI::Window;
408
409our @ISA = CFClient::UI::Bin::;
154 410
155use SDL::OpenGL; 411use SDL::OpenGL;
156 412
157sub add { 413sub new {
158 my ($self, $chld) = @_; 414 my ($class, %arg) = @_;
159 415
160 push @{$self->{childs}}, $chld; 416 my $self = $class->SUPER::new (%arg);
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} 417}
175 418
176sub update { 419sub update {
177 my ($self) = @_; 420 my ($self) = @_;
178 ::refresh (); 421
422 # we want to do this delayed...
423 $self->render_chld;
424 $self->SUPER::update;
425}
426
427sub render_chld {
428 my ($self) = @_;
429
430 $self->{texture} =
431 CFClient::Texture->new_from_opengl (
432 $self->{w}, $self->{h}, sub { $self->child->draw }
433 );
434}
435
436sub size_allocate {
437 my ($self, $w, $h) = @_;
438
439 return unless $self->{w} != $w || $self->{h} != $h;
440
441 $self->{w} = $w;
442 $self->{h} = $h;
443
444 $self->child->size_allocate ($w, $h);
445
446 $self->render_chld;
179} 447}
180 448
181sub _draw { 449sub _draw {
182 my ($self) = @_; 450 my ($self) = @_;
183 451
184 $_->draw for @{$self->{childs}}; 452 my ($w, $h) = ($self->w, $self->h);
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;
209}
210
211sub render_chld {
212 my ($self) = @_;
213 my $chld = $self->get;
214 my ($w, $h) = $self->size_request;
215
216 $self->{texture} =
217 Crossfire::Client::Texture->new_from_opengl (
218 $w, $h, sub { $chld->draw }
219 );
220 $self->{texture}->upload;
221}
222
223sub size_request {
224 my ($self) = @_;
225 my $chld = $self->get
226 or return (0, 0);
227 $chld->size_request
228}
229
230sub _draw {
231 my ($self) = @_;
232
233 my ($w, $h) = $self->size_request;#TODO# use width/height of texture
234 453
235 my $tex = $self->{texture} 454 my $tex = $self->{texture}
236 or return; 455 or return;
237 456
238 glEnable GL_BLEND; 457 glEnable GL_BLEND;
458 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
239 glEnable GL_TEXTURE_2D; 459 glEnable GL_TEXTURE_2D;
240 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 460 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
241 glBindTexture GL_TEXTURE_2D, $tex->{name};
242 461
243 glColor 1, 0, 1; 462 $tex->draw_quad (0, 0, $w, $h);
244
245 glBegin GL_QUADS;
246 glTexCoord 0, 0; glVertex 0, 0;
247 glTexCoord 0, 1; glVertex 0, $h;
248 glTexCoord 1, 1; glVertex $w, $h;
249 glTexCoord 1, 0; glVertex $w, 0;
250 glEnd;
251 463
252 glDisable GL_BLEND; 464 glDisable GL_BLEND;
253 glDisable GL_TEXTURE_2D; 465 glDisable GL_TEXTURE_2D;
254} 466}
255 467
256package Crossfire::Client::Widget::Frame; 468#############################################################################
257 469
258our @ISA = Crossfire::Client::Widget::Container::; 470package CFClient::UI::Frame;
471
472our @ISA = CFClient::UI::Bin::;
259 473
260use SDL::OpenGL; 474use SDL::OpenGL;
261 475
262sub size_request { 476sub size_request {
263 my ($self) = @_; 477 my ($self) = @_;
264 my $chld = $self->get 478 my $chld = $self->child
265 or return (0, 0); 479 or return (0, 0);
266 480
267 $chld->move (2, 2); 481 $chld->move (2, 2);
268 482
269 map { $_ + 4 } $chld->size_request; 483 map { $_ + 4 } $chld->size_request;
270} 484}
271 485
486sub size_allocate {
487 my ($self, $w, $h) = @_;
488
489 return unless $self->{w} != $w || $self->{h} != $h;
490
491 $self->{w} = $w;
492 $self->{h} = $h;
493
494 $self->child->size_allocate ($w - 4, $h - 4);
495 $self->child->move (2, 2);
496}
497
272sub _draw { 498sub _draw {
273 my ($self) = @_; 499 my ($self) = @_;
274 500
275 my $chld = $self->get; 501 my $chld = $self->child;
276 502
277 my ($w, $h) = $chld->size_request; 503 my ($w, $h) = $chld->size_request;
278 504
279 glBegin GL_QUADS; 505 glBegin GL_QUADS;
280 glColor 0, 0, 0; 506 glColor 0, 0, 0;
281 glTexCoord 0, 0; glVertex 0 , 0; 507 glVertex 0 , 0;
282 glTexCoord 0, 1; glVertex 0 , $h + 4; 508 glVertex 0 , $h + 4;
283 glTexCoord 1, 1; glVertex $w + 4 , $h + 4; 509 glVertex $w + 4 , $h + 4;
284 glTexCoord 1, 0; glVertex $w + 4 , 0; 510 glVertex $w + 4 , 0;
285 glEnd; 511 glEnd;
286 512
287 $chld->draw; 513 $chld->draw;
288} 514}
289 515
516#############################################################################
517
290package Crossfire::Client::Widget::FancyFrame; 518package CFClient::UI::FancyFrame;
291 519
292our @ISA = Crossfire::Client::Widget::Frame::; 520our @ISA = CFClient::UI::Bin::;
293 521
294use SDL::OpenGL; 522use SDL::OpenGL;
295 523
296sub new { 524my @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 $_ } 525 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
302 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 526 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 527
308sub size_request { 528sub size_request {
309 my ($self) = @_; 529 my ($self) = @_;
530
310 my ($w, $h) = $self->get->size_request; 531 my ($w, $h) = $self->SUPER::size_request;
311 532
312 $h += $self->{txts}->[1]->{height}; 533 $h += $tex[1]->{h};
313 $h += $self->{txts}->[4]->{height}; 534 $h += $tex[4]->{h};
314 $w += $self->{txts}->[2]->{width}; 535 $w += $tex[2]->{w};
315 $w += $self->{txts}->[3]->{width}; 536 $w += $tex[3]->{w};
316
317 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height});
318 537
319 ($w, $h) 538 ($w, $h)
320} 539}
321 540
541sub size_allocate {
542 my ($self, $w, $h) = @_;
543
544 return unless $self->{w} != $w || $self->{h} != $h;
545
546 $self->SUPER::size_allocate ($w, $h);
547
548 $h -= $tex[1]->{h};
549 $h -= $tex[4]->{h};
550 $w -= $tex[2]->{w};
551 $w -= $tex[3]->{w};
552
553 $h = $h < 0 ? 0 : $h;
554 $w = $w < 0 ? 0 : $w;
555
556 my $child = $self->child;
557
558 $child->size_allocate ($w, $h);
559 $child->move ($tex[3]->{w}, $tex[1]->{h});
560}
561
322sub _draw { 562sub _draw {
323 my ($self) = @_; 563 my ($self) = @_;
324 564
325 my ($w, $h) = $self->size_request; 565 my ($w, $h) = ($self->{w}, $self->{h});
326 my ($cw, $ch) = $self->get->size_request; 566 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
327 567
328 glEnable GL_BLEND; 568 glEnable GL_BLEND;
569 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
329 glEnable GL_TEXTURE_2D; 570 glEnable GL_TEXTURE_2D;
330 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
331 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 571 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
332 572
333
334 my $top = $self->{txts}->[1]; 573 my $top = $tex[1];
335 glBindTexture GL_TEXTURE_2D, $top->{name}; 574 $top->draw_quad (0, 0, $w, $top->{h});
336 575
337 glColor 1, 1, 1, 0.8;
338
339 glBegin GL_QUADS;
340 glTexCoord 0, 0; glVertex 0 , 0;
341 glTexCoord 0, 1; glVertex 0 , $top->{height};
342 glTexCoord 1, 1; glVertex $w , $top->{height};
343 glTexCoord 1, 0; glVertex $w , 0;
344 glEnd;
345
346 my $left = $self->{txts}->[3]; 576 my $left = $tex[3];
347 glBindTexture GL_TEXTURE_2D, $left->{name}; 577 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
348 578
349 glColor 1, 1, 1, 0.8;
350
351 glBegin GL_QUADS;
352 glTexCoord 0, 0; glVertex 0 , $top->{height};
353 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
354 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
355 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
356 glEnd;
357
358 my $right = $self->{txts}->[2]; 579 my $right = $tex[2];
359 glBindTexture GL_TEXTURE_2D, $right->{name}; 580 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
360 581
361 glColor 1, 1, 1, 0.8;
362
363 glBegin GL_QUADS;
364 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
365 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
366 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
367 glTexCoord 1, 0; glVertex $w , $top->{height};
368 glEnd;
369
370 my $bottom = $self->{txts}->[4]; 582 my $bottom = $tex[4];
371 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 583 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
372 584
373 glColor 1, 1, 1, 0.8; 585 my $bg = $tex[0];
374
375 glBegin GL_QUADS;
376 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
377 glTexCoord 0, 1; glVertex 0 , $h;
378 glTexCoord 1, 1; glVertex $w , $h;
379 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
380 glEnd;
381
382 my $bg = $self->{txts}->[0];
383 glBindTexture GL_TEXTURE_2D, $bg->{name}; 586 glBindTexture GL_TEXTURE_2D, $bg->{name};
587 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
384 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 588 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
385 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 589 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
386 590
387 glColor 1, 1, 1, 0.8;
388
389 my $rep_x = $cw / $bg->{width}; 591 my $rep_x = $cw / $bg->{w};
390 my $rep_y = $ch / $bg->{height}; 592 my $rep_y = $ch / $bg->{h};
391 593
392 glBegin GL_QUADS; 594 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
393 glTexCoord 0, 0; glVertex $left->{width}, $top->{height};
394 glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch;
395 glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch;
396 glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height};
397 glEnd;
398
399 $self->get->draw;
400 595
401 glDisable GL_BLEND; 596 glDisable GL_BLEND;
402 glDisable GL_TEXTURE_2D; 597 glDisable GL_TEXTURE_2D;
403}
404 598
405package Crossfire::Client::Widget::Table; 599 $self->child->draw;
406 600
407our @ISA = Crossfire::Client::Widget::Container::; 601}
602
603#############################################################################
604
605package CFClient::UI::Table;
606
607our @ISA = CFClient::UI::Base::;
408 608
409use SDL::OpenGL; 609use SDL::OpenGL;
410 610
411sub add { 611sub add {
412 my ($self, $x, $y, $chld) = @_; 612 my ($self, $x, $y, $chld) = @_;
413 my $old_chld = $self->{childs}[$y][$x]; 613 my $old_chld = $self->{children}[$y][$x];
414 614
415 $self->{childs}[$y][$x] = $chld; 615 $self->{children}[$y][$x] = $chld;
416 $chld->set_parent ($self); 616 $chld->set_parent ($self);
417 $self->update; 617 $self->update;
418} 618}
419 619
420sub max_row_height { 620sub max_row_height {
421 my ($self, $row) = @_; 621 my ($self, $row) = @_;
422 622
423 my $hs = 0; 623 my $hs = 0;
424 for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { 624 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
425 my $c = $self->{childs}->[$row]->[$xi]; 625 my $c = $self->{children}->[$row]->[$xi];
426 if ($c) { 626 if ($c) {
427 my ($w, $h) = $c->size_request; 627 my ($w, $h) = $c->size_request;
428 if ($hs < $h) { $hs = $h } 628 if ($hs < $h) { $hs = $h }
429 } 629 }
430 } 630 }
433 633
434sub max_col_width { 634sub max_col_width {
435 my ($self, $col) = @_; 635 my ($self, $col) = @_;
436 636
437 my $ws = 0; 637 my $ws = 0;
438 for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { 638 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
439 my $c = ($self->{childs}->[$yi] || [])->[$col]; 639 my $c = ($self->{children}->[$yi] || [])->[$col];
440 if ($c) { 640 if ($c) {
441 my ($w, $h) = $c->size_request; 641 my ($w, $h) = $c->size_request;
442 if ($ws < $w) { $ws = $w } 642 if ($ws < $w) { $ws = $w }
443 } 643 }
444 } 644 }
448sub size_request { 648sub size_request {
449 my ($self) = @_; 649 my ($self) = @_;
450 650
451 my ($hs, $ws) = (0, 0); 651 my ($hs, $ws) = (0, 0);
452 652
453 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 653 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
454 $hs += $self->max_row_height ($yi); 654 $hs += $self->max_row_height ($yi);
455 } 655 }
456 656
457 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 657 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
458 my $wm = 0; 658 my $wm = 0;
459 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 659 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
460 $wm += $self->max_col_width ($xi) 660 $wm += $self->max_col_width ($xi)
461 } 661 }
462 if ($ws < $wm) { $ws = $wm } 662 if ($ws < $wm) { $ws = $wm }
463 } 663 }
464 664
467 667
468sub _draw { 668sub _draw {
469 my ($self) = @_; 669 my ($self) = @_;
470 670
471 my $y = 0; 671 my $y = 0;
472 for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { 672 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
473 my $x = 0; 673 my $x = 0;
474 674
475 for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { 675 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
476 676
477 my $c = $self->{childs}->[$yi]->[$xi]; 677 my $c = $self->{children}->[$yi]->[$xi];
478 if ($c) { 678 if ($c) {
479 $c->move ($x, $y, 0); #TODO: Move to size_request 679 $c->move ($x, $y, 0); #TODO: Move to size_request
480 $c->draw if $c; 680 $c->draw if $c;
481 } 681 }
482 682
485 685
486 $y += $self->max_row_height ($yi); 686 $y += $self->max_row_height ($yi);
487 } 687 }
488} 688}
489 689
490package Crossfire::Client::Widget::VBox; 690#############################################################################
491 691
692package CFClient::UI::VBox;
693
492our @ISA = Crossfire::Client::Widget::Container::; 694our @ISA = CFClient::UI::Container::;
493 695
494use SDL::OpenGL; 696use SDL::OpenGL;
495 697
496sub add {
497 my ($self, $chld) = @_;
498 push @{$self->{childs}}, $chld;
499 $chld->set_parent ($self);
500 $self->update;
501}
502
503sub size_request { 698sub size_request {
504 my ($self) = @_; 699 my ($self) = @_;
505 700
506 my ($hs, $ws) = (0, 0); 701 my @alloc = map [$_->size_request], @{$self->{children}};
507 for (@{$self->{childs} || []}) { 702
508 my ($w, $h) = $_->size_request;
509 $hs += $h;
510 if ($ws < $w) { $ws = $w }
511 } 703 (
512 704 (List::Util::max map $_->[0], @alloc),
513 return ($ws, $hs); 705 (List::Util::sum map $_->[1], @alloc),
706 )
514} 707}
515 708
516sub _draw { 709sub size_allocate {
517 my ($self) = @_; 710 my ($self, $w, $h) = @_;
518 711
519 my ($x, $y); 712 return unless $self->{w} != $w || $self->{h} != $h;
520 for (@{$self->{childs} || []}) { 713
521 $_->move (0, $y, 0); #TODO: move to size_request 714 $self->{w} = $w;
522 $_->draw; 715 $self->{h} = $h;
523 my ($w, $h) = $_->size_request; 716
717 return unless $self->{h};
718
719 my $children = $self->{children};
720
721 my @h = map +($_->size_request)[1], @$children;
722
723 my $req_h = List::Util::sum @h;
724
725 if ($req_h > $h) {
726 # ah well, not enough space
727 $_ = $h[$_] * $h / $req_h for @h;
728 } else {
729 my @exp = grep $_->{expand}, @$children;
730 @exp = @$children unless @exp;
731
732 my %exp = map +($_ => 1), @exp;
733
734 for (0 .. $#$children) {
735 my $child = $children->[$_];
736
737 my $alloc_h = $h[$_];
738 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
739 $h[$_] = $alloc_h;
740 }
741 }
742
743 my $y = 0;
744 for (0 .. $#$children) {
745 my $child = $children->[$_];
746 my $h = $h[$_];
747 $child->move (0, $y);
748 $child->size_allocate ($w, $h);
749
524 $y += $h; 750 $y += $h;
525 } 751 }
526} 752}
527 753
528package Crossfire::Client::Widget::Label; 754#############################################################################
529 755
530our @ISA = Crossfire::Client::Widget::; 756package CFClient::UI::Label;
757
758our @ISA = CFClient::UI::Base::;
531 759
532use SDL::OpenGL; 760use SDL::OpenGL;
533 761
534sub new { 762sub new {
535 my ($class, $x, $y, $z, $height, $text) = @_; 763 my ($class, %arg) = @_;
536 764
537 # TODO: color, and make height, xyz etc. optional 765 my $self = $class->SUPER::new (
538 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 766 fg => [1, 1, 1],
767 height => $::FONTSIZE,
768 text => "",
769 align => -1,
770 layout => new CFClient::Layout,
771 %arg
772 );
539 773
540 $self->set_text ($text); 774 $self->set_text ($self->{text});
541 775
542 $self 776 $self
777}
778
779sub escape_text {
780 local $_ = $_[1];
781
782 s/&/&amp;/g;
783 s/>/&gt;/g;
784 s/</&lt;/g;
785
786 $_[1]
543} 787}
544 788
545sub set_text { 789sub set_text {
546 my ($self, $text) = @_; 790 my ($self, $text) = @_;
547 791
548 $self->{text} = $text; 792 $self->{text} = $text;
549 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 793 $self->{layout}->set_markup ($text);
550 794
795 delete $self->{texture};
551 $self->update; 796 $self->update;
552} 797}
553 798
554sub get_text { 799sub get_text {
555 my ($self, $text) = @_; 800 my ($self, $text) = @_;
558} 803}
559 804
560sub size_request { 805sub size_request {
561 my ($self) = @_; 806 my ($self) = @_;
562 807
563 ( 808 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height});
810 $self->{layout}->size
811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
812# (
564 $self->{texture}{width}, 813# $self->{texture}{w},
565 $self->{texture}{height}, 814# $self->{texture}{h},
566 ) 815# )
816# } else {
817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
818#
819# ($w, $h)
820# }
821}
822
823sub size_allocate {
824 my ($self, $w, $h) = @_;
825
826 return unless $self->{w} != $w || $self->{h} != $h;
827
828 $self->SUPER::size_allocate ($w, $h);
829 delete $self->{texture};
830}
831
832sub update {
833 my ($self) = @_;
834
835 delete $self->{texture};
836 $self->SUPER::update;
567} 837}
568 838
569sub _draw { 839sub _draw {
570 my ($self) = @_; 840 my ($self) = @_;
571 841
572 my $tex = $self->{texture}; 842 my $tex = $self->{texture} ||= do {
843 $self->{layout}->set_width ($self->{w});
844 new_from_layout CFClient::Texture $self->{layout}
845 };
573 846
574 glEnable GL_BLEND; 847 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
575 glEnable GL_TEXTURE_2D; 849 glEnable GL_TEXTURE_2D;
576 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
577 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
578 glBindTexture GL_TEXTURE_2D, $tex->{name};
579 851
580 glColor 1, 1, 1, 0.6; # TODO color 852 glColor @{$self->{fg}};
581 853
582 glBegin GL_QUADS; 854 my $x =
583 glTexCoord 0, 0; glVertex 0 , 0; 855 $self->{align} < 0 ? 0
584 glTexCoord 0, 1; glVertex 0 , $tex->{height}; 856 : $self->{align} > 0 ? $self->{w} - $tex->{w}
585 glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; 857 : ($self->{w} - $tex->{w}) * 0.5;
586 glTexCoord 1, 0; glVertex $tex->{width}, 0;
587 glEnd;
588 858
859 $tex->draw_quad ($x, 0);
860
861 glDisable GL_TEXTURE_2D;
589 glDisable GL_BLEND; 862 glDisable GL_BLEND;
590 glDisable GL_TEXTURE_2D; 863 glGetError and die;
591} 864}
592 865
593package Crossfire::Client::Widget::TextEntry; 866#############################################################################
594 867
868package CFClient::UI::Entry;
869
595our @ISA = Crossfire::Client::Widget::Label::; 870our @ISA = CFClient::UI::Label::;
596 871
597use SDL; 872use SDL;
598use SDL::OpenGL; 873use SDL::OpenGL;
599 874
875sub new {
876 my $class = shift;
877
878 $class->SUPER::new (
879 fg => [1, 1, 1],
880 bg => [0, 0, 0, 0.4],
881 active_bg => [1, 1, 1],
882 active_fg => [0, 0, 0],
883 @_
884 )
885}
886
887sub _set_text {
888 my ($self, $text) = @_;
889
890 $self->{last_activity} = $::NOW;
891
892 $self->{text} = $text;
893 $self->{layout}->set_width ($self->{w});
894
895 $text =~ s/./*/g if $self->{hidden};
896
897
898 $self->{layout}->set_markup ($self->escape_text ($text));
899
900 $text = substr $text, 0, $self->{cursor};
901 utf8::encode $text;
902
903 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
904}
905
906sub size_request {
907 my ($self) = @_;
908
909 my ($w, $h) = $self->SUPER::size_request;
910
911 ($w + 1, $h) # add 1 for cursor
912}
913
914sub size_allocate {
915 my ($self, $w, $h) = @_;
916
917 return unless $self->{w} != $w || $self->{h} != $h;
918
919 $self->SUPER::size_allocate ($w, $h);
920
921 $self->_set_text ($self->{text});
922}
923
924sub set_text {
925 my ($self, $text) = @_;
926
927 $self->{cursor} = length $text;
928 $self->_set_text ($text);
929 $self->update;
930}
931
600sub key_down { 932sub key_down {
601 my ($self, $ev) = @_; 933 my ($self, $ev) = @_;
602 934
603 my $mod = $ev->key_mod; 935 my $mod = $ev->key_mod;
604 my $sym = $ev->key_sym; 936 my $sym = $ev->key_sym;
605 937
606 $ev->set_unicode (1);
607 my $uni = $ev->key_unicode; 938 my $uni = $ev->key_unicode;
608 939
609 my $text = $self->get_text; 940 my $text = $self->get_text;
610 941
611 if ($sym == SDLK_BACKSPACE) { 942 if ($sym == SDLK_BACKSPACE) {
612 substr $text, -1, 1, ''; 943 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
613 944 } elsif ($sym == SDLK_DELETE) {
945 substr $text, $self->{cursor}, 1, "";
946 } elsif ($sym == SDLK_LEFT) {
947 --$self->{cursor} if $self->{cursor};
948 } elsif ($sym == SDLK_RIGHT) {
949 ++$self->{cursor} if $self->{cursor} < length $self->{text};
614 } elsif ($uni) { 950 } elsif ($uni) {
615 $text .= chr $uni; 951 substr $text, $self->{cursor}++, 0, chr $uni;
616 } 952 }
953
617 $self->set_text ($text); 954 $self->_set_text ($text);
955 $self->update;
618} 956}
619 957
958sub focus_in {
959 my ($self) = @_;
620 960
621# XXX: TextView isn't neccessary with pango multiline text rendering 961 $self->{last_activity} = $::NOW;
622package Crossfire::Client::Widget::TextView; 962
963 $self->SUPER::focus_in;
964}
965
966sub button_down {
967 my ($self, $ev, $x, $y) = @_;
968
969 $self->SUPER::button_down ($ev, $x, $y);
970
971 my $idx = $self->{layout}->xy_to_index ($x, $y);
972
973 # byte-index to char-index
974 my $text = $self->{layout};
975 utf8::encode $text;
976 $self->{cursor} = length substr $text, 0, $idx;
977
978 $self->_set_text ($self->{text});
979 $self->update;
980}
981
982sub mouse_motion {
983 my ($self, $ev, $x, $y) = @_;
984# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
985}
986
987sub _draw {
988 my ($self) = @_;
989
990 local $self->{fg} = $self->{fg};
991
992 if ($FOCUS == $self) {
993 glColor @{$self->{active_bg}};
994 $self->{fg} = $self->{active_fg};
995 } else {
996 glColor @{$self->{bg}};
997 }
998
999 glBegin GL_QUADS;
1000 glVertex 0 , 0;
1001 glVertex 0 , $self->{h};
1002 glVertex $self->{w}, $self->{h};
1003 glVertex $self->{w}, 0;
1004 glEnd;
1005
1006 $self->SUPER::_draw;
1007
1008 #TODO: force update every cursor change :(
1009 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1010 glColor @{$self->{fg}};
1011 glBegin GL_LINES;
1012 glVertex $self->{cur_x}, $self->{cur_y};
1013 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1014 glEnd;
1015 }
1016}
1017
1018#############################################################################
1019
1020package CFClient::UI::Slider;
623 1021
624use strict; 1022use strict;
625 1023
626our @ISA = qw/Crossfire::Client::Widget/;
627
628use SDL::OpenGL; 1024use SDL::OpenGL;
629use SDL::OpenGL::Constants; 1025
1026our @ISA = CFClient::UI::DrawBG::;
1027
1028sub size_request {
1029 my ($self) = @_;
1030
1031 my $w = 50;
1032 my $h = 10;
1033
1034 $self->{vertical} ? ($h, $w) : ($w, $h)
1035}
630 1036
631sub new { 1037sub new {
632 my ($class, $text, $h) = @_; 1038 my $class = shift;
1039
1040 # range [value, low, high, page]
1041
633 my $self = $class->SUPER::new (); 1042 $class->SUPER::new (
634 1043 fg => [1, 1, 1],
635 $self->{txt_height} = $h; 1044 active_fg => [0, 0, 0],
636 @{$self->{lines}} = split /\r?\n/, $text; 1045 range => [0, 0, 100, 10],
637 1046 vertical => 1,
638 for (split /\r?\n/, $text) { 1047 @_
639 $self->add_line ($_);
640 } 1048 )
641 $self
642} 1049}
643 1050
644#sub render_lines { 1051sub button_down {
1052 my ($self, $ev, $x, $y) = @_;
1053
1054 $self->SUPER::button_down ($ev, $x, $y);
1055 $self->mouse_motion ($ev, $x, $y);
1056}
1057
1058sub mouse_motion {
1059 my ($self, $ev, $x, $y) = @_;
1060
1061 if ($GRAB == $self) {
1062 my ($value, $lo, $hi, $page) = @{$self->{range}};
1063
1064 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1065
1066 $x = $x * ($hi - $lo) / $w + $lo;
1067 $x = $lo if $x < $lo;
1068 $x = $hi - $page if $x > $hi - $page;
1069 $self->{range}[0] = $x;
1070
1071 $self->emit (changed => $x);
1072 $self->update;
1073 }
1074}
1075
1076sub _draw {
645# my ($self) = @_; 1077 my ($self) = @_;
646# 1078
647# $self->{txt_lines} = []; 1079 $self->SUPER::_draw ();
648# 1080
649# for (@{$self->{lines}}) { 1081 my ($w, $h) = @$self{qw(w h)};
650# push @{$self->{txt_lines}}, 1082
651# new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_; 1083 if ($self->{vertical}) {
1084 # draw a vertical slider like a rotated horizontal slider
1085
1086 glRotate 90, 0, 0, 1;
1087 glTranslate 0, -$self->{w}, 0;
1088
1089 ($w, $h) = ($h, $w);
652# } 1090 }
653#}
654 1091
655sub add_line { 1092 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
656 my ($self, $line) = @_; 1093 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
657 push @{$self->{lines}}, $line;
658 1094
659 push @{$self->{txt_lines}}, 1095 my ($value, $lo, $hi, $page) = @{$self->{range}};
660 new_from_text Crossfire::Client::Texture $line, $self->{txt_height};
661}
662 1096
663sub size_request { 1097 $page = int $page * $w / ($hi - $lo);
664 my ($self) = @_; 1098 $value = int +($value - $lo) * $w / ($hi - $lo);
665 1099
666 my $w = 0; 1100 $w -= $page;
667 my $h = 0; 1101 $page &= ~1;
1102 glTranslate $page * 0.5, 0, 0;
668 1103
669 for (@{$self->{txt_lines}}) { 1104 glColor @$fg;
670 if ($w < $_->{width}) { $w = $_->{width} } 1105 glBegin GL_LINES;
671 $h += $_->{height}; 1106 glVertex 0, 0; glVertex 0, $h;
672 } 1107 glVertex $w - 1, 0; glVertex $w - 1, $h;
1108 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1109 glEnd;
673 1110
674 return ($w, $h); 1111 my $knob_a = $value - $page * 0.5;
675} 1112 my $knob_b = $value + $page * 0.5;
676
677sub draw_line {
678 my ($self, $tex, $y) = @_;
679
680 glBindTexture GL_TEXTURE_2D, $tex->{name};
681
682 glColor 1, 0, 1;
683 1113
684 glBegin GL_QUADS; 1114 glBegin GL_QUADS;
685 glTexCoord 0, 0; glVertex 0 , $y; 1115 glColor @$fg;
686 glTexCoord 0, 1; glVertex 0 , $y + $tex->{height}; 1116 glVertex $knob_a, 0;
687 glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height}; 1117 glVertex $knob_a, $h;
688 glTexCoord 1, 0; glVertex $tex->{width}, $y; 1118 glVertex $knob_b, $h;
1119 glVertex $knob_b, 0;
1120
1121 if ($knob_a < $knob_b - 2) {
1122 glColor @$bg;
1123 glVertex $knob_a + 1, 1;
1124 glVertex $knob_a + 1, $h - 1;
1125 glVertex $knob_b - 1, $h - 1;
1126 glVertex $knob_b - 1, 1;
1127 }
689 glEnd; 1128 glEnd;
690} 1129}
691 1130
692sub _draw { 1131#############################################################################
693 my ($self) = @_;
694 1132
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}
708
709package Crossfire::Client::Widget::MapWidget; 1133package CFClient::UI::MapWidget;
710 1134
711use strict; 1135use strict;
712 1136
713use List::Util qw(min max); 1137use List::Util qw(min max);
714 1138
715use SDL; 1139use SDL;
716use SDL::OpenGL; 1140use SDL::OpenGL;
717use SDL::OpenGL::Constants;
718 1141
719our @ISA = Crossfire::Client::Widget::; 1142our @ISA = CFClient::UI::Base::;
1143
1144sub new {
1145 my $class = shift;
1146
1147 $class->SUPER::new (
1148 z => -1,
1149 list => (glGenLists 1),
1150 @_
1151 )
1152}
720 1153
721sub key_down { 1154sub key_down {
722 print "MAPKEYDOWN\n"; 1155 print "MAPKEYDOWN\n";
723} 1156}
724 1157
725sub key_up { 1158sub key_up {
726} 1159}
727 1160
1161sub size_request {
1162 (
1163 1 + int $::WIDTH / 32,
1164 1 + int $::HEIGHT / 32,
1165 )
1166}
1167
1168sub update {
1169 my ($self) = @_;
1170
1171 $self->{need_update} = 1;
1172 $self->SUPER::update;
1173}
1174
728sub _draw { 1175sub _draw {
729 my ($self) = @_; 1176 my ($self) = @_;
730 1177
1178 if (delete $self->{need_update}) {
1179 glNewList $self->{list}, GL_COMPILE;
1180
731 my $mx = $::CONN->{mapx}; 1181 my $mx = $::CONN->{mapx};
732 my $my = $::CONN->{mapy}; 1182 my $my = $::CONN->{mapy};
733 1183
734 my $map = $::CONN->{map}; 1184 my $map = $::CONN->{map};
735 1185
736 my ($xofs, $yofs); 1186 my ($xofs, $yofs);
737 1187
738 my $sw = 1 + int $::WIDTH / 32; 1188 my $sw = 1 + int $::WIDTH / 32;
739 my $sh = 1 + int $::HEIGHT / 32; 1189 my $sh = 1 + int $::HEIGHT / 32;
740 1190
741 if ($::CONN->{mapw} > $sw) { 1191 if ($::CONN->{mapw} > $sw) {
742 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1192 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
743 } else { 1193 } else {
744 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1194 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
745 } 1195 }
746 1196
747 if ($::CONN->{maph} > $sh) { 1197 if ($::CONN->{maph} > $sh) {
748 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1198 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
749 } else { 1199 } else {
750 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1200 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
751 } 1201 }
752 1202
753 glEnable GL_TEXTURE_2D; 1203 glEnable GL_TEXTURE_2D;
754 glEnable GL_BLEND; 1204 glEnable GL_BLEND;
1205 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
755 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
756 1207
1208 my $sw4 = ($sw + 3) & ~3;
1209 my $darkness = "\x00" x ($sw4 * $sh);
1210
757 for my $x (0 .. $sw - 1) { 1211 for my $x (0 .. $sw - 1) {
1212 my $row = $map->[$x + $xofs];
758 for my $y (0 .. $sh - 1) { 1213 for my $y (0 .. $sh - 1) {
759 1214
760 my $cell = $map->[$x + $xofs][$y + $yofs] 1215 my $cell = $row->[$y + $yofs]
761 or next; 1216 or next;
762 1217
763 my $darkness = $cell->[0] * (1 / 255); 1218 my $dark = $cell->[0];
764 if ($darkness < 0) { 1219 if ($dark < 0) {
765 glColor 0.3, 0.3, 0.3; 1220 substr $darkness, $y * $sw4 + $x, 1, chr 224;
766 } else { 1221 } else {
767 glColor $darkness, $darkness, $darkness; 1222 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
768 } 1223 }
769 1224
770 for my $num (grep $_, @$cell[1,2,3]) { 1225 for my $num (grep $_, @$cell[1,2,3]) {
771 my $tex = $::CONN->{face}[$num]{texture} || next; 1226 my $tex = $::CONN->{face}[$num]{texture} || next;
1227
1228 my ($w, $h) = @$tex{qw(w h)};
772 1229
773 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1230 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
774
775 my $w = $tex->{width};
776 my $h = $tex->{height};
777
778 my $px = ($x + 1) * 32 - $w;
779 my $py = ($y + 1) * 32 - $h;
780
781 glBegin GL_QUADS;
782 glTexCoord 0, 0; glVertex $px , $py;
783 glTexCoord 0, 1; glVertex $px , $py + $h;
784 glTexCoord 1, 1; glVertex $px + $w, $py + $h;
785 glTexCoord 1, 0; glVertex $px + $w, $py;
786 glEnd; 1231 }
787 } 1232 }
788 } 1233 }
1234
1235# if (1) { # higher quality darkness
1236# $lighting =~ s/(.)/$1$1$1/gs;
1237# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1238#
1239# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1240#
1241# $lighting = $pb->get_pixels;
1242# $lighting =~ s/(.)../$1/gs;
789 } 1243# }
790 1244
1245 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1246
1247 $darkness = new CFClient::Texture
1248 w => $sw4,
1249 h => $sh,
1250 data => $darkness,
1251 internalformat => GL_ALPHA,
1252 format => GL_ALPHA;
1253
1254 glColor 0.45, 0.45, 0.45, 1;
1255 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1256
791 glDisable GL_TEXTURE_2D; 1257 glDisable GL_TEXTURE_2D;
792 glDisable GL_BLEND; 1258 glDisable GL_BLEND;
1259
1260 glEndList;
1261 }
1262
1263 glCallList $self->{list};
793} 1264}
794 1265
795my %DIR = ( 1266my %DIR = (
796 SDLK_KP8, [1, "north"], 1267 SDLK_KP8, [1, "north"],
797 SDLK_KP9, [2, "northeast"], 1268 SDLK_KP9, [2, "northeast"],
841 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1312 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
842 $::CONN->send ("command run_stop"); 1313 $::CONN->send ("command run_stop");
843 } 1314 }
844} 1315}
845 1316
8461; 1317#############################################################################
847 1318
1319package CFClient::UI::Animator;
1320
1321use SDL::OpenGL;
1322
1323our @ISA = CFClient::UI::Bin::;
1324
1325sub moveto {
1326 my ($self, $x, $y) = @_;
1327
1328 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1329 $self->{speed} = 0.001;
1330 $self->{time} = 1;
1331
1332 ::animation_start $self;
1333}
1334
1335sub animate {
1336 my ($self, $interval) = @_;
1337
1338 $self->{time} -= $interval * $self->{speed};
1339 if ($self->{time} <= 0) {
1340 $self->{time} = 0;
1341 ::animation_stop $self;
1342 }
1343
1344 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
1345
1346 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
1347 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
1348}
1349
1350sub _draw {
1351 my ($self) = @_;
1352
1353 glPushMatrix;
1354 glRotate $self->{time} * 1000, 0, 1, 0;
1355 $self->{children}[0]->draw;
1356 glPopMatrix;
1357}
1358
1359#############################################################################
1360
1361package CFClient::UI::Toplevel;
1362
1363our @ISA = CFClient::UI::Container::;
1364
1365sub size_request {
1366 ($::WIDTH, $::HEIGHT)
1367}
1368
1369sub size_allocate {
1370 my ($self, $w, $h) = @_;
1371
1372 $self->SUPER::size_allocate ($w, $h);
1373
1374 $_->size_allocate ($_->size_request)
1375 for @{$self->{children}};
1376}
1377
1378sub translate {
1379 my ($self, $x, $y) = @_;
1380
1381 ($x, $y)
1382}
1383
1384sub update {
1385 my ($self) = @_;
1386
1387 $self->size_allocate ($self->size_request);
1388 ::refresh ();
1389}
1390
1391sub add {
1392 my ($self, $widget) = @_;
1393
1394 $self->SUPER::add ($widget);
1395
1396 $widget->size_allocate ($widget->size_request);
1397}
1398
1399sub draw {
1400 my ($self) = @_;
1401
1402 $self->_draw;
1403}
1404
1405#############################################################################
1406
1407package CFClient::UI;
1408
1409$TOPLEVEL = new CFClient::UI::Toplevel;
1410
14111
1412

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines