internal bool DoClickableText(IMGUIPass cardCreationPass, Input input, string text, Color textColor) { bool result = false; int horisontalPadding = 5; int buttonWidth = (int)Font.MeasureString(text).X + 2 * horisontalPadding; Rectangle buttonRectangle = new Rectangle((int)Position.X, (int)Position.Y + ScrollValue, buttonWidth, Font.LineSpacing); int rightmostX = buttonRectangle.Right; if (IsScrollArea && rightmostX >= RightmostScrollX) { RightmostScrollX = rightmostX; } switch (cardCreationPass) { case IMGUIPass.Draw: { Vector2 textPosition = new Vector2(buttonRectangle.Left + horisontalPadding, buttonRectangle.Top); Renderer.DrawString(Font, text, textPosition, textColor); if (Game1.PointInRectangle(input.MousePosition, buttonRectangle)) { Renderer.DrawRectangleOutline(2, Game1.flareHighlightColor, buttonRectangle); } if (!IsScrollArea) { Renderer.DrawRectangle(buttonRectangle, Game1.tileColor); } } break; case IMGUIPass.Update: { if (input.LeftMouseDisengaged() && Game1.PointInRectangle(input.MousePosition, buttonRectangle)) { result = true; } } break; } Position.X += buttonWidth; return(result); }
public void UpdateAndDraw(GameTime gameTime, IMGUIPass pass) { // RESET List <Keys> keysConsumed = new List <Keys>(); if (input.KeyPressed(Keys.R) && (input.KeyboardState.IsKeyDown(Keys.LeftControl) || input.KeyboardState.IsKeyDown(Keys.RightControl))) { keysConsumed.Add(Keys.R); network.SendResetMessage(); Reset(); } // CARD CREATION bool mouseActionConsumed = false; Panel panel = new Panel(new Vector2(5, 0), renderer); panel.Font = font; int start = (int)'A'; int end = (int)'Z'; for (int i = start; i <= end; i++) { if (panel.DoClickableText(pass, input, ((char)i).ToString())) { shownLetter = (char)i; mouseActionConsumed = true; } } if (panel.DoClickableText(pass, input, "*")) { shownLetter = '*'; mouseActionConsumed = true; } if (panel.DoClickableText(pass, input, "C")) { SpawnCounterAndSendNetworkMessage(); mouseActionConsumed = true; } // TYPED CARD RETREIVAL if (pass == IMGUIPass.Update) { if (lastKeyboardState != null) { for (int i = 65; i <= 90; i++) { Keys key = (Keys)i; if (!keysConsumed.Contains(key) && keyboardState.IsKeyDown(key) && lastKeyboardState.IsKeyUp(key)) { int keyInt = i; if (!keyboardState.IsKeyDown(Keys.LeftShift) && !keyboardState.IsKeyDown(Keys.RightShift)) { keyInt += 32; } searchText += (char)keyInt; searchTextColor = textColor; } } if (keyboardState.IsKeyDown(Keys.Space) && lastKeyboardState.IsKeyUp(Keys.Space)) { searchText += " "; } if (keyboardState.IsKeyDown(Keys.OemComma) && lastKeyboardState.IsKeyUp(Keys.OemComma)) { searchText += ","; } if (keyboardState.IsKeyDown(Keys.OemMinus) && lastKeyboardState.IsKeyUp(Keys.OemMinus)) { searchText += "-"; } if (keyboardState.IsKeyDown(Keys.OemQuotes) && lastKeyboardState.IsKeyUp(Keys.OemQuotes)) { searchText += "'"; } if (searchText.Length > 0 && keyboardState.IsKeyDown(Keys.Back) && lastKeyboardState.IsKeyUp(Keys.Back)) { searchText = searchText.Substring(0, searchText.Length - 1); } if (keyboardState.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Enter)) { if (!TryGetFile(searchText)) { searchTextColor = Color.Red; } else { searchText = ""; } } } } panel.DoText(pass, searchText, searchTextColor); panel.Row(); if (input.RightMouseEngaged() && shownLetter != (char)0) { shownLetter = (char)0; mouseActionConsumed = true; } if (shownLetter != (char)0) { List <CardData> cardsToShow; if (shownLetter == '*') { cardsToShow = cardData.Where(x => x.Starred).ToList(); } else { cardsToShow = cardData.Where(x => x.Name.ToUpper()[0] == shownLetter).ToList(); } panel.VerticalScroll(pass, cardSelectScrollValue); foreach (CardData card in cardsToShow) { Color starColor = Color.White; if (!card.Starred) { starColor = Color.Black; } if (panel.DoClickableText(pass, input, "*", starColor)) { card.Starred = !card.Starred; SaveStarredCards(); } if (panel.DoClickableText(pass, input, card.Name)) { SpawnCardAndSendNetworkMessage(card.Name, card.TexturePath); shownLetter = (char)0; mouseActionConsumed = true; cardSelectScrollValue = 0; } panel.Row(); } cardSelectScrollValue = panel.EndScroll(pass, input, cardSelectScrollValue); } // MANA BOXES if (!mouseActionConsumed || pass == IMGUIPass.Draw) { int manaBoxIndex_TopAvaliable = 0; int manaBoxIndex_TopUsed = 1; int manaBoxIndex_BottomAvaliable = 2; int manaBoxIndex_BottomUsed = 3; int manaBoxWidth = manaBoxWidthInMana * manaTextureSize; int totalManaBoxWidth = 2 * manaBoxWidth + manaTextureSize; int totalManaBoxHeight = manaTextureSize * manaBoxHeightInMana; float startX = Graphics.PreferredBackBufferWidth - totalManaBoxWidth - manaTextureSize; Vector2[] refreshButtonPositions = new Vector2[2] { new Vector2(startX, 0), new Vector2(startX, Graphics.PreferredBackBufferHeight - totalManaBoxHeight) }; int[] avaliableManaIndex = new int[2] { manaBoxIndex_TopAvaliable, manaBoxIndex_BottomAvaliable }; int[] usedManaIndex = new int[2] { manaBoxIndex_TopUsed, manaBoxIndex_BottomUsed }; for (int i = 0; i <= 1; i++) { // Refresh Button Rectangle refreshButtonTextureRectangle = new Rectangle((int)refreshButtonPositions[i].X, (int)refreshButtonPositions[i].Y, (int)manaTextureSize, (int)manaTextureSize); switch (pass) { case IMGUIPass.Draw: Texture2D refreshManaTexture = Content.Load <Texture2D>("Mana\\refresh.png"); renderer.Draw(refreshManaTexture, refreshButtonTextureRectangle, Color.White); break; case IMGUIPass.Update: if (input.LeftMouseDisengaged() && Game1.PointInRectangle(input.MousePosition, refreshButtonTextureRectangle)) { RefreshMana(avaliableManaIndex[i]); mouseActionConsumed = true; network.SendRefreshMana(avaliableManaIndex[i]); } break; } Vector2 availiableManaBoxPosition = refreshButtonPositions[i] + new Vector2(manaTextureSize, 0); Vector2 usedManaBoxPosition = availiableManaBoxPosition + new Vector2(manaBoxWidth, 0); Vector2[] manaBoxPosition = new Vector2[2] { availiableManaBoxPosition, usedManaBoxPosition }; int[] myManaIndex = new int[2] { avaliableManaIndex[i], usedManaIndex[i] }; int[] swapManaIndex = new int[2] { usedManaIndex[i], avaliableManaIndex[i] }; Color[] textureColor = new Color[2] { Color.White, Color.Gray }; for (int j = 0; j <= 1; j++) { Vector2 currentPosition = new Vector2(manaBoxPosition[j].X, manaBoxPosition[j].Y); int column = 0; for (int manaType = 0; manaType < 5; manaType++) { Texture2D manaTexture = Content.Load <Texture2D>(manaTextures[manaType]); for (int mana = 0; mana < manaBoxes[myManaIndex[j], manaType]; mana++) { Rectangle manaTextureRectangle = new Rectangle((int)currentPosition.X, (int)currentPosition.Y, (int)manaTextureSize, (int)manaTextureSize); switch (pass) { case IMGUIPass.Draw: renderer.Draw(manaTexture, manaTextureRectangle, textureColor[j]); break; case IMGUIPass.Update: if (Game1.PointInRectangle(input.MousePosition, manaTextureRectangle)) { if (input.LeftMouseDisengaged()) { manaBoxes[myManaIndex[j], manaType]--; manaBoxes[swapManaIndex[j], manaType]++; mouseActionConsumed = true; network.SendTransferMana(myManaIndex[j], swapManaIndex[j], manaType); } else if (input.RightMouseDisengaged()) { manaBoxes[myManaIndex[j], manaType]--; mouseActionConsumed = true; network.SendRemoveMana(myManaIndex[j], manaType); } } break; } currentPosition.X += manaTextureSize; column++; if (column >= manaBoxWidthInMana) { column = 0; currentPosition.X = manaBoxPosition[j].X; currentPosition.Y += manaTextureSize; } } } } // Add Mana Buttons Vector2 addManaBoxPosition = usedManaBoxPosition + new Vector2(manaBoxWidth, 0); for (int manaType = 0; manaType < manaTextures.Count; manaType++) { Rectangle manaTextureRectangle = new Rectangle((int)addManaBoxPosition.X, (int)addManaBoxPosition.Y + manaType * manaTextureSize, (int)manaTextureSize, (int)manaTextureSize); switch (pass) { case IMGUIPass.Draw: Texture2D manaTexture = Content.Load <Texture2D>(manaTextures[manaType]); renderer.Draw(manaTexture, manaTextureRectangle, Color.White); break; case IMGUIPass.Update: if (input.LeftMouseDisengaged() && Game1.PointInRectangle(input.MousePosition, manaTextureRectangle)) { manaBoxes[avaliableManaIndex[i], manaType]++; mouseActionConsumed = true; network.SendCreateMana(avaliableManaIndex[i], manaType); } break; } } // Blue Lines if (pass == IMGUIPass.Draw) { Rectangle manaBoxRectangle = new Rectangle((int)availiableManaBoxPosition.X, (int)availiableManaBoxPosition.Y, totalManaBoxWidth, totalManaBoxHeight); renderer.DrawRectangleOutline(2, flareColor, manaBoxRectangle); renderer.DrawLine(2, flareColor, usedManaBoxPosition, usedManaBoxPosition + new Vector2(0, totalManaBoxHeight)); renderer.DrawLine(2, flareColor, addManaBoxPosition, addManaBoxPosition + new Vector2(0, totalManaBoxHeight)); renderer.DrawRectangle(manaBoxRectangle, tileColor); } } } // CARDS AND COUNTERS // Move And Drop Drag Target if (pass == IMGUIPass.Update && !mouseActionConsumed) { if (dragTarget != null) { dragTarget.Position += input.MousePosition - input.LastMousePosition; if (lastDragMessageSentSeconds > 0.2) { network.SendEntityMovedMessage(dragTarget.NetworkID, dragTarget.Position); lastDragMessageSentSeconds = 0; } if (input.LeftMouseDisengaged()) { network.SendEntityMovedMessage(dragTarget.NetworkID, dragTarget.Position); lastDragMessageSentSeconds = 0; dragTarget = null; mouseActionConsumed = true; } } } Entities.Sort(new EntityComparerReverse()); foreach (Entity entity in Entities) { switch (entity.Type) { case EntityType.Card: if (pass == IMGUIPass.Draw) { Card card = (Card)entity.TypeSpecificClass; float rotation = 0; if (card.Tapped) { rotation = (float)Math.PI / 2f; } renderer.Draw(card.Texture, entity.Position, Color.White, rotation, card.GetHalf()); } else // Update Pass { // Start Drag if (input.LeftMouseEngaged() && dragTarget == null && !mouseActionConsumed) { if (PointInRectangle(input.MousePosition, entity.GetBounds())) { dragTarget = entity; dragTarget.Depth = GetNextHeighestDepth(); mouseActionConsumed = true; } } // Tap if (input.RightMouseDisengaged() && !mouseActionConsumed) { if (PointInRectangle(input.MousePosition, entity.GetBounds())) { Card card = (Card)entity.TypeSpecificClass; card.Tapped = !card.Tapped; network.SendEntityTappedMessage(entity.NetworkID); mouseActionConsumed = true; } } } break; case EntityType.Counter: Counter counter = (Counter)entity.TypeSpecificClass; if (pass == IMGUIPass.Draw) { Vector2 topArrowPoint = entity.Position + new Vector2(Counter.TextAreaWidth / 2f, 0); Vector2 bottomArrowPoint = topArrowPoint + new Vector2(0, 2 * Counter.Buttonheight + Counter.TextAreaHeight); Rectangle textAreaRectangle = new Rectangle((int)entity.Position.X, (int)entity.Position.Y + Counter.Buttonheight, Counter.TextAreaWidth, Counter.TextAreaHeight); // Top arrow renderer.DrawLine(1, flareColor, new Vector2(textAreaRectangle.Left, textAreaRectangle.Top), topArrowPoint); renderer.DrawLine(1, flareColor, topArrowPoint, new Vector2(textAreaRectangle.Right, textAreaRectangle.Top)); // Bottom arrow renderer.DrawLine(1, flareColor, new Vector2(textAreaRectangle.Left, textAreaRectangle.Bottom), bottomArrowPoint); renderer.DrawLine(1, flareColor, bottomArrowPoint, new Vector2(textAreaRectangle.Right, textAreaRectangle.Bottom)); string text = counter.Value.ToString(); SpriteFont fontToUse = font; Vector2 centerOffset = (new Vector2(Counter.TextAreaWidth, Counter.TextAreaHeight) - fontToUse.MeasureString(text)) / 2f; renderer.DrawString(fontToUse, text, new Vector2(textAreaRectangle.Left, textAreaRectangle.Top) + centerOffset, textColor); renderer.DrawRectangle(textAreaRectangle, Game1.tileColor); } else // Update Pass { if (dragTarget == null && !mouseActionConsumed) { if (input.LeftMouseEngaged()) { if (PointInRectangle(input.MousePosition, new Rectangle((int)entity.Position.X, (int)entity.Position.Y + Counter.Buttonheight, Counter.TextAreaHeight, Counter.TextAreaHeight))) { dragTarget = entity; dragTarget.Depth = GetNextHeighestDepth(); mouseActionConsumed = true; } } else if (input.LeftMouseDisengaged()) { Vector2 startOfBottomTriangle = entity.Position + new Vector2(0, Counter.Buttonheight + Counter.TextAreaHeight); int counterChange = 0; if (PointInTriangle(input.MousePosition, entity.Position + new Vector2(0, Counter.Buttonheight), entity.Position + new Vector2(Counter.TextAreaWidth / 2, 0), entity.Position + new Vector2(Counter.TextAreaWidth, Counter.Buttonheight))) { counterChange = 1; } else if (PointInTriangle(input.MousePosition, startOfBottomTriangle + new Vector2(0, 0), startOfBottomTriangle + new Vector2(Counter.TextAreaWidth / 2, Counter.Buttonheight), startOfBottomTriangle + new Vector2(Counter.TextAreaWidth, 0))) { counterChange = -1; } if (counterChange != 0) { counter.Value += counterChange; network.SendCounterChangeMessage(entity.NetworkID, counter.Value); mouseActionConsumed = true; } } } } break; } } }