Implement "show_cursor" attribute in Graphics module

If false (the default), the system cursor is hidden
inside the game window.
This commit is contained in:
Jonas Kulla 2013-09-24 22:52:42 +02:00
parent a9454fdf9c
commit fe557bca1d
7 changed files with 105 additions and 41 deletions

View File

@ -100,4 +100,4 @@ To alleviate possible porting of heavily Win32API reliant scripts, I have added
* The `Input.press?` family of functions accepts three additional button constants: `::MOUSELEFT`, `::MOUSEMIDDLE` and `::MOUSERIGHT` for the respective mouse buttons. * The `Input.press?` family of functions accepts three additional button constants: `::MOUSELEFT`, `::MOUSEMIDDLE` and `::MOUSERIGHT` for the respective mouse buttons.
* The `Input` module has two additional functions, `#mouse_x` and `#mouse_y` to query the mouse pointer position relative to the game window. * The `Input` module has two additional functions, `#mouse_x` and `#mouse_y` to query the mouse pointer position relative to the game window.
* The `Graphics` module has an additional property `fullscreen`, which represents the current fullscreen mode (`true` = fullscreen, `false` = windowed). * The `Graphics` module has two additional properties: `fullscreen` represents the current fullscreen mode (`true` = fullscreen, `false` = windowed), `show_cursor` hides the system cursor inside the game window when `false`.

View File

