protected override void DrawWindowContent() { if (ImGui.BeginMenuBar()) { if (ImGui.BeginMenu("View")) { if (ImGui.BeginMenu("Mode")) { PropertyDrawer.DrawEditorValue(typeof(ViewportPanel).GetMember(nameof(ViewportPanel.ProjectioMode))[0], this); if (CurrentCamera != null) { PropertyDrawer.DrawEditorValue(typeof(ViewportPanel).GetMember(nameof(ViewportPanel.CurrentCamera))[0], this); } ImGui.EndMenu(); } ImGui.Separator(); if (ImGui.MenuItem("Focus")) { if (Context.ActiveEntity?.Transform != null) { CurrentCamera.Position = Context.ActiveEntity.Transform.Position; } } ImGui.Separator(); if (ImGui.MenuItem("Reset")) { CurrentCamera.Position = new Vector3(0, 0, 0); } ImGui.EndMenu(); } ImGui.EndMenuBar(); } if (Context.Scene != null) { // resize check { Vector2 viewportPanelSize = ImGui.GetContentRegionAvail(); if (viewportPanelSize != viewportSize && viewportPanelSize.X > 0 && viewportPanelSize.Y > 0) { viewportSize = viewportPanelSize; pipeline.Framebuffer.Resize((uint)viewportSize.X, (uint)viewportSize.Y); CurrentCamera.Resize(viewportSize.X, viewportSize.Y); } } // draw the framebuffer as image ImGui.Image(new IntPtr(pipeline.Framebuffer.ColorAttachments[pipeline.FbStructureIndex.Color]), viewportSize, new Vector2(0, 1), new Vector2(1, 0)); pipeline.Framebuffer.Bind(); Renderer.Clear(); pipeline.Framebuffer.ClearAttachment((uint)pipeline.FbStructureIndex.Id, 0); // interaction if (ImGui.IsItemHovered() && Focused) { // camera var io = ImGui.GetIO(); if (io.MouseWheel != 0) { CurrentCamera?.Zoom(io.MouseWheel); } if (ImGui.IsMouseDown(ImGuiMouseButton.Middle)) { if ((io.KeyMods & ImGuiKeyModFlags.Shift) > 0) { CurrentCamera?.Pan(io.MouseDelta * new Vector2(-1, 1)); } else { CurrentCamera?.Move(io.MouseDelta * new Vector2(-1, 1)); } } // selection else if (EnableSelect && ImGui.IsMouseClicked(ImGuiMouseButton.Left)) { Vector2 texpos = ImGui.GetMousePos() - new Vector2(WindowContentAreaMin.X, WindowContentAreaMax.Y); texpos.Y = -texpos.Y; int result = pipeline.Framebuffer.ReadPixel((uint)Context.Scene.Pipeline.FbStructureIndex.Id, (int)texpos.X, (int)texpos.Y); EditorApplication.Log.Trace($"selected entity id {result}"); Context.ActiveEntity = Context.Scene.GetEntity(result); } } // draw Context.Scene.Pipeline = pipeline; if (CurrentCamera != null) { Context.Scene.OnRenderEditor(CurrentCamera.ViewProjectionMatrix); } Framebuffer.Unbind(); } }
private void DrawComponents(Entity context) { // TODO: fix bool entityEnabled = context.Enabled; if (ImGui.Checkbox("Enabled", ref entityEnabled)) { context.Enabled = entityEnabled; } var components = context.Components; for (int compi = 0; compi < components.Count; compi++) { Component component = components[compi]; Type componentType = component.GetType(); bool stay = true; ImGui.PushID(componentType.Name + compi); bool valcol = !component.Valid; if (valcol) { ImGui.PushStyleColor(ImGuiCol.Header, new Vector4(0.412f, 0.118f, 0.118f, 1.0f)); ImGui.PushStyleColor(ImGuiCol.HeaderHovered, new Vector4(0.729f, 0.208f, 0.208f, 1.0f)); } // item shifting drop unsafe { // smoll fix bool resetTarget = false; if (targetedComponent == component) { ImGui.Button("##target", new Vector2(ImGui.GetColumnWidth(), 2.5f)); if (ImGui.IsItemHovered()) { resetTarget = true; } } if (ImGui.BeginDragDropTarget()) { var payload = ImGui.AcceptDragDropPayload("_COMPONENT"); if (payload.NativePtr != null && selectedDragNDropComponent != null) { int tci = Context.ActiveEntity.GetComponentIndex(targetedComponent); if (tci > Context.ActiveEntity.GetComponentIndex(selectedDragNDropComponent)) { tci--; } Context.ActiveEntity.ShiftComponent(selectedDragNDropComponent, tci); selectedDragNDropComponent = null; targetedComponent = null; } ImGui.EndDragDropTarget(); } if (resetTarget) { targetedComponent = null; } } bool collapsingHeader = ImGui.CollapsingHeader(componentType.Name, ref stay); // item shifting drag { if (ImGui.BeginDragDropSource()) { ImGui.Text("Component: " + componentType.Name); selectedDragNDropComponent = component; ImGui.SetDragDropPayload("_COMPONENT", IntPtr.Zero, 0); ImGui.EndDragDropSource(); } if (ImGui.BeginDragDropTarget()) { targetedComponent = component; ImGui.EndDragDropTarget(); } } // contex menu { if (ImGui.BeginPopupContextItem()) { if (ImGui.MenuItem("Move Up")) { context.ShiftComponent(component, Math.Max(0, compi - 1)); } if (ImGui.MenuItem("Move Down")) { context.ShiftComponent(component, Math.Min(components.Count - 1, compi + 1)); } ImGui.Separator(); if (ImGui.MenuItem("Remove")) { Context.ActiveEntity.RemoveComponent(component); } ImGui.EndPopup(); } } var style = ImGui.GetStyle(); float collapsingHeaderButtonOffset = ((ImGui.GetTextLineHeight() + style.FramePadding.Y * 2) + 1); ImGui.SameLine(ImGui.GetContentRegionAvail().X - (collapsingHeaderButtonOffset * 1 + style.FramePadding.X + style.FramePadding.Y)); bool enabled = component.Enabled; if (ImGui.Checkbox("##enabled", ref enabled)) { component.Enabled = enabled; } ImGui.SameLine(ImGui.GetContentRegionAvail().X - (collapsingHeaderButtonOffset * 2 + style.FramePadding.X + style.FramePadding.Y)); if (ImGui.ArrowButton("up", ImGuiDir.Up)) { context.ShiftComponent(component, Math.Max(0, compi - 1)); } ImGui.SameLine(ImGui.GetContentRegionAvail().X - (collapsingHeaderButtonOffset * 3 + style.FramePadding.X + style.FramePadding.Y)); if (ImGui.ArrowButton("down", ImGuiDir.Down)) { context.ShiftComponent(component, Math.Min(components.Count - 1, compi + 1)); } if (collapsingHeader) { ImGuiUtils.BeginGroupFrame(); MemberInfo[] membs = componentType.GetMembers(); for (int mi = 0; mi < membs.Length; mi++) { var mem = membs[mi]; if (mem.MemberType == MemberTypes.Field || mem.MemberType == MemberTypes.Property) { PropertyDrawer.DrawEditorValue(mem, component); } } //ImGui.Separator(); ImGuiUtils.EndGroupFrame(); } if (valcol) { ImGui.PopStyleColor(2); } ImGui.PopID(); if (!stay) { Context.ActiveEntity.RemoveComponent(component); } } }
protected override void DrawWindowContent() { bool resetView = false; bool openRename = false; if (ImGui.BeginMenuBar()) { if (ImGui.BeginMenu("View", Context.Scene != null)) { if (ImGui.MenuItem("Reset")) { resetView = true; } ImGui.EndMenu(); } if (ImGui.BeginMenu("Image", Context.Scene != null)) { if (ImGui.MenuItem("Load")) { LoadAsset(); } if (ImGui.MenuItem("Unload", SelectedTextureAsset != null)) { UnloadAsset(); } ImGui.Separator(); if (ImGui.MenuItem("Rename", SelectedTextureAsset != null)) { openRename = true; } ImGui.EndMenu(); } ImGui.PushItemWidth(256); PropertyDrawer.DrawEditorValue(this.GetType().GetField(nameof(SelectedTextureAsset)), this); ImGui.PopItemWidth(); ImGui.EndMenuBar(); } if (openRename) { ImGui.OpenPopup("renameasset"); } if (ImGui.BeginPopup("renameasset")) { byte[] bytes = new byte[256]; Encoding.UTF8.GetBytes(SelectedTextureAsset.Name).CopyTo(bytes, 0); ImGui.Text("Rename"); if (ImGui.InputText("New name", bytes, (uint)bytes.Length)) { ; } { // ! always trim end null bytes!!! string text = Encoding.UTF8.GetString(bytes).TrimEnd('\0'); SelectedTextureAsset.Name = text; } ImGui.EndPopup(); } if (SelectedTextureAsset != null) { //ImGui.SetCursorPos(WindowSize / 2 - currentTexture.Texture.Size / 2); var zoomedImageSize = SelectedTextureAsset.Texture.Size * zoom; if (zoomedImageSize.X < WindowSize.X && zoomedImageSize.Y < WindowSize.Y) { ImGui.SetCursorPos((WindowSize - zoomedImageSize) * 0.5f); } ImGui.PushStyleColor(ImGuiCol.Button, 0xff000000); ImGui.PushStyleColor(ImGuiCol.ButtonHovered, 0xff000000); ImGui.PushStyleColor(ImGuiCol.ButtonActive, 0xff000000); ImGui.ImageButton(new IntPtr(SelectedTextureAsset.Texture.ID), SelectedTextureAsset.Texture.Size * zoom, new Vector2(0, 1), new Vector2(1, 0), 0); ImGui.PopStyleColor(3); bool imageHovered = ImGui.IsItemHovered(); if (resetView) { ResetView(); } if (viewPos.X != ImGui.GetScrollX() + WindowSize.X / 2 || viewPos.Y != ImGui.GetScrollY() + WindowSize.Y / 2) { viewPos.X = ImGui.GetScrollX() + WindowSize.X / 2; viewPos.Y = ImGui.GetScrollY() + WindowSize.Y / 2; } if (imageHovered) { MouseMove(); //var io = ImGui.GetIO(); //float my_tex_w = SelectedTextureAsset.Texture.Width; //float my_tex_h = SelectedTextureAsset.Texture.Height; //Vector2 pos = ImGui.GetCursorScreenPos(); //ImGui.BeginTooltip(); //float region_sz = 32.0f; //float region_x = io.MousePos.X - pos.X - region_sz * 0.5f; //float region_y = io.MousePos.Y - pos.Y - region_sz * 0.5f; //float zoom = 4.0f; //if (region_x < 0.0f) { region_x = 0.0f; } //else if (region_x > my_tex_w - region_sz) { region_x = my_tex_w - region_sz; } //if (region_y < 0.0f) { region_y = 0.0f; } //else if (region_y > my_tex_h - region_sz) { region_y = my_tex_h - region_sz; } //ImGui.Text($"Min: ({region_x:F2}, {region_y:F2})"); //ImGui.Text($"Max: ({region_x + region_sz:F2}, {region_y + region_sz:F2})"); //Vector2 uv0 = new Vector2((region_x) / my_tex_w, (region_y) / my_tex_h); //Vector2 uv1 = new Vector2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h); //ImGui.Image(new IntPtr(SelectedTextureAsset.Texture.ID), new Vector2(region_sz * zoom, region_sz * zoom), uv0, uv1); //ImGui.EndTooltip(); } else { // correct image pos ImGui.SetScrollX(viewPos.X - WindowSize.X / 2); ImGui.SetScrollY(viewPos.Y - WindowSize.Y / 2); } if (ImGui.IsWindowHovered()) { MouseZoom(); } } }