Fix wallpaper handling a bit; fix screen shaking; spaces -> tabs

This commit is contained in:
KockaAdmiralac 2018-03-12 10:50:58 +01:00
parent 7292edd7ca
commit 0b48ead68c
2 changed files with 67 additions and 62 deletions

View File

@ -10,37 +10,36 @@
RB_METHOD(oneshotSetYesNo) RB_METHOD(oneshotSetYesNo)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
const char *yes; const char *yes;
const char *no; const char *no;
rb_get_args(argc, argv, "zz", &yes, &no RB_ARG_END); rb_get_args(argc, argv, "zz", &yes, &no RB_ARG_END);
shState->oneshot().setYesNo(yes, no); shState->oneshot().setYesNo(yes, no);
return Qnil; return Qnil;
} }
RB_METHOD(oneshotMsgBox) RB_METHOD(oneshotMsgBox)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
int type; int type;
VALUE body; VALUE body;
VALUE title = Qnil; VALUE title = Qnil;
rb_get_args(argc, argv, "iS|S", &type, &body, &title RB_ARG_END); rb_get_args(argc, argv, "iS|S", &type, &body, &title RB_ARG_END);
std::string bodyStr = std::string(RSTRING_PTR(body), RSTRING_LEN(body)); std::string bodyStr = std::string(RSTRING_PTR(body), RSTRING_LEN(body));
std::string titleStr = (title == Qnil) ? "" : std::string(RSTRING_PTR(title), RSTRING_LEN(title)); std::string titleStr = (title == Qnil) ? "" : std::string(RSTRING_PTR(title), RSTRING_LEN(title));
return rb_bool_new(shState->oneshot().msgbox(type, bodyStr.c_str(), titleStr.c_str())); return rb_bool_new(shState->oneshot().msgbox(type, bodyStr.c_str(), titleStr.c_str()));
} }
RB_METHOD(oneshotTextInput) RB_METHOD(oneshotTextInput)
{ {
// const char* prompt, int char_limit, const char* font RB_UNUSED_PARAM;
RB_UNUSED_PARAM; VALUE prompt;
VALUE prompt; int char_limit = 100;
int char_limit = 100; VALUE font = Qnil;
VALUE font = Qnil; rb_get_args(argc, argv, "S|iS", &prompt, &char_limit, &font RB_ARG_END);
rb_get_args(argc, argv, "S|iS", &prompt, &char_limit, &font RB_ARG_END); std::string promptStr = std::string(RSTRING_PTR(prompt), RSTRING_LEN(prompt));
std::string promptStr = std::string(RSTRING_PTR(prompt), RSTRING_LEN(prompt)); std::string fontStr = (font == Qnil) ? "" : std::string(RSTRING_PTR(font), RSTRING_LEN(font));
std::string fontStr = (font == Qnil) ? "" : std::string(RSTRING_PTR(font), RSTRING_LEN(font)); return rb_str_new2(shState->oneshot().textinput(promptStr.c_str(), char_limit, fontStr.c_str()).c_str());
return rb_str_new2(shState->oneshot().textinput(promptStr.c_str(), char_limit, fontStr.c_str()).c_str());
} }
RB_METHOD(oneshotResetObscured) RB_METHOD(oneshotResetObscured)
@ -67,11 +66,11 @@ RB_METHOD(oneshotAllowExit)
RB_METHOD(oneshotExiting) RB_METHOD(oneshotExiting)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
bool exiting; bool exiting;
rb_get_args(argc, argv, "b", &exiting RB_ARG_END); rb_get_args(argc, argv, "b", &exiting RB_ARG_END);
shState->oneshot().setExiting(exiting); shState->oneshot().setExiting(exiting);
return Qnil; return Qnil;
} }
RB_METHOD(oneshotShake) RB_METHOD(oneshotShake)
@ -79,52 +78,54 @@ RB_METHOD(oneshotShake)
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
int absx, absy; int absx, absy;
SDL_GetWindowPosition(shState->rtData().window, &absx, &absy); SDL_GetWindowPosition(shState->rtData().window, &absx, &absy);
int state;
srand(time(NULL)); srand(time(NULL));
for (int i = 0; i < 60; ++i) { for (int i = 0; i < 60; ++i) {
int max = 60 - i; int max = 60 - i;
int x = rand() % (max * 2) - max; int x = rand() % (max * 2) - max;
int y = rand() % (max * 2) - max; int y = rand() % (max * 2) - max;
SDL_SetWindowPosition(shState->rtData().window, absx + x, absy + y); SDL_SetWindowPosition(shState->rtData().window, absx + x, absy + y);
rb_eval_string_protect("sleep 0.02", &state);
} }
return Qnil; return Qnil;
} }
RB_METHOD(oneshotCRC32) RB_METHOD(oneshotCRC32)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
VALUE string; VALUE string;
boost::crc_32_type result; boost::crc_32_type result;
rb_get_args(argc, argv, "S", &string RB_ARG_END); rb_get_args(argc, argv, "S", &string RB_ARG_END);
std::string str = std::string(RSTRING_PTR(string), RSTRING_LEN(string)); std::string str = std::string(RSTRING_PTR(string), RSTRING_LEN(string));
result.process_bytes(str.data(), str.length()); result.process_bytes(str.data(), str.length());
return UINT2NUM(result.checksum()); return UINT2NUM(result.checksum());
} }
void oneshotBindingInit() void oneshotBindingInit()
{ {
VALUE module = rb_define_module("Oneshot"); VALUE module = rb_define_module("Oneshot");
VALUE msg = rb_define_module_under(module, "Msg"); VALUE msg = rb_define_module_under(module, "Msg");
//Constants // Constants
rb_const_set(module, rb_intern("USER_NAME"), rb_str_new2(shState->oneshot().userName().c_str())); rb_const_set(module, rb_intern("USER_NAME"), rb_str_new2(shState->oneshot().userName().c_str()));
rb_const_set(module, rb_intern("SAVE_PATH"), rb_str_new2(shState->oneshot().savePath().c_str())); rb_const_set(module, rb_intern("SAVE_PATH"), rb_str_new2(shState->oneshot().savePath().c_str()));
rb_const_set(module, rb_intern("DOCS_PATH"), rb_str_new2(shState->oneshot().docsPath().c_str())); rb_const_set(module, rb_intern("DOCS_PATH"), rb_str_new2(shState->oneshot().docsPath().c_str()));
rb_const_set(module, rb_intern("GAME_PATH"), rb_str_new2(shState->oneshot().gamePath().c_str())); rb_const_set(module, rb_intern("GAME_PATH"), rb_str_new2(shState->oneshot().gamePath().c_str()));
rb_const_set(module, rb_intern("JOURNAL"), rb_str_new2(shState->oneshot().journal().c_str())); rb_const_set(module, rb_intern("JOURNAL"), rb_str_new2(shState->oneshot().journal().c_str()));
rb_const_set(module, rb_intern("LANG"), rb_str_new2(shState->oneshot().lang().c_str())); rb_const_set(module, rb_intern("LANG"), rb_str_new2(shState->oneshot().lang().c_str()));
rb_const_set(msg, rb_intern("INFO"), INT2FIX(Oneshot::MSG_INFO)); rb_const_set(msg, rb_intern("INFO"), INT2FIX(Oneshot::MSG_INFO));
rb_const_set(msg, rb_intern("YESNO"), INT2FIX(Oneshot::MSG_YESNO)); rb_const_set(msg, rb_intern("YESNO"), INT2FIX(Oneshot::MSG_YESNO));
rb_const_set(msg, rb_intern("WARN"), INT2FIX(Oneshot::MSG_WARN)); rb_const_set(msg, rb_intern("WARN"), INT2FIX(Oneshot::MSG_WARN));
rb_const_set(msg, rb_intern("ERR"), INT2FIX(Oneshot::MSG_ERR)); rb_const_set(msg, rb_intern("ERR"), INT2FIX(Oneshot::MSG_ERR));
//Functions // Functions
_rb_define_module_function(module, "set_yes_no", oneshotSetYesNo); _rb_define_module_function(module, "set_yes_no", oneshotSetYesNo);
_rb_define_module_function(module, "msgbox", oneshotMsgBox); _rb_define_module_function(module, "msgbox", oneshotMsgBox);
_rb_define_module_function(module, "textinput", oneshotTextInput); _rb_define_module_function(module, "textinput", oneshotTextInput);
_rb_define_module_function(module, "reset_obscured", oneshotResetObscured); _rb_define_module_function(module, "reset_obscured", oneshotResetObscured);
_rb_define_module_function(module, "obscured_cleared?", oneshotObscuredCleared); _rb_define_module_function(module, "obscured_cleared?", oneshotObscuredCleared);
_rb_define_module_function(module, "allow_exit", oneshotAllowExit); _rb_define_module_function(module, "allow_exit", oneshotAllowExit);
_rb_define_module_function(module, "exiting", oneshotExiting); _rb_define_module_function(module, "exiting", oneshotExiting);
_rb_define_module_function(module, "shake", oneshotShake); _rb_define_module_function(module, "shake", oneshotShake);
_rb_define_module_function(module, "crc32", oneshotCRC32); _rb_define_module_function(module, "crc32", oneshotCRC32);
} }