@ -81,26 +81,26 @@ RB_METHOD(graphicsFrameReset)
return rb_fix_new(value); \ return rb_fix_new(value); \
} }
#define DEF_GRA_PROP_B(PropName) \
RB_METHOD(graphics##Get##PropName) \
{ \
RB_UNUSED_PARAM; \
return rb_bool_new(gState->graphics().get##PropName()); \
} \
RB_METHOD(graphics##Set##PropName) \
{ \
RB_UNUSED_PARAM; \
bool value; \
rb_get_args(argc, argv, "b", &value); \
gState->graphics().set##PropName(value); \
return rb_bool_new(value); \
}
DEF_GRA_PROP_I(FrameRate) DEF_GRA_PROP_I(FrameRate)
DEF_GRA_PROP_I(FrameCount) DEF_GRA_PROP_I(FrameCount)
RB_METHOD(graphicsGetFullscreen) DEF_GRA_PROP_B(Fullscreen)
{ DEF_GRA_PROP_B(ShowCursor)
RB_UNUSED_PARAM;
return rb_bool_new(gState->graphics().getFullscreen());
}
RB_METHOD(graphicsSetFullscreen)
{
RB_UNUSED_PARAM;
bool mode;
rb_get_args(argc, argv, "b", &mode);
gState->graphics().setFullscreen(mode);
return rb_bool_new(mode);
}
#define INIT_GRA_PROP_BIND(PropName, prop_name_s) \ #define INIT_GRA_PROP_BIND(PropName, prop_name_s) \
{ \ { \
@ -120,5 +120,6 @@ void graphicsBindingInit()
INIT_GRA_PROP_BIND( FrameRate, "frame_rate" ); INIT_GRA_PROP_BIND( FrameRate, "frame_rate" );
INIT_GRA_PROP_BIND( FrameCount, "frame_count" ); INIT_GRA_PROP_BIND( FrameCount, "frame_count" );
INIT_GRA_PROP_BIND( Fullscreen, "fullscreen" ); INIT_GRA_PROP_BIND( Fullscreen, "fullscreen" );
INIT_GRA_PROP_BIND( ShowCursor, "show_cursor" );
} }

View File

@ -76,28 +76,28 @@ MRB_FUNCTION(graphicsFrameReset)
mrb_int value; \ mrb_int value; \
mrb_get_args(mrb, "i", &value); \ mrb_get_args(mrb, "i", &value); \
gState->graphics().set##PropName(value); \ gState->graphics().set##PropName(value); \
return mrb_nil_value(); \ return mrb_fixnum_value(value); \
}
#define DEF_GRA_PROP_B(PropName) \
MRB_FUNCTION(graphics##Get##PropName) \
{ \
MRB_FUN_UNUSED_PARAM; \
return mrb_bool_value(gState->graphics().get##PropName()); \
} \
MRB_FUNCTION(graphics##Set##PropName) \
{ \
mrb_bool value; \
mrb_get_args(mrb, "b", &value); \
gState->graphics().set##PropName(value); \
return mrb_bool_value(value); \
} }
DEF_GRA_PROP_I(FrameRate) DEF_GRA_PROP_I(FrameRate)
DEF_GRA_PROP_I(FrameCount) DEF_GRA_PROP_I(FrameCount)
MRB_FUNCTION(graphicsGetFullscreen) DEF_GRA_PROP_B(Fullscreen)
{ DEF_GRA_PROP_B(ShowCursor)
MRB_FUN_UNUSED_PARAM;
return mrb_bool_value(gState->graphics().getFullscreen());
}
MRB_FUNCTION(graphicsSetFullscreen)
{
mrb_bool mode;
mrb_get_args(mrb, "b", &mode);
gState->graphics().setFullscreen(mode);
return mrb_bool_value(mode);
}
#define INIT_GRA_PROP_BIND(PropName, prop_name_s) \ #define INIT_GRA_PROP_BIND(PropName, prop_name_s) \
{ \ { \
@ -117,5 +117,6 @@ void graphicsBindingInit(mrb_state *mrb)
INIT_GRA_PROP_BIND( FrameRate, "frame_rate" ); INIT_GRA_PROP_BIND( FrameRate, "frame_rate" );
INIT_GRA_PROP_BIND( FrameCount, "frame_count" ); INIT_GRA_PROP_BIND( FrameCount, "frame_count" );
INIT_GRA_PROP_BIND( Fullscreen, "fullscreen" ); INIT_GRA_PROP_BIND( Fullscreen, "fullscreen" );
INIT_GRA_PROP_BIND( ShowCursor, "show_cursor" );
} }

View File

@ -53,11 +53,13 @@ enum
REQUEST_TERMINATION, REQUEST_TERMINATION,
REQUEST_SETFULLSCREEN, REQUEST_SETFULLSCREEN,
REQUEST_WINRESIZE, REQUEST_WINRESIZE,
REQUEST_MESSAGEBOX REQUEST_MESSAGEBOX,
REQUEST_SETCURSORVISIBLE
}; };
EventThread::EventThread() EventThread::EventThread()
: fullscreen(false) : fullscreen(false),
showCursor(false)
{} {}
void EventThread::process(RGSSThreadData &rtData) void EventThread::process(RGSSThreadData &rtData)
@ -68,6 +70,8 @@ void EventThread::process(RGSSThreadData &rtData)
fullscreen = rtData.config.fullscreen; fullscreen = rtData.config.fullscreen;
bool cursorInWindow = false;
bool terminate = false; bool terminate = false;
SDL_Joystick *js = 0; SDL_Joystick *js = 0;
@ -92,11 +96,28 @@ void EventThread::process(RGSSThreadData &rtData)
event.window.data2); event.window.data2);
break; break;
case SDL_WINDOWEVENT_ENTER :
cursorInWindow = true;
updateCursorState(cursorInWindow);
break;
case SDL_WINDOWEVENT_LEAVE :
cursorInWindow = false;
updateCursorState(cursorInWindow);
break;
case SDL_WINDOWEVENT_CLOSE : case SDL_WINDOWEVENT_CLOSE :
terminate = true; terminate = true;
break; break;
case SDL_WINDOWEVENT_FOCUS_GAINED :
cursorInWindow = true;
updateCursorState(cursorInWindow);
break;
case SDL_WINDOWEVENT_FOCUS_LOST : case SDL_WINDOWEVENT_FOCUS_LOST :
cursorInWindow = false;
updateCursorState(cursorInWindow);
resetInputStates(); resetInputStates();
break; break;
} }
@ -135,6 +156,11 @@ void EventThread::process(RGSSThreadData &rtData)
msgBoxDone = true; msgBoxDone = true;
break; break;
case REQUEST_SETCURSORVISIBLE :
showCursor = event.user.code;
updateCursorState(cursorInWindow);
break;
case SDL_KEYUP : case SDL_KEYUP :
keyStates[event.key.keysym.scancode] = false; keyStates[event.key.keysym.scancode] = false;
break; break;
@ -214,6 +240,14 @@ void EventThread::setFullscreen(SDL_Window *win, bool mode)
fullscreen = mode; fullscreen = mode;
} }
void EventThread::updateCursorState(bool inWindow)
{
if (inWindow)
SDL_ShowCursor(showCursor ? SDL_TRUE : SDL_FALSE);
else
SDL_ShowCursor(SDL_TRUE);
}
void EventThread::requestTerminate() void EventThread::requestTerminate()
{ {
SDL_Event event; SDL_Event event;
@ -241,6 +275,14 @@ void EventThread::requestWindowResize(int width, int height)
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
void EventThread::requestShowCursor(bool mode)
{
SDL_Event event;
event.type = REQUEST_SETCURSORVISIBLE;
event.user.code = mode;
SDL_PushEvent(&event);
}
void EventThread::showMessageBox(const char *body, int flags) void EventThread::showMessageBox(const char *body, int flags)
{ {
msgBoxDone = false; msgBoxDone = false;
@ -257,7 +299,12 @@ void EventThread::showMessageBox(const char *body, int flags)
resetInputStates(); resetInputStates();
} }
bool EventThread::getFullscreen() bool EventThread::getFullscreen() const
{ {
return fullscreen; return fullscreen;
} }
bool EventThread::getShowCursor() const
{
return showCursor;
}

View File

@ -69,17 +69,21 @@ public:
/* Called from rgss thread */ /* Called from rgss thread */
void requestFullscreenMode(bool mode); void requestFullscreenMode(bool mode);
void requestWindowResize(int width, int height); void requestWindowResize(int width, int height);
void requestShowCursor(bool mode);
void requestTerminate(); void requestTerminate();
bool getFullscreen(); bool getFullscreen() const;
bool getShowCursor() const;
void showMessageBox(const char *body, int flags = 0); void showMessageBox(const char *body, int flags = 0);
private: private:
void resetInputStates(); void resetInputStates();
void setFullscreen(SDL_Window *, bool mode); void setFullscreen(SDL_Window *, bool mode);
void updateCursorState(bool inWindow);
bool fullscreen; bool fullscreen;
bool showCursor;
volatile bool msgBoxDone; volatile bool msgBoxDone;
}; };

View File

@ -869,6 +869,16 @@ void Graphics::setFullscreen(bool value)
p->threadData->ethread->requestFullscreenMode(value); p->threadData->ethread->requestFullscreenMode(value);
} }
bool Graphics::getShowCursor() const
{
return p->threadData->ethread->getShowCursor();
}
void Graphics::setShowCursor(bool value)
{
p->threadData->ethread->requestShowCursor(value);
}
Scene *Graphics::getScreen() const Scene *Graphics::getScreen() const
{ {
return &p->screen; return &p->screen;

View File

@ -59,6 +59,7 @@ public:
/* Non-standard extension */ /* Non-standard extension */
DECL_ATTR( Fullscreen, bool ) DECL_ATTR( Fullscreen, bool )
DECL_ATTR( ShowCursor, bool )
/* <internal> */ /* <internal> */
Scene *getScreen() const; Scene *getScreen() const;