stetre / moonnuklear

Lua bindings for Nuklear
Other
110 stars 10 forks source link

Need example of changing font size after window creation #5

Closed awsdert closed 4 years ago

awsdert commented 4 years ago

Found the doc detailing how to set font size before the window is created but can't find any info on after it is created. Also getting nil when I try to retrieve the default font despite moving the variable declaration in backend.lua to a place I could access outside of the module, was there a function for that? If so please include in the example.

stetre commented 4 years ago

The backend.lua is just an example, you can make a local copy and modify it as you want to suit your needs.

Regarding the font size, you don't change it after creation. What you do instead is create different 'font' objects during initialisation, one for each type+size you are interested in, and then when you are drawing the widgets you swap them in and out. You actually push and pop them on a stack of fonts, like so:

-- ...
-- using the default font
-- ...
nk.style_push_font(ctx, myfont14)
   -- ...
   --  using myfont14 
   -- ...
   nk.style_push_font(ctx, myfont16)
      -- ...
      --  using myfont16 
      -- ...
   nk.style_pop_font(ctx)
   -- ...
   --  back to using myfont14 
   -- ...
nk.style_pop_font(ctx)
-- ...
-- back to using the default font
-- ...

You can also simply 'set' a font using nk.style_set_font(), which you would typically do during initialisation to set the default to use one among those you created.

stetre commented 4 years ago

I added a simple example, fontchange.lua. Hope that helps.

awsdert commented 4 years ago

ty :) already made local copy and started modifying though :D at work atm so 1st chance I get to try will be 2mw at earliest, ty very much for the time being though :)

awsdert commented 4 years ago

Finally got round to trying out that fontchange.lua file, I commented out my original lua code and just dumped the contents of the fonchange.lua file above the comment and ran it as is, that gave me an error at line 46 I think it was when it was trying to add the first font, I then changed the DEFAULT_FONT_PATH variable to match that of my system ('/usr/share/fonts/TTF') and that gave me an error also, I thought it might be because of the lack of '/' character at the end but I also got an error that way too so I went back to without the separator for now and copied the error message:

NK_ASSERT failed: nuklear/nuklear.h line 13510

Since your more familiar with nuklear, gl, glfw and obviously moon libs, do you haveany idea what this means?

awsdert commented 4 years ago

Took some time to get to the line from the github page but this is what I've found there:

NK_ASSERT(config->ttf_size); Taking a look through the atlas:add bit of the code, figured maybe your copy is not upto date and the line as actually to do with pathing so I'm changing the paths to be prefixed with DEFAULT_FONT_PATH instead

awsdert commented 4 years ago

Nope, no matter what I tried it didn't work,here's what I've changed the section to so far:

local GASP_FONT_DIR = '/usr/share/fonts/TTF'
local GASP_FONT_1EM = 20
...
function gasp_add_font(name)
    if size == "large" then
        size = GASP_FONT_1EM + 4
    elseif size == "small" then
        size = GASP_FONT_1EM - 4
    else
        size = GASP_FONT_1EM
    end
    if not name then
        name = GASP_FONT_DIR
    else
        name = GASP_FONT_DIR .. "/" .. name
    end
    return atlas:add( size, name )
end
local fonts = {
    default = gasp_add_font(),
    droid = gasp_add_font( "DroidSans.ttf" ),
    droid_big = gasp_add_font( "DroidSans.ttf", "large" ),
    roboto = gasp_add_font( "Roboto-Regular.ttf" ),
    future = gasp_add_font( "kenvector_future_thin.ttf" ),
    clean = gasp_add_font( "ProggyClean.ttf" ),
    tiny = gasp_add_font( "ProggyTiny.ttf" ),
    cousine = gasp_add_font( "Cousine-Regular.ttf" )
}
stetre commented 4 years ago

The DEFAULT_FONT_PATH in the example should be the full path name of a ttf file, not a directory.

awsdert commented 4 years ago

