From 50b99a75b9c13b815667d8d0af9a5650de739248 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Thu, 21 Apr 2011 19:44:30 -0300 Subject: [PATCH] lua scripting --- CMakeLists.txt | 8 +- data/lib/init.lua | 0 data/menustate.lua | 22 -- .../mainmenu/entergamewindow.yml} | 3 + .../mainmenu/infowindow.yml} | 0 data/modules/mainmenu/init.lua | 1 - data/modules/mainmenu/mainmenu.lua | 24 +- data/modules/mainmenu/mainmenu.yml | 6 +- .../mainmenu/optionswindow.yml} | 5 +- data/ui/mainMenu.yml | 37 -- src/framework/prerequisites.h | 1 + src/framework/script/luafunctions.cpp | 112 ++++++ src/framework/script/luascript.cpp | 344 ++++++++++++++++++ src/framework/script/luascript.h | 95 +++++ src/framework/script/scriptable.h | 39 ++ src/framework/ui/uibutton.h | 2 + src/framework/ui/uicheckbox.h | 5 +- src/framework/ui/uicontainer.h | 2 + src/framework/ui/uielement.h | 1 + src/framework/ui/uilabel.h | 2 + src/framework/ui/uilayout.h | 7 +- src/framework/ui/uiloader.cpp | 22 +- src/framework/ui/uiloader.h | 4 + src/framework/ui/uitextedit.h | 2 + src/framework/ui/uiwindow.h | 2 + src/main.cpp | 4 +- src/menustate.cpp | 69 +--- 27 files changed, 674 insertions(+), 145 deletions(-) delete mode 100644 data/lib/init.lua delete mode 100644 data/menustate.lua rename data/{ui/enterGameWindow.yml => modules/mainmenu/entergamewindow.yml} (93%) rename data/{ui/infoWindow.yml => modules/mainmenu/infowindow.yml} (100%) delete mode 100644 data/modules/mainmenu/init.lua rename data/{ui/optionsWindow.yml => modules/mainmenu/optionswindow.yml} (96%) delete mode 100644 data/ui/mainMenu.yml create mode 100644 src/framework/script/luafunctions.cpp create mode 100644 src/framework/script/luascript.cpp create mode 100644 src/framework/script/luascript.h create mode 100644 src/framework/script/scriptable.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a8bb5a5..1a177991 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}") # find needed packages SET(Boost_USE_STATIC_LIBS ON) SET(Boost_USE_MULTITHREADED ON) -FIND_PACKAGE(Boost COMPONENTS system regex REQUIRED) +FIND_PACKAGE(Boost COMPONENTS system regex signals REQUIRED) FIND_PACKAGE(OpenGL REQUIRED) FIND_PACKAGE(Lua51 REQUIRED) FIND_PACKAGE(YamlCpp REQUIRED) @@ -52,7 +52,7 @@ IF(CMAKE_BUILD_TYPE STREQUAL "Debug") ADD_DEFINITIONS(-D_DEBUG) ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug") -SET(SOURCES src/framework/script/script.cpp +SET(SOURCES # game sources src/main.cpp src/menustate.cpp @@ -68,8 +68,8 @@ SET(SOURCES src/framework/script/script.cpp src/framework/core/engine.cpp # framework script - src/framework/script/script.cpp - src/framework/script/scriptfunctions.cpp + src/framework/script/luascript.cpp + src/framework/script/luafunctions.cpp # framework utilities src/framework/util/color.cpp diff --git a/data/lib/init.lua b/data/lib/init.lua deleted file mode 100644 index e69de29b..00000000 diff --git a/data/menustate.lua b/data/menustate.lua deleted file mode 100644 index eb26f26a..00000000 --- a/data/menustate.lua +++ /dev/null @@ -1,22 +0,0 @@ -MainMenuController = {} -enterGameController = {} - -function enterGameController.enterGameButton_clicked() - createEnterGameWindow() -end - -function mainMenuController.createMainMenu() - mainMenuPanel = loadUI("ui/mainMenuPanel.yml") - enterGameButton = mainMenuPanel:getElementById("enterGameButton") - enterGameButton.setOnClick(enterGameController.enterGameButton_clicked) - exitButton = mainMenuPanel:getElementById("exitButton") - exitButton:setOnClick(function() exitGame() end) -end - -function onEnter() - mainMenuController:createMainMenu() -end - -function onLeave() - -end diff --git a/data/ui/enterGameWindow.yml b/data/modules/mainmenu/entergamewindow.yml similarity index 93% rename from data/ui/enterGameWindow.yml rename to data/modules/mainmenu/entergamewindow.yml index 8871ec21..0a38b1c8 100644 --- a/data/ui/enterGameWindow.yml +++ b/data/modules/mainmenu/entergamewindow.yml @@ -49,6 +49,9 @@ window#enterGameWindow: anchors.bottom: parent.bottom margin.bottom: 10 margin.right: 13 + onClick: | + enterGameWindow:destroy() + enterGameWindow = nil textEdit#accountNameTextEdit: anchors.right: parent.right diff --git a/data/ui/infoWindow.yml b/data/modules/mainmenu/infowindow.yml similarity index 100% rename from data/ui/infoWindow.yml rename to data/modules/mainmenu/infowindow.yml diff --git a/data/modules/mainmenu/init.lua b/data/modules/mainmenu/init.lua deleted file mode 100644 index 296b7928..00000000 --- a/data/modules/mainmenu/init.lua +++ /dev/null @@ -1 +0,0 @@ -require("modules/mainmenu/mainmenu") \ No newline at end of file diff --git a/data/modules/mainmenu/mainmenu.lua b/data/modules/mainmenu/mainmenu.lua index 22150b0a..32529fbf 100644 --- a/data/modules/mainmenu/mainmenu.lua +++ b/data/modules/mainmenu/mainmenu.lua @@ -1 +1,23 @@ -loadUI("modules/mainmenu/mainmenu.yml") \ No newline at end of file +-- events +function MainMenu_enterGameClicked() + enterGameWindow = loadUI("modules/mainmenu/entergamewindow.yml") +end + +function MainMenu_optionsClicked() + optionsWindow = loadUI("modules/mainmenu/optionswindow.yml") +end + +function MainMenu_infoClicked() + infoWindow = loadUI("modules/mainmenu/infowindow.yml") +end + +function MainMenu_exitClicked() + exitGame() +end + +-- create main menu +function MainMenu_create() + menuPanel = loadUI("modules/mainmenu/mainmenu.yml") +end + +MainMenu_create() \ No newline at end of file diff --git a/data/modules/mainmenu/mainmenu.yml b/data/modules/mainmenu/mainmenu.yml index 1438a708..58b99944 100644 --- a/data/modules/mainmenu/mainmenu.yml +++ b/data/modules/mainmenu/mainmenu.yml @@ -11,6 +11,7 @@ panel#mainMenu: anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter margin.top: 16 + onClick: MainMenu_enterGameClicked() button#accessAccountButton: text: Access Account @@ -23,15 +24,18 @@ panel#mainMenu: anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter margin.top: 76 + onClick: MainMenu_optionsClicked() button#infoButton: text: Info anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter margin.top: 106 + onClick: MainMenu_infoClicked() button#exitGameButton: text: Exit anchors.top: parent.top anchors.horizontalCenter: parent.horizontalCenter - margin.top: 136 \ No newline at end of file + margin.top: 136 + onClick: MainMenu_exitClicked() \ No newline at end of file diff --git a/data/ui/optionsWindow.yml b/data/modules/mainmenu/optionswindow.yml similarity index 96% rename from data/ui/optionsWindow.yml rename to data/modules/mainmenu/optionswindow.yml index f580e888..61c4f18c 100644 --- a/data/ui/optionsWindow.yml +++ b/data/modules/mainmenu/optionswindow.yml @@ -108,4 +108,7 @@ window#optionsWindow: anchors.right: parent.right anchors.bottom: parent.bottom margin.right: 10 - margin.bottom: 13 \ No newline at end of file + margin.bottom: 13 + onClick: | + optionsWindow:destroy() + optionsWindow = nil \ No newline at end of file diff --git a/data/ui/mainMenu.yml b/data/ui/mainMenu.yml deleted file mode 100644 index 1438a708..00000000 --- a/data/ui/mainMenu.yml +++ /dev/null @@ -1,37 +0,0 @@ -panel#mainMenu: - skin: roundedGridPanel - size: [117, 171] - anchors.left: parent.left - anchors.bottom: parent.bottom - margin.left: 60 - margin.bottom: 70 - - button#enterGameButton: - text: Enter Game - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - margin.top: 16 - - button#accessAccountButton: - text: Access Account - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - margin.top: 46 - - button#optionsButton: - text: Options - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - margin.top: 76 - - button#infoButton: - text: Info - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - margin.top: 106 - - button#exitGameButton: - text: Exit - anchors.top: parent.top - anchors.horizontalCenter: parent.horizontalCenter - margin.top: 136 \ No newline at end of file diff --git a/src/framework/prerequisites.h b/src/framework/prerequisites.h index 0714b2d9..af132389 100644 --- a/src/framework/prerequisites.h +++ b/src/framework/prerequisites.h @@ -69,6 +69,7 @@ typedef int8_t int8; #include #include #include +#include #define foreach BOOST_FOREACH typedef boost::function Callback; diff --git a/src/framework/script/luafunctions.cpp b/src/framework/script/luafunctions.cpp new file mode 100644 index 00000000..165c5fdf --- /dev/null +++ b/src/framework/script/luafunctions.cpp @@ -0,0 +1,112 @@ +/* The MIT License + * + * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +#include +#include