Pārlūkot izejas kodu

set font size on runtime

John Dodis 1 gadu atpakaļ
vecāks
revīzija
7f610bd8b1
2 mainītis faili ar 32 papildinājumiem un 4 dzēšanām
  1. 7 3
      main.lua
  2. 25 1
      ui2d/ui2d.lua

+ 7 - 3
main.lua

@@ -16,7 +16,7 @@ local progress = { value = 0, adder = 0 }
 local txt1 = "Αυτό είναι utf8 κείμενο"
 
 function lovr.load()
-	UI2D.Init( 14 )
+	UI2D.Init()
 end
 
 function lovr.update( dt )
@@ -62,8 +62,12 @@ function lovr.draw( pass )
 	UI2D.Label( "We're doing progress...", true )
 	UI2D.ProgressBar( progress.value )
 	UI2D.Separator()
-	UI2D.Button( "first button2" )
-	UI2D.Button( "first button2" )
+	if UI2D.Button( "Font size +" ) then
+		UI2D.SetFontSize( UI2D.GetFontSize() + 1 )
+	end
+	if UI2D.Button( "Font size -" ) then
+		UI2D.SetFontSize( UI2D.GetFontSize() - 1 )
+	end
 	released, sl1 = UI2D.SliderInt( "a slider", sl1, 0, 100 )
 	if released then
 		print( released, sl1 )

+ 25 - 1
ui2d/ui2d.lua

@@ -109,6 +109,9 @@ color_themes.light =
 
 local colors = color_themes.dark
 
+-- -------------------------------------------------------------------------- --
+--                             Internals                                      --
+-- -------------------------------------------------------------------------- --
 local function Clamp( n, n_min, n_max )
 	if n < n_min then
 		n = n_min
@@ -296,13 +299,19 @@ function lovr.keyreleased( key, scancode )
 	repeating_key = nil
 end
 
----------------------------------------------------------------
+-- -------------------------------------------------------------------------- --
+--                                User                                        --
+-- -------------------------------------------------------------------------- --
 function UI2D.Init( size )
 	font.handle = lovr.graphics.newFont( "ui2d/" .. "DejaVuSansMono.ttf", size or 14, 4 )
 	font.handle:setPixelDensity( 1.0 )
 	font.h = font.handle:getHeight()
 	font.w = font.handle:getWidth( "W" )
+	font.size = size or 14
 	lovr.system.setKeyRepeat( true )
+
+	margin = math.floor( font.h / 2 )
+	separator_thickness = math.floor( font.h / 7 )
 end
 
 function UI2D.InputInfo()
@@ -551,6 +560,21 @@ function UI2D.ResetColor( col_name )
 	end
 end
 
+function UI2D.SetFontSize( size )
+	font.handle = lovr.graphics.newFont( "ui2d/" .. "DejaVuSansMono.ttf", size, 4 )
+	font.handle:setPixelDensity( 1.0 )
+	font.h = font.handle:getHeight()
+	font.w = font.handle:getWidth( "W" )
+	font.size = size
+
+	margin = math.floor( font.h / 2 )
+	separator_thickness = math.floor( font.h / 7 )
+end
+
+function UI2D.GetFontSize()
+	return font.size
+end
+
 function UI2D.SameLine()
 	layout.same_line = true
 end