@@ -5,15 +5,19 @@ import Components from '../../ui/diagram/components';
55import DrawingUtils from '../../ui/utils/DrawingUtils.js' ;
66import { snapToGrid } from '../../ui/diagram/Utils.js' ;
77import { hoverFor } from '../../ui/diagram/boundingBox' ;
8+ import { CURRENT } from '../../ui/diagram/Constants' ;
89
910import {
1011 ADDING_MOVED ,
1112 MOVING_MOVED ,
1213 DELETE_COMPONENT ,
1314 CHANGE_COMPONENT_VALUE ,
14- SET_HOVERED_COMPONENT
15+ SET_HOVERED_COMPONENT ,
16+ UPDATE_CURRENT_OFFSETS
1517} from '../actions.js' ;
1618
19+ const STANDING_OFFSET = CURRENT . DOT_DISTANCE / 2 ;
20+ 1721const { diff } = DrawingUtils ;
1822
1923const moreThanOne = R . pipe (
@@ -71,6 +75,18 @@ function moveWholeComponent(views, action) {
7175 } ;
7276}
7377
78+ /*
79+ * views = {
80+ * typeID - type of view e.g. Resistor
81+ * id - UID
82+ * value - e.g. 5Ω (TODO extend to support multi-value components)
83+ * dragPoints - real coordinates of the two drag points
84+ * connectors - coordinates of the connectors in the transformed canvas (used for rendering)
85+ * realConnectors - coordinates of the connectors in the real canvas
86+ *
87+ * currentOffsets - keeps track of current flow
88+ * }
89+ */
7490export default function viewsReducer ( views = { } , action ) {
7591 switch ( action . type ) {
7692 case ADDING_MOVED : {
@@ -96,11 +112,10 @@ export default function viewsReducer(views = {}, action) {
96112 typeID,
97113 id,
98114 value : Component . defaultValue ,
99- dragPoints, // real coordinates of the drag points
100- 101- // TODO name these better
102- connectors, // coordinates of the connectors in the transformed canvas - used for rendering
103- realConnectors
115+ dragPoints,
116+ connectors,
117+ realConnectors,
118+ currentOffsets : R . repeat ( STANDING_OFFSET , Component . numOfCurrentPaths )
104119 }
105120 } ;
106121 }
@@ -174,6 +189,39 @@ export default function viewsReducer(views = {}, action) {
174189 ) , views ) ;
175190 }
176191
192+ case UPDATE_CURRENT_OFFSETS : {
193+ const {
194+ delta, // milliseconds
195+ currentSpeed,
196+ componentStates
197+ } = action ;
198+ 199+ // Shamelessly stolen from Paul Falstad. I really wish I knew where these numbers came from.
200+ const currentMultiplier = 1.7 * delta * Math . exp ( currentSpeed / 3.5 - 14.2 ) ;
201+ 202+ const updateOffsets = view => {
203+ const toOffset = ( [ current , prevOffset ] ) => {
204+ const extraOffset = ( current * currentMultiplier ) % ( CURRENT . DOT_DISTANCE / 2 ) ;
205+ let offset = ( prevOffset + extraOffset ) % CURRENT . DOT_DISTANCE ;
206+ if ( offset < 0 ) {
207+ offset += CURRENT . DOT_DISTANCE ;
208+ }
209+ return offset ;
210+ } ;
211+ 212+ const Type = Components [ view . typeID ] ;
213+ const componentState = componentStates [ view . id ] ;
214+ const currents = Type . getCurrents ( view , componentState ) ;
215+ const currentsAndOffsets = R . zip ( currents , view . currentOffsets ) ;
216+ 217+ return {
218+ ...view ,
219+ currentOffsets : R . map ( toOffset , currentsAndOffsets )
220+ } ;
221+ } ;
222+ return R . map ( updateOffsets , views ) ;
223+ }
224+ 177225 default : return views ;
178226 }
179227}
0 commit comments