Skip to content

Commit 5fcf557

Browse files
committed
make palettes remember previous state Closes #35
1 parent 5387131 commit 5fcf557

8 files changed

+172
-14
lines changed

addons/nklbdev.enchanced_tilemap_editor/bottom_panel.gd

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extends VBoxContainer
22

33
const Common = preload("common.gd")
4+
const WeakRefStorage = preload("weakref_storage.gd")
45
const Instrument = preload("instruments/_base.gd")
56
const InstrumentStamp = preload("instruments/stamp.gd")
67
const InstrumentLine = preload("instruments/line.gd")
@@ -74,13 +75,28 @@ func _init(selection_paper: Selection, tiles_paper: Paper, autotiles_paper: Pape
7475
if __palettes_option_button.get_item_count() > 0:
7576
__on_palettes_option_button_item_selected(0)
7677

78+
var __last_states: WeakRefStorage = WeakRefStorage.new()
79+
var __tile_map: TileMap
7780
func set_up(tile_map: TileMap) -> void:
81+
__tile_map = tile_map
82+
if __tile_map.tile_set:
83+
var last_selected_palette_id = __last_states.pop(__tile_map.tile_set)
84+
if last_selected_palette_id != null:
85+
last_selected_palette_id = last_selected_palette_id as int
86+
var idx = __palettes_option_button.get_item_index(last_selected_palette_id)
87+
if __palettes_option_button.selected != idx:
88+
__palettes_option_button.select(idx)
89+
__palettes_option_button.emit_signal("item_selected", idx)
7890
for palette_index in __palettes_option_button.get_item_count():
7991
__palettes_option_button.get_item_metadata(palette_index).set_up(tile_map)
8092

8193
func tear_down() -> void:
82-
for palette_index in __palettes_option_button.get_item_count():
83-
__palettes_option_button.get_item_metadata(palette_index).tear_down()
94+
var last_selected_palette_id: int = __palettes_option_button.get_selected_id()
95+
if __tile_map.tile_set:
96+
__last_states.push(__tile_map.tile_set, last_selected_palette_id)
97+
__tile_map = null
98+
for palette_index in __palettes_option_button.get_item_count():
99+
__palettes_option_button.get_item_metadata(palette_index).tear_down()
84100

85101
func process_input_event_key(event: InputEventKey) -> bool:
86102
return __current_palette.process_input_event_key(event)

addons/nklbdev.enchanced_tilemap_editor/palette/_base.gd

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extends Control
22

33
const Common = preload("../common.gd")
4+
const WeakRefStorage = preload("../weakref_storage.gd")
45
const Subpalette = preload("_subpalette.gd")
56
const ToolBar = preload("tool_bar.gd")
67
const Instrument = preload("../instruments/_base.gd")
@@ -25,11 +26,28 @@ func _init(title: String, icon_name: String, subpalettes: Array) -> void:
2526
if __subpalettes_option_button.get_item_count() > 0:
2627
__on_subpalettes_option_button_item_selected(0)
2728

29+
30+
var __tile_map: TileMap
31+
var __last_states: WeakRefStorage = WeakRefStorage.new()
2832
func set_up(tile_map: TileMap) -> void:
33+
__tile_map = tile_map
34+
if __tile_map.tile_set:
35+
var last_selected_subpalette_id = __last_states.pop(__tile_map.tile_set)
36+
if last_selected_subpalette_id != null:
37+
last_selected_subpalette_id = last_selected_subpalette_id as int
38+
var idx = __subpalettes_option_button.get_item_index(last_selected_subpalette_id)
39+
if __subpalettes_option_button.selected != idx:
40+
__subpalettes_option_button.select(idx)
41+
__subpalettes_option_button.emit_signal("item_selected", idx)
2942
for subpalette_index in __subpalettes_option_button.get_item_count():
3043
__subpalettes_option_button.get_item_metadata(subpalette_index).set_up(tile_map)
3144

3245
func tear_down() -> void:
46+
var last_selected_subpalette_id = __subpalettes_option_button.get_selected_id()
47+
if __tile_map.tile_set:
48+
__last_states.push(__tile_map.tile_set, last_selected_subpalette_id)
49+
__tile_map = null
50+
3351
for subpalette_index in __subpalettes_option_button.get_item_count():
3452
__subpalettes_option_button.get_item_metadata(subpalette_index).tear_down()
3553

addons/nklbdev.enchanced_tilemap_editor/palette/_list_and_content_subpalette.gd

+54-12
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,47 @@ func _init(title: String, icon_name: String).(title, icon_name) -> void:
9696
]).build()
9797
__center_view_button.set_anchors_and_margins_preset(Control.PRESET_TOP_RIGHT, Control.PRESET_MODE_MINSIZE)
9898

