Example #1
0
void App::handleEvent(Event *e)
{
    // Handle the input
    if (e->getDispatcher() == core->getInput())
    {
        InputEvent* inputEvent = (InputEvent*)e;

        if (e->getEventCode() == InputEvent::EVENT_MOUSEWHEEL_UP)
        {
            // Zoomin by 10%
            this->camOffset.x -= this->camOffset.x * 0.1;
            if (this->camOffset.x < 0)
                this->camOffset.x = 0;
        }
        else if (e->getEventCode() == InputEvent::EVENT_MOUSEWHEEL_DOWN)
        {
            // Zoomout by 10%
            this->camOffset.x += this->camOffset.x * 0.1;
        }
        else if (e->getEventCode() == InputEvent::EVENT_KEYDOWN)
        {
            if (inputEvent->keyCode() == KEY_LEFT)
            {
                // Cycle to the left
                int index = universe->entities().indexOf(this->viewTarget);
                int size = universe->entities().size();

                if (index == 0)
                    index = size - 1;
                else
                    index -= 1;

                this->viewTarget = universe->entities().at(index);
            }
            else if (inputEvent->keyCode() == KEY_RIGHT)
            {
                // Cycle to the right
                int index = universe->entities().indexOf(this->viewTarget);
                int size = universe->entities().size();

                if (index == size - 1)
                    index = 0;
                else
                    index += 1;

                this->viewTarget = universe->entities().at(index);
            }
            else if (inputEvent->keyCode() == KEY_UP)
            {
                // Increase time simulated
                if (deltaIndex < DELTA_SIZE - 1)
                    deltaIndex++;
                this->universe->setDeltaTime(this->deltaTimes[this->deltaIndex]);
            }
            else if (inputEvent->keyCode() == KEY_DOWN)
            {
                // Decrease time simulated
                if (deltaIndex > 0)
                    this->deltaIndex--;
                this->universe->setDeltaTime(this->deltaTimes[this->deltaIndex]);
            }
        }
        else if (e->getEventCode() == InputEvent::EVENT_MOUSEDOWN)
        {
            // Right mouse button is held
            if (inputEvent->getMouseButton() == CoreInput::MOUSE_BUTTON2)
                this->mouseDown = true;
        }
        else if (e->getEventCode() == InputEvent::EVENT_MOUSEUP)
        {
            // Right mouse button is no longer held
            if (inputEvent->getMouseButton() == CoreInput::MOUSE_BUTTON2)
                this->mouseDown = false;
        }
        else if (e->getEventCode() == InputEvent::EVENT_MOUSEMOVE)
        {
            // Move the camera offset
            if (this->mouseDown)
            {
                Vector2 delta = this->core->getInput()->getMouseDelta();

                this->camOffset.y += -1 * PI * delta.y * 0.001;
                this->camOffset.z += -1 * PI * delta.x * 0.001;

                // Clamp theta to [0,PI]
                if (this->camOffset.y > PI)
                    this->camOffset.y = PI;
                else if (this->camOffset.y < 0)
                    this->camOffset.y = 0;

                // Allow phi to wrap around from [0,2*PI]
                if (this->camOffset.z < 0)
                    this->camOffset.z = 2 * PI;
                else if (this->camOffset.z > 2 * PI)
                    this->camOffset.z = 0;
            }
        }
    }
}