1 //
2 // This file is part of Gambit
3 // Copyright (c) 1994-2016, The Gambit Project (http://www.gambit-project.org)
4 //
5 // FILE: src/gui/efglayout.h
6 // Interface to tree layout representation
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 //
22 
23 #ifndef EFGLAYOUT_H
24 #define EFGLAYOUT_H
25 
26 #include "gambit/gambit.h"
27 #include "style.h"
28 #include "gamedoc.h"
29 
30 class gbtNodeEntry {
31 private:
32   Gambit::GameNode m_node;        // the corresponding node in the game
33   gbtNodeEntry *m_parent; // parent node
34   int m_x, m_y;        // Cartesian coordinates of node
35   gbtNodeEntry *m_nextMember;  // entry of next information set member
36   bool m_inSupport;    // true if node reachable in current support
37   int m_size;         // horizontal size of the node
38   mutable wxRect m_outcomeRect;
39   mutable Gambit::Array<wxRect> m_payoffRect;
40   mutable wxRect m_branchAboveRect, m_branchBelowRect;
41   int m_token;        // token to draw for node
42   wxColour m_color;   // color of node
43 
44   int m_branchStyle;  // lines or fork-tine
45   int m_branchLabel;  // horizontal or rotated
46   int m_branchLength; // length of branch (exclusive of tine, if present)
47 
48   int m_level;        // depth of the node in tree
49   int m_sublevel;     // # of the infoset line on this level
50   double m_actionProb;  // probability incoming action is taken
51 
52   wxString m_nodeAboveLabel, m_nodeBelowLabel;
53   wxString m_branchAboveLabel, m_branchBelowLabel;
54 
55   wxFont m_nodeAboveFont, m_nodeBelowFont;
56   wxFont m_branchAboveFont, m_branchBelowFont;
57 
58   const gbtStyle *m_style;
59 
60 public:
61   gbtNodeEntry(Gambit::GameNode p_parent);
62 
GetNode(void)63   Gambit::GameNode GetNode(void) const { return m_node; }
64 
GetParent(void)65   gbtNodeEntry *GetParent(void) const { return m_parent; }
SetParent(gbtNodeEntry * p_parent)66   void SetParent(gbtNodeEntry *p_parent) { m_parent = p_parent; }
67 
X(void)68   int X(void) const { return m_x; }
SetX(int p_x)69   void SetX(int p_x) { m_x = p_x; }
Y(void)70   int Y(void) const { return m_y; }
SetY(int p_y)71   void SetY(int p_y) { m_y = p_y; }
72 
GetNextMember(void)73   gbtNodeEntry *GetNextMember(void) const { return m_nextMember; }
SetNextMember(gbtNodeEntry * p_member)74   void SetNextMember(gbtNodeEntry *p_member) { m_nextMember = p_member; }
75 
InSupport(void)76   bool InSupport(void) const { return m_inSupport; }
SetInSupport(bool p_inSupport)77   void SetInSupport(bool p_inSupport) { m_inSupport = p_inSupport; }
78 
79   int GetChildNumber(void) const;
80 
GetColor(void)81   const wxColour &GetColor(void) const { return m_color; }
SetColor(const wxColour & p_color)82   void SetColor(const wxColour &p_color) { m_color = p_color; }
83 
GetSize(void)84   int GetSize(void) const { return m_size; }
SetSize(int p_size)85   void SetSize(int p_size) { m_size = p_size; }
86 
GetToken(void)87   int GetToken(void) const { return m_token; }
SetToken(int p_token)88   void SetToken(int p_token) { m_token = p_token; }
89 
GetBranchStyle(void)90   int GetBranchStyle(void) const { return m_branchStyle; }
SetBranchStyle(int p_style)91   void SetBranchStyle(int p_style) { m_branchStyle = p_style; }
92 
GetBranchLabelStyle(void)93   int GetBranchLabelStyle(void) const { return m_branchLabel; }
SetBranchLabelStyle(int p_style)94   void SetBranchLabelStyle(int p_style) { m_branchLabel = p_style; }
95 
GetBranchLength(void)96   int GetBranchLength(void) const { return m_branchLength; }
SetBranchLength(int p_length)97   void SetBranchLength(int p_length) { m_branchLength = p_length; }
98 
GetLevel(void)99   int GetLevel(void) const { return m_level; }
SetLevel(int p_level)100   void SetLevel(int p_level) { m_level = p_level; }
101 
GetSublevel(void)102   int GetSublevel(void) const { return m_sublevel; }
SetSublevel(int p_sublevel)103   void SetSublevel(int p_sublevel) { m_sublevel = p_sublevel; }
104 
GetNodeAboveLabel(void)105   const wxString &GetNodeAboveLabel(void) const { return m_nodeAboveLabel; }
SetNodeAboveLabel(const wxString & p_label)106   void SetNodeAboveLabel(const wxString &p_label)
107     { m_nodeAboveLabel = p_label; }
108 
GetNodeBelowLabel(void)109   const wxString &GetNodeBelowLabel(void) const { return m_nodeBelowLabel; }
SetNodeBelowLabel(const wxString & p_label)110   void SetNodeBelowLabel(const wxString &p_label)
111     { m_nodeBelowLabel = p_label; }
112 
GetBranchAboveLabel(void)113   const wxString &GetBranchAboveLabel(void) const
114     { return m_branchAboveLabel; }
SetBranchAboveLabel(const wxString & p_label)115   void SetBranchAboveLabel(const wxString &p_label)
116     { m_branchAboveLabel = p_label; }
117 
GetBranchBelowLabel(void)118   const wxString &GetBranchBelowLabel(void) const
119     { return m_branchBelowLabel; }
SetBranchBelowLabel(const wxString & p_label)120   void SetBranchBelowLabel(const wxString &p_label)
121     { m_branchBelowLabel = p_label; }
122 
GetNodeAboveFont(void)123   const wxFont &GetNodeAboveFont(void) const { return m_nodeAboveFont; }
SetNodeAboveFont(const wxFont & p_font)124   void SetNodeAboveFont(const wxFont &p_font) { m_nodeAboveFont = p_font; }
125 
GetNodeBelowFont(void)126   const wxFont &GetNodeBelowFont(void) const { return m_nodeBelowFont; }
SetNodeBelowFont(const wxFont & p_font)127   void SetNodeBelowFont(const wxFont &p_font) { m_nodeBelowFont = p_font; }
128 
GetBranchAboveFont(void)129   const wxFont &GetBranchAboveFont(void) const { return m_branchAboveFont; }
SetBranchAboveFont(const wxFont & p_font)130   void SetBranchAboveFont(const wxFont &p_font) { m_branchAboveFont = p_font; }
131 
GetBranchBelowFont(void)132   const wxFont &GetBranchBelowFont(void) const { return m_branchBelowFont; }
SetBranchBelowFont(const wxFont & p_font)133   void SetBranchBelowFont(const wxFont &p_font) { m_branchBelowFont = p_font; }
134 
GetActionProb(void)135   const double &GetActionProb(void) const { return m_actionProb; }
SetActionProb(const double & p_prob)136   void SetActionProb(const double &p_prob) { m_actionProb = p_prob; }
137 
SetStyle(const gbtStyle * p_style)138   void SetStyle(const gbtStyle *p_style) { m_style = p_style; }
139 
140   bool NodeHitTest(int p_x, int p_y) const;
141 #if wxCHECK_VERSION(2,7,0)
OutcomeHitTest(int p_x,int p_y)142   bool OutcomeHitTest(int p_x, int p_y) const
143   { return (m_outcomeRect.Contains(p_x, p_y)); }
BranchAboveHitTest(int p_x,int p_y)144   bool BranchAboveHitTest(int p_x, int p_y) const
145   { return (m_branchAboveRect.Contains(p_x, p_y)); }
BranchBelowHitTest(int p_x,int p_y)146   bool BranchBelowHitTest(int p_x, int p_y) const
147   { return (m_branchBelowRect.Contains(p_x, p_y)); }
148 #else
OutcomeHitTest(int p_x,int p_y)149   bool OutcomeHitTest(int p_x, int p_y) const
150   { return (m_outcomeRect.Inside(p_x, p_y)); }
BranchAboveHitTest(int p_x,int p_y)151   bool BranchAboveHitTest(int p_x, int p_y) const
152   { return (m_branchAboveRect.Inside(p_x, p_y)); }
BranchBelowHitTest(int p_x,int p_y)153   bool BranchBelowHitTest(int p_x, int p_y) const
154   { return (m_branchBelowRect.Inside(p_x, p_y)); }
155 #endif
156 
GetOutcomeExtent(void)157   const wxRect &GetOutcomeExtent(void) const { return m_outcomeRect; }
GetPayoffExtent(int pl)158   const wxRect &GetPayoffExtent(int pl) const { return m_payoffRect[pl]; }
159 
160   void Draw(wxDC &, Gambit::GameNode selection, bool p_noHints) const;
161   void DrawIncomingBranch(wxDC &) const;
162   void DrawOutcome(wxDC &, bool p_noHints) const;
163 };
164 
165 class gbtEfgDisplay;
166 
167 class gbtTreeLayout : public gbtGameView {
168 private:
169   /* gbtEfgDisplay *m_parent; */
170   Gambit::Array<gbtNodeEntry *> m_nodeList;
171   mutable int m_maxX, m_maxY, m_maxLevel;
172   int m_infosetSpacing;
173 
174   const int c_leftMargin, c_topMargin;
175 
176   gbtNodeEntry *GetEntry(Gambit::GameNode) const;
177 
178   gbtNodeEntry *NextInfoset(gbtNodeEntry *);
179   void CheckInfosetEntry(gbtNodeEntry *);
180 
181   void BuildNodeList(Gambit::GameNode, const Gambit::BehaviorSupportProfile &, int);
182 
183   int LayoutSubtree(Gambit::GameNode, const Gambit::BehaviorSupportProfile &, int &, int &, int &);
184   void FillInfosetTable(Gambit::GameNode, const Gambit::BehaviorSupportProfile &);
185   void UpdateTableInfosets(void);
186   void UpdateTableParents(void);
187 
188   wxString CreateNodeLabel(const gbtNodeEntry *, int) const;
189   wxString CreateBranchLabel(const gbtNodeEntry *, int) const;
190 
191   void RenderSubtree(wxDC &dc, bool p_noHints) const;
192 
193   // Overriding gbtGameView members
OnUpdate(void)194   void OnUpdate(void) { }
195 
196 public:
197   gbtTreeLayout(gbtEfgDisplay *p_parent, gbtGameDocument *p_doc);
~gbtTreeLayout()198   virtual ~gbtTreeLayout() { }
199 
200   Gambit::GameNode PriorSameLevel(Gambit::GameNode) const;
201   Gambit::GameNode NextSameLevel(Gambit::GameNode) const;
202 
203   void BuildNodeList(const Gambit::BehaviorSupportProfile &);
204   void Layout(const Gambit::BehaviorSupportProfile &);
205   void GenerateLabels(void);
206 
207   // The following member functions are for temporary compatibility only
GetNodeEntry(Gambit::GameNode p_node)208   gbtNodeEntry *GetNodeEntry(Gambit::GameNode p_node) const
209     { return GetEntry(p_node); }
210   gbtNodeEntry *GetValidParent(Gambit::GameNode);
211   gbtNodeEntry *GetValidChild(Gambit::GameNode);
212 
MaxX(void)213   int MaxX(void) const { return m_maxX; }
MaxY(void)214   int MaxY(void) const { return m_maxY; }
215 
216   Gambit::GameNode NodeHitTest(int, int) const;
217   Gambit::GameNode OutcomeHitTest(int, int) const;
218   Gambit::GameNode BranchAboveHitTest(int, int) const;
219   Gambit::GameNode BranchBelowHitTest(int, int) const;
220   Gambit::GameNode InfosetHitTest(int, int) const;
221 
222   void Render(wxDC &, bool p_noHints) const;
223 };
224 
225 #endif  // EFGLAYOUT_H
226 
227