Browse Source

Change Lua Hello World sample in OOP.

Aster Jian 12 years ago
parent
commit
1c02a7a7a7
2 changed files with 61 additions and 34 deletions
  1. 32 12
      Bin/Data/LuaScripts/01_HelloWorld.lua
  2. 29 22
      Bin/Data/LuaScripts/Utilities/Sample.lua

+ 32 - 12
Bin/Data/LuaScripts/01_HelloWorld.lua

@@ -1,28 +1,48 @@
 ExecuteFile("LuaScripts/Utilities/Sample.lua")
 ExecuteFile("LuaScripts/Utilities/Sample.lua")
 
 
-function Start()
-    SampleStart()
-    
-    CreateText()
+HelloWorld = {}
+
+function HelloWorld:new()
+    local o = {}
+    self.__index = self
+    setmetatable(o, self)
+    setmetatable(self, {__index = Sample}) -- Set Sample as base class.
+    return o
 end
 end
 
 
-function Stop()
-
-    SampleStop()
+function HelloWorld:Start()
+    Sample:Start() -- Call Start function in Sample.
+    self:CreateText()
 end
 end
 
 
-function CreateText()
-    local cache = GetCache()
+function HelloWorld:CreateText()
     local context = GetContext()
     local context = GetContext()
     local helloText = Text:new(context)
     local helloText = Text:new(context)
 
 
     helloText:SetText("Hello World from Urho3D!");
     helloText:SetText("Hello World from Urho3D!");
 
 
+    local cache = GetCache()
     helloText:SetFont(cache:GetFont("Fonts/Anonymous Pro.ttf"), 30)
     helloText:SetFont(cache:GetFont("Fonts/Anonymous Pro.ttf"), 30)
     helloText:SetColor(Color(0.0, 1.0, 0.0))
     helloText:SetColor(Color(0.0, 1.0, 0.0))
+    
+    helloText.horizontalAlignment = HA_CENTER;
+    helloText.verticalAlignment = VA_CENTER;
+    
+    GetUI():GetRoot():AddChild(helloText)
+end
 
 
-    helloText:SetHorizontalAlignment(HA_CENTER);
-    helloText:SetVerticalAlignment(VA_CENTER);
+local sample = nil
 
 
-    GetUI():GetRoot():AddChild(helloText)
+function Start()
+    if sample == nil then
+        sample = HelloWorld:new()
+    end
+    sample:Start()
+end
+
+function Stop()
+    if sample ~= nil then
+        sample:Stop()
+    end
+    sample = nil
 end
 end

+ 29 - 22
Bin/Data/LuaScripts/Utilities/Sample.lua

@@ -1,56 +1,63 @@
-local logoSprite = nil
+Sample = {}
 
 
-local cache = GetCache()
-local engine = GetEngine()
-local ui = GetUI()
-
-function SampleStart()
-    CreateLogo()
-    CreateConsoleAndDebugHud()
+function Sample:Start()
+    self.logoSprite = nil
+    self:CreateLogo()
+    self:CreateConsoleAndDebugHud()
 end
 end
 
 
-function SampleStop()
-    logoSprite = nil
+function Sample:Stop()
+    self.logoSprite = nil
 end
 end
 
 
-function CreateLogo()
+function Sample:CreateLogo()
+    local cache = GetCache()
     local logoTexture = cache:GetTexture2D("Textures/LogoLarge.png")
     local logoTexture = cache:GetTexture2D("Textures/LogoLarge.png")
     if logoTexture == nil then
     if logoTexture == nil then
         return
         return
-    end    
+    end
+    
+    local ui = GetUI()
+    self.logoSprite = ui.root:CreateSprite()
+    --self.logoSprite.texture = logoTexture
+    self.logoSprite:SetTexture(logoTexture)
+    
     
     
-    logoSprite = ui.root:CreateSprite()
-    logoSprite.texture = logoTexture
     local textureWidth = logoTexture.width
     local textureWidth = logoTexture.width
     local textureHeight = logoTexture.height
     local textureHeight = logoTexture.height
     
     
-    logoSprite:SetScale(256 / textureWidth)
-    logoSprite.size = IntVector2(textureWidth, textureHeight)
-    logoSprite.hotSpot = IntVector2(0, textureHeight)
-
-    logoSprite:SetAlignment(HA_LEFT, VA_BOTTOM);
+    self.logoSprite:SetScale(256 / textureWidth)
+    --self.logoSprite.size = IntVector2(textureWidth, textureHeight)
+    self.logoSprite:SetSize(textureWidth, textureHeight)
+    self.logoSprite.hotSpot = IntVector2(0, textureHeight)
+    
+    self.logoSprite:SetAlignment(HA_LEFT, VA_BOTTOM);
 end
 end
 
 
-function CreateConsoleAndDebugHud()
+function Sample:CreateConsoleAndDebugHud()
+    local cache = GetCache()
     local uiStyle = cache:GetXMLFile("UI/DefaultStyle.xml")
     local uiStyle = cache:GetXMLFile("UI/DefaultStyle.xml")
     if uiStyle == nil then
     if uiStyle == nil then
         return
         return
     end
     end
     
     
+    local engine = GetEngine()
     local console = engine:CreateConsole()
     local console = engine:CreateConsole()
     console.defaultStyle = uiStyle
     console.defaultStyle = uiStyle
     
     
     local debugHud = engine:CreateDebugHud()
     local debugHud = engine:CreateDebugHud()
     debugHud.defaultStyle = uiStyle
     debugHud.defaultStyle = uiStyle
     
     
-    SubscribeToEvent("KeyDown", "HandleKeyDownEventForSample")
+    SubscribeToEvent("KeyDown", "Sample.HandleKeyDownEvent")
 end
 end
 
 
-function HandleKeyDownEventForSample(eventType, eventData)
+function Sample.HandleKeyDownEvent(eventType, eventData)
     local key = eventData:GetInt("Key")
     local key = eventData:GetInt("Key")
     
     
     if key == KEY_ESC then
     if key == KEY_ESC then
+        local ui = GetUI()
         if ui:GetFocusElement() == nil then
         if ui:GetFocusElement() == nil then
+            local engine = GetEngine()
             engine:Exit()
             engine:Exit()
         else
         else
             local console = GetConsole()
             local console = GetConsole()