Skip to content

Commit 0d7fb0a

Browse files
committed
Make blocks code runnable
1 parent 6e22f70 commit 0d7fb0a

File tree

4 files changed

+77
-38
lines changed

4 files changed

+77
-38
lines changed

src/codingworkshops/views/Project.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template lang="pug">
22
.full.d-flex.flex-column.justify-content-between(v-if="!$rest.loading && $rest.project.ok")
3-
Nico.p-5(:show-greeting="false" language="Python" editorMode="block" :script-boilerplate="false")
3+
Nico.p-5(:show-greeting="false" language="Python" editorMode="blocks" :script-boilerplate="false")
44
div.p-4.d-flex.justify-content-between.border-light.bt-only(style="height: 70px;")
55
div
66
router-link.mr-green(:to="{ name: 'home' }")

src/nico/src/nico/App.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Game(v-if="showTabs.game" v-show="view === 'game'" :show-greeting="showGreeting")
66
Editor(v-if="showTabs.editor && editorMode == 'code'" v-show="view === 'editor'" :language="language")
7-
BlockEditor(v-if="showTabs.editor && editorMode == 'block'" v-show="view === 'editor'")
7+
BlockEditor(v-if="showTabs.editor && editorMode == 'blocks'" v-show="view === 'editor'")
88
Sprite(v-if="showTabs.sprite" v-show="view === 'sprite'")
99
Tile(v-if="showTabs.tile" v-show="view === 'tile'")
1010
Settings(v-if="showTabs.settings" v-show="view === 'settings'")
@@ -68,11 +68,12 @@ export default {
6868
...mapState('nico', ['view']),
6969
},
7070
mounted () {
71-
this.setLanguage(this.language)
71+
this.setEditorMode(this.editorMode)
72+
if (this.editorMode === 'code') this.setLanguage(this.language)
7273
if (this.scriptBoilerplate) this.loadBoilerplate()
7374
},
7475
methods: {
75-
...mapMutations('nico', ['loadBoilerplate']),
76+
...mapMutations('nico', ['loadBoilerplate', 'setEditorMode']),
7677
...mapActions('nico', ['setLanguage']),
7778
},
7879
}

src/nico/src/nico/components/BlockEditor.vue

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
<template lang="pug">
2-
.d-flex
3-
.flex-1.m-3
4-
h6.font-weight-bold Control Flow
5-
Block(:children="controlFlow" clone)
6-
Expr(:exprs="vars" clone)
7-
// mw-35 just for displaying blocksRoot
8-
.flex-1.m-3.mw-35
9-
h6.font-weight-bold Nico
10-
Block(:children="nicoFuncs" clone)
11-
| {{ blocksRoot }}
12-
br
13-
| {{ compiledBlocks }}
14-
.flex-1.p-3.w-50
15-
Block(:children="blocksRoot")
2+
div
3+
.d-flex
4+
.flex-1.m-3
5+
h6.font-weight-bold Control Flow
6+
Block(:children="controlFlow" clone)
7+
Expr(:exprs="vars" clone)
8+
// mw-35 just for displaying blocksRoot
9+
.flex-1.m-3.mw-35
10+
h6.font-weight-bold Nico
11+
Block(:children="nicoFuncs" clone)
12+
| {{ blocksRoot }}
13+
br
14+
| {{ compiledBlocks }}
15+
.flex-1.p-3.w-50
16+
Block(:children="blocksRoot")
1617
</template>
1718

1819
<script>
1920
import Block from './Block'
2021
import Expr from './Expr'
2122
import { compile } from '../compileBlocks'
23+
import { mapMutations } from 'vuex';
2224
2325
const makeLit = value => ({
2426
type: 'literal',
@@ -62,6 +64,11 @@ export default {
6264
func: 'rect',
6365
params: [0, 0, 0, 0].map(makeLit),
6466
},
67+
{
68+
type: 'callMars',
69+
func: 'text',
70+
params: ['', 0, 0].map(makeLit),
71+
}
6572
],
6673
vars: [
6774
{
@@ -76,6 +83,17 @@ export default {
7683
return compile(this.blocksRoot)
7784
},
7885
},
86+
methods: {
87+
...mapMutations('nico', ['setCode', 'setBlocks'])
88+
},
89+
watch: {
90+
blocksRoot: {
91+
handler () {
92+
this.setBlocks(this.blocksRoot)
93+
},
94+
deep: true,
95+
}
96+
}
7997
}
8098
</script>
8199

src/nico/src/nico/store.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@ import generateSet from '@/generateSet'
22
import * as languages from './languages'
33

44
import { initMars } from './mars'
5+
import { compile } from './compileBlocks'
56

67
export default {
78
namespaced: true,
89

910
state: {
1011
code: window.localStorage.getItem('code') || '',
12+
blocks: [],
1113
errors: [],
1214
warnings: [],
1315
view: window.localStorage.getItem('view') || 'game',
1416
paused: false,
1517
running: false,
1618
mainCtx: null,
1719
hasBeenRun: false,
20+
editorMode: 'code',
1821
language: new languages.Python(),
1922
loading: false,
2023
langLoading: false,
@@ -44,6 +47,7 @@ export default {
4447
'mainCtx',
4548
'paused',
4649
'clicks',
50+
'editorMode',
4751
'language',
4852
'langLoading',
4953
'mars',
@@ -68,6 +72,9 @@ export default {
6872
state.code = code
6973
window.localStorage.setItem('code', state.code)
7074
},
75+
setBlocks (state, blocks) {
76+
state.blocks = blocks
77+
},
7178
setRunning (state, running) {
7279
state.running = running
7380
state.hasBeenRun = true
@@ -118,26 +125,39 @@ export default {
118125

119126
// this timeout is necessary for vuex to register the change in `loading`
120127
setTimeout(() => {
121-
language
122-
.refresh(state.code, mars)
123-
.then(({ success, draw, update, init, errors, warnings, blocked }) => {
124-
commit('setLoading', false)
125-
commit('setWarnings', warnings || [])
126-
commit('setClicks', 0)
127-
128-
if (success) {
129-
commit('setRunning', true)
130-
commit('setPaused', false)
131-
132-
commit('setDrawFunc', draw)
133-
commit('setUpdateFunc', update)
134-
135-
init()
136-
startMars()
137-
} else if (!blocked) {
138-
commit('setErrors', errors)
139-
}
140-
})
128+
if (state.editorMode === 'blocks') {
129+
commit('setLoading', false)
130+
commit('setWarnings', [])
131+
commit('setClicks', 0)
132+
commit('setRunning', true)
133+
commit('setPaused', false)
134+
const code = `(function(){${compile(state.blocks)}})`
135+
const draw = eval(code)
136+
commit('setDrawFunc', draw)
137+
commit('setUpdateFunc', () => {})
138+
startMars()
139+
} else if (state.editorMode === 'code') {
140+
language
141+
.refresh(state.code, mars)
142+
.then(({ success, draw, update, init, errors, warnings, blocked }) => {
143+
commit('setLoading', false)
144+
commit('setWarnings', warnings || [])
145+
commit('setClicks', 0)
146+
147+
if (success) {
148+
commit('setRunning', true)
149+
commit('setPaused', false)
150+
151+
commit('setDrawFunc', draw)
152+
commit('setUpdateFunc', update)
153+
154+
init()
155+
startMars()
156+
} else if (!blocked) {
157+
commit('setErrors', errors)
158+
}
159+
})
160+
}
141161
// delay helps make sure that the old game stops
142162
}, 100)
143163
},

0 commit comments

Comments
 (0)