rules can be exported in json, rule presets

This commit is contained in:
Ali Gator 2022-01-12 19:10:16 +01:00
parent 9613f023b1
commit 441238bfbc
3 changed files with 78 additions and 27 deletions

View File

@ -23,7 +23,7 @@ export default {
computed: { computed: {
...mapGetters({ ...mapGetters({
cellProperties: 'getCellProperties', cellProperties: 'getCellProperties',
rules: 'getRuleSet1d', rules: 'get1dRules',
drawing: 'isDrawing', drawing: 'isDrawing',
canvasWidth: 'getCanvasWidth', canvasWidth: 'getCanvasWidth',
canvasHeight: 'getCanvasHeight', canvasHeight: 'getCanvasHeight',

View File

@ -11,7 +11,7 @@
> >
<option <option
v-for="state in initial1dStates" v-for="state in initial1dStates"
:key="state.id" :key="'preset1d' + state.id"
:value="state.id" :value="state.id"
>{{ state.name }} >{{ state.name }}
</option> </option>
@ -21,18 +21,41 @@
<div class="form-field"> <div class="form-field">
<label>Rules</label> <label>Rules</label>
</div> </div>
<div class="form-field">
<label>Rules presets
<br>
<select
name="rules1d"
value="rules"
@input="update1dRules"
>
<option
v-for="(rule, name) in presetRules"
:key="'rule1d' + name"
:value="name"
>{{ name }}
</option>
</select>
</label>
</div>
<div class="form-field">
<a
style="cursor: pointer;"
@click="copy1dRules"
>copy rules</a>
</div>
<div <div
v-for="rule in rules" v-for="(rule, name) in rules"
:key="rule" :key="'rule1d' + name"
class="form-field" class="form-field"
> >
<label>{{ rule }} <label>{{ name }}
<input <input
:value="getRule(rule)" :value="rule"
type="checkbox" type="checkbox"
:name="rule" :name="name"
:checked="getRule(rule) == 1" :checked="rule"
@input="update1dRules" @input="update1dSingleRule"
> >
</label> </label>
</div> </div>
@ -44,12 +67,6 @@
value="start" value="start"
@click="draw1d" @click="draw1d"
> >
<!-- <input -->
<!-- type="button" -->
<!-- name="stop" -->
<!-- class="stop" -->
<!-- value="stop" -->
<!-- > -->
<input <input
type="button" type="button"
name="reset" name="reset"
@ -72,7 +89,13 @@ export default {
data() { data() {
// TODO: Why not a getter in the store? // TODO: Why not a getter in the store?
return { return {
rules: ["111", "110", "101", "100", "011", "010", "001", "000"], presetRules: {
"rule 73" : {"100":0,"101":0,"110":1,"111":0,"011":1,"010":0,"001":0,"000":1},
"rule 86" : {"100":1,"101":0,"110":0,"111":1,"011":0,"010":1,"001":0,"000":1},
"rule 90" : {"100":1,"101":0,"110":1,"111":0,"011":0,"010":0,"001":1,"000":0},
"rule 45?" : {"100":0,"101":0,"110":1,"111":0,"011":1,"010":0,"001":1,"000":1},
"rule 54?" : {"100":1,"101":0,"110":1,"111":1,"011":0,"010":1,"001":1,"000":0}
},
initial1dStates: [ initial1dStates: [
{ id : "onecell", { id : "onecell",
name : "One cell at center", name : "One cell at center",
@ -88,20 +111,41 @@ export default {
}, },
computed: { computed: {
...mapGetters({ ...mapGetters({
initial1dState: 'getInitial1dState' initial1dState: 'getInitial1dState',
}) rules: 'get1dRules'
}),
rules1dFileName() {
return Object.keys(this.rules).map(
(index) => {
return this.rules[index]
}).join("_") + ".json"
},
}, },
methods: { methods: {
copy1dRules(){
const rules = JSON.stringify(this.rules)
navigator.clipboard.writeText(rules)
},
isCurrentPreset(event) { isCurrentPreset(event) {
console.log(elem.value)
const elem = event.target const elem = event.target
return this.initialState === elem.value return this.initialState === elem.value
}, },
update1dRules(event) { update1dSingleRule(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} const data = { 'rule' : elem.name, 'value' : value}
this.$store.commit('update1dRules', data) this.$store.commit('update1dSingleRule', data)
},
update1dRules(event) {
const elem = event.target
const name = elem.value
const rules = this.presetRules[name]
console.log(rules)
Object.keys(rules).map((index) => {
const data = { 'rule' : index, 'value' : rules[index]}
console.log(rules[index])
this.$store.commit('update1dSingleRule', data)
})
}, },
updateInitial1dState(event) { updateInitial1dState(event) {
const elem = event.target const elem = event.target
@ -111,13 +155,17 @@ export default {
this.$root.$store.state.drawing = 1 this.$root.$store.state.drawing = 1
this.$root.$emit('draw1d') this.$root.$emit('draw1d')
}, },
getRule(id) {
const rules = this.$store.state.rules1d
return rules[id]
},
reset() { reset() {
this.$root.$emit('reset') this.$root.$emit('reset')
}, },
} }
} }
</script> </script>
<style>
.menu-row a {
color: white;
font-weight: bold;
text-decoration: none;
font-size: small;
}
</style>

View File

@ -27,9 +27,12 @@ export default new Vuex.Store({
initial1dState: "onecell" initial1dState: "onecell"
}, },
mutations: { mutations: {
update1dRules(state, data) { update1dSingleRule(state, data) {
state.rules1d[data.rule] = data.value state.rules1d[data.rule] = data.value
}, },
update1dRules(state, data) {
state.rules1d = data
},
setCellProperties(state, data) { setCellProperties(state, data) {
state.cellProperties[data.name] = data.value state.cellProperties[data.name] = data.value
}, },
@ -53,7 +56,7 @@ export default new Vuex.Store({
getCellProperties(state) { getCellProperties(state) {
return state.cellProperties return state.cellProperties
}, },
getRuleSet1d(state) { get1dRules(state) {
return state.rules1d return state.rules1d
}, },
getRule1d(state) { getRule1d(state) {