From ba62863ff75d558bd17c3aef9b14634549eedcaa Mon Sep 17 00:00:00 2001
From: Eduardo Bart <edub4rt@gmail.com>
Date: Wed, 16 Nov 2011 15:03:11 -0200
Subject: [PATCH] add style for colorbox in outfit window

---
 README.rdoc                                |   2 +-
 modules/core_styles/images/colorbox.png    | Bin 0 -> 250 bytes
 modules/core_styles/styles/checkboxes.otui |  17 ++++++++-
 modules/outfit/outfit.lua                  |  34 +++++++++---------
 modules/outfit/outfit.otui                 |  34 +++++++-----------
 src/framework/luascript/luavaluecasts.cpp  |  13 +++----
 src/framework/ui/uicheckbox.cpp            |  13 ++++++-
 src/framework/ui/uicheckbox.h              |   4 +--
 src/framework/ui/uiwidget.cpp              |  13 +++----
 src/framework/ui/uiwidget.h                |   2 +-
 src/otclient/otclientluafunctions.cpp      |  40 +++++++++++++++++++++
 11 files changed, 116 insertions(+), 56 deletions(-)
 create mode 100644 modules/core_styles/images/colorbox.png

diff --git a/README.rdoc b/README.rdoc
index 782d63d2..b71bf3ce 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -17,7 +17,7 @@ Checkout our website at {http://otclient.info}[http://otclient.info/] for tutori
 == Need help?
 
 If you have any questions or are looking for more information, please feel free to ask on our official
-forum at http://otclient.info/
+forum at http://otclient.info
 
 == Bugs
 
diff --git a/modules/core_styles/images/colorbox.png b/modules/core_styles/images/colorbox.png
new file mode 100644
index 0000000000000000000000000000000000000000..59d5276711f52d250c71492f6221d9d901592ac2
GIT binary patch
literal 250
zcmeAS@N?(olHy`uVBq!ia0vp^3P3Et!3HGD8EPYe6k~CayA#8@b22Z19JVBHcNd2L
zAh=-f^2tCE&H|6fVg?3oVGw3ym^DWND9B#o>FdgVn_EDTQD^DlE$Toa+02lL66gHf
z+|;}hAeVu`xhOTUBsE2$JhLQ2!QIn0AVn{g9Vi~?>EamTas2Jojl2f}IGhi<XKhPi
zNK0(pzv8kAbBl;c#G)-F>liE=W|jZ@#ydg9%FviWiK+bmo%LE33~Tpz8*RS3U*a5-
j*oFpHodlT!U##~r?&sZl_J@XA7|2#nS3j3^P6<r_i6cw?

literal 0
HcmV?d00001

diff --git a/modules/core_styles/styles/checkboxes.otui b/modules/core_styles/styles/checkboxes.otui
index cf7817e9..306c7a09 100644
--- a/modules/core_styles/styles/checkboxes.otui
+++ b/modules/core_styles/styles/checkboxes.otui
@@ -30,4 +30,19 @@ CheckBox < UICheckBox
 
   $disabled:
     background-color: #ffffff88
-    color: #aaaaaa88
\ No newline at end of file
+    color: #aaaaaa88
+
+ColorBox < UICheckBox
+  size: 16 16
+  box-size: 16 16
+  background-color: #ffffffff
+
+  $checked:
+    image:
+      source: /core_styles/images/colorbox.png
+      coords: 16 0 16 16
+
+  $!checked:
+    image:
+      source: /core_styles/images/colorbox.png
+      coords: 0 0 16 16
diff --git a/modules/outfit/outfit.lua b/modules/outfit/outfit.lua
index 88df051c..b4711c34 100644
--- a/modules/outfit/outfit.lua
+++ b/modules/outfit/outfit.lua
@@ -9,10 +9,10 @@ local currentOutfit = 1
 local function update()
   local creatureWidget = window:getChildById('creature')
   creatureWidget:setOutfitType(outfits[currentOutfit][1])
-  
+
   local nameWidget = window:getChildById('name')
   nameWidget:setText(outfits[currentOutfit][2])
-  
+
   local availableAddons = outfits[currentOutfit][3]
   local addon1 = window:getChildById('addon1')
   local addon2 = window:getChildById('addon2')
@@ -20,7 +20,7 @@ local function update()
   addon1:setChecked(false)
   addon2:setChecked(false)
   addon3:setChecked(false)
-  
+
   -- Maybe rework this someday
   if availableAddons == 1 then
     addon1:setChecked(true)
@@ -42,7 +42,7 @@ local function update()
     addon2:setChecked(true)
     addon3:setChecked(true)
   end
-  
+
 end
 
 -- public functions
@@ -61,28 +61,28 @@ function Outfit.create(creature, outfitList)
   Outfit.destroy()
   window = loadUI("/outfit/outfit.otui", UI.root)
   window:lock()
-  
+
   local creatureWidget = window:getChildById('creature')
   creatureWidget:setCreature(creature)
-  
+
   local firstColor = UIWidget.create()
   window:addChild(firstColor)
   firstColor:setStyle('ColorFirst')
-  
+
   for i=0,18 do
     for j=0,6 do
       local color = UICheckBox.create()
       window:addChild(color)
-      
+
       local outfitColor = getOufitColor(j*19 + i)
-      
+
       color:setStyle('Color')
       color:setBackgroundColor(outfitColor)
-      color:setMarginTop(j * 3 + j * 12)
-      color:setMarginLeft(i * 3 + i * 12)
+      color:setMarginTop(j * 3 + j * 14)
+      color:setMarginLeft(i * 3 + i * 14)
     end
   end
-  
+
   outfits = outfitList
   currentOutfit = 1
   update()
@@ -96,14 +96,14 @@ function Outfit.destroy()
 end
 
 function Outfit.nextType()
-  
+
   currentOutfit = currentOutfit + 1
   if currentOutfit > #outfits then
     currentOutfit = 1
   end
-  
+
   update()
-  
+
 end
 
 function Outfit.previousType()
@@ -111,9 +111,9 @@ function Outfit.previousType()
   if currentOutfit <= 0 then
     currentOutfit = #outfits
   end
-  
+
   update()
-  
+
 end
 
 -- hooked events
diff --git a/modules/outfit/outfit.otui b/modules/outfit/outfit.otui
index 8e6f795f..4444d9a3 100644
--- a/modules/outfit/outfit.otui
+++ b/modules/outfit/outfit.otui
@@ -1,31 +1,23 @@
-ColorCheckBox < UICheckBox
-  size: 12 12
-  box-size: 12 12
-  background-color: #ffffffff
-  
-  image:
-    source: /core_styles/images/empty_rect.png
-
 ColorFirst < UIWidget
   id: color
   margin.left: 20
   anchors.top: creature.top
   anchors.left: creature.right
-  
-Color < ColorCheckBox
+
+Color < ColorBox
   anchors.top: color.top
   anchors.left: color.right
-  
+
 Window
   title: Select Outfit
-  size: 420 280
-  
+  size: 450 280
+
   anchors.horizontalCenter: parent.horizontalCenter
   anchors.verticalCenter: parent.verticalCenter
-  
+
   onEnter: Outfit.accept()
   onEscape: Outfit.destroy()
-  
+
   Label
     id: name
     text: Outfit Name
@@ -34,7 +26,7 @@ Window
     anchors.left: parent.left
     margin.top: 34
     margin.left: 20
-  
+
   Creature
     id: creature
     anchors.top: name.bottom
@@ -48,7 +40,7 @@ Window
     margin.top: 3
     anchors.top: creature.bottom
     anchors.right: creature.right
-    
+
   Button
     @onClick: Outfit.previousType()
     text: <<
@@ -56,7 +48,7 @@ Window
     margin.top: 3
     anchors.top: creature.bottom
     anchors.left: creature.left
-    
+
   CheckBox
     id: addon1
     text: Addon 1
@@ -65,7 +57,7 @@ Window
     width: 100
     anchors.top: prev.bottom
     anchors.left: prev.left
-    
+
   CheckBox
     id: addon2
     text: Addon 2
@@ -74,7 +66,7 @@ Window
     width: 100
     anchors.top: prev.bottom
     anchors.left: prev.left
-    
+
   CheckBox
     id: addon3
     text: Addon 3
@@ -83,7 +75,7 @@ Window
     width: 100
     anchors.top: prev.bottom
     anchors.left: prev.left
-  
+
   HorizontalSeparator
     anchors.left: parent.left
     anchors.right: parent.right
diff --git a/src/framework/luascript/luavaluecasts.cpp b/src/framework/luascript/luavaluecasts.cpp
index 20f24b86..2287b485 100644
--- a/src/framework/luascript/luavaluecasts.cpp
+++ b/src/framework/luascript/luavaluecasts.cpp
@@ -163,13 +163,14 @@ bool luavalue_cast(int index, Rect& rect)
         rect.setWidth(g_lua.popInteger());
         g_lua.getField("height", index);
         rect.setHeight(g_lua.popInteger());
+        return true;
     } else if(g_lua.isString()) {
         return Fw::cast(g_lua.toString(index), rect);
     } else if(g_lua.isNil()) {
         rect = Rect();
         return true;
     }
-    return true;
+    return false;
 }
 
 // point
@@ -196,7 +197,7 @@ bool luavalue_cast(int index, Point& point)
         point = Point();
         return true;
     }
