new hotbar, more minecraft-like
authorPerttu Ahola <celeron55@gmail.com>
Mon, 14 Feb 2011 15:41:49 +0000 (17:41 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Mon, 14 Feb 2011 15:41:49 +0000 (17:41 +0200)
src/guiInventoryMenu.cpp
src/guiInventoryMenu.h
src/main.cpp

index 102a0ae7250a4d2edb812090bab7a56570afcfc8..ef795a5f43be32dd100a7766ca59dd0b3cf56aea 100644 (file)
@@ -21,21 +21,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "guiInventoryMenu.h"
 #include "constants.h"
 
-void drawInventoryItem(gui::IGUIEnvironment* env,
+void drawInventoryItem(video::IVideoDriver *driver,
+               gui::IGUIFont *font,
                InventoryItem *item, core::rect<s32> rect,
                const core::rect<s32> *clip)
 {
-       gui::IGUISkin* skin = env->getSkin();
-       if (!skin)
+       if(item == NULL)
                return;
-       video::IVideoDriver* driver = env->getVideoDriver();
        
        video::ITexture *texture = NULL;
-       
-       if(item != NULL)
-       {
-               texture = item->getImage();
-       }
+       texture = item->getImage();
 
        if(texture != NULL)
        {
@@ -48,22 +43,28 @@ void drawInventoryItem(gui::IGUIEnvironment* env,
        }
        else
        {
-               video::SColor bgcolor(128,128,128,128);
+               video::SColor bgcolor(255,50,50,128);
                driver->draw2DRectangle(bgcolor, rect, clip);
        }
 
-       if(item != NULL)
+       if(font != NULL)
        {
-               gui::IGUIFont *font = skin->getFont();
                std::string text = item->getText();
                if(font && text != "")
                {
-                       core::rect<s32> rect2(rect.UpperLeftCorner,
-                                       (core::dimension2d<u32>(rect.getWidth(), 15)));
+                       v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
+                       v2s32 sdim(dim.X,dim.Y);
+
+                       core::rect<s32> rect2(
+                               /*rect.UpperLeftCorner,
+                               core::dimension2d<u32>(rect.getWidth(), 15)*/
+                               rect.LowerRightCorner - sdim,
+                               sdim
+                       );
 
                        video::SColor bgcolor(128,0,0,0);
                        driver->draw2DRectangle(bgcolor, rect2, clip);
-
+                       
                        font->draw(text.c_str(), rect2,
                                        video::SColor(255,255,255,255), false, false,
                                        clip);
@@ -184,10 +185,16 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s)
 {
        video::IVideoDriver* driver = Environment->getVideoDriver();
 
+       // Get font
+       gui::IGUIFont *font = NULL;
+       gui::IGUISkin* skin = Environment->getSkin();
+       if (skin)
+               font = skin->getFont();
+       
        InventoryList *ilist = m_inventory->getList(s.listname);
        
        core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
-
+       
        for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
        {
                s32 x = (i%s.geom.X) * spacing.X;
@@ -204,10 +211,19 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s)
                        driver->draw2DRectangle(video::SColor(255,255,0,0),
                                        core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
                                                        rect.LowerRightCorner + v2s32(2,2)),
-                                                       &AbsoluteClippingRect);
+                                       &AbsoluteClippingRect);
+               }
+
+               if(item)
+               {
+                       drawInventoryItem(driver, font, item,
+                                       rect, &AbsoluteClippingRect);
+               }
+               else
+               {
+                       video::SColor bgcolor(255,128,128,128);
+                       driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
                }
-               drawInventoryItem(Environment, item,
-                               rect, &AbsoluteClippingRect);
        }
 }
 
index 6211bb24bdf2eed3823b8a755f206b9e7af728cf..45a5e236a42ebbcb987e7c9f7ed76cfab2469b8c 100644 (file)
@@ -26,9 +26,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "utility.h"
 #include "modalMenu.h"
 
-void drawInventoryItem(gui::IGUIEnvironment* env,
+void drawInventoryItem(video::IVideoDriver *driver,
+               gui::IGUIFont *font,
                InventoryItem *item, core::rect<s32> rect,
-               const core::rect<s32> *clip=0);
+               const core::rect<s32> *clip);
 
 class GUIInventoryMenu : public GUIModalMenu
 {
index 1f8091a7f9baf730af55acf8e3c7736cbb23b572..bcc8dc446c256d6ef3a9fef8d3f353c0fd546ee7 100644 (file)
@@ -399,11 +399,14 @@ extern void set_default_settings();
 IrrlichtDevice *g_device = NULL;\r
 Client *g_client = NULL;\r
 \r
-//const s16 quickinv_size = 48;\r
-//const s16 quickinv_spacing = 64;\r
-const s16 quickinv_size = 32;\r
-const s16 quickinv_spacing = 40;\r
-const s16 quickinv_itemcount = 8;\r
+/*const s16 quickinv_size = 40;\r
+const s16 quickinv_padding = 8;\r
+const s16 quickinv_spacing = quickinv_size + quickinv_padding;\r
+const s16 quickinv_outer_padding = 4;\r
+const s16 quickinv_itemcount = 8;*/\r
+\r
+const s32 hotbar_itemcount = 8;\r
+const s32 hotbar_imagesize = 36;\r
 \r
 /*\r
        GUI Stuff\r
@@ -629,24 +632,14 @@ public:
                                        }\r
                                }\r
 \r
-                               // Material selection\r
-                               /*if(event.KeyInput.Key == irr::KEY_KEY_F)\r
-                               {\r
-                                       if(g_selected_item < PLAYER_INVENTORY_SIZE-1)\r
-                                               g_selected_item++;\r
-                                       else\r
-                                               g_selected_item = 0;\r
-                                       dstream<<DTIME<<"Selected item: "\r
-                                                       <<g_selected_item<<std::endl;\r
-                               }*/\r
-\r
+                               // Item selection\r
                                if(event.KeyInput.Key >= irr::KEY_KEY_0\r
                                                && event.KeyInput.Key <= irr::KEY_KEY_9)\r
                                {\r
                                        u16 s1 = event.KeyInput.Key - irr::KEY_KEY_0;\r
                                        if(event.KeyInput.Key == irr::KEY_KEY_0)\r
                                                s1 = 10;\r
-                                       if(s1 < PLAYER_INVENTORY_SIZE)\r
+                                       if(s1 < PLAYER_INVENTORY_SIZE && s1 < hotbar_itemcount)\r
                                                g_selected_item = s1-1;\r
                                        dstream<<DTIME<<"Selected item: "\r
                                                        <<g_selected_item<<std::endl;\r
@@ -715,9 +708,12 @@ public:
                                {\r
                                        /*dstream<<"event.MouseInput.Wheel="\r
                                                        <<event.MouseInput.Wheel<<std::endl;*/\r
+                                       \r
+                                       u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,\r
+                                                       hotbar_itemcount-1);\r
                                        if(event.MouseInput.Wheel < 0)\r
                                        {\r
-                                               if(g_selected_item < PLAYER_INVENTORY_SIZE-1)\r
+                                               if(g_selected_item < max_item)\r
                                                        g_selected_item++;\r
                                                else\r
                                                        g_selected_item = 0;\r
@@ -727,7 +723,7 @@ public:
                                                if(g_selected_item > 0)\r
                                                        g_selected_item--;\r
                                                else\r
-                                                       g_selected_item = PLAYER_INVENTORY_SIZE-1;\r
+                                                       g_selected_item = max_item;\r
                                        }\r
                                }\r
                        }\r
@@ -1179,6 +1175,8 @@ void updateViewingRange(f32 frametime_in, Client *client)
        frametime_old = frametime;\r
 }\r
 \r
