~vcs-imports/ember/master

« back to all changes in this revision

Viewing changes to src/services/input/Input.cpp

  • Committer: Erik Ogenvik
  • Date: 2019-11-10 13:55:41 UTC
  • Revision ID: git-v1:88b8cd7f93863a4db3fc477a818550cd71df6950
Make int to float conversion more explicit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
76
76
                mScreenHeight(0),
77
77
                mMainVideoSurface(nullptr),
78
78
                mIconSurface(nullptr),
79
 
                mInvertMouse(1),
 
79
                mInvertMouse(false),
80
80
                mHandleOpenGL(false),
81
81
                mMainWindowId(0),
82
82
                mLastTimeInputProcessingStart(microsec_clock::local_time()),
104
104
        }
105
105
}
106
106
 
107
 
std::string Input::createWindow(unsigned int width, unsigned int height, bool fullscreen, bool resizable, bool centered, bool handleOpenGL) {
 
107
std::string Input::createWindow(unsigned int width, unsigned int height, bool fullscreen, bool resizable, bool handleOpenGL) {
108
108
 
109
109
 
110
110
        mHandleOpenGL = handleOpenGL;
127
127
                mMainVideoSurface = nullptr;
128
128
                mMainWindowId = 0;
129
129
        }
130
 
        mMainVideoSurface = SDL_CreateWindow("Ember", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int) width, (int) height, flags); // create an SDL window
 
130
        mMainVideoSurface = SDL_CreateWindow("Ember",
 
131
                                                                                 SDL_WINDOWPOS_CENTERED,
 
132
                                                                                 SDL_WINDOWPOS_CENTERED,
 
133
                                                                                 (int) width,
 
134
                                                                                 (int) height,
 
135
                                                                                 flags); // create an SDL window
131
136
        createIcon();
132
137
 
133
138
        mMainWindowId = SDL_GetWindowID(mMainVideoSurface);
386
391
}
387
392
 
388
393
void Input::startInteraction() {
389
 
        mConfigListenerContainer->registerConfigListenerWithDefaults("input", "catchmouse", sigc::mem_fun(*this, &Input::Config_CatchMouse), true);
390
 
        mConfigListenerContainer->registerConfigListenerWithDefaults("input", "invertcamera", sigc::mem_fun(*this, &Input::Config_InvertCamera), true);
 
394
        mConfigListenerContainer->registerConfigListenerWithDefaults("input",
 
395
                                                                                                                                 "catchmouse",
 
396
                                                                                                                                 sigc::mem_fun(*this, &Input::Config_CatchMouse),
 
397
                                                                                                                                 true);
 
398
        mConfigListenerContainer->registerConfigListenerWithDefaults("input",
 
399
                                                                                                                                 "invertcamera",
 
400
                                                                                                                                 sigc::mem_fun(*this, &Input::Config_InvertCamera),
 
401
                                                                                                                                 true);
391
402
}
392
403
 
393
404
void Input::processInput() {
433
444
                }
434
445
                if (mouseRelativeX != 0 || mouseRelativeY != 0) {
435
446
 
 
447
 
436
448
                        //we'll calculate the mouse movement difference and send the values to those
437
449
                        //listening to the MouseMoved event
438
 
                        float diffX, diffY;
439
 
                        diffX = mouseRelativeX / (float) mScreenWidth;
440
 
                        diffY = mouseRelativeY / (float) mScreenHeight;
 
450
                        float diffX = (float) mouseRelativeX / (float) mScreenWidth;
 
451
                        float diffY = (float) mouseRelativeY / (float) mScreenHeight;
441
452
                        MouseMotion motion{};
442
453
                        motion.xPosition = mouseX;
443
454
                        motion.yPosition = mouseY;
444
 
                        motion.xRelativeMovement = diffX * mInvertMouse;
445
 
                        motion.yRelativeMovement = diffY * mInvertMouse;
446
 
                        motion.xRelativeMovementInPixels = mouseRelativeX * mInvertMouse;
447
 
                        motion.yRelativeMovementInPixels = mouseRelativeY * mInvertMouse;
 
455
                        motion.xRelativeMovement = mInvertMouse ? -diffX : diffX;
 
456
                        motion.yRelativeMovement = mInvertMouse ? -diffY : diffY;
 
457
                        motion.xRelativeMovementInPixels = mInvertMouse ? -mouseRelativeX : mouseRelativeX;
 
458
                        motion.yRelativeMovementInPixels = mInvertMouse ? -mouseRelativeY : mouseRelativeY;
448
459
                        motion.timeSinceLastMovement = secondsSinceLast;
449
460
 
450
461
                        EventMouseMoved.emit(motion, mCurrentInputMode);
471
482
                        } else {
472
483
                                mMousePosition.xPixelPosition = mouseX;
473
484
                                mMousePosition.yPixelPosition = mouseY;
474
 
                                mMousePosition.xRelativePosition = mouseX / (float) mScreenWidth;
475
 
                                mMousePosition.yRelativePosition = mouseY / (float) mScreenHeight;
 
485
                                mMousePosition.xRelativePosition = (float) mouseX / (float) mScreenWidth;
 
486
                                mMousePosition.yRelativePosition = (float) mouseY / (float) mScreenHeight;
476
487
                        }
477
488
 
478
489
                }
622
633
                utf8::utf8to32(text, text + len, std::back_inserter(utf32result));
623
634
                if (!utf32result.empty()) {
624
635
                        int character = utf32result.front();
625
 
                        for (IInputAdapterStore::const_iterator I = mAdapters.begin(); I != mAdapters.end() && !mSuppressForCurrentEvent;) {
 
636
                        for (auto I = mAdapters.begin(); I != mAdapters.end() && !mSuppressForCurrentEvent;) {
626
637
                                IInputAdapter* adapter = *I;
627
638
                                ++I;
628
639
                                if (!(adapter)->injectChar(character)) {
751
762
 
752
763
void Input::Config_InvertCamera(const std::string& section, const std::string& key, varconf::Variable& variable) {
753
764
        if (variable.is_bool()) {
754
 
                mInvertMouse = static_cast<bool>(variable) ? -1 : 1;
 
765
                mInvertMouse = static_cast<bool>(variable);
755
766
        }
756
767
}
757
768