Adonthell  0.4
win_select.cc
1 /*
2  $Id: win_select.cc,v 1.7 2002/01/05 21:22:56 ksterker Exp $
3 
4  (C) Copyright 2000 Joel Vennin
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT AN Y WARRANTY.
11 
12  See the COPYING file for more details
13 */
14 
15 #include "win_select.h"
16 
17 #include "audio.h"
18 
19 win_select::win_select()
20 {
21  cur_select_ = list_wb_.begin();
22 
23  set_mode(MODE_BRIGHTNESS);
24 
25  border_select_ = NULL;
26 
27  set_circle(false);
28 
29  finish_scroll_ = true;
30  //set_visible_scrollbar(false);
31 }
32 
33 
34 void win_select::add(win_base * w)
35 {
36  win_scroll::add(w);
37  cur_select_ = list_wb_.begin ();
38  //if select has a border for the selection, just set border to the win_base added
39  if(border_select_ != NULL) w->set_border(*border_select_);
40 
41  //set the object to unselect
42  rules(false, w);
43 
44  set_default();
45 
46  //update_cur_select_position();
47 }
48 
49 
50 void win_select::remove(win_base * w)
51 {
52  rules(false, w);
53 
54  win_scroll::remove(w);
55 
56  set_default();
57 }
58 
59 
60 void win_select::remove_all()
61 {
62  win_scroll::remove_all();
63  cur_select_ = list_wb_.begin();
64 }
65 
66 
67 void win_select::next()
68 {
69  //test if next possible
70  if(cur_select_ == list_wb_.end() || list_wb_.size() == 0) return;
71 
72  audio::play_wave (-1, 1);
73  (*cur_select_)->on_unselect();
74 
75  //unselect cur element
76  rules(false,*cur_select_);
77  (*cur_select_)->set_activate (false);
78 
79  //create a temporary index
80  lwb :: iterator i = cur_select_;
81 
82  //go next
83  i++;
84 
85  //while not a the end, not be selected and different at old object go to the next
86  while( i != list_wb_.end() && !(*i)->is_can_be_selected() && i != cur_select_) i++;
87 
88  //if at end of list and select circle is activate
89  if(i == list_wb_.end())
90  {
91  //cur is the begin of list
92  if( circle_ )
93  {
94  i = list_wb_.begin();
95  while(i != list_wb_.end() && !(*i)->is_can_be_selected() && i != cur_select_) i++;
96  if(i != list_wb_.end()) cur_select_ = i;
97  }
98  }else cur_select_ = i;
99 
100  rules(true,*cur_select_);
101 
102  (*cur_select_)->on_select();
103 
104  on_next();
105 
106  finish_scroll_ =false;
107 
108  //update_cur_select_position();
109 }
110 
111 
112 void win_select::previous()
113 {
114  if(cur_select_==list_wb_.end() || list_wb_.size() == 0) return;
115 
116  audio::play_wave (-1, 1);
117  (*cur_select_)->on_unselect();
118 
119  //set to unselect object
120  rules(false,*cur_select_);
121 
122  (*cur_select_)->set_activate (false);
123 
124  lwb::iterator i=cur_select_;
125 
126  if(circle_)
127  {
128  if(i==list_wb_.begin()) i=list_wb_.end();
129  i--;
130  }
131  else if(i!=list_wb_.begin()) i--;
132 
133  while(i != list_wb_.begin() && !(*i)->is_can_be_selected() && i != cur_select_) i--;
134 
135  if( i== list_wb_.begin() && !(*i)->is_can_be_selected())
136  {
137  if(circle_)
138  {
139  i = list_wb_.end();
140  i--;
141  while(i!=list_wb_.begin() && !(*i)->is_can_be_selected() && i!=cur_select_) i--;
142  if((*i)->is_can_be_selected()) cur_select_=i;
143  }
144  } else cur_select_=i;
145 
146  (*cur_select_)->on_select();
147 
148  //set to select object
149  rules(true,*cur_select_);
150 
151  //update_position();
152  on_previous();
153 
154  finish_scroll_ =false;
155  //update_cur_select_position();
156 }
157 
158 
159 void win_select::activate()
160 {
161  if(cur_select_ == list_wb_.end()) return;
162 
163  audio::play_wave (-1, 0);
164 
165  //set_activate(false);
166 
167  (*cur_select_)->set_activate(true);
168 
169  on_activate_key();
170 }
171 
172 
174 {
175 
177  {
178  if(focus_object_) return true;
179 
180  if(input::has_been_pushed(win_keys::KEY_NEXT)) next();
181  if(input::has_been_pushed(win_keys::KEY_PREVIOUS)) previous();
182  if(input::has_been_pushed(win_keys::KEY_ACTIVATE) ||
183  input::has_been_pushed(win_keys::KEY_ACTIVATE_ENTRY)) activate();
184 
185  return true;
186  }
187  return false;
188 }
189 
190 void win_select::rules(const bool b, win_base * wb)
191 {
192  if(!wb->is_can_be_selected()) return;
193 
194  switch(mode_)
195  {
196  case MODE_BORDER:
197  wb->set_visible_border(b);
198  break;
199 
200  case MODE_BRIGHTNESS:
201  wb->set_brightness(!b);
202  break;
203  }
204 }
205 
206 
207 void win_select::set_default()
208 {
209  if(list_wb_.size() == 0) return;
210 
211  if(cur_select_ != list_wb_.end())
212  {
213  rules(false,*cur_select_);
214  }
215 
216  cur_select_ = list_wb_.begin();
217 
218  while(cur_select_ != list_wb_.end() && !(*cur_select_)->is_can_be_selected()) cur_select_++;
219 
220  if(cur_select_ != list_wb_.end()) rules(true,*cur_select_);
221 
222  finish_scroll_ =false;
223 }
224 
225 void win_select::set_default_object(const win_base * wb)
226 {
227  if(list_wb_.size() == 0) return;
228 
229  if(cur_select_ != list_wb_.end()) rules(false,*cur_select_);
230 
231  cur_select_ = list_wb_.begin();
232 
233  while(cur_select_ != list_wb_.end() && *cur_select_ != wb) cur_select_++;
234 
235  if(cur_select_ != list_wb_.end()) rules(true,*cur_select_);
236 
237  finish_scroll_ =false;
238  //update_cur_select_position();
239 }
240 
241 
242 void win_select::set_default_position(const u_int16 pos)
243 {
244  if(list_wb_.size() == 0 || pos > list_wb_.size()) return;
245 
246  if(cur_select_ != list_wb_.end()) rules(false,*cur_select_);
247 
248  cur_select_ = list_wb_.begin();
249 
250  u_int16 i = 0;
251 
252  while(cur_select_ != list_wb_.end() && i++ < pos) cur_select_++;
253 
254  if(cur_select_ != list_wb_.end()) rules(true,*cur_select_);
255 
256  finish_scroll_ =false;
257  //update_cur_select_position();
258 }
259 
260 
261 win_base * win_select::get_selected_object()
262 {
263  if(cur_select_ != list_wb_.end() || list_wb_.size() == 0) return *cur_select_;
264  else return NULL;
265 }
266 
267 u_int16 win_select::get_selected_position()
268 {
269  u_int16 pos_=0;
270 
271  lwb::iterator i = list_wb_.begin();
272 
273  if(i==list_wb_.end()) return 0;
274 
275  while( (*i++) != (*cur_select_)) pos_++;
276 
277  return pos_+1;
278 }
279 
280 
281 
282 void win_select::update_cur_select_position()
283 {
284  if(!max_amplitude_) return;
285 
286  while( 1 )
287  {
288  // if((*cur_select_)->height() > height() + (space_with_border_ << 1) ) break;
289  // Workaround if the object is bigger than the select (Alex).
290  if((*cur_select_)->height() + (space_with_border_ << 1) > height()) break;
291  else if((*cur_select_)->y() + (*cur_select_)->pad_y() < space_with_border_ ) up();
292  else if((*cur_select_)->y() + (*cur_select_)->pad_y() + (*cur_select_)->height() > height() - space_with_border_) down();
293  else break;
294  }
295 }
296 
297 
299 {
300  if(win_scroll::update())
301  {
302  if(!finish_scroll_)
303  {
304  if(!max_amplitude_) {finish_scroll_=true;return true;}
305  if((*cur_select_)->height() + (space_with_border_ << 1) > height()) {finish_scroll_ = true; return true;}
306  else if((*cur_select_)->y() + (*cur_select_)->pad_y() < space_with_border_ ) up();
307  else if((*cur_select_)->y() + (*cur_select_)->pad_y() + (*cur_select_)->height() > height() - space_with_border_) down();
308  else finish_scroll_ = true;
309  }
310  return true;
311  }
312  return false;
313 }
314 
315 
316 
317 /*
318 #include <list>
319 #include "types.h"
320 #include "input.h"
321 #include "image.h"
322 #include "win_types.h"
323 #include "win_base.h"
324 #include "win_border.h"
325 #include "win_scrollbar.h"
326 #include "win_theme.h"
327 #include "win_container.h"
328 #include "win_scrolled.h"
329 #include "win_select.h"
330 
331 //STATIC INIT
332 win_select * win_select::curselect_=NULL;
333 bool win_select::activate_keyboard_=true;
334 
335 s_int32 win_select::next_key=WIN_SELECT_DEFAULT_KEY_NEXT;
336 s_int32 win_select::previous_key=WIN_SELECT_DEFAULT_KEY_PREVIOUS;
337 s_int32 win_select::activate_key=WIN_SELECT_DEFAULT_KEY_ACTIVATE;
338 s_int32 win_select::back_key=WIN_SELECT_DEFAULT_KEY_BACK;
339 
340 
341 win_select::win_select(s_int16 tx,s_int16 ty,u_int16 tl,u_int16 th,win_theme * wth):win_scrolled(tx,ty,tl,th,wth)
342 {
343  //father select is the father of this object
344  fatherselect_=NULL;
345  //if there are no select object attached to the static curselet, this object become the curselect
346  if(!curselect_) curselect_=this;
347 
348  index_list=list_obj.begin();
349  //the default mode select for this object is the border
350  mode_selected_=WIN_SELECT_MODE_BORDER;
351  //by default, the scroll bar is not visible
352  visible_scrollbar_=false;
353  //if when you select you change the first to the last
354  select_circle_=false;
355 
356  //type of the select
357  type_selected_=WIN_SELECT_TYPE_NORMAL;
358 
359 }
360 
361 
362 win_select::~win_select()
363 {
364  //WARNING CHECK BACK
365  if(curselect_==this)
366  {
367  back();
368  curselect_=NULL;
369  }
370  //WARNING DESTROY()
371 }
372 
373 
374 void win_select::add(win_base * wb)
375 {
376  //call the add of win_scrolled
377  win_scrolled::add(wb);
378 
379  //prevent the new object is in select
380  wb->set_in_select(true);
381 
382  //set the parameters for this object
383  set_select_object(wb,false);
384 
385  //when you add an object, automatically the index become the first element of the list
386  set_default();
387 
388  //a select can have just one mode to select each object in the list
389  wb->set_select_mode_(mode_selected_);
390 }
391 
392 void win_select::add(win_select * ws)
393 {
394  //add a select object
395  win_select::add((win_base*)ws);
396  //add the object but prevent the new object that this objetc is the father
397  ws->fatherselect_=this;
398 }
399 
400 void win_select::remove(win_base * wb)
401 {
402  //remove an objetc prevent that the object is not in_select
403  win_scrolled::remove(wb);
404  wb->set_in_select(false);
405  wb->set_draw_brightness(false);
406  index_list=list_obj.begin();
407 }
408 
409 void win_select::remove_all()
410 {
411  for(list<win_base *>::iterator i=list_obj.begin();i!=list_obj.end();i++)
412  {
413  (*i)->set_in_select(false);
414  (*i)->wb_father_=NULL;//part of win_container
415  }
416  list_obj.clear();
417  index_list=list_obj.begin();
418 
419  //win scrolled part
420  max_amplitude_=0;
421  cur_amplitude_=0;
422  theme_->scrollbar->update_scroll_bar(this);
423 }
424 
425 void win_select::destroy()
426 {
427  win_scrolled::destroy();
428  index_list=list_obj.begin();
429 }
430 
431 
432 void win_select::on_next()
433 {
434  if(callback_[WIN_SIG_NEXT_KEY]) (callback_[WIN_SIG_NEXT_KEY])();
435 }
436 
437 void win_select::on_previous()
438 {
439  if(callback_[WIN_SIG_PREVIOUS_KEY]) (callback_[WIN_SIG_PREVIOUS_KEY])();
440 }
441 
442 bool win_select::update()
443 {
444  if(win_scrolled::update())
445  {
446  if(focus_ && activate_keyboard_ && activated_)
447  {
448  if(input::has_been_pushed (SDLKey (next_key))) next_();
449  if(input::has_been_pushed (SDLKey (previous_key))) previous_();
450  if(input::has_been_pushed (SDLKey (back_key))) back();
451  if(input::has_been_pushed (SDLKey (activate_key))) activate___();
452  }
453  return true;
454  }
455  return false;
456 }
457 
458 //if b is true, cur object take parameters to an selected object
459 void win_select::set_select_object(win_base * wb,bool b)
460 {
461  switch(mode_selected_)
462  {
463  case WIN_SELECT_MODE_BRIGHTNESS:
464  wb->set_draw_brightness(!b);
465  break;
466  case WIN_SELECT_MODE_BORDER:
467  wb->set_border_visible(b);
468  }
469 
470  switch(type_selected_)
471  {
472  case WIN_SELECT_TYPE_NORMAL:
473  wb->set_visible(true);
474  break;
475  case WIN_SELECT_TYPE_SCROLL:
476  wb->set_visible(b);
477  break;
478  }
479 }
480 
481 
482 //WARNING: lots of confuse in next_ and previous maybe rewrite them
483 void win_select::next_()
484 {
485  //test if next is possible
486  if(index_list==list_obj.end() || !activated_) return;
487 
488  //unselect cur element
489  (*index_list)->on_unselect();
490 
491  set_select_object(*index_list,false);
492 
493  //create a temporary index
494  list<win_base*>::iterator cur_i=index_list;
495 
496  //to next elements
497  cur_i++;
498 
499  //while not a the end, not be selected and different at old object go to the next
500  while(cur_i!=list_obj.end() && !(*cur_i)->is_can_be_selected() && cur_i!=index_list) cur_i++;
501 
502  //if at end of list and select circle is activate
503  if(cur_i==list_obj.end())
504  {
505  //cur is the begin of list
506  if(select_circle_)
507  {
508  cur_i=list_obj.begin();
509  while(cur_i!=list_obj.end() && !(*cur_i)->is_can_be_selected() && cur_i!=index_list) cur_i++;
510  if(cur_i!=list_obj.end()) index_list=cur_i;
511  }
512  }else index_list=cur_i;
513 
514  set_select_object(*index_list,true);
515 
516  (*index_list)->on_select();
517 
518  update_position();
519 
520  on_next();
521 }
522 
523 void win_select::previous_()
524 {
525  if(index_list==list_obj.end() || !activated_) return;
526 
527  (*index_list)->on_unselect();
528 
529  //set to unselect object
530  set_select_object(*index_list,false);
531 
532  list<win_base*>::iterator cur_i=index_list;
533 
534 
535 
536  if(select_circle_)
537  {
538 
539  if(cur_i==list_obj.begin()) cur_i=list_obj.end();
540  cur_i--;
541  }
542  else if(cur_i!=list_obj.begin()) cur_i--;
543 
544 
545 
546 
547 
548 
549 
550  while(cur_i!=list_obj.begin() && !(*cur_i)->is_can_be_selected() && cur_i!=index_list) cur_i--;
551 
552  if(cur_i==list_obj.begin() && !(*cur_i)->is_can_be_selected())
553  {
554  if(select_circle_)
555  {
556 
557  cur_i=list_obj.end();
558  cur_i--;
559 
560  while(cur_i!=list_obj.begin() && !(*cur_i)->is_can_be_selected() && cur_i!=index_list) cur_i--;
561  if((*cur_i)->is_can_be_selected()) index_list=cur_i;
562  }
563  } else index_list=cur_i;
564 
565  (*index_list)->on_select();
566 
567 
568  //set to select object
569  set_select_object(*index_list,true);
570 
571  update_position();
572  on_previous();
573 
574 }
575 
576 
577 void win_select::set_can_be_selected_all(bool b)
578 {
579  for(list<win_base*>::iterator i=list_obj.begin();i!=list_obj.end();i++)
580  (*i)->set_can_be_selected(b);
581 }
582 
583 void win_select::set_default()
584 {
585  if(list_obj.size()==0) return;
586 
587  if(index_list!=list_obj.end()) set_select_object(*index_list,false);
588 
589  index_list=list_obj.begin();
590 
591  while(index_list!=list_obj.end() && !(*index_list)->is_can_be_selected()) index_list++;
592 
593  if(index_list!=list_obj.end()) set_select_object(*index_list,true);
594 }
595 
596 
597 void win_select::update_position()
598 {
599 
600  //this function is used to see the cur object which is selected
601  static bool tmp_is_visible_;
602 
603  //if(index_list==list_obj.end()){cout << "tets\n"; return;}
604 
605  switch(type_selected_)
606  {
607  case WIN_SELECT_TYPE_NORMAL:
608  tmp_is_visible_=false;
609  //if not amplitude --> all object in the list is visible
610  if(!max_amplitude_) {tmp_is_visible_=true;return;}
611  while(!tmp_is_visible_)
612  {
613 
614  // if(((*index_list)->y()+(*index_list)->pady()>space_between_border_) &&
615  // ((*index_list)->y()+(*index_list)->height()>height_-space_between_border_)) {tmp_is_visible_=true;cout << "16\n";}
616  if((*index_list)->height()>height_+(space_between_border_<<1)) tmp_is_visible_=true;
617 
618  else if((*index_list)->y()+(*index_list)->pady()<space_between_border_) up();
619  else if((*index_list)->y()+(*index_list)->pady()+(*index_list)->height()>height_-space_between_border_) down();
620  else tmp_is_visible_=true;
621  }
622  break;
623 
624  case WIN_SELECT_TYPE_SCROLL:
625  //set position of the cur select object
626 
627  (*index_list)->move(space_between_border_,space_between_border_);
628 
629  break;
630  default:
631  break;
632  }
633 }
634 
635 
636 void win_select::set_type(u_int8 t)
637 {
638  type_selected_=t;
639  //set all object to the new type set
640  for(list<win_base*>::iterator i=list_obj.begin();i!=list_obj.end();i++)
641  set_select_object(*i,false);
642 
643  //select the first object
644  set_default();
645 }
646 
647 
648 
649 //return the cur object which is selected
650 win_base * win_select::get()
651 {
652  if(index_list==list_obj.end()) return NULL;
653  else return (*index_list);
654 }
655 
656 //if error return 0
657 u_int16 win_select::get_pos()
658 {
659  u_int16 pos_=0;
660  list<win_base *>::iterator i=list_obj.begin();
661  if(i==list_obj.end()) return 0;
662  while((*i++)!=(*index_list)) pos_++;
663  return pos_+1;
664 }
665 
666 void win_select::set_default(u_int16 nb)
667 {
668  if(index_list==list_obj.end()) return;
669  u_int16 j=1;
670  (*index_list)->set_select(false);
671  if(mode_selected_==WIN_SELECT_MODE_BRIGHTNESS)
672  (*index_list)->set_draw_brightness(true);
673  else if(mode_selected_==WIN_SELECT_MODE_BORDER)
674  (*index_list)->set_border_visible(false);
675 
676  index_list=list_obj.begin();
677  while(index_list!=list_obj.end() && j++<nb) index_list++;
678  if(index_list!=list_obj.end())
679  {
680  (*index_list)->set_select(true);
681  if(mode_selected_==WIN_SELECT_MODE_BRIGHTNESS)
682  (*index_list)->set_draw_brightness(false);
683  else if(mode_selected_==WIN_SELECT_MODE_BORDER)
684  (*index_list)->set_border_visible(true);
685  }
686 }
687 
688 void win_select::set_default(win_base * wb)
689 {
690  if(index_list==list_obj.end()) return;
691  (*index_list)->set_select(false);
692 
693  if(mode_selected_==WIN_SELECT_MODE_BRIGHTNESS)
694  (*index_list)->set_draw_brightness(true);
695  else if(mode_selected_==WIN_SELECT_MODE_BORDER)
696  (*index_list)->set_border_visible(false);
697 
698  index_list=list_obj.begin();
699  while(index_list != list_obj.end() && (*index_list)!=wb) index_list++;
700  if(index_list!=list_obj.end())
701  {
702  (*index_list)->set_select(true);
703  if(mode_selected_==WIN_SELECT_MODE_BRIGHTNESS)
704  (*index_list)->set_draw_brightness(false);
705  else if(mode_selected_==WIN_SELECT_MODE_BORDER)
706  (*index_list)->set_border_visible(true);
707  }
708 }
709 
710 void win_select::set_select_mode(u_int8 mode)
711 {
712  mode_selected_=mode;
713  if(mode_selected_==WIN_SELECT_MODE_BRIGHTNESS)
714  for(list<win_base*>::iterator i=list_obj.begin();i!=list_obj.end();i++)
715  (*i)->set_draw_brightness(true);
716  if(index_list!=list_obj.end()) (*index_list)->set_draw_brightness(false);
717  for(list<win_base*>::iterator i=list_obj.begin();i!=list_obj.end();i++)
718  (*i)->set_select_mode_(mode);
719 }
720 
721 void win_select::activate()
722 {
723  if(curselect_) curselect_->activate___();
724 }
725 
726 void win_select::next()
727 {
728  if(curselect_) curselect_->next_();
729 }
730 
731 void win_select::previous()
732 {
733  if(curselect_) curselect_->previous_();
734 }
735 
736 bool win_select::activate___()
737 {
738  if(index_list!=list_obj.end())
739  {
740  (*index_list)->set_activated(true);
741  on_activate_key();
742  return true;
743  }
744  return false;
745 }
746 
747 void win_select::on_activate()
748 {
749  win_scrolled::on_activate();
750  if(!curselect_) {curselect_=this;return;}
751  if(curselect_==this) return;
752  //if curselect
753  curselect_->on_unactivate();
754  curselect_=this;
755 }
756 
757 bool win_select::back()
758 {
759  //if true we can go to the father
760  if(curselect_)
761  {
762  if(curselect_->fatherselect_)
763  {
764  if(curselect_->index_list!=curselect_->list_obj.end())
765  {
766  curselect_->on_unactivate();
767  curselect_=curselect_->fatherselect_;
768  curselect_->on_activate();
769  return true;
770  }
771  }
772  }
773  return false;
774 }
775 
776 void win_select::set_cur_select(win_select * ws)
777 {
778  if(curselect_ && curselect_!=ws) curselect_->set_activated(false);
779  curselect_=ws;
780 }
781 
782 void win_select::init()
783 {
784  if(curselect_) curselect_->set_activated(false);
785  curselect_=NULL;
786 }
787 
788 
789 
790 
791 
792 
793 
794 
795 
796 
797 
798 */
799 
800 
801 
802 
803 
804 
805 
u_int16 height() const
Returns the height of the drawing_area.
Definition: drawing_area.h:97
#define u_int16
16 bits long unsigned integer
Definition: types.h:32
bool input_update()
Input Update process .
Definition: win_scroll.cc:212
bool update()
Update process.
Definition: win_scroll.cc:186
static bool has_been_pushed(SDLKey key)
Returns whether a key has been pushed since last function call, false otherwise.
Definition: input.cc:76
bool input_update()
Input Update process .
Definition: win_select.cc:173
virtual void set_brightness(const bool b)
Set the transluency parameter.
Definition: win_base.h:194
bool is_can_be_selected() const
Test if win_* can be selected.
Definition: win_base.h:214
Common properties for each win_base&#39;s object.
Definition: win_base.h:47
bool update()
Update process.
Definition: win_select.cc:298