Skip to content

Commit 70e4eed

Browse files
committed
🐛 Fix: Changing item display properties does not update the project's saved flag
Also fixed incorrect property name for `itemDisplay` (`item_display`)
1 parent f21169e commit 70e4eed

File tree

3 files changed

+93
-12
lines changed

3 files changed

+93
-12
lines changed

src/components/vanillaItemDisplayElementPanel.svelte

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
<script lang="ts" context="module">
1+
<script lang="ts">
22
import { ITEM_DISPLAY_ITEM_DISPLAY_SELECT } from '../interface/panel/vanillaItemDisplayElement'
33
import { VanillaItemDisplay } from '../outliner/vanillaItemDisplay'
44
import { events } from '../util/events'
55
import { Valuable } from '../util/stores'
66
import { translate } from '../util/translation'
7-
</script>
87
9-
<script lang="ts">
108
let selectedDisplay = VanillaItemDisplay.selected.at(0)
9+
let lastSelected = selectedDisplay
1110
1211
let item = new Valuable<string>('')
1312
let error = new Valuable<string>('')
1413
let itemDisplaySlot: HTMLDivElement
1514
let visible = false
1615
16+
let unsub: (() => void) | undefined
17+
1718
events.UPDATE_SELECTION.subscribe(() => {
1819
selectedDisplay = VanillaItemDisplay.selected.at(0)
1920
if (!selectedDisplay || selected.length > 1) {
@@ -22,10 +23,49 @@
2223
visible = false
2324
return
2425
}
25-
item = selectedDisplay._item
26+
$item = selectedDisplay.item
27+
error = selectedDisplay.error
28+
visible = true
29+
})
30+
31+
events.UPDATE_SELECTION.subscribe(() => {
32+
unsub?.()
33+
34+
lastSelected = selectedDisplay
35+
selectedDisplay = VanillaItemDisplay.selected.at(0)
36+
37+
if (!selectedDisplay) {
38+
visible = false
39+
return
40+
}
41+
42+
$item = selectedDisplay.item
2643
error = selectedDisplay.error
2744
ITEM_DISPLAY_ITEM_DISPLAY_SELECT.set(selectedDisplay.itemDisplay)
2845
visible = true
46+
47+
unsub = item.subscribe(value => {
48+
if (selectedDisplay == undefined || selectedDisplay !== lastSelected) {
49+
lastSelected = selectedDisplay
50+
return
51+
}
52+
if (value === selectedDisplay.item) return
53+
54+
Undo.initEdit({ elements: VanillaItemDisplay.selected })
55+
56+
if (VanillaItemDisplay.selected.length > 1) {
57+
for (const display of VanillaItemDisplay.selected) {
58+
display.item = value
59+
}
60+
} else {
61+
selectedDisplay.item = value
62+
}
63+
Project!.saved = false
64+
65+
Undo.finishEdit(`Change Item Display's Item to "${$item}"`, {
66+
elements: VanillaItemDisplay.selected,
67+
})
68+
})
2969
})
3070
3171
requestAnimationFrame(() => {

src/interface/panel/vanillaItemDisplayElement.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { isCurrentFormat } from '../../blueprintFormat'
22
import VanillaItemDisplayElementPanel from '../../components/vanillaItemDisplayElementPanel.svelte'
33
import { PACKAGE } from '../../constants'
4-
import { VanillaItemDisplay } from '../../outliner/vanillaItemDisplay'
4+
import { ItemDisplayMode, VanillaItemDisplay } from '../../outliner/vanillaItemDisplay'
5+
import { events } from '../../util/events'
56
import { injectSvelteCompomponentMod } from '../../util/injectSvelteComponent'
67
import { translate } from '../../util/translation'
78

@@ -46,8 +47,11 @@ ITEM_DISPLAY_ITEM_DISPLAY_SELECT.get = function () {
4647
if (!selected) return 'left'
4748
return selected.itemDisplay
4849
}
49-
ITEM_DISPLAY_ITEM_DISPLAY_SELECT.set = function (this: BarSelect<string>, value: string) {
50-
const selected = VanillaItemDisplay.selected[0]
50+
ITEM_DISPLAY_ITEM_DISPLAY_SELECT.set = function (
51+
this: BarSelect<ItemDisplayMode>,
52+
value: ItemDisplayMode
53+
) {
54+
const selected = VanillaItemDisplay.selected.at(0)
5155
if (!selected) return this
5256
this.value = value
5357
const name = this.getNameFor(value)
@@ -57,6 +61,32 @@ ITEM_DISPLAY_ITEM_DISPLAY_SELECT.set = function (this: BarSelect<string>, value:
5761
if (!this.nodes.includes(this.node)) {
5862
$(this.node).find('bb-select').text(name)
5963
}
60-
selected.itemDisplay = value
64+
65+
if (selected.itemDisplay === value) return this
66+
67+
Undo.initEdit({ elements: VanillaItemDisplay.selected })
68+
if (VanillaItemDisplay.selected.length > 1) {
69+
for (const display of VanillaItemDisplay.selected) {
70+
display.itemDisplay = value
71+
}
72+
} else {
73+
selected.itemDisplay = value
74+
}
75+
Project!.saved = false
76+
Undo.finishEdit(`Change Item Display Node's Item Display Mode to ${value}`, {
77+
elements: VanillaItemDisplay.selected,
78+
})
6179
return this
6280
}
81+
function updateItemDisplaySelect() {
82+
console.log('updateItemDisplaySelect')
83+
let value = VanillaItemDisplay.selected.at(0)?.itemDisplay
84+
value ??= 'none'
85+
ITEM_DISPLAY_ITEM_DISPLAY_SELECT.set(value)
86+
}
87+
events.UNDO.subscribe(() => {
88+
updateItemDisplaySelect()
89+
})
90+
events.REDO.subscribe(() => {
91+
updateItemDisplaySelect()
92+
})

src/outliner/vanillaItemDisplay.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@ import { translate } from '../util/translation'
1212
import { ResizableOutlinerElement } from './resizableOutlinerElement'
1313
import { sanitizeOutlinerElementName } from './util'
1414

15+
export type ItemDisplayMode =
16+
| 'none'
17+
| 'thirdperson_lefthand'
18+
| 'thirdperson_righthand'
19+
| 'firstperson_lefthand'
20+
| 'firstperson_righthand'
21+
| 'head'
22+
| 'gui'
23+
| 'ground'
24+
| 'fixed'
25+
1526
interface VanillaItemDisplayOptions {
1627
name?: string
1728
item?: string
18-
item_display?: string
29+
itemDisplay?: ItemDisplayMode
1930
position?: ArrayVector3
2031
rotation?: ArrayVector3
2132
scale?: ArrayVector3
@@ -33,7 +44,7 @@ export class VanillaItemDisplay extends ResizableOutlinerElement {
3344

3445
// Properties
3546
public _item = new Valuable('minecraft:diamond')
36-
public _itemDisplay = new Valuable('none')
47+
public _itemDisplay = new Valuable<ItemDisplayMode>('none')
3748
public config: IBlueprintBoneConfigJSON
3849

3950
public error = new Valuable('')
@@ -111,7 +122,7 @@ export class VanillaItemDisplay extends ResizableOutlinerElement {
111122
if (this._itemDisplay === undefined) return 'none'
112123
return this._itemDisplay.get()
113124
}
114-
set itemDisplay(value: string) {
125+
set itemDisplay(value: ItemDisplayMode) {
115126
if (this._itemDisplay === undefined) return
116127
this._itemDisplay.set(value)
117128
}
@@ -191,7 +202,7 @@ export class VanillaItemDisplay extends ResizableOutlinerElement {
191202
}
192203
}
193204
new Property(VanillaItemDisplay, 'string', 'item', { default: 'minecraft:diamond' })
194-
new Property(VanillaItemDisplay, 'string', 'item_display', { default: 'none' })
205+
new Property(VanillaItemDisplay, 'string', 'itemDisplay', { default: 'none' })
195206
new Property(VanillaItemDisplay, 'object', 'config', {
196207
get default() {
197208
return new BoneConfig().toJSON()

0 commit comments

Comments
 (0)