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