File size: 2,187 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
----- XTemplateEditor (preview support)

if FirstLoad then
	GedXTemplatePreviewObject = false
	GedXTemplateEditorId = 0
end

DefineClass("GedPreview", "XDialog")

local function OpenPreview(template)
	local preview = OpenDialog("GedPreview")
	XTemplateSpawn(template.id, preview)
	for _, win in ipairs(preview) do
		win:Open()
	end
end

local function ClosePreview()
	CloseDialog("GedPreview")
end

local function GedOpenXTemplatePreview(template, ged_id, live_preview)
	sprocall(ClosePreview)
	if not template.id then return end -- preset category selected

	sprocall(OpenPreview, template)
	if GetDialog("GedPreview") and #GetDialog("GedPreview") > 0 then
		if live_preview then
			GedXTemplatePreviewObject = template
		else
			GedXTemplatePreviewObject = false
		end
		GedXTemplateEditorId = ged_id
	end
end

local function SendXTemplateActionStates(ged)
	if not ged then
		for _, ged in pairs(GedConnections) do
			if ged.app_template == "XTemplateEditor" then
				SendXTemplateActionStates(ged)
			end
		end
		return
	end
	ged:Send("rfnApp", "SetActionToggled", "LivePreviewXTemplate", GedXTemplateEditorId == ged.ged_id and GedXTemplatePreviewObject and true)
	ged:Send("rfnApp", "SetActionToggled", "PreviewXTemplate", GedXTemplateEditorId == ged.ged_id and not GedXTemplatePreviewObject or false)
end

local function GedCloseXTemplatePreview()
	CloseDialog("GedPreview")
	GedXTemplatePreviewObject = false
	GedXTemplateEditorId = 0
	SendXTemplateActionStates()
end

function OnMsg.GedClosing(ged_id)
	if GedXTemplateEditorId == ged_id then
		GedCloseXTemplatePreview()
	end
end

function OnMsg.ObjModified(obj)
	local preview = GetDialog("GedPreview")
	if GedXTemplatePreviewObject == obj and preview then
		preview:DeleteThread("update")
		preview:CreateThread("update", function()
			GedOpenXTemplatePreview(obj, GedXTemplateEditorId, true)
		end)
	end
end

function GedOpPreviewXTemplate(socket, obj, live_preview)
	if GetDialog("GedPreview") and #GetDialog("GedPreview") > 0 and live_preview == (GedXTemplatePreviewObject == obj) then
		GedCloseXTemplatePreview()
	else
		GedOpenXTemplatePreview(obj, socket.ged_id, live_preview)
	end
	SendXTemplateActionStates(socket)
end