-    return true;
+    return false;
 }
 
 // size
@@ -223,7 +224,7 @@ bool luavalue_cast(int index, Size& size)
         size = Size();
         return true;
     }
-    return true;
+    return false;
 }
 
 // otml nodes
@@ -270,9 +271,9 @@ bool luavalue_cast(int index, OTMLNodePtr& node)
                 node->writeAt(cnodeName, g_lua.toString());
             g_lua.pop();
         }
-    } else
-        return false;
-    return true;
+        return true;
+    }
+    return false;
 }
 
 // object ptr
diff --git a/src/framework/ui/uicheckbox.cpp b/src/framework/ui/uicheckbox.cpp
index 1b18ec50..b8758ca9 100644
--- a/src/framework/ui/uicheckbox.cpp
+++ b/src/framework/ui/uicheckbox.cpp
@@ -48,7 +48,7 @@ void UICheckBox::render()
 void UICheckBox::onMouseRelease(const Point& mousePos, Fw::MouseButton button)
 {
     if(isPressed() && getRect().contains(mousePos))
-        setState(Fw::CheckedState, !isChecked());
+        setChecked(!isChecked());
 }
 
 void UICheckBox::onStyleApply(const OTMLNodePtr& styleNode)
@@ -68,3 +68,14 @@ void UICheckBox::onStyleApply(const OTMLNodePtr& styleNode)
         }
     }
 }
