From 347e480d5a0edfaca3ad34ff0f3cab935e37375c Mon Sep 17 00:00:00 2001 From: Reuh Date: Thu, 21 Apr 2016 21:51:45 +0200 Subject: [PATCH] Moved ctr.gfx.set|getTextSize to ctr.gfx.font.set|getSize, added missing documentation to font:width Started working again on the editor --- sdcard/3ds/ctruLua/editor/color.lua | 2 +- sdcard/3ds/ctruLua/editor/main.lua | 12 +++++------- sdcard/3ds/ctruLua/editor/syntax.lua | 4 ++-- sdcard/3ds/ctruLua/main.lua | 2 +- source/font.c | 27 +++++++++++++++++++++++++++ source/font.h | 2 ++ source/gfx.c | 23 ----------------------- 7 files changed, 38 insertions(+), 34 deletions(-) diff --git a/sdcard/3ds/ctruLua/editor/color.lua b/sdcard/3ds/ctruLua/editor/color.lua index 5f6ef58..ed32152 100644 --- a/sdcard/3ds/ctruLua/editor/color.lua +++ b/sdcard/3ds/ctruLua/editor/color.lua @@ -15,4 +15,4 @@ return { ["keyword.control"] = hex(0xF92672FF), ["keyword.operator"] = hex(0xF92672FF), ["support.function"] = hex(0x66D9EFFF) -} \ No newline at end of file +} diff --git a/sdcard/3ds/ctruLua/editor/main.lua b/sdcard/3ds/ctruLua/editor/main.lua index 1831568..3c001e5 100644 --- a/sdcard/3ds/ctruLua/editor/main.lua +++ b/sdcard/3ds/ctruLua/editor/main.lua @@ -35,12 +35,13 @@ local coloredLines = syntax(lines, color) -- Variables local lineHeight = 10 +local fontSize = 9 local cursorX, cursorY = 1, 1 local scrollX, scrollY = 0, 0 -- Helper functions local function displayedText(text) - return text:gsub("\t", " ") + return text:gsub("\t", " "), nil end -- Set defaults @@ -48,6 +49,7 @@ gfx.set3D(false) gfx.color.setDefault(color.default) gfx.color.setBackground(color.background) gfx.font.setDefault(font) +gfx.font.setSize(fontSize) while ctr.run() do hid.read() @@ -157,11 +159,7 @@ while ctr.run() do for _,colored in ipairs(coloredLines[i]) do local str = displayedText(colored[1]) - - gfx.color.setDefault(colored[2]) - gfx.text(x, y, str) - gfx.color.setDefault(color.default) - + gfx.text(x, y, str, fontSize, colored[2]) x = x + font:width(str) end end @@ -184,4 +182,4 @@ while ctr.run() do gfx.render() end -font:unload() \ No newline at end of file +font:unload() diff --git a/sdcard/3ds/ctruLua/editor/syntax.lua b/sdcard/3ds/ctruLua/editor/syntax.lua index bebe3f5..18a47b9 100644 --- a/sdcard/3ds/ctruLua/editor/syntax.lua +++ b/sdcard/3ds/ctruLua/editor/syntax.lua @@ -50,7 +50,7 @@ local syntax = { return function(lines, color) local ret = {} - for _,line in ipairs(lines) do + for _, line in ipairs(lines) do local colored = { { line, color.default } } for _, patterns in ipairs(syntax) do @@ -87,4 +87,4 @@ return function(lines, color) end return ret -end \ No newline at end of file +end diff --git a/sdcard/3ds/ctruLua/main.lua b/sdcard/3ds/ctruLua/main.lua index e867261..9d21667 100644 --- a/sdcard/3ds/ctruLua/main.lua +++ b/sdcard/3ds/ctruLua/main.lua @@ -12,7 +12,7 @@ local function displayError(err, trace) gfx.set3D(false) gfx.color.setBackground(0xFF0000B3) gfx.color.setDefault(0xFFFDFDFD) - gfx.setTextSize(12) + gfx.font.setSize(12) gfx.font.setDefault(gfx.font.load(ctr.root .. "resources/VeraMono.ttf")) while ctr.run() do diff --git a/source/font.c b/source/font.c index 80542c9..a29f356 100644 --- a/source/font.c +++ b/source/font.c @@ -15,6 +15,8 @@ The `font` module #include "font.h" +u32 textSize = 9; + /*** Load a TTF font. @function load @@ -70,6 +72,28 @@ static int font_getDefault(lua_State *L) { return 1; } +/*** +Set the default text size. +@function setSize +@tparam number size new default text size +*/ +static int font_setSize(lua_State *L) { + textSize = luaL_checkinteger(L, 1); + + return 0; +} + +/*** +Return the default text size. +@function getSize +@treturn number the default text size +*/ +static int font_getSize(lua_State *L) { + lua_pushinteger(L, textSize); + + return 1; +} + /*** font object @section Methods @@ -79,6 +103,7 @@ font object Return the width of a string with a font. @function :width @tparam string text the text to test +@tparam[opt=default size] integer font size, in pixels @treturn number the width of the text (in pixels) */ static int font_object_width(lua_State *L) { @@ -127,6 +152,8 @@ static const struct luaL_Reg font_lib[] = { { "load", font_load }, { "setDefault", font_setDefault }, { "getDefault", font_getDefault }, + { "setSize", font_setSize }, + { "getSize", font_getSize }, { NULL, NULL } }; diff --git a/source/font.h b/source/font.h index 888176c..2dc87ad 100644 --- a/source/font.h +++ b/source/font.h @@ -5,4 +5,6 @@ typedef struct { sftd_font *font; } font_userdata; +extern u32 textSize; + #endif diff --git a/source/gfx.c b/source/gfx.c index d172eca..447f8df 100644 --- a/source/gfx.c +++ b/source/gfx.c @@ -26,7 +26,6 @@ typedef struct { bool isGfxInitialized = false; bool is3DEnabled = false; //TODO: add a function for this in the ctrulib/sf2dlib. -u32 textSize = 9; /*** The `ctr.gfx.color` module. @@ -373,26 +372,6 @@ static int gfx_calcBoundingBox(lua_State *L) { return 2; } -/*** -Set the default text size. -@function setTextSize -@tparam number size new default text size -*/ -static int gfx_setTextSize(lua_State *L) { - textSize = luaL_checkinteger(L, 1); - return 0; -} - -/*** -Return the default text size. -@function getTextSize -@treturn number the default text size -*/ -static int gfx_getTextSize(lua_State *L) { - lua_pushinteger(L, textSize); - return 1; -} - /*** Enables or disable the scissor test. When the scissor test is enabled, the drawing area will be limited to a specific rectangle, every pixel drawn outside will be discarded. @@ -593,8 +572,6 @@ static const struct luaL_Reg gfx_lib[] = { { "text", gfx_text }, { "wrappedText", gfx_wrappedText }, { "calcBoundingBox", gfx_calcBoundingBox }, - { "setTextSize", gfx_setTextSize }, - { "getTextSize", gfx_getTextSize }, { "scissor", gfx_scissor }, { "target", gfx_target }, { "console", gfx_console },