diff --git a/game/engines/default/engine/Mouse.lua b/game/engines/default/engine/Mouse.lua index ae2d854860512c3db98c4288574cfb0b0064a42a..57cc697506ffe3f7468407a7d37825f680d92fcf 100644 --- a/game/engines/default/engine/Mouse.lua +++ b/game/engines/default/engine/Mouse.lua @@ -60,8 +60,8 @@ function _M:setCurrent() end --- Registers a click zone that when clicked will call the object's "onClick" method -function _M:registerZone(x, y, w, h, fct, mode) - table.insert(self.areas, 1, {x1=x,y1=y,x2=x+w,y2=y+h, fct=fct, mode}) +function _M:registerZone(x, y, w, h, fct, mode, name) + table.insert(self.areas, 1, {x1=x,y1=y,x2=x+w,y2=y+h, fct=fct, mode, name=name}) end function _M:registerZones(t) diff --git a/game/engines/default/engine/utils.lua b/game/engines/default/engine/utils.lua index 504eab53a72898dc89a96399f882b3337e09f986..f18bffdc35bd93db460423badf3ab480151aebca 100644 --- a/game/engines/default/engine/utils.lua +++ b/game/engines/default/engine/utils.lua @@ -103,7 +103,7 @@ function table.reverse(t) end function table.listify(t) local tt = {} - for k, e in pairs(t) do tt[#tt+1] = {k, e} print("listify", #tt, k, e) end + for k, e in pairs(t) do tt[#tt+1] = {k, e} end return tt end @@ -162,10 +162,12 @@ local Puid_cap = "UID:" * lpeg.C(lpeg.R"09"^1) * ":" * lpeg.C(lpeg.R"09") local Pcolorname = (lpeg.R"AZ" + "_")^3 local Pcode = (lpeg.R"af" + lpeg.R"09" + lpeg.R"AF") local Pcolorcode = Pcode * Pcode +local Pfontstyle = "{" * (lpeg.P"bold" + lpeg.P"italic" + lpeg.P"underline" + lpeg.P"normal") * "}" +local Pfontstyle_cap = "{" * lpeg.C(lpeg.P"bold" + lpeg.P"italic" + lpeg.P"underline" + lpeg.P"normal") * "}" local Pcolorcodefull = Pcolorcode * Pcolorcode * Pcolorcode function string.removeColorCodes(str) - return str:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", "") + return str:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", "") end function string.splitLine(str, max_width, font) @@ -173,7 +175,7 @@ function string.splitLine(str, max_width, font) local lines = {} local cur_line, cur_size = "", 0 for _, v in ipairs(str:split(lpeg.S"\n ")) do - local shortv = v:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", "") + local shortv = v:lpegSub("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", "") local w, h = font:size(shortv) if cur_size + space_w + w < max_width then @@ -247,7 +249,7 @@ end local tmps = core.display.newSurface(1, 1) getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g, b) - local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", true) + local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", true) r = r or 255 g = g or 255 b = b or 255 @@ -257,6 +259,7 @@ getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g, local nr, ng, nb = lpeg.match("#" * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * "#", v) local col = lpeg.match("#" * lpeg.C(Pcolorname) * "#", v) local uid, mo = lpeg.match("#" * Puid_cap * "#", v) + local fontstyle = lpeg.match("#" * Pfontstyle_cap * "#", v) if nr and ng and nb then oldr, oldg, oldb = r, g, b r, g, b = nr:parseHex(), ng:parseHex(), nb:parseHex() @@ -278,6 +281,8 @@ getmetatable(tmps).__index.drawColorString = function(s, font, str, x, y, r, g, if h > max_h then max_h = h end x = x + (w or 0) end + elseif fontstyle then + font:setStyle(fontstyle) else local w, h = font:size(v) if h > max_h then max_h = h end @@ -296,7 +301,7 @@ end getmetatable(tmps).__index.drawColorStringBlended = function(s, font, str, x, y, r, g, b) - local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname) * "#", true) + local list = str:split("#" * (Puid + Pcolorcodefull + Pcolorname + Pfontstyle) * "#", true) r = r or 255 g = g or 255 b = b or 255 @@ -306,6 +311,7 @@ getmetatable(tmps).__index.drawColorStringBlended = function(s, font, str, x, y, local nr, ng, nb = lpeg.match("#" * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * lpeg.C(Pcolorcode) * "#", v) local col = lpeg.match("#" * lpeg.C(Pcolorname) * "#", v) local uid, mo = lpeg.match("#" * Puid_cap * "#", v) + local fontstyle = lpeg.match("#" * Pfontstyle_cap * "#", v) if nr and ng and nb then oldr, oldg, oldb = r, g, b r, g, b = nr:parseHex(), ng:parseHex(), nb:parseHex() @@ -327,6 +333,8 @@ getmetatable(tmps).__index.drawColorStringBlended = function(s, font, str, x, y, if h > max_h then max_h = h end x = x + (w or 0) end + elseif fontstyle then + font:setStyle(fontstyle) else local w, h = font:size(v) if h > max_h then max_h = h end @@ -586,3 +594,22 @@ function util.uuid() local uuid = tpl:gsub("[xy]", function(c) if c=='y' then return rng.table(y) else return rng.table(x) end end) return uuid end + +function util.browserOpenUrl(url) + local tries = { + "rundll32 url.dll,FileProtocolHandler %s", -- Windows + "open %s", -- OSX + "xdg-open %s", -- Linux - portable way + "gnome-open %s", -- Linux - Gnome + "kde-open %s", -- Linux - Kde + "firefox %s", -- Linux - try to find something + "mozilla-firefox %s", -- Linux - try to find something + } + while #tries > 0 do + local urlbase = table.remove(tries, 1) + urlbase = urlbase:format(url) + print("Trying to run URL with command: ", urlbase) + if os.execute(urlbase) == 0 then return true end + end + return false +end diff --git a/game/engines/default/special/mainmenu/class/Game.lua b/game/engines/default/special/mainmenu/class/Game.lua index 8a8ebce93a64ffc407012f2aea20d01dede6f5cc..d0cc59c3a32dcea014482eed3a4914e7a80092d1 100644 --- a/game/engines/default/special/mainmenu/class/Game.lua +++ b/game/engines/default/special/mainmenu/class/Game.lua @@ -45,12 +45,12 @@ function _M:run() self.mod_list = Module:listModules() self.save_list = Module:listSavefiles() - -- Setup display - self:selectStepMain() - self:checkLogged() self:engineVersion() + -- Setup display + self:selectStepMain() + -- Ok everything is good to go, activate the game in the engine! self:setCurrent() @@ -61,8 +61,15 @@ end function _M:checkLogged() if profile.auth then - self.s_log = core.display.drawStringBlendedNewSurface(self.profile_font, "Online Profile: "..profile.auth.name.."[http://te4.org/players/"..profile.auth.page.."]", 255, 255, 0) + self.logged_url = "http://te4.org/players/"..profile.auth.page + local str = "Online Profile: "..profile.auth.name.."[#LIGHT_BLUE##{underline}#"..self.logged_url.."#LAST##{normal}#]" + local plain = str:removeColorCodes() + local w, h = self.profile_font:size(plain) + self.s_log = core.display.newSurface(w, h) + self.s_log:erase(0, 0, 0) + self.s_log:drawColorStringBlended(self.profile_font, str, 0, 0, 255, 255, 0) else + self.logged_url = nil self.s_log = nil end end @@ -212,6 +219,7 @@ function _M:selectStepMain() self.news.text = env.text print("Latest engine version available: ", env.version[4], env.version[1], env.version[2], env.version[3]) self.latest_engine_version = env.version + if env.link then self.news.link = env.link end else self.news = nil end @@ -233,7 +241,11 @@ Now go and have some fun!]] } end - self.tooltip:set("#AQUAMARINE#%s#WHITE#\n---\n%s", self.news.title, self.news.text) + if self.news.link then + self.tooltip:set("#AQUAMARINE#%s#WHITE#\n---\n%s\n---\n#LIGHT_BLUE##{underline}#%s#LAST##{normal}#", self.news.title, self.news.text, self.news.link) + else + self.tooltip:set("#AQUAMARINE#%s#WHITE#\n---\n%s", self.news.title, self.news.text) + end end self.step.do_tooltip = true @@ -248,6 +260,19 @@ Now go and have some fun!]] self.step:setKeyHandling() self.step:setMouseHandling() + + if self.s_log then + local w, h = self.s_log:getSize() + self.mouse:registerZone(self.w - w, self.h - h, w, h, function(button) + if button == "left" then util.browserOpenUrl(self.logged_url) end + end, {button=true}) + end + + if self.news.link then + self.mouse:registerZone(5, self.tooltip.h - 30, self.tooltip.w, 30, function(button) + if button == "left" then util.browserOpenUrl(self.news.link) end + end, {button=true}) + end end function _M:selectStepNew() diff --git a/src/core_lua.c b/src/core_lua.c index 55f2ace9eb49e727d9194532a71a620ff0182b25..f6fce6950adeaccaaf665857ce15950607906917 100644 --- a/src/core_lua.c +++ b/src/core_lua.c @@ -505,6 +505,18 @@ static int sdl_font_lineskip(lua_State *L) return 1; } +static int sdl_font_style(lua_State *L) +{ + TTF_Font **f = (TTF_Font**)auxiliar_checkclass(L, "sdl{font}", 1); + const char *style = luaL_checkstring(L, 2); + + if (!strcmp(style, "normal")) TTF_SetFontStyle(*f, 0); + else if (!strcmp(style, "bold")) TTF_SetFontStyle(*f, TTF_STYLE_BOLD); + else if (!strcmp(style, "italic")) TTF_SetFontStyle(*f, TTF_STYLE_ITALIC); + else if (!strcmp(style, "underline")) TTF_SetFontStyle(*f, TTF_STYLE_UNDERLINE); + return 0; +} + static int sdl_surface_drawstring(lua_State *L) { SDL_Surface **s = (SDL_Surface**)auxiliar_checkclass(L, "sdl{surface}", 1); @@ -1378,6 +1390,7 @@ static const struct luaL_reg sdl_font_reg[] = {"size", sdl_font_size}, {"height", sdl_font_height}, {"lineSkip", sdl_font_lineskip}, + {"setStyle", sdl_font_style}, {NULL, NULL}, };