aboutsummaryrefslogtreecommitdiff
path: root/Simulation/SimBase_Execute.cpp
blob: eb3166a2f043bc85d6b22c10b594c49515efd323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "SimBase.hpp"

void SimBase::execute()
{
    while (window.isOpen())
    {
        update();

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            if (event.type == sf::Event::Resized)
            {
                camera.onResize();
            }

            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Button::Left)
                {
                    camera.onClick();
                }
            }

            if (event.type == sf::Event::MouseWheelMoved)
            {
                sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
                float f1 = 2.f / 10.f;
                float f2 = 1.f - f1;

                if (event.mouseWheel.delta > 0)
                {
                    sf::Vector2f target = camera.getCrds() * f2 + mousePos * f1;
                    camera.shift(target, 1);
                }
                else
                {
                    mousePos = camera.getCrds() * 2.f - mousePos;
                    sf::Vector2f target = camera.getCrds() * f2 + mousePos * f1;
                    camera.shift(target, -1);
                }
            }

            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Escape)
                {
                    window.close();
                }

                if (event.key.code == sf::Keyboard::Space)
                {
                    paused = !paused;
                    text.simState.setString("Sim. state: " + std::string(paused ? "PAUSED" : "RUNNING"));
                }

                if (event.key.code == sf::Keyboard::G)
                {
                    gfx = !gfx;
                    text.gfx.setString("Graphics: " + std::string(gfx ? "ON" : "OFF"));
                }

                if (event.key.code == sf::Keyboard::T)
                {
                    displayText = !displayText;
                }

                if (event.key.code == sf::Keyboard::V)
                {
                    vSync = !vSync;
                    window.setVerticalSyncEnabled(vSync);
                    text.vSync.setString("V. Sync: " + std::string(vSync ? "ON" : "OFF"));
                }

                if (event.key.code == sf::Keyboard::Z)
                {
                    camera.shift(sf::Vector2f(), -2);
                }

                if (event.key.code == sf::Keyboard::F11)
                {
                    sf::ContextSettings ctx = window.getSettings();
                    std::string title = "Neural Guppies - 0.1 beta";

                    if (fullscreen)
                    {
                        fullscreen = false;
                        window.create(sf::VideoMode(prms.WIN_WIDTH, prms.WIN_HEIGHT), title, sf::Style::Default, ctx);
                        sf::Image icon;
                        icon.loadFromFile("gfx/icon.png");
                        window.setIcon(32, 32, icon.getPixelsPtr());
                    }
                    else
                    {
                        fullscreen = true;
                        window.create(sf::VideoMode::getDesktopMode(), title, sf::Style::Fullscreen, ctx);
                    }

                    window.setVerticalSyncEnabled(vSync);
                    camera.onResize();
                }

                if (event.key.code == sf::Keyboard::I)
                {
                    camera.shift(camera.getCrds(), 1, false);
                }

                if (event.key.code == sf::Keyboard::O)
                {
                    camera.shift(camera.getCrds(), -1, false);
                }

                if ( event.key.code == sf::Keyboard::W ||
                     event.key.code == sf::Keyboard::A ||
                     event.key.code == sf::Keyboard::S ||
                     event.key.code == sf::Keyboard::D )
                {
                    float shift = 30.f * camera.getZoom();
                    sf::Vector2f shiftVec;

                    if (event.key.code == sf::Keyboard::W)
                    {
                        shiftVec.y = -shift;
                    }
                    else if (event.key.code == sf::Keyboard::A)
                    {
                        shiftVec.x = -shift;
                    }
                    else if (event.key.code == sf::Keyboard::S)
                    {
                        shiftVec.y = shift;
                    }
                    else if (event.key.code == sf::Keyboard::D)
                    {
                        shiftVec.x = shift;
                    }

                    camera.shift(camera.getCrds() + shiftVec);
                }
            }
        }

        draw();
    }

    //onClose();
}