summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-09-17 11:20:58 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-09-17 11:20:58 (GMT)
commit6e44ebc358d1206c147f514225373da07b43c015 (patch)
tree35e97c28991c4aff46a9e5a4182b53dedb26e7ee /src
parente52e9ce91ccca13115fec0fdb0111e7e5d39d10d (diff)
downloadpowder-6e44ebc358d1206c147f514225373da07b43c015.zip
powder-6e44ebc358d1206c147f514225373da07b43c015.tar.gz
Checkbox, Slider and ProgressBar components for ui API
Diffstat (limited to 'src')
-rw-r--r--src/cat/LuaCheckbox.cpp114
-rw-r--r--src/cat/LuaCheckbox.h33
-rw-r--r--src/cat/LuaProgressBar.cpp71
-rw-r--r--src/cat/LuaProgressBar.h30
-rw-r--r--src/cat/LuaScriptInterface.cpp12
-rw-r--r--src/cat/LuaSlider.cpp112
-rw-r--r--src/cat/LuaSlider.h33
-rw-r--r--src/cat/LuaWindow.cpp9
-rw-r--r--src/interface/Checkbox.cpp5
-rw-r--r--src/interface/Checkbox.h1
-rw-r--r--src/interface/ProgressBar.cpp20
-rw-r--r--src/interface/ProgressBar.h4
-rw-r--r--src/interface/Slider.cpp23
-rw-r--r--src/interface/Slider.h2
14 files changed, 461 insertions, 8 deletions
diff --git a/src/cat/LuaCheckbox.cpp b/src/cat/LuaCheckbox.cpp
new file mode 100644
index 0000000..1ddc0c2
--- /dev/null
+++ b/src/cat/LuaCheckbox.cpp
@@ -0,0 +1,114 @@
+extern "C"
+{
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+}
+
+#include <iostream>
+#include "LuaCheckbox.h"
+#include "LuaScriptInterface.h"
+#include "interface/Checkbox.h"
+
+const char LuaCheckbox::className[] = "Checkbox";
+
+#define method(class, name) {#name, &class::name}
+Luna<LuaCheckbox>::RegType LuaCheckbox::methods[] = {
+ method(LuaCheckbox, action),
+ method(LuaCheckbox, text),
+ method(LuaCheckbox, position),
+ method(LuaCheckbox, size),
+ method(LuaCheckbox, visible),
+ method(LuaCheckbox, checked),
+ {0, 0}
+};
+
+LuaCheckbox::LuaCheckbox(lua_State * l) :
+ LuaComponent(l),
+ actionFunction(0)
+{
+ int posX = luaL_optinteger(l, 1, 0);
+ int posY = luaL_optinteger(l, 2, 0);
+ int sizeX = luaL_optinteger(l, 3, 10);
+ int sizeY = luaL_optinteger(l, 4, 10);
+ std::string text = luaL_optstring(l, 5, "");
+
+ checkbox = new ui::Checkbox(ui::Point(posX, posY), ui::Point(sizeX, sizeY), text);
+ component = checkbox;
+ class ClickAction : public ui::CheckboxAction
+ {
+ LuaCheckbox * luaCheckbox;
+ public:
+ ClickAction(LuaCheckbox * luaCheckbox) : luaCheckbox(luaCheckbox) {}
+ void ActionCallback(ui::Checkbox * sender)
+ {
+ luaCheckbox->triggerAction();
+ }
+ };
+ checkbox->SetActionCallback(new ClickAction(this));
+}
+
+int LuaCheckbox::checked(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TBOOLEAN);
+ checkbox->SetChecked(lua_toboolean(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushboolean(l, checkbox->GetChecked());
+ return 1;
+ }
+}
+
+int LuaCheckbox::action(lua_State * l)
+{
+ if(lua_type(l, 1) != LUA_TNIL)
+ {
+ luaL_checktype(l, 1, LUA_TFUNCTION);
+ lua_pushvalue(l, 1);
+ actionFunction = luaL_ref(l, LUA_REGISTRYINDEX);
+ }
+ else
+ {
+ actionFunction = 0;
+ }
+ return 0;
+}
+
+int LuaCheckbox::text(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ luaL_checktype(l, 1, LUA_TSTRING);
+ checkbox->SetText(lua_tostring(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushstring(l, checkbox->GetText().c_str());
+ return 1;
+ }
+}
+
+void LuaCheckbox::triggerAction()
+{
+ if(actionFunction)
+ {
+ lua_rawgeti(l, LUA_REGISTRYINDEX, actionFunction);
+ lua_rawgeti(l, LUA_REGISTRYINDEX, UserData);
+ lua_pushboolean(l, checkbox->GetChecked());
+ if (lua_pcall(l, 2, 0, 0))
+ {
+ ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
+ }
+ }
+}
+
+LuaCheckbox::~LuaCheckbox()
+{
+} \ No newline at end of file
diff --git a/src/cat/LuaCheckbox.h b/src/cat/LuaCheckbox.h
new file mode 100644
index 0000000..479cab2
--- /dev/null
+++ b/src/cat/LuaCheckbox.h
@@ -0,0 +1,33 @@
+#pragma once
+
+extern "C" {
+ #include "lua.h"
+ #include "lauxlib.h"
+ #include "lualib.h"
+}
+
+#include "LuaLuna.h"
+#include "LuaComponent.h"
+
+namespace ui
+{
+ class Checkbox;
+}
+
+class LuaScriptInterface;
+
+class LuaCheckbox: public LuaComponent
+{
+ ui::Checkbox * checkbox;
+ int actionFunction;
+ void triggerAction();
+ int action(lua_State * l);
+ int checked(lua_State * l);
+ int text(lua_State * l);
+public:
+ static const char className[];
+ static Luna<LuaCheckbox>::RegType methods[];
+
+ LuaCheckbox(lua_State * l);
+ ~LuaCheckbox();
+}; \ No newline at end of file
diff --git a/src/cat/LuaProgressBar.cpp b/src/cat/LuaProgressBar.cpp
new file mode 100644
index 0000000..71b8f7b
--- /dev/null
+++ b/src/cat/LuaProgressBar.cpp
@@ -0,0 +1,71 @@
+extern "C"
+{
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+}
+
+#include <iostream>
+#include "LuaProgressBar.h"
+#include "LuaScriptInterface.h"
+#include "interface/ProgressBar.h"
+
+const char LuaProgressBar::className[] = "ProgressBar";
+
+#define method(class, name) {#name, &class::name}
+Luna<LuaProgressBar>::RegType LuaProgressBar::methods[] = {
+ method(LuaProgressBar, position),
+ method(LuaProgressBar, size),
+ method(LuaProgressBar, visible),
+ method(LuaProgressBar, progress),
+ method(LuaProgressBar, status),
+ {0, 0}
+};
+
+LuaProgressBar::LuaProgressBar(lua_State * l) :
+ LuaComponent(l)
+{
+ int posX = luaL_optinteger(l, 1, 0);
+ int posY = luaL_optinteger(l, 2, 0);
+ int sizeX = luaL_optinteger(l, 3, 10);
+ int sizeY = luaL_optinteger(l, 4, 10);
+ int value = luaL_optinteger(l, 5, 0);
+ std::string status = luaL_optstring(l, 6, "");
+
+ progressBar = new ui::ProgressBar(ui::Point(posX, posY), ui::Point(sizeX, sizeY), value, status);
+ component = progressBar;
+}
+
+int LuaProgressBar::progress(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ progressBar->SetProgress(lua_tointeger(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushinteger(l, progressBar->GetProgress());
+ return 1;
+ }
+}
+
+int LuaProgressBar::status(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ progressBar->SetStatus(std::string(lua_tostring(l, 1)));
+ return 0;
+ }
+ else
+ {
+ lua_pushstring(l, progressBar->GetStatus().c_str());
+ return 1;
+ }
+}
+
+LuaProgressBar::~LuaProgressBar()
+{
+} \ No newline at end of file
diff --git a/src/cat/LuaProgressBar.h b/src/cat/LuaProgressBar.h
new file mode 100644
index 0000000..9de56e7
--- /dev/null
+++ b/src/cat/LuaProgressBar.h
@@ -0,0 +1,30 @@
+#pragma once
+
+extern "C" {
+ #include "lua.h"
+ #include "lauxlib.h"
+ #include "lualib.h"
+}
+
+#include "LuaLuna.h"
+#include "LuaComponent.h"
+
+namespace ui
+{
+ class ProgressBar;
+}
+
+class LuaScriptInterface;
+
+class LuaProgressBar: public LuaComponent
+{
+ ui::ProgressBar * progressBar;
+ int progress(lua_State * l);
+ int status(lua_State * l);
+public:
+ static const char className[];
+ static Luna<LuaProgressBar>::RegType methods[];
+
+ LuaProgressBar(lua_State * l);
+ ~LuaProgressBar();
+}; \ No newline at end of file
diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp
index 98bd5ba..ff282ff 100644
--- a/src/cat/LuaScriptInterface.cpp
+++ b/src/cat/LuaScriptInterface.cpp
@@ -31,6 +31,9 @@
#include "LuaButton.h"
#include "LuaLabel.h"
#include "LuaTextbox.h"
+#include "LuaCheckbox.h"
+#include "LuaSlider.h"
+#include "LuaProgressBar.h"
#ifdef WIN
#include <direct.h>
@@ -301,6 +304,9 @@ void LuaScriptInterface::initInterfaceAPI()
Luna<LuaButton>::Register(l);
Luna<LuaLabel>::Register(l);
Luna<LuaTextbox>::Register(l);
+ Luna<LuaCheckbox>::Register(l);
+ Luna<LuaSlider>::Register(l);
+ Luna<LuaProgressBar>::Register(l);
}
int LuaScriptInterface::interface_addComponent(lua_State * l)
@@ -313,6 +319,12 @@ int LuaScriptInterface::interface_addComponent(lua_State * l)
component = Luna<LuaLabel>::get(luaComponent)->GetComponent();
else if(luaComponent = Luna<LuaTextbox>::tryGet(l, 1))
component = Luna<LuaTextbox>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaCheckbox>::tryGet(l, 1))
+ component = Luna<LuaCheckbox>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaSlider>::tryGet(l, 1))
+ component = Luna<LuaSlider>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaProgressBar>::tryGet(l, 1))
+ component = Luna<LuaProgressBar>::get(luaComponent)->GetComponent();
else
luaL_typerror(l, 1, "Component");
if(luacon_ci->Window && component)
diff --git a/src/cat/LuaSlider.cpp b/src/cat/LuaSlider.cpp
new file mode 100644
index 0000000..0fedbfb
--- /dev/null
+++ b/src/cat/LuaSlider.cpp
@@ -0,0 +1,112 @@
+extern "C"
+{
+#include "lua.h"
+#include "lauxlib.h"
+#include "lualib.h"
+}
+
+#include <iostream>
+#include "LuaSlider.h"
+#include "LuaScriptInterface.h"
+#include "interface/Slider.h"
+
+const char LuaSlider::className[] = "Slider";
+
+#define method(class, name) {#name, &class::name}
+Luna<LuaSlider>::RegType LuaSlider::methods[] = {
+ method(LuaSlider, onValueChanged),
+ method(LuaSlider, position),
+ method(LuaSlider, size),
+ method(LuaSlider, visible),
+ method(LuaSlider, value),
+ method(LuaSlider, steps),
+ {0, 0}
+};
+
+LuaSlider::LuaSlider(lua_State * l) :
+ LuaComponent(l),
+ onValueChangedFunction(0)
+{
+ int posX = luaL_optinteger(l, 1, 0);
+ int posY = luaL_optinteger(l, 2, 0);
+ int sizeX = luaL_optinteger(l, 3, 10);
+ int sizeY = luaL_optinteger(l, 4, 10);
+ int steps = luaL_optinteger(l, 5, 10);
+
+ slider = new ui::Slider(ui::Point(posX, posY), ui::Point(sizeX, sizeY), steps);
+ component = slider;
+ class ValueAction : public ui::SliderAction
+ {
+ LuaSlider * luaSlider;
+ public:
+ ValueAction(LuaSlider * luaSlider) : luaSlider(luaSlider) {}
+ void ValueChangedCallback(ui::Slider * sender)
+ {
+ luaSlider->triggerOnValueChanged();
+ }
+ };
+ slider->SetActionCallback(new ValueAction(this));
+}
+
+int LuaSlider::steps(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ slider->SetSteps(lua_tointeger(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushinteger(l, slider->GetSteps());
+ return 1;
+ }
+}
+
+int LuaSlider::onValueChanged(lua_State * l)
+{
+ if(lua_type(l, 1) != LUA_TNIL)
+ {
+ luaL_checktype(l, 1, LUA_TFUNCTION);
+ lua_pushvalue(l, 1);
+ onValueChangedFunction = luaL_ref(l, LUA_REGISTRYINDEX);
+ }
+ else
+ {
+ onValueChangedFunction = 0;
+ }
+ return 0;
+}
+
+int LuaSlider::value(lua_State * l)
+{
+ int args = lua_gettop(l);
+ if(args)
+ {
+ slider->SetValue(lua_tointeger(l, 1));
+ return 0;
+ }
+ else
+ {
+ lua_pushinteger(l, slider->GetValue());
+ return 1;
+ }
+}
+
+void LuaSlider::triggerOnValueChanged()
+{
+ if(onValueChangedFunction)
+ {
+ lua_rawgeti(l, LUA_REGISTRYINDEX, onValueChangedFunction);
+ lua_rawgeti(l, LUA_REGISTRYINDEX, UserData);
+ lua_pushinteger(l, slider->GetValue());
+ if (lua_pcall(l, 2, 0, 0))
+ {
+ ci->Log(CommandInterface::LogError, lua_tostring(l, -1));
+ }
+ }
+}
+
+LuaSlider::~LuaSlider()
+{
+} \ No newline at end of file
diff --git a/src/cat/LuaSlider.h b/src/cat/LuaSlider.h
new file mode 100644
index 0000000..f9f327e
--- /dev/null
+++ b/src/cat/LuaSlider.h
@@ -0,0 +1,33 @@
+#pragma once
+
+extern "C" {
+ #include "lua.h"
+ #include "lauxlib.h"
+ #include "lualib.h"
+}
+
+#include "LuaLuna.h"
+#include "LuaComponent.h"
+
+namespace ui
+{
+ class Slider;
+}
+
+class LuaScriptInterface;
+
+class LuaSlider: public LuaComponent
+{
+ ui::Slider * slider;
+ int onValueChangedFunction;
+ void triggerOnValueChanged();
+ int onValueChanged(lua_State * l);
+ int steps(lua_State * l);
+ int value(lua_State * l);
+public:
+ static const char className[];
+ static Luna<LuaSlider>::RegType methods[];
+
+ LuaSlider(lua_State * l);
+ ~LuaSlider();
+}; \ No newline at end of file
diff --git a/src/cat/LuaWindow.cpp b/src/cat/LuaWindow.cpp
index 16553d8..433e4d1 100644
--- a/src/cat/LuaWindow.cpp
+++ b/src/cat/LuaWindow.cpp
@@ -11,6 +11,9 @@ extern "C"
#include "LuaButton.h"
#include "LuaLabel.h"
#include "LuaTextbox.h"
+#include "LuaCheckbox.h"
+#include "LuaSlider.h"
+#include "LuaProgressBar.h"
#include "interface/Button.h"
#include "interface/Label.h"
#include "interface/Window.h"
@@ -106,6 +109,12 @@ int LuaWindow::addComponent(lua_State * l)
component = Luna<LuaLabel>::get(luaComponent)->GetComponent();
else if(luaComponent = Luna<LuaTextbox>::tryGet(l, 1))
component = Luna<LuaTextbox>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaCheckbox>::tryGet(l, 1))
+ component = Luna<LuaCheckbox>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaSlider>::tryGet(l, 1))
+ component = Luna<LuaSlider>::get(luaComponent)->GetComponent();
+ else if(luaComponent = Luna<LuaProgressBar>::tryGet(l, 1))
+ component = Luna<LuaProgressBar>::get(luaComponent)->GetComponent();
else
luaL_typerror(l, 1, "Component");
if(component)
diff --git a/src/interface/Checkbox.cpp b/src/interface/Checkbox.cpp
index 2481b23..c0dd47b 100644
--- a/src/interface/Checkbox.cpp
+++ b/src/interface/Checkbox.cpp
@@ -25,6 +25,11 @@ void Checkbox::SetText(std::string text)
this->text = text;
}
+std::string Checkbox::GetText()
+{
+ return text;
+}
+
void Checkbox::OnMouseClick(int x, int y, unsigned int button)
{
if(checked)
diff --git a/src/interface/Checkbox.h b/src/interface/Checkbox.h
index ba96cc7..f9d305c 100644
--- a/src/interface/Checkbox.h
+++ b/src/interface/Checkbox.h
@@ -27,6 +27,7 @@ class Checkbox: public ui::Component {
public:
Checkbox(ui::Point position, ui::Point size, std::string text);
void SetText(std::string text);
+ std::string GetText();
void Draw(const Point& screenPos);
virtual void OnMouseEnter(int x, int y);
virtual void OnMouseLeave(int x, int y);
diff --git a/src/interface/ProgressBar.cpp b/src/interface/ProgressBar.cpp
index a622bab..eda88f6 100644
--- a/src/interface/ProgressBar.cpp
+++ b/src/interface/ProgressBar.cpp
@@ -3,17 +3,26 @@
using namespace ui;
-ProgressBar::ProgressBar(Point position, Point size):
+ProgressBar::ProgressBar(Point position, Point size, int startProgress, std::string startStatus):
Component(position, size),
intermediatePos(0.0f),
- progressStatus("")
+ progressStatus(""),
+ progress(0)
{
- progress = 0;
+ SetStatus(startStatus);
+ SetProgress(startProgress);
}
void ProgressBar::SetProgress(int progress)
{
this->progress = progress;
+ if(this->progress > 100)
+ this->progress = 100;
+}
+
+int ProgressBar::GetProgress()
+{
+ return progress;
}
void ProgressBar::SetStatus(std::string status)
@@ -21,6 +30,11 @@ void ProgressBar::SetStatus(std::string status)
progressStatus = status;
}
+std::string ProgressBar::GetStatus()
+{
+ return progressStatus;
+}
+
void ProgressBar::Draw(const Point & screenPos)
{
Graphics * g = ui::Engine::Ref().g;
diff --git a/src/interface/ProgressBar.h b/src/interface/ProgressBar.h
index 9d803cc..fc47f0c 100644
--- a/src/interface/ProgressBar.h
+++ b/src/interface/ProgressBar.h
@@ -10,9 +10,11 @@ namespace ui
float intermediatePos;
std::string progressStatus;
public:
- ProgressBar(Point position, Point size);
+ ProgressBar(Point position, Point size, int startProgress = 0, std::string startStatus = "");
virtual void SetProgress(int progress);
+ virtual int GetProgress();
virtual void SetStatus(std::string status);
+ virtual std::string GetStatus();
virtual void Draw(const Point & screenPos);
virtual void Tick(float dt);
};
diff --git a/src/interface/Slider.cpp b/src/interface/Slider.cpp
index 652ae59..d5639cb 100644
--- a/src/interface/Slider.cpp
+++ b/src/interface/Slider.cpp
@@ -72,10 +72,6 @@ void Slider::OnMouseUp(int x, int y, unsigned button)
}
}
-int Slider::GetValue()
-{
- return sliderPosition;
-}
void Slider::SetColour(Colour col1, Colour col2)
{
@@ -88,6 +84,11 @@ void Slider::SetColour(Colour col1, Colour col2)
bgGradient = (unsigned char*)Graphics::GenerateGradient(pix, fl, 2, Size.X-7);
}
+int Slider::GetValue()
+{
+ return sliderPosition;
+}
+
void Slider::SetValue(int value)
{
if(value < 0)
@@ -97,6 +98,20 @@ void Slider::SetValue(int value)
sliderPosition = value;
}
+int Slider::GetSteps()
+{
+ return sliderSteps;
+}
+
+void Slider::SetSteps(int steps)
+{
+ if(steps < 0)
+ steps = 0;
+ if(steps < sliderPosition)
+ sliderPosition = steps;
+ sliderSteps = steps;
+}
+
void Slider::Draw(const Point& screenPos)
{
Graphics * g = Engine::Ref().g;
diff --git a/src/interface/Slider.h b/src/interface/Slider.h
index 21792bc..65ca0ba 100644
--- a/src/interface/Slider.h
+++ b/src/interface/Slider.h
@@ -38,6 +38,8 @@ public:
void SetActionCallback(SliderAction * action) { actionCallback = action; }
int GetValue();
void SetValue(int value);
+ int GetSteps();
+ void SetSteps(int steps);
virtual ~Slider();
};