<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>clickable | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/clickable/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Sun, 09 Aug 2020 10:30:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>clickable | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>MonoGame &#8211; How to make a clickable button</title>
		<link>https://learncgames.com/monogame-how-to-make-a-clickable-button/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=monogame-how-to-make-a-clickable-button</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 14 Aug 2020 23:00:25 +0000</pubDate>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[MonoGame]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[clickable]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1169</guid>

					<description><![CDATA[<p>I&#8217;m using this in Android games but the principle applies to any MonoGame game. Here clickable and touchable mean the same. You need three things to make something clickable. The area. For simplicity sake this will always be rectangular. The Action. When you touch (or click it), you want it to run code. An id [&#8230;]</p>
The post <a href="https://learncgames.com/monogame-how-to-make-a-clickable-button/">MonoGame – How to make a clickable button</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img fetchpriority="high" decoding="async" class="alignleft size-full wp-image-1171" src="https://learncgames.com/wp-content/uploads/2020/08/clearButton.png" alt="Clear Button with one card clicked" width="477" height="513" srcset="https://learncgames.com/wp-content/uploads/2020/08/clearButton.png 477w, https://learncgames.com/wp-content/uploads/2020/08/clearButton-279x300.png 279w" sizes="(max-width: 477px) 100vw, 477px" />I&#8217;m using this in Android games but the principle applies to any MonoGame game. Here clickable and touchable mean the same.</p>
<p>You need three things to make something clickable.</p>
<ol>
<li>The area. For simplicity sake this will always be rectangular.</li>
<li>The Action. When you touch (or click it), you want it to run code.</li>
<li>An id of sorts. If you have multiple buttons etc, you meed to distinguish them.</li>
</ol>
<p>We&#8217;ll use an int for 3. Use const ints to give it a name rather than being a magic number.</p>
<p>&nbsp;</p>
<p>When you click whatever, the Event should include the ide so let&#8217;s define the event Type first.</p>
<pre><code class="language-csharp" data-line="">public class ButtonClickedEventArgs : EventArgs
{
    public int Id { get; set; }
}</code></pre>
<p>Not rocket science is it!</p>
<p>Now lets define a ClickButton class.</p>
<pre><code class="language-csharp" data-line="">public class ClickButton
{
    public int Id { get; set; }
    public Rectangle TouchArea { get; set; }
    public Action&lt;object, ButtonClickedEventArgs&gt; AnAction { get; set; } = null;

    public ClickButton(int id)
    {
        Id = id;
        TouchArea = new Rectangle(0, 0, 0, 0);
    }
}</code></pre>
<p>Again nothing too complicated. There&#8217;s an int Id which which is set in the Constructor, a Rectangle TouchArea and an Action which is defined to include our ButtonClickedEventArgs.</p>
<p>Here;&#8217;s some example code. I&#8217;ve defined a graphic for a Clear button in my game and this is loaded as usual in the LoadContent().  I&#8217;ve also defined the button in Class declaration.</p>
<pre>ClickButton ClearButton;</pre>
<p>In LoadControl(), this is also initialised. Here&#8217;s it&#8217;s two line. It could be done in one, by altering the constuctor.</p>
<pre><code class="language-csharp" data-line="">ClearButton = new ClickButton(1);  // 1 is the id
ClearButton.AnAction += ClearCards;</code></pre>
<p>ClearCards is a method that does something when the button is cleared. This matches our Event handler as it has the usal two parameters for an Event except the args is our ButtonClickedEventArgs.</p>
<pre><code class="language-csharp" data-line="">private void ClearCards(object sender, ButtonClickedEventArgs args)  {
   // all the code here, not shown
}</code></pre>
<h2>Where is the TouchArea defined?</h2>
<p>So are we all setup ready to rock&#8217;n&#8217;roll? Well not quite. We haven&#8217;t said exactly where the TouchArea is. That of course depends on where we draw the button. In my game, the ClearButton moves across. It&#8217;s absent when all six cards are present but as soon as one has been touched and a back card touched to move it, the Clear Card appears. In the top image you can see it to the right of the five top cards. I had clicked the seven of clubs when it was in the top row then the first back on the top row of the three rows.</p>
<p>For my next move I clicked the ten of diamonds and the second back on that first row where the ten of diamonds no wit. It now looks like this and the Clear Button has moved over.  If I click the Clear button it will move the seven clubs and ten diamonds back to the top row and replace them in the first row with backs.</p>
<p><img decoding="async" class="alignleft size-full wp-image-1172" src="https://learncgames.com/wp-content/uploads/2020/08/ClearButtin2.png" alt="Second click" width="480" height="510" srcset="https://learncgames.com/wp-content/uploads/2020/08/ClearButtin2.png 480w, https://learncgames.com/wp-content/uploads/2020/08/ClearButtin2-282x300.png 282w" sizes="(max-width: 480px) 100vw, 480px" />To set the TouchArea of the ClearButton, I do it in the Draw method. It&#8217;s as simple as this:</p>
<pre><code class="language-csharp" data-line="">if (hand.Count &lt; 6) // Draw ClearButton on end
  {
    spriteBatch.Draw(clearButton, new Rectangle(x, y, cardWidth, cardHeight), 
        new Rectangle(0, 0, cardWidth, cardHeight), Color.White);
    ClearButton.TouchArea = new Rectangle(x, y, cardWidth, cardHeight);
  }</code></pre>
<p>There&#8217;s actually a small bug visible. When you click a top row card, the game highlights it by reducing its opacity to 0.5. If you look at the ten of diamonds, you can see it still has that reduced opacity; it should have been restored to 1.0 when it was moved to the first row. The same is true in the top picture for the seven clubs! Easily overlooked but just a one-line fix.</p>
<h2>Some Touchy Code</h2>
<p>The very last bit is to fire touch detection and this is done in the <strong>Update()</strong> method. When you touch the screen, calling <strong>Touchpanel.GetState()</strong> returns a collection of touches. This is my code. It cycles through the touches collection, gets the X, Y position of each touch and adjusts it to match the virtual screen coordinates. (<em>Drawing to a virtual screen means this looks the same on every Android irrespective of its physical screen sizes and resolution</em>).</p>
<pre><code class="language-csharp" data-line="">touchState = TouchPanel.GetState();
foreach (var touch in touchState)
{
   if (touch.State == TouchLocationState.Pressed)
    {
         var x = touch.Position.X / xRatio; // adjust for virtual screen
         var y = touch.Position.Y / yRatio;
         CheckClickedTop(x, y, hand);
         if (pickedCard == null) return; // no point checking further...
         for (var i = 0; i &lt; 3; i++)
            CheckClickedBottom(x, y, commonCards[i], true);
            if (hand.Count == 6) continue; // All top cards so no ClearButton visible
            // Check if Clear button
            if (ClearButton.TouchArea.Contains(x, y))
              {
                  ClearButton.AnAction(ClearButton, new ButtonClickedEventArgs() { Id = ClearButton.Id }); 
              }
      }
}</code></pre>
<p>The top cards are held in a class called hand and the bottom three rows are held in CommonCards[3] each of which is a hand. The CheckClicked.. methods cycle through the cards in the hand comparing coordinates to see which card if at all has been clicked. If it has it calls the AnAction Event for the one clicked.  The very last if shows how this works to check if the ClearButton was clicked. I&#8217;ll simplify this code by moving it into a ClickButton method.</p>The post <a href="https://learncgames.com/monogame-how-to-make-a-clickable-button/">MonoGame – How to make a clickable button</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1169</post-id>	</item>
	</channel>
</rss>
