File: MenuView.cpp

package info (click to toggle)
arkrpg 0.1.4b-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,104 kB
  • ctags: 5,445
  • sloc: cpp: 28,145; sh: 9,006; ansic: 3,259; makefile: 344
file content (208 lines) | stat: -rw-r--r-- 5,008 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* $Id: MenuView.cpp,v 1.8 2003/03/23 20:09:25 mrq Exp $
**
** Ark - Libraries, Tools & Programs for MMORPG developpements.
** Copyright (C) 1999-2000 The Contributors of the ArkRPG Project
** Please see the file "AUTHORS" for a list of contributors
**
** This program 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.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef WIN32
 #include <windows.h>
#endif

#include <Ark/ArkSystem.h>
#include <Client/MenuView.h>
#include <iostream>
#include <sstream>

namespace Client
{  
   MenuButton::MenuButton ()
   {
      m_State = NORMAL;
   }

   MenuButton::MenuButton (int xpos, int ypos,
			   Ark::Cache *cache,
			   const Ark::String &imgname,
			   const Ark::String &action,
			   scalar scrfactor)
   {
      const char *names[3] = {"", "_hover", "_press"};

      for (size_t i = 0; i < NUMIMAGES; ++i)
      {
	 Ark::String fullname = (Ark::String("{menu}/") + imgname + names[i] + ".png");
	 cache->Get(Ark::V_TEXTURE, fullname, m_Images[i]);
	 m_Images[i]->Configure();
      }

      m_State = NORMAL;
      m_X = xpos;
      m_Y = ypos;
      m_Action = action;
      m_ScrFactor = scrfactor;
   }

   MenuButton::~MenuButton()
   {
   }

   bool
   MenuButton::InArea (int x, int y)
   {
      if (x >= (m_X * m_ScrFactor) &&
	  x < (m_X + m_Images[0]->GetWidth())*m_ScrFactor && 
	  y >= (m_Y * m_ScrFactor) &&
	  y < (m_Y + m_Images[0]->GetHeight())*m_ScrFactor)
	 return true;
      else
	 return false;
   }
		       
   void
   MenuButton::Render(UIRenderer *ui)
   {
      int mx, my;
      SDL_GetMouseState (&mx, &my);

      if (m_Images[m_State])
      {
	 if (m_State != PRESS)
	 {
	    if (InArea (mx, my))  m_State = HOVER;
	    else m_State = NORMAL;
	 }
	    
	 ui->SetTexture (m_Images[m_State]);
	 ui->DrawTexturedRectangle
	    ((int)(m_X * m_ScrFactor),
	     (int)(m_Y * m_ScrFactor),
	     (int)(m_Images[m_State]->GetWidth() * m_ScrFactor),
	     (int)(m_Images[m_State]->GetHeight() * m_ScrFactor));
	 ui->Flush();
      }
   }

   bool
   MenuButton::HandleMButton (bool down, int b, int x, int y)
   {
      if (down && InArea(x,y) && b == 1)
      {
	 m_State = PRESS;
	 return true;
      }
      else if (!down && m_State == PRESS && b == 1)
      {
	 g_Client->HandleMenuString(m_Action);
	 Ark::Sys()->Log("User selected '%s'\n", m_Action.c_str());
	 m_State = NORMAL;
	 return true;
      }

      return false;
   }

   bool
   MenuButton::HandleMotion (int state, int x, int y)
   {
      return true;
   }
   
   ////////////////////////////////////////////////////////////////////////


   MenuView::MenuView (UIRenderer *rnd) 
   {
      Ark::Config menucfg;
      Ark::Cache *cache = &rnd->Rdr()->GetCache();

      menucfg.Load("{menu}/menu.cfg");

      ////////////
      cache->Get(Ark::V_TEXTURE,
		 Ark::String("{menu}/") + menucfg.GetStr("menu::BgImage", "")
		 + ".png",  m_Bg);

      if (m_Bg) m_Bg->Configure();
      const float scrfactor = scalar(rnd->width) / scalar(m_Bg->GetWidth());

      ///////////
      size_t numbuttons = menucfg.GetInt("menu::NumButtons", 0);

      m_Children.resize(numbuttons);
      for (size_t i = 0; i < numbuttons; ++i)
      {
	 std::ostringstream sstr;
	 sstr << "menu::Button" << i << "::";
	 Ark::String prefix = sstr.str();

	 m_Children[i] = MenuButton
	    (menucfg.GetInt(prefix + "X", 0),
	     menucfg.GetInt(prefix + "Y", 0),
	     cache,
	     menucfg.GetStr(prefix + "Image", ""),
	     menucfg.GetStr(prefix + "Action", ""),
	     scrfactor);
      }
   }

   MenuView::~MenuView ()
   {
   }
   
   void
   MenuView::Render(UIRenderer *ui)
   {
      ui->SetTexture (m_Bg);
      ui->DrawTexturedRectangle (0, 0, ui->width, ui->height);

      // Render my children.
      std::vector<MenuButton>::iterator i;
      for (i = m_Children.begin(); i != m_Children.end(); ++i)
	 i->Render (ui);
   }

   void
   MenuView::HandleMButton (bool down, int b, int x, int y)
   {
      std::vector<MenuButton>::iterator i;
	    
      for (i = m_Children.begin(); i != m_Children.end(); ++i)
      {
	 if (i->HandleMButton (down, b, x, y))
	    return;
      }
   }

   void
   MenuView::HandleMotion (int state, int x, int y)
   {
      std::vector<MenuButton>::iterator i;
	    
      for (i = m_Children.begin(); i != m_Children.end(); ++i)
      {
	 if (i->HandleMotion (state, x, y))
	    return;
      }
   }

}