source: golgotha/src/golg/editor/dialogs/e_time.cc @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 15.4 KB
RevLine 
[80]1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "window/window.hh"
10#include "window/style.hh"
11#include "gui/button.hh"
12#include "editor/dialogs/e_time.hh"
13#include "window/win_evt.hh"
14#include "gui/image_win.hh"
15#include "gui/button.hh"
16#include "device/keys.hh"
17#include "gui/text_input.hh"
18#include "gui/deco_win.hh"
19#include "gui/text.hh"
20#include "math/num_type.hh"
21#include "editor/e_state.hh"
22#include "math/spline.hh"
23#include "m_flow.hh"
24#include "editor/editor.hh"
25#include "editor/e_state.hh"
26#include "loaders/load.hh"
27#include "gui/butbox.hh"
28#include "g1_speed.hh"
29#include "mess_id.hh"
30#include "app/app.hh"
31#include "editor/e_res.hh"
32
33class g1_time_show : public i4_window_class
34{
35  i4_graphical_style_class *style;
36  i4_event_handler_reference_class<g1_time_line> tl;
37public:
38  g1_time_show(i4_graphical_style_class *style,
39               g1_time_line *tl)
40    : i4_window_class(style->font_hint->normal_font->largest_width()*6,
41                      style->font_hint->normal_font->largest_height()),
42      style(style)
43  {
44    tl=tl;
45  }
46
47  void draw(i4_draw_context_class &context)
48  {
49    style->deco_neutral_fill(local_image, 0,0, width()-1, height()-1, context);
50
51    if (!tl.get()) return;
52
53    i4_font_class *f=style->font_hint->normal_font;
54    f->set_color(style->color_hint->text_foreground);
55
56    i4_float t=tl->current_frame()/(i4_float)(G1_MOVIE_HZ);
57    sw32 sec=(sw32)t;
58    sw32 msec=(sw32)((t-sec)*100);
59
60    i4_str *out=g1_editor_instance.get_editor_string("time_format").sprintf(20, sec, msec);
61    f->put_string(local_image, 0,0, *out, context);
62    delete out;
63  }
64
65  char *name() { return "time show"; }
66};
67
68class g1_frame_show : public i4_text_input_class
69{
70  i4_event_handler_reference_class<g1_time_line> tl;
71public:
72  g1_frame_show(i4_graphical_style_class *style,
73               g1_time_line *tl)
74    : i4_text_input_class(style,
75                          g1_editor_instance.get_editor_string("0"),
76                          50,
77                          8)
78  {
79    tl=tl;
80  }
81
82  virtual void become_unactive()
83  {
84    i4_const_str::iterator i=st->begin();
85    w32 v=i.read_number();   
86    i4_text_input_class::become_unactive();
87  }
88
89  void receive_event(i4_event *ev)
90  {
91    if (!tl.get()) return;
92
93    CAST_PTR(kev, i4_key_press_event_class, ev);
94    if (ev->type()==i4_event::KEY_PRESS && kev->key==I4_ENTER)
95    {
96      become_unactive();
97
98      i4_const_str::iterator i=st->begin();
99      sw32 x=i.read_number();
100
101      if (tl->selected_spline() && x>0)
102      {
103        g1_editor_instance.add_undo(G1_MAP_MOVIE);
104
105        i4_spline_class::point *p;
106        w32 frame=tl->current_frame();
107        p=tl->selected_spline()->get_control_point_previous_to_frame(frame);
108
109        sw32 add=x-(p->next->frame-p->frame);
110        p=p->next;
111
112
113        for (;p;p=p->next)
114          p->frame+=add;
115
116        g1_editor_instance.changed();
117
118       
119      }
120
121
122    }
123    else i4_text_input_class::receive_event(ev);
124  }
125   
126  void update()
127  {
128    i4_str *st;
129    if (!tl.get()) return;
130    if (tl->selected_spline())
131    {
132      i4_spline_class::point *p;
133      w32 frame=tl->current_frame();
134      p=tl->selected_spline()->get_control_point_previous_to_frame(frame);
135
136      if (p && p->next)
137        st=g1_editor_instance.get_editor_string("frame_format").sprintf(20,
138                                                                        p->next->frame-p->frame);
139      else
140        st=new i4_str(g1_editor_instance.get_editor_string("no_next"));
141    }
142    else
143      st=new i4_str(g1_editor_instance.get_editor_string("not_sel"));
144
145    change_text(*st);
146    delete st;
147  }
148   
149  char *name() { return "time show"; }
150};
151
152class g1_time_scroller : public i4_parent_window_class
153{
154  i4_event_handler_reference_class<g1_time_line> tl;
155  i4_graphical_style_class *style;
156  sw32 mx,my;
157  i4_bool mouse_down;
158
159public:
160  char *name() { return "time sroller"; }
161
162  g1_time_scroller(w16 w, w16 h,
163                   i4_graphical_style_class *style,
164                   g1_time_line *tl)
165    : i4_parent_window_class(w,h),
166      style(style)
167  {
168    tl=tl;
169    mouse_down=i4_F;
170  }
171   
172  void parent_draw(i4_draw_context_class &context)
173  {
174    style->draw_in_deco(local_image, 0,0, width()-1, height()-1, i4_F, context);
175
176    w32 l,r,t,b;
177    style->get_in_deco_size(l,r,t,b);
178    local_image->bar(l,r,width()-r,height()-t, style->color_hint->window.active.medium, context);
179
180    local_image->line(l,height()/2,width()-r, height()/2, 0xffffffff, context);
181   
182    if (!tl.get()) return;
183    int tf=tl->total_frames();
184    if (tf)
185    {
186      w32 o=tl->current_frame()*(width()-l-r)/tf+l;
187      local_image->line(o,t,o,height()-b, 0xffffffff, context);
188    }
189  }
190
191
192  void mouse_change_time()
193  {
194    if (!tl.get()) return;
195
196    w32 l,r,t,b;
197    style->get_in_deco_size(l,r,t,b);
198    if (mx>=width()-r)
199      tl->set_current(tl->total_frames()-1, i4_T);
200    else if (mx<l)
201      tl->set_current(0, i4_T);
202    else
203      tl->set_current( (mx-l)*tl->total_frames()/(width()-l-r), i4_T);
204    request_redraw();
205
206  }
207
208  void g1_time_scroller::receive_event(i4_event *ev)
209  {
210   
211    switch (ev->type())
212    {
213      case i4_event::MOUSE_MOVE :
214      {       
215        CAST_PTR(mev, i4_mouse_move_event_class, ev);
216        mx=mev->x;
217        my=mev->y;
218        if (mouse_down)
219          mouse_change_time();
220      } break;
221
222      case i4_event::MOUSE_BUTTON_DOWN :
223      {
224        CAST_PTR(mev, i4_mouse_button_down_event_class, ev);
225        if (mev->but==i4_mouse_button_down_event_class::LEFT)
226        {
227          i4_window_request_mouse_grab_class grab(this);
228          i4_kernel.send_event(parent,&grab);
229          mouse_down=i4_T;
230          mouse_change_time();
231        }
232      } break;
233
234      case i4_event::MOUSE_BUTTON_UP :
235      {
236        CAST_PTR(mev, i4_mouse_button_up_event_class, ev);
237        if (mev->but==i4_mouse_button_up_event_class::LEFT && mouse_down)
238        {
239          mouse_down=i4_F;
240          i4_window_request_mouse_ungrab_class grab(this);
241          i4_kernel.send_event(parent,&grab);
242        }
243      } break;
244    }
245  }
246   
247};
248
249
250
251i4_button_class *g1_time_line::create_img_win(char *icon_res_name,
252                                              w32 mess_id,
253                                              i4_graphical_style_class *style)
254
255{
256  i4_image_class *im=i4_load_image(g1_editor_instance.get_editor_string(icon_res_name));
257  I4_ASSERT(im,"icon missing");
258 
259  char help[30];
260  sprintf(help,"%s_help",icon_res_name);
261
262  i4_object_message_event_class *omes=new i4_object_message_event_class(this, mess_id);
263  i4_event_reaction_class *press=new i4_event_reaction_class(this, omes);
264  i4_button_class *b=new i4_button_class(&g1_ges(help),
265                                         new i4_image_window_class(im, i4_T),
266                                         style,
267                                         press);
268  return b;
269}
270
271void g1_time_line::create_time_win(i4_graphical_style_class *style)
272{
273  frame_show=new g1_frame_show(style, this);
274  frame_show->update();
275
276  i4_const_str next_str=g1_editor_instance.get_editor_string("next");
277  i4_window_class *frame_text=new i4_text_window_class(next_str, style);
278
279  sec_win=new g1_time_show(style, this);
280
281  w32 w=frame_text->width()+10+frame_show->width();
282  w32 h=frame_show->height() + 2 + sec_win->height();
283
284  i4_deco_window_class *d=new i4_deco_window_class(w,h, i4_F, style); 
285  time_win=d;
286
287  w32 x1=d->get_x1(), y1=d->get_y1();
288
289  time_win->add_child(x1,y1+2, frame_text);
290  time_win->add_child(x1+frame_text->width()+5, y1, frame_show.get());
291  time_win->add_child(x1,y1+frame_show->height()+1, sec_win.get());
292}
293
294g1_time_line::~g1_time_line()
295{
296  if (g1_frame_change_notify)
297  {
298    delete g1_frame_change_notify;
299    g1_frame_change_notify=0;
300  }
301 
302  if (g1_scene_change_notify)
303  {
304    delete g1_scene_change_notify;
305    g1_scene_change_notify=0;
306  }
307
308  if (g1_movie_stop_notify)
309  {
310    delete g1_movie_stop_notify;
311    g1_movie_stop_notify=0;
312  }
313
314  if (scroller.get())
315      delete scroller.get();
316     
317  if (time_win.get())
318    delete time_win.get();
319
320  if (bbox.get())
321    delete bbox.get();
322
323  if (last_scene.get())
324    delete last_scene.get();
325
326  if (next_scene.get())
327    delete next_scene.get();
328
329  if (scene_number_input.get())
330    delete scene_number_input.get();
331}
332
333g1_time_line::g1_time_line(i4_parent_window_class *parent,
334                           i4_graphical_style_class *style,
335                           g1_edit_state_class *state)
336  : state(state)
337{
338  i4_object_message_event_class *o;
339  o=new i4_object_message_event_class(this,FRAME_CHANGED);
340  g1_frame_change_notify=new i4_event_reaction_class(this, o);
341                                                         
342  o=new i4_object_message_event_class(this,SCENE_CHANGED);                           
343  g1_scene_change_notify=new i4_event_reaction_class(this, o);
344
345  o=new i4_object_message_event_class(this,MOVIE_STOPPED);
346  g1_movie_stop_notify=new i4_event_reaction_class(this, o);
347
348  bbox=new i4_button_box_class(this, i4_F);
349 
350
351  i4_button_class *left, *right, *rewind, *fforward;
352  left=create_img_win("e_left", LAST_TIME, style); 
353  left->set_repeat_down(i4_T);
354  left->set_popup(i4_T);
355
356  right=create_img_win("e_right", NEXT_TIME, style); 
357  right->set_repeat_down(i4_T);
358  right->set_popup(i4_T);
359
360  play=create_img_win("e_play", PLAY, style);
361  i4_user_message_event_class *stop=new i4_user_message_event_class(G1_STOP_MOVIE);
362  play->send.depress=new i4_event_reaction_class(i4_current_app, stop);
363                                                 
364
365  rewind=create_img_win("e_rewind", REWIND, style);
366  fforward=create_img_win("e_fforward", FFORWARD, style);
367
368  bbox->add_button(0,0,rewind);
369  bbox->add_button(0,0,left);
370  bbox->add_button(0,0,play.get());
371  bbox->add_button(0,0,right);
372  bbox->add_button(0,0,fforward);
373
374  bbox->arrange_down_right();
375
376  h=bbox->height();
377 
378
379  create_time_win(style);
380  if (time_win->height()>h)
381    h=time_win->height();
382
383  last_scene=create_img_win("e_left", LAST_SCENE, style); 
384  last_scene->set_repeat_down(i4_T);
385  last_scene->set_popup(i4_T);
386
387  next_scene=create_img_win("e_right", NEXT_SCENE, style); 
388  next_scene->set_repeat_down(i4_T);
389  next_scene->set_popup(i4_T);
390 
391  i4_const_str fmt=g1_editor_instance.get_editor_string("frame_format");
392  w32 scene_d=current_movie() ? current_movie()->get_scene()+1 : 1;
393  i4_str *scene_number=fmt.sprintf(10, scene_d);
394  scene_number_input=new i4_text_input_class(style,*scene_number, 40, 8, this);
395  delete scene_number;
396
397  sw32 xon=0;
398  parent->add_child(xon, parent->height()-last_scene->height()-1, last_scene.get());
399  xon+=last_scene->width();
400
401  parent->add_child(xon, parent->height()-scene_number_input->height()-1,
402                    scene_number_input.get());
403  xon+=scene_number_input->width();
404
405  parent->add_child(xon, parent->height()-next_scene->height()-1, next_scene.get());
406  xon+=next_scene->width();
407
408
409  scroller=new g1_time_scroller(parent->width() - xon -
410                                bbox->width() -
411                                time_win->width(),
412                                h, style, this);
413 
414
415  parent->add_child(xon, parent->height()-scroller->height()-1, scroller.get());
416  xon+=scroller->width();
417
418  parent->add_child(xon, parent->height()-bbox->height()-1, bbox.get());
419  xon+=bbox->width();
420
421  parent->add_child(xon, parent->height()-time_win->height()-1, time_win.get());
422}
423
424
425void g1_time_line::update()
426{
427  scroller->request_redraw(i4_F);
428  frame_show->update();
429  sec_win->request_redraw(i4_F);
430}
431
432i4_spline_class *g1_time_line::selected_spline()
433{
434  return 0;
435}
436
437
438g1_movie_flow_class *g1_time_line::current_movie()
439{
440  g1_get_current_movie_event gm;
441  i4_kernel.send_event(i4_current_app, &gm);
442  return gm.mflow;
443}
444
445void g1_time_line::set_current(w32 frame, i4_bool stop_play)
446
447  g1_movie_flow_class *m=current_movie();
448  if (m)
449  {
450    g1_editor_instance.add_undo(G1_MAP_MOVIE);
451
452    m->set_frame(frame);
453
454    i4_user_message_event_class c(G1_MAP_CHANGED);
455    i4_kernel.send_event(i4_current_app, &c);
456    update();   
457   
458    if (stop_play)
459      play->do_depress();
460  }
461}
462
463
464w32 g1_time_line::current_frame()
465{
466  g1_movie_flow_class *m=current_movie();
467  if (m)
468    return m->get_frame();
469  else
470    return 0;
471}
472
473w32 g1_time_line::total_frames()
474{
475  g1_movie_flow_class *m=current_movie();
476  if (m && m->current())
477    return m->current()->total_frames();
478  else return 0;
479}
480
481void g1_time_line::update_scene()
482{
483  g1_movie_flow_class *m=current_movie();
484
485  i4_const_str fmt=g1_editor_instance.get_editor_string("frame_format");
486  i4_str *scene_number=fmt.sprintf(10, m->get_scene()+1);
487  scene_number_input->change_text(*scene_number);
488  delete scene_number;
489}
490
491void g1_time_line::set_current_scene(sw32 scene)
492{
493  g1_editor_instance.add_undo(G1_MAP_MOVIE);
494
495  g1_movie_flow_class *m=current_movie();
496  m->set_scene(scene);
497  m->set_frame(0);
498
499  update_scene();
500
501  i4_user_message_event_class ch(G1_MAP_CHANGED);
502  i4_kernel.send_event(i4_current_app, &ch);
503}
504
505void g1_time_line::unpress_play()
506{
507  if (play.get())
508    play->do_depress();
509}
510
511void g1_time_line::receive_event(i4_event *ev)
512{
513  g1_movie_flow_class *m=current_movie();
514
515  CAST_PTR(oev, i4_object_message_event_class, ev);
516
517  if (!m || oev->type()!=i4_event::OBJECT_MESSAGE)
518    return ;
519
520  if (oev->object==this)
521  {
522    switch (oev->sub_type)
523    {
524      case LAST_TIME :
525        if (m->get_frame())
526          set_current(m->get_frame()-1, i4_T);
527        break;
528
529      case NEXT_TIME :
530        if (m->current() && m->get_frame()+1<m->current()->total_frames())         
531          set_current(m->get_frame()+1, i4_T);         
532        break;
533
534      case FFORWARD :
535        if (m->current())
536          set_current(m->current()->total_frames()-1, i4_T);
537        break;
538
539      case REWIND :
540        set_current(0, i4_T);
541        break;
542
543      case PLAY :
544      {
545        i4_user_message_event_class m(G1_PLAY_MOVIE);
546        i4_kernel.send_event(i4_current_app, &m);
547      } break;
548
549      case LAST_SCENE :     
550        if (m->current() && m->get_scene())
551         set_current_scene(m->get_scene()-1);
552        break;
553
554      case NEXT_SCENE :
555        if (m->current() && m->get_scene()<m->t_cut_scenes-1)
556         set_current_scene(m->get_scene()+1);
557        break;       
558
559      case SCENE_CHANGED :
560        update_scene();
561        break;
562
563      case FRAME_CHANGED :
564        update();
565        break;
566
567      case MOVIE_STOPPED :
568        unpress_play();
569        break;
570    }
571  } else if (oev->object==scene_number_input.get())
572  {
573    CAST_PTR(tc, i4_text_change_notify_event, ev);
574    i4_const_str::iterator i=tc->new_text->begin();
575    sw32 n=i.read_number()-1;
576    if (n>=0 && n<m->t_cut_scenes)
577      set_current_scene(n);
578  }
579}
Note: See TracBrowser for help on using the repository browser.