After further investigating it turned out droid fonts were not in the same directory as that one I was giving so I made some changes but still get the same error, here's the updated function (with the mistake of lost argument fixed)

function gasp_add_font(name,size,dir)
    if not dir then
        dir = GASP_FONT_DIR
    else
        dir = ROOT_FONT_DIR .. '/' .. dir
    end
    if size == "large" then
        size = GASP_FONT_1EM + 4
    elseif size == "small" then
        size = GASP_FONT_1EM - 4
    else
        size = GASP_FONT_1EM
    end
    if not name then
        name = dir .. "/"
    else
        name = dir .. "/" .. name
    end
    print( "Adding font '" .. name .. "' at size " .. size )
    return atlas:add( size, name )
end

And here's the output of the "succession" of calls

Adding font '/usr/share/fonts/TTF/' at size 20
stetre commented 4 years ago

See my previous comment. The atlas:add() function expects as second argument either nil or the full path name of a ttf file. The error, which is raised internally by nuklear, is most likely caused by the fact that you are passing it a directory path instead.

EDIT: It actually need not be a 'full' path name. It may as well be relative to the working directory, as in the examples. It must be the name of a ttf file, nonetheless.

awsdert commented 4 years ago

Ah, was a bit late to see that comment, anyways just you said I needed to change that default one to an actual font file, once I got past that (and change the font change code to reflect the actual fonts I have) I got a different problem, the elements don't render, here's what I got:

function add_font_label(ctx,name)
    nk.style_push_font(ctx, fonts[name])
    nk.label(ctx, "Using font '" .. name .. "'", nk.TEXT_CENTERED)
end

function rem_font_label(ctx)
    nk.style_pop_font(ctx)
    nk.label(ctx, "Back to using font '" .. name .. "'", nk.TEXT_CENTERED)
end

while not glfw.window_should_close(window) do
   glfw.wait_events_timeout(1/FPS) --glfw.poll_events()
   backend.new_frame()

   -- Draw the GUI --------------------------------------------
   if nk.window_begin(ctx, "GUI", {50, 50, 320, 220}, window_flags) then
      nk.layout_row_dynamic(ctx, 20, 1)
      nk.label(ctx, "Using default font 'default'", nk.TEXT_CENTERED)
      add_font_label(ctx,"droid_large")
      add_font_label(ctx,"noto_large")
      add_font_label(ctx,"noto")
      add_font_label(ctx,"noto_small")
      rem_font_label(ctx,"noto")
      rem_font_label(ctx,"noto_large")
      rem_font_label(ctx,"droid_large")
      rem_font_label(ctx,"default")
   end
   nk.window_end(ctx)

Since I doubt the problem is there what function should I look for to look for problems at? While I wait for a response I'll go help my neighbour like I usually do each week, after I'll look through the commented code for clues if you haven't responded already.

stetre commented 4 years ago

Hard to say from here without seeing it and without the full code. What do you mean by the "elements don't render"? You get a black window?

By the way, I'd say to first try executing the original fontchange.lua (with the original backend.lua). If it works (hopefully), then check the changes you've made. Maybe you accidentally erased some essential instruction, like the glfw.swap_buffers() call, for example.

awsdert commented 4 years ago

I get the main window but everything that should be behind the window is still visible (albeit off position) and then the window just closes on me, btw just got back

awsdert commented 4 years ago

Still haven't found what is going wrong, I uploaded the file just now, could you take a look please? https://github.com/awsdert/gasp/blob/master/lua/gasp.lua

stetre commented 4 years ago

The only error I can see is in the rem_font_label(ctx) function, where you use name which is not in the argument list. Other than that, the code seems correct to me. I tried it on my machine (replacing the fonts) and it works as expected.

Does the original unmodified fontchange.lua work correctly on your machine?

awsdert commented 4 years ago

that's a whoopsie and no the original won't work because I don't have some of those installed, besides the code I'm using is basically a copy past into functions instead (did so to more easily swap out font names), btw the fix didn't resolve the render issue

awsdert commented 4 years ago