+#if 0\r
+// TODO: Remove\r
 class GUIQuickInventory\r
 {\r
 public:\r
@@ -1193,6 +1191,9 @@ public:
        {\r
                core::rect<s32> imgsize(0,0,quickinv_size,quickinv_size);\r
                core::rect<s32> textsize(0,0,quickinv_size,quickinv_size);\r
+               bgtext = env->addStaticText(L"", core::rect<s32>(0,0,1,1), false, false);\r
+               bgtext->setBackgroundColor(\r
+                               video::SColor(128,0,0,0));\r
                for(s32 i=0; i<m_itemcount; i++)\r
                {\r
                        m_images.push_back(env->addImage(\r
@@ -1224,6 +1225,7 @@ public:
                {\r
                        m_images[i]->remove();\r
                }\r
+               bgtext->remove();\r
        }\r
 \r
        void updatePosition(v2s32 pos)\r
@@ -1234,6 +1236,10 @@ public:
                        m_images[i]->setRelativePosition(pos + spacing*i);\r
                        m_texts[i]->setRelativePosition(pos + spacing*i);\r
                }\r
+               core::rect<s32> bgrect(-quickinv_outer_padding,-quickinv_outer_padding,\r
+                               (quickinv_itemcount-1)*quickinv_spacing+quickinv_size+quickinv_outer_padding,\r
+                               quickinv_size+quickinv_outer_padding);\r
+               bgtext->setRelativePosition(bgrect+pos);\r
        }\r
 \r
        void setSelection(s32 i)\r
@@ -1265,7 +1271,7 @@ public:
                                m_images[i]->setImage(NULL);\r
 \r
                                if(m_selection == j)\r
-                                       m_texts[i]->setText(L"<-");\r
+                                       m_texts[i]->setText(L"->");\r
                                else\r
                                        m_texts[i]->setText(L"");\r
 \r
@@ -1277,9 +1283,9 @@ public:
                        m_images[i]->setImage(item->getImage());\r
                        \r
                        std::ostringstream os;\r
-                       os<<item->getText();\r
                        if(m_selection == j)\r
-                               os<<" <-";\r
+                               os<<"-> ";\r
+                       os<<item->getText();\r
                        m_texts[i]->setText(narrow_to_wide(os.str()).c_str());\r
 \r
                        /*wchar_t t[10];\r
@@ -1293,11 +1299,66 @@ public:
 \r
 private:\r
        s32 m_itemcount;\r
+       gui::IGUIStaticText *bgtext;\r
        core::array<gui::IGUIStaticText*> m_texts;\r
        core::array<gui::IGUIImage*> m_images;\r
        Inventory *m_inventory;\r
        s32 m_selection;\r
 };\r
+#endif\r
+\r
+void draw_hotbar(video::IVideoDriver *driver, gui::IGUIFont *font,\r
+               v2s32 centerlowerpos, s32 imgsize, s32 itemcount,\r
+               Inventory *inventory)\r
+{\r
+       InventoryList *mainlist = inventory->getList("main");\r
+       if(mainlist == NULL)\r
+       {\r
+               dstream<<"WARNING: draw_hotbar(): mainlist == NULL"<<std::endl;\r
+               return;\r
+       }\r
+       \r
+       s32 padding = imgsize/12;\r
+       //s32 height = imgsize + padding*2;\r
+       s32 width = itemcount*(imgsize+padding*2);\r
+       \r
+       // Position of upper left corner of bar\r
+       v2s32 pos = centerlowerpos - v2s32(width/2, imgsize+padding*2);\r
+       \r
+       // Draw background color\r
+       /*core::rect<s32> barrect(0,0,width,height);\r
+       barrect += pos;\r
+       video::SColor bgcolor(255,128,128,128);\r
+       driver->draw2DRectangle(bgcolor, barrect, NULL);*/\r
+\r
+       core::rect<s32> imgrect(0,0,imgsize,imgsize);\r
+\r
+       for(s32 i=0; i<itemcount; i++)\r
+       {\r
+               InventoryItem *item = mainlist->getItem(i);\r
+               \r
+               core::rect<s32> rect = imgrect + pos\r
+                               + v2s32(padding+i*(imgsize+padding*2), padding);\r
+               \r
+               if(g_selected_item == i)\r
+               {\r
+                       driver->draw2DRectangle(video::SColor(255,255,0,0),\r
+                                       core::rect<s32>(rect.UpperLeftCorner - v2s32(1,1)*padding,\r
+                                                       rect.LowerRightCorner + v2s32(1,1)*padding),\r
+                                       NULL);\r
+               }\r
+               else\r
+               {\r
+                       video::SColor bgcolor2(128,0,0,0);\r
+                       driver->draw2DRectangle(bgcolor2, rect, NULL);\r
+               }\r
+\r
+               if(item != NULL)\r
+               {\r
+                       drawInventoryItem(driver, font, item, rect, NULL);\r
+               }\r
+       }\r
+}\r
 \r
 // Chat data\r
 struct ChatLine\r
@@ -2047,8 +2108,8 @@ int main(int argc, char *argv[])
 \r
        /*GUIQuickInventory *quick_inventory = new GUIQuickInventory\r
                        (guienv, NULL, v2s32(10, 70), 5, &local_inventory);*/\r
-       GUIQuickInventory *quick_inventory = new GUIQuickInventory\r
-                       (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);\r
+       /*GUIQuickInventory *quick_inventory = new GUIQuickInventory\r
+                       (guienv, NULL, v2s32(0, 0), quickinv_itemcount, &local_inventory);*/\r
        \r
        // Test the text input system\r
        /*(new GUITextInputMenu(guienv, guiroot, -1, &g_menumgr,\r
@@ -2121,7 +2182,7 @@ int main(int argc, char *argv[])
                last_screensize = screensize;\r
                screensize = driver->getScreenSize();\r
                v2s32 displaycenter(screensize.X/2,screensize.Y/2);\r
-               bool screensize_changed = screensize != last_screensize;\r
+               //bool screensize_changed = screensize != last_screensize;\r
                \r
                // Hilight boxes collected during the loop and displayed\r
                core::list< core::aabbox3d<f32> > hilightboxes;\r
@@ -2130,11 +2191,11 @@ int main(int argc, char *argv[])
                std::wstring infotext;\r
 \r
                // When screen size changes, update positions and sizes of stuff\r
-               if(screensize_changed)\r
+               /*if(screensize_changed)\r
                {\r
                        v2s32 pos(displaycenter.X-((quickinv_itemcount-1)*quickinv_spacing+quickinv_size)/2, screensize.Y-quickinv_spacing);\r
                        quick_inventory->updatePosition(pos);\r
-               }\r
+               }*/\r
 \r
                //TimeTaker //timer1("//timer1");\r
                \r
@@ -2985,8 +3046,8 @@ int main(int argc, char *argv[])
                        old_selected_item = g_selected_item;\r
                        //std::cout<<"Updating local inventory"<<std::endl;\r
                        client.getLocalInventory(local_inventory);\r
-                       quick_inventory->setSelection(g_selected_item);\r
-                       quick_inventory->update();\r
+                       /*quick_inventory->setSelection(g_selected_item);\r
+                       quick_inventory->update();*/\r
                }\r
                \r
                /*\r
@@ -3089,6 +3150,14 @@ int main(int argc, char *argv[])
                */\r
                // 0-1ms\r
                guienv->drawAll();\r
+\r
+               /*\r
+                       Draw hotbar\r
+               */\r
+               {\r
+                       draw_hotbar(driver, font, v2s32(displaycenter.X, screensize.Y),\r
+                                       hotbar_imagesize, hotbar_itemcount, &local_inventory);\r
+               }\r
                \r
                // End drawing\r
                {\r
@@ -3123,7 +3192,7 @@ int main(int argc, char *argv[])
                        device->yield();*/\r
        }\r
 \r
-       delete quick_inventory;\r
+       //delete quick_inventory;\r
 \r
        /*\r
                Disable texture fetches and other stuff that is queued\r