MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NeighborSelectPanel.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  NeighborSelectPanel.h Panel for selecting neighbors in a Manhattan Neighborhood
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
27 #ifndef NEIGHBOR_SELECT_PANEL_H
28 #define NEIGHBOR_SELECT_PANEL_H
29 
30 #include "Panel.h"
31 #include "Parameter.h"
32 #include "ParameterController.h"
33 
34 namespace MFM
35 {
36  template <class CC, u32 R>
38  {
39  private:
41 
42  enum
43  {
44  SITES = EVENT_WINDOW_SITES(R)
45  };
46 
47  public:
48  typedef enum
49  {
50  NEIGHBOR_SELECT_MANY = 0,
51  NEIGHBOR_SELECT_ONE
52  }NeighborhoodSelectMode;
53 
54  private:
55  enum
56  {
57  CELL_SIZE = 16,
58  BORDER_SIZE = 4,
59 
60  CELL_SIZE_BIG = 24
61  };
62 
63  inline u32 GetCellSize() const
64  {
65  return Super::m_bigText ?
66  CELL_SIZE_BIG :
67  CELL_SIZE;
68  }
69 
70  inline FontAsset GetFontAsset() const
71  {
72  return Super::m_bigText ?
73  FONT_ASSET_HELPPANEL_BIG :
74  FONT_ASSET_HELPPANEL_SMALL;
75  }
76 
77  NeighborhoodSelectMode m_selectMode;
78 
79  SPoint m_selectedOne;
80 
81  BitVector<SITES> m_bitField;
82 
83  const char* m_text;
84 
85  static const u32 ENABLED_COLOR = 0xff20a020;
86 
87  MDist<R> & GetNeighborhood() const
88  {
89  return MDist<R>::get();
90  }
91 
92  void FlipBit(u32 bitNum)
93  {
94  if(IsBitOn(bitNum))
95  {
96  ClearBit(bitNum);
97  }
98  else
99  {
100  SetBit(bitNum);
101  }
102  }
103 
104  void SetBit(u32 bitNum)
105  {
106  m_bitField.WriteBit(bitNum, true);
108  {
112 
113  np->SetBit(bitNum);
114  }
115  }
116 
117  void ClearBit(u32 bitNum)
118  {
119  m_bitField.WriteBit(bitNum, false);
121  {
125 
126  np->ClearBit(bitNum);
127  }
128  }
129 
130  bool IsBitOn(u32 bitNum)
131  {
132  return m_bitField.ReadBit(bitNum);
133  }
134 
135  void PaintButtons(Drawing& d)
136  {
137  s32 offset = R * GetCellSize() + BORDER_SIZE;
138  SPoint renderPt;
139  MDist<R> & n = GetNeighborhood();
140 
141  for(u32 i = n.GetFirstIndex(0); i <= n.GetLastIndex(R); i++)
142  {
143  renderPt.Set(offset + (GetCellSize() - 1) * n.GetPoint(i).GetX(),
144  offset + (GetCellSize() - 1) * n.GetPoint(i).GetY());
145 
146  d.SetForeground(Drawing::BLACK);
147  d.FillRect(renderPt.GetX(), renderPt.GetY(), GetCellSize(), GetCellSize());
148 
149  switch(m_selectMode)
150  {
151  case NEIGHBOR_SELECT_MANY:
152  d.SetForeground(IsBitOn(i - n.GetFirstIndex(0)) ?
153  ENABLED_COLOR : Panel::GetForeground());
154  break;
155  case NEIGHBOR_SELECT_ONE:
156  d.SetForeground(n.GetPoint(i).Equals(m_selectedOne) ?
157  ENABLED_COLOR : Panel::GetForeground());
158  }
159  d.FillRect(renderPt.GetX() + 1, renderPt.GetY() + 1, GetCellSize() - 2, GetCellSize() - 2);
160  }
161  }
162 
163  void PaintText(Drawing& d)
164  {
165  UPoint textLoc(R * (GetCellSize() + 1) * 2 + GetCellSize() + BORDER_SIZE,
166  R * (GetCellSize() - 1) + BORDER_SIZE);
167 
168  d.SetForeground(Drawing::WHITE);
169  d.SetBackground(Drawing::GREY40);
170  d.SetFont(GetFontAsset());
171  if(m_text)
172  {
173  d.BlitBackedText(m_text, textLoc, MakeUnsigned(d.GetTextSize(m_text)));
174  }
175  }
176 
177  public:
180  m_selectMode(NEIGHBOR_SELECT_MANY),
181  m_selectedOne(0, 0)
182  {
183  Init();
184  }
185 
186  virtual void Init()
187  {
188  Panel::SetDesiredSize(500, (2 * BORDER_SIZE) + (R * 2 + 1) * GetCellSize());
189  Panel::SetForeground(Drawing::GREY80);
190  }
191 
192  void SetParameter(ElementParameter<CC>* pb)
193  {
194  Super::SetParameter(pb);
198 
199  if (!np)
200  {
201  FAIL(ILLEGAL_ARGUMENT);
202  }
203 
204  u64 ngb = np->GetValue();
205  m_bitField.WriteLong(0, SITES, ngb);
206  LOG.Debug("Neighborhood initted to %@", np);
207  }
208 
209  void SetSelectMode(NeighborhoodSelectMode mode)
210  {
211  m_selectMode = mode;
212  m_bitField.Clear();
213  m_selectedOne(0, 0);
214  }
215 
216  void SetText(const char* text)
217  {
218  m_text = text;
219  }
220 
221  virtual void PaintComponent(Drawing& d)
222  {
224  d.FillRect(0, 0, Panel::GetDimensions().GetX(), Panel::GetDimensions().GetY());
225 
226  PaintButtons(d);
227  PaintText(d);
228  }
229 
230  virtual bool Handle(MouseButtonEvent& event)
231  {
232  if(event.m_event.type == SDL_MOUSEBUTTONDOWN &&
233  event.m_event.button.button == SDL_BUTTON_LEFT)
234  {
235  s32 offset = R * GetCellSize() + BORDER_SIZE;
236  MDist<R> & n = GetNeighborhood();
237  Rect buttonRect;
238  SPoint clickPt(event.GetAt().GetX() - Panel::GetRenderPoint().GetX(),
239  event.GetAt().GetY() - Panel::GetRenderPoint().GetY());
240  buttonRect.SetSize(UPoint(GetCellSize(), GetCellSize()));
241 
242  for(u32 i = n.GetFirstIndex(0); i <= n.GetLastIndex(R); i++)
243  {
244  buttonRect.SetPosition(SPoint(offset + (GetCellSize() - 1) * n.GetPoint(i).GetX(),
245  offset + (GetCellSize() - 1) * n.GetPoint(i).GetY()));
246 
247  if(buttonRect.Contains(clickPt))
248  {
249  switch(m_selectMode)
250  {
251  case NEIGHBOR_SELECT_MANY:
252  FlipBit(i - n.GetFirstIndex(0));
253  break;
254  case NEIGHBOR_SELECT_ONE:
255  m_selectedOne.Set(n.GetPoint(i).GetX(), n.GetPoint(i).GetY());
256  break;
257  }
258  return true;
259  }
260  }
261  }
262  return false;
263  }
264  };
265 }
266 
267 
268 #endif /* NEIGHBOR_SELECT_PANEL_H */
Definition: Rect.h:40
u32 GetFirstIndex(const u32 radius) const
Definition: MDist.h:112
void Clear()
Definition: BitVector.tcc:26
Definition: Parameter.h:43
u32 GetLastIndex(const u32 radius) const
Definition: MDist.h:129
u32 SetForeground(const u32 color)
Definition: Drawing.h:188
Definition: Panel.h:74
void SetSize(const UPoint &newSize)
Definition: Rect.h:229
void WriteBit(u32 idx, bool bit)
Definition: BitVector.tcc:41
void SetPosition(const SPoint &newPos)
Definition: Rect.h:167
u32 SetForeground(const u32 color)
Definition: Panel.h:256
void Set(T x, T y)
Definition: Point.tcc:183
T GetY() const
Definition: Point.tcc:40
Definition: ParameterController.h:40
void BlitBackedText(const char *message, UPoint loc, UPoint size)
Definition: Drawing.cpp:233
SPoint GetTextSize(const char *message)
Definition: Drawing.cpp:201
u32 SetBackground(const u32 color)
Definition: Drawing.h:170
u32 GetForeground() const
Definition: Panel.h:248
Definition: MDist.h:69
bool Contains(const SPoint &point)
Definition: Rect.h:127
void Debug(const char *format,...)
Definition: Logger.h:301
virtual void PaintComponent(Drawing &d)
Definition: NeighborSelectPanel.h:221
static MDist< R > & get()
Definition: MDist.tcc:193
bool ReadBit(u32 idx)
Definition: BitVector.tcc:65
void FillRect(int x, int y, int w, int h) const
Definition: Drawing.cpp:115
#define EVENT_WINDOW_SITES(radius)
Definition: MDist.h:46
void WriteLong(const u32 startIdx, const u32 length, const u64 value)
Definition: BitVector.tcc:87
TTF_Font * SetFont(TTF_Font *newFont)
Definition: Drawing.h:209
virtual bool Handle(MouseButtonEvent &event)
Definition: NeighborSelectPanel.h:230
Definition: NeighborSelectPanel.h:37
Definition: Parameter.h:715
Definition: Drawing.h:44
T GetX() const
Definition: Point.tcc:34