~seh999/jcog/proto3

« back to all changes in this revision

Viewing changes to spacetime/src.tool/opencog/spacetime/tools/AnimatorTool.java

  • Committer: SeH
  • Date: 2009-09-19 22:59:48 UTC
  • Revision ID: seh999@gmail.com-20090919225948-q3ab80xa57i74mm6
start of major jReality refactoring

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *
3
 
 * This file is part of jReality. jReality is open source software, made
4
 
 * available under a BSD license:
5
 
 *
6
 
 * Copyright (c) 2003-2006, jReality Group: Charles Gunn, Tim Hoffmann, Markus
7
 
 * Schmies, Steffen Weissmann.
8
 
 *
9
 
 * All rights reserved.
10
 
 *
11
 
 * Redistribution and use in source and binary forms, with or without
12
 
 * modification, are permitted provided that the following conditions are met:
13
 
 *
14
 
 * - Redistributions of source code must retain the above copyright notice, this
15
 
 *   list of conditions and the following disclaimer.
16
 
 *
17
 
 * - Redistributions in binary form must reproduce the above copyright notice,
18
 
 *   this list of conditions and the following disclaimer in the documentation
19
 
 *   and/or other materials provided with the distribution.
20
 
 *
21
 
 * - Neither the name of jReality nor the names of its contributors nor the
22
 
 *   names of their associated organizations may be used to endorse or promote
23
 
 *   products derived from this software without specific prior written
24
 
 *   permission.
25
 
 *
26
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30
 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 
 * POSSIBILITY OF SUCH DAMAGE.
37
 
 *
38
 
 */
39
 
 
40
 
 
41
 
package opencog.spacetime.tools;
42
 
 
43
 
import java.util.HashMap;
44
 
import java.util.IdentityHashMap;
45
 
import java.util.Iterator;
46
 
 
47
 
import opencog.spacetime.scene.tool.AbstractTool;
48
 
import opencog.spacetime.scene.tool.InputSlot;
49
 
import opencog.spacetime.scene.tool.ToolContext;
50
 
import opencog.spacetime.toolsystem.ToolSystem;
51
 
 
52
 
 
53
 
 
54
 
/**
55
 
 *
56
 
 * TODO: comment this
57
 
 *
58
 
 * @author weissman
59
 
 *
60
 
 */
61
 
public class AnimatorTool extends AbstractTool {
62
 
 
63
 
        private static InputSlot timer = InputSlot.getDevice("SystemTime");
64
 
 
65
 
        private static HashMap<Object, AnimatorTool> instances=new HashMap<Object, AnimatorTool>();
66
 
 
67
 
        public static AnimatorTool getInstance(ToolContext context) {
68
 
                return getInstanceImpl((Object) context.getKey());
69
 
        }
70
 
 
71
 
        /**
72
 
         * WARNING: do not use this unless you write a tool system!!
73
 
         */
74
 
        public static AnimatorTool getInstanceImpl(Object key) {
75
 
                //if (!thread.getName().equals("jReality ToolSystem EventQueue"))
76
 
                //    throw new RuntimeException("no tool system event thread!");
77
 
                AnimatorTool instance = (AnimatorTool) instances.get(key);
78
 
                if (instance == null) {
79
 
                        instance = new AnimatorTool();
80
 
                        instances.put(key, instance);
81
 
                }
82
 
                return instance;
83
 
        }
84
 
 
85
 
        /**
86
 
         * WARNING: do not use this unless you write a tool system!!
87
 
         */
88
 
        public static void disposeInstance(Object key) {
89
 
                AnimatorTool at = instances.remove(key);
90
 
                at.dispose();
91
 
        }
92
 
 
93
 
        private TimerQueue timerQueue;
94
 
 
95
 
        private IdentityHashMap<Object, AnimatorTask> animators = new IdentityHashMap<Object, AnimatorTask>();
96
 
        private final Object mutex = new Object();
97
 
 
98
 
        private double totalTime;
99
 
 
100
 
        private AnimatorTool() {
101
 
                addCurrentSlot(timer, "Triggers the animator tasks.");
102
 
                timerQueue = new TimerQueue(this);
103
 
        }
104
 
 
105
 
        public void perform(ToolContext tc) {
106
 
                synchronized (mutex) {
107
 
                        int dt = tc.getAxisState(timer).intValue();
108
 
                        totalTime+=dt;
109
 
                        for (Iterator<AnimatorTask> i = animators.values().iterator(); i.hasNext(); ) {
110
 
                                AnimatorTask task = i.next();
111
 
                                if (!task.run(totalTime, dt)) {
112
 
                                        i.remove();
113
 
                                }
114
 
                        }
115
 
                }
116
 
        }
117
 
 
118
 
        public void schedule(Object key, AnimatorTask task) {
119
 
                synchronized (mutex) {
120
 
                        animators.put(key, task);
121
 
                }
122
 
        }
123
 
 
124
 
        public void deschedule(Object key) {
125
 
                synchronized (mutex) {
126
 
                        animators.remove(key);
127
 
                }
128
 
        }
129
 
 
130
 
        public TimerQueue getTimerQueue() {
131
 
                return timerQueue;
132
 
        }
133
 
 
134
 
        public void dispose() {
135
 
                synchronized (mutex) {
136
 
                        animators.clear();
137
 
                }
138
 
        }
139
 
 
140
 
        public static AnimatorTool getInstance(ToolSystem ts) {
141
 
                return getInstanceImpl(ts.getKey());
142
 
        }
143
 
 
144
 
}