Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PrefabPreviewEditorWindow = EditorWindow:New({
- Title = "Prefab Previewer",
- Submenu = "Witchy Project" -- This tells the engine where to place it in the View menu
- })
- function PrefabPreviewEditorWindow:Initialize()
- -- the behavior of this window assumes the assets obtained here
- -- won't be deleted while the window is active, so in a more
- -- robust scenario the code would be a bit more complex
- self.prefabs = AssetManager.GetAssetsOfType(AssetType.Prefab)
- -- this shows the relative path to the currently selected asset
- self.pathLabel = self:CreateLabel("Select a Prefab")
- -- the MVP, it's a default SceneView so it has an empty scene
- -- loaded in. One could also load an existing scene into it,
- -- or create several scene views if desired, in the same window.
- -- we provide an Update function which mimics the Update loop
- -- in the engine proper
- self.sceneView = self:CreateSceneView()
- self.sceneView.Update = function(deltaTime)
- self:SceneUpdate(deltaTime)
- end
- -- the list box that will hold our prefabs
- -- we will instantiate the selected prefab in the SceneView's
- -- scene for previewing purposes.
- self.prefabListBox = self:CreateListBox("Prefabs")
- for i = 1, #self.prefabs do
- self.prefabListBox:AddItem(self.prefabs[i].FileName)
- end
- self.prefabListBox.OnSelectionChanged = function(newIndex)
- self:OnSelectedPrefabChanged(newIndex)
- end
- end
- function PrefabPreviewEditorWindow:SceneUpdate(deltaTime)
- -- if we've instantiated a prefab, rotate the entity so it looks cool
- if self.activeEntity ~= nil then
- self.activeEntity:RotateRight(80.0 * deltaTime)
- end
- end
- function PrefabPreviewEditorWindow:OnSelectedPrefabChanged(newIndex)
- if newIndex > 0 then
- -- we only want at most one instantiated prefab in the scene
- -- so delete the current one, if exists.
- if self.activeEntity ~= nil then
- self.sceneView:DestroyEntity(self.activeEntity)
- self.activeEntity = nil
- end
- self.pathLabel:SetText(self.prefabs[newIndex].FilePath)
- self.activeEntity = self.sceneView:InstantiatePrefab(self.prefabs[newIndex].Id)
- self.activeEntity:SetWorldPosition(Vec3:new(0.0, 0.0, 0.0))
- self.activeEntity:ClearRotation()
- -- this long named function travels the entity's hierarchy and tries to build a
- -- bounding sphere that covers its, and its children's, bounding spheres.
- local boundingSphere = self.activeEntity:GetBoundingSphereCoveringHierarchyInWorldSpace()
- if boundingSphere then
- local targetPos = boundingSphere.Origin + Vec3:new(
- boundingSphere.Radius + boundingSphere.Radius * 0.2,
- 0.1,
- 0.1
- )
- self.sceneView:SetCameraPosition(targetPos)
- self.sceneView:SetCameraLookAt(boundingSphere.Origin)
- else
- self.sceneView:SetCameraPosition(Vec3:new(0.2, 0.0, 0.0))
- self.sceneView:SetCameraLookAt(Vec3:new(0.0, 0.0, 0.0))
- end
- else
- if self.activeEntity ~= nil then
- self.sceneView:DestroyEntity(self.activeEntity)
- self.activeEntity = nil
- end
- self.pathLabel:SetText("Select a Prefab")
- end
- end
- function PrefabPreviewEditorWindow:OnContentAreaResized(newWidth, newHeight)
- -- Stuck with absolute positioning/sizing until I figure out what layouts should work like
- local labelHeight = 20
- local leftRightMargin = 10
- local contentWidthWithoutMargins = newWidth - leftRightMargin - leftRightMargin
- local sceneViewWidth = 0.8 * contentWidthWithoutMargins
- local prefabListWidth = contentWidthWithoutMargins - sceneViewWidth
- local sceneAndListHeight = newHeight - labelHeight
- self.pathLabel:SetPosition(leftRightMargin, 0)
- self.pathLabel:SetSize(contentWidthWithoutMargins, labelHeight)
- self.sceneView:SetPosition(leftRightMargin, labelHeight)
- self.sceneView:SetSize(sceneViewWidth, sceneAndListHeight)
- self.prefabListBox:SetPosition(leftRightMargin + sceneViewWidth, labelHeight)
- self.prefabListBox:SetSize(prefabListWidth, sceneAndListHeight)
- end
Advertisement
Add Comment
Please, Sign In to add comment