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.

202 lines
9.0 KiB

  1. [Go back to tutorial home](tutorial.md)
  2. # Step 2: customize icon and content
  3. *(Estimated time: 30mn)*
  4. Let's now improve our activity to customize the content.
  5. ### Customize the icon
  6. The first task is to customize the icon. It's important because it's the visual identity of your activity.
  7. For our new activity, we'll take a Pawn icon inspired by Andrejs Kirma from The Noun Project. You could download this file [here](images/pawn-icon.svg). Right-click on it and then save it in a `pawn-icon.svg` file into a new `Pawn.activity/icons` directory.
  8. ![](images/tutorial_step2_1.png)
  9. In Sugarizer, images for icons or buttons must be SVG graphic files. It needs to be adapted to support users' colors because as you could see on the home page, all icons in Sugarizer are dynamically adapted to users' colors.
  10. To do that, we need to update our original SVG file.
  11. Here's an extract on our original `icons/pawn-icon.svg` file:
  12. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  13. <svg
  14. xmlns:dc="http://purl.org/dc/elements/1.1/"
  15. xmlns:cc="http://creativecommons.org/ns#"
  16. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  17. xmlns:svg="http://www.w3.org/2000/svg"
  18. xmlns="http://www.w3.org/2000/svg"
  19. xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/ sodipodi-0.dtd"
  20. xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  21. width="55pt"
  22. height="55pt"
  23. version="1.1"
  24. viewBox="0 0 55 55"
  25. id="svg2"
  26. inkscape:version="0.91 r13725"
  27. sodipodi:docname="pawn-icon.svg">
  28. ...
  29. <g
  30. id="g4"
  31. style="fill:#808080;fill-rule:evenodd" transform="matrix(0.39971901,0,0,0.39971901,6.4164118,6.3421946)">
  32. <path
  33. style="fill:#808080"
  34. d="M 69.117,19.117 C 69.419097,8.5405271 59.563829,-0.82065091 49.016209,0.02487387 38.442487,0.26964532 29.602814,10.583179 30.981695,21.071762 31.767586,31.615772 42.507095,39.916726 52.911417,38.013738 62.017401,36.775538 69.289121,28.312864 69.117,19.117 Z m -32.352,0 C 36.487927,11.039319 44.80784,4.2563204 52.665901,6.151898 60.600506,7.5124622 65.573683,16.956696 62.192903,24.268128 59.309957,31.766263 49.109454,34.806773 42.600068,30.090646 38.995267,27.691276 36.726564,23.451719 36.765,19.117 Z"
  35. id="path6"
  36. inkscape:connector-curvature="0" />
  37. ...
  38. </g>
  39. <g
  40. id="g4230"
  41. style="fill:#f0f0f0;stroke:#f0f0f0"
  42. transform="matrix(0.39971901,0,0,0.39971901,6.9463181,6.2146783)">
  43. ...
  44. </g>
  45. </svg>
  46. To adapt this SVG file to Sugarizer, we will update the start of the file to add ENTITY variables named **stroke_color** and **fill_color**.
  47. Then we will replace raw colors value `#xxxxxx` in the SVG file, by reference to `&fill_color;` and `&stroke_color;`.
  48. Here, using your text editor you must replace all `#808080` by `&fill_color;` and all `#f0f0f0` by `&stroke_color;`.
  49. Here is an extract of the result:
  50. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  51. <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' [
  52. <!ENTITY stroke_color "#010101">
  53. <!ENTITY fill_color "#FFFFFF">
  54. ]>
  55. <svg
  56. xmlns:dc="http://purl.org/dc/elements/1.1/"
  57. xmlns:cc="http://creativecommons.org/ns#"
  58. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  59. xmlns:svg="http://www.w3.org/2000/svg"
  60. xmlns="http://www.w3.org/2000/svg"
  61. xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/ sodipodi-0.dtd"
  62. xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
  63. width="55pt"
  64. height="55pt"
  65. version="1.1"
  66. viewBox="0 0 55 55"
  67. id="svg2"
  68. inkscape:version="0.91 r13725"
  69. sodipodi:docname="pawn-icon.svg">
  70. ...
  71. <g
  72. id="g4"
  73. style="fill:&fill_color;;fill-rule:evenodd" transform="matrix(0.39971901,0,0,0.39971901,6.4164118,6.3421946)">
  74. <path
  75. style="fill:&fill_color;"
  76. d="M 69.117,19.117 C 69.419097,8.5405271 59.563829,-0.82065091 49.016209,0.02487387 38.442487,0.26964532 29.602814,10.583179 30.981695,21.071762 31.767586,31.615772 42.507095,39.916726 52.911417,38.013738 62.017401,36.775538 69.289121,28.312864 69.117,19.117 Z m -32.352,0 C 36.487927,11.039319 44.80784,4.2563204 52.665901,6.151898 60.600506,7.5124622 65.573683,16.956696 62.192903,24.268128 59.309957,31.766263 49.109454,34.806773 42.600068,30.090646 38.995267,27.691276 36.726564,23.451719 36.765,19.117 Z"
  77. id="path6"
  78. inkscape:connector-curvature="0" />
  79. ...
  80. </g>
  81. <g
  82. id="g4230"
  83. style="fill:&stroke_color;;stroke:&stroke_color;"
  84. transform="matrix(0.39971901,0,0,0.39971901,6.9463181,6.2146783)">
  85. ...
  86. </g>
  87. </svg>
  88. To update the activity icon, replace also the content of `activity/activity-icon.svg` by the content of the `pawn-icon.svg` file.
  89. Let's run again our activity. Now, we have a beautiful pawn icon.
  90. ![](images/tutorial_step2_2.png)
  91. Note that a Sugar icon set is available in `lib/sugar-web/graphics/icons/`. You could also find more information about how to create icons for Sugar [here](https://wiki.sugarlabs.org/go/Development_Team/Almanac/Making_Icons).
  92. ### Customize content
  93. We will now change the content of our Window. More precisely, we're going to replace the **Congratulations** message by a welcome message to the user connected.
  94. Let's first remove the default message, you could find it in `index.html` file:
  95. <h1>Congratulations!</h1>
  96. <p>You are ready to develop this activity. Go ahead and edit the files.<br />
  97. You can remove this message in the index.html.</p>
  98. Replace these few lines by a single line:
  99. <div id="user"></div>
  100. We need only a single `div` because we will generate the welcome message dynamically in JavaScript.
  101. To do that, let's study the file `js/activity.js`. It's really the heart of your new activity. The current implementation is:
  102. define(["sugar-web/activity/activity"], function (activity) {
  103. // Manipulate the DOM only when it is ready.
  104. requirejs(['domReady!'], function (doc) {
  105. // Initialize the activity.
  106. activity.setup();
  107. });
  108. });
  109. These lines rely on the framework **require.js** that is used by Sugar-Web to handle JavaScript libraries dependancies. You could read more about the **require.js** framework [here](http://www.requirejs.org/) but shortly, there is only two functions to understand: `define` and `require`.
  110. * `define` is a way to define a new module and express its dependencies. Here for example we're going to define a new module that depends on the JavaScript library `sugar-web/activity/activity`. So when the `js/activity.js` is run, **require** will first load the Sugar-Web activity library and put a reference on it in the `activity` variable.
  111. * `requirejs` is pretty the same. It tells to **require**: run the following function but before that, load dependencies and give me a reference to it. There is small hack here because `domReady!` is a special library used to wait for the end of the HTML page loading.
  112. Then comes the most important line of our activity:
  113. // Initialize the activity.
  114. activity.setup();
  115. It's a call to the `setup` method of the Sugar-Web activity library.
  116. If you have to keep only one line in your activity, keep that one because it's responsible for all the magic inside Sugarizer: it initializes Datastore, Presence and the Sugarizer UI. A classical error for a beginner in Sugar-Web development is to forget this call. Let's do it by commenting the line:
  117. // Initialize the activity.
  118. //activity.setup();
  119. Then run again your new activity. Here's what happens:
  120. ![](images/tutorial_step2_3.png)
  121. Ooops! Colors for our nice icons has disappeared and when you click on the Stop button nothing happens. Clearly you've broken the Sugarizer logic :-(
  122. So uncomment this precious line and never forget to call it again!
  123. // Initialize the activity.
  124. activity.setup();
  125. Now, to display our welcome message, we will use the user's name.
  126. To do that we're going to use another Sugar-Web library named **env**. So, we need to add it in our dependencies. Update the define call to add the env Sugar-Web library:
  127. define(["sugar-web/activity/activity", "sugar-web/env"], function (activity, env) {
  128. This library contains a very interesting method `getEnvironment`. This method allow you to retrieve all users settings: name, prefered colours, language, favorites, ...
  129. So add a call to this method to retrieve the user name:
  130. // Initialize the activity.
  131. activity.setup();
  132. // Welcome user
  133. env.getEnvironment(function(err, environment) {
  134. document.getElementById("user").innerHTML = "<h1>"+"Hello"+" "+environment.user.name+" !</h1>";
  135. });
  136. When you call the getEnvironment method, it should load the Sugarizer environment then call your function with a JavaScript object environment that contains context of your activity and user settings. So we could display our welcome message by forcing HTML in the `div` object using `environment.user.name`. Here's the line:
  137. document.getElementById("user").innerHTML = "<h1>"+"Hello"+" "+environment.user.name+" !</h1>";
  138. That's all. Run the activity again.
  139. Congratulations! The welcome message will now appear:
  140. ![](images/tutorial_step2_4.png)
  141. [Go to next step](tutorial_step3.md)