Well I stuck a whole bunch of print statements in trying to find out if it stops midway and it seems the app exits during the attempt to end the font stash

awsdert commented 4 years ago

Ah apparently it was the CJK fonts, I did this:

    noto = gasp_add_font( "NotoSansDisplay-Regular.ttf", nil, "noto" ),
    noto_large = gasp_add_font( "NotoSansDisplay-Regular.ttf", "large", "noto" ),
    noto_small = gasp_add_font( "NotoSansDisplay-Regular.ttf", "small", "noto" )
    --[[
    cjk = gasp_add_font( "NotoSansCJK-Regular.ttc", nil, "noto-cjk" ),
    cjk_large = gasp_add_font( "NotoSansCJK-Regular.ttc", "large", "noto-cjk" ),
    cjk_small = gasp_add_font( "NotoSansCJK-Regular.ttc", "small", "noto-cjk" )
    ]]

And the issue went away, I thought *.ttc was a variant of TrueType and would simply work with minor glitches here and there

stetre commented 4 years ago

Ah, ok. Good to know, for future reference.

BTW, the ttf files for the fonts used in the examples are included in the MoonNuklear repository (in the moonnuklear/examples/fonts directory). All the examples should work out of the box when launched from the examples dir.

awsdert commented 4 years ago

At last achieved what I was looking for, at least on a simpler level, prefer a more advanced but I suppose nuklear needs more development for that to happen, anyways shove this link in the fontchange.lua file for peops to see a button based version that is confirmed to work on manjaro with X11

Whoops, forgot to actually paste the link: https://github.com/awsdert/gasp/tree/427023dbe9c6dc149fe99e1be47784fd571cb388

stetre commented 4 years ago

I don't see nuklear becoming much more advanced than this, since it's simple and minimal by design.

Out of curiosity I tried to build your project on my Ubuntu 18.04 machine (tbh, I don't even know what a cheat engine is...) but the linker failed on target private_gasp.elf, complaining for undefined references to the Lua C API functions. Do you want me to open an issue?

awsdert commented 4 years ago

Well the -llua flag is set, did you simply run make in the directory or did you manually change something? I can't imagine that lua would be missing from your computer unless like me you have to reinstall your os occasionally or are you using windows? Edit: Ah never mind, I forgot that when I was making full debug builds I moved the -llua flag to just the normal versions, currently switching it all back to just the original lua & moon libs now

stetre commented 4 years ago

Ok, I'll try again tomorrow (now I'll call it a day... it's time for supper here where I live).

awsdert commented 4 years ago

That's fine, I should have uploaded the changes I'm making my end anyway by then

stetre commented 4 years ago

I tried again just now and the linker is still complaining.

I noticed that you use liblua as a shared library, but building it from the original sources (as I do) yields only the static library (Lua by default provides no support for the shared object).

awsdert commented 4 years ago

Ah that'll be it then, have a look for lua in pacman or whatever you have (the one that actually runs it) I think I'm linking to that atm, haven't experienced any issues even after cleaning all objects etc out and recompiling so I will keep that in mind and probably mention it in the readme if installing lua fixes your linker problem

awsdert commented 4 years ago

I've tried making some changes and did a complete rebuild, experienced no problems my end, if you still haven't installed the 'lua' package then give this a try 1st https://github.com/awsdert/gasp/tree/e321507c1fb5e718741f6e133cab623bf98aec79 Just run 'make rebuild' in the root directory (I'll work getting it to work in the other directories later)

stetre commented 4 years ago

Linker still complaining. It might work if you move -l options to the end of the clang parameters list. For example, I manually tried this:

clang -fPIC -o gasp.elf gasp.c.o space.c.o nodes.c.o proc.c.o arguments.c.o -ldl -lm -lpthread -llua

instead of this:

clang -fPIC -ldl -lm -lpthread -llua -o gasp.elf gasp.c.o space.c.o nodes.c.o proc.c.o arguments.c.o

and it didn't complain.

awsdert commented 4 years ago

Alright I'll give that a try when I get home from work