+
+bool UICheckBox::isChecked()
+{
+    return hasState(Fw::CheckedState);
+}
+
+void UICheckBox::setChecked(bool checked)
+{
+    if(setState(Fw::CheckedState, checked))
+        callLuaField("onCheckChange", checked);
+}
diff --git a/src/framework/ui/uicheckbox.h b/src/framework/ui/uicheckbox.h
index 6f072f34..ace638e9 100644
--- a/src/framework/ui/uicheckbox.h
+++ b/src/framework/ui/uicheckbox.h
@@ -30,8 +30,8 @@ class UICheckBox : public UIWidget
 public:
     void render();
 
-    bool isChecked() { return hasState(Fw::CheckedState); }
-    void setChecked(bool checked) { setState(Fw::CheckedState, checked); }
+    bool isChecked();
+    void setChecked(bool checked);
 
     void setText(const std::string& text) { m_text = text; }
     std::string getText() { return m_text; }
diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp
index a30c962a..28201b29 100644
--- a/src/framework/ui/uiwidget.cpp
+++ b/src/framework/ui/uiwidget.cpp
@@ -615,10 +615,10 @@ void UIWidget::updateLayout()
         m_layout->update();
 }
 
-void UIWidget::setState(Fw::WidgetState state, bool on)
+bool UIWidget::setState(Fw::WidgetState state, bool on)
 {
     if(state == Fw::InvalidState)
-        return;
+        return false;
 
     int oldStates = m_states;
     if(on)
@@ -626,8 +626,11 @@ void UIWidget::setState(Fw::WidgetState state, bool on)
     else
         m_states &= ~state;
 
-    if(oldStates != m_states)
+    if(oldStates != m_states) {
         updateStyle();
+        return true;
+    }
+    return false;
 }
 
 bool UIWidget::hasState(Fw::WidgetState state)
