-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
288 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { button, compute, horizontal } from "openrct2-flexui"; | ||
import FeatureController from "../controllers/FeatureController"; | ||
import ColourChange from "../themeSettings/ColourChange"; | ||
|
||
const ridePaintSection = (fc: FeatureController) => { | ||
const rc = fc.rideController; | ||
const { rideHistory } = rc; | ||
|
||
const layout = horizontal({ | ||
// height:80, | ||
content: [ | ||
// undo button | ||
button({ | ||
text: "Undo", | ||
padding: "5px", | ||
disabled: compute(rideHistory.undoPointer, (pointer) => pointer <= 0), | ||
onClick: () => rideHistory.undoLastPaint(), | ||
}), | ||
|
||
// ride paint button | ||
button({ | ||
height: 30, | ||
padding: "5px", | ||
// width: "80%", | ||
text: "6. Paint selected rides", | ||
disabled: compute( | ||
fc.rideController.selectedRides, | ||
(rides) => (rides?.length || -1) <= 0 | ||
), | ||
onClick: () => ColourChange.colourRides(fc), | ||
tooltip: `Nothing changing? | ||
Make sure to enable 'Allow repainting of already painted rides'`, | ||
}), | ||
|
||
// paint redo button | ||
button({ | ||
text: "Redo", | ||
padding: "5px", | ||
disabled: compute( | ||
rideHistory.undoPointer, | ||
(pointer) => pointer >= rideHistory.ridePaintHistory.get().length - 1 | ||
), | ||
onClick: () => rideHistory.redoPaint(), | ||
}), | ||
], | ||
}); | ||
|
||
return layout; | ||
}; | ||
|
||
export default ridePaintSection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { ArrayStore, Store, arrayStore, store } from "openrct2-flexui"; | ||
import RideController from "../controllers/RideController"; | ||
import { debug } from "./logger"; | ||
import { convertRideToProxy, RideProxy } from "./RideProxy"; | ||
import ColourChange from "../themeSettings/ColourChange"; | ||
|
||
class RideHistory { | ||
/** | ||
* Version control history. Adds a new entry each time a ride/rides are painted. | ||
*/ | ||
ridePaintHistory: ArrayStore<RideProxy[]>; | ||
|
||
/** | ||
* Version control pointer to track the place in the undo/redo history | ||
*/ | ||
undoPointer: Store<number>; | ||
|
||
rc: RideController; | ||
|
||
constructor(rc: RideController) { | ||
this.ridePaintHistory = arrayStore<RideProxy[]>([]); | ||
this.undoPointer = store<number>(0); | ||
this.rc = rc; | ||
} | ||
|
||
/** | ||
* Add the rides being painted to the paint version control. | ||
*/ | ||
pushRidesToPaintHistory = (rides: Ride[]) => { | ||
// convert to a shallower version of ride for cheaper storage | ||
const ridesProxy = rides.map((ride) => convertRideToProxy(ride)); | ||
const pointer = this.undoPointer; | ||
const startingIndex = pointer.get(); | ||
// used for the splice to know where to start inserting/deleting. | ||
const deletionPoint = this.ridePaintHistory.get().length - pointer.get(); | ||
|
||
// if the user has undone some paints (and therefore is not at the top of the stack), | ||
// those paint histories will be lost | ||
const numPaints = this.ridePaintHistory.splice( | ||
startingIndex, | ||
deletionPoint, | ||
ridesProxy | ||
); | ||
// move the pointer up by 1 to point at the newest paint job | ||
pointer.set(pointer.get() + 1); | ||
|
||
debug( | ||
`\nPushed ${rides.length} rides to paint history in this batch. | ||
${numPaints.length} paint histories were discarded.` | ||
); | ||
}; | ||
|
||
public undoLastPaint = () => { | ||
const pointer = this.undoPointer; | ||
|
||
// if there have been no paints | ||
// or if you've undone all the way back to the beginning | ||
if (pointer.get() <= 0) { | ||
debug(`there are no paints to undo`); | ||
return; | ||
} | ||
|
||
// if it's on the most recent paint, add it to the array right before undoing | ||
debug(`\nUndoing paint.`); | ||
|
||
if (pointer.get() === this.ridePaintHistory.get().length) { | ||
debug(`At stack header, so adding current paint to the stack.`); | ||
this.pushRidesToPaintHistory(this.rc.selectedRides.get()); | ||
// Compensation for pointer being incremented during pushRidesToPaintHistory() | ||
pointer.set(pointer.get() - 1); | ||
} | ||
pointer.set(pointer.get() - 1); | ||
const ridesToUndoPaint = this.ridePaintHistory.get()[pointer.get()]; | ||
ridesToUndoPaint.map((ride) => | ||
ColourChange.setRideColour( | ||
ride as Ride, | ||
...this.getColoursFromProxy(ride) | ||
) | ||
); | ||
// refresh selected rides to update right column | ||
this.rc.selectedRides.set([...this.rc.selectedRides.get()]); | ||
}; | ||
|
||
public redoPaint = () => { | ||
const pointerNumber = this.undoPointer.get(); | ||
// make sure pointer isn't already at the head | ||
if (pointerNumber >= this.ridePaintHistory.get().length) { | ||
debug(`there are no paints to redo`); | ||
return; | ||
} | ||
|
||
this.undoPointer.set(pointerNumber + 1); | ||
// get the rides to redo painting | ||
const ridesToUndoPaint = | ||
this.ridePaintHistory.get()[this.undoPointer.get()]; | ||
debug(`\nRedoing paintjob.`); | ||
debug( | ||
`${ridesToUndoPaint.length} rides which will be brought back to redone state.` | ||
); | ||
|
||
if (!ridesToUndoPaint) { | ||
debug(`no rides to undo`); | ||
return; | ||
} | ||
ridesToUndoPaint.map((ride) => | ||
ColourChange.setRideColour( | ||
ride as Ride, | ||
...this.getColoursFromProxy(ride) | ||
) | ||
); | ||
// refresh selected rides to update right column | ||
this.rc.selectedRides.set([...this.rc.selectedRides.get()]); | ||
}; | ||
|
||
private getColoursFromProxy = (ride: RideProxy) => [ | ||
ride.colourSchemes[0].main, | ||
ride.colourSchemes[0].additional, | ||
ride.colourSchemes[0].supports, | ||
ride.vehicleColours[0].body, | ||
ride.vehicleColours[0].trim, | ||
ride.vehicleColours[0].tertiary, | ||
]; | ||
} | ||
|
||
export default RideHistory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { debug } from "./logger"; | ||
|
||
export type RideProxy = { | ||
id: number; | ||
name: string; | ||
vehicleColours: VehicleColour[]; | ||
colourSchemes: TrackColour[]; | ||
stationStyle: number; | ||
}; | ||
|
||
export const convertRideToProxy = (ride: Ride): RideProxy => { | ||
const { id, name, vehicleColours, colourSchemes, stationStyle } = ride; | ||
return { | ||
id, | ||
name, | ||
vehicleColours: [vehicleColours[0]], | ||
colourSchemes: [colourSchemes[0]], | ||
stationStyle, | ||
}; | ||
}; | ||
|
||
export const hydrateRideProxy = (rideProxy: RideProxy): Ride => { | ||
debug(`prepping to rehydrate ${JSON.stringify(rideProxy)}`); | ||
return map.rides.filter((ride) => ride.id === rideProxy.id)[0]; | ||
}; | ||
|
||
// map all rides into proxies | ||
// save into an array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.