Merged default into minor

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2014-07-30 01:32:10 -03:00
42 changed files with 640 additions and 356 deletions
+5 -5
View File
@@ -110,14 +110,14 @@ StringMap<Window::FullscreenType, Window::FULLSCREEN_TYPE_MAX_ENUM>::Entry Windo
StringMap<Window::FullscreenType, Window::FULLSCREEN_TYPE_MAX_ENUM> Window::fullscreenTypes(Window::fullscreenTypeEntries, sizeof(Window::fullscreenTypeEntries));
StringMap<MessageBoxType, MESSAGEBOX_MAX_ENUM>::Entry Window::messageBoxTypeEntries[] =
StringMap<Window::MessageBoxType, Window::MESSAGEBOX_MAX_ENUM>::Entry Window::messageBoxTypeEntries[] =
{
{"error", MESSAGEBOX_ERROR},
{"warning", MESSAGEBOX_WARNING},
{"info", MESSAGEBOX_INFO},
{"error", Window::MESSAGEBOX_ERROR},
{"warning", Window::MESSAGEBOX_WARNING},
{"info", Window::MESSAGEBOX_INFO},
};
StringMap<MessageBoxType, MESSAGEBOX_MAX_ENUM> Window::messageBoxTypes(Window::messageBoxTypeEntries, sizeof(Window::messageBoxTypeEntries));
StringMap<Window::MessageBoxType, Window::MESSAGEBOX_MAX_ENUM> Window::messageBoxTypes(Window::messageBoxTypeEntries, sizeof(Window::messageBoxTypeEntries));
} // window
} // love
+27 -9
View File
@@ -39,14 +39,6 @@ namespace window
// whole thing here because it uses the Window::Type enum.
struct WindowSettings;
enum MessageBoxType
{
MESSAGEBOX_ERROR,
MESSAGEBOX_WARNING,
MESSAGEBOX_INFO,
MESSAGEBOX_MAX_ENUM
};
class Window : public Module
{
public:
@@ -76,14 +68,39 @@ public:
FULLSCREEN_TYPE_MAX_ENUM
};
enum MessageBoxType
{
MESSAGEBOX_ERROR,
MESSAGEBOX_WARNING,
MESSAGEBOX_INFO,
MESSAGEBOX_MAX_ENUM
};
struct WindowSize
{
int width;
int height;
};
struct MessageBoxData
{
MessageBoxType type;
std::string title;
std::string message;
std::vector<std::string> buttons;
int enterButtonIndex;
int escapeButtonIndex;
bool attachToWindow;
};
virtual ~Window();
// Implements Module.
virtual ModuleType getModuleType() const { return M_WINDOW; }
virtual bool setWindow(int width = 800, int height = 600, WindowSettings *settings = nullptr) = 0;
virtual void getWindow(int &width, int &height, WindowSettings &settings) = 0;
@@ -128,7 +145,8 @@ public:
virtual const void *getHandle() const = 0;
virtual void showMessageBox(MessageBoxType type, const char *title, const char *message) = 0;
virtual bool showMessageBox(MessageBoxType type, const std::string &title, const std::string &message, bool attachtowindow) = 0;
virtual int showMessageBox(const MessageBoxData &data) = 0;
//virtual static Window *createSingleton() = 0;
//virtual static Window *getSingleton() = 0;
+62 -15
View File
@@ -98,11 +98,20 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
else
{
sdlflags |= SDL_WINDOW_FULLSCREEN;
SDL_DisplayMode mode = {0, width, height, 0, 0};
// Fullscreen window creation will bug out if no mode can be used.
SDL_DisplayMode mode = {0, width, height, 0, 0};
if (SDL_GetClosestDisplayMode(f.display, &mode, &mode) == 0)
return false;
if (SDL_GetClosestDisplayMode(f.display, &mode, &mode) == nullptr)
{
// GetClosestDisplayMode will fail if we request a size larger
// than the largest available display mode, so we'll try to use
// the largest (first) mode in that case.
if (SDL_GetDisplayMode(f.display, 0, &mode) < 0)
return false;
}
width = mode.w;
height = mode.h;
}
}
@@ -118,7 +127,7 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
sdlflags |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif
graphics::Graphics *gfx = (graphics::Graphics *) Module::findInstance("love.graphics.");
graphics::Graphics *gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
if (gfx != nullptr)
gfx->unSetMode();
@@ -431,7 +440,7 @@ bool Window::setFullscreen(bool fullscreen, Window::FullscreenType fstype)
updateSettings(newsettings);
// Update the viewport size now instead of waiting for event polling.
graphics::Graphics *gfx = (graphics::Graphics *) Module::findInstance("love.graphics.");
graphics::Graphics *gfx = Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS);
if (gfx != nullptr)
{
int width = curMode.width;
@@ -668,25 +677,63 @@ const void *Window::getHandle() const
return window;
}
void Window::showMessageBox(MessageBoxType type, const char *title, const char *message)
SDL_MessageBoxFlags Window::convertMessageBoxType(MessageBoxType type) const
{
SDL_MessageBoxFlags sdlflags;
switch (type)
{
case MESSAGEBOX_ERROR:
sdlflags = SDL_MESSAGEBOX_ERROR;
break;
return SDL_MESSAGEBOX_ERROR;
case MESSAGEBOX_WARNING:
sdlflags = SDL_MESSAGEBOX_WARNING;
break;
return SDL_MESSAGEBOX_WARNING;
case MESSAGEBOX_INFO:
default:
sdlflags = SDL_MESSAGEBOX_INFORMATION;
break;
return SDL_MESSAGEBOX_INFORMATION;
}
}
bool Window::showMessageBox(MessageBoxType type, const std::string &title, const std::string &message, bool attachtowindow)
{
SDL_MessageBoxFlags flags = convertMessageBoxType(type);
SDL_Window *sdlwindow = attachtowindow ? window : nullptr;
return SDL_ShowSimpleMessageBox(flags, title.c_str(), message.c_str(), sdlwindow) >= 0;
}
int Window::showMessageBox(const MessageBoxData &data)
{
SDL_MessageBoxData sdldata = {};
sdldata.flags = convertMessageBoxType(data.type);
sdldata.title = data.title.c_str();
sdldata.message = data.message.c_str();
sdldata.window = data.attachToWindow ? window : nullptr;
sdldata.numbuttons = (int) data.buttons.size();
std::vector<SDL_MessageBoxButtonData> sdlbuttons;
for (size_t i = 0; i < data.buttons.size(); i++)
{
SDL_MessageBoxButtonData sdlbutton = {};
sdlbutton.buttonid = (int) i;
sdlbutton.text = data.buttons[i].c_str();
if ((int) i == data.enterButtonIndex)
sdlbutton.flags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
if ((int) i == data.escapeButtonIndex)
sdlbutton.flags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
sdlbuttons.push_back(sdlbutton);
}
SDL_ShowSimpleMessageBox(sdlflags, title, message, window);
sdldata.buttons = &sdlbuttons[0];
int pressedbutton = -2;
SDL_ShowMessageBox(&sdldata, &pressedbutton);
return pressedbutton;
}
love::window::Window *Window::createSingleton()
+4 -1
View File
@@ -84,7 +84,8 @@ public:
const void *getHandle() const;
void showMessageBox(MessageBoxType type, const char *title, const char *message);
bool showMessageBox(MessageBoxType type, const std::string &title, const std::string &message, bool attachtowindow);
int showMessageBox(const MessageBoxData &data);
static love::window::Window *createSingleton();
static love::window::Window *getSingleton();
@@ -99,6 +100,8 @@ private:
// Update the saved window settings based on the window's actual state.
void updateSettings(const WindowSettings &newsettings);
SDL_MessageBoxFlags convertMessageBoxType(MessageBoxType type) const;
std::string windowTitle;
struct _currentMode
+83 -21
View File
@@ -26,11 +26,11 @@ namespace love
namespace window
{
static Window *instance = nullptr;
#define instance() (Module::getInstance<Window>(Module::M_WINDOW))
int w_getDisplayCount(lua_State *L)
{
lua_pushinteger(L, instance->getDisplayCount());
lua_pushinteger(L, instance()->getDisplayCount());
return 1;
}
@@ -39,7 +39,7 @@ int w_getDisplayName(lua_State *L)
int index = luaL_checkint(L, 1) - 1;
const char *name = nullptr;
luax_catchexcept(L, [&](){ name = instance->getDisplayName(index); });
luax_catchexcept(L, [&](){ name = instance()->getDisplayName(index); });
lua_pushstring(L, name);
return 1;
@@ -59,7 +59,7 @@ int w_setMode(lua_State *L)
if (lua_isnoneornil(L, 3))
{
luax_pushboolean(L, instance->setWindow(w, h, 0));
luax_pushboolean(L, instance()->setWindow(w, h, 0));
return 1;
}
@@ -113,7 +113,7 @@ int w_setMode(lua_State *L)
settings.display--;
luax_catchexcept(L,
[&](){ luax_pushboolean(L, instance->setWindow(w, h, &settings)); }
[&](){ luax_pushboolean(L, instance()->setWindow(w, h, &settings)); }
);
return 1;
@@ -123,7 +123,7 @@ int w_getMode(lua_State *L)
{
int w, h;
WindowSettings settings;
instance->getWindow(w, h, settings);
instance()->getWindow(w, h, settings);
lua_pushnumber(L, w);
lua_pushnumber(L, h);
@@ -176,7 +176,7 @@ int w_getFullscreenModes(lua_State *L)
{
int displayindex = luaL_optint(L, 1, 1) - 1;
std::vector<Window::WindowSize> modes = instance->getFullscreenSizes(displayindex);
std::vector<Window::WindowSize> modes = instance()->getFullscreenSizes(displayindex);
lua_createtable(L, modes.size(), 0);
@@ -212,9 +212,9 @@ int w_setFullscreen(lua_State *L)
bool success = false;
if (fstype == Window::FULLSCREEN_TYPE_MAX_ENUM)
success = instance->setFullscreen(fullscreen);
success = instance()->setFullscreen(fullscreen);
else
success = instance->setFullscreen(fullscreen, fstype);
success = instance()->setFullscreen(fullscreen, fstype);
luax_pushboolean(L, success);
return 1;
@@ -224,7 +224,7 @@ int w_getFullscreen(lua_State *L)
{
int w, h;
WindowSettings settings;
instance->getWindow(w, h, settings);
instance()->getWindow(w, h, settings);
const char *typestr;
if (!Window::getConstant(settings.fstype, typestr))
@@ -237,7 +237,7 @@ int w_getFullscreen(lua_State *L)
int w_isCreated(lua_State *L)
{
luax_pushboolean(L, instance->isCreated());
luax_pushboolean(L, instance()->isCreated());
return 1;
}
@@ -245,7 +245,7 @@ int w_getDesktopDimensions(lua_State *L)
{
int width = 0, height = 0;
int displayindex = luaL_optint(L, 1, 1) - 1;
instance->getDesktopDimensions(displayindex, width, height);
instance()->getDesktopDimensions(displayindex, width, height);
lua_pushinteger(L, width);
lua_pushinteger(L, height);
return 2;
@@ -254,13 +254,13 @@ int w_getDesktopDimensions(lua_State *L)
int w_setIcon(lua_State *L)
{
image::ImageData *i = luax_checktype<image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
luax_pushboolean(L, instance->setIcon(i));
luax_pushboolean(L, instance()->setIcon(i));
return 1;
}
int w_getIcon(lua_State *L)
{
image::ImageData *i = instance->getIcon();
image::ImageData *i = instance()->getIcon();
if (i)
{
i->retain();
@@ -274,46 +274,106 @@ int w_getIcon(lua_State *L)
int w_setTitle(lua_State *L)
{
std::string title = luax_checkstring(L, 1);
instance->setWindowTitle(title);
instance()->setWindowTitle(title);
return 0;
}
int w_getTitle(lua_State *L)
{
luax_pushstring(L, instance->getWindowTitle());
luax_pushstring(L, instance()->getWindowTitle());
return 1;
}
int w_hasFocus(lua_State *L)
{
luax_pushboolean(L, instance->hasFocus());
luax_pushboolean(L, instance()->hasFocus());
return 1;
}
int w_hasMouseFocus(lua_State *L)
{
luax_pushboolean(L, instance->hasMouseFocus());
luax_pushboolean(L, instance()->hasMouseFocus());
return 1;
}
int w_isVisible(lua_State *L)
{
luax_pushboolean(L, instance->isVisible());
luax_pushboolean(L, instance()->isVisible());
return 1;
}
int w_getPixelScale(lua_State *L)
{
lua_pushnumber(L, instance->getPixelScale());
lua_pushnumber(L, instance()->getPixelScale());
return 1;
}
int w_minimize(lua_State* /*L*/)
{
instance->minimize();
instance()->minimize();
return 0;
}
int w_showMessageBox(lua_State *L)
{
Window::MessageBoxData data = {};
const char *typestr = luaL_checkstring(L, 1);
if (!Window::getConstant(typestr, data.type))
return luaL_error(L, "Invalid messagebox type: %s", typestr);
data.title = luaL_checkstring(L, 2);
data.message = luaL_checkstring(L, 3);
// If we have a table argument, we assume a list of button names, which
// means we should use the more complex message box API.
if (lua_istable(L, 4))
{
size_t numbuttons = lua_objlen(L, 4);
if (numbuttons == 0)
return luaL_error(L, "Must have at least one messagebox button.");
// Array of button names.
for (size_t i = 0; i < numbuttons; i++)
{
lua_rawgeti(L, 4, i + 1);
data.buttons.push_back(luax_checkstring(L, -1));
lua_pop(L, 1);
}
// Optional table entry specifying the button to use when enter is pressed.
lua_getfield(L, 4, "enterbutton");
if (!lua_isnoneornil(L, -1))
data.enterButtonIndex = luaL_checkint(L, -1) - 1;
else
data.enterButtonIndex = 0;
lua_pop(L, 1);
// Optional table entry specifying the button to use when esc is pressed.
lua_getfield(L, 4, "escapebutton");
if (!lua_isnoneornil(L, -1))
data.escapeButtonIndex = luaL_checkint(L, -1) - 1;
else
data.escapeButtonIndex = (int) data.buttons.size() - 1;
lua_pop(L, 1);
data.attachToWindow = luax_optboolean(L, 5, true);
int pressedbutton = instance()->showMessageBox(data);
lua_pushinteger(L, pressedbutton + 1);
}
else
{
data.attachToWindow = luax_optboolean(L, 4, true);
// Display a simple message box.
bool success = instance()->showMessageBox(data.type, data.title, data.message, data.attachToWindow);
luax_pushboolean(L, success);
}
return 1;
}
static const luaL_Reg functions[] =
{
{ "getDisplayCount", w_getDisplayCount },
@@ -334,11 +394,13 @@ static const luaL_Reg functions[] =
{ "isVisible", w_isVisible },
{ "getPixelScale", w_getPixelScale },
{ "minimize", w_minimize },
{ "showMessageBox", w_showMessageBox },
{ 0, 0 }
};
extern "C" int luaopen_love_window(lua_State *L)
{
Window *instance = nullptr;
luax_catchexcept(L, [&](){ instance = sdl::Window::createSingleton(); });
WrappedModule w;
+1
View File
@@ -47,6 +47,7 @@ int w_hasMouseFocus(lua_State *L);
int w_isVisible(lua_State *L);
int w_getPixelScale(lua_State *L);
int w_minimize(lua_State *L);
int w_showMessageBox(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_window(lua_State *L);
} // window