View File

@ -33,23 +33,24 @@ static bool isCached = false;
RB_METHOD(wallpaperSet) RB_METHOD(wallpaperSet)
{ {
RB_UNUSED_PARAM; RB_UNUSED_PARAM;
const char *imageName; const char *name;
int color; int color;
rb_get_args(argc, argv, "zi", &imageName, &color RB_ARG_END); rb_get_args(argc, argv, "zi", &name, &color RB_ARG_END);
std::string imgname = shState->config().gameFolder + "/Wallpaper/" + imageName + ".bmp"; std::string path;
#ifdef _WIN32 #ifdef _WIN32
path = shState->config().gameFolder + "\\Wallpaper\\" + name + ".bmp";
// Crapify the slashes // Crapify the slashes
size_t index = 0; size_t index = 0;
for (;;) { for (;;) {
index = imgname.find("/", index); index = path.find("/", index);
if (index == std::string::npos) if (index == std::string::npos)
break; break;
imgname.replace(index, 1, "\\"); path.replace(index, 1, "\\");
index += 1; index += 1;
} }
WCHAR imgnameW[MAX_PATH]; WCHAR imgnameW[MAX_PATH];
WCHAR imgnameFull[MAX_PATH]; WCHAR imgnameFull[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, imgname.c_str(), -1, imgnameW, MAX_PATH); MultiByteToWideChar(CP_UTF8, 0, path.c_str(), -1, imgnameW, MAX_PATH);
GetFullPathNameW(imgnameW, MAX_PATH, imgnameFull, NULL); GetFullPathNameW(imgnameW, MAX_PATH, imgnameFull, NULL);
@ -105,23 +106,26 @@ end:
if (hKey) if (hKey)
RegCloseKey(hKey); RegCloseKey(hKey);
#else #else
std::size_t found = imgname.find("w32"); std::string nameFix(name);
if (found != std::string::npos) imgname.replace(imgname.end()-3, imgname.end(), "unix"); std::size_t found = nameFix.find("w32");
imgname = shState->config().gameFolder + "/Wallpaper/" + imageName + ".png"; if (found != std::string::npos) {
nameFix.replace(nameFix.end()-3, nameFix.end(), "unix");
}
path = "/Wallpaper/" + nameFix + ".png";
#ifdef __APPLE__ #ifdef __APPLE__
if (!isCached) { if (!isCached) {
MacDesktop::CacheCurrentBackground(); MacDesktop::CacheCurrentBackground();
isCached = true; isCached = true;
} }
MacDesktop::ChangeBackground(imgname, ((color >> 16) & 0xFF) / 255.0, ((color >> 8) & 0xFF) / 255.0, ((color) & 0xFF) / 255.0); MacDesktop::ChangeBackground(shState->config().gameFolder + path, ((color >> 16) & 0xFF) / 255.0, ((color >> 8) & 0xFF) / 255.0, ((color) & 0xFF) / 255.0);
#else #else
char gameDir[1024]; char gameDir[1024];
if (getcwd(gameDir, sizeof(gameDir)) != NULL) { if (getcwd(gameDir, sizeof(gameDir)) != NULL) {
std::string gameDirStr(gameDir); std::string gameDirStr(gameDir);
std::stringstream hexColor; std::stringstream hexColor;
hexColor << "#" << std::hex << color; hexColor << "#" << std::hex << color;
bgsetting->set_string("picture-uri", "file://" + gameDirStr + "/Wallpaper/" + imageName + ".png"); bgsetting->set_string("picture-uri", "file://" + gameDirStr + path);
bgsetting->set_string("picture-options", "scaled"); bgsetting->set_string("picture-options", "scaled");
bgsetting->set_string("primary-color", hexColor.str()); bgsetting->set_string("primary-color", hexColor.str());
} else { } else {