99+
const _content_holder_position_key: String = "content_holder_position"
100+
const _content_scaler_position_key: String = "content_scaler_position"
101+
const _content_scaler_scale_key: String = "content_scaler_scale"
102+
const _content_zoom_key: String = "content_scaler_scale"
103+
104+
func _after_set_up() -> void:
105+
if not _state.empty():
106+
var contents_states: Array = _state["contents_states"]
107+
if _item_list.get_item_count() == contents_states.size():
108+
var item_index: int = 0
109+
for content_state in contents_states:
110+
var content = _item_list.get_item_metadata(item_index)
111+
if content_state.has(_content_holder_position_key):
112+
content.set_meta(_content_holder_position_key, content_state[_content_holder_position_key])
113+
if content_state.has(_content_scaler_position_key):
114+
content.set_meta(_content_scaler_position_key, content_state[_content_scaler_position_key])
115+
if content_state.has(_content_scaler_scale_key):
116+
content.set_meta(_content_scaler_scale_key, content_state[_content_scaler_scale_key])
117+
if content_state.has(_content_zoom_key):
118+
content.set_meta(_content_zoom_key, content_state[_content_zoom_key])
119+
item_index += 1
120+
._after_set_up()
99121

100122
func _before_tear_down() -> void:
123+
._before_tear_down()
124+
if _content:
125+
__save_content_state_to_metadata()
126+
var contents_states: Array = []
127+
for item_index in _item_list.get_item_count():
128+
var content = _item_list.get_item_metadata(item_index)
129+
var content_state: Dictionary = {}
130+
if content.has_meta(_content_holder_position_key):
131+
content_state[_content_holder_position_key] = content.get_meta(_content_holder_position_key)
132+
if content.has_meta(_content_scaler_position_key):
133+
content_state[_content_scaler_position_key] = content.get_meta(_content_scaler_position_key)
134+
if content.has_meta(_content_scaler_scale_key):
135+
content_state[_content_scaler_scale_key] = content.get_meta(_content_scaler_scale_key)
136+
if content.has_meta(_content_zoom_key):
137+
content_state[_content_zoom_key] = content.get_meta(_content_zoom_key)
138+
contents_states.append(content_state)
139+
_state["contents_states"] = contents_states
101140
unselect()
102141
__clear_content_viewport()
103142

@@ -107,14 +146,14 @@ func _on_item_list_item_selected(index: int, metadata) -> void:
107146
__content_holder.add_child(_content)
108147
__center_view()
109148
__set_content_zoom(EDSCALE)
110-
if _content.has_meta("content_holder_position"):
111-
__content_holder.rect_position = _content.get_meta("content_holder_position")
112-
if _content.has_meta("content_scaler_position"):
113-
__content_scaler.rect_position = _content.get_meta("content_scaler_position")
114-
if _content.has_meta("content_scaler_scale"):
115-
__content_scaler.rect_scale = _content.get_meta("content_scaler_scale")
116-
if _content.has_meta("content_zoom"):
117-
__content_zoom = _content.get_meta("content_zoom")
149+
if _content.has_meta(_content_holder_position_key):
150+
__content_holder.rect_position = _content.get_meta(_content_holder_position_key)
151+
if _content.has_meta(_content_scaler_position_key):
152+
__content_scaler.rect_position = _content.get_meta(_content_scaler_position_key)
153+
if _content.has_meta(_content_scaler_scale_key):
154+
__content_scaler.rect_scale = Vector2.ONE * _content.get_meta(_content_scaler_scale_key)
155+
if _content.has_meta(_content_zoom_key):
156+
__content_zoom = _content.get_meta(_content_zoom_key)
118157
__update_zoom_label()
119158
__update_center_view_button()
120159

@@ -124,12 +163,15 @@ func _on_unselect() -> void:
124163
_previous_selected_tile = null
125164
_selected_pattern = null
126165

166+
func __save_content_state_to_metadata() -> void:
167+
_content.set_meta(_content_holder_position_key, __content_holder.rect_position)
168+
_content.set_meta(_content_scaler_position_key, __content_scaler.rect_position)
169+
_content.set_meta(_content_scaler_scale_key, __content_scaler.rect_scale.x)
170+
_content.set_meta(_content_zoom_key, __content_zoom)
171+
127172
func __clear_content_viewport() -> void:
128173
if _content:
129-
_content.set_meta("content_holder_position", __content_holder.rect_position)
130-
_content.set_meta("content_scaler_position", __content_scaler.rect_position)
131-
_content.set_meta("content_scaler_scale", __content_scaler.rect_scale)
132-
_content.set_meta("content_zoom", __content_zoom)
174+
__save_content_state_to_metadata()
133175
__content_holder.remove_child(_content)
134176
_content = null
135177
__center_view()

addons/nklbdev.enchanced_tilemap_editor/palette/_list_subpalette.gd

