rules preset in separate file

This commit is contained in:
Ali Gator 2022-12-02 15:52:43 +01:00
parent aa66c523a3
commit 239be6204e
2 changed files with 164 additions and 162 deletions

View File

@ -76,7 +76,9 @@
</template> </template>
<script> <script>
import { mapGetters } from "vuex"; import { mapActions, mapState, mapWritableState } from 'pinia'
import { presetRules, initialStates } from "./preset.js"
import { globalStore } from "../stores/index.js";
import MenuRow from "./MenuRow.vue"; import MenuRow from "./MenuRow.vue";
export default { export default {
name: "MenuElementaryCA", name: "MenuElementaryCA",
@ -84,107 +86,22 @@ export default {
MenuRow, MenuRow,
}, },
data() { data() {
// TODO: Why not a getter in the store?
return { return {
presetRules: [ presetRules: presetRules,
{ initialStates: initialStates,
name: "rule 73",
rules: {
100: 0,
101: 0,
110: 1,
111: 0,
"011": 1,
"010": 0,
"001": 0,
"000": 1,
},
},
{
name: "rule 86",
rules: {
100: 1,
101: 0,
110: 0,
111: 1,
"011": 0,
"010": 1,
"001": 0,
"000": 1,
},
},
{
name: "rule 90",
rules: {
100: 1,
101: 0,
110: 1,
111: 0,
"011": 0,
"010": 0,
"001": 1,
"000": 0,
},
},
{
name: "rule 45?",
rules: {
100: 0,
101: 0,
110: 1,
111: 0,
"011": 1,
"010": 0,
"001": 1,
"000": 1,
},
},
{
name: "rule 54?",
rules: {
100: 1,
101: 0,
110: 1,
111: 1,
"011": 0,
"010": 1,
"001": 1,
"000": 0,
},
},
{
name: "unknown rule",
rules: {
100: 0,
101: 0,
110: 0,
111: 1,
"011": 0,
"010": 0,
"001": 1,
"000": 1,
},
},
],
initialStates: [
{
id: "onecell",
name: "One cell at center",
description: "State with a single cell in the middle",
},
{
id: "random",
name: "Random cell",
description: "State populated with random cells",
},
],
}; };
}, },
computed: { computed: {
...mapGetters({ ...mapWritableState(
initialState: "getInitial1dState", globalStore, {
rules: "get1dRules", initialState: "initial1dState",
}), }
),
...mapState(
globalStore, {
rules: "rules1d"
}
),
rules1dFileName() { rules1dFileName() {
return ( return (
Object.keys(this.rules) Object.keys(this.rules)
@ -196,6 +113,7 @@ export default {
}, },
}, },
methods: { methods: {
...mapActions(globalStore, ["draw1d", "reset"]),
copyRules() { copyRules() {
const rules = JSON.stringify(this.rules); const rules = JSON.stringify(this.rules);
navigator.clipboard.writeText(rules); navigator.clipboard.writeText(rules);
@ -207,30 +125,19 @@ export default {
updateSingleRule(event) { updateSingleRule(event) {
const elem = event.target; const elem = event.target;
const value = elem.checked ? 1 : 0; const value = elem.checked ? 1 : 0;
const data = { rule: elem.name, value: value }; this.rules.rules[elem.name] = value;
this.$store.commit("update1dSingleRule", data);
}, },
updateRules(event) { updateRules(event) {
// TODO : change this, awfully confusing
const elem = event.target; const elem = event.target;
const name = elem.value; const name = elem.value;
const rules = this.presetRules.find((ruleset) => { const newRuleset = this.presetRules.find((ruleset) => {
return ruleset.name === name; return ruleset.name === name;
}); });
Object.keys(rules.rules).map((value) => { this.rules.rules = newRuleset.rules
const data = { name: name, rule: value, value: rules.rules[value] };
this.$store.commit("update1dSingleRule", data);
});
}, },
updateInitialState(event) { updateInitialState(event) {
const elem = event.target; const elem = event.target;
this.$store.commit("setInitial1dState", elem.value); this.initial1dState = elem.value
},
draw1d() {
this.$store.dispatch("draw1d");
},
reset() {
this.$store.dispatch("reset");
}, },
}, },
}; };

95
src/components/preset.js Normal file
View File

@ -0,0 +1,95 @@
const presetRules = [
{
name: "rule 73",
rules: {
100: 0,
101: 0,
110: 1,
111: 0,
"011": 1,
"010": 0,
"001": 0,
"000": 1,
},
},
{
name: "rule 86",
rules: {
100: 1,
101: 0,
110: 0,
111: 1,
"011": 0,
"010": 1,
"001": 0,
"000": 1,
},
},
{
name: "rule 90",
rules: {
100: 1,
101: 0,
110: 1,
111: 0,
"011": 0,
"010": 0,
"001": 1,
"000": 0,
},
},
{
name: "rule 45?",
rules: {
100: 0,
101: 0,
110: 1,
111: 0,
"011": 1,
"010": 0,
"001": 1,
"000": 1,
},
},
{
name: "rule 54?",
rules: {
100: 1,
101: 0,
110: 1,
111: 1,
"011": 0,
"010": 1,
"001": 1,
"000": 0,
},
},
{
name: "unknown rule",
rules: {
100: 0,
101: 0,
110: 0,
111: 1,
"011": 0,
"010": 0,
"001": 1,
"000": 1,
},
},
]
const initialStates = [
{
id: "onecell",
name: "One cell at center",
description: "State with a single cell in the middle",
},
{
id: "random",
name: "Random cell",
description: "State populated with random cells",
},
]
export { presetRules, initialStates }