not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
6.1 KiB

  1. [Go back to tutorial home](tutorial.md)
  2. # Step 3: add a toolbar button
  3. *(Estimated time: 30mn)*
  4. It's time now to implement the logic of our new activity.
  5. Because the activity is called "Pawn", the idea is to allow user to play pawns on the board.
  6. In Sugar, the toolbar - the black area at the top of the window - is the place where activities put actions for the user. So we will add a new button in the toolbar to let the user play a new pawn.
  7. ### Create a new toolbar button
  8. Here is the icon we will use. It's just a stylized plus sign.
  9. ![](images/tutorial_step3_1.png)
  10. You could download it [here](images/add.svg). Right-click on it and then save it as a SVG file in `icons/add.svg`.
  11. ***Warning***: *The plus sign is white on a transparent background so you will see nothing if you display it on a white background.*
  12. To add this icon in the toolbar, we will first update the `index.html` file for your activity. Look for the toolbar section in the file. It looks like this:
  13. <div id="main-toolbar" class="toolbar">
  14. <button class="toolbutton" id="activity-button" title="My Activity"></button>
  15. <!-- Add more buttons here -->
  16. <!-- Buttons with class="pull-right" will be right aligned -->
  17. <button class="toolbutton pull-right" id="stop-button" title="Stop"></button>
  18. </div>
  19. Let's add our new button. It's just a `button` tag with the Sugar-Web `toolbutton` class. We give it the value `add-button` for the `id` property. Here is the result:
  20. <div id="main-toolbar" class="toolbar">
  21. <button class="toolbutton" id="activity-button" title="My Activity"></button>
  22. <!-- Add more buttons here -->
  23. <button class="toolbutton" id="add-button" title="Add pawn"></button>
  24. <!-- Buttons with class="pull-right" will be right aligned -->
  25. <button class="toolbutton pull-right" id="stop-button" title="Stop"></button>
  26. </div>
  27. We will now associate the icon to this new button. This association should be done in the `css/activity.css` file. Add these lines at the end of the file.
  28. #main-toolbar #add-button {
  29. background-image: url(../icons/add.svg);
  30. }
  31. Let's run the activity.
  32. ![](images/tutorial_step3_2.png)
  33. The new toolbar button is now here. Click on it. Of course nothing happen thought there is no logic beside. It's our next step.
  34. ### Add the event
  35. To display pawns on the board, we will first update our `index.html` file.
  36. Add a new `div` tag below the one created for the welcome message:
  37. <div id="user"></div>
  38. <div id="pawns"></div>
  39. We give the value `pawns` for the attribute `id` because it will contain all pawns on the board.
  40. To draw the pawn we will reuse our nice pawn icon. So each time there will be a click on the Plus button, we will add a new pawn icon on the board.
  41. Let's update the `activity/activity.js` to add this. But first, we will slightly adapt our `getEnvironment` call. We just add a `currentenv` variable to store the environment to avoid multiple call of the `getEnvironment` method:
  42. // Welcome user
  43. var currentenv;
  44. env.getEnvironment(function(err, environment) {
  45. currentenv = environment;
  46. document.getElementById("user").innerHTML = "<h1>"+"Hello"+" "+environment.user.name+" !</h1>";
  47. });
  48. Then just after the `getEnvironment` call, we will add the following event listener:
  49. // Handle click on add
  50. document.getElementById("add-button").addEventListener('click', function (event) {
  51. var pawn = document.createElement("div");
  52. pawn.className = "pawn";
  53. document.getElementById("pawns").appendChild(pawn);
  54. document.getElementById("user").innerHTML = "<h1>"+currentenv.user.name+" played !</h1>";
  55. });
  56. This source code does three things:
  57. * Declare a new event listener to add an action on the click event for the HTML element with the `id` value `add-button`, i.e. our toolbar button
  58. * Create a new `div` element with the `class` value `pawn` and append this element as child of our `pawns` `div`
  59. * Update the welcome message to indicate that a new pawn has been played by the user
  60. Make sense? Yes, except that something is missing: the link between the `div` created for the pawn and the pawn icon itself. To do that we have to update the `css/activity.css` file. Add these lines at the end of the file:
  61. .pawn {
  62. background-image: url(../icons/pawn-icon.svg);
  63. display: inline-block;
  64. width: 65px;
  65. height: 65px;
  66. }
  67. It defined a new CSS class named `pawn`, give it few cosmetic properties and specifically our pawn icon as background.
  68. Can't wait to run again our activity:
  69. ![](images/tutorial_step3_3.png)
  70. Wow, it works! Each time we will click on the Plus icon a new pawn will appears.
  71. By the way, we could do better. What if we could change the pawn color?
  72. ### Customize again the icon
  73. Sugar activities very often rely on the user colors. It's a way to let the user think he could customize the color of activities. So, it's a good way to better engage him into the activity.
  74. We decide to update our activity to paint the pawn with the user color. You could think it's complex: it's not! Just a call to another Sugar-Web library named **icon**.
  75. Let's reopen our `activity/activity.js` file.
  76. We will first add the reference to this new library at the begining of the file. Update the first line with:
  77. define(["sugar-web/activity/activity", "sugar-web/env", "sugar-web/graphics/icon"], function (activity, env, icon) {
  78. We have now three dependancies: **activity**, **env** and **icon**.
  79. The icon library contains a magic method called `colorize`. Just give it a reference to a Sugar-Web icon and a color and it will transform the background to a colorize icon.
  80. Let's add a call to this magic method just after the `appendChild` call:
  81. document.getElementById("pawns").appendChild(pawn);
  82. icon.colorize(pawn, currentenv.user.colorvalue);
  83. Very simple, we call the method with two parameters: the new pawn element and the user color that we could find in the environment.
  84. That's all. Let's play again with our activity:
  85. ![](images/tutorial_step3_4.png)
  86. Beautiful isn't it?
  87. Note that if you're interested by how to integrate the Sugar UI in your activities, you could find more samples on this subject [here](http://sugarlabs.github.io/sugar-web-samples/).
  88. [Go to next step](tutorial_step4.md)