+6
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ func _init(title: String, icon_name: String).(title, icon_name) -> void:
1818

1919
func _on_item_list_item_selected(index: int, metadata) -> void:
2020
emit_signal("selected", metadata)
21+
22+
func _after_set_up() -> void:
23+
._after_set_up()
24+
25+
func _before_tear_down() -> void:
26+
._before_tear_down()

addons/nklbdev.enchanced_tilemap_editor/palette/_subpalette.gd

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extends Control
22

33
const Common = preload("../common.gd")
4+
const WeakRefStorage = preload("../weakref_storage.gd")
45
const TreeBuilder = preload("../tree_builder.gd")
56
const Instrument = preload("../instruments/_base.gd")
67

@@ -19,12 +20,28 @@ func _init(title: String, icon_name: String) -> void:
1920
anchor_bottom = 1
2021
_item_list.connect("item_selected", self, "__on_item_list_item_selected")
2122

23+
var __last_states: WeakRefStorage = WeakRefStorage.new()
24+
var _state: Dictionary
2225
func set_up(tile_map: TileMap) -> void:
2326
_tile_map = tile_map
27+
_state.clear()
28+
if _tile_map.tile_set != null:
29+
var state = __last_states.pop(_tile_map.tile_set)
30+
if state != null:
31+
_state.merge(state)
2432
_after_set_up()
33+
if not _state.empty() and _state.item_idx >= 0 and _item_list.get_item_count() > 0:
34+
var idx = min(_state.item_idx, _item_list.get_item_count() - 1)
35+
_item_list.select(idx)
36+
_item_list.emit_signal("item_selected", idx)
2537

2638
func tear_down() -> void:
39+
var selected_items: PoolIntArray = _item_list.get_selected_items()
40+
_state["item_idx"] = -1 if selected_items.empty() else selected_items[0]
2741
_before_tear_down()
42+
if _tile_map.tile_set:
43+
__last_states.push(_tile_map.tile_set, _state.duplicate())
44+
_state.clear()
2845
_item_list.clear()
2946
_tile_map = null
3047

addons/nklbdev.enchanced_tilemap_editor/palette/subpalette_tiles_by_texture.gd

+4
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,7 @@ func _after_set_up() -> void:
139139
tile_texture.resource_path.get_file() if tile_texture.resource_path else "",
140140
tile_texture,
141141
TilesTextureRect.new(tile_texture, tile_set, self))
142+
._after_set_up()
143+
144+
func _before_tear_down() -> void:
145+
._before_tear_down()

addons/nklbdev.enchanced_tilemap_editor/palette/subpalette_tiles_individual.gd

+3
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,7 @@ func _after_set_up() -> void:
151151
"%s: %s" % [tile_id, tile_set.tile_get_name(tile_id)],
152152
tile_icon,
153153
TileOnTileMap.new(tile_id, _tile_map, self))
154+
._after_set_up()
154155

156+
func _before_tear_down() -> void:
157+
._before_tear_down()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
extends Object
2+
3+
var __weak_refs: Array
4+
var __values: Array
5+
6+
func push(key: Object, value) -> void:
7+
var is_element_replaced: bool = false
8+
var idx: int = __weak_refs.size() - 1
9+
while idx >= 0:
10+
var ref = __weak_refs[idx].get_ref()
11+
if ref == null:
12+
__weak_refs.pop_at(idx)
13+
__values.pop_at(idx)
14+
elif key == ref:
15+
__values[idx] = value
16+
is_element_replaced = true
17+
idx -= 1
18+
if not is_element_replaced:
19+
__weak_refs.push_back(weakref(key))
20+
__values.push_back(value)
21+
22+
func pop(key: Object):
23+
var value_to_return = null
24+
var idx: int = __weak_refs.size() - 1
25+
while idx >= 0:
26+
var ref = __weak_refs[idx].get_ref()
27+
if ref == null:
28+
__weak_refs.pop_at(idx)
29+
__values.pop_at(idx)
30+
elif key == ref:
31+
value_to_return = __values[idx]
32+
__weak_refs.pop_at(idx)
33+
__values.pop_at(idx)
34+
idx -= 1
35+
return value_to_return
36+
37+
func peek(key: Object):
38+
var value_to_return = null
39+
var idx: int = __weak_refs.size() - 1
40+
while idx >= 0:
41+
var ref = __weak_refs[idx].get_ref()
42+
if ref == null:
43+
__weak_refs.pop_at(idx)
44+
__values.pop_at(idx)
45+
elif key == ref:
46+
value_to_return = __values[idx]
47+
idx -= 1
48+
return value_to_return
49+
50+
func clear() -> void:
51+
__weak_refs.clear()
52+
__values.clear()

0 commit comments

Comments
 (0)