From b2fadd2ed1e392f51ff65acdf85ce6dc0a83546b Mon Sep 17 00:00:00 2001 From: Harry Felton Date: Fri, 26 May 2017 17:25:47 +1200 Subject: [PATCH] Classes can now specify a 'tmlContent' argument to be used. When an instance of the class has been spawned with content inside of a TML file, the content will be assigned to the argument name specified inside 'tmlContent' (for example, the content of a TML element may be assigned to 'text') --- example/ui/master.tml | 70 +++++++++++++++++++------------------- src/classes/nodes/Label.ti | 18 +++------- src/classes/utils/TML.ti | 6 +++- src/mixins/MTextDisplay.ti | 11 +++++- src/scripts/Class.lua | 2 ++ 5 files changed, 57 insertions(+), 50 deletions(-) diff --git a/example/ui/master.tml b/example/ui/master.tml index db3c1c0..5d352f2 100644 --- a/example/ui/master.tml +++ b/example/ui/master.tml @@ -7,33 +7,33 @@ - - + - + - - diff --git a/src/classes/nodes/Label.ti b/src/classes/nodes/Label.ti index fa09160..a29ecee 100644 --- a/src/classes/nodes/Label.ti +++ b/src/classes/nodes/Label.ti @@ -2,7 +2,7 @@ A Label is a node which displays a single line of text. The text cannot be changed by the user directly, however the text can be changed by the program. ]] -class Label extends Node { +class Label extends Node mixin MTextDisplay { labelFor = false; allowMouse = true; @@ -19,6 +19,10 @@ function Label:__init__( ... ) self:super() self:register "text" + + self:watchProperty( "text", function( self, key, value ) + self.width = #value + end ) end --[[ @@ -60,18 +64,6 @@ function Label:draw( force ) end end ---[[ - @instance - @desc Sets the text of a node. Once set, the nodes 'changed' status is set to true along with its parent(s) - @param -]] -function Label:setText( text ) - if self.text == text then return end - - self.text = text - self.width = #text -end - configureConstructor({ orderedArguments = { "text", "X", "Y" }, requiredArguments = { "text" }, diff --git a/src/classes/utils/TML.ti b/src/classes/utils/TML.ti index eb73596..39ac44d 100755 --- a/src/classes/utils/TML.ti +++ b/src/classes/utils/TML.ti @@ -11,8 +11,12 @@ local function formArguments( class, target ) local returnArguments, trailingTable, dynamics = {}, {}, {} if not constructor then return nil end - local argumentTypes = constructor.argumentTypes + if constructor.tmlContent and target.content then + args[ constructor.tmlContent ] = target.content + end + + local argumentTypes = constructor.argumentTypes local function handleArgument( val, target ) if type( val ) ~= "string" then return false diff --git a/src/mixins/MTextDisplay.ti b/src/mixins/MTextDisplay.ti index b81c8e9..521c3aa 100644 --- a/src/mixins/MTextDisplay.ti +++ b/src/mixins/MTextDisplay.ti @@ -120,6 +120,13 @@ function MTextDisplay:drawText( bg, tc ) end end +function MTextDisplay:setText( text ) + if text == self.text then return end + + self.changed = true + self.text = text +end + configureConstructor { argumentTypes = { verticalPadding = "number", @@ -129,5 +136,7 @@ configureConstructor { horizontalAlign = "string", text = "string" - } + }, + + tmlContent = "text" } diff --git a/src/scripts/Class.lua b/src/scripts/Class.lua index 39a8ee7..c3ae6de 100755 --- a/src/scripts/Class.lua +++ b/src/scripts/Class.lua @@ -321,6 +321,8 @@ local function compileCurrent() target = constructorTargets[ i ] cConstructor[ target ] = mergeValues( cConstructor[ target ], constructor and constructor[ target ] ) end + + cConstructor.tmlContent = cConstructor.tmlContent or constructor.tmlContent end end