Exemplo n.º 1
0
        public bool update(Cause cause, MouseEventArgs m, KeyEventArgs k)
        {
            // null m XOR k if the cause isn't relevant
            switch (cause)
            {
            case Cause.MOUSE_MOVE:
                for (int i = 0; i < objects.Length; i++)
                {
                    int  x   = (int)(m.X * (1280 / (float)main.getSize().Width));
                    int  y   = (int)(m.Y * (720 / (float)main.getSize().Height));
                    bool val = objects[i].isHovering(new Point(x, y));

                    selVector[i] = val;
                    objects[i].setSelect(val);
                }
                break;

            case Cause.KEY_PRESS:
                // SET CONTROLS
                Keys[] controls = main.getSettings().getControls();
                Keys[] up       = new Keys[] { controls[0], Keys.Up };
                Keys[] down     = new Keys[] { controls[1], Keys.Down };
                Keys[] left     = new Keys[] { controls[2], Keys.Left };
                Keys[] right    = new Keys[] { controls[3], Keys.Right };

                // FIND SELECTED
                int selected = -1;
                for (int i = 0; i < objects.Length; i++)
                {
                    if (selVector[i])
                    {
                        selected = i;
                        break;
                    }
                }

                if (selected == -1)
                {
                    bool assignedSel = false;

                    for (int i = 0; i < objects.Length; i++)
                    {
                        if (objects[i].canBeSelected() && !assignedSel)
                        {
                            selVector[i]     = true;
                            lastSelVector[i] = true;
                            objects[i].setSelect(true);
                            assignedSel = true;
                        }
                        else
                        {
                            selVector[i]     = false;
                            lastSelVector[i] = false;
                            objects[i].setSelect(false);
                        }
                    }
                    return(true);
                }

                int    candIndex       = selected;
                double closestDistance = Double.MaxValue;

                for (int i = 0; i < objects.Length; i++)
                {
                    if (i != selected && objects[i].canBeSelected())
                    {
                        Point old       = objects[selected].getLocation();
                        Point cand      = objects[i].getLocation();
                        bool  direction = false;
                        // Principal direction
                        if (k.KeyCode == up[0] || k.KeyCode == up[1])
                        {
                            // UP
                            direction = cand.Y < old.Y;
                            //direction = cand.Y < old.Y &&
                            //    Math.Abs(cand.Y - old.Y) >
                            //    Math.Abs(cand.X - old.X);
                        }
                        else if (k.KeyCode == down[0] ||
                                 k.KeyCode == down[1])
                        {
                            // DOWN
                            direction = cand.Y > old.Y;
                            //direction = cand.Y > old.Y &&
                            //    Math.Abs(cand.Y - old.Y) >
                            //    Math.Abs(cand.X - old.X);
                        }
                        else if (k.KeyCode == left[0] ||
                                 k.KeyCode == left[1])
                        {
                            // LEFT
                            direction = cand.X < old.X;
                            //direction = cand.X < old.X &&
                            //    Math.Abs(cand.Y - old.Y) <
                            //    Math.Abs(cand.X - old.X);
                        }
                        else if (k.KeyCode == right[0] ||
                                 k.KeyCode == right[1])
                        {
                            // RIGHT
                            direction = cand.X > old.X;
                            //direction = cand.X > old.X &&
                            //    Math.Abs(cand.Y - old.Y) <
                            //    Math.Abs(cand.X - old.X);
                        }

                        if (direction &&
                            MathExt.Distance(old, cand) < closestDistance)
                        {
                            candIndex       = i;
                            closestDistance = MathExt.Distance(old, cand);
                        }
                    }
                }

                if (candIndex != selected)
                {
                    for (int i = 0; i < selVector.Length; i++)
                    {
                        selVector[i] = (i == candIndex);
                        objects[i].setSelect(i == candIndex);
                    }
                }

                break;
            }

            bool different = false;

            for (int i = 0; i < selVector.Length; i++)
            {
                different |= (selVector[i] != lastSelVector[i]);
                // Safe as index has already been compared
                lastSelVector[i] = selVector[i];
            }

            return(different);
        }
Exemplo n.º 2
0
 public void changeSpeed(int inc)
 {
     speed = MathExt.Bounded(2, speed + inc, 14);
 }