1+ package com .codingame .gameengine .module .interactivedisplay ;
2+
3+ import com .codingame .gameengine .core .AbstractPlayer ;
4+ import com .codingame .gameengine .core .GameManager ;
5+ import com .codingame .gameengine .core .Module ;
6+ import com .codingame .gameengine .module .entities .Entity ;
7+ import com .google .inject .Inject ;
8+ import com .google .inject .Singleton ;
9+
10+ import java .util .HashMap ;
11+ import java .util .Map ;
12+
13+ /**
14+ * The InteractiveDisplayModule allows you to display entities when the mouse is over an entity or when an entity is
15+ * clicked.
16+ */
17+ @ Singleton
18+ public class InteractiveDisplayModule implements Module {
19+ public static final String BOTH = "B" ;
20+ public static final String HOVER_ONLY = "H" ;
21+ public static final String CLICK_ONLY = "C" ;
22+
23+ private static final String DISPLAY = "D" ;
24+ private static final String RESIZE = "R" ;
25+
26+ GameManager <AbstractPlayer > gameManager ;
27+ Map <Integer , Map <Integer , String >> newRegistration ;
28+ Map <Integer , Map <Integer , String >> registration ;
29+
30+
31+ @ Inject
32+ InteractiveDisplayModule (GameManager <AbstractPlayer > gameManager ) {
33+ this .gameManager = gameManager ;
34+ gameManager .registerModule (this );
35+ newRegistration = new HashMap <>();
36+ registration = new HashMap <>();
37+ }
38+
39+ @ Override
40+ public void onGameInit () {
41+ sendFrameData ();
42+ }
43+
44+ @ Override
45+ public void onAfterGameTurn () {
46+ sendFrameData ();
47+ }
48+
49+ @ Override
50+ public void onAfterOnEnd () {
51+ }
52+
53+ private void sendFrameData () {
54+ if (!newRegistration .isEmpty ()) {
55+ Object data = new HashMap []{new HashMap <>(newRegistration )};
56+ newRegistration .clear ();
57+ gameManager .setViewData ("intDisplay" , data );
58+ }
59+ }
60+
61+
62+ /**
63+ * Make <code>displayEntities</code> appear when the mouse is over <code>entity</code>.
64+ *
65+ * @param entity the entity to track
66+ * @param displayEntity the entity to display when the mouse is over <code>entity</code>
67+ * @param mode when the displayEntity has to be displayed (HOVER_ONLY, CLICK_ONLY or BOTH)
68+ */
69+ public void addDisplay (Entity <?> entity , Entity <?> displayEntity , String mode ) {
70+ Map <Integer , String > displays = registration .getOrDefault (entity .getId (), new HashMap <>());
71+ displays .put (displayEntity .getId (), DISPLAY + "," + mode );
72+ registration .put (entity .getId (), displays );
73+ newRegistration .put (entity .getId (), displays );
74+ }
75+
76+ /**
77+ * Make <code>displayEntities</code> appear when the mouse is over <code>entity</code>.
78+ *
79+ * @param entity the entity to track
80+ * @param displayEntity the entity to display when the mouse is over <code>entity</code>
81+ */
82+ public void addDisplay (Entity <?> entity , Entity <?> displayEntity ) {
83+ addDisplay (entity , displayEntity , BOTH );
84+ }
85+
86+
87+ /**
88+ * Stop displaying/resizing entities when <code>entity</code> is hovered/clicked
89+ *
90+ * @param entity the entity to stop tracking
91+ */
92+ public void untrack (Entity <?> entity ) {
93+ newRegistration .put (entity .getId (), new HashMap <>());
94+ }
95+
96+ /**
97+ * Stop transforming associatedEntity when entity is clicked/Hovered
98+ *
99+ * @param entity the interactive entity
100+ * @param associatedEntity the entity that won't be transformed anymore
101+ */
102+ public void removeTransformation (Entity <?> entity , Entity <?> associatedEntity ) {
103+ Map <Integer , String > displays = registration .getOrDefault (entity .getId (), new HashMap <>());
104+ if (displays .remove (associatedEntity .getId ()) != null ) {
105+ newRegistration .put (entity .getId (), displays );
106+ }
107+ }
108+
109+
110+ /**
111+ * Make <code>associatedEntity</code> bigger when <code>entity</code> is hovered/clicked depending on <code>mode</code>
112+ *
113+ * @param entity the entity to track
114+ * @param associatedEntity the entity to resize
115+ * @param factor the factor by which the associatedEntity has to be resized
116+ * @param mode when the associatedEntity has to be resized (HOVER_ONLY, CLICK_ONLY or BOTH)
117+ */
118+ public void addResize (Entity <?> entity , Entity <?> associatedEntity , double factor , String mode ) {
119+ Map <Integer , String > resizes = registration .getOrDefault (entity .getId (), new HashMap <>());
120+ resizes .put (associatedEntity .getId (), RESIZE + "," + mode + "," + factor );
121+ registration .put (entity .getId (), resizes );
122+ newRegistration .put (entity .getId (), resizes );
123+ }
124+
125+ /**
126+ * Make <code>associatedEntity</code> <code>factor</code> times bigger when <code>entity</code> is hovered or clicked
127+ *
128+ * @param entity the entity to track
129+ * @param associatedEntity the entity to resize
130+ * @param factor the factor by which the associatedEntities have to be resized
131+ */
132+ public void addResize (Entity <?> entity , Entity <?> associatedEntity , double factor ) {
133+ addResize (entity , associatedEntity , factor , BOTH );
134+ }
135+
136+ /**
137+ * Make <code>entity</code> <code>factor</code> times bigger when it is hovered or clicked depending on <code>mode</code>
138+ *
139+ * @param entity the entity to resize
140+ * @param mode when the entity has to be resized (HOVER_ONLY, CLICK_ONLY or BOTH)
141+ */
142+ public void addResize (Entity <?> entity , double factor , String mode ) {
143+ addResize (entity , entity , factor , mode );
144+ }
145+
146+ /**
147+ * Make <code>entity</code> <code>factor</code> times bigger when it is hovered or clicked
148+ *
149+ * @param entity the entity to resize
150+ */
151+ public void addResize (Entity <?> entity , double factor ) {
152+ addResize (entity , entity , factor , BOTH );
153+ }
154+
155+
156+ }
0 commit comments