]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Menu.cpp
Replace MenuItem::init with a constructor
[lugaru.git] / Source / Menu.cpp
index 8eb10e4e33091e04bc4acfbd9b9927bf8fb0d750..6e2df4c5a412a9ff7abb22c7a173b274be15315b 100644 (file)
@@ -1,54 +1,60 @@
+/*
+Copyright (C) 2003, 2010 - Wolfire Games
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
+
+This file is part of Lugaru.
+
+Lugaru is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+Lugaru is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
 #include <vector>
 #include <string>
 #include "gamegl.h"
 
 #include "Menu.h"
-using namespace Menu;
 
 extern float multiplier;
 
-struct MenuItem {
-    enum MenuItemType {NONE, LABEL, BUTTON, IMAGE, IMAGEBUTTON, MAPMARKER, MAPLINE, MAPLABEL} type;
-    int id;
-    string text;
-    Texture texture;
-    int x, y, w, h;
-    float r, g, b;
-    float effectfade;
-
-    float linestartsize;
-    float lineendsize;
+std::vector<MenuItem> Menu::items;
 
-    void init(MenuItemType _type, int _id, const string& _text, Texture _texture,
-              int _x, int _y, int _w, int _h, float _r, float _g, float _b,
-              float _linestartsize = 1, float _lineendsize = 1) {
-        type = _type;
-        id = _id;
-        text = _text;
-        texture = _texture;
-        x = _x;
-        y = _y;
-        w = _w;
-        h = _h;
-        r = _r;
-        g = _g;
-        b = _b;
-        effectfade = 0;
-        linestartsize = _linestartsize;
-        lineendsize = _lineendsize;
-        if (type == MenuItem::BUTTON) {
-            if (w == -1)
-                w = text.length() * 10;
-            if (h == -1)
-                h = 20;
+MenuItem::MenuItem(MenuItemType _type, int _id, const string& _text, Texture _texture,
+          int _x, int _y, int _w, int _h, float _r, float _g, float _b,
+          float _linestartsize, float _lineendsize):
+    type(_type),
+    id(_id),
+    text(_text),
+    texture(_texture),
+    x(_x),
+    y(_y),
+    w(_w),
+    h(_h),
+    r(_r),
+    g(_g),
+    b(_b),
+    effectfade(0),
+    linestartsize(_linestartsize),
+    lineendsize(_lineendsize)
+{
+    if (type == MenuItem::BUTTON) {
+        if (w == -1) {
+            w = text.length() * 10;
+        }
+        if (h == -1) {
+            h = 20;
         }
     }
-};
-
-vector<MenuItem> items;
-
-
-
+}
 
 void Menu::clearMenu()
 {
@@ -57,38 +63,31 @@ void Menu::clearMenu()
 
 void Menu::addLabel(int id, const string& text, int x, int y, float r, float g, float b)
 {
-    items.push_back(MenuItem());
-    items.back().init(MenuItem::LABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
+    items.emplace_back(MenuItem::LABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
 }
 void Menu::addButton(int id, const string& text, int x, int y, float r, float g, float b)
 {
-    items.push_back(MenuItem());
-    items.back().init(MenuItem::BUTTON, id, text, Texture(), x, y, -1, -1, r, g, b);
+    items.emplace_back(MenuItem::BUTTON, id, text, Texture(), x, y, -1, -1, r, g, b);
 }
 void Menu::addImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
 {
-    items.push_back(MenuItem());
-    items.back().init(MenuItem::IMAGE, id, "", texture, x, y, w, h, r, g, b);
+    items.emplace_back(MenuItem::IMAGE, id, "", texture, x, y, w, h, r, g, b);
 }
 void Menu::addButtonImage(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
 {
-    items.push_back(MenuItem());
-    items.back().init(MenuItem::IMAGEBUTTON, id, "", texture, x, y, w, h, r, g, b);
+    items.emplace_back(MenuItem::IMAGEBUTTON, id, "", texture, x, y, w, h, r, g, b);
 }
 void Menu::addMapLine(int x, int y, int w, int h, float startsize, float endsize, float r, float g, float b)
 {
-    items.push_back(MenuItem());
-    items.back().init(MenuItem::MAPLINE, -1, "", Texture(), x, y, w, h, r, g, b, startsize, endsize);
+    items.emplace_back(MenuItem::MAPLINE, -1, "", Texture(), x, y, w, h, r, g, b, startsize, endsize);
 }
 void Menu::addMapMarker(int id, Texture texture, int x, int y, int w, int h, float r, float g, float b)
 {
-    items.push_back(MenuItem());
-    items.back().init(MenuItem::MAPMARKER, id, "", texture, x, y, w, h, r, g, b);
+    items.emplace_back(MenuItem::MAPMARKER, id, "", texture, x, y, w, h, r, g, b);
 }
 void Menu::addMapLabel(int id, const string& text, int x, int y, float r, float g, float b)
 {
-    items.push_back(MenuItem());
-    items.back().init(MenuItem::MAPLABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
+    items.emplace_back(MenuItem::MAPLABEL, id, text, Texture(), x, y, -1, -1, r, g, b);
 }
 
 void Menu::setText(int id, const string& text)
@@ -132,7 +131,7 @@ int Menu::getSelected(int mousex, int mousey)
     return -1;
 }
 
-void GUITick()
+void Menu::handleFadeEffect()
 {
     for (vector<MenuItem>::iterator it = items.begin(); it != items.end(); it++) {
         if (it->id == Game::selected) {
@@ -149,7 +148,7 @@ void GUITick()
 
 void Menu::drawItems()
 {
-    GUITick();
+    handleFadeEffect();
     glEnable(GL_TEXTURE_2D);
     glEnable(GL_ALPHA_TEST);
     glEnable(GL_BLEND);
@@ -242,15 +241,18 @@ void Menu::drawItems()
             glPushMatrix();
             glTranslatef(2, -5, 0); //from old code
             glBegin(GL_QUADS);
-            glVertex3f(linestart.x - offset.x * it->linestartsize,     linestart.y - offset.y * it->linestartsize,      0.0f);
-            glVertex3f(linestart.x + offset.x * it->linestartsize,     linestart.y + offset.y * it->linestartsize,      0.0f);
-            glVertex3f(lineend.x + offset.x * it->lineendsize,         lineend.y + offset.y * it->lineendsize, 0.0f);
-            glVertex3f(lineend.x - offset.x * it->lineendsize,                 lineend.y - offset.y * it->lineendsize, 0.0f);
+            glVertex3f(linestart.x - offset.x * it->linestartsize, linestart.y - offset.y * it->linestartsize, 0.0f);
+            glVertex3f(linestart.x + offset.x * it->linestartsize, linestart.y + offset.y * it->linestartsize, 0.0f);
+            glVertex3f(lineend.x + offset.x * it->lineendsize, lineend.y + offset.y * it->lineendsize, 0.0f);
+            glVertex3f(lineend.x - offset.x * it->lineendsize, lineend.y - offset.y * it->lineendsize, 0.0f);
             glEnd();
             glPopMatrix();
             glEnable(GL_TEXTURE_2D);
         }
         break;
+        default:
+        case MenuItem::NONE:
+        break;
         }
     }
 }