added text y alignment, added some stuff to api

This commit is contained in:
Josh
2018-06-14 13:11:06 -05:00
parent 56f7ce79a9
commit 3b341f7123
6 changed files with 72 additions and 17 deletions

12
API.md
View File

@@ -8,13 +8,21 @@ unparent(UIBase parent, UIBase child)
JUI enums:
TextAlignment
TextXAlign
```lua
LEFT
RIGHT
CENTER
```
TextYAlign
```
TOP
BOTTOM
CENTER
```
### UIBase class
*This class is abstracted and exists only for inheritance.*
@@ -90,9 +98,11 @@ Methods:
string getFontFace()
string getText()
number getTextSize()
string, string getTextAlignment()
setFontFace(string fontFile)
setText(string text)
setTextSize(number size)
setTextAlignment(string xAlignment, string yAlignment)
```
### UIButton class

View File

@@ -16,12 +16,18 @@ return {
Utils = require("lib.utils.JUtils"),
TextAlignment = {
TextXAlign = {
LEFT = "left",
RIGHT = "right",
CENTER = "center",
},
TextYAlign = {
TOP = "top",
CENTER = "center",
BOTTOM = "bottom",
},
Orientation = {
VERTICAL = "vertical",
HORIZONTAL = "horizontal",

View File

@@ -8,6 +8,12 @@ A library that adds simple GUI elements for you to utilize in your Love2D games.
* FOSS, designed to be easily extended to fit your game's needs
* Automatic element scaling
## Planned Features
* Text Scaling
* List and Grid elements
* Scrollbar element
The full API docs can be found in the API.md file.
@@ -48,7 +54,7 @@ JUI:parent(scene, frame) -- the current method of setting the parent of an objec
-- I don't particularly like this method, and am looking for an alternative
function love.update(delta)
scene:update(delta) -- the UIContainer will recursively call update(delta) on all descendants
scene:update(delta) -- the JUIScene will recursively call update(delta) on all descendants
end
function love.draw()
@@ -56,7 +62,6 @@ function love.draw()
end
```
## Other Information
### Help

View File

@@ -7,7 +7,8 @@ function Button:init()
self.super:init()
self.text = "Button"
self.textColor = Color:new(0, 0, 0)
self.textAlignment = "left"
self.textXAlignment = "left"
self.textYAlignment = "top"
self.textSize = 12
self.font = love.graphics.newFont(12)
self.fontFace = nil
@@ -26,7 +27,7 @@ function Button:getText()
end
function Button:getTextAlignment()
return self.textAlignment
return self.textXAlignment, self.textYAlignment
end
function Button:getFontFace()
@@ -41,8 +42,9 @@ function Button:setText(text)
self.text = text
end
function Button:setTextAlignment(alignment)
self.textAlignment = alignment
function Button:setTextAlignment(xAlignment, yAlignment)
self.textXAlignment = xAlignment
self.textYAlignment = yAlignment
end
function Button:setFontFace(fontface)
@@ -63,7 +65,15 @@ function Button:render()
love.graphics.setFont(self.font)
love.graphics.setColor(self.textColor:out())
love.graphics.printf(self.text, absPos.x, absPos.y, absSize.x, self.textAlignment)
local drawYPos = absPos.y
local textHeight = self.font:getHeight()
if self.textYAlignment == "center" then drawYPos = drawYPos + (absSize.y/2) - (textHeight/2) end
if self.textYAlignment == "bottom" then drawYPos = drawYPos + absSize.y - textHeight end
love.graphics.printf(self.text, absPos.x, absPos.y, absSize.x, self.textXAlignment)
love.graphics.setFont(self.baseReturnFont)
self:renderChildren()

View File

@@ -33,7 +33,8 @@ function Label:init()
self.text = "Label"
self.textColor = Color:new(0, 0, 0)
self.textAlignment = "left"
self.textXAlignment = "left"
self.textYAlignment = "top"
self.textSize = 12
self.font = love.graphics.newFont(12)
self.fontFace = nil
@@ -52,7 +53,7 @@ function Label:getText()
end
function Label:getTextAlignment()
return self.textAlignment
return self.textXAlignment, self.textYAlignment
end
function Label:getFontFace()
@@ -67,11 +68,11 @@ function Label:setText(text)
self.text = text
end
function Label:setTextAlignment(alignment)
self.textAlignment = alignment
function Label:setTextAlignment(xAlignment, yAlignment)
self.textXAlignment = xAlignment
self.textYAlignment = yAlignment
end
function Label:setFontFace(fontface)
self.fontFace = fontface
self:recalculateInternalFont()
@@ -90,7 +91,15 @@ function Label:render()
love.graphics.setFont(self.font)
love.graphics.setColor(self.textColor:out())
love.graphics.printf(self.text, absPos.x, absPos.y, absSize.x, self.textAlignment)
local drawYPos = absPos.y
local textHeight = self.font:getHeight()
if self.textYAlignment == "center" then drawYPos = drawYPos + (absSize.y/2) - (textHeight/2) end
if self.textYAlignment == "bottom" then drawYPos = drawYPos + absSize.y - textHeight end
love.graphics.printf(self.text, absPos.x, drawYPos, absSize.x, self.textXAlignment)
love.graphics.setFont(self.baseReturnFont)
self:renderChildren()
end

View File

@@ -12,13 +12,28 @@ local mainmenu = JUI.JUIScene:new()
local testSlider = JUI.Slider:new() do -- just formatting
testSlider:setPosition(JUI.Dimension:new(0.3, 0.2))
testSlider:setSize(JUI.Dimension:new(0.4, 0.1))
testSlider:setPosition(JUI.Dimension:new(0, 0.05, 10, 10))
testSlider:setSize(JUI.Dimension:new(0.2, 0.05))
testSlider:setValueRange(2, 32)
testSlider:setValueIncrement(2)
end
local text = JUI.Label:new() do
text:setPosition(JUI.Dimension:new(0.2, 0.05, 20, 10))
text:setSize(JUI.Dimension:new(0.1, 0.05))
text:setText("Slider: "..testSlider:getValue())
text:setTextSize(18)
text:setTextAlignment(JUI.TextXAlign.CENTER, JUI.TextYAlign.CENTER)
end
testSlider.valueChanged:connect(function(newVal)
text:setText("Slider: "..newVal)
end)
JUI:parent(mainmenu, testSlider)
JUI:parent(mainmenu, text)
local windowCreationSuccess
local debugInfoString = ""