Esempio n. 1
0
        public static bool ToggleButton(string strId, ref bool v)
        {
            Vector2       p         = ImGuiNET.ImGui.GetCursorScreenPos();
            ImDrawListPtr drawList  = ImGuiNET.ImGui.GetWindowDrawList();
            bool          isClicked = false;

            float height = ImGuiNET.ImGui.GetFrameHeight();
            float width  = height * 1.55f;
            float radius = height * 0.50f;

            if (ImGuiNET.ImGui.InvisibleButton(strId, new Vector2(width, height)))
            {
                isClicked = true;
                v         = !v;
            }
            uint colBg;
            uint colNub;

            if (ImGuiNET.ImGui.IsItemHovered())
            {
                colBg  = v ? ImGuiNET.ImGui.GetColorU32(ImGuiCol.ButtonHovered) : ImGuiNET.ImGui.GetColorU32(ImGuiCol.ButtonActive, 0.5f);
                colNub = v ? ImGuiNET.ImGui.GetColorU32(ImGuiCol.ScrollbarGrabActive) : ImGuiNET.ImGui.GetColorU32(ImGuiCol.ScrollbarGrabActive);
            }
            else
            {
                colBg  = v ? ImGuiNET.ImGui.GetColorU32(ImGuiCol.ButtonHovered, 0.8f) : ImGuiNET.ImGui.GetColorU32(ImGuiCol.ButtonHovered, 0.5f);
                colNub = v ? ImGuiNET.ImGui.GetColorU32(ImGuiCol.ScrollbarGrabHovered) : ImGuiNET.ImGui.GetColorU32(ImGuiCol.ScrollbarGrabHovered);
            }
            drawList.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), colBg, height * 0.5f);
            drawList.AddCircleFilled(new Vector2(v ? (p.X + width - radius) : (p.X + radius), p.Y + radius), radius - 1.5f, colNub);
            return(isClicked);
        }
Esempio n. 2
0
        //adapted from code somewhere from imgui internal
        public static bool ToggleButton(string str_id, bool isToggled, string?tooltip, bool isEnabled = true)
        {
            const uint TOGGLE_OFF_HOVER_COL   = 0xff888888;
            const uint TOGGLE_ON_HOVER_COL    = 0xff008800;
            const uint TOGGLE_OFF_NOHOVER_COL = 0xff686868;
            const uint TOGGLE_ON_NOHOVER_COL  = 0xff005500;

            Vector2       p         = ImGui.GetCursorScreenPos();
            ImDrawListPtr draw_list = ImGui.GetWindowDrawList();

            float height = ImGui.GetFrameHeight() - 2;
            float width  = height * 1.55f;
            float radius = height * 0.50f;

            ImGui.InvisibleButton(str_id, new Vector2(width, height));
            bool changed = isEnabled && ImGui.IsItemClicked();

            if (changed)
            {
                _lastActiveID      = ImGui.GetID(str_id);
                _LastActiveIdTimer = DateTime.UtcNow;
            }
            if (tooltip != null && ImGui.IsItemHovered())
            {
                ImGui.BeginTooltip();
                ImGui.Text(tooltip);
                ImGui.EndTooltip();
            }

            float t = isToggled ? 1.0f : 0.0f;

            float ANIM_SPEED = 0.08f;

            if (_lastActiveID == ImGui.GetID(str_id))
            {
                float t_anim = ImSaturate((float)(DateTime.UtcNow - _LastActiveIdTimer).TotalSeconds / ANIM_SPEED);
                t = isToggled ? (t_anim) : (1.0f - t_anim);
                if (t == 0f || t == 1.0f)
                {
                    _lastActiveID = 0;
                }
            }

            uint col_bg, col_btn;

            if (isEnabled)
            {
                if (ImGui.IsItemHovered())
                {
                    col_bg = isToggled ? TOGGLE_ON_HOVER_COL : TOGGLE_OFF_HOVER_COL;
                }
                else
                {
                    col_bg = isToggled ? TOGGLE_ON_NOHOVER_COL : TOGGLE_OFF_NOHOVER_COL;
                }

                col_btn = 0xffffffff;
            }
            else
            {
                col_btn = 0xff909090;
                col_bg  = 0xff454545;
            }
            draw_list.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), col_bg, height * 0.5f);
            draw_list.AddCircleFilled(new Vector2(p.X + radius + t * (width - radius * 2.0f), p.Y + radius), radius - 1.5f, col_btn);

            return(changed);
        }
Esempio n. 3
0
        private unsafe void RenderConnector(ImDrawListPtr list, ImConnection connection, Vector2 connector)
        {
            var hovered = IsConnectorHovered(connector);

            list.AddCircleFilled(connector, connectorRadius, (hovered ? hoveredConnectorColor : connectorColor).PackedValue);

            if ((connection.ConnectedTo.Count == 0 || connection.Input) && !OnScreen)
            {
                return;
            }

            if (!OnScreen)
            {
                var found = false;

                foreach (var c in connection.ConnectedTo)
                {
                    if (c.Parent.OnScreen)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return;
                }
            }

            if (hovered)
            {
                if (ImGui.IsMouseClicked(0))
                {
                    justStarted = true;

                    if (CurrentActive != null && CurrentActive == connection)
                    {
                        CurrentActive.Active = false;
                        CurrentActive        = null;
                    }
                    else
                    {
                        if (CurrentActive != null)
                        {
                            CurrentActive.Active = false;
                        }

                        CurrentActive        = connection;
                        CurrentActive.Active = true;

                        if (ImGuiHelper.CurrentActive == null)
                        {
                            ImGuiHelper.CurrentActive = this;
                        }
                        else
                        {
                            var active = ImGuiHelper.CurrentActive;
                            ImGuiHelper.CurrentActive = null;

                            if (active.CurrentActive != CurrentActive && active.CurrentActive.Input != CurrentActive.Input)
                            {
                                active.CurrentActive.ConnectedTo.Add(CurrentActive);
                                CurrentActive.ConnectedTo.Add(active.CurrentActive);
                            }

                            active.justStarted          = true;
                            active.CurrentActive.Active = false;
                            active.CurrentActive        = null;

                            if (CurrentActive != null)
                            {
                                CurrentActive.Active = false;
                                CurrentActive        = null;
                            }
                        }
                    }
                }
                else if (ImGui.IsMouseClicked(1) && connection.ConnectedTo.Count > 0)
                {
                    RemoveConnection(connection);
                }
            }

            if (!connection.Input && connection.ConnectedTo.Count > 0)
            {
                foreach (var to in connection.ConnectedTo)
                {
                    var p = to.Parent.Position + to.Offset;
                    DrawHermite(ImGui.GetBackgroundDrawList(), connector, p, 12, hovered || IsConnectorHovered(p) ? hoveredConnectionColor : connectionColor);
                }
            }

            if (CurrentActive == connection)
            {
                DrawHermite(ImGui.GetBackgroundDrawList(), connector, ImGui.GetIO().MousePos, 12, hovered ? hoveredConnectionColor : connectionColor);
            }
        }