@@ -700,9 +703,7 @@ void UIWidget::updateState(Fw::WidgetState state)
             child->updateState(state);
     }
 
-    if(newStatus != oldStatus) {
-        setState(state, newStatus);
-
+    if(setState(state, newStatus)) {
         if(state == Fw::FocusState) {
             g_dispatcher.addEvent(std::bind(&UIWidget::onFocusChange, asUIWidget(), newStatus, m_lastFocusReason));
         } else if(state == Fw::HoverState)
diff --git a/src/framework/ui/uiwidget.h b/src/framework/ui/uiwidget.h
index c1308956..11b80d3a 100644
--- a/src/framework/ui/uiwidget.h
+++ b/src/framework/ui/uiwidget.h
@@ -149,7 +149,7 @@ public:
 
     void updateStates();
     virtual void updateState(Fw::WidgetState state);
-    void setState(Fw::WidgetState state, bool on);
+    bool setState(Fw::WidgetState state, bool on);
     bool hasState(Fw::WidgetState state);
 
     void updateStyle();
diff --git a/src/otclient/otclientluafunctions.cpp b/src/otclient/otclientluafunctions.cpp
index 2c6596a0..3a0d3200 100644
--- a/src/otclient/otclientluafunctions.cpp
+++ b/src/otclient/otclientluafunctions.cpp
@@ -39,6 +39,9 @@
 #include <otclient/ui/uimap.h>
 #include <otclient/core/outfit.h>
 
+void push_luavalue(const Outfit& outfit);
+bool luavalue_cast(int index, Outfit& outfit);
+
 void OTClient::registerLuaFunctions()
 {
     g_lua.bindGlobalFunction("exit", std::bind(&OTClient::exit, &g_client));
@@ -90,3 +93,40 @@ void OTClient::registerLuaFunctions()
     g_lua.bindClassStaticFunction<Game>("talkPrivate", std::bind(&Game::talkPrivate, &g_game, _1, _2, _3));
 #endif
 }
+
+void push_luavalue(const Outfit& outfit)
+{
+    g_lua.newTable();
+    g_lua.pushInteger(outfit.getType());
+    g_lua.setField("type");
+    g_lua.pushInteger(outfit.getAddons());
+    g_lua.setField("addons");
+    g_lua.pushInteger(outfit.getHead());
+    g_lua.setField("head");
+    g_lua.pushInteger(outfit.getBody());
+    g_lua.setField("body");
+    g_lua.pushInteger(outfit.getLegs());
+    g_lua.setField("legs");
+    g_lua.pushInteger(outfit.getFeet());
+    g_lua.setField("feet");
+}
+
+bool luavalue_cast(int index, Outfit& outfit)
+{
+    if(g_lua.isTable(index)) {
+        g_lua.getField("type", index);
+        outfit.setType(g_lua.popInteger());
+        g_lua.getField("addons", index);
+        outfit.setAddons(g_lua.popInteger());
+        g_lua.getField("head", index);
+        outfit.setHead(g_lua.popInteger());
+        g_lua.getField("body", index);
+        outfit.setBody(g_lua.popInteger());
+        g_lua.getField("legs", index);
+        outfit.setLegs(g_lua.popInteger());
+        g_lua.getField("feet", index);
+        outfit.setFeet(g_lua.popInteger());
+        return true;
+    }
+    return false;
+}