Exemple #1
0
        public static GuiEvent FromGuiElement(GuiElement sender)
        {
            GuiEvent returnEvent = new GuiEvent((int)sender.GetPosition().x, (int)sender.GetPosition().y);

            returnEvent.clickedElement = sender;
            returnEvent.clickedTiles = sender.GetParentLayer().GetTilesAt(returnEvent.eventPos.x, returnEvent.eventPos.y);
            //presume no entities and tiles...?

            return returnEvent;
        }
        public void RenderGUIElement(GuiElement gelm)
        {
            HtmlElement guiElem = null;
            if (elementsByGuiId.ContainsKey(gelm.GetId()))
            {
                guiElem = elementsByGuiId[gelm.GetId()].As<HtmlElement>();
            }

            //if it's not rendered, render it
            if (guiElem == null)
            {
                guiElem = document.createElement("div").As<HtmlElement>();
                this.AddClass(guiElem, "GUIElement");
                //document.getElementById("gameBoard").appendChild(gentlement);
                this.elementsByGuiId[gelm.GetParentLayer().GetId()].appendChild(guiElem);
                //!! Basing this on size 12 font right now, and it's going to be totally wrong, and needs to be done up properly
                if (gelm.GetText() != "" && gelm.GetSprite() == null)
                {
                    guiElem.style.width = (gelm.GetText().Length * 12) + "px";
                    guiElem.style.height = 12 + "px";
                    guiElem.innerHTML = gelm.GetText();
                }
                else
                {
                    if (gelm.GetSize().width != 0 && gelm.GetSize().height != 0)
                    {
                        guiElem.style.width = gelm.GetSize().width + " px";
                        guiElem.style.height = gelm.GetSize().height + "px";
                    }

                    //Debug.log("Setting width to " + gelm.GetSize().width + ". Width is " + gentlement.style.width);
                    //Debug.log("Setting height to " + gelm.GetSize().height + ". Height is " + gentlement.style.height);
                }

                //since we're building this for the first time, put all the classes into a long list, to cut down on operations
                string styleString = "";
                foreach (string style in gelm.GetStyles())
                {
                    styleString += style + " ";
                }
                //styleString = styleString.TrimEnd();
                this.AddClass(guiElem, styleString);

                elementsByGuiId[gelm.GetId()] = guiElem;
            }

            //update the element with new position information if need be
            if (this.guiElementsToUpdate.Contains(gelm.GetId()))
            {
                guiElem.innerHTML = gelm.GetText();

                //reposition the element based on game position
                guiElem.style.left = gelm.GetPosition().x + "px";
                guiElem.style.top = gelm.GetPosition().y + "px";

                if (gelm.GetStyle("Background") != null)
                {
                    guiElem.style.background = gelm.GetStyle("Background");
                }

                this.guiElementsToUpdate.Remove(gelm.GetId());
            }

            if (gelm.GetSprite() == null)
            {
                return;
            }
            guiElem.innerHTML = "";

            //call the sprite's animate function
            //it will return the id of the frame that the sprite and its animation are currently on
            string frameId = gelm.GetSprite().Animate();

            //if that result is different from the value we have stored as this GameEntity's current display frame,
            if (frameId != gelm.GetSprite().CurrentRenderFrame)
            {
                //update this thing's CSS to the new frame
                //jQueryObject GameEntityDiv = jQuery.FromElement(GameEntityDiv);
                this.RemoveClass(guiElem, gelm.GetSprite().CurrentRenderFrame);
                this.AddClass(guiElem, frameId);
                //and update our current frameid
                gelm.GetSprite().CurrentRenderFrame = frameId;

                guiElem.style.width = gelm.GetSprite().Size.width + "px";
                guiElem.style.height = gelm.GetSprite().Size.height + "px";
            }
        }